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.

202 lines
7.5 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. * @version SVN: $Id$
  9. * @filesource $URL$
  10. */
  11. class ErrorHandler
  12. {
  13. static public function init()
  14. {
  15. set_error_handler(array('ErrorHandler', 'error_handler'));
  16. }
  17. static public function error_handler($errno, $errstr, $errfile, $errline)
  18. {
  19. $ob_handlers = ob_get_status();
  20. if (!empty($ob_handlers)) {
  21. ob_clean();
  22. }
  23. throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
  24. }
  25. static protected function getSource($file, $hiline)
  26. {
  27. $code = array();
  28. $i = 0;
  29. foreach (file($file) as $line) {
  30. $i++;
  31. if ($i >= $hiline - 10 && $i <= $hiline + 10) {
  32. if ($i == $hiline) {
  33. $code[] = '<tr class="error"><th>' . $i . '</th><td><span class="specific">' . htmlentities($line, ENT_QUOTES, 'UTF-8') . '</span></td></tr>';
  34. } else {
  35. $code[] = '<tr><th>' . $i . '</th><td>' . htmlentities($line, ENT_QUOTES, 'UTF-8') . '</td></tr>';
  36. }
  37. }
  38. if ($i > $hiline + 10) {
  39. break;
  40. }
  41. }
  42. return implode('', $code);
  43. }
  44. static protected function wrapArray($array, $name)
  45. {
  46. if (!$array) {
  47. return '<p>No ' . $name . ' data</p>';
  48. }
  49. $text = '<table class="req"><thead><tr><th>Variable</th><th>Value</th></tr></thead><tbody>';
  50. foreach ($array as $key => $value) {
  51. if (is_array($value) || is_object($value)) {
  52. $value = print_r($value, true);
  53. }
  54. $value = ($value) ? htmlentities($value, ENT_QUOTES, 'UTF-8') : '&nbsp;';
  55. $text .= '<tr><td>' . $key . '</td><td class="code"><div>' . $value . '</div></td></tr>';
  56. }
  57. $text .= '</tbody></table>';
  58. return $text;
  59. }
  60. static protected function wrapTrace($trace)
  61. {
  62. return '<code>' . nl2br($trace) . '</code>';
  63. }
  64. /**
  65. * @param Exception $exception
  66. */
  67. static public function showDebug($exception)
  68. {
  69. $ob_handlers = ob_get_status();
  70. if (!empty($ob_handlers)) {
  71. ob_end_clean();
  72. }
  73. $class = get_class($exception);
  74. $method = Env::Server('REQUEST_METHOD', '');
  75. $uri = Env::getRequestUri();
  76. $source = self::getSource($exception->getFile(), $exception->getLine());
  77. $time = date('r', Env::Server('REQUEST_TIME', time()));
  78. $trace = nl2br($exception->getTraceAsString());
  79. $get = self::wrapArray(Env::Get(), 'GET');
  80. $post = self::wrapArray(Env::Post(), 'POST');
  81. $session = self::wrapArray(Session::get(), 'SESSION');
  82. $files = self::wrapArray(Env::Files(), 'FILES');
  83. $cookies = self::wrapArray(Env::Cookie(), 'COOKIE');
  84. $server = self::wrapArray(Env::Server(), 'SERVER');
  85. $message = <<<EOD
  86. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  87. <html xmlns="http://www.w3.org/1999/xhtml">
  88. <head>
  89. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  90. <title>{$class} at {$uri}</title>
  91. <style type="text/css">
  92. html * { padding:0; margin:0; }
  93. body * { padding:10px 20px; }
  94. body * * { padding:0; }
  95. body { font:small sans-serif; }
  96. body>div { border-bottom:1px solid #ddd; }
  97. h1 { font-weight:normal; }
  98. h2 { margin-bottom:.8em; }
  99. h2 span { font-size:80%; color:#666; font-weight:normal; }
  100. h3 { margin:1em 0 .5em 0; }
  101. h4 { margin:0 0 .5em 0; font-weight: normal; }
  102. table { border:1px solid #ccc; border-collapse: collapse; width:100%; background:white; }
  103. tbody td, tbody th { vertical-align:top; padding:2px 3px; }
  104. thead th { padding:1px 6px 1px 3px; background:#fefefe; text-align:left; font-weight:normal; font-size:11px; border:1px solid #ddd; }
  105. tbody th { width:12em; text-align:right; color:#666; padding-right:.5em; }
  106. table.vars { margin:5px 0 2px 40px; }
  107. table.vars td, table.req td { font-family:monospace; }
  108. table td.code { width:100%; }
  109. table td.code div { overflow:hidden; }
  110. table.source th { color:#666; }
  111. table.source td { font-family:monospace; white-space:pre; border-bottom:1px solid #eee; }
  112. ul.traceback { list-style-type:none; }
  113. ul.traceback li.frame { margin-bottom:1em; }
  114. div.context { margin: 10px 0; }
  115. div.context ol { padding-left:30px; margin:0 10px; list-style-position: inside; }
  116. div.context ol li { font-family:monospace; white-space:pre; color:#666; cursor:pointer; }
  117. div.context ol.context-line li { color:black; background-color:#ccc; }
  118. div.context ol.context-line li span { float: right; }
  119. div.commands { margin-left: 40px; }
  120. div.commands a { color:black; text-decoration:none; }
  121. #summary { background: #ffc; }
  122. #summary h2 { font-weight: normal; color: #666; }
  123. #explanation { background:#eee; }
  124. #source{ background:#f6f6f6; }
  125. #traceback { background:#eee; }
  126. #requestinfo { background:#f6f6f6; padding-left:120px; }
  127. #summary table { border:none; background:transparent; }
  128. #requestinfo h2, #requestinfo h3 { position:relative; margin-left:-100px; }
  129. #requestinfo h3 { margin-bottom:-1em; }
  130. .error { background: #ffc; }
  131. .specific { color:#cc3300; font-weight:bold; }
  132. h2 span.commands { font-size:.7em;}
  133. span.commands a:link {color:#5E5694;}
  134. pre.exception_value { font-family: sans-serif; color: #666; font-size: 1.5em; margin: 10px 0 10px 0; }
  135. </style>
  136. </head>
  137. <body>
  138. <div id="summary">
  139. <h1>{$class} at {$uri}</h1>
  140. <pre class="exception_value">{$exception->getMessage()}</pre>
  141. <table class="meta">
  142. <tbody>
  143. <tr><th>Request Method:</th><td>{$method}</td></tr>
  144. <tr><th>Exception Type:</th><td>{$class}</td></tr>
  145. <tr><th>Exception Message:</th><td><pre>{$exception->getMessage()}</pre></td></tr>
  146. <tr><th>Exception Location:</th><td>{$exception->getFile()}, line {$exception->getLine()}</td></tr>
  147. <tr><th>Server time:</th><td>{$time}</td></tr>
  148. </tbody>
  149. </table>
  150. </div>
  151. <div id="source">
  152. <h2>Context</h2>
  153. <p>In file <code>{$exception->getFile()}</code>, error at line <strong>{$exception->getLine()}</strong></p>
  154. <h3>{$exception->getMessage()}</h3>
  155. <table class="source cut-top">
  156. <tbody>{$source}</tbody>
  157. </table>
  158. </div>
  159. <div id="traceback">
  160. <h2>Traceback</h2>
  161. <div id="browserTraceback"><code>{$trace}</code></div>
  162. </div>
  163. <div id="requestinfo">
  164. <h2>Request information</h2>
  165. <h3 id="get-info">GET</h3>
  166. {$get}
  167. <h3 id="post-info">POST</h3>
  168. {$post}
  169. <h3 id="session-info">SESSION</h3>
  170. {$session}
  171. <h3 id="files-info">FILES</h3>
  172. {$files}
  173. <h3 id="cookie-info">COOKIES</h3>
  174. {$cookies}
  175. <h3 id="meta-info">SERVER</h3>
  176. {$server}
  177. <h3 id="settings-info">Settings</h3>
  178. <p>todo</p>
  179. </div>
  180. <div id="explanation">
  181. <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>
  182. </div>
  183. </body>
  184. </html>
  185. EOD;
  186. return $message;
  187. }
  188. }