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