/** * 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; // Only run this function once if ($run === TRUE) { return; } $run = TRUE; // Start the environment setup benchmark Benchmark::start(SYSTEM_BENCHMARK . '_environment_setup'); // Define Kohana error constant define('E_KOHANA', 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(Kohana::CHARSET); if (Kohana_Config::instance()->loaded() === FALSE) { // Re-parse the include paths Kohana::include_paths(TRUE); } if (Kohana::$cache_lifetime = Kohana::config('core.internal_cache')) { // Are we using encryption for caches? Kohana::$internal_cache_encrypt = Kohana::config('core.internal_cache_encrypt'); if (Kohana::$internal_cache_encrypt === TRUE) { Kohana::$internal_cache_key = Kohana::config('core.internal_cache_key'); // Be sure the key is of acceptable length for the mcrypt algorithm used Kohana::$internal_cache_key = substr(Kohana::$internal_cache_key, 0, 24); } // Set the directory to be used for the internal cache if (!(Kohana::$internal_cache_path = Kohana::config('core.internal_cache_path'))) { Kohana::$internal_cache_path = APPPATH . 'cache/'; } // Load cached configuration and language files Kohana::$internal_cache['configuration'] = Kohana::cache('configuration', Kohana::$cache_lifetime); Kohana::$internal_cache['language'] = Kohana::cache('language', Kohana::$cache_lifetime); // Load cached file paths Kohana::$internal_cache['find_file_paths'] = Kohana::cache('find_file_paths', Kohana::$cache_lifetime); // Enable cache saving Event::add('system.shutdown', array('Kohana', 'internal_cache_save')); } // Start output buffering ob_start(array('Kohana', 'output_buffer')); // Save buffering level Kohana::$buffer_level = ob_get_level(); // Set autoloader spl_autoload_register(array('Kohana', 'auto_load')); // Register a shutdown function to handle system.shutdown events register_shutdown_function(array('Kohana', 'shutdown')); // Send default text/html UTF-8 header header('Content-Type: text/html; charset=' . Kohana::CHARSET); // Load i18n new I18n(); // Enable exception handling Kohana_Exception::enable(); // Enable error handling Kohana_PHP_Exception::enable(); // Load locales $locales = Kohana::config('locale.language'); // Make first locale the defined Kohana charset $locales[0] .= '.' . Kohana::CHARSET; // Set locale information Kohana::$locale = setlocale(LC_ALL, $locales); // Default to the default locale when none of the user defined ones where accepted Kohana::$locale = !Kohana::$locale ? Kohana::LOCALE . '.' . Kohana::CHARSET : Kohana::$locale; // Set locale for the I18n system I18n::set_locale(Kohana::$locale); // Set and validate the timezone date_default_timezone_set(Kohana::config('locale.timezone')); // register_globals is enabled if (ini_get('register_globals')) { if (isset($_REQUEST['GLOBALS'])) { // Prevent GLOBALS override attacks exit('Global variable overload attack.'); } // Destroy the REQUEST global $_REQUEST = array(); // These globals are standard and should not be removed $preserve = array('GLOBALS', '_REQUEST', '_GET', '_POST', '_FILES', '_COOKIE', '_SERVER', '_ENV', '_SESSION'); // This loop has the same effect as disabling register_globals foreach (array_diff(array_keys($GLOBALS), $preserve) as $key) { global ${$key}; ${$key} = NULL; // Unset the global variable unset($GLOBALS[$key], ${$key}); } // Warn the developer about register globals Kohana_Log::add('debug', 'Disable register_globals! It is evil and deprecated: http://php.net/register_globals'); } // Enable Kohana routing Event::add('system.routing', array('Router', 'find_uri')); Event::add('system.routing', array('Router', 'setup')); // Enable Kohana controller initialization Event::add('system.execute', array('Kohana', 'instance')); // Enable Kohana 404 pages Event::add('system.404', array('Kohana_404_Exception', 'trigger')); if (Kohana::config('core.enable_hooks') === TRUE) { // Find all the hook files $hooks = Kohana::list_files('hooks', TRUE); foreach ($hooks as $file) { // Load the hook include $file; } } // Stop the environment setup routine Benchmark::stop(SYSTEM_BENCHMARK . '_environment_setup'); }
/** * 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; // Only run this function once if ($run === TRUE) { return; } $run = TRUE; // Start the environment setup benchmark Benchmark::start(SYSTEM_BENCHMARK . '_environment_setup'); // Define Kohana error constant define('E_KOHANA', 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(Kohana::CHARSET); if (Kohana_Config::instance()->loaded() === FALSE) { // Re-parse the include paths Kohana::include_paths(TRUE); } if (Kohana::$cache_lifetime = Kohana::config('core.internal_cache')) { // Are we using encryption for caches? Kohana::$internal_cache_encrypt = Kohana::config('core.internal_cache_encrypt'); if (Kohana::$internal_cache_encrypt === TRUE) { Kohana::$internal_cache_key = Kohana::config('core.internal_cache_key'); // Be sure the key is of acceptable length for the mcrypt algorithm used Kohana::$internal_cache_key = substr(Kohana::$internal_cache_key, 0, 24); } // Set the directory to be used for the internal cache if (!(Kohana::$internal_cache_path = Kohana::config('core.internal_cache_path'))) { Kohana::$internal_cache_path = APPPATH . 'cache/'; } // Load cached configuration and language files Kohana::$internal_cache['configuration'] = Kohana::cache('configuration', Kohana::$cache_lifetime); Kohana::$internal_cache['language'] = Kohana::cache('language', Kohana::$cache_lifetime); // Load cached file paths Kohana::$internal_cache['find_file_paths'] = Kohana::cache('find_file_paths', Kohana::$cache_lifetime); // Enable cache saving Event::add('system.shutdown', array('Kohana', 'internal_cache_save')); } // Start output buffering ob_start(array('Kohana', 'output_buffer')); // Save buffering level Kohana::$buffer_level = ob_get_level(); // Set autoloader spl_autoload_register(array('Kohana', 'auto_load')); // Register a shutdown function to handle system.shutdown events register_shutdown_function(array('Kohana', 'shutdown')); // Send default text/html UTF-8 header header('Content-Type: text/html; charset=' . Kohana::CHARSET); // Load i18n new I18n(); // Enable exception handling Kohana_Exception::enable(); // Enable error handling Kohana_PHP_Exception::enable(); // Load locales $locales = Kohana::config('locale.language'); // Make first locale the defined Kohana charset $locales[0] .= '.' . Kohana::CHARSET; // Set locale information Kohana::$locale = setlocale(LC_ALL, $locales); // Default to the default locale when none of the user defined ones where accepted Kohana::$locale = !Kohana::$locale ? Kohana::LOCALE . '.' . Kohana::CHARSET : Kohana::$locale; // Set locale for the I18n system I18n::set_locale(Kohana::$locale); // Set and validate the timezone date_default_timezone_set(Kohana::config('locale.timezone')); // Enable Kohana routing Event::add('system.routing', array('Router', 'find_uri')); Event::add('system.routing', array('Router', 'setup')); // Enable Kohana controller initialization Event::add('system.execute', array('Kohana', 'instance')); // Enable Kohana 404 pages Event::add('system.404', array('Kohana_404_Exception', 'trigger')); if (Kohana::config('core.enable_hooks') === TRUE) { // Find all the hook files $hooks = Kohana::list_files('hooks', TRUE); foreach ($hooks as $file) { // Load the hook include $file; } } // Stop the environment setup routine Benchmark::stop(SYSTEM_BENCHMARK . '_environment_setup'); }