Esempio n. 1
0
 /**
  * Sets up the PHP environment. Adds error/exception handling, output
  * buffering, and adds an auto-loading method for loading classes.
  *
  * This method is run immediately when this file is loaded, and is
  * benchmarked as environment_setup.
  *
  * For security, this function also destroys the $_REQUEST global variable.
  * Using the proper global (GET, POST, COOKIE, etc) is inherently more secure.
  * The recommended way to fetch a global variable is using the Input library.
  * @see http://www.php.net/globals
  *
  * @return  void
  */
 public static function setup()
 {
     static $run;
     /**
      * CMD Line Debugging Tool. Allows us to run stuff via cmd line 
      * to show errors that normally would be surpressed in the browser.
      * 
      * 		Example Usage:
      * 			/usr/bin/php index.php "home/index" --show-errors --native-errors
      */
     if (PHP_SAPI == 'cli' && in_array("--show-errors", $_SERVER['argv'])) {
         Eight::$force_show_errors = YES;
         error_reporting(E_ALL ^ E_NOTICE);
         ini_set('display_errors', true);
         Eight::config_set('core.display_errors', YES);
         // Enable native errors?
         if (in_array("--native-errors", $_SERVER['argv'])) {
             Eight::$errors = FALSE;
             // Disables Eight error handling and lets PHP's error handling come through
         }
         // Remove the keys from the arguments
         foreach ($_SERVER['argv'] as $k => $v) {
             if ($v == "--show-errors" or $v == "--native-errors") {
                 unset($_SERVER['argv'][$k]);
             }
         }
     }
     // This function can only be run once
     if ($run === YES) {
         return;
     }
     // Define Eight error constant
     define('E_EIGHT', 42);
     // Define 404 error constant
     define('E_PAGE_NOT_FOUND', 43);
     // Define database error constant
     define('E_DATABASE_ERROR', 44);
     // Set the default charset for mb_* functions
     mb_internal_encoding(Eight::CHARSET);
     if (self::$cache_lifetime = self::config('core.internal_cache')) {
         // Load cached configuration and language files
         self::$internal_cache['configuration'] = self::cache('configuration', self::$cache_lifetime);
         self::$internal_cache['language'] = self::cache('language', self::$cache_lifetime);
         // Load cached file paths
         self::$internal_cache['find_file_paths'] = self::cache('find_file_paths', self::$cache_lifetime);
         // Enable cache saving
         Event::add('system.shutdown', array(__CLASS__, 'internal_cache_save'));
     }
     // Set autoloader
     spl_autoload_register(array('Eight', 'auto_load'));
     // Send default text/html UTF-8 header
     header('Content-Type: text/html; charset=UTF-8');
     // Should we be using Eight errors
     if (Eight::$errors === TRUE) {
         // Enable exception handling
         Eight_Exception::enable();
         // Enable error handling
         Eight_Exception_PHP::enable();
         // Wrap errors in tags so we can better find them
         ini_set('error_prepend_string', '<phperror>');
         ini_set('error_append_string', '</phperror>');
     }
     // Enable logging if the threshold is above 0
     if (Eight::config('log.threshold') > 0) {
         // Set the log directory
         self::log_directory(Eight::config('log.directory'));
         // Enable log writing at shutdown
         register_shutdown_function(array(__CLASS__, 'log_save'));
     }
     // Disable notices and "strict" errors, to prevent some oddities in
     // PHP 5.2 and when using Eight under CLI
     $ER = error_reporting(~E_NOTICE & ~E_STRICT);
     // Set the user agent
     self::$user_agent = trim($_SERVER['HTTP_USER_AGENT']);
     // Try to set the default timezone
     if (!($timezone = Eight::config('locale.timezone'))) {
         // Get the default timezone
         $timezone = date_default_timezone_get();
     }
     // Restore error reporting
     error_reporting($ER);
     // Set the default timezone
     date_default_timezone_set($timezone);
     // Start output buffering
     ob_start(array(__CLASS__, 'output_buffer'));
     // Save buffering level
     self::$buffer_level = ob_get_level();
     // Load locales
     $locales = Eight::config('locale.language');
     // Make first locale UTF-8
     $locales[0] .= '.UTF-8';
     // Set locale information
     self::$locale = setlocale(LC_ALL, $locales);
     // Enable Eight routing
     Event::add('system.routing', array('Router', 'find_uri'));
     Event::add('system.routing', array('Router', 'setup'));
     // Enable Eight controller initialization
     Event::add('system.execute', array('Eight', 'instance'));
     // Enable Eight 404 pages
     Event::add('system.404', array('Eight_Exception_404', 'trigger'));
     // Enable Eight output handling
     Event::add('system.shutdown', array('Eight', 'shutdown'));
     if ($config = Eight::config('core.enable_hooks')) {
         // Start the loading_hooks routine
         Benchmark::start(SYSTEM_BENCHMARK . '_loading_hooks');
         $hooks = array();
         if (!is_array($config)) {
             // All of the hooks are enabled, so we use list_files
             $hooks = Eight::list_files('hooks', YES);
         } else {
             // Individual hooks need to be found
             foreach ($config as $name) {
                 if ($hook = Eight::find_file('hooks', $name, NO)) {
                     // Hook was found, add it to loaded hooks
                     $hooks[] = $hook;
                 } else {
                     // This should never happen
                     Eight::log('error', 'Hook not found: ' . $name);
                 }
             }
         }
         // Length of extension, for offset
         $ext = -strlen(EXT);
         foreach ($hooks as $hook) {
             // Validate the filename extension
             if (substr($hook, $ext) === EXT) {
                 // Hook was found, include it
                 include $hook;
             } else {
                 // This should never happen
                 Eight::log('error', 'Hook not found: ' . $hook);
             }
         }
         // Stop the loading_hooks routine
         Benchmark::stop(SYSTEM_BENCHMARK . '_loading_hooks');
     }
     // Setup is complete, prevent it from being run again
     $run = YES;
 }