Overview
Comment: | ErrorManager: send HTML in emails, fix context for user agent, add more context |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | 7.3 |
Files: | files | file ages | folders |
SHA1: |
ee11bf91e20ebcb9dd4207431d924dfa |
User & Date: | bohwaz on 2021-11-08 16:44:12 |
Other Links: | branch diff | manifest | tags |
Context
2021-11-08
| ||
16:56 | ErrorManager: missing space in headers check-in: 19dd07ec44 user: bohwaz tags: 7.3 | |
16:44 | ErrorManager: send HTML in emails, fix context for user agent, add more context check-in: ee11bf91e2 user: bohwaz tags: 7.3 | |
2021-11-04
| ||
22:27 | Smartyer: handle relative paths for Windows targets check-in: 892ff930e3 user: bohwaz tags: 7.3 | |
Changes
Modified src/lib/KD2/ErrorManager.php from [5a81091db3] to [eb04e247c9].
︙ | ︙ | |||
252 253 254 255 256 257 258 | // Log exception to file if (ini_get('error_log')) { error_log($log); } | < < < < < < < < < | > > > > | 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 | // Log exception to file if (ini_get('error_log')) { error_log($log); } $exception_title = $e->getMessage(); unset($e); // Disable any output if it was buffering if (ob_get_level()) { ob_end_clean(); } if (self::$enabled != self::PRODUCTION || self::$email_errors) { $html_report = self::htmlReport($report); } if (PHP_SAPI == 'cli') { foreach ($report->errors as $e) { self::termPrint(sprintf(' /!\\ %s ', $e->type), self::RED); self::termPrint($e->message, self::RED_FAINT); |
︙ | ︙ | |||
324 325 326 327 328 329 330 | { if (!headers_sent()) { header_remove(); header('Content-Type: text/html; charset=UTF-8', true); header('HTTP/1.1 500 Internal Server Error', true); } | < | | < | | < | < | < < < | < | < < < | < > > > > > > > > > > > > > > > > > > > > > > > > > > | | | 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 | { if (!headers_sent()) { header_remove(); header('Content-Type: text/html; charset=UTF-8', true); header('HTTP/1.1 500 Internal Server Error', true); } echo $html_report; } // Log exception to email if (self::$email_errors) { self::sendEmail($exception_title, $report, $log, $html_report); } // Send report to URL if (self::$report_auto && self::$report_url) { self::sendReport($report, self::$report_url); } if ($exit) { exit(1); } } static protected function sendEmail(string $title, \stdClass $report, string $log, string $html): void { // From: sender $from = !empty($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : basename($report->context->root_directory ?? __FILE__); $msgid = $report->context->id . '@' . $from; $boundary = sprintf('-----=%s', md5(uniqid(rand()))); $header = sprintf("MIME-Version: 1.0\r\nFrom: \"%s\" <%s>\r\nIn-Reply-To: <%s>\r\nMessage-Id: <%s>\r\n", $from, self::$email_errors, $msgid, $msgid); $header.= sprintf("Content-Type: multipart/mixed; boundary=\"%s\"\r\n", $boundary); $header.= "\r\n"; $msg = "This message contains multiple MIME parts.\r\n\r\n"; $msg.= sprintf("--%s\r\n", $boundary); $msg.= "Content-Type: text/plain; charset=\"utf-8\"\r\n"; $msg.= "Content-Transfer-Encoding:8bit\r\n\r\n"; $msg.= $log . "\r\n\r\n"; $msg.= sprintf("--%s\r\n", $boundary); $msg.= "Content-Type: text/html; charset=\"utf-8\"\r\n"; $msg.= "Content-Transfer-Encoding:8bit\r\n\r\n"; $msg.= $html . "\r\n\r\n"; $msg.= sprintf("--%s--", $boundary); mail(self::$email_errors, sprintf('Error #%s: %s', $report->context->id, $title), $msg, $header); } /** * Prints a line to STDERR, eventually using a color */ static public function termPrint($message, $color = null) { if ($color && self::$term_color) { $message = chr(27) . $color . $message . chr(27) . "[0m"; } fwrite(STDERR, $message . PHP_EOL); } /** * Return file location without the document root */ static protected function getFileLocation($file) { if (!empty(self::$context['root_directory']) && ($pos = strpos($file, self::$context['root_directory'])) === 0) { return '...' . substr($file, strlen(self::$context['root_directory'])); } return $file; } /** * Generates a report from an exception |
︙ | ︙ | |||
495 496 497 498 499 500 501 | $e = $e->getPrevious(); } $report->errors = array_reverse($report->errors); unset($error, $e, $params, $t); | | | | | | | | | | | > > > > > > > > > > | | | | | | | | | | | > > | 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 | $e = $e->getPrevious(); } $report->errors = array_reverse($report->errors); unset($error, $e, $params, $t); $context = array_merge([ 'id' => base_convert(substr(sha1(json_encode($report->errors)), 0, 10), 16, 36), 'date' => date(DATE_ATOM), 'os' => PHP_OS, 'language' => 'PHP ' . PHP_VERSION, 'environment' => self::$enabled == self::DEVELOPMENT ? 'development' : 'production:' . self::$enabled, 'php_sapi' => PHP_SAPI, 'remote_ip' => isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : null, 'http_method' => isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : null, 'http_files' => self::dump($_FILES), 'http_post' => self::dump(array_keys($_POST), true), 'duration' => isset(self::$context['request_started']) ? microtime(true) - self::$context['request_started'] : null, 'memory_peak' => memory_get_peak_usage(true), 'memory_used' => memory_get_usage(true), ], self::$context); ksort($context); unset($context['request_started']); $report->context = (object) $context; if (!empty($_SERVER['HTTP_HOST']) && !empty($_SERVER['REQUEST_URI'])) { $proto = empty($_SERVER['HTTPS']) ? 'http' : 'https'; $report->context->url = $proto . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; } return $report; } /** * Displays an exception as HTML debug page */ static public function htmlException(\stdClass $e): string { $out = sprintf('<section><header><h1>%s</h1><h2>%s</h2></header>', $e->type, nl2br(htmlspecialchars($e->message))); foreach ($e->backtrace as $i=>$t) { $out .= '<article>'; if (isset($t->file) && isset($t->line)) { $dir = dirname($t->file); $dir = $dir == '/' ? $dir : $dir . '/'; $out .= sprintf('<h3>in %s<b>%s</b>:<i>%d</i></h3>', htmlspecialchars($dir), htmlspecialchars(basename($t->file)), $t->line); } if (isset($t->function)) { $out .= sprintf('<h4>→ %s <i>(%d arg.)</i></h4>', htmlspecialchars($t->function), isset($t->args) ? count($t->args) : 0); // Display call arguments if (!empty($t->args)) { $out .= '<table>'; foreach ($t->args as $name => $value) { $out .= sprintf('<tr><th>%s</th><td><pre>%s</pre></td>', htmlspecialchars($name), htmlspecialchars($value)); } $out .= '</table>'; } } // Display source code if (isset($t->code) && isset($t->line)) { $out .= self::htmlSource($t->code, $t->line); } $out .= '</article>'; } $out .= '</section>'; return $out; } static public function htmlSource(array $source, $line) { $out = ''; |
︙ | ︙ | |||
654 655 656 657 658 659 660 661 662 663 664 665 666 667 | case 'report': return (!self::$report_auto && self::$report_url) ? $match[2] : ''; } }, $str); return $str; } /** * Enable error manager * @param integer $type Type of error management (ErrorManager::PRODUCTION or ErrorManager::DEVELOPMENT) * @return void */ static public function enable($type = self::DEVELOPMENT) | > > > > > > > > > > > > > > > > > > > > > > > > > > | 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 | case 'report': return (!self::$report_auto && self::$report_url) ? $match[2] : ''; } }, $str); return $str; } static public function htmlReport(\stdClass $report): string { $out = ''; // Display debug $out .= self::htmlTemplate(ini_get('error_prepend_string'), $report); foreach ($report->errors as $e) { $out .= self::htmlException($e); } $out .= '<section><article><h2>Context</h2><table>'; foreach ($report->context as $name => $value) { $out .= sprintf('<tr><th>%s</th><td>%s</td></tr>', htmlspecialchars($name), htmlspecialchars($value)); } $out .= '</table></article></section>'; $out .= self::htmlTemplate(ini_get('error_append_string'), $report); return $out; } /** * Enable error manager * @param integer $type Type of error management (ErrorManager::PRODUCTION or ErrorManager::DEVELOPMENT) * @return void */ static public function enable($type = self::DEVELOPMENT) |
︙ | ︙ | |||
707 708 709 710 711 712 713 | if ($type == self::DEVELOPMENT) { self::startTimer('_global'); } // Assign default context static $defaults = [ | | | > | | | > > | 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 | if ($type == self::DEVELOPMENT) { self::startTimer('_global'); } // Assign default context static $defaults = [ 'hostname' => 'SERVER_NAME', 'http_user_agent' => 'HTTP_USER_AGENT', 'http_referrer' => 'HTTP_REFERER', 'user_addr' => 'REMOTE_ADDR', 'server_addr' => 'SERVER_ADDR', 'root_directory' => 'DOCUMENT_ROOT', ]; foreach ($defaults as $a => $b) { if (isset($_SERVER[$b]) && !isset(self::$context[$a])) { self::$context[$a] = $_SERVER[$b]; } } self::$context['request_started'] = microtime(true); } /** * Reset error management to PHP defaults * @return boolean */ static public function disable() |
︙ | ︙ | |||
797 798 799 800 801 802 803 | /** * Set the report context * @param mixed $env Variable content, could be application version, or an array of information... */ static public function setContext(array $context) { | | | 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 | /** * Set the report context * @param mixed $env Variable content, could be application version, or an array of information... */ static public function setContext(array $context) { self::$context = array_merge(self::$context, $context); } /** * Enable or disable reporting of errors to a remote URL * @param null|string $url Reporting URL * @param boolean $auto Automatic reporting? If not users will be able to report the error by clicking a button on the error page */ |
︙ | ︙ | |||
849 850 851 852 853 854 855 856 857 858 | { self::$custom_handlers[$class] = $callback; } /** * Copy of var_dump but returns a string instead of a variable * @param mixed $var variable to dump * @param integer $level Indentation level (internal use) * @return string */ | > | | | 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 | { self::$custom_handlers[$class] = $callback; } /** * Copy of var_dump but returns a string instead of a variable * @param mixed $var variable to dump * @param bool $hide_values Do not return values if set to TRUE * @param integer $level Indentation level (internal use) * @return string */ static public function dump($var, $hide_values = false, $level = 0) { if ($level > 20) { return '*RECURSION*'; } switch (gettype($var)) { case 'boolean': return 'bool(' . ($var ? 'true' : 'false') . ')'; case 'integer': return 'int(' . $var . ')'; case 'double': return 'float(' . $var . ')'; case 'string': return 'string(' . strlen($var) . ') "' . ($hide_values ? '***HIDDEN***' : $var) . '"'; case 'NULL': return 'NULL'; case 'resource': return 'resource(' . (int)$var . ') of type (' . get_resource_type($var) . ')'; case 'array': case 'object': if (is_object($var)) |
︙ | ︙ | |||
894 895 896 897 898 899 900 | $var = @$var->valid() ? @iterator_to_array($var) : []; } foreach ($var as $key=>$value) { $out .= str_repeat(' ', $level * 2); $out .= is_string($key) ? '["' . $key . '"]' : '[' . $key . ']'; | | | 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 | $var = @$var->valid() ? @iterator_to_array($var) : []; } foreach ($var as $key=>$value) { $out .= str_repeat(' ', $level * 2); $out .= is_string($key) ? '["' . $key . '"]' : '[' . $key . ']'; $out .= '=> ' . self::dump($value, $hide_values, $level) . PHP_EOL; } $out .= str_repeat(' ', --$level * 2) . '}'; return $out; default: return gettype($var); } |
︙ | ︙ |