static function enable($mode = NULL, $logDirectory = NULL, $email = NULL) { error_reporting(E_ALL | E_STRICT); if (is_bool($mode)) { self::$productionMode = $mode; } elseif (is_string($mode)) { $mode = preg_split('#[,\\s]+#', "{$mode} 127.0.0.1 ::1"); } if (is_array($mode)) { self::$productionMode = !isset($_SERVER['REMOTE_ADDR']) || !in_array($_SERVER['REMOTE_ADDR'], $mode, TRUE); } if (self::$productionMode === self::DETECT) { if (class_exists('NEnvironment')) { self::$productionMode = NEnvironment::isProduction(); } elseif (isset($_SERVER['SERVER_ADDR']) || isset($_SERVER['LOCAL_ADDR'])) { $addrs = array(); if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { $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; } } if (is_string($logDirectory) || $logDirectory === FALSE) { self::$logDirectory = $logDirectory; } else { self::$logDirectory = defined('APP_DIR') ? APP_DIR . '/../log' : getcwd() . '/log'; } if (self::$logDirectory) { ini_set('error_log', self::$logDirectory . '/php_error.log'); } if (function_exists('ini_set')) { ini_set('display_errors', !self::$productionMode); 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')) { throw new NotSupportedException('Function ini_set() must be enabled.'); } if ($email) { if (!is_string($email)) { throw new 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; } }
public static function enable($mode = NULL, $logFile = NULL, $email = NULL) { error_reporting(E_ALL | E_STRICT); if (is_bool($mode)) { self::$productionMode = $mode; } elseif (is_string($mode)) { $mode = preg_split('#[,\\s]+#', $mode); } if (is_array($mode)) { self::$productionMode = !isset($_SERVER['REMOTE_ADDR']) || !in_array($_SERVER['REMOTE_ADDR'], $mode, TRUE); } if (self::$productionMode === self::DETECT) { if (class_exists('Environment')) { self::$productionMode = NEnvironment::isProduction(); } elseif (isset($_SERVER['SERVER_ADDR']) || isset($_SERVER['LOCAL_ADDR'])) { $addr = isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : $_SERVER['LOCAL_ADDR']; $oct = explode('.', $addr); self::$productionMode = $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')); } else { self::$productionMode = !self::$consoleMode; } } if (self::$productionMode && $logFile !== FALSE) { self::$logFile = 'log/php_error.log'; if (class_exists('Environment')) { if (is_string($logFile)) { self::$logFile = NEnvironment::expand($logFile); } else { try { self::$logFile = NEnvironment::expand('%logDir%/php_error.log'); } catch (InvalidStateException $e) { } } } elseif (is_string($logFile)) { self::$logFile = $logFile; } ini_set('error_log', self::$logFile); } if (function_exists('ini_set')) { ini_set('display_errors', !self::$productionMode); 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')) { throw new NotSupportedException('Function ini_set() must be enabled.'); } self::$sendEmails = self::$logFile && $email; if (self::$sendEmails) { if (is_string($email)) { self::$emailHeaders['To'] = $email; } elseif (is_array($email)) { self::$emailHeaders = $email + self::$emailHeaders; } } if (!defined('E_DEPRECATED')) { define('E_DEPRECATED', 8192); } if (!defined('E_USER_DEPRECATED')) { define('E_USER_DEPRECATED', 16384); } register_shutdown_function(array(__CLASS__, '_shutdownHandler')); set_exception_handler(array(__CLASS__, '_exceptionHandler')); set_error_handler(array(__CLASS__, '_errorHandler')); self::$enabled = TRUE; }