Example #1
0
 /**
  * Initializes the environment:
  *
  * - Disables register_globals and magic_quotes_gpc
  * - Determines the current environment
  * - Set global settings
  * - Sanitizes GET, POST, and COOKIE variables
  * - Converts GET, POST, and COOKIE variables to the global character set
  *
  * Any of the global settings can be set here:
  *
  * - **boolean "errors"**     use internal error and exception handling?
  * - **boolean "caching"**    cache the location of files between requests?
  * - **string  "charset"**    character set used for all input and output
  * - **string  "base_url"**   set the base URL for the application
  * - **string  "index_file"** set the index.php file name
  *
  * @throws  Exception
  * @param   array   global settings
  * @return  void
  */
 public static function init(array $settings = NULL)
 {
     static $inited;
     // This function can only be run once
     if ($inited === TRUE) {
         return;
     }
     // The system is now ready
     $inited = TRUE;
     // Start an output buffer
     ob_start();
     isset($settings['errors']) && (Ko::$errors = (bool) $settings['errors']);
     if (self::$errors === TRUE) {
         // Enable the Ko shutdown handler, which catches E_FATAL errors.
         register_shutdown_function(array('Ko', 'shutdown_handler'));
         // Enable Ko exception handling, adds stack traces and error source.
         set_exception_handler(array('Ko', 'exception_handler'));
         // Enable Ko error handling, converts all PHP errors to exceptions.
         set_error_handler(array('Ko', 'error_handler'));
     }
     if (ini_get('register_globals')) {
         if (isset($_REQUEST['GLOBALS']) or isset($_FILES['GLOBALS'])) {
             // Prevent malicious GLOBALS overload attack
             echo "Global variable overload attack detected! Request aborted.\n";
             // Exit with an error status
             exit(1);
         }
         // Get the variable names of all globals
         $global_variables = array_keys($GLOBALS);
         // Remove the standard global variables from the list
         $global_variables = array_diff($global_variables, array('GLOBALS', '_REQUEST', '_GET', '_POST', '_FILES', '_COOKIE', '_SERVER', '_ENV', '_SESSION'));
         foreach ($global_variables as $name) {
             // Retrieve the global variable and make it null
             global ${$name};
             ${$name} = NULL;
             // Unset the global variable, effectively disabling register_globals
             unset($GLOBALS[$name], ${$name});
         }
     }
     // Determine if we are running in a command line environment
     self::$is_cli = PHP_SAPI === 'cli';
     // Determine if we are running in a Windows environment
     self::$is_windows = DIRECTORY_SEPARATOR === '\\';
     isset($settings['caching']) && (self::$caching = (bool) $settings['caching']);
     if (self::$caching === TRUE) {
         // Use the default cache directory
         self::$cache_dir = DATA_PATH . 'cache';
         self::$_files = self::cache('Ko::findFile()');
     }
     // Setup page charset
     isset($settings['charset']) && (self::$charset = strtolower($settings['charset']));
     // Setup page base_url
     isset($settings['base_url']) && (self::$base_url = rtrim($settings['base_url'], '/') . '/');
     // Setup page index_file
     isset($settings['index_file']) && (self::$index_file = trim($settings['index_file'], '/'));
     // Determine if the extremely evil magic quotes are enabled
     self::$magic_quotes = (bool) get_magic_quotes_gpc();
     // Sanitize all request variables
     $_GET = self::sanitize($_GET);
     $_POST = self::sanitize($_POST);
     $_COOKIE = self::sanitize($_COOKIE);
 }