blob: 5a78376a3faaef3e94100b271953456f9f05bfd1 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreev7ac33d72012-01-07 19:39:39 +02008 *
Andrey Andreevcce6bd12018-01-09 11:32:02 +02009 * Copyright (c) 2014 - 2018, British Columbia Institute of Technology
Andrey Andreev7ac33d72012-01-07 19:39:39 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * 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 Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * 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 Andreev1924e872016-01-11 12:55:34 +020031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
Andrey Andreevcce6bd12018-01-09 11:32:02 +020032 * @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
Andrey Andreevbd202c92016-01-11 12:50:18 +020034 * @link https://codeigniter.com
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020035 * @since Version 1.0.0
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000039
Derek Allard2067d1a2008-11-13 22:59:24 +000040/**
41 * Exceptions Class
42 *
43 * @package CodeIgniter
44 * @subpackage Libraries
45 * @category Exceptions
Derek Jonesf4a4bd82011-10-20 12:18:42 -050046 * @author EllisLab Dev Team
Andrey Andreevbd202c92016-01-11 12:50:18 +020047 * @link https://codeigniter.com/user_guide/libraries/exceptions.html
Derek Allard2067d1a2008-11-13 22:59:24 +000048 */
49class CI_Exceptions {
Andrey Andreev7ac33d72012-01-07 19:39:39 +020050
David Behler209b2cf2011-08-14 23:00:43 +020051 /**
52 * Nesting level of the output buffering mechanism
David Behler209b2cf2011-08-14 23:00:43 +020053 *
Timothy Warren48a7fbb2012-04-23 11:58:16 -040054 * @var int
David Behler209b2cf2011-08-14 23:00:43 +020055 */
Andrey Andreev7ac33d72012-01-07 19:39:39 +020056 public $ob_level;
Derek Allard2067d1a2008-11-13 22:59:24 +000057
David Behler209b2cf2011-08-14 23:00:43 +020058 /**
vlakoffd5ce5082014-04-25 10:13:04 +020059 * List of available error levels
David Behler209b2cf2011-08-14 23:00:43 +020060 *
Timothy Warren48a7fbb2012-04-23 11:58:16 -040061 * @var array
David Behler209b2cf2011-08-14 23:00:43 +020062 */
Andrey Andreev7ac33d72012-01-07 19:39:39 +020063 public $levels = array(
Timothy Warren40403d22012-04-19 16:38:50 -040064 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 Allard2067d1a2008-11-13 22:59:24 +000077
Timothy Warrenad475052012-04-19 13:21:06 -040078 /**
Andrey Andreev3e9d2b82012-10-27 14:28:51 +030079 * Class constructor
Andrey Andreev92ebfb62012-05-17 12:49:24 +030080 *
81 * @return void
Timothy Warrenad475052012-04-19 13:21:06 -040082 */
Greg Akera9263282010-11-10 15:26:43 -060083 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000084 {
85 $this->ob_level = ob_get_level();
Andrey Andreev7ac33d72012-01-07 19:39:39 +020086 // Note: Do not log messages from this constructor.
Derek Allard2067d1a2008-11-13 22:59:24 +000087 }
Barry Mienydd671972010-10-04 16:33:58 +020088
Derek Allard2067d1a2008-11-13 22:59:24 +000089 // --------------------------------------------------------------------
90
91 /**
92 * Exception Logger
93 *
Andrey Andreev3e9d2b82012-10-27 14:28:51 +030094 * Logs PHP generated error messages
Derek Allard2067d1a2008-11-13 22:59:24 +000095 *
Andrey Andreev3e9d2b82012-10-27 14:28:51 +030096 * @param int $severity Log level
97 * @param string $message Error message
98 * @param string $filepath File path
99 * @param int $line Line number
Andrey Andreev7ac33d72012-01-07 19:39:39 +0200100 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000101 */
Andrey Andreev7ac33d72012-01-07 19:39:39 +0200102 public function log_exception($severity, $message, $filepath, $line)
Barry Mienydd671972010-10-04 16:33:58 +0200103 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300104 $severity = isset($this->levels[$severity]) ? $this->levels[$severity] : $severity;
Andrey Andreev29947ee2013-11-12 18:36:46 +0200105 log_message('error', 'Severity: '.$severity.' --> '.$message.' '.$filepath.' '.$line);
Derek Allard2067d1a2008-11-13 22:59:24 +0000106 }
107
108 // --------------------------------------------------------------------
109
110 /**
Andrey Andreev3e9d2b82012-10-27 14:28:51 +0300111 * 404 Error Handler
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 *
Andrey Andreev3e9d2b82012-10-27 14:28:51 +0300113 * @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 Allard2067d1a2008-11-13 22:59:24 +0000118 */
Andrey Andreev7ac33d72012-01-07 19:39:39 +0200119 public function show_404($page = '', $log_error = TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200120 {
Andrey Andreev29947ee2013-11-12 18:36:46 +0200121 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 Allard2067d1a2008-11-13 22:59:24 +0000131
Derek Allard2ddc9492010-08-05 10:08:33 -0400132 // By default we log this, but allow a dev to skip it
133 if ($log_error)
134 {
Andrey Andreev29947ee2013-11-12 18:36:46 +0200135 log_message('error', $heading.': '.$page);
Derek Allard2ddc9492010-08-05 10:08:33 -0400136 }
137
Derek Jonesf6b442b2009-07-11 18:23:30 +0000138 echo $this->show_error($heading, $message, 'error_404', 404);
Andrey Andreev7cf682a2014-03-13 14:55:45 +0200139 exit(4); // EXIT_UNKNOWN_FILE
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 }
Barry Mienydd671972010-10-04 16:33:58 +0200141
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 // --------------------------------------------------------------------
143
144 /**
145 * General Error Page
146 *
Andrey Andreev3e9d2b82012-10-27 14:28:51 +0300147 * Takes an error message as input (either as a string or an array)
148 * and displays it using the specified template.
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 *
Andrey Andreev3e9d2b82012-10-27 14:28:51 +0300150 * @param string $heading Page heading
151 * @param string|string[] $message Error message
152 * @param string $template Template name
Andrey Andreev5232ba02012-10-27 15:25:05 +0300153 * @param int $status_code (default: 500)
Andrey Andreev3e9d2b82012-10-27 14:28:51 +0300154 *
155 * @return string Error page output
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 */
Andrey Andreev7ac33d72012-01-07 19:39:39 +0200157 public function show_error($heading, $message, $template = 'error_general', $status_code = 500)
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 {
Andrey Andreevd444d442014-10-06 00:00:08 +0300159 $templates_path = config_item('error_views_path');
160 if (empty($templates_path))
161 {
162 $templates_path = VIEWPATH.'errors'.DIRECTORY_SEPARATOR;
163 }
vlakoff08ea83b2014-04-14 14:34:06 +0200164
Andrey Andreev29947ee2013-11-12 18:36:46 +0200165 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 Allard2067d1a2008-11-13 22:59:24 +0000176
177 if (ob_get_level() > $this->ob_level + 1)
178 {
Barry Mienydd671972010-10-04 16:33:58 +0200179 ob_end_flush();
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 }
181 ob_start();
vlakoff08ea83b2014-04-14 14:34:06 +0200182 include($templates_path.$template.'.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 $buffer = ob_get_contents();
184 ob_end_clean();
185 return $buffer;
186 }
187
188 // --------------------------------------------------------------------
189
Andrey Andreev84f24c22015-09-24 15:17:28 +0300190 public function show_exception($exception)
Andrey Andreev4b838af2014-10-28 23:46:45 +0200191 {
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 Andreev4b838af2014-10-28 23:46:45 +0200210 $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 Allard2067d1a2008-11-13 22:59:24 +0000227 /**
228 * Native PHP error handler
229 *
Andrey Andreev3e9d2b82012-10-27 14:28:51 +0300230 * @param int $severity Error level
231 * @param string $message Error message
232 * @param string $filepath File path
233 * @param int $line Line number
Andrey Andreevee9d4282017-06-05 10:44:37 +0300234 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 */
Andrey Andreevc6a68e02012-03-26 14:30:10 +0300236 public function show_php_error($severity, $message, $filepath, $line)
Barry Mienydd671972010-10-04 16:33:58 +0200237 {
Andrey Andreevd444d442014-10-06 00:00:08 +0300238 $templates_path = config_item('error_views_path');
239 if (empty($templates_path))
240 {
241 $templates_path = VIEWPATH.'errors'.DIRECTORY_SEPARATOR;
242 }
vlakoff08ea83b2014-04-14 14:34:06 +0200243
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300244 $severity = isset($this->levels[$severity]) ? $this->levels[$severity] : $severity;
Barry Mienydd671972010-10-04 16:33:58 +0200245
Andrey Andreev29947ee2013-11-12 18:36:46 +0200246 // For safety reasons we don't show the full file path in non-CLI requests
247 if ( ! is_cli())
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 {
Andrey Andreev29947ee2013-11-12 18:36:46 +0200249 $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 Allard2067d1a2008-11-13 22:59:24 +0000261 }
Barry Mienydd671972010-10-04 16:33:58 +0200262
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 if (ob_get_level() > $this->ob_level + 1)
264 {
Barry Mienydd671972010-10-04 16:33:58 +0200265 ob_end_flush();
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 }
267 ob_start();
vlakoff08ea83b2014-04-14 14:34:06 +0200268 include($templates_path.$template.'.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 $buffer = ob_get_contents();
270 ob_end_clean();
271 echo $buffer;
272 }
273
Derek Allard2067d1a2008-11-13 22:59:24 +0000274}