/** * 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; // This function can only be run once if ($run === TRUE) { return; } // 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); 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')); } // Disable notices and "strict" errors $ER = error_reporting(~E_NOTICE & ~E_STRICT); // Set the user agent self::$user_agent = trim($_SERVER['HTTP_USER_AGENT']); if (function_exists('date_default_timezone_set')) { $timezone = self::config('locale.timezone'); // Set default timezone, due to increased validation of date settings // which cause massive amounts of E_NOTICEs to be generated in PHP 5.2+ date_default_timezone_set(empty($timezone) ? 'UTC' : $timezone); } // Restore error reporting error_reporting($ER); // Start output buffering ob_start(array(__CLASS__, 'output_buffer')); // Save buffering level self::$buffer_level = ob_get_level(); // Set autoloader spl_autoload_register(array('Kohana', 'auto_load')); // Set error handler set_error_handler(array('Kohana', 'exception_handler')); // Set exception handler set_exception_handler(array('Kohana', 'exception_handler')); // Send default text/html UTF-8 header header('Content-Type: text/html; charset=UTF-8'); // Load locales $locales = self::config('locale.language'); // Make first locale UTF-8 $locales[0] .= '.UTF-8'; // Set locale information self::$locale = setlocale(LC_ALL, $locales); if (self::$configuration['core']['log_threshold'] > 0) { // Set the log directory self::log_directory(self::$configuration['core']['log_directory']); // Enable log writing at shutdown register_shutdown_function(array(__CLASS__, 'log_save')); } // 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', 'show_404')); // Enable Kohana output handling Event::add('system.shutdown', array('Kohana', 'shutdown')); if (self::config('core.enable_hooks') === TRUE) { // Find all the hook files $hooks = self::list_files('hooks', TRUE); foreach ($hooks as $file) { // Load the hook include $file; } } // Setup is complete, prevent it from being run again $run = TRUE; // 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'); }
/** * 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 the environment locale. The first locale must always be a valid * `xx_XX` locale name to be used for i18n: * * Kohana::locale(array('de_DE@euro.UTF-8', 'de_DE.UTF-8', 'german')); * * When using this method, it is a good idea to provide many variations, as * locale availability on different systems is very unpredictable. * * @param array locale choices * @return void */ public static function locale(array $locales) { if (setlocale(LC_ALL, $locales) !== FALSE) { // Set the system locale self::$locale = substr($locales[0], 0, 5); if (($messages = Kohana::cache('kohana_i18n_' . self::$locale)) === NULL) { // Find all this languages translation files $files = self::find_file('i18n', self::$locale); $messages = array(); foreach ($files as $file) { // Load the messages in this file $messages = array_merge($messages, include $file); } // Cache the combined messages Kohana::cache('kohana_i18n_' . self::$locale, $messages); } // Load the language internally self::$language = $messages; } }