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 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 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 |
darwinel | 871754a | 2014-02-11 17:34:57 +0100 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2014, 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 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 27 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 29 | /** |
| 30 | * Exceptions Class |
| 31 | * |
| 32 | * @package CodeIgniter |
| 33 | * @subpackage Libraries |
| 34 | * @category Exceptions |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 35 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 36 | * @link http://codeigniter.com/user_guide/libraries/exceptions.html |
| 37 | */ |
| 38 | class CI_Exceptions { |
Andrey Andreev | 7ac33d7 | 2012-01-07 19:39:39 +0200 | [diff] [blame] | 39 | |
David Behler | 209b2cf | 2011-08-14 23:00:43 +0200 | [diff] [blame] | 40 | /** |
| 41 | * Nesting level of the output buffering mechanism |
David Behler | 209b2cf | 2011-08-14 23:00:43 +0200 | [diff] [blame] | 42 | * |
Timothy Warren | 48a7fbb | 2012-04-23 11:58:16 -0400 | [diff] [blame] | 43 | * @var int |
David Behler | 209b2cf | 2011-08-14 23:00:43 +0200 | [diff] [blame] | 44 | */ |
Andrey Andreev | 7ac33d7 | 2012-01-07 19:39:39 +0200 | [diff] [blame] | 45 | public $ob_level; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 46 | |
David Behler | 209b2cf | 2011-08-14 23:00:43 +0200 | [diff] [blame] | 47 | /** |
vlakoff | d5ce508 | 2014-04-25 10:13:04 +0200 | [diff] [blame] | 48 | * List of available error levels |
David Behler | 209b2cf | 2011-08-14 23:00:43 +0200 | [diff] [blame] | 49 | * |
Timothy Warren | 48a7fbb | 2012-04-23 11:58:16 -0400 | [diff] [blame] | 50 | * @var array |
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 $levels = array( |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame] | 53 | E_ERROR => 'Error', |
| 54 | E_WARNING => 'Warning', |
| 55 | E_PARSE => 'Parsing Error', |
| 56 | E_NOTICE => 'Notice', |
| 57 | E_CORE_ERROR => 'Core Error', |
| 58 | E_CORE_WARNING => 'Core Warning', |
| 59 | E_COMPILE_ERROR => 'Compile Error', |
| 60 | E_COMPILE_WARNING => 'Compile Warning', |
| 61 | E_USER_ERROR => 'User Error', |
| 62 | E_USER_WARNING => 'User Warning', |
| 63 | E_USER_NOTICE => 'User Notice', |
| 64 | E_STRICT => 'Runtime Notice' |
| 65 | ); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 66 | |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 67 | /** |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 68 | * Class constructor |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 69 | * |
| 70 | * @return void |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 71 | */ |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 72 | public function __construct() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 73 | { |
| 74 | $this->ob_level = ob_get_level(); |
Andrey Andreev | 7ac33d7 | 2012-01-07 19:39:39 +0200 | [diff] [blame] | 75 | // Note: Do not log messages from this constructor. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 76 | } |
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 | // -------------------------------------------------------------------- |
| 79 | |
| 80 | /** |
| 81 | * Exception Logger |
| 82 | * |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 83 | * Logs PHP generated error messages |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 84 | * |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 85 | * @param int $severity Log level |
| 86 | * @param string $message Error message |
| 87 | * @param string $filepath File path |
| 88 | * @param int $line Line number |
Andrey Andreev | 7ac33d7 | 2012-01-07 19:39:39 +0200 | [diff] [blame] | 89 | * @return void |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 90 | */ |
Andrey Andreev | 7ac33d7 | 2012-01-07 19:39:39 +0200 | [diff] [blame] | 91 | public function log_exception($severity, $message, $filepath, $line) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 92 | { |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 93 | $severity = isset($this->levels[$severity]) ? $this->levels[$severity] : $severity; |
Andrey Andreev | 29947ee | 2013-11-12 18:36:46 +0200 | [diff] [blame] | 94 | log_message('error', 'Severity: '.$severity.' --> '.$message.' '.$filepath.' '.$line); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | // -------------------------------------------------------------------- |
| 98 | |
| 99 | /** |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 100 | * 404 Error Handler |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 101 | * |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 102 | * @uses CI_Exceptions::show_error() |
| 103 | * |
| 104 | * @param string $page Page URI |
| 105 | * @param bool $log_error Whether to log the error |
| 106 | * @return void |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 | 29947ee | 2013-11-12 18:36:46 +0200 | [diff] [blame] | 110 | if (is_cli()) |
| 111 | { |
| 112 | $heading = 'Not Found'; |
| 113 | $message = 'The controller/method pair you requested was not found.'; |
| 114 | } |
| 115 | else |
| 116 | { |
| 117 | $heading = '404 Page Not Found'; |
| 118 | $message = 'The page you requested was not found.'; |
| 119 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 120 | |
Derek Allard | 2ddc949 | 2010-08-05 10:08:33 -0400 | [diff] [blame] | 121 | // By default we log this, but allow a dev to skip it |
| 122 | if ($log_error) |
| 123 | { |
Andrey Andreev | 29947ee | 2013-11-12 18:36:46 +0200 | [diff] [blame] | 124 | log_message('error', $heading.': '.$page); |
Derek Allard | 2ddc949 | 2010-08-05 10:08:33 -0400 | [diff] [blame] | 125 | } |
| 126 | |
Derek Jones | f6b442b | 2009-07-11 18:23:30 +0000 | [diff] [blame] | 127 | echo $this->show_error($heading, $message, 'error_404', 404); |
Andrey Andreev | 7cf682a | 2014-03-13 14:55:45 +0200 | [diff] [blame] | 128 | exit(4); // EXIT_UNKNOWN_FILE |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 129 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 130 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 131 | // -------------------------------------------------------------------- |
| 132 | |
| 133 | /** |
| 134 | * General Error Page |
| 135 | * |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 136 | * Takes an error message as input (either as a string or an array) |
| 137 | * and displays it using the specified template. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 138 | * |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 139 | * @param string $heading Page heading |
| 140 | * @param string|string[] $message Error message |
| 141 | * @param string $template Template name |
Andrey Andreev | 5232ba0 | 2012-10-27 15:25:05 +0300 | [diff] [blame] | 142 | * @param int $status_code (default: 500) |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 143 | * |
| 144 | * @return string Error page output |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 145 | */ |
Andrey Andreev | 7ac33d7 | 2012-01-07 19:39:39 +0200 | [diff] [blame] | 146 | public function show_error($heading, $message, $template = 'error_general', $status_code = 500) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 147 | { |
Andrey Andreev | d444d44 | 2014-10-06 00:00:08 +0300 | [diff] [blame^] | 148 | $templates_path = config_item('error_views_path'); |
| 149 | if (empty($templates_path)) |
| 150 | { |
| 151 | $templates_path = VIEWPATH.'errors'.DIRECTORY_SEPARATOR; |
| 152 | } |
vlakoff | 08ea83b | 2014-04-14 14:34:06 +0200 | [diff] [blame] | 153 | |
Andrey Andreev | 29947ee | 2013-11-12 18:36:46 +0200 | [diff] [blame] | 154 | if (is_cli()) |
| 155 | { |
| 156 | $message = "\t".(is_array($message) ? implode("\n\t", $message) : $message); |
| 157 | $template = 'cli'.DIRECTORY_SEPARATOR.$template; |
| 158 | } |
| 159 | else |
| 160 | { |
| 161 | set_status_header($status_code); |
| 162 | $message = '<p>'.(is_array($message) ? implode('</p><p>', $message) : $message).'</p>'; |
| 163 | $template = 'html'.DIRECTORY_SEPARATOR.$template; |
| 164 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 165 | |
| 166 | if (ob_get_level() > $this->ob_level + 1) |
| 167 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 168 | ob_end_flush(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 169 | } |
| 170 | ob_start(); |
vlakoff | 08ea83b | 2014-04-14 14:34:06 +0200 | [diff] [blame] | 171 | include($templates_path.$template.'.php'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 172 | $buffer = ob_get_contents(); |
| 173 | ob_end_clean(); |
| 174 | return $buffer; |
| 175 | } |
| 176 | |
| 177 | // -------------------------------------------------------------------- |
| 178 | |
| 179 | /** |
| 180 | * Native PHP error handler |
| 181 | * |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 182 | * @param int $severity Error level |
| 183 | * @param string $message Error message |
| 184 | * @param string $filepath File path |
| 185 | * @param int $line Line number |
| 186 | * @return string Error page output |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 187 | */ |
Andrey Andreev | c6a68e0 | 2012-03-26 14:30:10 +0300 | [diff] [blame] | 188 | public function show_php_error($severity, $message, $filepath, $line) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 189 | { |
Andrey Andreev | d444d44 | 2014-10-06 00:00:08 +0300 | [diff] [blame^] | 190 | $templates_path = config_item('error_views_path'); |
| 191 | if (empty($templates_path)) |
| 192 | { |
| 193 | $templates_path = VIEWPATH.'errors'.DIRECTORY_SEPARATOR; |
| 194 | } |
vlakoff | 08ea83b | 2014-04-14 14:34:06 +0200 | [diff] [blame] | 195 | |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 196 | $severity = isset($this->levels[$severity]) ? $this->levels[$severity] : $severity; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 197 | |
Andrey Andreev | 29947ee | 2013-11-12 18:36:46 +0200 | [diff] [blame] | 198 | // For safety reasons we don't show the full file path in non-CLI requests |
| 199 | if ( ! is_cli()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 200 | { |
Andrey Andreev | 29947ee | 2013-11-12 18:36:46 +0200 | [diff] [blame] | 201 | $filepath = str_replace('\\', '/', $filepath); |
| 202 | if (FALSE !== strpos($filepath, '/')) |
| 203 | { |
| 204 | $x = explode('/', $filepath); |
| 205 | $filepath = $x[count($x)-2].'/'.end($x); |
| 206 | } |
| 207 | |
| 208 | $template = 'html'.DIRECTORY_SEPARATOR.'error_php'; |
| 209 | } |
| 210 | else |
| 211 | { |
| 212 | $template = 'cli'.DIRECTORY_SEPARATOR.'error_php'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 213 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 214 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 215 | if (ob_get_level() > $this->ob_level + 1) |
| 216 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 217 | ob_end_flush(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 218 | } |
| 219 | ob_start(); |
vlakoff | 08ea83b | 2014-04-14 14:34:06 +0200 | [diff] [blame] | 220 | include($templates_path.$template.'.php'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 221 | $buffer = ob_get_contents(); |
| 222 | ob_end_clean(); |
| 223 | echo $buffer; |
| 224 | } |
| 225 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 226 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 227 | |
| 228 | /* End of file Exceptions.php */ |
Andrey Andreev | c6a68e0 | 2012-03-26 14:30:10 +0300 | [diff] [blame] | 229 | /* Location: ./system/core/Exceptions.php */ |