PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/framework/helpers/framework_helpers.php

http://personal-home-page-framework.googlecode.com/
PHP | 403 lines | 313 code | 39 blank | 51 comment | 14 complexity | 014386e42e90752ef921c594da7ed705 MD5 | raw file
  1. <?php
  2. function __autoload($class_name) {
  3. $class_name = camelcasify($class_name);
  4. if(stripos($class_name, "Controller") === false) {
  5. require(MODELS_PATH . "$class_name.php");
  6. }
  7. else {
  8. require(CONTROLLERS_PATH . "$class_name.php");
  9. }
  10. }
  11. function php_fw_exception_handler($e) {
  12. echo $e->getMessage();
  13. return true;
  14. }
  15. function php_fw_error_handler($errno, $errstr, $errfile, $errline, $errcontext) {
  16. if(IS_PRODUCTION) {
  17. render_500_error();
  18. return true;
  19. }
  20. $error_output = '<html>' .
  21. '<head>' .
  22. '<meta http-equiv="Content-type" content="text/html; charset=utf-8">' .
  23. '<title>Error Found</title>' .
  24. '<style type="text/css">' .
  25. 'html {background-color:#B80000;}' .
  26. 'body {background-color:white;width:800px;margin:20px auto;padding:10px;border:1px solid black;}' .
  27. 'h1 {text-align:center;color:#B80000;text-shadow:2px 2px 0px #bbb;}' .
  28. 'h1 strong {color:black;font-style:italic;}' .
  29. 'dt {margin:10px 0px 5px 0px;padding:10px;border:1px black solid;background-color:#B80000;font-weight:bold;font-size:12pt;color:white;text-shadow:1px 1px 0px black;}' .
  30. 'dl {margin:0px 20px 30px 20px;}' .
  31. 'dl span {margin-right:20px;font-size:8pt;color:#aaa;}' .
  32. 'dl strong {color:#B80000;}' .
  33. '</style>' .
  34. '</head>' .
  35. '<body>';
  36. $corrupted_file = file_get_contents($errfile);
  37. $lines = explode("\n", $corrupted_file);
  38. $start_line = ($errline - 4);
  39. $end_line = ($errline + 2);
  40. for($i = $start_line; $i <= $end_line; $i++) {
  41. if($i < 0) {
  42. continue;
  43. }
  44. if(isset($lines[$i])) {
  45. $current_line = trim($lines[$i]);
  46. if($i == $errline - 1) {
  47. $current_line = "<strong>$current_line</strong>";
  48. }
  49. $extracted_lines .= "<span>$i</span> " . $current_line . "<br />";
  50. }
  51. }
  52. $error_info = '<h1>Error with level <strong>%s</strong> found</h1>' .
  53. '<dl>' .
  54. '<dt>Error Message</dt>' .
  55. "<dl>$errstr</dl>" .
  56. '<dt>File where error occured</dt>' .
  57. "<dl>$errfile</dl>" .
  58. '<dt>Line where error occured</dt>' .
  59. "<dl>$errline</dl>" .
  60. '<dt>Extracted lines around error</dt>' .
  61. "<dl>$extracted_lines</dl>" .
  62. '<dt>Error context</dt>' .
  63. "<dl>" . var_export($errcontext, true) . "</dl>" .
  64. '<dt>Backtrace</dt>' .
  65. '<dl>' . nl2br(var_export(debug_backtrace(), true)) . '</dl>' .
  66. '</dl>';
  67. switch ($errno) {
  68. /* Fatal run-time errors.
  69. * These indicate errors that can not be recovered from, such as a memory allocation problem.
  70. * Execution of the script is halted.
  71. */
  72. case E_ERROR:
  73. $error_level = "E_ERROR";
  74. break;
  75. /* Run-time warnings (non-fatal errors).
  76. * Execution of the script is not halted.
  77. */
  78. case E_WARNING:
  79. $error_level = "E_WARNING";
  80. break;
  81. /* Compile-time parse errors.
  82. * Parse errors should only be generated by the parser.
  83. */
  84. case E_PARSE:
  85. $error_level = "E_PARSE";
  86. break;
  87. /* Run-time notices.
  88. * Indicate that the script encountered something that could indicate an error, but could also happen in the normal course of running a script.
  89. */
  90. case E_NOTICE:
  91. $error_level = "E_NOTICE";
  92. return true;
  93. break;
  94. /* Fatal errors that occur during PHP's initial startup.
  95. * This is like an E_ERROR, except it is generated by the core of PHP.
  96. */
  97. case E_CORE_ERROR:
  98. $error_level = "E_CORE_ERROR";
  99. break;
  100. /* Warnings (non-fatal errors) that occur during PHP's initial startup.
  101. * This is like an E_WARNING, except it is generated by the core of PHP.
  102. */
  103. case E_CORE_WARNING:
  104. $error_level = "E_CORE_WARNING";
  105. break;
  106. /* Fatal compile-time errors.
  107. * This is like an E_ERROR, except it is generated by the Zend Scripting Engine.
  108. */
  109. case E_COMPILE_ERROR:
  110. $error_level = "E_COMPILE_ERROR";
  111. break;
  112. /* Compile-time warnings (non-fatal errors).
  113. * This is like an E_WARNING, except it is generated by the Zend Scripting Engine.
  114. */
  115. case E_COMPILE_WARNING:
  116. $error_level = "E_COMPILE_WARNING";
  117. break;
  118. /* User-generated error message.
  119. * This is like an E_ERROR, except it is generated in PHP code by
  120. * using the PHP function trigger_error().
  121. */
  122. case E_USER_ERROR:
  123. $error_level = "E_USER_ERROR";
  124. break;
  125. /* User-generated warning message.
  126. * This is like an E_WARNING, except it is generated in PHP code by
  127. * using the PHP function trigger_error().
  128. */
  129. case E_USER_WARNING:
  130. $error_level = "E_USER_WARNING";
  131. break;
  132. /* User-generated notice message.
  133. * This is like an E_NOTICE, except it is generated in PHP code by
  134. * using the PHP function trigger_error().
  135. */
  136. case E_USER_NOTICE:
  137. $error_level = "E_USER_NOTICE";
  138. break;
  139. /* Enable to have PHP suggest changes to your code which will ensure the
  140. * best interoperability and forward compatibility of your code.
  141. */
  142. case E_STRICT:
  143. $error_level = "E_STRICT";
  144. break;
  145. /* Catchable fatal error. It indicates that a probably dangerous
  146. * error occured, but did not leave the Engine in an unstable state.
  147. * If the error is not caught by a user defined handle (see also
  148. * set_error_handler()), the application aborts as it was an E_ERROR.
  149. */
  150. case E_RECOVERABLE_ERROR:
  151. $error_level = "E_RECOVERABLE_ERROR";
  152. break;
  153. /* Run-time notices. Enable this to receive warnings about code that
  154. * will not work in future versions.
  155. */
  156. case E_DEPRECATED:
  157. $error_level = "E_DEPRECATED";
  158. break;
  159. /* User-generated warning message. This is like an E_DEPRECATED, except it
  160. * is generated in PHP code by using the PHP function trigger_error().
  161. */
  162. case E_USER_DEPRECATED:
  163. $error_level = "E_USER_DEPRECATED";
  164. break;
  165. }
  166. $error_info = sprintf($error_info, $error_level);
  167. $error_output .= $error_info .
  168. '</body>' .
  169. '</html>';
  170. global $output;
  171. $output["html"] = $error_output;
  172. render_content();
  173. return true;
  174. }
  175. function render_content($content = "html") {
  176. global $output;
  177. echo $output[$content];
  178. }
  179. function output() {
  180. global $maped_routes;
  181. if(empty($maped_routes) or empty($maped_routes[camelcasify(get_current_model())])) {
  182. $model = camelcasify(get_current_model());
  183. $action = get_current_action();
  184. $class = $model . "Controller";
  185. }
  186. else {
  187. $current_route = $maped_routes[camelcasify(get_current_model())];
  188. $model = camelcasify($current_route["model"]);
  189. $action = $current_model["action"];
  190. $class = $model . 'Controller';
  191. }
  192. $vars = get_class_vars($class);
  193. if($vars === false) {
  194. throw new Exception("Controller: <strong>$class</strong> doesn't exist.");
  195. return false;
  196. }
  197. if(!is_script()) {
  198. if(property_exists($class, $action . "_layout")) {
  199. $layout = $vars[$action . "_layout"];
  200. }
  201. else {
  202. $layout = $vars["default_layout"];
  203. }
  204. if(property_exists($class, "custom_layout_path")) {
  205. $layout_path = $vars['custom_layout_path'];
  206. }
  207. else {
  208. $layout_path = LAYOUTS_PATH;
  209. }
  210. require($layout_path . "$layout.php");
  211. }
  212. }
  213. function include_plugin($name) {
  214. require(PLUGINS_PATH . $name . ".php");
  215. }
  216. function include_core_library($name) {
  217. require(CORE_PATH . $name . ".php");
  218. }
  219. function is_post() {
  220. if(isset($_POST) and !empty($_POST)) {
  221. $args = func_get_args();
  222. if(empty($args)) {
  223. return true;
  224. }
  225. else {
  226. $ok = true;
  227. foreach($args as $arg) {
  228. if(isset($_POST[$arg]) and !empty($_POST[$arg])) {
  229. continue;
  230. }
  231. else {
  232. return false;
  233. }
  234. }
  235. }
  236. }
  237. else {
  238. return false;
  239. }
  240. }
  241. function redirect_to_root() {
  242. header("Location: " . ROOT);
  243. exit();
  244. }
  245. function is_script() {
  246. return ($_GET["script"] == true) ? true : false;
  247. }
  248. function get_current_model() {
  249. if(!empty($_GET["model"]) and isset($_GET["model"])) {
  250. return camelcasify($_GET["model"]);
  251. }
  252. else {
  253. return camelcasify(DEFAULT_CONTROLLER);
  254. }
  255. }
  256. function get_current_action() {
  257. if(!empty($_GET["action"]) and isset($_GET["action"])) {
  258. return $_GET["action"];
  259. }
  260. else {
  261. return "index";
  262. }
  263. }
  264. function get_current_id() {
  265. if(!empty($_GET["id"]) and isset($_GET["id"])) {
  266. return $_GET["id"];
  267. }
  268. else {
  269. return null;
  270. }
  271. }
  272. function redirect_to($options = array()) {
  273. if(empty($options["model"]) or empty($options["action"])) {
  274. $location = $options;
  275. }
  276. else {
  277. $location = ROOT . underscorify($options["model"]) . '/' . ((empty($options["id"])) ? '' : $options["id"] . '/') . $options["action"];
  278. }
  279. header("Location: $location");
  280. exit();
  281. }
  282. function render_404_error() {
  283. header("Location: " . PUBLIC_PATH . "404.php");
  284. }
  285. function render_500_error() {
  286. $called_uri = base64_encode($_SERVER['REQUEST_URI']);
  287. header("Location: " . PUBLIC_PATH . "500.php?called_uri=$called_uri");
  288. }
  289. function setup_get_parameters() {
  290. $url = $_SERVER['REQUEST_URI'];
  291. $url = parse_url($url);
  292. if(empty($url["query"])) {
  293. return true;
  294. }
  295. parse_str($url["query"], $parameters);
  296. foreach($parameters as $key => $value) {
  297. $_GET[$key] = $value;
  298. }
  299. return true;
  300. }
  301. function absolute_path($url) {
  302. $server_root = 'http://' . $_SERVER['SERVER_ADDR'];
  303. $page_root = $server_root . '/' . substr(ROOT, 1);
  304. $absolute_path = $page_root . $url;
  305. return $absolute_path;
  306. }
  307. function image_link($image) {
  308. return IMAGES_PATH . $image;
  309. }
  310. $exec_time = null;
  311. function start_execution_time() {
  312. global $exec_time;
  313. $exec_time = microtime(true);
  314. }
  315. function end_execution_time() {
  316. global $exec_time;
  317. $exec_time = microtime(true) - $exec_time;
  318. }
  319. function get_execution_time($end = false) {
  320. global $exec_time;
  321. if($end) {
  322. end_execution_time();
  323. }
  324. return round($exec_time, 2);
  325. }
  326. function get_query_count($without_fields = false) {
  327. global $db;
  328. return $db->get_query_count($without_fields);
  329. }
  330. function auto_stripslashes($value) {
  331. if(AUTO_STRIPSLASHES and !is_numeric($value) and get_class($value) === false) {
  332. $value = stripslashes($value);
  333. }
  334. return $value;
  335. }
  336. function underscorify($str) {
  337. $arr = preg_split('/([A-Z][^A-Z]*)/', $str, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
  338. $ret = implode("_", $arr);
  339. $ret = strtolower($ret);
  340. return $ret;
  341. }
  342. function camelcasify($str) {
  343. $arr = explode("_", $str);
  344. $arr = array_map("ucfirst", $arr);
  345. $ret = implode("", $arr);
  346. return $ret;
  347. }
  348. ?>