1455d7ec |
1 | <?php |
2 | /** |
3 | * error.class.php |
4 | * |
5 | * Copyright (c) 2005 The SquirrelMail Project Team |
6 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
7 | * |
8 | * This contains the custom error handler for SquirrelMail. |
9 | * |
10 | * @version $Id$ |
11 | * @package squirrelmail |
12 | */ |
13 | |
14 | define('SQM_NOTICE',0); |
15 | define('SQM_WARNING',1); |
16 | define('SQM_ERROR',2); |
1dbd1527 |
17 | define('SQM_STRICT',3); |
18 | // php5 E_STRICT constant (compatibility with php4) |
19 | if (! defined('E_STRICT')) define('E_STRICT',2048); |
20 | // Set docref_root (fixes URLs that link to php manual) |
21 | if (ini_get('docref_root')=='') ini_set('docref_root','http://www.php.net/'); |
1455d7ec |
22 | |
23 | /** |
24 | * Error Handler class |
25 | * |
26 | * This class contains a custom error handler in order to display |
27 | * user notices/warnings/errors and php notices and warnings in a template |
28 | * |
29 | * @author Marc Groot Koerkamp |
30 | * @package squirrelmail |
31 | */ |
32 | class ErrorHandler { |
33 | |
34 | /** |
35 | * Constructor |
36 | * @param object $oTemplate Template object |
37 | * @param string $sTemplateFile Template containing the error template |
38 | * @since 1.5.1 |
39 | */ |
40 | function ErrorHandler(&$oTemplate, $sTemplateFile) { |
41 | $this->TemplateName = $sTemplateFile; |
42 | $this->Template =& $oTemplate; |
43 | $this->aErrors = array(); |
44 | } |
45 | |
46 | /** |
47 | * Sets the error template |
48 | * @since 1.5.1 |
49 | */ |
50 | function SetTemplateFile($sTemplateFile) { |
51 | $this->TemplateFile = $sTemplateFile; |
52 | } |
53 | |
54 | /** |
55 | * Custom Error handler (set with set_error_handler() ) |
56 | * @private |
57 | * @since 1.5.1 |
58 | */ |
59 | function SquirrelMailErrorhandler($iErrNo, $sErrStr, $sErrFile, $iErrLine, $aContext) { |
60 | $aError = array( |
61 | 'type' => SQM_NOTICE,// Error type, notice, warning or fatal error; |
62 | 'category' => NULL, // SquirrelMail error category; |
63 | 'message' => NULL, // Error display message; |
64 | 'extra' => NULL, // Key value based array with extra error info; |
65 | 'link' => NULL, // Link to help location; |
66 | 'tip' => NULL // User tip. |
67 | ); |
68 | $iType = NULL; |
69 | $aErrorCategory = array(); |
1dbd1527 |
70 | |
6b00bba4 |
71 | /** |
72 | * Get current error reporting level. |
73 | * |
74 | * PHP 4.1.2 does not return current error reporting level in ini_get (php 5.1b3 and |
75 | * 4.3.10 does). Retrieve current error reporting level while setting error reporting |
76 | * to ini value and reset it to retrieved value. |
77 | */ |
78 | $iCurErrLevel = error_reporting(ini_get('error_reporting')); |
79 | error_reporting($iCurErrLevel); |
80 | |
1dbd1527 |
81 | /** |
82 | * Check error_reporting value before logging error. |
83 | * Don't log errors that are disabled by @ (error_reporting = 0). Some SquirrelMail scripts |
84 | * (sq_mb_list_encodings(), ldap function calls in functions/abook_ldap_server.php) |
85 | * handle errors themselves and @ is used to disable generic php error messages. |
1455d7ec |
86 | */ |
6b00bba4 |
87 | if ((bool) $iCurErrLevel) { |
1dbd1527 |
88 | /* |
89 | * The following errors cannot be handled by a user defined error handler: |
90 | * E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING |
91 | */ |
92 | switch ($iErrNo) { |
93 | case E_STRICT: |
94 | $iType = (is_null($iType)) ? SQM_STRICT : $iType; |
1455d7ec |
95 | case E_NOTICE: |
96 | $iType = (is_null($iType)) ? SQM_NOTICE : $iType; |
97 | case E_WARNING: |
98 | $iType = (is_null($iType)) ? SQM_WARNING : $iType; |
99 | $aErrorCategory[] = 'PHP'; |
100 | $aError['message'] = $sErrStr; |
101 | $aError['extra'] = array( |
1dbd1527 |
102 | 'FILE' => $sErrFile, |
103 | 'LINE' => $iErrLine) ;; |
1455d7ec |
104 | // what todo with $aContext? |
105 | break; |
106 | case E_USER_ERROR: |
107 | $iType = (is_null($iType)) ? SQM_ERROR : $iType; |
108 | case E_USER_NOTICE: |
109 | $iType = (is_null($iType)) ? SQM_NOTICE : $iType; |
110 | case E_USER_WARNING: |
111 | $iType = (is_null($iType)) ? SQM_WARNING : $iType; |
112 | if ($sErrFile == __FILE__) { // Error is triggered in this file and probably by sqm_trigger_error |
113 | $aErrorTemp = @unserialize($sErrStr); |
114 | if (!is_array($aErrorTemp)) { |
115 | $aError['message'] = $sErrStr; |
116 | $aErrorCategory[] = 'UNDEFINED'; |
117 | } else { |
118 | $aError = array_merge($aError,$aErrorTemp); |
119 | // special error handling below |
120 | if ($aError['category'] & SQM_ERROR_IMAP) { |
1dbd1527 |
121 | $aErrorCategory[] = 'IMAP'; |
122 | // imap related error handling inside |
1455d7ec |
123 | } |
124 | if ($aError['category'] & SQM_ERROR_FS) { |
1dbd1527 |
125 | $aErrorCategory[] = 'FILESYSTEM'; |
126 | // filesystem related error handling inside |
1455d7ec |
127 | } |
128 | if ($aError['category'] & SQM_ERROR_SMTP) { |
1dbd1527 |
129 | $aErrorCategory[] = 'SMTP'; |
130 | // smtp related error handling inside |
1455d7ec |
131 | } |
132 | if ($aError['category'] & SQM_ERROR_LDAP) { |
1dbd1527 |
133 | $aErrorCategory[] = 'LDAP'; |
134 | // ldap related error handling inside |
1455d7ec |
135 | } |
136 | if ($aError['category'] & SQM_ERROR_DB) { |
1dbd1527 |
137 | $aErrorCategory[] = 'DATABASE'; |
138 | // db related error handling inside |
1455d7ec |
139 | } |
140 | if ($aError['category'] & SQM_ERROR_PLUGIN) { |
1dbd1527 |
141 | $aErrorCategory[] = 'PLUGIN'; |
142 | do_hook_function('error_handler_plugin',$aError); |
143 | // plugin related error handling inside |
1455d7ec |
144 | } |
145 | //if ($aError['category'] & SQM_ERROR_X) { |
146 | // $aErrorCategory[] = 'X'; |
147 | // place holder for a new category |
148 | //} |
149 | } |
150 | unset($aErrorTemp); |
151 | } else { |
152 | $aError['message'] = $sErrStr; |
153 | $aErrorCategory[] = 'SQM_NOTICE'; |
154 | } |
155 | break; |
156 | default: break; |
1dbd1527 |
157 | } |
158 | |
159 | $aErrorTpl = array( |
160 | 'type' => $iType, |
161 | 'category' => $aErrorCategory, |
162 | 'message' => $aError['message'], |
163 | 'link' => $aError['link'], |
164 | 'tip' => $aError['tip'], |
165 | 'extra' => $aError['extra']); |
166 | // Add the notice/warning/error to the existing list of notices/warnings |
167 | $this->aErrors[] = $aErrorTpl; |
168 | $this->Template->assign('aErrors',$this->aErrors); |
1455d7ec |
169 | } |
1dbd1527 |
170 | |
1455d7ec |
171 | // Show the error immediate in case of fatal errors |
172 | if ($iType == SQM_ERROR) { |
173 | $this->DisplayErrors(); |
174 | exit(_("Terminating SquirrelMail due to a fatal error")); |
175 | } |
176 | } |
177 | |
178 | /** |
179 | * Display the error array in the error template |
180 | * @return void |
181 | * @since 1.5.1 |
182 | */ |
183 | function DisplayErrors() { |
184 | if (count($this->aErrors)) { |
185 | $this->Template->display($this->TemplateName); |
186 | } |
187 | } |
188 | } |
189 | |
190 | /** |
191 | * Custom Error handler for PHP version < 4.3.0 (set with set_error_handler() ) |
192 | * @author Marc Groot Koerkamp |
193 | * @since 1.5.1 |
194 | */ |
195 | function SquirrelMailErrorhandler($iErrNo, $sErrStr, $sErrFile, $iErrLine, $aContext) { |
196 | global $oTemplate; |
197 | static $oErrorHandler; |
198 | if (!isset($oErrorHandler)) { |
199 | $oErrorHandler = new ErrorHandler($oTemplate,'error_message.tpl'); |
200 | } |
201 | $oErrorHandler->SquirrelMailErrorhandler($iErrNo, $sErrStr, $sErrFile, $iErrLine, $aContext); |
202 | } |
203 | |
204 | /** |
205 | * Triggers an imap error. Utility function for sqm_trigger_error() |
206 | * @param integer $iErrNo error number defined in errors.php |
207 | * @param string $sRequest imap request string |
208 | * @param string $sResponse tagged imap response |
209 | * @param string $sMessage tagged imap response message |
210 | * @param array $aExtra optional associative array with extra error info |
211 | * @return void |
212 | * @author Marc Groot Koerkamp |
213 | * @since 1.5.1 |
214 | */ |
215 | function sqm_trigger_imap_error($iErrNo,$sRequest,$sResponse, $sMessage, $aExtra=array()) { |
216 | $aError = array( |
217 | 'REQUEST' => $sRequest, |
218 | 'RESPONSE' => $sResponse, |
219 | 'MESSAGE' => $sMessage); |
220 | $aError = array_merge($aExtra,$aError); |
221 | sqm_trigger_error($iErrNo,$aError); |
222 | } |
223 | |
224 | /** |
225 | * Trigger an error. |
226 | * @param integer $iErrNo error number defined in errors.php |
227 | * @param array $aExtra optional associative array with extra error info |
228 | * @return void |
229 | * @author Marc Groot Koerkamp |
230 | * @since 1.5.1 |
231 | */ |
232 | function sqm_trigger_error($iErrNo,$aExtra=array()) { |
233 | // Include the error definition file. |
234 | include_once(SM_PATH.'include/errors.php'); |
235 | $iPhpErr = E_USER_NOTICE; |
236 | if (is_array($aErrors) && isset($aErrors[$iErrNo]['level'])) { |
237 | if (is_array($aExtra) && count($aExtra)) { |
238 | $aErrors[$iErrNo]['extra'] = $aExtra; |
239 | } |
240 | // because trigger_error can only handle a string argument for the error description |
241 | // we serialize the result. |
242 | $sErrString = serialize($aErrors[$iErrNo]); |
243 | $iPhpErr = $aErrors[$iErrNo]['level']; |
244 | } else { |
245 | $sErrString = "Error number <$iErrNo> does not exist, fix the code or update the errors.php file"; |
246 | $iPhpErr = E_USER_ERROR; |
247 | } |
248 | trigger_error($sErrString, $iPhpErr); |
249 | } |
250 | ?> |