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 | |
71 | /** |
72 | * Check error_reporting value before logging error. |
73 | * Don't log errors that are disabled by @ (error_reporting = 0). Some SquirrelMail scripts |
74 | * (sq_mb_list_encodings(), ldap function calls in functions/abook_ldap_server.php) |
75 | * handle errors themselves and @ is used to disable generic php error messages. |
1455d7ec |
76 | */ |
1dbd1527 |
77 | if ((bool) ini_get('error_reporting')) { |
78 | /* |
79 | * The following errors cannot be handled by a user defined error handler: |
80 | * E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING |
81 | */ |
82 | switch ($iErrNo) { |
83 | case E_STRICT: |
84 | $iType = (is_null($iType)) ? SQM_STRICT : $iType; |
1455d7ec |
85 | case E_NOTICE: |
86 | $iType = (is_null($iType)) ? SQM_NOTICE : $iType; |
87 | case E_WARNING: |
88 | $iType = (is_null($iType)) ? SQM_WARNING : $iType; |
89 | $aErrorCategory[] = 'PHP'; |
90 | $aError['message'] = $sErrStr; |
91 | $aError['extra'] = array( |
1dbd1527 |
92 | 'FILE' => $sErrFile, |
93 | 'LINE' => $iErrLine) ;; |
1455d7ec |
94 | // what todo with $aContext? |
95 | break; |
96 | case E_USER_ERROR: |
97 | $iType = (is_null($iType)) ? SQM_ERROR : $iType; |
98 | case E_USER_NOTICE: |
99 | $iType = (is_null($iType)) ? SQM_NOTICE : $iType; |
100 | case E_USER_WARNING: |
101 | $iType = (is_null($iType)) ? SQM_WARNING : $iType; |
102 | if ($sErrFile == __FILE__) { // Error is triggered in this file and probably by sqm_trigger_error |
103 | $aErrorTemp = @unserialize($sErrStr); |
104 | if (!is_array($aErrorTemp)) { |
105 | $aError['message'] = $sErrStr; |
106 | $aErrorCategory[] = 'UNDEFINED'; |
107 | } else { |
108 | $aError = array_merge($aError,$aErrorTemp); |
109 | // special error handling below |
110 | if ($aError['category'] & SQM_ERROR_IMAP) { |
1dbd1527 |
111 | $aErrorCategory[] = 'IMAP'; |
112 | // imap related error handling inside |
1455d7ec |
113 | } |
114 | if ($aError['category'] & SQM_ERROR_FS) { |
1dbd1527 |
115 | $aErrorCategory[] = 'FILESYSTEM'; |
116 | // filesystem related error handling inside |
1455d7ec |
117 | } |
118 | if ($aError['category'] & SQM_ERROR_SMTP) { |
1dbd1527 |
119 | $aErrorCategory[] = 'SMTP'; |
120 | // smtp related error handling inside |
1455d7ec |
121 | } |
122 | if ($aError['category'] & SQM_ERROR_LDAP) { |
1dbd1527 |
123 | $aErrorCategory[] = 'LDAP'; |
124 | // ldap related error handling inside |
1455d7ec |
125 | } |
126 | if ($aError['category'] & SQM_ERROR_DB) { |
1dbd1527 |
127 | $aErrorCategory[] = 'DATABASE'; |
128 | // db related error handling inside |
1455d7ec |
129 | } |
130 | if ($aError['category'] & SQM_ERROR_PLUGIN) { |
1dbd1527 |
131 | $aErrorCategory[] = 'PLUGIN'; |
132 | do_hook_function('error_handler_plugin',$aError); |
133 | // plugin related error handling inside |
1455d7ec |
134 | } |
135 | //if ($aError['category'] & SQM_ERROR_X) { |
136 | // $aErrorCategory[] = 'X'; |
137 | // place holder for a new category |
138 | //} |
139 | } |
140 | unset($aErrorTemp); |
141 | } else { |
142 | $aError['message'] = $sErrStr; |
143 | $aErrorCategory[] = 'SQM_NOTICE'; |
144 | } |
145 | break; |
146 | default: break; |
1dbd1527 |
147 | } |
148 | |
149 | $aErrorTpl = array( |
150 | 'type' => $iType, |
151 | 'category' => $aErrorCategory, |
152 | 'message' => $aError['message'], |
153 | 'link' => $aError['link'], |
154 | 'tip' => $aError['tip'], |
155 | 'extra' => $aError['extra']); |
156 | // Add the notice/warning/error to the existing list of notices/warnings |
157 | $this->aErrors[] = $aErrorTpl; |
158 | $this->Template->assign('aErrors',$this->aErrors); |
1455d7ec |
159 | } |
1dbd1527 |
160 | |
1455d7ec |
161 | // Show the error immediate in case of fatal errors |
162 | if ($iType == SQM_ERROR) { |
163 | $this->DisplayErrors(); |
164 | exit(_("Terminating SquirrelMail due to a fatal error")); |
165 | } |
166 | } |
167 | |
168 | /** |
169 | * Display the error array in the error template |
170 | * @return void |
171 | * @since 1.5.1 |
172 | */ |
173 | function DisplayErrors() { |
174 | if (count($this->aErrors)) { |
175 | $this->Template->display($this->TemplateName); |
176 | } |
177 | } |
178 | } |
179 | |
180 | /** |
181 | * Custom Error handler for PHP version < 4.3.0 (set with set_error_handler() ) |
182 | * @author Marc Groot Koerkamp |
183 | * @since 1.5.1 |
184 | */ |
185 | function SquirrelMailErrorhandler($iErrNo, $sErrStr, $sErrFile, $iErrLine, $aContext) { |
186 | global $oTemplate; |
187 | static $oErrorHandler; |
188 | if (!isset($oErrorHandler)) { |
189 | $oErrorHandler = new ErrorHandler($oTemplate,'error_message.tpl'); |
190 | } |
191 | $oErrorHandler->SquirrelMailErrorhandler($iErrNo, $sErrStr, $sErrFile, $iErrLine, $aContext); |
192 | } |
193 | |
194 | /** |
195 | * Triggers an imap error. Utility function for sqm_trigger_error() |
196 | * @param integer $iErrNo error number defined in errors.php |
197 | * @param string $sRequest imap request string |
198 | * @param string $sResponse tagged imap response |
199 | * @param string $sMessage tagged imap response message |
200 | * @param array $aExtra optional associative array with extra error info |
201 | * @return void |
202 | * @author Marc Groot Koerkamp |
203 | * @since 1.5.1 |
204 | */ |
205 | function sqm_trigger_imap_error($iErrNo,$sRequest,$sResponse, $sMessage, $aExtra=array()) { |
206 | $aError = array( |
207 | 'REQUEST' => $sRequest, |
208 | 'RESPONSE' => $sResponse, |
209 | 'MESSAGE' => $sMessage); |
210 | $aError = array_merge($aExtra,$aError); |
211 | sqm_trigger_error($iErrNo,$aError); |
212 | } |
213 | |
214 | /** |
215 | * Trigger an error. |
216 | * @param integer $iErrNo error number defined in errors.php |
217 | * @param array $aExtra optional associative array with extra error info |
218 | * @return void |
219 | * @author Marc Groot Koerkamp |
220 | * @since 1.5.1 |
221 | */ |
222 | function sqm_trigger_error($iErrNo,$aExtra=array()) { |
223 | // Include the error definition file. |
224 | include_once(SM_PATH.'include/errors.php'); |
225 | $iPhpErr = E_USER_NOTICE; |
226 | if (is_array($aErrors) && isset($aErrors[$iErrNo]['level'])) { |
227 | if (is_array($aExtra) && count($aExtra)) { |
228 | $aErrors[$iErrNo]['extra'] = $aExtra; |
229 | } |
230 | // because trigger_error can only handle a string argument for the error description |
231 | // we serialize the result. |
232 | $sErrString = serialize($aErrors[$iErrNo]); |
233 | $iPhpErr = $aErrors[$iErrNo]['level']; |
234 | } else { |
235 | $sErrString = "Error number <$iErrNo> does not exist, fix the code or update the errors.php file"; |
236 | $iPhpErr = E_USER_ERROR; |
237 | } |
238 | trigger_error($sErrString, $iPhpErr); |
239 | } |
240 | ?> |