discuz_error.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. <?php
  2. /**
  3. * [Discuz!] (C)2001-2099 Discuz! Team
  4. * This is NOT a freeware, use is subject to license terms
  5. * https://license.discuz.vip
  6. */
  7. if (!defined('IN_DISCUZ')) {
  8. exit('Access Denied');
  9. }
  10. class discuz_error {
  11. public static function system_error($message, $show = false, $save = true, $halt = true) {
  12. list($showtrace, $logtrace) = discuz_error::debug_backtrace();
  13. if ($save) {
  14. $messagesave = '<b>' . $message . '</b><br><b>PHP:</b>' . $logtrace;
  15. discuz_error::write_error_log($messagesave);
  16. }
  17. if (!empty($GLOBALS['_G']['config']['security']['error']['showerror']) && $halt) {
  18. $show = true;
  19. }
  20. if ($show) {
  21. discuz_error::show_error('system', "<li>$message</li>", $showtrace, '', md5(discuz_error::clear($messagesave)));
  22. }
  23. if ($halt && 0) {
  24. header('HTTP/1.1 503 Service Unavailable');
  25. exit();
  26. } else {
  27. return $message;
  28. }
  29. }
  30. public static function template_error($message, $tplname) {
  31. $tplname = str_replace(DISCUZ_ROOT, '', $tplname);
  32. $message = $message . ': ' . $tplname;
  33. discuz_error::system_error($message);
  34. }
  35. public static function debug_backtrace() {
  36. $skipfunc[] = 'discuz_error->debug_backtrace';
  37. $skipfunc[] = 'discuz_error->db_error';
  38. $skipfunc[] = 'discuz_error->template_error';
  39. $skipfunc[] = 'discuz_error->system_error';
  40. $skipfunc[] = 'db_mysql->halt';
  41. $skipfunc[] = 'db_mysql->query';
  42. $skipfunc[] = 'DB::_execute';
  43. $show = $log = '';
  44. $debug_backtrace = debug_backtrace();
  45. krsort($debug_backtrace);
  46. foreach ($debug_backtrace as $k => $error) {
  47. $file = str_replace(DISCUZ_ROOT, '', $error['file']);
  48. $func = $error['class'] ?? '';
  49. $func .= $error['type'] ?? '';
  50. $func .= $error['function'] ?? '';
  51. if (in_array($func, $skipfunc)) {
  52. break;
  53. }
  54. $error['line'] = sprintf('%04d', $error['line']);
  55. $show .= "<li>[Line: {$error['line']}]" . $file . "($func)</li>";
  56. $log .= (!empty($log) ? ' -> ' : '') . $file . '#' . $func . ':' . $error['line'];
  57. }
  58. return [$show, $log];
  59. }
  60. public static function db_error($message, $sql) {
  61. global $_G;
  62. list($showtrace, $logtrace) = discuz_error::debug_backtrace();
  63. $title = lang('error', 'db_' . $message);
  64. $title_msg = lang('error', 'db_error_message');
  65. $title_sql = lang('error', 'db_query_sql');
  66. $title_backtrace = lang('error', 'backtrace');
  67. $title_help = lang('error', 'db_help_link');
  68. $db = &DB::object();
  69. $dberrno = $db->errno();
  70. $dberror = str_replace($db->tablepre, '', $db->error());
  71. $sql = dhtmlspecialchars(str_replace($db->tablepre, '', $sql));
  72. $msg = '<li>[Type] ' . $title . '</li>';
  73. $msg .= $dberrno ? '<li>[' . $dberrno . '] ' . $dberror . '</li>' : '';
  74. $msg .= $sql ? '<li>[Query] ' . $sql . '</li>' : '';
  75. $errormsg = '<b>' . $title . '</b>';
  76. $errormsg .= "[$dberrno]<br /><b>ERR:</b> $dberror<br />";
  77. if ($sql) {
  78. $errormsg .= '<b>SQL:</b> ' . $sql;
  79. }
  80. $errormsg .= '<br />';
  81. $errormsg .= '<b>PHP:</b> ' . $logtrace;
  82. discuz_error::write_error_log($errormsg);
  83. discuz_error::show_error('db', $msg, $showtrace, '', md5(discuz_error::clear($errormsg)));
  84. exit();
  85. }
  86. public static function exception_error($exception) {
  87. if ($exception instanceof DbException) {
  88. $type = 'db';
  89. } else {
  90. $type = 'system';
  91. }
  92. if ($type == 'db') {
  93. $errormsg = '(' . $exception->getCode() . ') ';
  94. $errormsg .= self::sql_clear($exception->getMessage());
  95. if ($exception->getSql()) {
  96. $errormsg .= '<div class="sql">';
  97. $errormsg .= self::sql_clear($exception->getSql());
  98. $errormsg .= '</div>';
  99. }
  100. } else {
  101. $errormsg = $exception->getMessage();
  102. }
  103. $trace = $exception->getTrace();
  104. krsort($trace);
  105. $trace[] = ['file' => $exception->getFile(), 'line' => $exception->getLine(), 'function' => 'break'];
  106. $logmsg = '';
  107. $phpmsg = [];
  108. foreach ($trace as $error) {
  109. if (!empty($error['function'])) {
  110. $fun = '';
  111. if (!empty($error['class'])) {
  112. $fun .= $error['class'] . $error['type'];
  113. }
  114. $fun .= $error['function'] . '(';
  115. if (!empty($error['args'])) {
  116. $mark = '';
  117. foreach ($error['args'] as $arg) {
  118. $fun .= $mark;
  119. if (is_array($arg)) {
  120. $fun .= 'Array';
  121. } elseif (is_bool($arg)) {
  122. $fun .= $arg ? 'true' : 'false';
  123. } elseif (is_int($arg)) {
  124. $fun .= (defined('DISCUZ_DEBUG') && DISCUZ_DEBUG) ? $arg : '%d';
  125. } elseif (is_float($arg)) {
  126. $fun .= (defined('DISCUZ_DEBUG') && DISCUZ_DEBUG) ? $arg : '%f';
  127. } elseif (is_resource($arg)) {
  128. $fun .= (defined('DISCUZ_DEBUG') && DISCUZ_DEBUG) ? 'Resource' : '%f';
  129. } elseif (is_object($arg)) {
  130. $fun .= (defined('DISCUZ_DEBUG') && DISCUZ_DEBUG) ? 'Object' : '%f';
  131. } else {
  132. $arg = (string)$arg;
  133. $fun .= (defined('DISCUZ_DEBUG') && DISCUZ_DEBUG) ? '\'' . dhtmlspecialchars(substr(self::clear($arg), 0, 10)) . (strlen($arg) > 10 ? ' ...' : '') . '\'' : '%s';
  134. }
  135. $mark = ', ';
  136. }
  137. }
  138. $fun .= ')';
  139. $error['function'] = $fun;
  140. }
  141. $phpmsg[] = [
  142. 'file' => str_replace([DISCUZ_ROOT, '\\'], ['', '/'], $error['file']),
  143. 'line' => $error['line'],
  144. 'function' => $error['function'],
  145. ];
  146. $file = str_replace([DISCUZ_ROOT, '\\'], ['', '/'], $error['file']);
  147. $func = $error['class'] ?? '';
  148. $func .= $error['type'] ?? '';
  149. $func .= $error['function'] ?? '';
  150. $line = sprintf('%04d', $error['line']);
  151. $logmsg .= (!empty($logmsg) ? ' -> ' : '') . $file . '#' . $func . ':' . $line;
  152. }
  153. $messagesave = '<b>' . $errormsg . '</b><br><b>PHP:</b>' . $logmsg;
  154. self::write_error_log($messagesave);
  155. self::show_error($type, $errormsg, $phpmsg, '', md5(discuz_error::clear($messagesave)));
  156. exit();
  157. }
  158. public static function show_error($type, $errormsg, $phpmsg = '', $typemsg = '', $backtraceid = '') {
  159. global $_G;
  160. ob_end_clean();
  161. $gzip = getglobal('gzipcompress');
  162. ob_start($gzip ? 'ob_gzhandler' : null);
  163. header('HTTP/1.1 503 Service Temporarily Unavailable');
  164. header('Status: 503 Service Temporarily Unavailable');
  165. header('Retry-After: 3600');
  166. $host = $_SERVER['HTTP_HOST'];
  167. $title = (!isset($_G['config']['security']['error']['showerror']) || !empty($_G['config']['security']['error']['showerror'])) ? ($type == 'db' ? 'Database' : 'System') : 'General';
  168. echo <<<EOT
  169. <!DOCTYPE html>
  170. <html>
  171. <head>
  172. <title>$host - $title Error</title>
  173. <meta charset="{$_G['config']['output']['charset']}" />
  174. <meta name="renderer" content="webkit" />
  175. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  176. <meta name="ROBOTS" content="NOINDEX,NOFOLLOW,NOARCHIVE" />
  177. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  178. <style type="text/css">
  179. <!--
  180. body { background-color: white; color: black; font: 9pt/11pt verdana, arial, sans-serif;}
  181. #container { max-width: 1024px; margin: auto; }
  182. #message { max-width: 1024px; color: black; }
  183. .red {color: red;}
  184. a:link { font: 9pt/11pt verdana, arial, sans-serif; color: red; }
  185. a:visited { font: 9pt/11pt verdana, arial, sans-serif; color: #4e4e4e; }
  186. a.guess { font: 11pt/13pt verdana, arial, sans-serif; color: blue; }
  187. h1 { color: #FF0000; font: 18pt "Verdana"; margin-bottom: 0.5em;}
  188. .bg1{ background-color: #FFFFCC;}
  189. .bg2{ background-color: #EEEEEE;}
  190. .bg3{ background-color: #FFA66C; font-weight: bold;}
  191. .table {background: #AAAAAA; font: 11pt Menlo,Consolas,"Lucida Console";}
  192. .table tbody{word-break: break-all;}
  193. .info {
  194. background: none repeat scroll 0 0 #F3F3F3;
  195. border: 0px solid #aaaaaa;
  196. border-radius: 10px 10px 10px 10px;
  197. color: #000000;
  198. font-size: 11pt;
  199. line-height: 160%;
  200. margin-bottom: 1em;
  201. padding: 1em;
  202. }
  203. .info svg { width: 40%; min-width: 200px; display: block; margin: auto; margin-bottom: 30px; fill: #999; }
  204. .info svg .xicon { fill: #d31f0d; }
  205. .help {
  206. background: #F3F3F3;
  207. border-radius: 10px 10px 10px 10px;
  208. font: 14px verdana, arial, sans-serif;
  209. text-align: center;
  210. line-height: 160%;
  211. padding: 1em;
  212. margin: 1em 0;
  213. }
  214. .sql {
  215. background: none repeat scroll 0 0 #FFFFCC;
  216. border: 1px solid #aaaaaa;
  217. color: #000000;
  218. font: arial, sans-serif;
  219. font-size: 9pt;
  220. line-height: 160%;
  221. margin-top: 1em;
  222. padding: 4px;
  223. }
  224. -->
  225. </style>
  226. </head>
  227. <body>
  228. <div id="container">
  229. <h1>Discuz! $title Error</h1>
  230. EOT;
  231. echo '<p>Time: ' . date('Y-m-d H:i:s O') . ' IP: ' . getglobal('clientip') . ' BackTraceID: ' . $backtraceid . '</p>';
  232. if (!empty($errormsg) && (!isset($_G['config']['security']['error']['showerror']) || !empty($_G['config']['security']['error']['showerror']))) {
  233. echo '<div class="info">' . $errormsg . '</div>';
  234. }
  235. if (isset($_G['config']['security']['error']['showerror']) && empty($_G['config']['security']['error']['showerror'])) {
  236. echo '<div class="info"><svg viewBox="0 0 16 16"><path d="M2.5 5a.5.5 0 100-1 .5.5 0 000 1zM4 5a.5.5 0 100-1 .5.5 0 000 1zm2-.5a.5.5 0 11-1 0 .5.5 0 011 0zM0 4a2 2 0 012-2h11a2 2 0 012 2v4a.5.5 0 01-1 0V7H1v5a1 1 0 001 1h5.5a.5.5 0 010 1H2a2 2 0 01-2-2V4zm1 2h13V4a1 1 0 00-1-1H2a1 1 0 00-1 1v2z"/><path d="M16 12.5a3.5 3.5 0 11-7 0 3.5 3.5 0 017 0zm-4.854-1.354a.5.5 0 000 .708l.647.646-.647.646a.5.5 0 00.708.708l.646-.647.646.647a.5.5 0 00.708-.708l-.647-.646.647-.646a.5.5 0 00-.708-.708l-.646.647-.646-.647a.5.5 0 00-.708 0z" class="xicon"/></svg></div>';
  237. }
  238. if (!empty($phpmsg) && (!isset($_G['config']['security']['error']['showerror']) || $_G['config']['security']['error']['showerror'] == '1')) {
  239. echo '<div class="info">';
  240. echo '<p><strong>PHP Debug</strong></p>';
  241. echo '<table cellpadding="5" cellspacing="1" width="100%" class="table">';
  242. if (is_array($phpmsg)) {
  243. echo '<tr class="bg2"><td>No.</td><td>File</td><td>Line</td><td>Code</td></tr>';
  244. foreach ($phpmsg as $k => $msg) {
  245. $k++;
  246. $explode = explode('/', $msg['file']);
  247. if (isset($explode['1']) && $explode['1'] == 'plugin') {
  248. $guess = $explode['2'];
  249. $bg = 'bg3';
  250. } else {
  251. $bg = 'bg1';
  252. }
  253. echo '<tr class="' . $bg . '">';
  254. echo '<td>' . $k . '</td>';
  255. echo '<td>' . $msg['file'] . '</td>';
  256. echo '<td>' . $msg['line'] . '</td>';
  257. echo '<td>' . $msg['function'] . '</td>';
  258. echo '</tr>';
  259. }
  260. } else {
  261. echo '<tr><td><ul>' . $phpmsg . '</ul></td></tr>';
  262. }
  263. echo '</table></div>';
  264. }
  265. }
  266. public static function clear($message) {
  267. return str_replace(["\t", "\r", "\n"], ' ', $message);
  268. }
  269. public static function sql_clear($message) {
  270. $message = self::clear($message);
  271. $message = str_replace(DB::object()->tablepre, '', $message);
  272. $message = dhtmlspecialchars($message);
  273. return $message;
  274. }
  275. public static function write_error_log($message) {
  276. $message = discuz_error::clear($message);
  277. $time = time();
  278. $file = DISCUZ_DATA . './log/' . date("Ym") . '_errorlog.php';
  279. $hash = md5($message);
  280. $ip = getglobal('clientip');
  281. $user = '<b>User:</b> IP=' . $ip . '; RIP:' . $_SERVER['REMOTE_ADDR'];
  282. $uri = 'Request: ' . dhtmlspecialchars(discuz_error::clear($_SERVER['REQUEST_URI']));
  283. $message = "<?PHP exit;?>\t{$time}\t$message\t$hash\t$user $uri\n";
  284. if ($fp = @fopen($file, 'rb')) {
  285. $lastlen = 50000;
  286. $maxtime = 60 * 10;
  287. $offset = filesize($file) - $lastlen;
  288. if ($offset > 0) {
  289. fseek($fp, $offset);
  290. }
  291. if ($data = fread($fp, $lastlen)) {
  292. $array = explode("\n", $data);
  293. if (is_array($array)) foreach ($array as $key => $val) {
  294. $row = explode("\t", $val);
  295. if ($row[0] != '<?PHP exit;?>') continue;
  296. if ($row[3] == $hash && ($row[1] > $time - $maxtime)) {
  297. return;
  298. }
  299. }
  300. }
  301. }
  302. error_log($message, 3, $file);
  303. }
  304. }