6 * This contains the custom error handler for SquirrelMail.
8 * @copyright © 2005-2006 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();
46 $this->header_sent
= false;
50 * Sets the error template
53 function SetTemplateFile($sTemplateFile) {
54 $this->TemplateFile
= $sTemplateFile;
58 * Sets if the page header is already sent
61 function HeaderSent() {
62 $this->header_sent
= true;
66 * Sets the error template
69 function AssignDelayedErrors(&$aDelayedErrors) {
70 $aErrors = array_merge($this->aErrors
,$aDelayedErrors);
71 $this->aErrors
= $aErrors;
72 $this->Template
->assign('aErrors',$this->aErrors
);
73 $aDelayedErrors = false;
78 * Custom Error handler (set with set_error_handler() )
82 function SquirrelMailErrorhandler($iErrNo, $sErrStr, $sErrFile, $iErrLine, $aContext) {
84 'type' => SQM_NOTICE
,// Error type, notice, warning or fatal error;
85 'category' => NULL, // SquirrelMail error category;
86 'message' => NULL, // Error display message;
87 'extra' => NULL, // Key value based array with extra error info;
88 'link' => NULL, // Link to help location;
89 'tip' => NULL // User tip.
92 $aErrorCategory = array();
95 * Get current error reporting level.
97 * PHP 4.1.2 does not return current error reporting level in ini_get (php 5.1b3 and
98 * 4.3.10 does). Retrieve current error reporting level while setting error reporting
99 * to ini value and reset it to retrieved value.
101 $iCurErrLevel = error_reporting(ini_get('error_reporting'));
102 error_reporting($iCurErrLevel);
105 * Check error_reporting value before logging error.
106 * Don't log errors that are disabled by @ (error_reporting = 0). Some SquirrelMail scripts
107 * (sq_mb_list_encodings(), ldap function calls in functions/abook_ldap_server.php)
108 * handle errors themselves and @ is used to disable generic php error messages.
110 if ($iErrNo & $iCurErrLevel) {
112 * The following errors cannot be handled by a user defined error handler:
113 * E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING
117 $iType = (is_null($iType)) ? SQM_STRICT
: $iType;
119 $iType = (is_null($iType)) ? SQM_NOTICE
: $iType;
121 $iType = (is_null($iType)) ? SQM_WARNING
: $iType;
122 $aErrorCategory[] = 'PHP';
123 $aError['message'] = $sErrStr;
124 $aError['extra'] = array(
126 'LINE' => $iErrLine) ;;
127 // what todo with $aContext?
130 $iType = (is_null($iType)) ? SQM_ERROR
: $iType;
132 $iType = (is_null($iType)) ? SQM_NOTICE
: $iType;
134 $iType = (is_null($iType)) ? SQM_WARNING
: $iType;
135 if ($sErrFile == __FILE__
) { // Error is triggered in this file and probably by sqm_trigger_error
136 $aErrorTemp = @unserialize
($sErrStr);
137 if (!is_array($aErrorTemp)) {
138 $aError['message'] = $sErrStr;
139 $aErrorCategory[] = 'UNDEFINED';
141 $aError = array_merge($aError,$aErrorTemp);
142 // special error handling below
143 if ($aError['category'] & SQM_ERROR_IMAP
) {
144 $aErrorCategory[] = 'IMAP';
145 // imap related error handling inside
147 if ($aError['category'] & SQM_ERROR_FS
) {
148 $aErrorCategory[] = 'FILESYSTEM';
149 // filesystem related error handling inside
151 if ($aError['category'] & SQM_ERROR_SMTP
) {
152 $aErrorCategory[] = 'SMTP';
153 // smtp related error handling inside
155 if ($aError['category'] & SQM_ERROR_LDAP
) {
156 $aErrorCategory[] = 'LDAP';
157 // ldap related error handling inside
159 if ($aError['category'] & SQM_ERROR_DB
) {
160 $aErrorCategory[] = 'DATABASE';
161 // db related error handling inside
163 if ($aError['category'] & SQM_ERROR_PLUGIN
) {
164 $aErrorCategory[] = 'PLUGIN';
165 do_hook_function('error_handler_plugin',$aError);
166 // plugin related error handling inside
168 //if ($aError['category'] & SQM_ERROR_X) {
169 // $aErrorCategory[] = 'X';
170 // place holder for a new category
175 $aError['message'] = $sErrStr;
176 $aErrorCategory[] = 'SQM_NOTICE';
184 'category' => $aErrorCategory,
185 'message' => $aError['message'],
186 'link' => $aError['link'],
187 'tip' => $aError['tip'],
188 'extra' => $aError['extra']);
189 // Add the notice/warning/error to the existing list of notices/warnings
190 $this->aErrors
[] = $aErrorTpl;
191 $this->Template
->assign('aErrors',$this->aErrors
);
194 // Show the error immediate in case of fatal errors
195 if ($iType == SQM_ERROR
) {
196 if (!$this->header_sent
) {
197 // replace this with template that can be assigned
198 displayHtmlHeader(_("Error"),'',false);
200 $this->DisplayErrors();
201 exit(_("Terminating SquirrelMail due to a fatal error"));
206 * Display the error array in the error template
210 function DisplayErrors() {
211 if (count($this->aErrors
)) {
212 $this->Template
->display($this->TemplateName
);
218 * Custom Error handler for PHP version < 4.3.0 (set with set_error_handler() )
219 * @author Marc Groot Koerkamp
222 function SquirrelMailErrorhandler($iErrNo, $sErrStr, $sErrFile, $iErrLine, $aContext) {
224 static $oErrorHandler;
225 if (!isset($oErrorHandler)) {
226 $oErrorHandler = new ErrorHandler($oTemplate,'error_message.tpl');
228 $oErrorHandler->SquirrelMailErrorhandler($iErrNo, $sErrStr, $sErrFile, $iErrLine, $aContext);
232 * Triggers an imap error. Utility function for sqm_trigger_error()
233 * @param string $sErrNo error string defined in errors.php
234 * @param string $sRequest imap request string
235 * @param string $sResponse tagged imap response
236 * @param string $sMessage tagged imap response message
237 * @param array $aExtra optional associative array with extra error info
239 * @author Marc Groot Koerkamp
242 function sqm_trigger_imap_error($sErrNo,$sRequest,$sResponse, $sMessage, $aExtra=array()) {
244 'REQUEST' => $sRequest,
245 'RESPONSE' => $sResponse,
246 'MESSAGE' => $sMessage);
247 $aError = array_merge($aExtra,$aError);
248 sqm_trigger_error($sErrNo,$aError);
253 * @param string $sErrNo error string defined in errors.php
254 * @param array $aExtra optional associative array with extra error info
256 * @author Marc Groot Koerkamp
259 function sqm_trigger_error($sErrNo,$aExtra=array()) {
261 if (!isset($aErrors)) {
262 // Include the error definition file.
263 include_once(SM_PATH
.'include/errors.php');
266 $iPhpErr = E_USER_NOTICE
;
267 if (is_array($aErrors) && isset($aErrors[$sErrNo]['level'])) {
268 if (is_array($aExtra) && count($aExtra)) {
269 $aErrors[$sErrNo]['extra'] = $aExtra;
271 // because trigger_error can only handle a string argument for the error description
272 // we serialize the result.
273 $sErrString = serialize($aErrors[$sErrNo]);
274 $iPhpErr = $aErrors[$sErrNo]['level'];
276 sm_print_r($aErrors);
277 $sErrString = "Error <$sErrNo> does not exist, fix the code or update the errors.php file";
278 $iPhpErr = E_USER_ERROR
;
280 trigger_error($sErrString, $iPhpErr);