PageRenderTime 39ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/public/codeCore/php/functions.php

https://github.com/IAmCorbin/MooKit
PHP | 313 lines | 201 code | 14 blank | 98 comment | 32 complexity | f4d25548f9032634e8af4a9b1c6d24f7 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /**
  3. * Gloabl Core Functions
  4. * @package MooKit
  5. */
  6. /////////////////////////////////////////////////////////////////////
  7. //Database Information Retrieval Functions//
  8. /////////////////////////////////////////////////////////////////////
  9. /**
  10. * Search and retrieve user information from the database
  11. * @param string $rType the return type desired - if "rows" is passed it will build table rows from object
  12. * @param string $alias the user alias to search for
  13. * @return mixed users in desired rType
  14. */
  15. function sharedGetUsers($rType="object", $alias=NULL) {
  16. //build rows if requested and user is an administrator (this should only be done in the administrator panel)
  17. if($rType === "rows" && (Security::clearance() & ACCESS_ADMIN) ) {
  18. $users = User::get($alias,"object");
  19. $rows = '';
  20. foreach($users as $user) {
  21. $access_level = getHumanAccess($user->access_level);
  22. $rows .= "<tr>".
  23. "<td>$user->user_id</td>".
  24. "<td>$user->alias</td>".
  25. "<td>$user->nameFirst</td>".
  26. "<td>$user->nameLast</td>".
  27. "<td>$user->email</td>".
  28. "<td><span class=\"adminAccessDec\">-</span>&nbsp;&nbsp;&nbsp;<span>$user->access_level</span><span class=\"adminAccessInc\">+</span></td>".
  29. "<td>$access_level</td>".
  30. '<td class="adminDeleteUser">X</td>'.
  31. "</tr>";
  32. }
  33. return $rows;
  34. } else
  35. return User::get($alias,$rType);
  36. }
  37. /**
  38. * Administrator - Search and return found links from the database
  39. * @param string $rType the return type desired - if "rows" is passed it will build table rows from object
  40. * @param string $name the link name to search for
  41. * @param bool $menuLink switch to grab only menu links
  42. * @param bool $notSubs switch to turn off the sublink table join
  43. * @return mixed links in desired rType
  44. */
  45. function adminGetLinks($rType="object", $name='', $menuLink=FALSE, $notSubs=FALSE) {
  46. //build rows if requested
  47. if($rType === "rows") {
  48. //grab links and sublinks from the database
  49. $links = Link::get($name,$menuLink,"object",$notSubs,ACCESS_ADMIN);
  50. $lastLink_id = null;
  51. $rows = '';
  52. if(is_array($links))
  53. foreach($links as $link) {
  54. //avoid double display of links
  55. if($link->link_id != $lastLink_id) {
  56. $access_level = getHumanAccess($link->access_level);
  57. $rows .= "<tr>".
  58. "<td name=\"link_id\">$link->link_id</td>".
  59. "<td name=\"name\">$link->name</td>".
  60. "<td name=\"href\">$link->href</td>".
  61. "<td name=\"desc\">$link->desc</td>".
  62. "<td name=\"weight\">$link->weight</td>".
  63. "<td name=\"ajaxLink\">$link->ajaxLink</td>".
  64. "<td name=\"menuLink\">$link->menuLink</td>".
  65. "<td name=\"access_level\">$link->access_level</td>".
  66. "<td name=\"sublinks\">".
  67. //SubLinks Editing Table
  68. "<table class=\"subLinks\">".
  69. "<thead>".
  70. "<th style=\"display: none;\">id</th>".
  71. "<th>name</th>".
  72. "<th>href</th>".
  73. "<th>desc</th>".
  74. "</thead>".
  75. "<tbody>";
  76. foreach($links as $sublink) {
  77. if($link->link_id === $sublink->link_id && $sublink->sublink_id) {
  78. $rows.="<tr class=\"sublinkRow\">".
  79. "<td style=\"display: none;\">".$sublink->sublink_id."</td>".
  80. "<td>".$sublink->sub_name."</td>".
  81. "<td>".$sublink->sub_href."</td>".
  82. "<td>".$sublink->sub_desc."</td>".
  83. "</tr>";
  84. }
  85. }
  86. $rows.="</tbody>".
  87. "</table>".
  88. '<form class="adminAddSublink singleton"><input type="text" name="name" size="20" value="Add a Sublink" /></form>'.
  89. "</td>".
  90. '<td class="adminDeleteLink">X</td>'.
  91. "</tr>";
  92. }
  93. $lastLink_id=$link->link_id;
  94. }
  95. return $rows;
  96. } else
  97. //grab links and sublinks from the database
  98. return Link::get($name,$menuLink,$rType,$notSubs,ACCESS_ADMIN);
  99. }
  100. /**
  101. * Search and return found posts from the database
  102. * @param string $rType the return type desired - if "rows" is passed it will build table rows from object
  103. * @param string $title the post title to search for
  104. * @param int $post_id the post id to search for
  105. * @return mixed links in desired rType
  106. */
  107. function createGetPosts($rType="object", $title=NULL, $post_id=NULL) {
  108. //build rows if requested
  109. if($rType == "rows") {
  110. $posts = Post::get($_SESSION['user_id'],$title,$post_id);
  111. $rows = '';
  112. if(is_array($posts))
  113. foreach($posts as $post) {
  114. $rows .= "<tr>".
  115. "<td name=\"post_id\">$post->post_id</td>".
  116. "<td name=\"title\">$post->title</td>".
  117. "<td name=\"creator_id\">$post->creator</td>".
  118. "<td name=\"createTime\">$post->createTime</td>".
  119. "<td name=\"modTime\">$post->modTime</td>".
  120. '<td class="createDeletePost">X</td>'.
  121. "</tr>";
  122. }
  123. return $rows;
  124. } else {
  125. return Post::get($_SESSION['user_id'],$title,$post_id,$rType);
  126. }
  127. }
  128. /////////END///////////////////////////////END////////////////
  129. //Database Information Retrieval Functions//
  130. /////////END///////////////////////////////END////////////////
  131. /////////////////////////////////////////////////////////////////////
  132. // Assorted Functions //
  133. /////////////////////////////////////////////////////////////////////
  134. /** check an array for existing keys, checks each key with array_key_exists
  135. * @param array $keyArray an array containing all the keys to check for
  136. * @param array $array the array to test - passed by reference
  137. * @param bool $setBlank switch to set missing key values to empty strings instead of returning false
  138. * @param bool $blankChk switch to check for blank values and return false if found
  139. * @return bool true/false
  140. */
  141. function array_keys_exist($keyArray, &$array, $setBlank=FALSE, $blankChk=FALSE) {
  142. foreach($keyArray as $key) {
  143. if(!array_key_exists($key, $array)) {
  144. if($setBlank) {
  145. $array[$key] = '';
  146. } else
  147. return false;
  148. }
  149. if($blankChk)
  150. if($array[$key] == '')
  151. return false;
  152. }
  153. return true;
  154. }
  155. /**
  156. * Translate a bitwise user access level into a human readable title
  157. * @param int $access_level the access level to translate
  158. */
  159. function getHumanAccess($access_level) {
  160. switch($access_level) {
  161. case 0:
  162. return $human = "Unauthorized User";
  163. case 1:
  164. return $human = "Basic User";
  165. case 3:
  166. return $human = "Creator";
  167. case 7:
  168. return $human = "Administrator";
  169. default:
  170. return $human = "Unknown (Error?)";
  171. }
  172. }
  173. /**
  174. * return references for an array of values
  175. * @param array $arr the array to return references for
  176. */
  177. function makeValuesReferenced($arr){
  178. $refs = array();
  179. foreach($arr as $key => $value)
  180. $refs[$key] = &$arr[$key];
  181. return $refs;
  182. }
  183. /**
  184. * print a line break with an optional centered message - used for debugging
  185. * @param string $msg the message
  186. * @param int $rows the number of rows
  187. * @param string $char the character to use
  188. * @param int $cols number of columns
  189. */
  190. function linebreak($msg=NULL,$rows=1,$char="~",$cols=100) {
  191. if($msg && !$rows) $rows = 4;
  192. for($x = 0; $x < $rows; $x++) {
  193. for($y = 0; $y < $cols; $y++)
  194. echo $char;
  195. echo "<br />";
  196. //optional message
  197. if($msg && $x==round($rows/2)-1) {
  198. $msgLength = strlen($msg);
  199. $msgMiddle = round(($cols/2)-($msgLength/2));
  200. for($y = 0; $y < $cols-$msgLength; $y++) {
  201. echo $char;
  202. //echo message in the middle
  203. if($y == $msgMiddle) echo $msg;
  204. }
  205. echo "<br />";
  206. }
  207. }
  208. }
  209. /**
  210. * Error Handling and Logging
  211. * {@see http://php.net/manual/en/function.set-error-handler.php}
  212. **/
  213. function ErrorHandler($errno, $errmsg, $filename, $linenum, $vars) {
  214. //Error Log filename
  215. $errorLogPath = ERROR_LOG_DIR."PHPerrors.xml";
  216. //how long to keep errors
  217. defined('PHP_ERROR_EXPIRE')? $errorExpireTime = strtotime(PHP_ERROR_EXPIRE) : $errorExpireTime = NULL;
  218. // define an assoc array of error string
  219. // in reality the only entries we should
  220. // consider are E_WARNING, E_NOTICE, E_USER_ERROR,
  221. // E_USER_WARNING and E_USER_NOTICE
  222. $errortype = array (
  223. E_ERROR => 'Error',
  224. E_WARNING => 'Warning',
  225. E_PARSE => 'Parsing Error',
  226. E_NOTICE => 'Notice',
  227. E_CORE_ERROR => 'Core Error',
  228. E_CORE_WARNING => 'Core Warning',
  229. E_COMPILE_ERROR => 'Compile Error',
  230. E_COMPILE_WARNING => 'Compile Warning',
  231. E_USER_ERROR => 'User Error',
  232. E_USER_WARNING => 'User Warning',
  233. E_USER_NOTICE => 'User Notice',
  234. E_STRICT => 'Runtime Notice',
  235. E_RECOVERABLE_ERROR => 'Catchable Fatal Error'
  236. );
  237. // set of errors for which a var trace will be saved
  238. $user_errors = array(E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE);
  239. if(is_readable($errorLogPath)) {
  240. // Create a new DOMDocument object with formatting
  241. $errorLog = new DOMDocument('1.0');
  242. $errorLog->formatOutput = true;
  243. $errorLog->preserveWhiteSpace = false;
  244. //Import error log file
  245. $errorLog->load($errorLogPath);
  246. //grab all errors
  247. $errors = $errorLog->documentElement->getElementsByTagName("error");
  248. //check time and remove old errors
  249. if($errorExpireTime) {
  250. $errorsLength = $errors->length-1;
  251. for($n = $errorsLength; $n >= 0; $n--) {
  252. $error = $errors->item($n);
  253. //get error time
  254. $errorTime = $error->getElementsByTagName("datetime");
  255. //convert time
  256. $errorTime = strtotime($errorTime->item(0)->nodeValue);
  257. //compare and remove if expired
  258. if( $errorTime < $errorExpireTime)
  259. $errorLog->documentElement->removeChild($error);
  260. }
  261. }
  262. //Create the new Error XML
  263. $err = new SimpleXMLElement("<error></error>");
  264. $err->addChild('datetime',date('d M Y H:i T'));
  265. $err->addChild('num',$errno);
  266. $err->addChild('type',$errortype[$errno]);
  267. $err->addChild('msg',$errmsg);
  268. $err->addChild('scriptname',$filename);
  269. $err->addChild('linenum',$linenum);
  270. if (in_array($errno, $user_errors)) {
  271. ob_start();
  272. var_export($vars);
  273. $vars = ob_get_contents();
  274. ob_clean(); //cleanup ob
  275. $err->addChild('vartrace', $vars);
  276. }
  277. //convert the simpleXML to a DOMElement
  278. $Err = dom_import_simplexml($err);
  279. //import the node, and all its children, to the document
  280. $Err = $errorLog->importNode($Err,true);
  281. // And then append it to the "<root>" node
  282. $errorLog->documentElement->appendChild($Err);
  283. // save the modified error log
  284. $errorLog->save($errorLogPath);
  285. } else {
  286. // SEND AN EMAIL TO ADMINISTRATOR IF ERROR LOGGING IS BROKEN
  287. }
  288. // !!!
  289. // Add Administrator Email notification depending on error type
  290. // !!!
  291. //if ($errno == E_USER_ERROR) {
  292. //mail("phpdev@example.com", "Critical User Error", $err);
  293. //}
  294. }
  295. set_error_handler("ErrorHandler");
  296. /////////END///////////////////////////////END////////////////
  297. // Assorted Functions //
  298. /////////END///////////////////////////////END////////////////
  299. ?>