Andrey Andreev | 7ac33d7 | 2012-01-07 19:39:39 +0200 | [diff] [blame^] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Andrey Andreev | 7ac33d7 | 2012-01-07 19:39:39 +0200 | [diff] [blame^] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | 7ac33d7 | 2012-01-07 19:39:39 +0200 | [diff] [blame^] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 11 | * This source file is subject to the Open Software License (OSL 3.0) that is |
| 12 | * bundled with this package in the files license.txt / license.rst. It is |
| 13 | * also available through the world wide web at this URL: |
| 14 | * http://opensource.org/licenses/OSL-3.0 |
| 15 | * If you did not receive a copy of the license and are unable to obtain it |
| 16 | * through the world wide web, please send an email to |
| 17 | * licensing@ellislab.com so we can send you a copy immediately. |
| 18 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
| 27 | |
| 28 | // ------------------------------------------------------------------------ |
| 29 | |
| 30 | /** |
| 31 | * Exceptions Class |
| 32 | * |
| 33 | * @package CodeIgniter |
| 34 | * @subpackage Libraries |
| 35 | * @category Exceptions |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 36 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 37 | * @link http://codeigniter.com/user_guide/libraries/exceptions.html |
| 38 | */ |
| 39 | class CI_Exceptions { |
Andrey Andreev | 7ac33d7 | 2012-01-07 19:39:39 +0200 | [diff] [blame^] | 40 | |
| 41 | public $action; |
| 42 | public $severity; |
| 43 | public $message; |
| 44 | public $filename; |
| 45 | public $line; |
David Behler | 209b2cf | 2011-08-14 23:00:43 +0200 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * Nesting level of the output buffering mechanism |
David Behler | 209b2cf | 2011-08-14 23:00:43 +0200 | [diff] [blame] | 49 | * |
| 50 | * @var int |
David Behler | 209b2cf | 2011-08-14 23:00:43 +0200 | [diff] [blame] | 51 | */ |
Andrey Andreev | 7ac33d7 | 2012-01-07 19:39:39 +0200 | [diff] [blame^] | 52 | public $ob_level; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 53 | |
David Behler | 209b2cf | 2011-08-14 23:00:43 +0200 | [diff] [blame] | 54 | /** |
| 55 | * List if available error levels |
| 56 | * |
| 57 | * @var array |
David Behler | 209b2cf | 2011-08-14 23:00:43 +0200 | [diff] [blame] | 58 | */ |
Andrey Andreev | 7ac33d7 | 2012-01-07 19:39:39 +0200 | [diff] [blame^] | 59 | public $levels = array( |
| 60 | E_ERROR => 'Error', |
| 61 | E_WARNING => 'Warning', |
| 62 | E_PARSE => 'Parsing Error', |
| 63 | E_NOTICE => 'Notice', |
| 64 | E_CORE_ERROR => 'Core Error', |
| 65 | E_CORE_WARNING => 'Core Warning', |
| 66 | E_COMPILE_ERROR => 'Compile Error', |
| 67 | E_COMPILE_WARNING => 'Compile Warning', |
| 68 | E_USER_ERROR => 'User Error', |
| 69 | E_USER_WARNING => 'User Warning', |
| 70 | E_USER_NOTICE => 'User Notice', |
| 71 | E_STRICT => 'Runtime Notice' |
| 72 | ); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 73 | |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 74 | public function __construct() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 75 | { |
| 76 | $this->ob_level = ob_get_level(); |
Andrey Andreev | 7ac33d7 | 2012-01-07 19:39:39 +0200 | [diff] [blame^] | 77 | // Note: Do not log messages from this constructor. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 78 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 79 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 80 | // -------------------------------------------------------------------- |
| 81 | |
| 82 | /** |
| 83 | * Exception Logger |
| 84 | * |
| 85 | * This function logs PHP generated error messages |
| 86 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 87 | * @param string the error severity |
| 88 | * @param string the error string |
| 89 | * @param string the error filepath |
| 90 | * @param string the error line number |
Andrey Andreev | 7ac33d7 | 2012-01-07 19:39:39 +0200 | [diff] [blame^] | 91 | * @return void |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 92 | */ |
Andrey Andreev | 7ac33d7 | 2012-01-07 19:39:39 +0200 | [diff] [blame^] | 93 | public function log_exception($severity, $message, $filepath, $line) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 94 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 95 | $severity = ( ! isset($this->levels[$severity])) ? $severity : $this->levels[$severity]; |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 96 | log_message('error', 'Severity: '.$severity.' --> '.$message. ' '.$filepath.' '.$line, TRUE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | // -------------------------------------------------------------------- |
| 100 | |
| 101 | /** |
| 102 | * 404 Page Not Found Handler |
| 103 | * |
David Behler | cda768a | 2011-08-14 23:52:48 +0200 | [diff] [blame] | 104 | * @param string the page |
| 105 | * @param bool log error yes/no |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 106 | * @return string |
| 107 | */ |
Andrey Andreev | 7ac33d7 | 2012-01-07 19:39:39 +0200 | [diff] [blame^] | 108 | public function show_404($page = '', $log_error = TRUE) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 109 | { |
Andrey Andreev | 7ac33d7 | 2012-01-07 19:39:39 +0200 | [diff] [blame^] | 110 | $heading = '404 Page Not Found'; |
| 111 | $message = 'The page you requested was not found.'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 112 | |
Derek Allard | 2ddc949 | 2010-08-05 10:08:33 -0400 | [diff] [blame] | 113 | // By default we log this, but allow a dev to skip it |
| 114 | if ($log_error) |
| 115 | { |
| 116 | log_message('error', '404 Page Not Found --> '.$page); |
| 117 | } |
| 118 | |
Derek Jones | f6b442b | 2009-07-11 18:23:30 +0000 | [diff] [blame] | 119 | echo $this->show_error($heading, $message, 'error_404', 404); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 120 | exit; |
| 121 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 122 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 123 | // -------------------------------------------------------------------- |
| 124 | |
| 125 | /** |
| 126 | * General Error Page |
| 127 | * |
| 128 | * This function takes an error message as input |
| 129 | * (either as a string or an array) and displays |
| 130 | * it using the specified template. |
| 131 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 132 | * @param string the heading |
| 133 | * @param string the message |
| 134 | * @param string the template name |
David Behler | cda768a | 2011-08-14 23:52:48 +0200 | [diff] [blame] | 135 | * @param int the status code |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 136 | * @return string |
| 137 | */ |
Andrey Andreev | 7ac33d7 | 2012-01-07 19:39:39 +0200 | [diff] [blame^] | 138 | public function show_error($heading, $message, $template = 'error_general', $status_code = 500) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 139 | { |
Derek Jones | 817163a | 2009-07-11 17:05:58 +0000 | [diff] [blame] | 140 | set_status_header($status_code); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 141 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 142 | $message = '<p>'.implode('</p><p>', ( ! is_array($message)) ? array($message) : $message).'</p>'; |
| 143 | |
| 144 | if (ob_get_level() > $this->ob_level + 1) |
| 145 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 146 | ob_end_flush(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 147 | } |
| 148 | ob_start(); |
Andrey Andreev | 7ac33d7 | 2012-01-07 19:39:39 +0200 | [diff] [blame^] | 149 | include(APPPATH.'errors'.DIRECTORY_SEPARATOR.$template.'.php'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 150 | $buffer = ob_get_contents(); |
| 151 | ob_end_clean(); |
| 152 | return $buffer; |
| 153 | } |
| 154 | |
| 155 | // -------------------------------------------------------------------- |
| 156 | |
| 157 | /** |
| 158 | * Native PHP error handler |
| 159 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 160 | * @param string the error severity |
| 161 | * @param string the error string |
| 162 | * @param string the error filepath |
| 163 | * @param string the error line number |
| 164 | * @return string |
| 165 | */ |
| 166 | function show_php_error($severity, $message, $filepath, $line) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 167 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 168 | $severity = ( ! isset($this->levels[$severity])) ? $severity : $this->levels[$severity]; |
Andrey Andreev | 7ac33d7 | 2012-01-07 19:39:39 +0200 | [diff] [blame^] | 169 | $filepath = str_replace('\\', '/', $filepath); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 170 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 171 | // For safety reasons we do not show the full file path |
| 172 | if (FALSE !== strpos($filepath, '/')) |
| 173 | { |
| 174 | $x = explode('/', $filepath); |
| 175 | $filepath = $x[count($x)-2].'/'.end($x); |
| 176 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 177 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 178 | if (ob_get_level() > $this->ob_level + 1) |
| 179 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 180 | ob_end_flush(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 181 | } |
| 182 | ob_start(); |
Andrey Andreev | 7ac33d7 | 2012-01-07 19:39:39 +0200 | [diff] [blame^] | 183 | include(APPPATH.'errors'.DIRECTORY_SEPARATOR.'error_php.php'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 184 | $buffer = ob_get_contents(); |
| 185 | ob_end_clean(); |
| 186 | echo $buffer; |
| 187 | } |
| 188 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 189 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 190 | |
| 191 | /* End of file Exceptions.php */ |
Andrey Andreev | 7ac33d7 | 2012-01-07 19:39:39 +0200 | [diff] [blame^] | 192 | /* Location: ./system/core/Exceptions.php */ |