blob: ead8d814e5c2dcac1f46957885d2666b5d994410 [file] [log] [blame]
Derek Jones37f4b9c2011-07-01 17:56:50 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * 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 Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @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 Allard2067d1a2008-11-13 22:59:24 +000023 * @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 Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000037 * @link http://codeigniter.com/user_guide/libraries/exceptions.html
38 */
39class CI_Exceptions {
40 var $action;
41 var $severity;
42 var $message;
43 var $filename;
44 var $line;
David Behler209b2cf2011-08-14 23:00:43 +020045
46 /**
47 * Nesting level of the output buffering mechanism
David Behler209b2cf2011-08-14 23:00:43 +020048 *
49 * @var int
50 * @access public
51 */
Derek Allard2067d1a2008-11-13 22:59:24 +000052 var $ob_level;
53
David Behler209b2cf2011-08-14 23:00:43 +020054 /**
55 * List if available error levels
56 *
57 * @var array
58 * @access public
59 */
Derek Allard2067d1a2008-11-13 22:59:24 +000060 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 Mienydd671972010-10-04 16:33:58 +020078 */
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();
Derek Jones37f4b9c2011-07-01 17:56:50 -050082 // Note: Do not log messages from this constructor.
Derek Allard2067d1a2008-11-13 22:59:24 +000083 }
Barry Mienydd671972010-10-04 16:33:58 +020084
Derek Allard2067d1a2008-11-13 22:59:24 +000085 // --------------------------------------------------------------------
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 Mienydd671972010-10-04 16:33:58 +0200100 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000101 $severity = ( ! isset($this->levels[$severity])) ? $severity : $this->levels[$severity];
Barry Mienydd671972010-10-04 16:33:58 +0200102
Derek Jones37f4b9c2011-07-01 17:56:50 -0500103 log_message('error', 'Severity: '.$severity.' --> '.$message. ' '.$filepath.' '.$line, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 }
105
106 // --------------------------------------------------------------------
107
108 /**
109 * 404 Page Not Found Handler
110 *
111 * @access private
David Behlercda768a2011-08-14 23:52:48 +0200112 * @param string the page
113 * @param bool log error yes/no
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 * @return string
115 */
Derek Allard2ddc9492010-08-05 10:08:33 -0400116 function show_404($page = '', $log_error = TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200117 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 $heading = "404 Page Not Found";
119 $message = "The page you requested was not found.";
120
Derek Allard2ddc9492010-08-05 10:08:33 -0400121 // 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 Jonesf6b442b2009-07-11 18:23:30 +0000127 echo $this->show_error($heading, $message, 'error_404', 404);
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 exit;
129 }
Barry Mienydd671972010-10-04 16:33:58 +0200130
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 // --------------------------------------------------------------------
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 Behlercda768a2011-08-14 23:52:48 +0200144 * @param int the status code
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 * @return string
146 */
Derek Jones817163a2009-07-11 17:05:58 +0000147 function show_error($heading, $message, $template = 'error_general', $status_code = 500)
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 {
Derek Jones817163a2009-07-11 17:05:58 +0000149 set_status_header($status_code);
Barry Mienydd671972010-10-04 16:33:58 +0200150
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 $message = '<p>'.implode('</p><p>', ( ! is_array($message)) ? array($message) : $message).'</p>';
152
153 if (ob_get_level() > $this->ob_level + 1)
154 {
Barry Mienydd671972010-10-04 16:33:58 +0200155 ob_end_flush();
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 }
157 ob_start();
Greg Aker3a746652011-04-19 10:59:47 -0500158 include(APPPATH.'errors/'.$template.'.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 $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 Mienydd671972010-10-04 16:33:58 +0200177 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 $severity = ( ! isset($this->levels[$severity])) ? $severity : $this->levels[$severity];
Barry Mienydd671972010-10-04 16:33:58 +0200179
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 $filepath = str_replace("\\", "/", $filepath);
Barry Mienydd671972010-10-04 16:33:58 +0200181
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 // 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 Mienydd671972010-10-04 16:33:58 +0200188
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 if (ob_get_level() > $this->ob_level + 1)
190 {
Barry Mienydd671972010-10-04 16:33:58 +0200191 ob_end_flush();
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 }
193 ob_start();
Greg Aker3a746652011-04-19 10:59:47 -0500194 include(APPPATH.'errors/error_php.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 $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 Jonesc68dfbf2010-03-02 12:59:23 -0600205/* Location: ./system/core/Exceptions.php */