blob: 6324fba2bdf989f78f467c50610c2fafd3c4b558 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
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 Andreevbdb96ca2014-10-28 00:13:31 +02009 * Copyright (c) 2014, 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
darwinel871754a2014-02-11 17:34:57 +010031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020032 * @copyright Copyright (c) 2014, British Columbia Institute of Technology (http://bcit.ca/)
33 * @license http://opensource.org/licenses/MIT MIT License
34 * @link http://codeigniter.com
35 * @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
Derek Allard2067d1a2008-11-13 22:59:24 +000047 * @link http://codeigniter.com/user_guide/libraries/exceptions.html
48 */
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
190 /**
191 * Native PHP error handler
192 *
Andrey Andreev3e9d2b82012-10-27 14:28:51 +0300193 * @param int $severity Error level
194 * @param string $message Error message
195 * @param string $filepath File path
196 * @param int $line Line number
197 * @return string Error page output
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 */
Andrey Andreevc6a68e02012-03-26 14:30:10 +0300199 public function show_php_error($severity, $message, $filepath, $line)
Barry Mienydd671972010-10-04 16:33:58 +0200200 {
Andrey Andreevd444d442014-10-06 00:00:08 +0300201 $templates_path = config_item('error_views_path');
202 if (empty($templates_path))
203 {
204 $templates_path = VIEWPATH.'errors'.DIRECTORY_SEPARATOR;
205 }
vlakoff08ea83b2014-04-14 14:34:06 +0200206
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300207 $severity = isset($this->levels[$severity]) ? $this->levels[$severity] : $severity;
Barry Mienydd671972010-10-04 16:33:58 +0200208
Andrey Andreev29947ee2013-11-12 18:36:46 +0200209 // For safety reasons we don't show the full file path in non-CLI requests
210 if ( ! is_cli())
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 {
Andrey Andreev29947ee2013-11-12 18:36:46 +0200212 $filepath = str_replace('\\', '/', $filepath);
213 if (FALSE !== strpos($filepath, '/'))
214 {
215 $x = explode('/', $filepath);
216 $filepath = $x[count($x)-2].'/'.end($x);
217 }
218
219 $template = 'html'.DIRECTORY_SEPARATOR.'error_php';
220 }
221 else
222 {
223 $template = 'cli'.DIRECTORY_SEPARATOR.'error_php';
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 }
Barry Mienydd671972010-10-04 16:33:58 +0200225
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 if (ob_get_level() > $this->ob_level + 1)
227 {
Barry Mienydd671972010-10-04 16:33:58 +0200228 ob_end_flush();
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 }
230 ob_start();
vlakoff08ea83b2014-04-14 14:34:06 +0200231 include($templates_path.$template.'.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 $buffer = ob_get_contents();
233 ob_end_clean();
234 echo $buffer;
235 }
236
Derek Allard2067d1a2008-11-13 22:59:24 +0000237}
Derek Allard2067d1a2008-11-13 22:59:24 +0000238
239/* End of file Exceptions.php */
Andrey Andreevc6a68e02012-03-26 14:30:10 +0300240/* Location: ./system/core/Exceptions.php */