/** * Handler to catch uncaught exception. * @param \Exception * @return void * @internal */ public static function _exceptionHandler(\Exception $exception, $shutdown = FALSE) { if (!self::$enabled) { return; } self::$enabled = FALSE; // prevent double rendering if (!headers_sent()) { $protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1'; $code = isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE ') !== FALSE ? 503 : 500; header("{$protocol} {$code}", TRUE, $code); } try { if (self::$productionMode) { try { self::log($exception, self::ERROR); } catch (\Exception $e) { echo 'FATAL ERROR: unable to log error'; } if (self::isHtmlMode()) { require __DIR__ . '/templates/error.phtml'; } else { echo "ERROR: the server encountered an internal error and was unable to complete your request.\n"; } } else { if (!connection_aborted() && self::isHtmlMode()) { self::getBlueScreen()->render($exception); self::getBar()->render(); } elseif (connection_aborted() || !self::fireLog($exception)) { $file = self::log($exception, self::ERROR); if (!headers_sent()) { header("X-Nette-Error-Log: {$file}"); } echo "{$exception}\n" . ($file ? "(stored in {$file})\n" : ''); if (self::$browser) { exec(self::$browser . ' ' . escapeshellarg($file)); } } } foreach (self::$onFatalError as $handler) { call_user_func($handler, $exception); } } catch (\Exception $e) { if (self::$productionMode) { echo self::isHtmlMode() ? '<meta name=robots content=noindex>FATAL ERROR' : 'FATAL ERROR'; } else { echo "FATAL ERROR: thrown ", get_class($e), ': ', $e->getMessage(), "\nwhile processing ", get_class($exception), ': ', $exception->getMessage(), "\n"; } } if (!$shutdown) { exit(254); } }
/** * Handler to catch uncaught exception. * @param \Exception * @return void * @internal */ public static function _exceptionHandler(\Exception $exception) { if (!headers_sent()) { // for PHP < 5.2.4 $protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1'; header($protocol . ' 500', TRUE, 500); } try { if (self::$productionMode) { try { self::log($exception, self::ERROR); } catch (\Exception $e) { echo 'FATAL ERROR: unable to log error'; } if (self::$consoleMode) { echo "ERROR: the server encountered an internal error and was unable to complete your request.\n"; } elseif (self::isHtmlMode()) { require __DIR__ . '/templates/error.phtml'; } } else { if (self::$consoleMode) { // dump to console echo "{$exception}\n"; if ($file = self::log($exception)) { echo "(stored in {$file})\n"; if (self::$browser) { exec(self::$browser . ' ' . escapeshellarg($file)); } } } elseif (self::isHtmlMode()) { // dump to browser self::$blueScreen->render($exception); if (self::$bar) { self::$bar->render(); } } elseif (!self::fireLog($exception, self::ERROR)) { // AJAX or non-HTML mode $file = self::log($exception); if (!headers_sent()) { header("X-Nette-Error-Log: {$file}"); } } } foreach (self::$onFatalError as $handler) { call_user_func($handler, $exception); } } catch (\Exception $e) { if (self::$productionMode) { echo self::isHtmlMode() ? '<meta name=robots content=noindex>FATAL ERROR' : 'FATAL ERROR'; } else { echo "FATAL ERROR: thrown ", get_class($e), ': ', $e->getMessage(), "\nwhile processing ", get_class($exception), ': ', $exception->getMessage(), "\n"; } } self::$enabled = FALSE; // un-register shutdown function exit(255); }
static function _exceptionHandler(\Exception $exception) { if (!headers_sent()) { $protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1'; $code = isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE ') !== FALSE ? 503 : 500; header("{$protocol} {$code}", TRUE, $code); } try { if (self::$productionMode) { try { self::log($exception, self::ERROR); } catch (\Exception $e) { echo 'FATAL ERROR: unable to log error'; } if (self::$consoleMode) { echo "ERROR: the server encountered an internal error and was unable to complete your request.\n"; } elseif (self::isHtmlMode()) { ?> <!DOCTYPE html> <meta charset="utf-8"> <meta name=robots content=noindex> <meta name=generator content="Nette Framework"> <style>body{color:#333;background:white;width:500px;margin:100px auto}h1{font:bold 47px/1.5 sans-serif;margin:.6em 0}p{font:21px/1.5 Georgia,serif;margin:1.5em 0}small{font-size:70%;color:gray}</style> <title>Server Error</title> <h1>Server Error</h1> <p>We're sorry! The server encountered an internal error and was unable to complete your request. Please try again later.</p> <p><small>error 500</small></p> <?php } } else { if (self::$consoleMode) { echo "{$exception}\n"; if ($file = self::log($exception)) { echo "(stored in {$file})\n"; if (self::$browser) { exec(self::$browser . ' ' . escapeshellarg($file)); } } } elseif (self::isHtmlMode()) { self::$blueScreen->render($exception); if (self::$bar) { self::$bar->render(); } } elseif (!self::fireLog($exception)) { $file = self::log($exception, self::ERROR); if (!headers_sent()) { header("X-Nette-Error-Log: {$file}"); } } } foreach (self::$onFatalError as $handler) { call_user_func($handler, $exception); } } catch (\Exception $e) { if (self::$productionMode) { echo self::isHtmlMode() ? '<meta name=robots content=noindex>FATAL ERROR' : 'FATAL ERROR'; } else { echo "FATAL ERROR: thrown ", get_class($e), ': ', $e->getMessage(), "\nwhile processing ", get_class($exception), ': ', $exception->getMessage(), "\n"; } } self::$enabled = FALSE; exit(254); }
/** * Handler to catch uncaught exception. * @param \Exception * @return void * @internal */ public static function _exceptionHandler(\Exception $exception) { if (!headers_sent()) { // for PHP < 5.2.4 header('HTTP/1.1 500 Internal Server Error'); } $htmlMode = !self::$ajaxDetected && !preg_match('#^Content-Type: (?!text/html)#im', implode("\n", headers_list())); try { if (self::$productionMode) { self::log($exception, self::ERROR); if (self::$consoleMode) { echo "ERROR: the server encountered an internal error and was unable to complete your request.\n"; } elseif ($htmlMode) { require __DIR__ . '/templates/error.phtml'; } } else { if (self::$consoleMode) { // dump to console echo "{$exception}\n"; } elseif ($htmlMode) { // dump to browser self::$blueScreen->render($exception); if (self::$bar) { self::$bar->render(); } } elseif (!self::fireLog($exception, self::ERROR)) { // AJAX or non-HTML mode self::log($exception); } } foreach (self::$onFatalError as $handler) { call_user_func($handler, $exception); } } catch (\Exception $e) { echo "\nNette\\Debug FATAL ERROR: thrown ", get_class($e), ': ', $e->getMessage(), "\nwhile processing ", get_class($exception), ': ', $exception->getMessage(), "\n"; } self::$enabled = FALSE; // un-register shutdown function exit(255); }
/** * Handler to catch uncaught exception. * @param \Exception * @return void * @internal */ public static function _exceptionHandler(\Exception $exception) { if (!headers_sent()) { // for PHP < 5.2.4 header('HTTP/1.1 500 Internal Server Error'); } try { if (self::$productionMode) { try { self::log($exception, self::ERROR); } catch (\Exception $e) { echo 'FATAL ERROR: unable to log error'; } if (self::$consoleMode) { echo "ERROR: the server encountered an internal error and was unable to complete your request.\n"; } elseif (self::isHtmlMode()) { require __DIR__ . '/templates/error.phtml'; } } else { if (self::$consoleMode) { // dump to console echo "{$exception}\n"; } elseif (self::isHtmlMode()) { // dump to browser self::$blueScreen->render($exception); if (self::$bar) { self::$bar->render(); } } elseif (!self::fireLog($exception, self::ERROR)) { // AJAX or non-HTML mode self::log($exception); } } foreach (self::$onFatalError as $handler) { call_user_func($handler, $exception); } } catch (\Exception $e) { if (self::$productionMode) { echo self::isHtmlMode() ? '<meta name=robots content=noindex>FATAL ERROR' : 'FATAL ERROR'; } else { echo "FATAL ERROR: thrown ", get_class($e), ': ', $e->getMessage(), "\nwhile processing ", get_class($exception), ': ', $exception->getMessage(), "\n"; } } self::$enabled = FALSE; // un-register shutdown function exit(255); }
/** * Enables displaying or logging errors and exceptions. * @param mixed production, development mode, autodetection or IP address(es) whitelist. * @param string error log directory; enables logging in production mode, FALSE means that logging is disabled * @param string administrator email; enables email sending in production mode * @return void */ public static function enable($mode = NULL, $logDirectory = NULL, $email = NULL) { error_reporting(E_ALL | E_STRICT); // production/development mode detection if (is_bool($mode)) { self::$productionMode = $mode; } elseif (is_string($mode)) { // IP addresses $mode = preg_split('#[,\\s]+#', "{$mode} 127.0.0.1 ::1"); } if (is_array($mode)) { // IP addresses whitelist detection self::$productionMode = !isset($_SERVER['REMOTE_ADDR']) || !in_array($_SERVER['REMOTE_ADDR'], $mode, TRUE); } if (self::$productionMode === self::DETECT) { if (class_exists('Nette\\Environment')) { self::$productionMode = Nette\Environment::isProduction(); } elseif (isset($_SERVER['SERVER_ADDR']) || isset($_SERVER['LOCAL_ADDR'])) { // IP address based detection $addrs = array(); if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { // proxy server detected $addrs = preg_split('#,\\s*#', $_SERVER['HTTP_X_FORWARDED_FOR']); } if (isset($_SERVER['REMOTE_ADDR'])) { $addrs[] = $_SERVER['REMOTE_ADDR']; } $addrs[] = isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : $_SERVER['LOCAL_ADDR']; self::$productionMode = FALSE; foreach ($addrs as $addr) { $oct = explode('.', $addr); if ($addr !== '::1' && (count($oct) !== 4 || $oct[0] !== '10' && $oct[0] !== '127' && ($oct[0] !== '172' || $oct[1] < 16 || $oct[1] > 31) && ($oct[0] !== '169' || $oct[1] !== '254') && ($oct[0] !== '192' || $oct[1] !== '168'))) { self::$productionMode = TRUE; break; } } } else { self::$productionMode = !self::$consoleMode; } } // logging configuration if (is_string($logDirectory)) { self::$logDirectory = realpath($logDirectory); if (self::$logDirectory === FALSE) { throw new Nette\DirectoryNotFoundException("Directory '{$logDirectory}' is not found."); } } elseif ($logDirectory === FALSE) { self::$logDirectory = FALSE; } else { self::$logDirectory = defined('APP_DIR') ? APP_DIR . '/../log' : getcwd() . '/log'; } if (self::$logDirectory) { ini_set('error_log', self::$logDirectory . '/php_error.log'); } // php configuration if (function_exists('ini_set')) { ini_set('display_errors', !self::$productionMode); // or 'stderr' ini_set('html_errors', FALSE); ini_set('log_errors', FALSE); } elseif (ini_get('display_errors') != !self::$productionMode && ini_get('display_errors') !== (self::$productionMode ? 'stderr' : 'stdout')) { // intentionally == throw new Nette\NotSupportedException('Function ini_set() must be enabled.'); } if ($email) { if (!is_string($email)) { throw new Nette\InvalidArgumentException('Email address must be a string.'); } self::$email = $email; } if (!defined('E_DEPRECATED')) { define('E_DEPRECATED', 8192); } if (!defined('E_USER_DEPRECATED')) { define('E_USER_DEPRECATED', 16384); } if (!self::$enabled) { register_shutdown_function(array(__CLASS__, '_shutdownHandler')); set_exception_handler(array(__CLASS__, '_exceptionHandler')); set_error_handler(array(__CLASS__, '_errorHandler')); self::$enabled = TRUE; } }
static function _exceptionHandler(\Exception $exception) { if (!headers_sent()) { header('HTTP/1.1 500 Internal Server Error'); } try { if (self::$productionMode) { try { self::log($exception, self::ERROR); } catch (\Exception $e) { echo 'FATAL ERROR: unable to log error'; } if (self::$consoleMode) { echo "ERROR: the server encountered an internal error and was unable to complete your request.\n"; } elseif (self::isHtmlMode()) { ?> <!DOCTYPE html> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name=robots content=noindex><meta name=generator content="Nette Framework"> <style>body{color:#333;background:white;width:500px;margin:100px auto}h1{font:bold 47px/1.5 sans-serif;margin:.6em 0}p{font:21px/1.5 Georgia,serif;margin:1.5em 0}small{font-size:70%;color:gray}</style> <title>Server Error</title> <h1>Server Error</h1> <p>We're sorry! The server encountered an internal error and was unable to complete your request. Please try again later.</p> <p><small>error 500</small></p> <?php } } else { if (self::$consoleMode) { echo "{$exception}\n"; } elseif (self::isHtmlMode()) { self::$blueScreen->render($exception); if (self::$bar) { self::$bar->render(); } } elseif (!self::fireLog($exception, self::ERROR)) { self::log($exception); } } foreach (self::$onFatalError as $handler) { call_user_func($handler, $exception); } } catch (\Exception $e) { if (self::$productionMode) { echo self::isHtmlMode() ? '<meta name=robots content=noindex>FATAL ERROR' : 'FATAL ERROR'; } else { echo "FATAL ERROR: thrown ", get_class($e), ': ', $e->getMessage(), "\nwhile processing ", get_class($exception), ': ', $exception->getMessage(), "\n"; } } self::$enabled = FALSE; exit(255); }