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.

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