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