6 * This contains the custom error handler for SquirrelMail.
8 * @copyright 2005-2021 The SquirrelMail Project Team
9 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
11 * @package squirrelmail
15 define('SQM_NOTICE',0);
16 define('SQM_WARNING',1);
17 define('SQM_ERROR',2);
18 define('SQM_STRICT',3);
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/');
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
31 * @author Marc Groot Koerkamp
32 * @package squirrelmail
37 * Constructor (PHP5 style, required in some future version of PHP)
38 * @param object $oTemplate Template object
39 * @param string $sTemplateFile Template containing the error template
42 function __construct(&$oTemplate, $sTemplateFile) {
43 # echo 'init error handler...';
44 $this->TemplateName
= $sTemplateFile;
45 $this->Template
=& $oTemplate;
46 $this->aErrors
= array();
47 $this->header_sent
= false;
48 $this->delayed_errors
= false;
49 $this->Template
->assign('delayed_errors', $this->delayed_errors
);
53 * Constructor (PHP4 style, kept for compatibility reasons)
54 * @param object $oTemplate Template object
55 * @param string $sTemplateFile Template containing the error template
58 function ErrorHandler(&$oTemplate, $sTemplateFile) {
59 self
::__construct($oTemplate, $sTemplateFile);
63 * Sets the error template
66 function SetTemplateFile($sTemplateFile) {
67 $this->TemplateFile
= $sTemplateFile;
71 * Sets if the page header is already sent
74 function HeaderSent() {
75 $this->header_sent
= true;
76 $this->Template
->assign('header_sent', true);
80 * Turn on/off delayed error handling
83 function setDelayedErrors ($val = true) {
84 $this->delayed_errors
= $val===true;
85 $this->Template
->assign('delayed_errors', $this->delayed_errors
);
89 * Store errors generated in a previous script but couldn't be displayed
90 * due to a header redirect. This requires storing of aDelayedErrors in the session
91 * @param array $aDelayedErrors array with errors stored in the $this->aErrors format.
94 function AssignDelayedErrors(&$aDelayedErrors) {
95 $aErrors = array_merge($this->aErrors
,$aDelayedErrors);
96 $this->aErrors
= $aErrors;
97 $this->Template
->assign('aErrors',$this->aErrors
);
98 $aDelayedErrors = false;
103 * Custom Error handler (set with set_error_handler() )
107 function SquirrelMailErrorhandler($iErrNo, $sErrStr, $sErrFile, $iErrLine, $aContext) {
109 'type' => SQM_NOTICE
,// Error type, notice, warning or fatal error;
110 'category' => NULL, // SquirrelMail error category;
111 'message' => NULL, // Error display message;
112 'extra' => NULL, // Key value based array with extra error info;
113 'link' => NULL, // Link to help location;
114 'tip' => NULL // User tip.
117 $aErrorCategory = array();
120 * Get current error reporting level.
122 * PHP 4.1.2 does not return current error reporting level in ini_get (php 5.1b3 and
123 * 4.3.10 does). Retrieve current error reporting level while setting error reporting
124 * to ini value and reset it to retrieved value.
126 $iCurErrLevel = error_reporting(ini_get('error_reporting'));
127 error_reporting($iCurErrLevel);
130 * Check error_reporting value before logging error.
131 * Don't log errors that are disabled by @ (error_reporting = 0). Some SquirrelMail scripts
132 * (sq_mb_list_encodings(), ldap function calls in functions/abook_ldap_server.php)
133 * handle errors themselves and @ is used to disable generic php error messages.
135 if ($iErrNo & $iCurErrLevel) {
137 * The following errors cannot be handled by a user defined error handler:
138 * E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING
142 $iType = (is_null($iType)) ? SQM_STRICT
: $iType;
144 $iType = (is_null($iType)) ? SQM_NOTICE
: $iType;
146 $iType = (is_null($iType)) ? SQM_WARNING
: $iType;
147 $aErrorCategory[] = 'PHP';
148 $aError['message'] = $sErrStr;
149 $aError['extra'] = array(
151 'LINE' => $iErrLine) ;
152 // what todo with $aContext?
155 $iType = (is_null($iType)) ? SQM_ERROR
: $iType;
157 $iType = (is_null($iType)) ? SQM_NOTICE
: $iType;
159 $iType = (is_null($iType)) ? SQM_WARNING
: $iType;
160 if ($sErrFile == __FILE__
) { // Error is triggered in this file and probably by sqm_trigger_error
161 $aErrorTemp = @unserialize
($sErrStr);
162 if (!is_array($aErrorTemp)) {
163 $aError['message'] = $sErrStr;
164 $aErrorCategory[] = 'UNDEFINED';
166 $aError = array_merge($aError,$aErrorTemp);
167 // special error handling below
168 if ($aError['category'] & SQM_ERROR_IMAP
) {
169 $aErrorCategory[] = 'IMAP';
170 // imap related error handling inside
172 if ($aError['category'] & SQM_ERROR_FS
) {
173 $aErrorCategory[] = 'FILESYSTEM';
174 // filesystem related error handling inside
176 if ($aError['category'] & SQM_ERROR_SMTP
) {
177 $aErrorCategory[] = 'SMTP';
178 // smtp related error handling inside
180 if ($aError['category'] & SQM_ERROR_LDAP
) {
181 $aErrorCategory[] = 'LDAP';
182 // ldap related error handling inside
184 if ($aError['category'] & SQM_ERROR_DB
) {
185 $aErrorCategory[] = 'DATABASE';
186 // db related error handling inside
188 if ($aError['category'] & SQM_ERROR_PLUGIN
) {
189 $aErrorCategory[] = 'PLUGIN';
190 do_hook('error_handler_plugin', $aError);
191 // plugin related error handling inside
193 //if ($aError['category'] & SQM_ERROR_X) {
194 // $aErrorCategory[] = 'X';
195 // place holder for a new category
200 $aError['message'] = $sErrStr;
201 $aErrorCategory[] = 'SQM_NOTICE';
208 * If delayed error handling is enabled, always record the location
209 * and tag the error is delayed to make debugging easier.
211 if (isset($this->Template
->values
['delayed_errors']) && $this->Template
->values
['delayed_errors']) {
212 $aErrorCategory[] = 'Delayed';
213 $aError['extra'] = array(
215 'LINE' => $iErrLine) ;
220 'category' => $aErrorCategory,
221 'message' => $aError['message'],
222 'link' => $aError['link'],
223 'tip' => $aError['tip'],
224 'extra' => $aError['extra']);
225 // Add the notice/warning/error to the existing list of notices/warnings
226 $this->aErrors
[] = $aErrorTpl;
227 $this->Template
->assign('aErrors',$this->aErrors
);
230 // Show the error immediate in case of fatal errors
231 if ($iType == SQM_ERROR
) {
232 if (isset($this->Template
->values
['header_sent']) && !$this->Template
->values
['header_sent']) {
233 // TODO replace this with template that can be assigned
234 // UPDATE: displayHtmlHeader() no longer sends anything
235 // directly to the browser itself and instead
236 // displays all output through the template file
237 // "protocol_header" as well as calls to the
238 // template's header() method, so perhaps the
239 // above TODO is alleviated?? (however, I don't fully
240 // understand the problem behind the TODO comment myself (Paul))
241 displayHtmlHeader(_("Error"),'',false);
243 $this->DisplayErrors();
244 exit(_("Terminating SquirrelMail due to a fatal error"));
249 * Force the delayed errors to be stored in the session in case
250 * $this->displayErrors() never gets called, e.g. in compose.php
252 function saveDelayedErrors () {
253 if($this->delayed_errors
) {
254 // Check for previous delayed errors...
255 sqgetGlobalVar('delayed_errors', $delayed_errors, SQ_SESSION
);
256 if (is_array($delayed_errors)) {
257 $this->AssignDelayedErrors($delayed_errors);
258 sqsession_unregister("delayed_errors");
261 if (count($this->aErrors
) > 0) {
262 sqsession_register($this->aErrors
,"delayed_errors");
268 * Display the error array in the error template
272 function DisplayErrors() {
273 // Check for delayed errors...
274 if (!$this->delayed_errors
) {
275 sqgetGlobalVar('delayed_errors', $delayed_errors, SQ_SESSION
);
276 if (is_array($delayed_errors)) {
277 $this->AssignDelayedErrors($delayed_errors);
278 sqsession_unregister("delayed_errors");
282 if (isset($this->Template
->values
['aErrors']) && count($this->Template
->values
['aErrors']) > 0) {
283 foreach ($this->Template
->values
['aErrors'] as $err) {
284 if (!in_array($err, $this->aErrors
, true)) {
285 $this->aErrors
[] = $err;
288 $this->Template
->assign('aErrors',$this->aErrors
);
291 if (count($this->aErrors
) > 0) {
292 if ($this->delayed_errors
) {
293 sqsession_register($this->aErrors
,"delayed_errors");
295 $this->Template
->display($this->TemplateName
);
302 * Custom Error handler for PHP version < 4.3.0 (set with set_error_handler() )
303 * @author Marc Groot Koerkamp
306 function SquirrelMailErrorhandler($iErrNo, $sErrStr, $sErrFile, $iErrLine, $aContext) {
308 static $oErrorHandler;
309 if (!isset($oErrorHandler)) {
310 $oErrorHandler = new ErrorHandler($oTemplate,'error_message.tpl');
312 $oErrorHandler->SquirrelMailErrorhandler($iErrNo, $sErrStr, $sErrFile, $iErrLine, $aContext);
316 * Triggers an imap error. Utility function for sqm_trigger_error()
317 * @param string $sErrNo error string defined in errors.php
318 * @param string $sRequest imap request string
319 * @param string $sResponse tagged imap response
320 * @param string $sMessage tagged imap response message
321 * @param array $aExtra optional associative array with extra error info
323 * @author Marc Groot Koerkamp
326 function sqm_trigger_imap_error($sErrNo,$sRequest,$sResponse, $sMessage, $aExtra=array()) {
328 'REQUEST' => $sRequest,
329 'RESPONSE' => $sResponse,
330 'MESSAGE' => $sMessage);
331 $aError = array_merge($aExtra,$aError);
332 sqm_trigger_error($sErrNo,$aError);
337 * @param string $sErrNo error string defined in errors.php
338 * @param array $aExtra optional associative array with extra error info
340 * @author Marc Groot Koerkamp
343 function sqm_trigger_error($sErrNo,$aExtra=array()) {
345 if (!isset($aErrors)) {
346 // Include the error definition file.
347 include_once(SM_PATH
.'include/errors.php');
350 $iPhpErr = E_USER_NOTICE
;
351 if (is_array($aErrors) && isset($aErrors[$sErrNo]['level'])) {
352 if (is_array($aExtra) && count($aExtra)) {
353 $aErrors[$sErrNo]['extra'] = $aExtra;
355 // because trigger_error can only handle a string argument for the error description
356 // we serialize the result.
357 $sErrString = serialize($aErrors[$sErrNo]);
358 $iPhpErr = $aErrors[$sErrNo]['level'];
360 sm_print_r($aErrors);
361 $sErrString = "Error <$sErrNo> does not exist, fix the code or update the errors.php file";
362 $iPhpErr = E_USER_ERROR
;
364 trigger_error($sErrString, $iPhpErr);