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.

197 lines
7.4 KiB

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