discuz_error.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. <?php
  2. /**
  3. * [Discuz!] (C)2001-2099 Comsenz Inc.
  4. * This is NOT a freeware, use is subject to license terms
  5. *
  6. * $Id: discuz_error.php 33361 2013-05-31 08:59:06Z nemohou $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. class discuz_error
  12. {
  13. public static function system_error($message, $show = true, $save = true, $halt = true) {
  14. if(!empty($message)) {
  15. $message = lang('error', $message);
  16. } else {
  17. $message = lang('error', 'error_unknow');
  18. }
  19. list($showtrace, $logtrace) = discuz_error::debug_backtrace();
  20. if($save) {
  21. $messagesave = '<b>'.$message.'</b><br><b>PHP:</b>'.$logtrace;
  22. discuz_error::write_error_log($messagesave);
  23. }
  24. if($show) {
  25. if(!defined('IN_MOBILE')) {
  26. discuz_error::show_error('system', "<li>$message</li>", $showtrace, 0);
  27. } else {
  28. discuz_error::mobile_show_error('system', "<li>$message</li>", $showtrace, 0);
  29. }
  30. }
  31. if($halt) {
  32. exit();
  33. } else {
  34. return $message;
  35. }
  36. }
  37. public static function template_error($message, $tplname) {
  38. $message = lang('error', $message);
  39. $tplname = str_replace(DISCUZ_ROOT, '', $tplname);
  40. $message = $message.': '.$tplname;
  41. discuz_error::system_error($message);
  42. }
  43. public static function debug_backtrace() {
  44. $skipfunc[] = 'discuz_error->debug_backtrace';
  45. $skipfunc[] = 'discuz_error->db_error';
  46. $skipfunc[] = 'discuz_error->template_error';
  47. $skipfunc[] = 'discuz_error->system_error';
  48. $skipfunc[] = 'db_mysql->halt';
  49. $skipfunc[] = 'db_mysql->query';
  50. $skipfunc[] = 'DB::_execute';
  51. $show = $log = '';
  52. $debug_backtrace = debug_backtrace();
  53. krsort($debug_backtrace);
  54. foreach ($debug_backtrace as $k => $error) {
  55. $file = str_replace(DISCUZ_ROOT, '', $error['file']);
  56. $func = isset($error['class']) ? $error['class'] : '';
  57. $func .= isset($error['type']) ? $error['type'] : '';
  58. $func .= isset($error['function']) ? $error['function'] : '';
  59. if(in_array($func, $skipfunc)) {
  60. break;
  61. }
  62. $error[line] = sprintf('%04d', $error['line']);
  63. $show .= "<li>[Line: $error[line]]".$file."($func)</li>";
  64. $log .= !empty($log) ? ' -> ' : '';$file.':'.$error['line'];
  65. $log .= $file.':'.$error['line'];
  66. }
  67. return array($show, $log);
  68. }
  69. public static function db_error($message, $sql) {
  70. global $_G;
  71. list($showtrace, $logtrace) = discuz_error::debug_backtrace();
  72. $title = lang('error', 'db_'.$message);
  73. $title_msg = lang('error', 'db_error_message');
  74. $title_sql = lang('error', 'db_query_sql');
  75. $title_backtrace = lang('error', 'backtrace');
  76. $title_help = lang('error', 'db_help_link');
  77. $db = &DB::object();
  78. $dberrno = $db->errno();
  79. $dberror = str_replace($db->tablepre, '', $db->error());
  80. $sql = dhtmlspecialchars(str_replace($db->tablepre, '', $sql));
  81. $msg = '<li>[Type] '.$title.'</li>';
  82. $msg .= $dberrno ? '<li>['.$dberrno.'] '.$dberror.'</li>' : '';
  83. $msg .= $sql ? '<li>[Query] '.$sql.'</li>' : '';
  84. discuz_error::show_error('db', $msg, $showtrace, false);
  85. unset($msg, $phperror);
  86. $errormsg = '<b>'.$title.'</b>';
  87. $errormsg .= "[$dberrno]<br /><b>ERR:</b> $dberror<br />";
  88. if($sql) {
  89. $errormsg .= '<b>SQL:</b> '.$sql;
  90. }
  91. $errormsg .= "<br />";
  92. $errormsg .= '<b>PHP:</b> '.$logtrace;
  93. discuz_error::write_error_log($errormsg);
  94. exit();
  95. }
  96. public static function exception_error($exception) {
  97. if($exception instanceof DbException) {
  98. $type = 'db';
  99. } else {
  100. $type = 'system';
  101. }
  102. if($type == 'db') {
  103. $errormsg = '('.$exception->getCode().') ';
  104. $errormsg .= self::sql_clear($exception->getMessage());
  105. if($exception->getSql()) {
  106. $errormsg .= '<div class="sql">';
  107. $errormsg .= self::sql_clear($exception->getSql());
  108. $errormsg .= '</div>';
  109. }
  110. } else {
  111. $errormsg = $exception->getMessage();
  112. }
  113. $trace = $exception->getTrace();
  114. krsort($trace);
  115. $trace[] = array('file'=>$exception->getFile(), 'line'=>$exception->getLine(), 'function'=> 'break');
  116. $phpmsg = array();
  117. foreach ($trace as $error) {
  118. if(!empty($error['function'])) {
  119. $fun = '';
  120. if(!empty($error['class'])) {
  121. $fun .= $error['class'].$error['type'];
  122. }
  123. $fun .= $error['function'].'(';
  124. if(!empty($error['args'])) {
  125. $mark = '';
  126. foreach($error['args'] as $arg) {
  127. $fun .= $mark;
  128. if(is_array($arg)) {
  129. $fun .= 'Array';
  130. } elseif(is_bool($arg)) {
  131. $fun .= $arg ? 'true' : 'false';
  132. } elseif(is_int($arg)) {
  133. $fun .= (defined('DISCUZ_DEBUG') && DISCUZ_DEBUG) ? $arg : '%d';
  134. } elseif(is_float($arg)) {
  135. $fun .= (defined('DISCUZ_DEBUG') && DISCUZ_DEBUG) ? $arg : '%f';
  136. } else {
  137. $fun .= (defined('DISCUZ_DEBUG') && DISCUZ_DEBUG) ? '\''.dhtmlspecialchars(substr(self::clear($arg), 0, 10)).(strlen($arg) > 10 ? ' ...' : '').'\'' : '%s';
  138. }
  139. $mark = ', ';
  140. }
  141. }
  142. $fun .= ')';
  143. $error['function'] = $fun;
  144. }
  145. $phpmsg[] = array(
  146. 'file' => str_replace(array(DISCUZ_ROOT, '\\'), array('', '/'), $error['file']),
  147. 'line' => $error['line'],
  148. 'function' => $error['function'],
  149. );
  150. }
  151. self::show_error($type, $errormsg, $phpmsg);
  152. exit();
  153. }
  154. public static function show_error($type, $errormsg, $phpmsg = '', $typemsg = '') {
  155. global $_G;
  156. ob_end_clean();
  157. $gzip = getglobal('gzipcompress');
  158. ob_start($gzip ? 'ob_gzhandler' : null);
  159. $host = $_SERVER['HTTP_HOST'];
  160. $title = $type == 'db' ? 'Database' : 'System';
  161. echo <<<EOT
  162. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  163. <html>
  164. <head>
  165. <title>$host - $title Error</title>
  166. <meta http-equiv="Content-Type" content="text/html; charset={$_G['config']['output']['charset']}" />
  167. <meta name="ROBOTS" content="NOINDEX,NOFOLLOW,NOARCHIVE" />
  168. <style type="text/css">
  169. <!--
  170. body { background-color: white; color: black; font: 9pt/11pt verdana, arial, sans-serif;}
  171. #container { width: 1024px; }
  172. #message { width: 1024px; color: black; }
  173. .red {color: red;}
  174. a:link { font: 9pt/11pt verdana, arial, sans-serif; color: red; }
  175. a:visited { font: 9pt/11pt verdana, arial, sans-serif; color: #4e4e4e; }
  176. h1 { color: #FF0000; font: 18pt "Verdana"; margin-bottom: 0.5em;}
  177. .bg1{ background-color: #FFFFCC;}
  178. .bg2{ background-color: #EEEEEE;}
  179. .table {background: #AAAAAA; font: 11pt Menlo,Consolas,"Lucida Console"}
  180. .info {
  181. background: none repeat scroll 0 0 #F3F3F3;
  182. border: 0px solid #aaaaaa;
  183. border-radius: 10px 10px 10px 10px;
  184. color: #000000;
  185. font-size: 11pt;
  186. line-height: 160%;
  187. margin-bottom: 1em;
  188. padding: 1em;
  189. }
  190. .help {
  191. background: #F3F3F3;
  192. border-radius: 10px 10px 10px 10px;
  193. font: 12px verdana, arial, sans-serif;
  194. text-align: center;
  195. line-height: 160%;
  196. padding: 1em;
  197. }
  198. .sql {
  199. background: none repeat scroll 0 0 #FFFFCC;
  200. border: 1px solid #aaaaaa;
  201. color: #000000;
  202. font: arial, sans-serif;
  203. font-size: 9pt;
  204. line-height: 160%;
  205. margin-top: 1em;
  206. padding: 4px;
  207. }
  208. -->
  209. </style>
  210. </head>
  211. <body>
  212. <div id="container">
  213. <h1>Discuz! $title Error</h1>
  214. <div class='info'>$errormsg</div>
  215. EOT;
  216. if(!empty($phpmsg)) {
  217. echo '<div class="info">';
  218. echo '<p><strong>PHP Debug</strong></p>';
  219. echo '<table cellpadding="5" cellspacing="1" width="100%" class="table">';
  220. if(is_array($phpmsg)) {
  221. echo '<tr class="bg2"><td>No.</td><td>File</td><td>Line</td><td>Code</td></tr>';
  222. foreach($phpmsg as $k => $msg) {
  223. $k++;
  224. echo '<tr class="bg1">';
  225. echo '<td>'.$k.'</td>';
  226. echo '<td>'.$msg['file'].'</td>';
  227. echo '<td>'.$msg['line'].'</td>';
  228. echo '<td>'.$msg['function'].'</td>';
  229. echo '</tr>';
  230. }
  231. } else {
  232. echo '<tr><td><ul>'.$phpmsg.'</ul></td></tr>';
  233. }
  234. echo '</table></div>';
  235. }
  236. $helplink = '';
  237. if($type == 'db') {
  238. $helplink = "http://faq.comsenz.com/?type=mysql&dberrno=".rawurlencode(DB::errno())."&dberror=".rawurlencode(str_replace(DB::object()->tablepre, '', DB::error()));
  239. $helplink = "<a href=\"$helplink\" target=\"_blank\"><span class=\"red\">Need Help?</span></a>";
  240. }
  241. $endmsg = lang('error', 'error_end_message', array('host'=>$host));
  242. echo <<<EOT
  243. <div class="help">$endmsg. $helplink</div>
  244. </div>
  245. </body>
  246. </html>
  247. EOT;
  248. $exit && exit();
  249. }
  250. public static function mobile_show_error($type, $errormsg, $phpmsg) {
  251. global $_G;
  252. ob_end_clean();
  253. ob_start();
  254. $host = $_SERVER['HTTP_HOST'];
  255. $phpmsg = trim($phpmsg);
  256. $title = 'Mobile '.($type == 'db' ? 'Database' : 'System');
  257. echo <<<EOT
  258. <?xml version="1.0" encoding="utf-8"?>
  259. <!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
  260. <html>
  261. <head>
  262. <title>$host - $title Error</title>
  263. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  264. <meta name="ROBOTS" content="NOINDEX,NOFOLLOW,NOARCHIVE" />
  265. <style type="text/css">
  266. <!--
  267. body { background-color: white; color: black; }
  268. UL, LI { margin: 0; padding: 2px; list-style: none; }
  269. #message { color: black; background-color: #FFFFCC; }
  270. #bodytitle { font: 11pt/13pt verdana, arial, sans-serif; height: 20px; vertical-align: top; }
  271. .bodytext { font: 8pt/11pt verdana, arial, sans-serif; }
  272. .help { font: 12px verdana, arial, sans-serif; color: red;}
  273. .red {color: red;}
  274. a:link { font: 8pt/11pt verdana, arial, sans-serif; color: red; }
  275. a:visited { font: 8pt/11pt verdana, arial, sans-serif; color: #4e4e4e; }
  276. -->
  277. </style>
  278. </head>
  279. <body>
  280. <table cellpadding="1" cellspacing="1" id="container">
  281. <tr>
  282. <td id="bodytitle" width="100%">Discuz! $title Error </td>
  283. </tr>
  284. EOT;
  285. echo <<<EOT
  286. <tr><td><hr size="1"/></td></tr>
  287. <tr><td class="bodytext">Error messages: </td></tr>
  288. <tr>
  289. <td class="bodytext" id="message">
  290. <ul> $errormsg</ul>
  291. </td>
  292. </tr>
  293. EOT;
  294. if(!empty($phpmsg) && $type == 'db') {
  295. echo <<<EOT
  296. <tr><td class="bodytext">&nbsp;</td></tr>
  297. <tr><td class="bodytext">Program messages: </td></tr>
  298. <tr>
  299. <td class="bodytext">
  300. <ul> $phpmsg </ul>
  301. </td>
  302. </tr>
  303. EOT;
  304. }
  305. $endmsg = lang('error', 'mobile_error_end_message', array('host'=>$host));
  306. echo <<<EOT
  307. <tr>
  308. <td class="help"><br />$endmsg</td>
  309. </tr>
  310. </table>
  311. </body>
  312. </html>
  313. EOT;
  314. $exit && exit();
  315. }
  316. public static function clear($message) {
  317. return str_replace(array("\t", "\r", "\n"), " ", $message);
  318. }
  319. public static function sql_clear($message) {
  320. $message = self::clear($message);
  321. $message = str_replace(DB::object()->tablepre, '', $message);
  322. $message = dhtmlspecialchars($message);
  323. return $message;
  324. }
  325. public static function write_error_log($message) {
  326. $message = discuz_error::clear($message);
  327. $time = time();
  328. $file = DISCUZ_ROOT.'./data/log/'.date("Ym").'_errorlog.php';
  329. $hash = md5($message);
  330. $uid = getglobal('uid');
  331. $ip = getglobal('clientip');
  332. $user = '<b>User:</b> uid='.intval($uid).'; IP='.$ip.'; RIP:'.$_SERVER['REMOTE_ADDR'];
  333. $uri = 'Request: '.dhtmlspecialchars(discuz_error::clear($_SERVER['REQUEST_URI']));
  334. $message = "<?PHP exit;?>\t{$time}\t$message\t$hash\t$user $uri\n";
  335. if($fp = @fopen($file, 'rb')) {
  336. $lastlen = 50000;
  337. $maxtime = 60 * 10;
  338. $offset = filesize($file) - $lastlen;
  339. if($offset > 0) {
  340. fseek($fp, $offset);
  341. }
  342. if($data = fread($fp, $lastlen)) {
  343. $array = explode("\n", $data);
  344. if(is_array($array)) foreach($array as $key => $val) {
  345. $row = explode("\t", $val);
  346. if($row[0] != '<?PHP exit;?>') continue;
  347. if($row[3] == $hash && ($row[1] > $time - $maxtime)) {
  348. return;
  349. }
  350. }
  351. }
  352. }
  353. error_log($message, 3, $file);
  354. }
  355. }