/** * Write an array of exceptions to a log file with * an attachment file (filename in log entry). */ public function write(array $messages) { // Abort if no messages if (empty($messages)) { return; } // Set the monthly directory name $directory = $this->_directory . date('Y/m/d') . DIRECTORY_SEPARATOR; if (!is_dir($directory)) { // Create the monthly directory mkdir($directory, 0777, TRUE); } foreach ($messages as $message) { if (is_object($message['body']) && $message['body'] instanceof Exception) { // Make a filename $filename = $directory . $message['time'] . '.htm'; // Get the trace ob_start(); Kohana::exception_handler($message['body']); $trace = ob_get_clean(); // Write the trace file_put_contents($filename, $trace); } } }
/** * Magic method, returns the output of render(). If any exceptions are * thrown, the exception output will be returned instead. * * @return string */ public function __toString() { try { return $this->render(); } catch (Exception $e) { // Display the exception message Kohana::exception_handler($e); return ''; } }
public static function handle(Exception $e) { switch (get_class($e)) { case 'Error_404': case 'ReflectionException': echo Request::factory('errors/404')->execute(); break; default: return Kohana::exception_handler($e); break; } }
/** * Handles exceptions * * @param Exception $e the exception to handle * * @return bool */ public static function handle(Exception $e) { switch (get_class($e)) { case 'Vendo_404': case 'ReflectionException': // This is bad Request::instance()->status = 404; $view = new View_Error_404(); $view->message = $e->getMessage(); $view->title = 'File Not Found'; echo $view; return TRUE; break; default: return Kohana::exception_handler($e); break; } }
public function doIncludeViews($text) { if (preg_match_all('/{{(\\S+?)}}/m', $text, $matches, PREG_SET_ORDER)) { $replace = array(); foreach ($matches as $set) { list($search, $view) = $set; try { $replace[$search] = View::factory($view)->render(); } catch (Exception $e) { ob_start(); // Capture the exception handler output and insert it instead Kohana::exception_handler($e); $replace[$search] = ob_get_clean(); } } $text = strtr($text, $replace); } return $text; }
/** * Catches errors that are not caught by the error handler, such as E_PARSE. * * @uses Kohana::exception_handler() * @return void */ public static function shutdown_handler() { try { if (self::$caching === TRUE and self::$_files_changed === TRUE) { // Write the file path cache Kohana::cache('Kohana::find_file()', self::$_files); } } catch (Exception $e) { // Pass the exception to the handler Kohana::exception_handler($e); } if ($error = error_get_last()) { // If an output buffer exists, clear it ob_get_level() and ob_clean(); // Fake an exception for nice debugging Kohana::exception_handler(new ErrorException($error['message'], $error['type'], 0, $error['file'], $error['line'])); } }
/** * Catches errors that are not caught by the error handler, such as E_PARSE. * * @uses Kohana::exception_handler * @return void */ public static function shutdown_handler() { if (!Kohana::$_init) { // Do not execute when not active return; } try { if (Kohana::$caching === TRUE and Kohana::$_files_changed === TRUE) { // Write the file path cache Kohana::cache('Kohana::find_file()', Kohana::$_files); } } catch (Exception $e) { // Pass the exception to the handler Kohana::exception_handler($e); } if (Kohana::$errors and $error = error_get_last() and in_array($error['type'], Kohana::$shutdown_errors)) { // Clean the output buffer ob_get_level() and ob_clean(); // Fake an exception for nice debugging Kohana::exception_handler(new ErrorException($error['message'], $error['type'], 0, $error['file'], $error['line'])); // Shutdown now to avoid a "death loop" exit(1); } }
/** * Tests Kohana::exception_handler() * * @test * @dataProvider provider_exception_handler * @covers Kohana::exception_handler * @param boolean $exception_type Exception type to throw * @param boolean $message Message to pass to exception * @param boolean $is_cli Use cli mode? * @param boolean $expected Output for Kohana::exception_handler * @param string $expexcted_message What to look for in the output string */ public function teste_exception_handler($exception_type, $message, $is_cli, $expected, $expected_message) { try { Kohana::$is_cli = $is_cli; throw new $exception_type($message); } catch (Exception $e) { ob_start(); $this->assertEquals($expected, Kohana::exception_handler($e)); $view = ob_get_contents(); ob_clean(); $this->assertContains($expected_message, $view); } Kohana::$is_cli = TRUE; }
/** * Magic method, returns the output of render(). If any exceptions are * thrown, the exception output will be returned instead. * @return string */ public function __toString() { try { return $this->render(); } catch (Exception $e) { // Display the exception message if (substr(Kohana::VERSION, 0, 3) == '3.0') { Kohana::exception_handler($e); } else { Kohana_Exception::handler($e); } return ''; } }
/** * Renders the HTML code using specified view. * If no view is specified, loads the default one specified in module configuration. * * @uses View::factory() * @param string $view View location * @return View|NULL Rendered View, that includes title, metas, favicons, stylesheets and javascripts */ public function render($view = NULL) { try { $view = View::factory(empty($view) ? $this->config->get('view') : $view); $view->set('title', $this->title)->set('meta', $this->meta)->set('favicons', $this->favicons)->set('stylesheets', $this->stylesheets)->set('javascripts', $this->javascripts); return $view; } catch (Exception $e) { // Display the exception message Kohana::exception_handler($e); return NULL; } }
/** * 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')) { // Are we using encryption for caches? self::$internal_cache_encrypt = self::config('core.internal_cache_encrypt'); if (self::$internal_cache_encrypt === TRUE) { self::$internal_cache_key = self::config('core.internal_cache_key'); // Be sure the key is of acceptable length for the mcrypt algorithm used self::$internal_cache_key = substr(self::$internal_cache_key, 0, 24); } // Set the directory to be used for the internal cache if (!(self::$internal_cache_path = self::config('core.internal_cache_path'))) { self::$internal_cache_path = APPPATH . '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 = !empty($_SERVER['HTTP_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) ? date_default_timezone_get() : $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 fatal error handler register_shutdown_function(function () { if ($error = error_get_last()) { ob_clean(); Kohana::exception_handler($error['type'], $error['message'], $error['file'], $error['line'], NULL); } }); // 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'); }