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.

194 lines
7.3 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)) {
  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 = self::wrapTrace($exception->getTraceAsString());
  74. $get = self::wrapArray(Env::Get(), 'GET');
  75. $post = self::wrapArray(Env::Post(), 'POST');
  76. $files = self::wrapArray(Env::Files(), 'FILES');
  77. $cookies = self::wrapArray(Env::Cookie(), 'COOKIE');
  78. $server = self::wrapArray(Env::Server(), 'SERVER');
  79. $message =<<<EOD
  80. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  81. <html xmlns="http://www.w3.org/1999/xhtml">
  82. <head>
  83. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  84. <title>{$class} at {$uri}</title>
  85. <style type="text/css">
  86. html * { padding:0; margin:0; }
  87. body * { padding:10px 20px; }
  88. body * * { padding:0; }
  89. body { font:small sans-serif; }
  90. body>div { border-bottom:1px solid #ddd; }
  91. h1 { font-weight:normal; }
  92. h2 { margin-bottom:.8em; }
  93. h2 span { font-size:80%; color:#666; font-weight:normal; }
  94. h3 { margin:1em 0 .5em 0; }
  95. h4 { margin:0 0 .5em 0; font-weight: normal; }
  96. table { border:1px solid #ccc; border-collapse: collapse; width:100%; background:white; }
  97. tbody td, tbody th { vertical-align:top; padding:2px 3px; }
  98. thead th { padding:1px 6px 1px 3px; background:#fefefe; text-align:left; font-weight:normal; font-size:11px; border:1px solid #ddd; }
  99. tbody th { width:12em; text-align:right; color:#666; padding-right:.5em; }
  100. table.vars { margin:5px 0 2px 40px; }
  101. table.vars td, table.req td { font-family:monospace; }
  102. table td.code { width:100%; }
  103. table td.code div { overflow:hidden; }
  104. table.source th { color:#666; }
  105. table.source td { font-family:monospace; white-space:pre; border-bottom:1px solid #eee; }
  106. ul.traceback { list-style-type:none; }
  107. ul.traceback li.frame { margin-bottom:1em; }
  108. div.context { margin: 10px 0; }
  109. div.context ol { padding-left:30px; margin:0 10px; list-style-position: inside; }
  110. div.context ol li { font-family:monospace; white-space:pre; color:#666; cursor:pointer; }
  111. div.context ol.context-line li { color:black; background-color:#ccc; }
  112. div.context ol.context-line li span { float: right; }
  113. div.commands { margin-left: 40px; }
  114. div.commands a { color:black; text-decoration:none; }
  115. #summary { background: #ffc; }
  116. #summary h2 { font-weight: normal; color: #666; }
  117. #explanation { background:#eee; }
  118. #source{ background:#f6f6f6; }
  119. #traceback { background:#eee; }
  120. #requestinfo { background:#f6f6f6; padding-left:120px; }
  121. #summary table { border:none; background:transparent; }
  122. #requestinfo h2, #requestinfo h3 { position:relative; margin-left:-100px; }
  123. #requestinfo h3 { margin-bottom:-1em; }
  124. .error { background: #ffc; }
  125. .specific { color:#cc3300; font-weight:bold; }
  126. h2 span.commands { font-size:.7em;}
  127. span.commands a:link {color:#5E5694;}
  128. pre.exception_value { font-family: sans-serif; color: #666; font-size: 1.5em; margin: 10px 0 10px 0; }
  129. </style>
  130. </head>
  131. <body>
  132. <div id="summary">
  133. <h1>{$class} at {$uri}</h1>
  134. <pre class="exception_value">{$exception->getMessage()}</pre>
  135. <table class="meta">
  136. <tbody>
  137. <tr><th>Request Method:</th><td>{$method}</td></tr>
  138. <tr><th>Exception Type:</th><td>{$class}</td></tr>
  139. <tr><th>Exception Message:</th><td><pre>{$exception->getMessage()}</pre></td></tr>
  140. <tr><th>Exception Location:</th><td>{$exception->getFile()}, line {$exception->getLine()}</td></tr>
  141. <tr><th>Server time:</th><td>{$time}</td></tr>
  142. </tbody>
  143. </table>
  144. </div>
  145. <div id="source">
  146. <h2>Context</h2>
  147. <p>In file <code>{$exception->getFile()}</code>, error at line <strong>{$exception->getLine()}</strong></p>
  148. <h3>{$exception->getMessage()}</h3>
  149. <table class="source cut-top">
  150. <tbody>{$source}</tbody>
  151. </table>
  152. </div>
  153. <div id="traceback">
  154. <h2>Traceback</h2>
  155. <div id="browserTraceback">{$trace}</div>
  156. </div>
  157. <div id="requestinfo">
  158. <h2>Request information</h2>
  159. <h3 id="get-info">GET</h3>
  160. {$get}
  161. <h3 id="post-info">POST</h3>
  162. {$post}
  163. <h3 id="files-info">FILES</h3>
  164. {$files}
  165. <h3 id="cookie-info">COOKIES</h3>
  166. {$cookies}
  167. <h3 id="meta-info">SERVER</h3>
  168. {$server}
  169. <h3 id="settings-info">Settings</h3>
  170. <p>todo</p>
  171. </div>
  172. <div id="explanation">
  173. <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>
  174. </div>
  175. </body>
  176. </html>
  177. EOD;
  178. return $message;
  179. }
  180. }