Beispiel #1
0
 /**
  * Enables displaying or logging errors and exceptions.
  * @param  mixed   production, development mode, autodetection or IP address(es) whitelist.
  * @param  string  error log directory
  * @param  string  administrator email; enables email sending in production mode
  * @return void
  */
 public static function enable($mode = NULL, $logDirectory = NULL, $email = NULL)
 {
     self::$time = isset($_SERVER['REQUEST_TIME_FLOAT']) ? $_SERVER['REQUEST_TIME_FLOAT'] : microtime(TRUE);
     error_reporting(E_ALL);
     if ($mode !== NULL || self::$productionMode === NULL) {
         self::$productionMode = is_bool($mode) ? $mode : !self::detectDebugMode($mode);
     }
     // logging configuration
     if ($email !== NULL) {
         self::$email = $email;
     }
     if ($logDirectory !== NULL) {
         self::$logDirectory = $logDirectory;
     }
     if (self::$logDirectory) {
         if (!is_dir(self::$logDirectory) || !preg_match('#([a-z]:)?[/\\\\]#Ai', self::$logDirectory)) {
             self::$logDirectory = NULL;
             self::exceptionHandler(new \RuntimeException('Logging directory not found or is not absolute path.'));
         }
     }
     // 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')) {
         self::exceptionHandler(new \RuntimeException("Unable to set 'display_errors' because function ini_set() is disabled."));
     }
     if (!self::$enabled) {
         register_shutdown_function([__CLASS__, 'shutdownHandler']);
         set_exception_handler([__CLASS__, 'exceptionHandler']);
         set_error_handler([__CLASS__, 'errorHandler']);
         array_map('class_exists', [Bar::class, BlueScreen::class, DefaultBarPanel::class, Dumper::class, FireLogger::class, Helpers::class, Logger::class]);
         self::$enabled = TRUE;
     }
 }
Beispiel #2
0
 /**
  * Enables displaying or logging errors and exceptions.
  * @param  mixed   production, development mode, autodetection or IP address(es) whitelist.
  * @param  string  error log directory
  * @param  string  administrator email; enables email sending in production mode
  * @return void
  */
 public static function enable($mode = NULL, $logDirectory = NULL, $email = NULL)
 {
     if ($mode !== NULL || self::$productionMode === NULL) {
         self::$productionMode = is_bool($mode) ? $mode : !self::detectDebugMode($mode);
     }
     self::$reserved = str_repeat('t', 300000.0);
     self::$time = isset($_SERVER['REQUEST_TIME_FLOAT']) ? $_SERVER['REQUEST_TIME_FLOAT'] : microtime(TRUE);
     self::$obLevel = ob_get_level();
     self::$cpuUsage = !self::$productionMode && function_exists('getrusage') ? getrusage() : NULL;
     // logging configuration
     if ($email !== NULL) {
         self::$email = $email;
     }
     if ($logDirectory !== NULL) {
         self::$logDirectory = $logDirectory;
     }
     if (self::$logDirectory) {
         if (!is_dir(self::$logDirectory) || !preg_match('#([a-z]+:)?[/\\\\]#Ai', self::$logDirectory)) {
             self::$logDirectory = NULL;
             self::exceptionHandler(new \RuntimeException('Logging directory not found or is not absolute path.'));
         }
     }
     // 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')) {
         self::exceptionHandler(new \RuntimeException("Unable to set 'display_errors' because function ini_set() is disabled."));
     }
     error_reporting(E_ALL | E_STRICT);
     if (!self::$enabled) {
         register_shutdown_function(array(__CLASS__, 'shutdownHandler'));
         set_exception_handler(array(__CLASS__, 'exceptionHandler'));
         set_error_handler(array(__CLASS__, 'errorHandler'));
         array_map('class_exists', array('Tracy\\Bar', 'Tracy\\BlueScreen', 'Tracy\\DefaultBarPanel', 'Tracy\\Dumper', 'Tracy\\FireLogger', 'Tracy\\Helpers', 'Tracy\\Logger'));
         self::$enabled = TRUE;
     }
 }
Beispiel #3
0
 /**
  * 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)
 {
     self::$time = isset($_SERVER['REQUEST_TIME_FLOAT']) ? $_SERVER['REQUEST_TIME_FLOAT'] : microtime(TRUE);
     if (isset($_SERVER['REQUEST_URI'])) {
         self::$source = (!empty($_SERVER['HTTPS']) && strcasecmp($_SERVER['HTTPS'], 'off') ? 'https://' : 'http://') . (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '') . $_SERVER['REQUEST_URI'];
     } else {
         self::$source = empty($_SERVER['argv']) ? 'CLI' : 'CLI: ' . implode(' ', $_SERVER['argv']);
     }
     error_reporting(E_ALL | E_STRICT);
     // production/development mode detection
     if (is_bool($mode)) {
         self::$productionMode = $mode;
     } elseif ($mode !== self::DETECT || self::$productionMode === NULL) {
         // IP addresses or computer names whitelist detection
         $list = is_string($mode) ? preg_split('#[,\\s]+#', $mode) : (array) $mode;
         if (!isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
             $list[] = '127.0.0.1';
             $list[] = '::1';
         }
         self::$productionMode = !in_array(isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : php_uname('n'), $list, TRUE);
     }
     // logging configuration
     if ($email !== NULL) {
         self::$email = $email;
     }
     if (is_string($logDirectory)) {
         self::$logDirectory = realpath($logDirectory);
         if (self::$logDirectory === FALSE) {
             self::_exceptionHandler(new \RuntimeException("Log directory is not found or is not directory."));
         }
     } elseif ($logDirectory === FALSE) {
         self::$logDirectory = NULL;
     }
     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')) {
         self::_exceptionHandler(new \RuntimeException("Unable to set 'display_errors' because function ini_set() is disabled."));
     }
     if (!self::$enabled) {
         register_shutdown_function(array(__CLASS__, '_shutdownHandler'));
         set_exception_handler(array(__CLASS__, '_exceptionHandler'));
         set_error_handler(array(__CLASS__, '_errorHandler'));
         foreach (array('Tracy\\Bar', 'Tracy\\BlueScreen', 'Tracy\\DefaultBarPanel', 'Tracy\\Dumper', 'Tracy\\FireLogger', 'Tracy\\Helpers', 'Tracy\\Logger') as $class) {
             class_exists($class);
         }
         self::$enabled = TRUE;
     }
 }
Beispiel #4
0
 /**
  * Handler to catch uncaught exception.
  * @param  \Exception
  * @return void
  * @internal
  */
 public static function _exceptionHandler(\Exception $exception, $exit = TRUE)
 {
     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);
         if (self::isHtmlMode()) {
             header('Content-Type: text/html; charset=UTF-8');
         }
     }
     $logMsg = 'Unable to log error. Check if directory is writable and path is absolute.';
     if (self::$productionMode) {
         try {
             self::log($exception, self::EXCEPTION);
         } catch (\Exception $e) {
         }
         $error = isset($e) ? $logMsg : NULL;
         if (self::isHtmlMode()) {
             require __DIR__ . '/templates/error.phtml';
         } else {
             echo "ERROR: application encountered an error and can not continue.\n{$error}\n";
         }
     } elseif (!connection_aborted() && self::isHtmlMode()) {
         self::getBlueScreen()->render($exception);
         self::getBar()->render();
     } elseif (connection_aborted() || !self::fireLog($exception)) {
         try {
             $file = self::log($exception, self::EXCEPTION);
             if ($file && !headers_sent()) {
                 header("X-Tracy-Error-Log: {$file}");
             }
             echo "{$exception}\n" . ($file ? "(stored in {$file})\n" : '');
             if ($file && self::$browser) {
                 exec(self::$browser . ' ' . escapeshellarg($file));
             }
         } catch (\Exception $e) {
             echo "{$exception}\n{$logMsg} {$e->getMessage()}\n";
         }
     }
     try {
         foreach (self::$onFatalError as $handler) {
             call_user_func($handler, $exception);
         }
     } catch (\Exception $e) {
         try {
             self::log($e, self::EXCEPTION);
         } catch (\Exception $e) {
         }
     }
     if ($exit) {
         exit(254);
     }
 }
Beispiel #5
0
 /**
  * Enables displaying or logging errors and exceptions.
  * @param  mixed   production, development mode, autodetection or IP address(es) whitelist.
  * @param  string  error log directory
  * @param  string  administrator email; enables email sending in production mode
  * @return void
  */
 public static function enable($mode = NULL, $logDirectory = NULL, $email = NULL)
 {
     if ($mode !== NULL || self::$productionMode === NULL) {
         self::$productionMode = is_bool($mode) ? $mode : !self::detectDebugMode($mode);
     }
     self::$maxLen =& self::$maxLength;
     self::$reserved = str_repeat('t', 300000.0);
     self::$time = isset($_SERVER['REQUEST_TIME_FLOAT']) ? $_SERVER['REQUEST_TIME_FLOAT'] : microtime(TRUE);
     self::$obLevel = ob_get_level();
     self::$cpuUsage = !self::$productionMode && function_exists('getrusage') ? getrusage() : NULL;
     // logging configuration
     if ($email !== NULL) {
         self::$email = $email;
     }
     if ($logDirectory !== NULL) {
         self::$logDirectory = $logDirectory;
     }
     if (self::$logDirectory) {
         if (!is_dir(self::$logDirectory) || !preg_match('#([a-z]+:)?[/\\\\]#Ai', self::$logDirectory)) {
             self::$logDirectory = NULL;
             self::exceptionHandler(new \RuntimeException('Logging directory not found or is not absolute path.'));
         }
     }
     // 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')) {
         self::exceptionHandler(new \RuntimeException("Unable to set 'display_errors' because function ini_set() is disabled."));
     }
     error_reporting(E_ALL);
     if (self::$enabled) {
         return;
     }
     self::$enabled = TRUE;
     register_shutdown_function([__CLASS__, 'shutdownHandler']);
     set_exception_handler([__CLASS__, 'exceptionHandler']);
     set_error_handler([__CLASS__, 'errorHandler']);
     array_map('class_exists', ['Tracy\\Bar', 'Tracy\\BlueScreen', 'Tracy\\DefaultBarPanel', 'Tracy\\Dumper', 'Tracy\\FireLogger', 'Tracy\\Helpers', 'Tracy\\Logger']);
     if (self::$productionMode) {
     } elseif (headers_sent($file, $line) || ob_get_length()) {
         throw new \LogicException(__METHOD__ . '() called after some output has been sent. ' . ($file ? "Output started at {$file}:{$line}." : 'Try Tracy\\OutputDebugger to find where output started.'));
     } elseif (self::getBar()->dispatchAssets()) {
         exit;
     } elseif (session_status() === PHP_SESSION_ACTIVE) {
         self::dispatch();
     }
 }
Beispiel #6
0
 /**
  * Handler to catch uncaught exception.
  * @param  \Exception
  * @return void
  * @internal
  */
 public static function _exceptionHandler(\Exception $exception, $exit = TRUE)
 {
     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::EXCEPTION);
             } 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-Tracy-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 ($exit) {
         exit(254);
     }
 }