/** * 系统错误,可直接将Exception对象传给$msg * @param string/Exception $msg */ public static function show_500($msg = null) { Core::close_buffers(false); # 避免输出的CSS头试抛出页面无法显示 @header('Content-Type: text/html;charset=' . Core::config('charset'), true); HttpIO::$status = 500; HttpIO::send_headers(); if (null === $msg) { $msg = __('Internal Server Error'); } if (IS_DEBUG && class_exists('DevException', false)) { if ($msg instanceof Exception) { $e = $msg; } else { $e = new Exception($msg); } echo DevException::exception_handler($e, true); exit; } if (IS_CLI) { echo "[31m", $msg, CRLF, "[39m", CRLF; exit; } try { if ($msg instanceof Exception) { $error = $msg->getMessage(); $trace_obj = $msg; } else { $error = $msg; $trace_obj = new Exception($msg); } $error_config = Core::config('error500'); $view = new View('error/500'); if ($error_config && isset($error_config['close']) && $error_config['close'] == true) { # 不记录 $view->error_saved = false; $error_no = ''; } else { $trace_array = array('project' => Core::$project, 'admin_mode' => IS_ADMIN_MODE, 'uri' => HttpIO::$uri, 'url' => HttpIO::PROTOCOL . $_SERVER['HTTP_HOST'] . $_SERVER["REQUEST_URI"], 'post' => HttpIO::POST(null, HttpIO::PARAM_TYPE_OLDDATA), 'get' => $_SERVER['QUERY_STRING'], 'cookie' => HttpIO::COOKIE(null, HttpIO::PARAM_TYPE_OLDDATA), 'client_ip' => HttpIO::IP, 'user_agent' => HttpIO::USER_AGENT, 'referrer' => HttpIO::REFERRER, 'server_ip' => $_SERVER["SERVER_ADDR"]); $date = @date('Y-m-d'); $no = strtoupper(substr(md5(serialize($trace_array)), 10, 10)); $error_no = $date . '-' . $no; # 其它数据 $trace_array['server_name'] = function_exists('php_uname') ? php_uname('a') : 'unknown'; $trace_array['time'] = TIME; $trace_array['use_time'] = microtime(1) - START_TIME; $trace_array['trace'] = (string) $trace_obj; $trace_string = Core::json_encode($trace_array); $view->error_saved = true; # 记录错误日志 try { if (isset($error_config['save_type']) && $error_config['save_type']) { $save_type = $error_config['save_type']; } else { $save_type = 'file'; } if ($save_type === 'file') { # 文件模式 $write_mode = Core::config('file_write_mode'); if (preg_match('#^(db|cache|fluent)://(([a-z0-9\\.\\-_]+)(?:\\:|/)([a-z0-9_]+))$#i', $write_mode, $m)) { $save_type = $m[1]; $error_config['server'] = $m[2]; $error_config['type_config'] = $m[3]; } } switch ($save_type) { case 'database': $obj = $error_config['type_config'] ? new Database($error_config['type_config']) : new Database(); $data = array('time' => strtotime($date . ' 00:00:00'), 'no' => $no, 'log' => $obj->is_support_object_value() ? $trace_array : $trace_string, 'expire_time' => TIME + 7 * 86400); $obj->insert('error500_log', $data); break; case 'cache': $obj = $error_config['type_config'] ? new Cache($error_config['type_config']) : new Cache(); if (!$obj->get($error_no)) { $obj->set($error_no, $trace_string, 7 * 86400); } break; case 'fluent': if (strpos($error_config['server'], ':') !== false) { $fd_server = 'tcp://' . $error_config['server']; } else { $fd_server = 'udp://' . $error_config['server']; } $obj = Fluent::instance($fd_server); $obj->push('system.error500', $trace_array); break; default: $file = DIR_LOG . 'error500' . DS . str_replace('-', DS, $date) . DS . $no . '.log'; if (!is_file($file)) { File::create_file($file, date('Y-m-d\\TH:i:s') . ' - ' . $trace_string, null, null, $error_config['type_config'] ? $error_config['type_config'] : 'default'); } break; } } catch (Exception $e) { } } $view->error_no = $error_no; $view->error = $error; $view->render(true); } catch (Exception $e) { list($REQUEST_URI) = explode('?', $_SERVER['REQUEST_URI'], 2); $REQUEST_URI = htmlspecialchars(rawurldecode($REQUEST_URI)); echo '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">' . CRLF . '<html>' . CRLF . '<head>' . CRLF . '<title>Internal Server Error</title>' . CRLF . '</head>' . CRLF . '<body>' . CRLF . '<h1>Internal Server Error</h1>' . CRLF . '<p>The requested URL ' . $REQUEST_URI . ' was error on this server.</p>' . CRLF . '<hr />' . CRLF . $_SERVER['SERVER_SIGNATURE'] . '<br/><br/><br/>' . CRLF . 'Powered by MyQEE V' . Core::VERSION . CRLF . '</body>' . CRLF . '</html>'; } exit; }