blob: 108861de74bb4f99f4cb31765317019fd161de3c [file] [log] [blame]
Derek Allard2067d1a2008-11-13 22:59:24 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
Derek Jones7f3719f2010-01-05 13:35:37 +00009 * @copyright Copyright (c) 2008 - 2010, EllisLab, Inc.
Derek Allard2067d1a2008-11-13 22:59:24 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * Exceptions Class
20 *
21 * @package CodeIgniter
22 * @subpackage Libraries
23 * @category Exceptions
24 * @author ExpressionEngine Dev Team
25 * @link http://codeigniter.com/user_guide/libraries/exceptions.html
26 */
27class CI_Exceptions {
28 var $action;
29 var $severity;
30 var $message;
31 var $filename;
32 var $line;
33 var $ob_level;
34
35 var $levels = array(
36 E_ERROR => 'Error',
37 E_WARNING => 'Warning',
38 E_PARSE => 'Parsing Error',
39 E_NOTICE => 'Notice',
40 E_CORE_ERROR => 'Core Error',
41 E_CORE_WARNING => 'Core Warning',
42 E_COMPILE_ERROR => 'Compile Error',
43 E_COMPILE_WARNING => 'Compile Warning',
44 E_USER_ERROR => 'User Error',
45 E_USER_WARNING => 'User Warning',
46 E_USER_NOTICE => 'User Notice',
47 E_STRICT => 'Runtime Notice'
48 );
49
50
51 /**
52 * Constructor
53 *
Barry Mienydd671972010-10-04 16:33:58 +020054 */
Derek Allard2067d1a2008-11-13 22:59:24 +000055 function CI_Exceptions()
56 {
57 $this->ob_level = ob_get_level();
58 // Note: Do not log messages from this constructor.
59 }
Barry Mienydd671972010-10-04 16:33:58 +020060
Derek Allard2067d1a2008-11-13 22:59:24 +000061 // --------------------------------------------------------------------
62
63 /**
64 * Exception Logger
65 *
66 * This function logs PHP generated error messages
67 *
68 * @access private
69 * @param string the error severity
70 * @param string the error string
71 * @param string the error filepath
72 * @param string the error line number
73 * @return string
74 */
75 function log_exception($severity, $message, $filepath, $line)
Barry Mienydd671972010-10-04 16:33:58 +020076 {
Derek Allard2067d1a2008-11-13 22:59:24 +000077 $severity = ( ! isset($this->levels[$severity])) ? $severity : $this->levels[$severity];
Barry Mienydd671972010-10-04 16:33:58 +020078
Derek Allard2067d1a2008-11-13 22:59:24 +000079 log_message('error', 'Severity: '.$severity.' --> '.$message. ' '.$filepath.' '.$line, TRUE);
80 }
81
82 // --------------------------------------------------------------------
83
84 /**
85 * 404 Page Not Found Handler
86 *
87 * @access private
88 * @param string
89 * @return string
90 */
Derek Allard2ddc9492010-08-05 10:08:33 -040091 function show_404($page = '', $log_error = TRUE)
Barry Mienydd671972010-10-04 16:33:58 +020092 {
Derek Allard2067d1a2008-11-13 22:59:24 +000093 $heading = "404 Page Not Found";
94 $message = "The page you requested was not found.";
95
Derek Allard2ddc9492010-08-05 10:08:33 -040096 // By default we log this, but allow a dev to skip it
97 if ($log_error)
98 {
99 log_message('error', '404 Page Not Found --> '.$page);
100 }
101
Derek Jonesf6b442b2009-07-11 18:23:30 +0000102 echo $this->show_error($heading, $message, 'error_404', 404);
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 exit;
104 }
Barry Mienydd671972010-10-04 16:33:58 +0200105
Derek Allard2067d1a2008-11-13 22:59:24 +0000106 // --------------------------------------------------------------------
107
108 /**
109 * General Error Page
110 *
111 * This function takes an error message as input
112 * (either as a string or an array) and displays
113 * it using the specified template.
114 *
115 * @access private
116 * @param string the heading
117 * @param string the message
118 * @param string the template name
119 * @return string
120 */
Derek Jones817163a2009-07-11 17:05:58 +0000121 function show_error($heading, $message, $template = 'error_general', $status_code = 500)
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 {
Derek Jones817163a2009-07-11 17:05:58 +0000123 set_status_header($status_code);
Barry Mienydd671972010-10-04 16:33:58 +0200124
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 $message = '<p>'.implode('</p><p>', ( ! is_array($message)) ? array($message) : $message).'</p>';
126
127 if (ob_get_level() > $this->ob_level + 1)
128 {
Barry Mienydd671972010-10-04 16:33:58 +0200129 ob_end_flush();
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 }
131 ob_start();
132 include(APPPATH.'errors/'.$template.EXT);
133 $buffer = ob_get_contents();
134 ob_end_clean();
135 return $buffer;
136 }
137
138 // --------------------------------------------------------------------
139
140 /**
141 * Native PHP error handler
142 *
143 * @access private
144 * @param string the error severity
145 * @param string the error string
146 * @param string the error filepath
147 * @param string the error line number
148 * @return string
149 */
150 function show_php_error($severity, $message, $filepath, $line)
Barry Mienydd671972010-10-04 16:33:58 +0200151 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 $severity = ( ! isset($this->levels[$severity])) ? $severity : $this->levels[$severity];
Barry Mienydd671972010-10-04 16:33:58 +0200153
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 $filepath = str_replace("\\", "/", $filepath);
Barry Mienydd671972010-10-04 16:33:58 +0200155
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 // For safety reasons we do not show the full file path
157 if (FALSE !== strpos($filepath, '/'))
158 {
159 $x = explode('/', $filepath);
160 $filepath = $x[count($x)-2].'/'.end($x);
161 }
Barry Mienydd671972010-10-04 16:33:58 +0200162
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 if (ob_get_level() > $this->ob_level + 1)
164 {
Barry Mienydd671972010-10-04 16:33:58 +0200165 ob_end_flush();
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 }
167 ob_start();
168 include(APPPATH.'errors/error_php'.EXT);
169 $buffer = ob_get_contents();
170 ob_end_clean();
171 echo $buffer;
172 }
173
174
175}
176// END Exceptions Class
177
178/* End of file Exceptions.php */
Derek Jonesc68dfbf2010-03-02 12:59:23 -0600179/* Location: ./system/core/Exceptions.php */