blob: 398459d54e9720078d17275909657c37c0e7e780 [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 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev7ac33d72012-01-07 19:39:39 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev7ac33d72012-01-07 19:39:39 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * 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 Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
darwinel871754a2014-02-11 17:34:57 +010021 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * Exceptions Class
31 *
32 * @package CodeIgniter
33 * @subpackage Libraries
34 * @category Exceptions
Derek Jonesf4a4bd82011-10-20 12:18:42 -050035 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @link http://codeigniter.com/user_guide/libraries/exceptions.html
37 */
38class CI_Exceptions {
Andrey Andreev7ac33d72012-01-07 19:39:39 +020039
David Behler209b2cf2011-08-14 23:00:43 +020040 /**
41 * Nesting level of the output buffering mechanism
David Behler209b2cf2011-08-14 23:00:43 +020042 *
Timothy Warren48a7fbb2012-04-23 11:58:16 -040043 * @var int
David Behler209b2cf2011-08-14 23:00:43 +020044 */
Andrey Andreev7ac33d72012-01-07 19:39:39 +020045 public $ob_level;
Derek Allard2067d1a2008-11-13 22:59:24 +000046
David Behler209b2cf2011-08-14 23:00:43 +020047 /**
vlakoffcdf3dfa2014-04-12 07:33:42 +020048 * Path to the error templates
49 *
50 * @var string
51 */
52 protected $_templates_path;
53
54 /**
David Behler209b2cf2011-08-14 23:00:43 +020055 * List if available error levels
56 *
Timothy Warren48a7fbb2012-04-23 11:58:16 -040057 * @var array
David Behler209b2cf2011-08-14 23:00:43 +020058 */
Andrey Andreev7ac33d72012-01-07 19:39:39 +020059 public $levels = array(
Timothy Warren40403d22012-04-19 16:38:50 -040060 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 Allard2067d1a2008-11-13 22:59:24 +000073
Timothy Warrenad475052012-04-19 13:21:06 -040074 /**
Andrey Andreev3e9d2b82012-10-27 14:28:51 +030075 * Class constructor
Andrey Andreev92ebfb62012-05-17 12:49:24 +030076 *
77 * @return void
Timothy Warrenad475052012-04-19 13:21:06 -040078 */
Greg Akera9263282010-11-10 15:26:43 -060079 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000080 {
81 $this->ob_level = ob_get_level();
Andrey Andreev7ac33d72012-01-07 19:39:39 +020082 // Note: Do not log messages from this constructor.
vlakoffcdf3dfa2014-04-12 07:33:42 +020083
84 $config =& get_config();
85
86 $this->_templates_path = (isset($config['error_templates_path']) && $config['error_templates_path'] !== '')
87 ? $config['error_templates_path']
88 : VIEWPATH.'errors'.DIRECTORY_SEPARATOR;
Derek Allard2067d1a2008-11-13 22:59:24 +000089 }
Barry Mienydd671972010-10-04 16:33:58 +020090
Derek Allard2067d1a2008-11-13 22:59:24 +000091 // --------------------------------------------------------------------
92
93 /**
94 * Exception Logger
95 *
Andrey Andreev3e9d2b82012-10-27 14:28:51 +030096 * Logs PHP generated error messages
Derek Allard2067d1a2008-11-13 22:59:24 +000097 *
Andrey Andreev3e9d2b82012-10-27 14:28:51 +030098 * @param int $severity Log level
99 * @param string $message Error message
100 * @param string $filepath File path
101 * @param int $line Line number
Andrey Andreev7ac33d72012-01-07 19:39:39 +0200102 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 */
Andrey Andreev7ac33d72012-01-07 19:39:39 +0200104 public function log_exception($severity, $message, $filepath, $line)
Barry Mienydd671972010-10-04 16:33:58 +0200105 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300106 $severity = isset($this->levels[$severity]) ? $this->levels[$severity] : $severity;
Andrey Andreev29947ee2013-11-12 18:36:46 +0200107 log_message('error', 'Severity: '.$severity.' --> '.$message.' '.$filepath.' '.$line);
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 }
109
110 // --------------------------------------------------------------------
111
112 /**
Andrey Andreev3e9d2b82012-10-27 14:28:51 +0300113 * 404 Error Handler
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 *
Andrey Andreev3e9d2b82012-10-27 14:28:51 +0300115 * @uses CI_Exceptions::show_error()
116 *
117 * @param string $page Page URI
118 * @param bool $log_error Whether to log the error
119 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 */
Andrey Andreev7ac33d72012-01-07 19:39:39 +0200121 public function show_404($page = '', $log_error = TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200122 {
Andrey Andreev29947ee2013-11-12 18:36:46 +0200123 if (is_cli())
124 {
125 $heading = 'Not Found';
126 $message = 'The controller/method pair you requested was not found.';
127 }
128 else
129 {
130 $heading = '404 Page Not Found';
131 $message = 'The page you requested was not found.';
132 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000133
Derek Allard2ddc9492010-08-05 10:08:33 -0400134 // By default we log this, but allow a dev to skip it
135 if ($log_error)
136 {
Andrey Andreev29947ee2013-11-12 18:36:46 +0200137 log_message('error', $heading.': '.$page);
Derek Allard2ddc9492010-08-05 10:08:33 -0400138 }
139
Derek Jonesf6b442b2009-07-11 18:23:30 +0000140 echo $this->show_error($heading, $message, 'error_404', 404);
Andrey Andreev7cf682a2014-03-13 14:55:45 +0200141 exit(4); // EXIT_UNKNOWN_FILE
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 }
Barry Mienydd671972010-10-04 16:33:58 +0200143
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 // --------------------------------------------------------------------
145
146 /**
147 * General Error Page
148 *
Andrey Andreev3e9d2b82012-10-27 14:28:51 +0300149 * Takes an error message as input (either as a string or an array)
150 * and displays it using the specified template.
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 *
Andrey Andreev3e9d2b82012-10-27 14:28:51 +0300152 * @param string $heading Page heading
153 * @param string|string[] $message Error message
154 * @param string $template Template name
Andrey Andreev5232ba02012-10-27 15:25:05 +0300155 * @param int $status_code (default: 500)
Andrey Andreev3e9d2b82012-10-27 14:28:51 +0300156 *
157 * @return string Error page output
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 */
Andrey Andreev7ac33d72012-01-07 19:39:39 +0200159 public function show_error($heading, $message, $template = 'error_general', $status_code = 500)
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 {
Andrey Andreev29947ee2013-11-12 18:36:46 +0200161 if (is_cli())
162 {
163 $message = "\t".(is_array($message) ? implode("\n\t", $message) : $message);
164 $template = 'cli'.DIRECTORY_SEPARATOR.$template;
165 }
166 else
167 {
168 set_status_header($status_code);
169 $message = '<p>'.(is_array($message) ? implode('</p><p>', $message) : $message).'</p>';
170 $template = 'html'.DIRECTORY_SEPARATOR.$template;
171 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000172
173 if (ob_get_level() > $this->ob_level + 1)
174 {
Barry Mienydd671972010-10-04 16:33:58 +0200175 ob_end_flush();
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 }
177 ob_start();
vlakoffcdf3dfa2014-04-12 07:33:42 +0200178 include($this->_templates_path.$template.'.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 $buffer = ob_get_contents();
180 ob_end_clean();
181 return $buffer;
182 }
183
184 // --------------------------------------------------------------------
185
186 /**
187 * Native PHP error handler
188 *
Andrey Andreev3e9d2b82012-10-27 14:28:51 +0300189 * @param int $severity Error level
190 * @param string $message Error message
191 * @param string $filepath File path
192 * @param int $line Line number
193 * @return string Error page output
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 */
Andrey Andreevc6a68e02012-03-26 14:30:10 +0300195 public function show_php_error($severity, $message, $filepath, $line)
Barry Mienydd671972010-10-04 16:33:58 +0200196 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300197 $severity = isset($this->levels[$severity]) ? $this->levels[$severity] : $severity;
Barry Mienydd671972010-10-04 16:33:58 +0200198
Andrey Andreev29947ee2013-11-12 18:36:46 +0200199 // For safety reasons we don't show the full file path in non-CLI requests
200 if ( ! is_cli())
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 {
Andrey Andreev29947ee2013-11-12 18:36:46 +0200202 $filepath = str_replace('\\', '/', $filepath);
203 if (FALSE !== strpos($filepath, '/'))
204 {
205 $x = explode('/', $filepath);
206 $filepath = $x[count($x)-2].'/'.end($x);
207 }
208
209 $template = 'html'.DIRECTORY_SEPARATOR.'error_php';
210 }
211 else
212 {
213 $template = 'cli'.DIRECTORY_SEPARATOR.'error_php';
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 }
Barry Mienydd671972010-10-04 16:33:58 +0200215
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 if (ob_get_level() > $this->ob_level + 1)
217 {
Barry Mienydd671972010-10-04 16:33:58 +0200218 ob_end_flush();
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 }
220 ob_start();
vlakoffcdf3dfa2014-04-12 07:33:42 +0200221 include($this->_templates_path.$template.'.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 $buffer = ob_get_contents();
223 ob_end_clean();
224 echo $buffer;
225 }
226
Derek Allard2067d1a2008-11-13 22:59:24 +0000227}
Derek Allard2067d1a2008-11-13 22:59:24 +0000228
229/* End of file Exceptions.php */
Andrey Andreevc6a68e02012-03-26 14:30:10 +0300230/* Location: ./system/core/Exceptions.php */