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