Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
| 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Greg Aker | 741de1c | 2010-11-10 14:52:57 -0600 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.1.6 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
| 7 | * @package CodeIgniter |
| 8 | * @author ExpressionEngine Dev Team |
Derek Jones | 7f3719f | 2010-01-05 13:35:37 +0000 | [diff] [blame] | 9 | * @copyright Copyright (c) 2008 - 2010, EllisLab, Inc. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 10 | * @license http://codeigniter.com/user_guide/license.html |
| 11 | * @link http://codeigniter.com |
| 12 | * @since Version 1.0 |
| 13 | * @filesource |
| 14 | */ |
| 15 | |
| 16 | // ------------------------------------------------------------------------ |
| 17 | |
| 18 | /** |
| 19 | * Exceptions Class |
| 20 | * |
| 21 | * @package CodeIgniter |
| 22 | * @subpackage Libraries |
| 23 | * @category Exceptions |
| 24 | * @author ExpressionEngine Dev Team |
| 25 | * @link http://codeigniter.com/user_guide/libraries/exceptions.html |
| 26 | */ |
| 27 | class CI_Exceptions { |
| 28 | var $action; |
| 29 | var $severity; |
| 30 | var $message; |
| 31 | var $filename; |
| 32 | var $line; |
| 33 | var $ob_level; |
| 34 | |
| 35 | var $levels = array( |
| 36 | E_ERROR => 'Error', |
| 37 | E_WARNING => 'Warning', |
| 38 | E_PARSE => 'Parsing Error', |
| 39 | E_NOTICE => 'Notice', |
| 40 | E_CORE_ERROR => 'Core Error', |
| 41 | E_CORE_WARNING => 'Core Warning', |
| 42 | E_COMPILE_ERROR => 'Compile Error', |
| 43 | E_COMPILE_WARNING => 'Compile Warning', |
| 44 | E_USER_ERROR => 'User Error', |
| 45 | E_USER_WARNING => 'User Warning', |
| 46 | E_USER_NOTICE => 'User Notice', |
| 47 | E_STRICT => 'Runtime Notice' |
| 48 | ); |
| 49 | |
| 50 | |
| 51 | /** |
| 52 | * Constructor |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 53 | */ |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame^] | 54 | public function __construct() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 55 | { |
| 56 | $this->ob_level = ob_get_level(); |
| 57 | // Note: Do not log messages from this constructor. |
| 58 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 59 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 60 | // -------------------------------------------------------------------- |
| 61 | |
| 62 | /** |
| 63 | * Exception Logger |
| 64 | * |
| 65 | * This function logs PHP generated error messages |
| 66 | * |
| 67 | * @access private |
| 68 | * @param string the error severity |
| 69 | * @param string the error string |
| 70 | * @param string the error filepath |
| 71 | * @param string the error line number |
| 72 | * @return string |
| 73 | */ |
| 74 | function log_exception($severity, $message, $filepath, $line) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 75 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 76 | $severity = ( ! isset($this->levels[$severity])) ? $severity : $this->levels[$severity]; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 77 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 78 | log_message('error', 'Severity: '.$severity.' --> '.$message. ' '.$filepath.' '.$line, TRUE); |
| 79 | } |
| 80 | |
| 81 | // -------------------------------------------------------------------- |
| 82 | |
| 83 | /** |
| 84 | * 404 Page Not Found Handler |
| 85 | * |
| 86 | * @access private |
| 87 | * @param string |
| 88 | * @return string |
| 89 | */ |
Derek Allard | 2ddc949 | 2010-08-05 10:08:33 -0400 | [diff] [blame] | 90 | function show_404($page = '', $log_error = TRUE) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 91 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 92 | $heading = "404 Page Not Found"; |
| 93 | $message = "The page you requested was not found."; |
| 94 | |
Derek Allard | 2ddc949 | 2010-08-05 10:08:33 -0400 | [diff] [blame] | 95 | // By default we log this, but allow a dev to skip it |
| 96 | if ($log_error) |
| 97 | { |
| 98 | log_message('error', '404 Page Not Found --> '.$page); |
| 99 | } |
| 100 | |
Derek Jones | f6b442b | 2009-07-11 18:23:30 +0000 | [diff] [blame] | 101 | echo $this->show_error($heading, $message, 'error_404', 404); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 102 | exit; |
| 103 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 104 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 105 | // -------------------------------------------------------------------- |
| 106 | |
| 107 | /** |
| 108 | * General Error Page |
| 109 | * |
| 110 | * This function takes an error message as input |
| 111 | * (either as a string or an array) and displays |
| 112 | * it using the specified template. |
| 113 | * |
| 114 | * @access private |
| 115 | * @param string the heading |
| 116 | * @param string the message |
| 117 | * @param string the template name |
| 118 | * @return string |
| 119 | */ |
Derek Jones | 817163a | 2009-07-11 17:05:58 +0000 | [diff] [blame] | 120 | function show_error($heading, $message, $template = 'error_general', $status_code = 500) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 121 | { |
Derek Jones | 817163a | 2009-07-11 17:05:58 +0000 | [diff] [blame] | 122 | set_status_header($status_code); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 123 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 124 | $message = '<p>'.implode('</p><p>', ( ! is_array($message)) ? array($message) : $message).'</p>'; |
| 125 | |
| 126 | if (ob_get_level() > $this->ob_level + 1) |
| 127 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 128 | ob_end_flush(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 129 | } |
| 130 | ob_start(); |
| 131 | include(APPPATH.'errors/'.$template.EXT); |
| 132 | $buffer = ob_get_contents(); |
| 133 | ob_end_clean(); |
| 134 | return $buffer; |
| 135 | } |
| 136 | |
| 137 | // -------------------------------------------------------------------- |
| 138 | |
| 139 | /** |
| 140 | * Native PHP error handler |
| 141 | * |
| 142 | * @access private |
| 143 | * @param string the error severity |
| 144 | * @param string the error string |
| 145 | * @param string the error filepath |
| 146 | * @param string the error line number |
| 147 | * @return string |
| 148 | */ |
| 149 | function show_php_error($severity, $message, $filepath, $line) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 150 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 151 | $severity = ( ! isset($this->levels[$severity])) ? $severity : $this->levels[$severity]; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 152 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 153 | $filepath = str_replace("\\", "/", $filepath); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 154 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 155 | // For safety reasons we do not show the full file path |
| 156 | if (FALSE !== strpos($filepath, '/')) |
| 157 | { |
| 158 | $x = explode('/', $filepath); |
| 159 | $filepath = $x[count($x)-2].'/'.end($x); |
| 160 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 161 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 162 | if (ob_get_level() > $this->ob_level + 1) |
| 163 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 164 | ob_end_flush(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 165 | } |
| 166 | ob_start(); |
| 167 | include(APPPATH.'errors/error_php'.EXT); |
| 168 | $buffer = ob_get_contents(); |
| 169 | ob_end_clean(); |
| 170 | echo $buffer; |
| 171 | } |
| 172 | |
| 173 | |
| 174 | } |
| 175 | // END Exceptions Class |
| 176 | |
| 177 | /* End of file Exceptions.php */ |
Derek Jones | c68dfbf | 2010-03-02 12:59:23 -0600 | [diff] [blame] | 178 | /* Location: ./system/core/Exceptions.php */ |