From e40ee176b7a9142f61644745991cb22564392741 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Mon, 15 Aug 2016 16:50:44 +0200 Subject: [util] traceError(): Don't output html junk if running via CLI --- inc/util.inc.php | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) (limited to 'inc/util.inc.php') diff --git a/inc/util.inc.php b/inc/util.inc.php index 8ad3928a..bee07765 100644 --- a/inc/util.inc.php +++ b/inc/util.inc.php @@ -14,7 +14,11 @@ class Util { if (defined('API') && API) { error_log('API ERROR: ' . $message); - error_log(print_r(debug_backtrace(), true)); + error_log(self::formatBacktracePlain(debug_backtrace())); + } + if (php_sapi_name() === 'cli') { + // Don't spam HTML when invoked via cli, above error_log should have gone to stdout/stderr + exit(1); } Header('HTTP/1.1 500 Internal Server Error'); Header('Content-Type: text/html; charset=utf-8'); @@ -38,7 +42,7 @@ class Util echo ''; } echo "

Stack Trace

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

Globals

";
 			echo print_r($GLOBALS, true);
 			echo '
'; @@ -75,7 +79,7 @@ SADFACE; exit(0); } - public static function formatBacktrace($trace, $escape = true) + public static function formatBacktraceHtml($trace, $escape = true) { $output = ''; foreach ($trace as $idx => $line) { @@ -101,6 +105,30 @@ SADFACE; return $output; } + public static function formatBacktracePlain($trace) + { + $output = ''; + foreach ($trace as $idx => $line) { + $args = array(); + foreach ($line['args'] as $arg) { + if (is_string($arg)) { + $arg = "'$arg'"; + } elseif (is_object($arg)) { + $arg = 'instanceof ' . get_class($arg); + } elseif (is_array($arg)) { + $arg = 'Array(' . count($arg) . ')'; + } + $args[] = $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; + } + /** * Redirects the user via a '302 Moved' header. * An active session will be saved, any messages that haven't -- cgit v1.2.3-55-g7522