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);
21 * This class contains a custom error handler in order to display
22 * user notices/warnings/errors and php notices and warnings in a template
24 * @author Marc Groot Koerkamp
25 * @package squirrelmail
31 * @param object $oTemplate Template object
32 * @param string $sTemplateFile Template containing the error template
35 function ErrorHandler(&$oTemplate, $sTemplateFile) {
36 $this->TemplateName
= $sTemplateFile;
37 $this->Template
=& $oTemplate;
38 $this->aErrors
= array();
42 * Sets the error template
45 function SetTemplateFile($sTemplateFile) {
46 $this->TemplateFile
= $sTemplateFile;
50 * Custom Error handler (set with set_error_handler() )
54 function SquirrelMailErrorhandler($iErrNo, $sErrStr, $sErrFile, $iErrLine, $aContext) {
56 'type' => SQM_NOTICE
,// Error type, notice, warning or fatal error;
57 'category' => NULL, // SquirrelMail error category;
58 'message' => NULL, // Error display message;
59 'extra' => NULL, // Key value based array with extra error info;
60 'link' => NULL, // Link to help location;
61 'tip' => NULL // User tip.
64 $aErrorCategory = array();
66 * The following errors cannot be handled by a user defined error handler:
67 * E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING
71 $iType = (is_null($iType)) ? SQM_NOTICE
: $iType;
73 $iType = (is_null($iType)) ? SQM_WARNING
: $iType;
74 $aErrorCategory[] = 'PHP';
75 $aError['message'] = $sErrStr;
76 $aError['extra'] = array(
78 'LINE' => $iErrLine) ;;
79 // what todo with $aContext?
82 $iType = (is_null($iType)) ? SQM_ERROR
: $iType;
84 $iType = (is_null($iType)) ? SQM_NOTICE
: $iType;
86 $iType = (is_null($iType)) ? SQM_WARNING
: $iType;
87 if ($sErrFile == __FILE__
) { // Error is triggered in this file and probably by sqm_trigger_error
88 $aErrorTemp = @unserialize
($sErrStr);
89 if (!is_array($aErrorTemp)) {
90 $aError['message'] = $sErrStr;
91 $aErrorCategory[] = 'UNDEFINED';
93 $aError = array_merge($aError,$aErrorTemp);
94 // special error handling below
95 if ($aError['category'] & SQM_ERROR_IMAP
) {
96 $aErrorCategory[] = 'IMAP';
97 // imap related error handling inside
99 if ($aError['category'] & SQM_ERROR_FS
) {
100 $aErrorCategory[] = 'FILESYSTEM';
101 // filesystem related error handling inside
103 if ($aError['category'] & SQM_ERROR_SMTP
) {
104 $aErrorCategory[] = 'SMTP';
105 // smtp related error handling inside
107 if ($aError['category'] & SQM_ERROR_LDAP
) {
108 $aErrorCategory[] = 'LDAP';
109 // ldap related error handling inside
111 if ($aError['category'] & SQM_ERROR_DB
) {
112 $aErrorCategory[] = 'DATABASE';
113 // db related error handling inside
115 if ($aError['category'] & SQM_ERROR_PLUGIN
) {
116 $aErrorCategory[] = 'PLUGIN';
117 do_hook_function('error_handler_plugin',$aError);
118 // plugin related error handling inside
120 //if ($aError['category'] & SQM_ERROR_X) {
121 // $aErrorCategory[] = 'X';
122 // place holder for a new category
127 $aError['message'] = $sErrStr;
128 $aErrorCategory[] = 'SQM_NOTICE';
135 'category' => $aErrorCategory,
136 'message' => $aError['message'],
137 'link' => $aError['link'],
138 'tip' => $aError['tip'],
139 'extra' => $aError['extra']);
140 // Add the notice/warning/error to the existing list of notices/warnings
141 $this->aErrors
[] = $aErrorTpl;
142 $this->Template
->assign('aErrors',$this->aErrors
);
143 // Show the error immediate in case of fatal errors
144 if ($iType == SQM_ERROR
) {
145 $this->DisplayErrors();
146 exit(_("Terminating SquirrelMail due to a fatal error"));
151 * Display the error array in the error template
155 function DisplayErrors() {
156 if (count($this->aErrors
)) {
157 $this->Template
->display($this->TemplateName
);
163 * Custom Error handler for PHP version < 4.3.0 (set with set_error_handler() )
164 * @author Marc Groot Koerkamp
167 function SquirrelMailErrorhandler($iErrNo, $sErrStr, $sErrFile, $iErrLine, $aContext) {
169 static $oErrorHandler;
170 if (!isset($oErrorHandler)) {
171 $oErrorHandler = new ErrorHandler($oTemplate,'error_message.tpl');
173 $oErrorHandler->SquirrelMailErrorhandler($iErrNo, $sErrStr, $sErrFile, $iErrLine, $aContext);
177 * Triggers an imap error. Utility function for sqm_trigger_error()
178 * @param integer $iErrNo error number defined in errors.php
179 * @param string $sRequest imap request string
180 * @param string $sResponse tagged imap response
181 * @param string $sMessage tagged imap response message
182 * @param array $aExtra optional associative array with extra error info
184 * @author Marc Groot Koerkamp
187 function sqm_trigger_imap_error($iErrNo,$sRequest,$sResponse, $sMessage, $aExtra=array()) {
189 'REQUEST' => $sRequest,
190 'RESPONSE' => $sResponse,
191 'MESSAGE' => $sMessage);
192 $aError = array_merge($aExtra,$aError);
193 sqm_trigger_error($iErrNo,$aError);
198 * @param integer $iErrNo error number defined in errors.php
199 * @param array $aExtra optional associative array with extra error info
201 * @author Marc Groot Koerkamp
204 function sqm_trigger_error($iErrNo,$aExtra=array()) {
205 // Include the error definition file.
206 include_once(SM_PATH
.'include/errors.php');
207 $iPhpErr = E_USER_NOTICE
;
208 if (is_array($aErrors) && isset($aErrors[$iErrNo]['level'])) {
209 if (is_array($aExtra) && count($aExtra)) {
210 $aErrors[$iErrNo]['extra'] = $aExtra;
212 // because trigger_error can only handle a string argument for the error description
213 // we serialize the result.
214 $sErrString = serialize($aErrors[$iErrNo]);
215 $iPhpErr = $aErrors[$iErrNo]['level'];
217 $sErrString = "Error number <$iErrNo> does not exist, fix the code or update the errors.php file";
218 $iPhpErr = E_USER_ERROR
;
220 trigger_error($sErrString, $iPhpErr);