5 * Copyright (c) 2005 The SquirrelMail Project Team
6 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 * This contains the custom error handler for SquirrelMail.
11 * @package squirrelmail
14 define('SQM_NOTICE',0);
15 define('SQM_WARNING',1);
16 define('SQM_ERROR',2);
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/');
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
29 * @author Marc Groot Koerkamp
30 * @package squirrelmail
36 * @param object $oTemplate Template object
37 * @param string $sTemplateFile Template containing the error template
40 function ErrorHandler(&$oTemplate, $sTemplateFile) {
41 $this->TemplateName
= $sTemplateFile;
42 $this->Template
=& $oTemplate;
43 $this->aErrors
= array();
47 * Sets the error template
50 function SetTemplateFile($sTemplateFile) {
51 $this->TemplateFile
= $sTemplateFile;
55 * Custom Error handler (set with set_error_handler() )
59 function SquirrelMailErrorhandler($iErrNo, $sErrStr, $sErrFile, $iErrLine, $aContext) {
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.
69 $aErrorCategory = array();
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.
77 if ((bool) ini_get('error_reporting')) {
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
84 $iType = (is_null($iType)) ? SQM_STRICT
: $iType;
86 $iType = (is_null($iType)) ? SQM_NOTICE
: $iType;
88 $iType = (is_null($iType)) ? SQM_WARNING
: $iType;
89 $aErrorCategory[] = 'PHP';
90 $aError['message'] = $sErrStr;
91 $aError['extra'] = array(
93 'LINE' => $iErrLine) ;;
94 // what todo with $aContext?
97 $iType = (is_null($iType)) ? SQM_ERROR
: $iType;
99 $iType = (is_null($iType)) ? SQM_NOTICE
: $iType;
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';
108 $aError = array_merge($aError,$aErrorTemp);
109 // special error handling below
110 if ($aError['category'] & SQM_ERROR_IMAP
) {
111 $aErrorCategory[] = 'IMAP';
112 // imap related error handling inside
114 if ($aError['category'] & SQM_ERROR_FS
) {
115 $aErrorCategory[] = 'FILESYSTEM';
116 // filesystem related error handling inside
118 if ($aError['category'] & SQM_ERROR_SMTP
) {
119 $aErrorCategory[] = 'SMTP';
120 // smtp related error handling inside
122 if ($aError['category'] & SQM_ERROR_LDAP
) {
123 $aErrorCategory[] = 'LDAP';
124 // ldap related error handling inside
126 if ($aError['category'] & SQM_ERROR_DB
) {
127 $aErrorCategory[] = 'DATABASE';
128 // db related error handling inside
130 if ($aError['category'] & SQM_ERROR_PLUGIN
) {
131 $aErrorCategory[] = 'PLUGIN';
132 do_hook_function('error_handler_plugin',$aError);
133 // plugin related error handling inside
135 //if ($aError['category'] & SQM_ERROR_X) {
136 // $aErrorCategory[] = 'X';
137 // place holder for a new category
142 $aError['message'] = $sErrStr;
143 $aErrorCategory[] = 'SQM_NOTICE';
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
);
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"));
169 * Display the error array in the error template
173 function DisplayErrors() {
174 if (count($this->aErrors
)) {
175 $this->Template
->display($this->TemplateName
);
181 * Custom Error handler for PHP version < 4.3.0 (set with set_error_handler() )
182 * @author Marc Groot Koerkamp
185 function SquirrelMailErrorhandler($iErrNo, $sErrStr, $sErrFile, $iErrLine, $aContext) {
187 static $oErrorHandler;
188 if (!isset($oErrorHandler)) {
189 $oErrorHandler = new ErrorHandler($oTemplate,'error_message.tpl');
191 $oErrorHandler->SquirrelMailErrorhandler($iErrNo, $sErrStr, $sErrFile, $iErrLine, $aContext);
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
202 * @author Marc Groot Koerkamp
205 function sqm_trigger_imap_error($iErrNo,$sRequest,$sResponse, $sMessage, $aExtra=array()) {
207 'REQUEST' => $sRequest,
208 'RESPONSE' => $sResponse,
209 'MESSAGE' => $sMessage);
210 $aError = array_merge($aExtra,$aError);
211 sqm_trigger_error($iErrNo,$aError);
216 * @param integer $iErrNo error number defined in errors.php
217 * @param array $aExtra optional associative array with extra error info
219 * @author Marc Groot Koerkamp
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;
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'];
235 $sErrString = "Error number <$iErrNo> does not exist, fix the code or update the errors.php file";
236 $iPhpErr = E_USER_ERROR
;
238 trigger_error($sErrString, $iPhpErr);