You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

240 lines
9.0 KiB

13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
  1. <?php
  2. /**
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage exception
  7. * @since 2010-02-26
  8. */
  9. class ErrorHandler
  10. {
  11. static public function init()
  12. {
  13. set_error_handler(array('ErrorHandler', 'error_handler'));
  14. }
  15. static public function error_handler($errno, $errstr, $errfile, $errline)
  16. {
  17. $ob_handlers = ob_get_status();
  18. if (!empty($ob_handlers)) {
  19. ob_end_clean();
  20. }
  21. if (error_reporting() !== 0) {
  22. throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
  23. }
  24. return true;
  25. }
  26. static public function logError($exception)
  27. {
  28. $error = 0;
  29. if ($exception instanceof ErrorException) {
  30. $error = $exception->getSeverity();
  31. }
  32. switch ($error) {
  33. case E_NOTICE:
  34. $error = 'Notice';
  35. break;
  36. case E_WARNING:
  37. $error = 'Warning';
  38. break;
  39. case E_ERROR:
  40. $error = 'Fatal Error';
  41. break;
  42. default:
  43. $error = 'Unknown Error';
  44. break;
  45. }
  46. $message = 'PHP ' . $error . ': ' . $exception->getMessage() . ' in ' . $exception->getFile() . ':' . $exception->getLine() . ' \nStack trace:\n' . $exception->getTraceAsString() . self::getHTTPEErrorConditions();
  47. // PHP Fatal error: Uncaught exception 'LogicException' in /www/test.tfs/face/htdocs/index.php:11\nStack trace:\n#0 {main}\n thrown in /www/test.tfs/face/htdocs/index.php on line 11, referer: http://test.tfs.manekeno.netmonsters.ru/news/create
  48. error_log($message);
  49. }
  50. static public function getHTTPEErrorConditions()
  51. {
  52. $text = null;
  53. if (!is_null(Env::Server('REQUEST_METHOD')) && !is_null(Env::Server('REQUEST_URI'))) {
  54. $text = ', URL: ' . Env::Server('REQUEST_METHOD') . ' ' . Env::Server('REQUEST_URI');
  55. $text .= ', referrer: ' . Env::Server('HTTP_REFERER');
  56. }
  57. return $text;
  58. }
  59. static protected function getSource($file, $hiline)
  60. {
  61. $code = array();
  62. $i = 0;
  63. foreach (file($file) as $line) {
  64. $i++;
  65. if ($i >= $hiline - 10 && $i <= $hiline + 10) {
  66. if ($i == $hiline) {
  67. $code[] = '<tr class="error"><th>' . $i . '</th><td><span class="specific">' . htmlentities($line, ENT_QUOTES, 'UTF-8') . '</span></td></tr>';
  68. } else {
  69. $code[] = '<tr><th>' . $i . '</th><td>' . htmlentities($line, ENT_QUOTES, 'UTF-8') . '</td></tr>';
  70. }
  71. }
  72. if ($i > $hiline + 10) {
  73. break;
  74. }
  75. }
  76. return implode('', $code);
  77. }
  78. static protected function wrapArray($array, $name)
  79. {
  80. if (!$array) {
  81. return '<p>No ' . $name . ' data</p>';
  82. }
  83. $text = '<table class="req"><thead><tr><th>Variable</th><th>Value</th></tr></thead><tbody>';
  84. foreach ($array as $key => $value) {
  85. if (is_array($value) || is_object($value)) {
  86. $value = print_r($value, true);
  87. }
  88. $value = ($value) ? htmlentities($value, ENT_QUOTES, 'UTF-8') : '&nbsp;';
  89. $text .= '<tr><td>' . $key . '</td><td class="code"><div>' . $value . '</div></td></tr>';
  90. }
  91. $text .= '</tbody></table>';
  92. return $text;
  93. }
  94. static protected function wrapTrace($trace)
  95. {
  96. return '<code>' . nl2br($trace) . '</code>';
  97. }
  98. /**
  99. * @param Exception $exception
  100. * @return string
  101. */
  102. static public function showDebug($exception)
  103. {
  104. $ob_handlers = ob_get_status();
  105. if (!empty($ob_handlers)) {
  106. ob_end_clean();
  107. }
  108. $class = get_class($exception);
  109. $method = Env::Server('REQUEST_METHOD', '');
  110. $uri = Env::getRequestUri();
  111. $source = self::getSource($exception->getFile(), $exception->getLine());
  112. $time = date('r', Env::Server('REQUEST_TIME', time()));
  113. $trace = nl2br($exception->getTraceAsString());
  114. $get = self::wrapArray(Env::Get(), 'GET');
  115. $post = self::wrapArray(Env::Post(), 'POST');
  116. $session = self::wrapArray(Session::get(), 'SESSION');
  117. $files = self::wrapArray(Env::Files(), 'FILES');
  118. $cookies = self::wrapArray(Env::Cookie(), 'COOKIE');
  119. $server = self::wrapArray(Env::Server(), 'SERVER');
  120. $message = <<<EOD
  121. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  122. <html xmlns="http://www.w3.org/1999/xhtml">
  123. <head>
  124. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  125. <title>{$class} at {$uri}</title>
  126. <style type="text/css">
  127. html * { padding:0; margin:0; }
  128. body * { padding:10px 20px; }
  129. body * * { padding:0; }
  130. body { font:small sans-serif; }
  131. body>div { border-bottom:1px solid #ddd; }
  132. h1 { font-weight:normal; }
  133. h2 { margin-bottom:.8em; }
  134. h2 span { font-size:80%; color:#666; font-weight:normal; }
  135. h3 { margin:1em 0 .5em 0; }
  136. h4 { margin:0 0 .5em 0; font-weight: normal; }
  137. table { border:1px solid #ccc; border-collapse: collapse; width:100%; background:white; }
  138. tbody td, tbody th { vertical-align:top; padding:2px 3px; }
  139. thead th { padding:1px 6px 1px 3px; background:#fefefe; text-align:left; font-weight:normal; font-size:11px; border:1px solid #ddd; }
  140. tbody th { width:12em; text-align:right; color:#666; padding-right:.5em; }
  141. table.vars { margin:5px 0 2px 40px; }
  142. table.vars td, table.req td { font-family:monospace; }
  143. table td.code { width:100%; }
  144. table td.code div { overflow:hidden; }
  145. table.source th { color:#666; }
  146. table.source td { font-family:monospace; white-space:pre; border-bottom:1px solid #eee; }
  147. ul.traceback { list-style-type:none; }
  148. ul.traceback li.frame { margin-bottom:1em; }
  149. div.context { margin: 10px 0; }
  150. div.context ol { padding-left:30px; margin:0 10px; list-style-position: inside; }
  151. div.context ol li { font-family:monospace; white-space:pre; color:#666; cursor:pointer; }
  152. div.context ol.context-line li { color:black; background-color:#ccc; }
  153. div.context ol.context-line li span { float: right; }
  154. div.commands { margin-left: 40px; }
  155. div.commands a { color:black; text-decoration:none; }
  156. #summary { background: #ffc; }
  157. #summary h2 { font-weight: normal; color: #666; }
  158. #explanation { background:#eee; }
  159. #source{ background:#f6f6f6; }
  160. #traceback { background:#eee; }
  161. #requestinfo { background:#f6f6f6; padding-left:120px; }
  162. #summary table { border:none; background:transparent; }
  163. #requestinfo h2, #requestinfo h3 { position:relative; margin-left:-100px; }
  164. #requestinfo h3 { margin-bottom:-1em; }
  165. .error { background: #ffc; }
  166. .specific { color:#cc3300; font-weight:bold; }
  167. h2 span.commands { font-size:.7em;}
  168. span.commands a:link {color:#5E5694;}
  169. pre.exception_value { font-family: sans-serif; color: #666; font-size: 1.5em; margin: 10px 0 10px 0; }
  170. </style>
  171. </head>
  172. <body>
  173. <div id="summary">
  174. <h1>{$class} at {$uri}</h1>
  175. <pre class="exception_value">{$exception->getMessage()}</pre>
  176. <table class="meta">
  177. <tbody>
  178. <tr><th>Request Method:</th><td>{$method}</td></tr>
  179. <tr><th>Exception Type:</th><td>{$class}</td></tr>
  180. <tr><th>Exception Message:</th><td><pre>{$exception->getMessage()}</pre></td></tr>
  181. <tr><th>Exception Location:</th><td>{$exception->getFile()}, line {$exception->getLine()}</td></tr>
  182. <tr><th>Server time:</th><td>{$time}</td></tr>
  183. </tbody>
  184. </table>
  185. </div>
  186. <div id="source">
  187. <h2>Context</h2>
  188. <p>In file <code>{$exception->getFile()}</code>, error at line <strong>{$exception->getLine()}</strong></p>
  189. <h3>{$exception->getMessage()}</h3>
  190. <table class="source cut-top">
  191. <tbody>{$source}</tbody>
  192. </table>
  193. </div>
  194. <div id="traceback">
  195. <h2>Traceback</h2>
  196. <div id="browserTraceback"><code>{$trace}</code></div>
  197. </div>
  198. <div id="requestinfo">
  199. <h2>Request information</h2>
  200. <h3 id="get-info">GET</h3>
  201. {$get}
  202. <h3 id="post-info">POST</h3>
  203. {$post}
  204. <h3 id="session-info">SESSION</h3>
  205. {$session}
  206. <h3 id="files-info">FILES</h3>
  207. {$files}
  208. <h3 id="cookie-info">COOKIES</h3>
  209. {$cookies}
  210. <h3 id="meta-info">SERVER</h3>
  211. {$server}
  212. <h3 id="settings-info">Settings</h3>
  213. <p>todo</p>
  214. </div>
  215. <div id="explanation">
  216. <p>You're seeing this error because you have defined constant <code>DEBUG = True</code> in your config file. Change that to <code>False</code>, and it will display a standard 500 page.</p>
  217. </div>
  218. </body>
  219. </html>
  220. EOD;
  221. return $message;
  222. }
  223. }