From 06a6ae7bea8fad080997e1ec1fd6595b5c164257 Mon Sep 17 00:00:00 2001 From: jove Date: Mon, 22 Aug 2016 21:56:32 +0100 Subject: [PATCH 1/4] Refactors the error handling function. --- inc/display.php | 47 +++++++++++++++++++++-------------------------- 1 file changed, 21 insertions(+), 26 deletions(-) diff --git a/inc/display.php b/inc/display.php index 90cf3434..d7cb3355 100644 --- a/inc/display.php +++ b/inc/display.php @@ -73,7 +73,7 @@ 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); @@ -86,40 +86,35 @@ function error($message, $priority = true, $debug_stuff = false) { die(); } - if ($config['debug'] && isset($db_error)) { - $debug_stuff = array_combine(array('SQLSTATE', 'Error code', 'Error message'), $db_error); - } - 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); } - // 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'); - } + header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error'); // 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); + $data=array('error'=>$message); + if($config['debug']){ + $data['debug']=$debug_stuff; } - return ($item !== $pw || !$pw); - }; - + print json_encode($data); + exit(); + } - if ($debug_stuff) - $debug_stuff = array_filter($debug_stuff, $debug_callback); die(Element('page.html', array( 'config' => $config, @@ -130,7 +125,7 @@ function error($message, $priority = true, $debug_stuff = false) { '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) + 'debug' => str_replace("\n", ' ', utf8tohtml(print_r($debug_stuff, true))) )) ))); } From d0d83ab8d7669a38700dfc04229a279f6c0e482c Mon Sep 17 00:00:00 2001 From: ReCaffeinated Date: Mon, 22 Aug 2016 15:35:36 -0500 Subject: [PATCH 2/4] We're reworking the error handler# --- :q | 9 +++++++++ inc/functions.php | 46 +++------------------------------------------- post.php | 2 +- 3 files changed, 13 insertions(+), 44 deletions(-) create mode 100644 :q diff --git a/:q b/:q new file mode 100644 index 00000000..1423a79e --- /dev/null +++ b/:q @@ -0,0 +1,9 @@ + +# Please enter the commit message for your changes. Lines starting +# with '#' will be ignored, and an empty message aborts the commit. +# On branch installfix +# Changes to be committed: +# modified: install.php +This prevents a crash due to instance-config.php not existing, it will now check if the file exists and if not creates the file + + diff --git a/inc/functions.php b/inc/functions.php index ff5d51df..759e6529 100755 --- a/inc/functions.php +++ b/inc/functions.php @@ -26,7 +26,7 @@ if (!extension_loaded('gettext')) { // the user is not currently logged in as a moderator $mod = false; -register_shutdown_function('fatal_error_handler'); +register_shutdown_function('error'); mb_internal_encoding('UTF-8'); loadConfig(); @@ -53,7 +53,7 @@ $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'; + $error = function_exists('error') ? 'error' : 'error'; $boardsuffix = isset($board['uri']) ? $board['uri'] : ''; @@ -271,7 +271,7 @@ function loadConfig() { $_SERVER['REMOTE_ADDR'] = $m[2]; if ($config['verbose_errors']) { - set_error_handler('verbose_error_handler'); + set_error_handler('error'); error_reporting(E_ALL); ini_set('display_errors', true); ini_set('html_errors', false); @@ -332,36 +332,6 @@ 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('Error' . - '' . - '

Error

' . $message . '
' . - '

This alternative error page is being displayed because the other couldn\'t be found or hasn\'t loaded yet.

'); -} - -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 ' . $error['file'] . ' 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'])) { @@ -372,16 +342,6 @@ 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; diff --git a/post.php b/post.php index 99ef4850..0022f178 100644 --- a/post.php +++ b/post.php @@ -325,7 +325,7 @@ if (isset($_POST['delete'])) { $post['file_tmp'] = tempnam($config['tmp'], 'url'); function unlink_tmp_file($file) { @unlink($file); - fatal_error_handler(); + error(); } register_shutdown_function('unlink_tmp_file', $post['file_tmp']); From 77b99b2c4f64b73b062f2d542fbf04903a1d814d Mon Sep 17 00:00:00 2001 From: jove Date: Mon, 22 Aug 2016 23:39:36 +0100 Subject: [PATCH 3/4] Finishes error reworking. --- inc/display.php | 58 --------------------------------- inc/error.php | 83 +++++++++++++++++++++++++++++++++++++++++++++++ inc/functions.php | 18 ++++------ 3 files changed, 90 insertions(+), 69 deletions(-) create mode 100644 inc/error.php diff --git a/inc/display.php b/inc/display.php index d7cb3355..ce39197c 100644 --- a/inc/display.php +++ b/inc/display.php @@ -71,64 +71,6 @@ 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']) { - $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); - } - - header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error'); - - // Is there a reason to disable this? - if (isset($_POST['json_response'])) { - header('Content-Type: text/json; charset=utf-8'); - $data=array('error'=>$message); - if($config['debug']){ - $data['debug']=$debug_stuff; - } - print json_encode($data); - exit(); - } - - - 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", ' ', utf8tohtml(print_r($debug_stuff, true))) - )) - ))); -} function loginForm($error=false, $username=false, $redirect=false) { global $config; diff --git a/inc/error.php b/inc/error.php new file mode 100644 index 00000000..9452e393 --- /dev/null +++ b/inc/error.php @@ -0,0 +1,83 @@ +getMessage()); +} + +set_exception_handler('exception_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 . "
Original error:".$error_recursion); + } + $error_recursion=$message; + + 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 (defined('STDIN')) { + // Running from CLI + echo('Error: ' . $message . "\n"); + debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); + die(); + } + + 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); + } + } + + // Is there a reason to disable this? + 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", ' ', utf8tohtml(print_r($debug_stuff, true))) + )) + ))); +} diff --git a/inc/functions.php b/inc/functions.php index 759e6529..e8beaba2 100755 --- a/inc/functions.php +++ b/inc/functions.php @@ -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,11 +26,10 @@ if (!extension_loaded('gettext')) { // the user is not currently logged in as a moderator $mod = false; -register_shutdown_function('error'); mb_internal_encoding('UTF-8'); loadConfig(); -function init_locale($locale, $error='error') { +function init_locale($locale) { if (extension_loaded('gettext')) { if (setlocale(LC_ALL, $locale) === false) { //$error('The specified locale (' . $locale . ') does not exist on your platform!'); @@ -40,7 +39,7 @@ function init_locale($locale, $error='error') { 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'); @@ -53,8 +52,6 @@ $current_locale = 'en'; function loadConfig() { global $board, $config, $__ip, $debug, $__version, $microtime_start, $current_locale, $events; - $error = function_exists('error') ? 'error' : 'error'; - $boardsuffix = isset($board['uri']) ? $board['uri'] : ''; if (!isset($_SERVER['REMOTE_ADDR'])) @@ -77,7 +74,7 @@ function loadConfig() { if ($config['locale'] != $current_locale) { $current_locale = $config['locale']; - init_locale($config['locale'], $error); + init_locale($config['locale']); } } else { @@ -118,7 +115,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 @@ -148,7 +145,7 @@ function loadConfig() { if ($config['locale'] != $current_locale) { $current_locale = $config['locale']; - init_locale($config['locale'], $error); + init_locale($config['locale']); } require 'inc/config.php'; @@ -161,7 +158,7 @@ function loadConfig() { if ($config['locale'] != $current_locale) { $current_locale = $config['locale']; - init_locale($config['locale'], $error); + init_locale($config['locale']); } if (!isset($config['global_message'])) @@ -271,7 +268,6 @@ function loadConfig() { $_SERVER['REMOTE_ADDR'] = $m[2]; if ($config['verbose_errors']) { - set_error_handler('error'); error_reporting(E_ALL); ini_set('display_errors', true); ini_set('html_errors', false); From f0a625b238ce09a2e5f905ca64f480f38d8ca668 Mon Sep 17 00:00:00 2001 From: jove Date: Wed, 31 Aug 2016 20:51:09 +0100 Subject: [PATCH 4/4] Adds proper fatal error handling and fixes CLI error reporting. --- :q | 9 --------- inc/error.php | 25 +++++++++++++++++-------- post.php | 2 +- 3 files changed, 18 insertions(+), 18 deletions(-) delete mode 100644 :q diff --git a/:q b/:q deleted file mode 100644 index 1423a79e..00000000 --- a/:q +++ /dev/null @@ -1,9 +0,0 @@ - -# Please enter the commit message for your changes. Lines starting -# with '#' will be ignored, and an empty message aborts the commit. -# On branch installfix -# Changes to be committed: -# modified: install.php -This prevents a crash due to instance-config.php not existing, it will now check if the file exists and if not creates the file - - diff --git a/inc/error.php b/inc/error.php index 9452e393..5138592c 100644 --- a/inc/error.php +++ b/inc/error.php @@ -15,15 +15,32 @@ function exception_handler(Exception $e){ 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 . "
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) { @@ -31,13 +48,6 @@ function error($message, $priority = true, $debug_stuff = false) { _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']) { $debug_stuff=array(); if(isset($db_error)){ @@ -55,7 +65,6 @@ function error($message, $priority = true, $debug_stuff = false) { } } - // Is there a reason to disable this? if (isset($_POST['json_response'])) { header('Content-Type: text/json; charset=utf-8'); $data=array('error'=>$message); diff --git a/post.php b/post.php index 0022f178..99ef4850 100644 --- a/post.php +++ b/post.php @@ -325,7 +325,7 @@ if (isset($_POST['delete'])) { $post['file_tmp'] = tempnam($config['tmp'], 'url'); function unlink_tmp_file($file) { @unlink($file); - error(); + fatal_error_handler(); } register_shutdown_function('unlink_tmp_file', $post['file_tmp']);