1455d7ec |
1 | <?php |
4b4abf93 |
2 | |
1455d7ec |
3 | /** |
4 | * error.class.php |
5 | * |
1455d7ec |
6 | * This contains the custom error handler for SquirrelMail. |
7 | * |
47ccfad4 |
8 | * @copyright © 2005-2006 The SquirrelMail Project Team |
4b4abf93 |
9 | * @license http://opensource.org/licenses/gpl-license.php GNU Public License |
1455d7ec |
10 | * @version $Id$ |
11 | * @package squirrelmail |
12 | */ |
13 | |
57311df7 |
14 | /** Used defines */ |
1455d7ec |
15 | define('SQM_NOTICE',0); |
16 | define('SQM_WARNING',1); |
17 | define('SQM_ERROR',2); |
1dbd1527 |
18 | define('SQM_STRICT',3); |
57311df7 |
19 | |
1dbd1527 |
20 | // php5 E_STRICT constant (compatibility with php4) |
21 | if (! defined('E_STRICT')) define('E_STRICT',2048); |
22 | // Set docref_root (fixes URLs that link to php manual) |
23 | if (ini_get('docref_root')=='') ini_set('docref_root','http://www.php.net/'); |
1455d7ec |
24 | |
25 | /** |
26 | * Error Handler class |
27 | * |
28 | * This class contains a custom error handler in order to display |
29 | * user notices/warnings/errors and php notices and warnings in a template |
30 | * |
31 | * @author Marc Groot Koerkamp |
32 | * @package squirrelmail |
33 | */ |
34 | class ErrorHandler { |
35 | |
36 | /** |
37 | * Constructor |
38 | * @param object $oTemplate Template object |
39 | * @param string $sTemplateFile Template containing the error template |
40 | * @since 1.5.1 |
41 | */ |
42 | function ErrorHandler(&$oTemplate, $sTemplateFile) { |
1d9716e4 |
43 | # echo 'init error handler...'; |
1455d7ec |
44 | $this->TemplateName = $sTemplateFile; |
45 | $this->Template =& $oTemplate; |
46 | $this->aErrors = array(); |
81de00c0 |
47 | $this->header_sent = false; |
1d9716e4 |
48 | $this->delayed_errors = false; |
49 | $this->Template->assign('delayed_errors', $this->delayed_errors); |
1455d7ec |
50 | } |
51 | |
52 | /** |
53 | * Sets the error template |
54 | * @since 1.5.1 |
55 | */ |
56 | function SetTemplateFile($sTemplateFile) { |
57 | $this->TemplateFile = $sTemplateFile; |
58 | } |
59 | |
81de00c0 |
60 | /** |
61 | * Sets if the page header is already sent |
62 | * @since 1.5.1 |
63 | */ |
64 | function HeaderSent() { |
65 | $this->header_sent = true; |
66 | } |
67 | |
1d9716e4 |
68 | /** |
69 | * Turn on/off delayed error handling |
70 | * @since 1.5.2 |
71 | */ |
72 | function setDelayedErrors ($val = true) { |
73 | $this->delayed_errors = $val===true; |
74 | $this->Template->assign('delayed_errors', $this->delayed_errors); |
75 | } |
76 | |
81de00c0 |
77 | /** |
85a1f578 |
78 | * Store errors generated in a previous script but couldn't be displayed |
79 | * due to a header redirect. This requires storing of aDelayedErrors in the session |
80 | * @param array $aDelayedErrors array with errors stored in the $this->aErrors format. |
81de00c0 |
81 | * @since 1.5.1 |
82 | */ |
83 | function AssignDelayedErrors(&$aDelayedErrors) { |
84 | $aErrors = array_merge($this->aErrors,$aDelayedErrors); |
85 | $this->aErrors = $aErrors; |
86 | $this->Template->assign('aErrors',$this->aErrors); |
87 | $aDelayedErrors = false; |
88 | } |
89 | |
90 | |
1455d7ec |
91 | /** |
92 | * Custom Error handler (set with set_error_handler() ) |
93 | * @private |
94 | * @since 1.5.1 |
95 | */ |
96 | function SquirrelMailErrorhandler($iErrNo, $sErrStr, $sErrFile, $iErrLine, $aContext) { |
97 | $aError = array( |
98 | 'type' => SQM_NOTICE,// Error type, notice, warning or fatal error; |
99 | 'category' => NULL, // SquirrelMail error category; |
100 | 'message' => NULL, // Error display message; |
101 | 'extra' => NULL, // Key value based array with extra error info; |
102 | 'link' => NULL, // Link to help location; |
103 | 'tip' => NULL // User tip. |
104 | ); |
105 | $iType = NULL; |
106 | $aErrorCategory = array(); |
1dbd1527 |
107 | |
6b00bba4 |
108 | /** |
109 | * Get current error reporting level. |
110 | * |
cbed20a4 |
111 | * PHP 4.1.2 does not return current error reporting level in ini_get (php 5.1b3 and |
112 | * 4.3.10 does). Retrieve current error reporting level while setting error reporting |
6b00bba4 |
113 | * to ini value and reset it to retrieved value. |
114 | */ |
115 | $iCurErrLevel = error_reporting(ini_get('error_reporting')); |
116 | error_reporting($iCurErrLevel); |
117 | |
1dbd1527 |
118 | /** |
119 | * Check error_reporting value before logging error. |
120 | * Don't log errors that are disabled by @ (error_reporting = 0). Some SquirrelMail scripts |
121 | * (sq_mb_list_encodings(), ldap function calls in functions/abook_ldap_server.php) |
122 | * handle errors themselves and @ is used to disable generic php error messages. |
1455d7ec |
123 | */ |
cbed20a4 |
124 | if ($iErrNo & $iCurErrLevel) { |
1dbd1527 |
125 | /* |
126 | * The following errors cannot be handled by a user defined error handler: |
127 | * E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING |
128 | */ |
129 | switch ($iErrNo) { |
130 | case E_STRICT: |
131 | $iType = (is_null($iType)) ? SQM_STRICT : $iType; |
1455d7ec |
132 | case E_NOTICE: |
133 | $iType = (is_null($iType)) ? SQM_NOTICE : $iType; |
134 | case E_WARNING: |
135 | $iType = (is_null($iType)) ? SQM_WARNING : $iType; |
136 | $aErrorCategory[] = 'PHP'; |
137 | $aError['message'] = $sErrStr; |
138 | $aError['extra'] = array( |
1dbd1527 |
139 | 'FILE' => $sErrFile, |
1d9716e4 |
140 | 'LINE' => $iErrLine) ; |
1455d7ec |
141 | // what todo with $aContext? |
142 | break; |
143 | case E_USER_ERROR: |
144 | $iType = (is_null($iType)) ? SQM_ERROR : $iType; |
145 | case E_USER_NOTICE: |
146 | $iType = (is_null($iType)) ? SQM_NOTICE : $iType; |
147 | case E_USER_WARNING: |
148 | $iType = (is_null($iType)) ? SQM_WARNING : $iType; |
149 | if ($sErrFile == __FILE__) { // Error is triggered in this file and probably by sqm_trigger_error |
150 | $aErrorTemp = @unserialize($sErrStr); |
151 | if (!is_array($aErrorTemp)) { |
152 | $aError['message'] = $sErrStr; |
153 | $aErrorCategory[] = 'UNDEFINED'; |
154 | } else { |
155 | $aError = array_merge($aError,$aErrorTemp); |
156 | // special error handling below |
157 | if ($aError['category'] & SQM_ERROR_IMAP) { |
1dbd1527 |
158 | $aErrorCategory[] = 'IMAP'; |
159 | // imap related error handling inside |
1455d7ec |
160 | } |
161 | if ($aError['category'] & SQM_ERROR_FS) { |
1dbd1527 |
162 | $aErrorCategory[] = 'FILESYSTEM'; |
163 | // filesystem related error handling inside |
1455d7ec |
164 | } |
165 | if ($aError['category'] & SQM_ERROR_SMTP) { |
1dbd1527 |
166 | $aErrorCategory[] = 'SMTP'; |
167 | // smtp related error handling inside |
1455d7ec |
168 | } |
169 | if ($aError['category'] & SQM_ERROR_LDAP) { |
1dbd1527 |
170 | $aErrorCategory[] = 'LDAP'; |
171 | // ldap related error handling inside |
1455d7ec |
172 | } |
173 | if ($aError['category'] & SQM_ERROR_DB) { |
1dbd1527 |
174 | $aErrorCategory[] = 'DATABASE'; |
175 | // db related error handling inside |
1455d7ec |
176 | } |
177 | if ($aError['category'] & SQM_ERROR_PLUGIN) { |
1dbd1527 |
178 | $aErrorCategory[] = 'PLUGIN'; |
179 | do_hook_function('error_handler_plugin',$aError); |
180 | // plugin related error handling inside |
1455d7ec |
181 | } |
182 | //if ($aError['category'] & SQM_ERROR_X) { |
183 | // $aErrorCategory[] = 'X'; |
184 | // place holder for a new category |
185 | //} |
186 | } |
187 | unset($aErrorTemp); |
188 | } else { |
189 | $aError['message'] = $sErrStr; |
190 | $aErrorCategory[] = 'SQM_NOTICE'; |
191 | } |
192 | break; |
193 | default: break; |
1dbd1527 |
194 | } |
195 | |
1d9716e4 |
196 | /** |
197 | * If delayed error handling is enabled, always record the location |
198 | * and tag the error is delayed to make debugging easier. |
199 | */ |
200 | if (isset($this->Template->values['delayed_errors']) && $this->Template->values['delayed_errors']) { |
201 | $aErrorCategory[] = 'Delayed'; |
202 | $aError['extra'] = array( |
203 | 'FILE' => $sErrFile, |
204 | 'LINE' => $iErrLine) ; |
205 | } |
206 | |
1dbd1527 |
207 | $aErrorTpl = array( |
208 | 'type' => $iType, |
209 | 'category' => $aErrorCategory, |
210 | 'message' => $aError['message'], |
211 | 'link' => $aError['link'], |
212 | 'tip' => $aError['tip'], |
213 | 'extra' => $aError['extra']); |
214 | // Add the notice/warning/error to the existing list of notices/warnings |
215 | $this->aErrors[] = $aErrorTpl; |
216 | $this->Template->assign('aErrors',$this->aErrors); |
1455d7ec |
217 | } |
1dbd1527 |
218 | |
1455d7ec |
219 | // Show the error immediate in case of fatal errors |
220 | if ($iType == SQM_ERROR) { |
81de00c0 |
221 | if (!$this->header_sent) { |
85a1f578 |
222 | // TODO replace this with template that can be assigned |
81de00c0 |
223 | displayHtmlHeader(_("Error"),'',false); |
224 | } |
1455d7ec |
225 | $this->DisplayErrors(); |
226 | exit(_("Terminating SquirrelMail due to a fatal error")); |
227 | } |
228 | } |
229 | |
230 | /** |
231 | * Display the error array in the error template |
232 | * @return void |
233 | * @since 1.5.1 |
234 | */ |
235 | function DisplayErrors() { |
1d9716e4 |
236 | // Check for delayed errors... |
237 | if (!$this->delayed_errors) { |
238 | sqgetGlobalVar('delayed_errors', $delayed_errors, SQ_SESSION); |
239 | if (is_array($delayed_errors)) { |
240 | $this->AssignDelayedErrors($delayed_errors); |
241 | sqsession_unregister("delayed_errors"); |
242 | } |
243 | } |
244 | |
245 | if (isset($this->Template->values['aErrors']) && count($this->Template->values['aErrors']) > 0) { |
246 | $this->aErrors = array_merge($this->aErrors, $this->Template->values['aErrors']); |
247 | $this->Template->assign('aErrors',$this->aErrors); |
248 | } |
249 | |
250 | if (count($this->aErrors) > 0) { |
251 | if ($this->delayed_errors) { |
252 | sqsession_register($this->aErrors,"delayed_errors"); |
253 | } else { |
254 | $this->Template->display($this->TemplateName); |
255 | } |
1455d7ec |
256 | } |
257 | } |
258 | } |
259 | |
260 | /** |
261 | * Custom Error handler for PHP version < 4.3.0 (set with set_error_handler() ) |
262 | * @author Marc Groot Koerkamp |
263 | * @since 1.5.1 |
264 | */ |
265 | function SquirrelMailErrorhandler($iErrNo, $sErrStr, $sErrFile, $iErrLine, $aContext) { |
266 | global $oTemplate; |
267 | static $oErrorHandler; |
268 | if (!isset($oErrorHandler)) { |
269 | $oErrorHandler = new ErrorHandler($oTemplate,'error_message.tpl'); |
270 | } |
271 | $oErrorHandler->SquirrelMailErrorhandler($iErrNo, $sErrStr, $sErrFile, $iErrLine, $aContext); |
272 | } |
273 | |
274 | /** |
275 | * Triggers an imap error. Utility function for sqm_trigger_error() |
6d31e3a3 |
276 | * @param string $sErrNo error string defined in errors.php |
1455d7ec |
277 | * @param string $sRequest imap request string |
278 | * @param string $sResponse tagged imap response |
279 | * @param string $sMessage tagged imap response message |
280 | * @param array $aExtra optional associative array with extra error info |
281 | * @return void |
282 | * @author Marc Groot Koerkamp |
283 | * @since 1.5.1 |
284 | */ |
6d31e3a3 |
285 | function sqm_trigger_imap_error($sErrNo,$sRequest,$sResponse, $sMessage, $aExtra=array()) { |
1455d7ec |
286 | $aError = array( |
287 | 'REQUEST' => $sRequest, |
288 | 'RESPONSE' => $sResponse, |
289 | 'MESSAGE' => $sMessage); |
290 | $aError = array_merge($aExtra,$aError); |
6d31e3a3 |
291 | sqm_trigger_error($sErrNo,$aError); |
1455d7ec |
292 | } |
293 | |
294 | /** |
295 | * Trigger an error. |
6d31e3a3 |
296 | * @param string $sErrNo error string defined in errors.php |
1455d7ec |
297 | * @param array $aExtra optional associative array with extra error info |
298 | * @return void |
299 | * @author Marc Groot Koerkamp |
300 | * @since 1.5.1 |
301 | */ |
6d31e3a3 |
302 | function sqm_trigger_error($sErrNo,$aExtra=array()) { |
303 | static $aErrors; |
304 | if (!isset($aErrors)) { |
305 | // Include the error definition file. |
306 | include_once(SM_PATH.'include/errors.php'); |
307 | } |
308 | |
1455d7ec |
309 | $iPhpErr = E_USER_NOTICE; |
6d31e3a3 |
310 | if (is_array($aErrors) && isset($aErrors[$sErrNo]['level'])) { |
1455d7ec |
311 | if (is_array($aExtra) && count($aExtra)) { |
6d31e3a3 |
312 | $aErrors[$sErrNo]['extra'] = $aExtra; |
1455d7ec |
313 | } |
314 | // because trigger_error can only handle a string argument for the error description |
315 | // we serialize the result. |
6d31e3a3 |
316 | $sErrString = serialize($aErrors[$sErrNo]); |
317 | $iPhpErr = $aErrors[$sErrNo]['level']; |
1455d7ec |
318 | } else { |
6d31e3a3 |
319 | sm_print_r($aErrors); |
320 | $sErrString = "Error <$sErrNo> does not exist, fix the code or update the errors.php file"; |
1455d7ec |
321 | $iPhpErr = E_USER_ERROR; |
322 | } |
323 | trigger_error($sErrString, $iPhpErr); |
324 | } |