blob: 839093911d774faeb078a2bb2c23bb45022a1206 [file] [log] [blame]
adminb0dd10f2006-08-25 17:25:49 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * Code Igniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
8 * @author Rick Ellis
9 * @copyright Copyright (c) 2006, pMachine, Inc.
admine334c472006-10-21 19:44:22 +000010 * @license http://www.codeignitor.com/user_guide/license.html
adminb0dd10f2006-08-25 17:25:49 +000011 * @link http://www.codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
admine334c472006-10-21 19:44:22 +000015
adminb0dd10f2006-08-25 17:25:49 +000016// ------------------------------------------------------------------------
17
18/**
19 * Exceptions Class
admine334c472006-10-21 19:44:22 +000020 *
adminb0dd10f2006-08-25 17:25:49 +000021 * @package CodeIgniter
22 * @subpackage Libraries
23 * @category Exceptions
24 * @author Rick Ellis
25 * @link http://www.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;
Rick Ellis325197e2006-11-20 17:29:05 +000033 var $ob_level;
adminb0dd10f2006-08-25 17:25:49 +000034
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 /**
admine334c472006-10-21 19:44:22 +000052 * Constructor
adminb0dd10f2006-08-25 17:25:49 +000053 *
54 */
55 function CI_Exceptions()
56 {
Rick Ellis325197e2006-11-20 17:29:05 +000057 $this->ob_level = ob_get_level();
adminbc042dd2006-09-21 02:46:59 +000058 // Note: Do not log messages from this constructor.
adminb0dd10f2006-08-25 17:25:49 +000059 }
adminb0dd10f2006-08-25 17:25:49 +000060
61 // --------------------------------------------------------------------
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)
76 {
77 $severity = ( ! isset($this->levels[$severity])) ? $severity : $this->levels[$severity];
78
79 log_message('error', 'Severity: '.$severity.' '.$severity.' --> '.$message. ' '.$filepath.' '.$line, TRUE);
80 }
admine334c472006-10-21 19:44:22 +000081
adminb0dd10f2006-08-25 17:25:49 +000082 // --------------------------------------------------------------------
83
84 /**
85 * 404 Page Not Found Handler
86 *
87 * @access private
88 * @param string
89 * @return string
90 */
91 function show_404($page = '')
92 {
93 $heading = "404 Page Not Found";
admine334c472006-10-21 19:44:22 +000094 $message = "The page you requested was not found.";
adminb0dd10f2006-08-25 17:25:49 +000095
96 log_message('error', '404 Page Not Found --> '.$page);
97 echo $this->show_error($heading, $message, 'error_404');
98 exit;
99 }
adminb0dd10f2006-08-25 17:25:49 +0000100
101 // --------------------------------------------------------------------
102
103 /**
104 * General Error Page
105 *
106 * This function takes an error message as input
adminbd6bee72006-10-21 19:39:00 +0000107 * (either as a string or an array) and displays
adminb0dd10f2006-08-25 17:25:49 +0000108 * it using the specified template.
109 *
110 * @access private
111 * @param string the heading
112 * @param string the message
113 * @param string the template name
114 * @return string
115 */
116 function show_error($heading, $message, $template = 'error_general')
117 {
118 $message = '<p>'.implode('</p><p>', ( ! is_array($message)) ? array($message) : $message).'</p>';
admine0cd6092006-10-20 01:00:31 +0000119
Rick Ellis325197e2006-11-20 17:29:05 +0000120 if (ob_get_level() > $this->ob_level + 1)
admine0cd6092006-10-20 01:00:31 +0000121 {
122 ob_end_flush();
123 }
adminb0dd10f2006-08-25 17:25:49 +0000124 ob_start();
adminf3a62042006-10-29 20:24:11 +0000125 include(APPPATH.'errors/'.$template.EXT);
adminb0dd10f2006-08-25 17:25:49 +0000126 $buffer = ob_get_contents();
127 ob_end_clean();
128 return $buffer;
129 }
admine334c472006-10-21 19:44:22 +0000130
adminb0dd10f2006-08-25 17:25:49 +0000131 // --------------------------------------------------------------------
132
133 /**
134 * Native PHP error handler
135 *
136 * @access private
137 * @param string the error severity
138 * @param string the error string
139 * @param string the error filepath
140 * @param string the error line number
141 * @return string
142 */
143 function show_php_error($severity, $message, $filepath, $line)
144 {
145 $severity = ( ! isset($this->levels[$severity])) ? $severity : $this->levels[$severity];
146
147 $filepath = str_replace("\\", "/", $filepath);
148
149 // For safety reasons we do not show the full file path
150 if (FALSE !== strpos($filepath, '/'))
151 {
152 $x = explode('/', $filepath);
153 $filepath = $x[count($x)-2].'/'.end($x);
154 }
admine0cd6092006-10-20 01:00:31 +0000155
Rick Ellis325197e2006-11-20 17:29:05 +0000156 if (ob_get_level() > $this->ob_level + 1)
admine0cd6092006-10-20 01:00:31 +0000157 {
158 ob_end_flush();
159 }
adminb0dd10f2006-08-25 17:25:49 +0000160 ob_start();
adminf3a62042006-10-29 20:24:11 +0000161 include(APPPATH.'errors/error_php'.EXT);
adminb0dd10f2006-08-25 17:25:49 +0000162 $buffer = ob_get_contents();
163 ob_end_clean();
164 echo $buffer;
165 }
adminb0dd10f2006-08-25 17:25:49 +0000166
adminbd6bee72006-10-21 19:39:00 +0000167
adminb0dd10f2006-08-25 17:25:49 +0000168}
adminbd6bee72006-10-21 19:39:00 +0000169// END Exceptions Class
adminb0dd10f2006-08-25 17:25:49 +0000170?>