' . $message . '
PHP:' . $logtrace; discuz_error::write_error_log($messagesave); } if (!empty($GLOBALS['_G']['config']['security']['error']['showerror']) && $halt) { $show = true; } if ($show) { discuz_error::show_error('system', "
  • $message
  • ", $showtrace, '', md5(discuz_error::clear($messagesave))); } if ($halt && 0) { header('HTTP/1.1 503 Service Unavailable'); exit(); } else { return $message; } } public static function template_error($message, $tplname) { $tplname = str_replace(DISCUZ_ROOT, '', $tplname); $message = $message . ': ' . $tplname; discuz_error::system_error($message); } public static function debug_backtrace() { $skipfunc[] = 'discuz_error->debug_backtrace'; $skipfunc[] = 'discuz_error->db_error'; $skipfunc[] = 'discuz_error->template_error'; $skipfunc[] = 'discuz_error->system_error'; $skipfunc[] = 'db_mysql->halt'; $skipfunc[] = 'db_mysql->query'; $skipfunc[] = 'DB::_execute'; $show = $log = ''; $debug_backtrace = debug_backtrace(); krsort($debug_backtrace); foreach ($debug_backtrace as $k => $error) { $file = str_replace(DISCUZ_ROOT, '', $error['file']); $func = $error['class'] ?? ''; $func .= $error['type'] ?? ''; $func .= $error['function'] ?? ''; if (in_array($func, $skipfunc)) { break; } $error['line'] = sprintf('%04d', $error['line']); $show .= "
  • [Line: {$error['line']}]" . $file . "($func)
  • "; $log .= (!empty($log) ? ' -> ' : '') . $file . '#' . $func . ':' . $error['line']; } return [$show, $log]; } public static function db_error($message, $sql) { global $_G; list($showtrace, $logtrace) = discuz_error::debug_backtrace(); $title = lang('error', 'db_' . $message); $title_msg = lang('error', 'db_error_message'); $title_sql = lang('error', 'db_query_sql'); $title_backtrace = lang('error', 'backtrace'); $title_help = lang('error', 'db_help_link'); $db = &DB::object(); $dberrno = $db->errno(); $dberror = str_replace($db->tablepre, '', $db->error()); $sql = dhtmlspecialchars(str_replace($db->tablepre, '', $sql)); $msg = '
  • [Type] ' . $title . '
  • '; $msg .= $dberrno ? '
  • [' . $dberrno . '] ' . $dberror . '
  • ' : ''; $msg .= $sql ? '
  • [Query] ' . $sql . '
  • ' : ''; $errormsg = '' . $title . ''; $errormsg .= "[$dberrno]
    ERR: $dberror
    "; if ($sql) { $errormsg .= 'SQL: ' . $sql; } $errormsg .= '
    '; $errormsg .= 'PHP: ' . $logtrace; discuz_error::write_error_log($errormsg); discuz_error::show_error('db', $msg, $showtrace, '', md5(discuz_error::clear($errormsg))); exit(); } public static function exception_error($exception) { if ($exception instanceof DbException) { $type = 'db'; } else { $type = 'system'; } if ($type == 'db') { $errormsg = '(' . $exception->getCode() . ') '; $errormsg .= self::sql_clear($exception->getMessage()); if ($exception->getSql()) { $errormsg .= '
    '; $errormsg .= self::sql_clear($exception->getSql()); $errormsg .= '
    '; } } else { $errormsg = $exception->getMessage(); } $trace = $exception->getTrace(); krsort($trace); $trace[] = ['file' => $exception->getFile(), 'line' => $exception->getLine(), 'function' => 'break']; $logmsg = ''; $phpmsg = []; foreach ($trace as $error) { if (!empty($error['function'])) { $fun = ''; if (!empty($error['class'])) { $fun .= $error['class'] . $error['type']; } $fun .= $error['function'] . '('; if (!empty($error['args'])) { $mark = ''; foreach ($error['args'] as $arg) { $fun .= $mark; if (is_array($arg)) { $fun .= 'Array'; } elseif (is_bool($arg)) { $fun .= $arg ? 'true' : 'false'; } elseif (is_int($arg)) { $fun .= (defined('DISCUZ_DEBUG') && DISCUZ_DEBUG) ? $arg : '%d'; } elseif (is_float($arg)) { $fun .= (defined('DISCUZ_DEBUG') && DISCUZ_DEBUG) ? $arg : '%f'; } elseif (is_resource($arg)) { $fun .= (defined('DISCUZ_DEBUG') && DISCUZ_DEBUG) ? 'Resource' : '%f'; } elseif (is_object($arg)) { $fun .= (defined('DISCUZ_DEBUG') && DISCUZ_DEBUG) ? 'Object' : '%f'; } else { $arg = (string)$arg; $fun .= (defined('DISCUZ_DEBUG') && DISCUZ_DEBUG) ? '\'' . dhtmlspecialchars(substr(self::clear($arg), 0, 10)) . (strlen($arg) > 10 ? ' ...' : '') . '\'' : '%s'; } $mark = ', '; } } $fun .= ')'; $error['function'] = $fun; } $phpmsg[] = [ 'file' => str_replace([DISCUZ_ROOT, '\\'], ['', '/'], $error['file']), 'line' => $error['line'], 'function' => $error['function'], ]; $file = str_replace([DISCUZ_ROOT, '\\'], ['', '/'], $error['file']); $func = $error['class'] ?? ''; $func .= $error['type'] ?? ''; $func .= $error['function'] ?? ''; $line = sprintf('%04d', $error['line']); $logmsg .= (!empty($logmsg) ? ' -> ' : '') . $file . '#' . $func . ':' . $line; } $messagesave = '' . $errormsg . '
    PHP:' . $logmsg; self::write_error_log($messagesave); self::show_error($type, $errormsg, $phpmsg, '', md5(discuz_error::clear($messagesave))); exit(); } public static function show_error($type, $errormsg, $phpmsg = '', $typemsg = '', $backtraceid = '') { global $_G; ob_end_clean(); $gzip = getglobal('gzipcompress'); ob_start($gzip ? 'ob_gzhandler' : null); header('HTTP/1.1 503 Service Temporarily Unavailable'); header('Status: 503 Service Temporarily Unavailable'); header('Retry-After: 3600'); $host = $_SERVER['HTTP_HOST']; $title = (!isset($_G['config']['security']['error']['showerror']) || !empty($_G['config']['security']['error']['showerror'])) ? ($type == 'db' ? 'Database' : 'System') : 'General'; echo << $host - $title Error

    Discuz! $title Error

    EOT; echo '

    Time: ' . date('Y-m-d H:i:s O') . ' IP: ' . getglobal('clientip') . ' BackTraceID: ' . $backtraceid . '

    '; if (!empty($errormsg) && (!isset($_G['config']['security']['error']['showerror']) || !empty($_G['config']['security']['error']['showerror']))) { echo '
    ' . $errormsg . '
    '; } if (isset($_G['config']['security']['error']['showerror']) && empty($_G['config']['security']['error']['showerror'])) { echo '
    '; } if (!empty($phpmsg) && (!isset($_G['config']['security']['error']['showerror']) || $_G['config']['security']['error']['showerror'] == '1')) { echo '
    '; echo '

    PHP Debug

    '; echo ''; if (is_array($phpmsg)) { echo ''; foreach ($phpmsg as $k => $msg) { $k++; $explode = explode('/', $msg['file']); if (isset($explode['1']) && $explode['1'] == 'plugin') { $guess = $explode['2']; $bg = 'bg3'; } else { $bg = 'bg1'; } echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; } } else { echo ''; } echo '
    No.FileLineCode
    ' . $k . '' . $msg['file'] . '' . $msg['line'] . '' . $msg['function'] . '
      ' . $phpmsg . '
    '; } } public static function clear($message) { return str_replace(["\t", "\r", "\n"], ' ', $message); } public static function sql_clear($message) { $message = self::clear($message); $message = str_replace(DB::object()->tablepre, '', $message); $message = dhtmlspecialchars($message); return $message; } public static function write_error_log($message) { $message = discuz_error::clear($message); $time = time(); $file = DISCUZ_DATA . './log/' . date("Ym") . '_errorlog.php'; $hash = md5($message); $ip = getglobal('clientip'); $user = 'User: IP=' . $ip . '; RIP:' . $_SERVER['REMOTE_ADDR']; $uri = 'Request: ' . dhtmlspecialchars(discuz_error::clear($_SERVER['REQUEST_URI'])); $message = "\t{$time}\t$message\t$hash\t$user $uri\n"; if ($fp = @fopen($file, 'rb')) { $lastlen = 50000; $maxtime = 60 * 10; $offset = filesize($file) - $lastlen; if ($offset > 0) { fseek($fp, $offset); } if ($data = fread($fp, $lastlen)) { $array = explode("\n", $data); if (is_array($array)) foreach ($array as $key => $val) { $row = explode("\t", $val); if ($row[0] != '') continue; if ($row[3] == $hash && ($row[1] > $time - $maxtime)) { return; } } } } error_log($message, 3, $file); } }