| 68 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* CodeIgniter
|
|
|
4 |
*
|
|
|
5 |
* An open source application development framework for PHP
|
|
|
6 |
*
|
|
|
7 |
* This content is released under the MIT License (MIT)
|
|
|
8 |
*
|
| 2049 |
lars |
9 |
* Copyright (c) 2014 - 2017, British Columbia Institute of Technology
|
| 68 |
lars |
10 |
*
|
|
|
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:
|
|
|
17 |
*
|
|
|
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
|
|
|
31 |
* @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
|
| 2049 |
lars |
32 |
* @copyright Copyright (c) 2014 - 2017, British Columbia Institute of Technology (http://bcit.ca/)
|
| 68 |
lars |
33 |
* @license http://opensource.org/licenses/MIT MIT License
|
|
|
34 |
* @link https://codeigniter.com
|
|
|
35 |
* @since Version 1.0.0
|
|
|
36 |
* @filesource
|
|
|
37 |
*/
|
|
|
38 |
defined('BASEPATH') OR exit('No direct script access allowed');
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* Exceptions Class
|
|
|
42 |
*
|
|
|
43 |
* @package CodeIgniter
|
|
|
44 |
* @subpackage Libraries
|
|
|
45 |
* @category Exceptions
|
|
|
46 |
* @author EllisLab Dev Team
|
|
|
47 |
* @link https://codeigniter.com/user_guide/libraries/exceptions.html
|
|
|
48 |
*/
|
|
|
49 |
class CI_Exceptions {
|
|
|
50 |
|
|
|
51 |
/**
|
|
|
52 |
* Nesting level of the output buffering mechanism
|
|
|
53 |
*
|
|
|
54 |
* @var int
|
|
|
55 |
*/
|
|
|
56 |
public $ob_level;
|
|
|
57 |
|
|
|
58 |
/**
|
|
|
59 |
* List of available error levels
|
|
|
60 |
*
|
|
|
61 |
* @var array
|
|
|
62 |
*/
|
|
|
63 |
public $levels = array(
|
|
|
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 |
);
|
|
|
77 |
|
|
|
78 |
/**
|
|
|
79 |
* Class constructor
|
|
|
80 |
*
|
|
|
81 |
* @return void
|
|
|
82 |
*/
|
|
|
83 |
public function __construct()
|
|
|
84 |
{
|
|
|
85 |
$this->ob_level = ob_get_level();
|
|
|
86 |
// Note: Do not log messages from this constructor.
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
// --------------------------------------------------------------------
|
|
|
90 |
|
|
|
91 |
/**
|
|
|
92 |
* Exception Logger
|
|
|
93 |
*
|
|
|
94 |
* Logs PHP generated error messages
|
|
|
95 |
*
|
|
|
96 |
* @param int $severity Log level
|
|
|
97 |
* @param string $message Error message
|
|
|
98 |
* @param string $filepath File path
|
|
|
99 |
* @param int $line Line number
|
|
|
100 |
* @return void
|
|
|
101 |
*/
|
|
|
102 |
public function log_exception($severity, $message, $filepath, $line)
|
|
|
103 |
{
|
|
|
104 |
$severity = isset($this->levels[$severity]) ? $this->levels[$severity] : $severity;
|
|
|
105 |
log_message('error', 'Severity: '.$severity.' --> '.$message.' '.$filepath.' '.$line);
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
// --------------------------------------------------------------------
|
|
|
109 |
|
|
|
110 |
/**
|
|
|
111 |
* 404 Error Handler
|
|
|
112 |
*
|
|
|
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
|
|
|
118 |
*/
|
|
|
119 |
public function show_404($page = '', $log_error = TRUE)
|
|
|
120 |
{
|
|
|
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 |
}
|
|
|
131 |
|
|
|
132 |
// By default we log this, but allow a dev to skip it
|
|
|
133 |
if ($log_error)
|
|
|
134 |
{
|
|
|
135 |
log_message('error', $heading.': '.$page);
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
echo $this->show_error($heading, $message, 'error_404', 404);
|
|
|
139 |
exit(4); // EXIT_UNKNOWN_FILE
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
// --------------------------------------------------------------------
|
|
|
143 |
|
|
|
144 |
/**
|
|
|
145 |
* General Error Page
|
|
|
146 |
*
|
|
|
147 |
* Takes an error message as input (either as a string or an array)
|
|
|
148 |
* and displays it using the specified template.
|
|
|
149 |
*
|
|
|
150 |
* @param string $heading Page heading
|
|
|
151 |
* @param string|string[] $message Error message
|
|
|
152 |
* @param string $template Template name
|
|
|
153 |
* @param int $status_code (default: 500)
|
|
|
154 |
*
|
|
|
155 |
* @return string Error page output
|
|
|
156 |
*/
|
|
|
157 |
public function show_error($heading, $message, $template = 'error_general', $status_code = 500)
|
|
|
158 |
{
|
|
|
159 |
$templates_path = config_item('error_views_path');
|
|
|
160 |
if (empty($templates_path))
|
|
|
161 |
{
|
|
|
162 |
$templates_path = VIEWPATH.'errors'.DIRECTORY_SEPARATOR;
|
|
|
163 |
}
|
|
|
164 |
|
|
|
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 |
}
|
|
|
176 |
|
|
|
177 |
if (ob_get_level() > $this->ob_level + 1)
|
|
|
178 |
{
|
|
|
179 |
ob_end_flush();
|
|
|
180 |
}
|
|
|
181 |
ob_start();
|
|
|
182 |
include($templates_path.$template.'.php');
|
|
|
183 |
$buffer = ob_get_contents();
|
|
|
184 |
ob_end_clean();
|
|
|
185 |
return $buffer;
|
|
|
186 |
}
|
|
|
187 |
|
|
|
188 |
// --------------------------------------------------------------------
|
|
|
189 |
|
|
|
190 |
public function show_exception($exception)
|
|
|
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 |
{
|
|
|
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 |
|
|
|
227 |
/**
|
|
|
228 |
* Native PHP error handler
|
|
|
229 |
*
|
|
|
230 |
* @param int $severity Error level
|
|
|
231 |
* @param string $message Error message
|
|
|
232 |
* @param string $filepath File path
|
|
|
233 |
* @param int $line Line number
|
| 2107 |
lars |
234 |
* @return void
|
| 68 |
lars |
235 |
*/
|
|
|
236 |
public function show_php_error($severity, $message, $filepath, $line)
|
|
|
237 |
{
|
|
|
238 |
$templates_path = config_item('error_views_path');
|
|
|
239 |
if (empty($templates_path))
|
|
|
240 |
{
|
|
|
241 |
$templates_path = VIEWPATH.'errors'.DIRECTORY_SEPARATOR;
|
|
|
242 |
}
|
|
|
243 |
|
|
|
244 |
$severity = isset($this->levels[$severity]) ? $this->levels[$severity] : $severity;
|
|
|
245 |
|
|
|
246 |
// For safety reasons we don't show the full file path in non-CLI requests
|
|
|
247 |
if ( ! is_cli())
|
|
|
248 |
{
|
|
|
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';
|
|
|
261 |
}
|
|
|
262 |
|
|
|
263 |
if (ob_get_level() > $this->ob_level + 1)
|
|
|
264 |
{
|
|
|
265 |
ob_end_flush();
|
|
|
266 |
}
|
|
|
267 |
ob_start();
|
|
|
268 |
include($templates_path.$template.'.php');
|
|
|
269 |
$buffer = ob_get_contents();
|
|
|
270 |
ob_end_clean();
|
|
|
271 |
echo $buffer;
|
|
|
272 |
}
|
|
|
273 |
|
|
|
274 |
}
|