blob: 8c32085fe49d4f8de7eda22b0f3fada249e1be12 [file] [log] [blame]
Andrey Andreev7ac33d72012-01-07 19:39:39 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
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
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, 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 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * Exceptions Class
30 *
31 * @package CodeIgniter
32 * @subpackage Libraries
33 * @category Exceptions
Derek Jonesf4a4bd82011-10-20 12:18:42 -050034 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000035 * @link http://codeigniter.com/user_guide/libraries/exceptions.html
36 */
37class CI_Exceptions {
Andrey Andreev7ac33d72012-01-07 19:39:39 +020038
David Behler209b2cf2011-08-14 23:00:43 +020039 /**
40 * Nesting level of the output buffering mechanism
David Behler209b2cf2011-08-14 23:00:43 +020041 *
Timothy Warren48a7fbb2012-04-23 11:58:16 -040042 * @var int
David Behler209b2cf2011-08-14 23:00:43 +020043 */
Andrey Andreev7ac33d72012-01-07 19:39:39 +020044 public $ob_level;
Derek Allard2067d1a2008-11-13 22:59:24 +000045
David Behler209b2cf2011-08-14 23:00:43 +020046 /**
47 * List if available error levels
48 *
Timothy Warren48a7fbb2012-04-23 11:58:16 -040049 * @var array
David Behler209b2cf2011-08-14 23:00:43 +020050 */
Andrey Andreev7ac33d72012-01-07 19:39:39 +020051 public $levels = array(
Timothy Warren40403d22012-04-19 16:38:50 -040052 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 Allard2067d1a2008-11-13 22:59:24 +000065
Timothy Warrenad475052012-04-19 13:21:06 -040066 /**
67 * Initialize execption class
Andrey Andreev92ebfb62012-05-17 12:49:24 +030068 *
69 * @return void
Timothy Warrenad475052012-04-19 13:21:06 -040070 */
Greg Akera9263282010-11-10 15:26:43 -060071 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000072 {
73 $this->ob_level = ob_get_level();
Andrey Andreev7ac33d72012-01-07 19:39:39 +020074 // Note: Do not log messages from this constructor.
Derek Allard2067d1a2008-11-13 22:59:24 +000075 }
Barry Mienydd671972010-10-04 16:33:58 +020076
Derek Allard2067d1a2008-11-13 22:59:24 +000077 // --------------------------------------------------------------------
78
79 /**
80 * Exception Logger
81 *
82 * This function logs PHP generated error messages
83 *
Derek Allard2067d1a2008-11-13 22:59:24 +000084 * @param string the error severity
85 * @param string the error string
86 * @param string the error filepath
87 * @param string the error line number
Andrey Andreev7ac33d72012-01-07 19:39:39 +020088 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +000089 */
Andrey Andreev7ac33d72012-01-07 19:39:39 +020090 public function log_exception($severity, $message, $filepath, $line)
Barry Mienydd671972010-10-04 16:33:58 +020091 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +030092 $severity = isset($this->levels[$severity]) ? $this->levels[$severity] : $severity;
Derek Jones37f4b9c2011-07-01 17:56:50 -050093 log_message('error', 'Severity: '.$severity.' --> '.$message. ' '.$filepath.' '.$line, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +000094 }
95
96 // --------------------------------------------------------------------
97
98 /**
99 * 404 Page Not Found Handler
100 *
David Behlercda768a2011-08-14 23:52:48 +0200101 * @param string the page
102 * @param bool log error yes/no
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 * @return string
104 */
Andrey Andreev7ac33d72012-01-07 19:39:39 +0200105 public function show_404($page = '', $log_error = TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200106 {
Andrey Andreev7ac33d72012-01-07 19:39:39 +0200107 $heading = '404 Page Not Found';
108 $message = 'The page you requested was not found.';
Derek Allard2067d1a2008-11-13 22:59:24 +0000109
Derek Allard2ddc9492010-08-05 10:08:33 -0400110 // By default we log this, but allow a dev to skip it
111 if ($log_error)
112 {
113 log_message('error', '404 Page Not Found --> '.$page);
114 }
115
Derek Jonesf6b442b2009-07-11 18:23:30 +0000116 echo $this->show_error($heading, $message, 'error_404', 404);
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 exit;
118 }
Barry Mienydd671972010-10-04 16:33:58 +0200119
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 // --------------------------------------------------------------------
121
122 /**
123 * General Error Page
124 *
125 * This function takes an error message as input
126 * (either as a string or an array) and displays
127 * it using the specified template.
128 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 * @param string the heading
130 * @param string the message
131 * @param string the template name
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300132 * @param int the status code
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 * @return string
134 */
Andrey Andreev7ac33d72012-01-07 19:39:39 +0200135 public function show_error($heading, $message, $template = 'error_general', $status_code = 500)
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 {
Derek Jones817163a2009-07-11 17:05:58 +0000137 set_status_header($status_code);
Barry Mienydd671972010-10-04 16:33:58 +0200138
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300139 $message = '<p>'.implode('</p><p>', is_array($message) ? $message : array($message)).'</p>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000140
141 if (ob_get_level() > $this->ob_level + 1)
142 {
Barry Mienydd671972010-10-04 16:33:58 +0200143 ob_end_flush();
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 }
145 ob_start();
Timothy Warren09069dd2012-05-14 15:21:33 -0400146 include(APPPATH.'views/errors/'.$template.'.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 $buffer = ob_get_contents();
148 ob_end_clean();
149 return $buffer;
150 }
151
152 // --------------------------------------------------------------------
153
154 /**
155 * Native PHP error handler
156 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 * @param string the error severity
158 * @param string the error string
159 * @param string the error filepath
160 * @param string the error line number
161 * @return string
162 */
Andrey Andreevc6a68e02012-03-26 14:30:10 +0300163 public function show_php_error($severity, $message, $filepath, $line)
Barry Mienydd671972010-10-04 16:33:58 +0200164 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300165 $severity = isset($this->levels[$severity]) ? $this->levels[$severity] : $severity;
Andrey Andreev7ac33d72012-01-07 19:39:39 +0200166 $filepath = str_replace('\\', '/', $filepath);
Barry Mienydd671972010-10-04 16:33:58 +0200167
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 // For safety reasons we do not show the full file path
169 if (FALSE !== strpos($filepath, '/'))
170 {
171 $x = explode('/', $filepath);
172 $filepath = $x[count($x)-2].'/'.end($x);
173 }
Barry Mienydd671972010-10-04 16:33:58 +0200174
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 if (ob_get_level() > $this->ob_level + 1)
176 {
Barry Mienydd671972010-10-04 16:33:58 +0200177 ob_end_flush();
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 }
179 ob_start();
Timothy Warren09069dd2012-05-14 15:21:33 -0400180 include(APPPATH.'views/errors/error_php.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 $buffer = ob_get_contents();
182 ob_end_clean();
183 echo $buffer;
184 }
185
Derek Allard2067d1a2008-11-13 22:59:24 +0000186}
Derek Allard2067d1a2008-11-13 22:59:24 +0000187
188/* End of file Exceptions.php */
Andrey Andreevc6a68e02012-03-26 14:30:10 +0300189/* Location: ./system/core/Exceptions.php */