leftypol/inc/error.php

94 lines
2.5 KiB
PHP
Raw Permalink Normal View History

<?php
2016-09-01 20:45:52 +01:00
2024-06-23 22:22:02 +02:00
function error_handler($errno, $errstr, $errfile, $errline) {
2024-06-23 22:35:34 +02:00
if (error_reporting() & $errno) {
$config['debug'] = true;
error("$errstr in $errfile at line $errline");
2016-09-01 20:45:52 +01:00
}
return false;
}
2024-06-23 22:35:34 +02:00
function exception_handler($e) {
2024-07-13 11:20:22 +02:00
error_log($e);
2016-09-01 20:45:52 +01:00
error($e->getMessage());
}
set_exception_handler('exception_handler');
2024-06-23 22:35:34 +02:00
function fatal_error_handler() {
2016-09-01 20:45:52 +01:00
if (($error = error_get_last()) && $error['type'] == E_ERROR) {
2024-06-23 22:35:34 +02:00
error("Caught fatal error: {$error['message']} in {$error['file']} at line {$error['line']}");
2016-09-01 20:45:52 +01:00
}
}
register_shutdown_function('fatal_error_handler');
// Due to composer autoload, this isn't implicitly global anymore
global $error_recursion;
2024-06-23 22:35:34 +02:00
$error_recursion = false;
2016-09-01 20:45:52 +01:00
function error($message, $priority = true, $debug_stuff = false) {
global $board, $mod, $config, $db_error, $error_recursion;
2024-06-23 14:21:10 +02:00
2024-06-23 22:35:34 +02:00
if($error_recursion !== false) {
die("Error recursion detected with $message<br>Original error: $error_recursion");
2016-09-01 20:45:52 +01:00
}
2024-06-23 14:21:10 +02:00
2024-06-23 22:35:34 +02:00
$error_recursion = $message;
2024-06-23 14:21:10 +02:00
2016-09-01 20:45:52 +01:00
if (defined('STDIN')) {
// Running from CLI
2024-06-23 22:35:34 +02:00
echo("Error: $message\n");
2016-09-01 20:45:52 +01:00
debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
die();
}
2024-06-23 14:21:10 +02:00
2024-06-23 22:35:34 +02:00
if (!empty($config)) {
2016-09-01 20:45:52 +01:00
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']) {
2024-06-23 22:35:34 +02:00
$debug_stuff = [];
if (isset($db_error)) {
$debug_stuff = array_combine([ 'SQLSTATE', 'Error code', 'Error message' ], $db_error);
2016-09-01 20:45:52 +01:00
}
$debug_stuff['backtrace'] = debug_backtrace();
$pw = $config['db']['password'];
2024-06-23 15:19:51 +02:00
$debug_callback = function($item) use (&$debug_callback, $pw) {
2016-09-01 20:45:52 +01:00
if (is_array($item)) {
2024-06-23 15:19:51 +02:00
$item = array_filter($item, $debug_callback);
2016-09-01 20:45:52 +01:00
}
2024-06-23 22:35:34 +02:00
return $item !== $pw || !$pw;
2016-09-01 20:45:52 +01:00
};
$debug_stuff = array_map($debug_callback, $debug_stuff);
}
}
if (isset($_POST['json_response'])) {
header('Content-Type: text/json; charset=utf-8');
2024-06-23 22:35:34 +02:00
$data = [ 'error' => $message ];
if (!empty($config) && $config['debug']) {
$data['debug'] = $debug_stuff;
2016-09-01 20:45:52 +01:00
}
print json_encode($data);
exit();
}
2024-06-23 22:35:34 +02:00
header("{$_SERVER['SERVER_PROTOCOL']} 500 Internal Server Error");
2024-06-23 14:21:10 +02:00
2024-06-23 22:35:34 +02:00
die(Element('page.html', [
2016-09-01 20:45:52 +01:00
'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)))
))
2024-06-23 22:35:34 +02:00
]));
2016-09-01 20:45:52 +01:00
}