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.

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