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.

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