Merge pull request #76 from lainchan/revert-75-error-overhaul

Revert "Error overhaul"
This commit is contained in:
Michael Walker 2016-09-01 19:01:36 +01:00 committed by GitHub
commit e5a71d3573
3 changed files with 114 additions and 99 deletions

View File

@ -71,6 +71,69 @@ function createBoardlist($mod=false) {
);
}
function error($message, $priority = true, $debug_stuff = false) {
global $board, $mod, $config, $db_error;
if ($config['syslog'] && $priority !== false) {
// Use LOG_NOTICE instead of LOG_ERR or LOG_WARNING because most error message are not significant.
_syslog($priority !== true ? $priority : LOG_NOTICE, $message);
}
if (defined('STDIN')) {
// Running from CLI
echo('Error: ' . $message . "\n");
debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
die();
}
if ($config['debug'] && isset($db_error)) {
$debug_stuff = array_combine(array('SQLSTATE', 'Error code', 'Error message'), $db_error);
}
if ($config['debug']) {
$debug_stuff['backtrace'] = debug_backtrace();
}
// Return the bad request header, necessary for AJAX posts
// czaks: is it really so? the ajax errors only work when this is commented out
// better yet use it when ajax is disabled
if (!isset ($_POST['json_response'])) {
header($_SERVER['SERVER_PROTOCOL'] . ' 400 Bad Request');
}
// Is there a reason to disable this?
if (isset($_POST['json_response'])) {
header('Content-Type: text/json; charset=utf-8');
die(json_encode(array(
'error' => $message
)));
}
$pw = $config['db']['password'];
$debug_callback = function(&$item) use (&$debug_callback, $pw) {
if (is_array($item)) {
$item = array_filter($item, $debug_callback);
}
return ($item !== $pw || !$pw);
};
if ($debug_stuff)
$debug_stuff = array_filter($debug_stuff, $debug_callback);
die(Element('page.html', array(
'config' => $config,
'title' => _('Error'),
'subtitle' => _('An error has occured.'),
'body' => Element('error.html', array(
'config' => $config,
'message' => $message,
'mod' => $mod,
'board' => isset($board) ? $board : false,
'debug' => is_array($debug_stuff) ? str_replace("\n", '
', utf8tohtml(print_r($debug_stuff, true))) : utf8tohtml($debug_stuff)
))
)));
}
function loginForm($error=false, $username=false, $redirect=false) {
global $config;

View File

@ -1,92 +0,0 @@
<?PHP
function error_handler($errno,$errstr,$errfile, $errline, $errcontext){
if(error_reporting() & $errno){
error($errstr . ' in ' . $errfile . ' at line ' . $errline);
}
return false;
}
set_error_handler('error_handler');
function exception_handler(Exception $e){
error($e->getMessage());
}
set_exception_handler('exception_handler');
function fatal_error_handler(){
if (($error = error_get_last()) && $error['type'] == E_ERROR) {
error('Caught fatal error: ' . $error['message'] . ' in ' . $error['file'] . ' at line ' . $error['line']);
}
}
register_shutdown_function('fatal_error_handler');
$error_recursion=false;
function error($message, $priority = true, $debug_stuff = false) {
global $board, $mod, $config, $db_error, $error_recursion;
if($error_recursion!==false){
die("Error recursion detected with " . $message . "<br>Original error:".$error_recursion);
}
$error_recursion=$message;
if (defined('STDIN')) {
// Running from CLI
echo('Error: ' . $message . "\n");
debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
die();
}
if(!empty($config)){
if ($config['syslog'] && $priority !== false) {
// Use LOG_NOTICE instead of LOG_ERR or LOG_WARNING because most error message are not significant.
_syslog($priority !== true ? $priority : LOG_NOTICE, $message);
}
if ($config['debug']) {
$debug_stuff=array();
if(isset($db_error)){
$debug_stuff = array_combine(array('SQLSTATE', 'Error code', 'Error message'), $db_error);
}
$debug_stuff['backtrace'] = debug_backtrace();
$pw = $config['db']['password'];
$debug_callback = function(&$item) use (&$debug_callback, $pw) {
if (is_array($item)) {
return array_map($item, $debug_callback);
}
return ($item == $pw ? 'hunter2' : $item);
};
$debug_stuff = array_map($debug_stuff, $debug_callback);
}
}
if (isset($_POST['json_response'])) {
header('Content-Type: text/json; charset=utf-8');
$data=array('error'=>$message);
if(!empty($config) && $config['debug']){
$data['debug']=$debug_stuff;
}
print json_encode($data);
exit();
}
header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error');
die(Element('page.html', array(
'config' => $config,
'title' => _('Error'),
'subtitle' => _('An error has occured.'),
'body' => Element('error.html', array(
'config' => $config,
'message' => $message,
'mod' => $mod,
'board' => isset($board) ? $board : false,
'debug' => str_replace("\n", '&#10;', utf8tohtml(print_r($debug_stuff, true)))
))
)));
}

View File

@ -12,7 +12,7 @@ if (realpath($_SERVER['SCRIPT_FILENAME']) == str_replace('\\', '/', __FILE__)) {
define('TINYBOARD', null);
$microtime_start = microtime(true);
require_once 'inc/error.php';
require_once 'inc/cache.php';
require_once 'inc/display.php';
require_once 'inc/template.php';
@ -26,10 +26,11 @@ if (!extension_loaded('gettext')) {
// the user is not currently logged in as a moderator
$mod = false;
register_shutdown_function('fatal_error_handler');
mb_internal_encoding('UTF-8');
loadConfig();
function init_locale($locale) {
function init_locale($locale, $error='error') {
if (extension_loaded('gettext')) {
if (setlocale(LC_ALL, $locale) === false) {
//$error('The specified locale (' . $locale . ') does not exist on your platform!');
@ -39,7 +40,7 @@ function init_locale($locale) {
textdomain('tinyboard');
} else {
if (_setlocale(LC_ALL, $locale) === false) {
error('The specified locale (' . $locale . ') does not exist on your platform!');
$error('The specified locale (' . $locale . ') does not exist on your platform!');
}
_bindtextdomain('tinyboard', './inc/locale');
_bind_textdomain_codeset('tinyboard', 'UTF-8');
@ -52,6 +53,8 @@ $current_locale = 'en';
function loadConfig() {
global $board, $config, $__ip, $debug, $__version, $microtime_start, $current_locale, $events;
$error = function_exists('error') ? 'error' : 'basic_error_function_because_the_other_isnt_loaded_yet';
$boardsuffix = isset($board['uri']) ? $board['uri'] : '';
if (!isset($_SERVER['REMOTE_ADDR']))
@ -74,7 +77,7 @@ function loadConfig() {
if ($config['locale'] != $current_locale) {
$current_locale = $config['locale'];
init_locale($config['locale']);
init_locale($config['locale'], $error);
}
}
else {
@ -115,7 +118,7 @@ function loadConfig() {
}
if (!file_exists('inc/instance-config.php'))
error('Tinyboard is not configured! Create inc/instance-config.php.');
$error('Tinyboard is not configured! Create inc/instance-config.php.');
// Initialize locale as early as possible
@ -145,7 +148,7 @@ function loadConfig() {
if ($config['locale'] != $current_locale) {
$current_locale = $config['locale'];
init_locale($config['locale']);
init_locale($config['locale'], $error);
}
require 'inc/config.php';
@ -158,7 +161,7 @@ function loadConfig() {
if ($config['locale'] != $current_locale) {
$current_locale = $config['locale'];
init_locale($config['locale']);
init_locale($config['locale'], $error);
}
if (!isset($config['global_message']))
@ -268,6 +271,7 @@ function loadConfig() {
$_SERVER['REMOTE_ADDR'] = $m[2];
if ($config['verbose_errors']) {
set_error_handler('verbose_error_handler');
error_reporting(E_ALL);
ini_set('display_errors', true);
ini_set('html_errors', false);
@ -328,6 +332,36 @@ function loadConfig() {
}
}
function basic_error_function_because_the_other_isnt_loaded_yet($message, $priority = true) {
global $config;
if ($config['syslog'] && $priority !== false) {
// Use LOG_NOTICE instead of LOG_ERR or LOG_WARNING because most error message are not significant.
_syslog($priority !== true ? $priority : LOG_NOTICE, $message);
}
// Yes, this is horrible.
die('<!DOCTYPE html><html><head><title>Error</title>' .
'<style type="text/css">' .
'body{text-align:center;font-family:arial, helvetica, sans-serif;font-size:10pt;}' .
'p{padding:0;margin:20px 0;}' .
'p.c{font-size:11px;}' .
'</style></head>' .
'<body><h2>Error</h2>' . $message . '<hr/>' .
'<p class="c">This alternative error page is being displayed because the other couldn\'t be found or hasn\'t loaded yet.</p></body></html>');
}
function fatal_error_handler() {
if ($error = error_get_last()) {
if ($error['type'] == E_ERROR) {
if (function_exists('error')) {
error('Caught fatal error: ' . $error['message'] . ' in <strong>' . $error['file'] . '</strong> on line ' . $error['line'], LOG_ERR);
} else {
basic_error_function_because_the_other_isnt_loaded_yet('Caught fatal error: ' . $error['message'] . ' in ' . $error['file'] . ' on line ' . $error['line'], LOG_ERR);
}
}
}
}
function _syslog($priority, $message) {
if (isset($_SERVER['REMOTE_ADDR'], $_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI'])) {
@ -338,6 +372,16 @@ function _syslog($priority, $message) {
}
}
function verbose_error_handler($errno, $errstr, $errfile, $errline) {
if (error_reporting() == 0)
return false; // Looks like this warning was suppressed by the @ operator.
error(utf8tohtml($errstr), true, array(
'file' => $errfile . ':' . $errline,
'errno' => $errno,
'error' => $errstr,
'backtrace' => array_slice(debug_backtrace(), 1)
));
}
function define_groups() {
global $config;