From 5eb8df7432a708284862e4b126e418265d36b4ab Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Mon, 2 May 2022 18:49:09 +0200 Subject: [inc/Util] Add types, move error printing functions to their own class --- inc/errorhandler.inc.php | 148 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 148 insertions(+) create mode 100644 inc/errorhandler.inc.php (limited to 'inc/errorhandler.inc.php') diff --git a/inc/errorhandler.inc.php b/inc/errorhandler.inc.php new file mode 100644 index 00000000..c7a32b02 --- /dev/null +++ b/inc/errorhandler.inc.php @@ -0,0 +1,148 @@ +Fatal Error'; + echo '

Flagrant System error

'; + echo "

Message

$message
"; + if (strpos($message, 'Database') !== false) { + echo '
Try running database setup
'; + } + echo "

"; + if (defined('CONFIG_DEBUG') && CONFIG_DEBUG) { + global $SLX_ERRORS; + if (!empty($SLX_ERRORS)) { + echo '

PHP Errors

';
+				foreach ($SLX_ERRORS as $error) {
+					echo htmlspecialchars("{$error['errstr']} ({$error['errfile']}:{$error['errline']}\n");
+				}
+				echo '
'; + } + echo "

Stack Trace

"; + echo '
', self::formatBacktraceHtml(debug_backtrace()), '
'; + echo "

Globals

";
+			echo htmlspecialchars(print_r($GLOBALS, true));
+			echo '
'; + } else { + echo << +________________________¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶________ +____________________¶¶¶___________________¶¶¶¶_____ +________________¶¶¶_________________________¶¶¶¶___ +______________¶¶______________________________¶¶¶__ +___________¶¶¶_________________________________¶¶¶_ +_________¶¶_____________________________________¶¶¶ +________¶¶_________¶¶¶¶¶___________¶¶¶¶¶_________¶¶ +______¶¶__________¶¶¶¶¶¶__________¶¶¶¶¶¶_________¶¶ +_____¶¶___________¶¶¶¶____________¶¶¶¶___________¶¶ +____¶¶___________________________________________¶¶ +___¶¶___________________________________________¶¶_ +__¶¶____________________¶¶¶¶____________________¶¶_ +_¶¶_______________¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶______________¶¶__ +_¶¶____________¶¶¶¶___________¶¶¶¶¶___________¶¶___ +¶¶¶_________¶¶¶__________________¶¶__________¶¶____ +¶¶_________¶______________________¶¶________¶¶_____ +¶¶¶______¶________________________¶¶_______¶¶______ +¶¶¶_____¶_________________________¶¶_____¶¶________ +_¶¶¶___________________________________¶¶__________ +__¶¶¶________________________________¶¶____________ +___¶¶¶____________________________¶¶_______________ +____¶¶¶¶______________________¶¶¶__________________ +_______¶¶¶¶¶_____________¶¶¶¶¶_____________________ + +SADFACE; + } + echo ''; + exit(0); + } + + public static function formatBacktraceHtml($trace): string + { + $output = ''; + foreach ($trace as $idx => $line) { + $args = array(); + foreach ($line['args'] as $arg) { + $arg = self::formatArgument($arg); + $args[] = '' . htmlspecialchars($arg) . ''; + } + $frame = str_pad('#' . $idx, 3, ' ', STR_PAD_LEFT); + $function = htmlspecialchars($line['function']); + $args = implode(', ', $args); + $file = preg_replace('~(/[^/]+)$~', '$1', htmlspecialchars($line['file'])); + // Add line + $output .= $frame . ' ' . $function . '(' + . $args . ')' . ' @ ' . $file . ':' . $line['line'] . "\n"; + } + return $output; + } + + public static function formatBacktracePlain($trace): string + { + $output = ''; + foreach ($trace as $idx => $line) { + $args = array(); + foreach ($line['args'] as $arg) { + $args[] = self::formatArgument($arg); + } + $frame = str_pad('#' . $idx, 3, ' ', STR_PAD_LEFT); + $args = implode(', ', $args); + // Add line + $output .= "\n" . $frame . ' ' . $line['function'] . '(' + . $args . ')' . ' @ ' . $line['file'] . ':' . $line['line']; + } + return $output; + } + + private static function formatArgument($arg, $expandArray = true) + { + if (is_string($arg)) { + $arg = "'$arg'"; + } elseif (is_object($arg)) { + $arg = 'instanceof ' . get_class($arg); + } elseif (is_array($arg)) { + if ($expandArray && count($arg) < 20) { + $expanded = ''; + foreach ($arg as $key => $value) { + if (!empty($expanded)) { + $expanded .= ', '; + } + $expanded .= $key . ': ' . self::formatArgument($value, false); + if (strlen($expanded) > 200) + break; + } + if (strlen($expanded) <= 200) + return '[' . $expanded . ']'; + } + $arg = 'Array(' . count($arg) . ')'; + } + return $arg; + } +} \ No newline at end of file -- cgit v1.2.3-55-g7522