Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame] | 5 | * An open source application development framework for PHP |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 7 | * This content is released under the MIT License (MIT) |
Andrey Andreev | 7ac33d7 | 2012-01-07 19:39:39 +0200 | [diff] [blame] | 8 | * |
Andrey Andreev | cce6bd1 | 2018-01-09 11:32:02 +0200 | [diff] [blame^] | 9 | * Copyright (c) 2014 - 2018, British Columbia Institute of Technology |
Andrey Andreev | 7ac33d7 | 2012-01-07 19:39:39 +0200 | [diff] [blame] | 10 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | * of this software and associated documentation files (the "Software"), to deal |
| 13 | * in the Software without restriction, including without limitation the rights |
| 14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | * copies of the Software, and to permit persons to whom the Software is |
| 16 | * furnished to do so, subject to the following conditions: |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 17 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 18 | * The above copyright notice and this permission notice shall be included in |
| 19 | * all copies or substantial portions of the Software. |
| 20 | * |
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 27 | * THE SOFTWARE. |
| 28 | * |
| 29 | * @package CodeIgniter |
| 30 | * @author EllisLab Dev Team |
Andrey Andreev | 1924e87 | 2016-01-11 12:55:34 +0200 | [diff] [blame] | 31 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/) |
Andrey Andreev | cce6bd1 | 2018-01-09 11:32:02 +0200 | [diff] [blame^] | 32 | * @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/) |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 33 | * @license http://opensource.org/licenses/MIT MIT License |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 34 | * @link https://codeigniter.com |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 35 | * @since Version 1.0.0 |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 36 | * @filesource |
| 37 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 38 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 39 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 40 | /** |
| 41 | * Exceptions Class |
| 42 | * |
| 43 | * @package CodeIgniter |
| 44 | * @subpackage Libraries |
| 45 | * @category Exceptions |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 46 | * @author EllisLab Dev Team |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 47 | * @link https://codeigniter.com/user_guide/libraries/exceptions.html |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 48 | */ |
| 49 | class CI_Exceptions { |
Andrey Andreev | 7ac33d7 | 2012-01-07 19:39:39 +0200 | [diff] [blame] | 50 | |
David Behler | 209b2cf | 2011-08-14 23:00:43 +0200 | [diff] [blame] | 51 | /** |
| 52 | * Nesting level of the output buffering mechanism |
David Behler | 209b2cf | 2011-08-14 23:00:43 +0200 | [diff] [blame] | 53 | * |
Timothy Warren | 48a7fbb | 2012-04-23 11:58:16 -0400 | [diff] [blame] | 54 | * @var int |
David Behler | 209b2cf | 2011-08-14 23:00:43 +0200 | [diff] [blame] | 55 | */ |
Andrey Andreev | 7ac33d7 | 2012-01-07 19:39:39 +0200 | [diff] [blame] | 56 | public $ob_level; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 57 | |
David Behler | 209b2cf | 2011-08-14 23:00:43 +0200 | [diff] [blame] | 58 | /** |
vlakoff | d5ce508 | 2014-04-25 10:13:04 +0200 | [diff] [blame] | 59 | * List of available error levels |
David Behler | 209b2cf | 2011-08-14 23:00:43 +0200 | [diff] [blame] | 60 | * |
Timothy Warren | 48a7fbb | 2012-04-23 11:58:16 -0400 | [diff] [blame] | 61 | * @var array |
David Behler | 209b2cf | 2011-08-14 23:00:43 +0200 | [diff] [blame] | 62 | */ |
Andrey Andreev | 7ac33d7 | 2012-01-07 19:39:39 +0200 | [diff] [blame] | 63 | public $levels = array( |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame] | 64 | E_ERROR => 'Error', |
| 65 | E_WARNING => 'Warning', |
| 66 | E_PARSE => 'Parsing Error', |
| 67 | E_NOTICE => 'Notice', |
| 68 | E_CORE_ERROR => 'Core Error', |
| 69 | E_CORE_WARNING => 'Core Warning', |
| 70 | E_COMPILE_ERROR => 'Compile Error', |
| 71 | E_COMPILE_WARNING => 'Compile Warning', |
| 72 | E_USER_ERROR => 'User Error', |
| 73 | E_USER_WARNING => 'User Warning', |
| 74 | E_USER_NOTICE => 'User Notice', |
| 75 | E_STRICT => 'Runtime Notice' |
| 76 | ); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 77 | |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 78 | /** |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 79 | * Class constructor |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 80 | * |
| 81 | * @return void |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 82 | */ |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 83 | public function __construct() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 84 | { |
| 85 | $this->ob_level = ob_get_level(); |
Andrey Andreev | 7ac33d7 | 2012-01-07 19:39:39 +0200 | [diff] [blame] | 86 | // Note: Do not log messages from this constructor. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 87 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 88 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 89 | // -------------------------------------------------------------------- |
| 90 | |
| 91 | /** |
| 92 | * Exception Logger |
| 93 | * |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 94 | * Logs PHP generated error messages |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 95 | * |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 96 | * @param int $severity Log level |
| 97 | * @param string $message Error message |
| 98 | * @param string $filepath File path |
| 99 | * @param int $line Line number |
Andrey Andreev | 7ac33d7 | 2012-01-07 19:39:39 +0200 | [diff] [blame] | 100 | * @return void |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 101 | */ |
Andrey Andreev | 7ac33d7 | 2012-01-07 19:39:39 +0200 | [diff] [blame] | 102 | public function log_exception($severity, $message, $filepath, $line) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 103 | { |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 104 | $severity = isset($this->levels[$severity]) ? $this->levels[$severity] : $severity; |
Andrey Andreev | 29947ee | 2013-11-12 18:36:46 +0200 | [diff] [blame] | 105 | log_message('error', 'Severity: '.$severity.' --> '.$message.' '.$filepath.' '.$line); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | // -------------------------------------------------------------------- |
| 109 | |
| 110 | /** |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 111 | * 404 Error Handler |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 112 | * |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 113 | * @uses CI_Exceptions::show_error() |
| 114 | * |
| 115 | * @param string $page Page URI |
| 116 | * @param bool $log_error Whether to log the error |
| 117 | * @return void |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 118 | */ |
Andrey Andreev | 7ac33d7 | 2012-01-07 19:39:39 +0200 | [diff] [blame] | 119 | public function show_404($page = '', $log_error = TRUE) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 120 | { |
Andrey Andreev | 29947ee | 2013-11-12 18:36:46 +0200 | [diff] [blame] | 121 | if (is_cli()) |
| 122 | { |
| 123 | $heading = 'Not Found'; |
| 124 | $message = 'The controller/method pair you requested was not found.'; |
| 125 | } |
| 126 | else |
| 127 | { |
| 128 | $heading = '404 Page Not Found'; |
| 129 | $message = 'The page you requested was not found.'; |
| 130 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 131 | |
Derek Allard | 2ddc949 | 2010-08-05 10:08:33 -0400 | [diff] [blame] | 132 | // By default we log this, but allow a dev to skip it |
| 133 | if ($log_error) |
| 134 | { |
Andrey Andreev | 29947ee | 2013-11-12 18:36:46 +0200 | [diff] [blame] | 135 | log_message('error', $heading.': '.$page); |
Derek Allard | 2ddc949 | 2010-08-05 10:08:33 -0400 | [diff] [blame] | 136 | } |
| 137 | |
Derek Jones | f6b442b | 2009-07-11 18:23:30 +0000 | [diff] [blame] | 138 | echo $this->show_error($heading, $message, 'error_404', 404); |
Andrey Andreev | 7cf682a | 2014-03-13 14:55:45 +0200 | [diff] [blame] | 139 | exit(4); // EXIT_UNKNOWN_FILE |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 140 | } |
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 | // -------------------------------------------------------------------- |
| 143 | |
| 144 | /** |
| 145 | * General Error Page |
| 146 | * |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 147 | * Takes an error message as input (either as a string or an array) |
| 148 | * and displays it using the specified template. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 149 | * |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 150 | * @param string $heading Page heading |
| 151 | * @param string|string[] $message Error message |
| 152 | * @param string $template Template name |
Andrey Andreev | 5232ba0 | 2012-10-27 15:25:05 +0300 | [diff] [blame] | 153 | * @param int $status_code (default: 500) |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 154 | * |
| 155 | * @return string Error page output |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 156 | */ |
Andrey Andreev | 7ac33d7 | 2012-01-07 19:39:39 +0200 | [diff] [blame] | 157 | public function show_error($heading, $message, $template = 'error_general', $status_code = 500) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 158 | { |
Andrey Andreev | d444d44 | 2014-10-06 00:00:08 +0300 | [diff] [blame] | 159 | $templates_path = config_item('error_views_path'); |
| 160 | if (empty($templates_path)) |
| 161 | { |
| 162 | $templates_path = VIEWPATH.'errors'.DIRECTORY_SEPARATOR; |
| 163 | } |
vlakoff | 08ea83b | 2014-04-14 14:34:06 +0200 | [diff] [blame] | 164 | |
Andrey Andreev | 29947ee | 2013-11-12 18:36:46 +0200 | [diff] [blame] | 165 | if (is_cli()) |
| 166 | { |
| 167 | $message = "\t".(is_array($message) ? implode("\n\t", $message) : $message); |
| 168 | $template = 'cli'.DIRECTORY_SEPARATOR.$template; |
| 169 | } |
| 170 | else |
| 171 | { |
| 172 | set_status_header($status_code); |
| 173 | $message = '<p>'.(is_array($message) ? implode('</p><p>', $message) : $message).'</p>'; |
| 174 | $template = 'html'.DIRECTORY_SEPARATOR.$template; |
| 175 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 176 | |
| 177 | if (ob_get_level() > $this->ob_level + 1) |
| 178 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 179 | ob_end_flush(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 180 | } |
| 181 | ob_start(); |
vlakoff | 08ea83b | 2014-04-14 14:34:06 +0200 | [diff] [blame] | 182 | include($templates_path.$template.'.php'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 183 | $buffer = ob_get_contents(); |
| 184 | ob_end_clean(); |
| 185 | return $buffer; |
| 186 | } |
| 187 | |
| 188 | // -------------------------------------------------------------------- |
| 189 | |
Andrey Andreev | 84f24c2 | 2015-09-24 15:17:28 +0300 | [diff] [blame] | 190 | public function show_exception($exception) |
Andrey Andreev | 4b838af | 2014-10-28 23:46:45 +0200 | [diff] [blame] | 191 | { |
| 192 | $templates_path = config_item('error_views_path'); |
| 193 | if (empty($templates_path)) |
| 194 | { |
| 195 | $templates_path = VIEWPATH.'errors'.DIRECTORY_SEPARATOR; |
| 196 | } |
| 197 | |
| 198 | $message = $exception->getMessage(); |
| 199 | if (empty($message)) |
| 200 | { |
| 201 | $message = '(null)'; |
| 202 | } |
| 203 | |
| 204 | if (is_cli()) |
| 205 | { |
| 206 | $templates_path .= 'cli'.DIRECTORY_SEPARATOR; |
| 207 | } |
| 208 | else |
| 209 | { |
Andrey Andreev | 4b838af | 2014-10-28 23:46:45 +0200 | [diff] [blame] | 210 | $templates_path .= 'html'.DIRECTORY_SEPARATOR; |
| 211 | } |
| 212 | |
| 213 | if (ob_get_level() > $this->ob_level + 1) |
| 214 | { |
| 215 | ob_end_flush(); |
| 216 | } |
| 217 | |
| 218 | ob_start(); |
| 219 | include($templates_path.'error_exception.php'); |
| 220 | $buffer = ob_get_contents(); |
| 221 | ob_end_clean(); |
| 222 | echo $buffer; |
| 223 | } |
| 224 | |
| 225 | // -------------------------------------------------------------------- |
| 226 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 227 | /** |
| 228 | * Native PHP error handler |
| 229 | * |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 230 | * @param int $severity Error level |
| 231 | * @param string $message Error message |
| 232 | * @param string $filepath File path |
| 233 | * @param int $line Line number |
Andrey Andreev | ee9d428 | 2017-06-05 10:44:37 +0300 | [diff] [blame] | 234 | * @return void |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 235 | */ |
Andrey Andreev | c6a68e0 | 2012-03-26 14:30:10 +0300 | [diff] [blame] | 236 | public function show_php_error($severity, $message, $filepath, $line) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 237 | { |
Andrey Andreev | d444d44 | 2014-10-06 00:00:08 +0300 | [diff] [blame] | 238 | $templates_path = config_item('error_views_path'); |
| 239 | if (empty($templates_path)) |
| 240 | { |
| 241 | $templates_path = VIEWPATH.'errors'.DIRECTORY_SEPARATOR; |
| 242 | } |
vlakoff | 08ea83b | 2014-04-14 14:34:06 +0200 | [diff] [blame] | 243 | |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 244 | $severity = isset($this->levels[$severity]) ? $this->levels[$severity] : $severity; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 245 | |
Andrey Andreev | 29947ee | 2013-11-12 18:36:46 +0200 | [diff] [blame] | 246 | // For safety reasons we don't show the full file path in non-CLI requests |
| 247 | if ( ! is_cli()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 248 | { |
Andrey Andreev | 29947ee | 2013-11-12 18:36:46 +0200 | [diff] [blame] | 249 | $filepath = str_replace('\\', '/', $filepath); |
| 250 | if (FALSE !== strpos($filepath, '/')) |
| 251 | { |
| 252 | $x = explode('/', $filepath); |
| 253 | $filepath = $x[count($x)-2].'/'.end($x); |
| 254 | } |
| 255 | |
| 256 | $template = 'html'.DIRECTORY_SEPARATOR.'error_php'; |
| 257 | } |
| 258 | else |
| 259 | { |
| 260 | $template = 'cli'.DIRECTORY_SEPARATOR.'error_php'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 261 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 262 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 263 | if (ob_get_level() > $this->ob_level + 1) |
| 264 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 265 | ob_end_flush(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 266 | } |
| 267 | ob_start(); |
vlakoff | 08ea83b | 2014-04-14 14:34:06 +0200 | [diff] [blame] | 268 | include($templates_path.$template.'.php'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 269 | $buffer = ob_get_contents(); |
| 270 | ob_end_clean(); |
| 271 | echo $buffer; |
| 272 | } |
| 273 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 274 | } |