6 * This contains the custom error handler for SquirrelMail.
8 * @copyright © 2005 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
38 * @param object $oTemplate Template object
39 * @param string $sTemplateFile Template containing the error template
42 function ErrorHandler(&$oTemplate, $sTemplateFile) {
43 $this->TemplateName
= $sTemplateFile;
44 $this->Template
=& $oTemplate;
45 $this->aErrors
= array();
49 * Sets the error template
52 function SetTemplateFile($sTemplateFile) {
53 $this->TemplateFile
= $sTemplateFile;
57 * Custom Error handler (set with set_error_handler() )
61 function SquirrelMailErrorhandler($iErrNo, $sErrStr, $sErrFile, $iErrLine, $aContext) {
63 'type' => SQM_NOTICE
,// Error type, notice, warning or fatal error;
64 'category' => NULL, // SquirrelMail error category;
65 'message' => NULL, // Error display message;
66 'extra' => NULL, // Key value based array with extra error info;
67 'link' => NULL, // Link to help location;
68 'tip' => NULL // User tip.
71 $aErrorCategory = array();
74 * Get current error reporting level.
76 * PHP 4.1.2 does not return current error reporting level in ini_get (php 5.1b3 and
77 * 4.3.10 does). Retrieve current error reporting level while setting error reporting
78 * to ini value and reset it to retrieved value.
80 $iCurErrLevel = error_reporting(ini_get('error_reporting'));
81 error_reporting($iCurErrLevel);
84 * Check error_reporting value before logging error.
85 * Don't log errors that are disabled by @ (error_reporting = 0). Some SquirrelMail scripts
86 * (sq_mb_list_encodings(), ldap function calls in functions/abook_ldap_server.php)
87 * handle errors themselves and @ is used to disable generic php error messages.
89 if ($iErrNo & $iCurErrLevel) {
91 * The following errors cannot be handled by a user defined error handler:
92 * E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING
96 $iType = (is_null($iType)) ? SQM_STRICT
: $iType;
98 $iType = (is_null($iType)) ? SQM_NOTICE
: $iType;
100 $iType = (is_null($iType)) ? SQM_WARNING
: $iType;
101 $aErrorCategory[] = 'PHP';
102 $aError['message'] = $sErrStr;
103 $aError['extra'] = array(
105 'LINE' => $iErrLine) ;;
106 // what todo with $aContext?
109 $iType = (is_null($iType)) ? SQM_ERROR
: $iType;
111 $iType = (is_null($iType)) ? SQM_NOTICE
: $iType;
113 $iType = (is_null($iType)) ? SQM_WARNING
: $iType;
114 if ($sErrFile == __FILE__
) { // Error is triggered in this file and probably by sqm_trigger_error
115 $aErrorTemp = @unserialize
($sErrStr);
116 if (!is_array($aErrorTemp)) {
117 $aError['message'] = $sErrStr;
118 $aErrorCategory[] = 'UNDEFINED';
120 $aError = array_merge($aError,$aErrorTemp);
121 // special error handling below
122 if ($aError['category'] & SQM_ERROR_IMAP
) {
123 $aErrorCategory[] = 'IMAP';
124 // imap related error handling inside
126 if ($aError['category'] & SQM_ERROR_FS
) {
127 $aErrorCategory[] = 'FILESYSTEM';
128 // filesystem related error handling inside
130 if ($aError['category'] & SQM_ERROR_SMTP
) {
131 $aErrorCategory[] = 'SMTP';
132 // smtp related error handling inside
134 if ($aError['category'] & SQM_ERROR_LDAP
) {
135 $aErrorCategory[] = 'LDAP';
136 // ldap related error handling inside
138 if ($aError['category'] & SQM_ERROR_DB
) {
139 $aErrorCategory[] = 'DATABASE';
140 // db related error handling inside
142 if ($aError['category'] & SQM_ERROR_PLUGIN
) {
143 $aErrorCategory[] = 'PLUGIN';
144 do_hook_function('error_handler_plugin',$aError);
145 // plugin related error handling inside
147 //if ($aError['category'] & SQM_ERROR_X) {
148 // $aErrorCategory[] = 'X';
149 // place holder for a new category
154 $aError['message'] = $sErrStr;
155 $aErrorCategory[] = 'SQM_NOTICE';
163 'category' => $aErrorCategory,
164 'message' => $aError['message'],
165 'link' => $aError['link'],
166 'tip' => $aError['tip'],
167 'extra' => $aError['extra']);
168 // Add the notice/warning/error to the existing list of notices/warnings
169 $this->aErrors
[] = $aErrorTpl;
170 $this->Template
->assign('aErrors',$this->aErrors
);
173 // Show the error immediate in case of fatal errors
174 if ($iType == SQM_ERROR
) {
175 $this->DisplayErrors();
176 exit(_("Terminating SquirrelMail due to a fatal error"));
181 * Display the error array in the error template
185 function DisplayErrors() {
186 if (count($this->aErrors
)) {
187 $this->Template
->display($this->TemplateName
);
193 * Custom Error handler for PHP version < 4.3.0 (set with set_error_handler() )
194 * @author Marc Groot Koerkamp
197 function SquirrelMailErrorhandler($iErrNo, $sErrStr, $sErrFile, $iErrLine, $aContext) {
199 static $oErrorHandler;
200 if (!isset($oErrorHandler)) {
201 $oErrorHandler = new ErrorHandler($oTemplate,'error_message.tpl');
203 $oErrorHandler->SquirrelMailErrorhandler($iErrNo, $sErrStr, $sErrFile, $iErrLine, $aContext);
207 * Triggers an imap error. Utility function for sqm_trigger_error()
208 * @param integer $iErrNo error number defined in errors.php
209 * @param string $sRequest imap request string
210 * @param string $sResponse tagged imap response
211 * @param string $sMessage tagged imap response message
212 * @param array $aExtra optional associative array with extra error info
214 * @author Marc Groot Koerkamp
217 function sqm_trigger_imap_error($iErrNo,$sRequest,$sResponse, $sMessage, $aExtra=array()) {
219 'REQUEST' => $sRequest,
220 'RESPONSE' => $sResponse,
221 'MESSAGE' => $sMessage);
222 $aError = array_merge($aExtra,$aError);
223 sqm_trigger_error($iErrNo,$aError);
228 * @param integer $iErrNo error number defined in errors.php
229 * @param array $aExtra optional associative array with extra error info
231 * @author Marc Groot Koerkamp
234 function sqm_trigger_error($iErrNo,$aExtra=array()) {
235 // Include the error definition file.
236 include_once(SM_PATH
.'include/errors.php');
237 $iPhpErr = E_USER_NOTICE
;
238 if (is_array($aErrors) && isset($aErrors[$iErrNo]['level'])) {
239 if (is_array($aExtra) && count($aExtra)) {
240 $aErrors[$iErrNo]['extra'] = $aExtra;
242 // because trigger_error can only handle a string argument for the error description
243 // we serialize the result.
244 $sErrString = serialize($aErrors[$iErrNo]);
245 $iPhpErr = $aErrors[$iErrNo]['level'];
247 $sErrString = "Error number <$iErrNo> does not exist, fix the code or update the errors.php file";
248 $iPhpErr = E_USER_ERROR
;
250 trigger_error($sErrString, $iPhpErr);