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 * Store errors generated in a previous script but couldn't be displayed
67 * due to a header redirect. This requires storing of aDelayedErrors in the session
68 * @param array $aDelayedErrors array with errors stored in the $this->aErrors format.
71 function AssignDelayedErrors(&$aDelayedErrors) {
72 $aErrors = array_merge($this->aErrors
,$aDelayedErrors);
73 $this->aErrors
= $aErrors;
74 $this->Template
->assign('aErrors',$this->aErrors
);
75 $aDelayedErrors = false;
80 * Custom Error handler (set with set_error_handler() )
84 function SquirrelMailErrorhandler($iErrNo, $sErrStr, $sErrFile, $iErrLine, $aContext) {
86 'type' => SQM_NOTICE
,// Error type, notice, warning or fatal error;
87 'category' => NULL, // SquirrelMail error category;
88 'message' => NULL, // Error display message;
89 'extra' => NULL, // Key value based array with extra error info;
90 'link' => NULL, // Link to help location;
91 'tip' => NULL // User tip.
94 $aErrorCategory = array();
97 * Get current error reporting level.
99 * PHP 4.1.2 does not return current error reporting level in ini_get (php 5.1b3 and
100 * 4.3.10 does). Retrieve current error reporting level while setting error reporting
101 * to ini value and reset it to retrieved value.
103 $iCurErrLevel = error_reporting(ini_get('error_reporting'));
104 error_reporting($iCurErrLevel);
107 * Check error_reporting value before logging error.
108 * Don't log errors that are disabled by @ (error_reporting = 0). Some SquirrelMail scripts
109 * (sq_mb_list_encodings(), ldap function calls in functions/abook_ldap_server.php)
110 * handle errors themselves and @ is used to disable generic php error messages.
112 if ($iErrNo & $iCurErrLevel) {
114 * The following errors cannot be handled by a user defined error handler:
115 * E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING
119 $iType = (is_null($iType)) ? SQM_STRICT
: $iType;
121 $iType = (is_null($iType)) ? SQM_NOTICE
: $iType;
123 $iType = (is_null($iType)) ? SQM_WARNING
: $iType;
124 $aErrorCategory[] = 'PHP';
125 $aError['message'] = $sErrStr;
126 $aError['extra'] = array(
128 'LINE' => $iErrLine) ;;
129 // what todo with $aContext?
132 $iType = (is_null($iType)) ? SQM_ERROR
: $iType;
134 $iType = (is_null($iType)) ? SQM_NOTICE
: $iType;
136 $iType = (is_null($iType)) ? SQM_WARNING
: $iType;
137 if ($sErrFile == __FILE__
) { // Error is triggered in this file and probably by sqm_trigger_error
138 $aErrorTemp = @unserialize
($sErrStr);
139 if (!is_array($aErrorTemp)) {
140 $aError['message'] = $sErrStr;
141 $aErrorCategory[] = 'UNDEFINED';
143 $aError = array_merge($aError,$aErrorTemp);
144 // special error handling below
145 if ($aError['category'] & SQM_ERROR_IMAP
) {
146 $aErrorCategory[] = 'IMAP';
147 // imap related error handling inside
149 if ($aError['category'] & SQM_ERROR_FS
) {
150 $aErrorCategory[] = 'FILESYSTEM';
151 // filesystem related error handling inside
153 if ($aError['category'] & SQM_ERROR_SMTP
) {
154 $aErrorCategory[] = 'SMTP';
155 // smtp related error handling inside
157 if ($aError['category'] & SQM_ERROR_LDAP
) {
158 $aErrorCategory[] = 'LDAP';
159 // ldap related error handling inside
161 if ($aError['category'] & SQM_ERROR_DB
) {
162 $aErrorCategory[] = 'DATABASE';
163 // db related error handling inside
165 if ($aError['category'] & SQM_ERROR_PLUGIN
) {
166 $aErrorCategory[] = 'PLUGIN';
167 do_hook_function('error_handler_plugin',$aError);
168 // plugin related error handling inside
170 //if ($aError['category'] & SQM_ERROR_X) {
171 // $aErrorCategory[] = 'X';
172 // place holder for a new category
177 $aError['message'] = $sErrStr;
178 $aErrorCategory[] = 'SQM_NOTICE';
186 'category' => $aErrorCategory,
187 'message' => $aError['message'],
188 'link' => $aError['link'],
189 'tip' => $aError['tip'],
190 'extra' => $aError['extra']);
191 // Add the notice/warning/error to the existing list of notices/warnings
192 $this->aErrors
[] = $aErrorTpl;
193 $this->Template
->assign('aErrors',$this->aErrors
);
196 // Show the error immediate in case of fatal errors
197 if ($iType == SQM_ERROR
) {
198 if (!$this->header_sent
) {
199 // TODO replace this with template that can be assigned
200 displayHtmlHeader(_("Error"),'',false);
202 $this->DisplayErrors();
203 exit(_("Terminating SquirrelMail due to a fatal error"));
208 * Display the error array in the error template
212 function DisplayErrors() {
213 if (count($this->aErrors
)) {
214 $this->Template
->display($this->TemplateName
);
220 * Custom Error handler for PHP version < 4.3.0 (set with set_error_handler() )
221 * @author Marc Groot Koerkamp
224 function SquirrelMailErrorhandler($iErrNo, $sErrStr, $sErrFile, $iErrLine, $aContext) {
226 static $oErrorHandler;
227 if (!isset($oErrorHandler)) {
228 $oErrorHandler = new ErrorHandler($oTemplate,'error_message.tpl');
230 $oErrorHandler->SquirrelMailErrorhandler($iErrNo, $sErrStr, $sErrFile, $iErrLine, $aContext);
234 * Triggers an imap error. Utility function for sqm_trigger_error()
235 * @param string $sErrNo error string defined in errors.php
236 * @param string $sRequest imap request string
237 * @param string $sResponse tagged imap response
238 * @param string $sMessage tagged imap response message
239 * @param array $aExtra optional associative array with extra error info
241 * @author Marc Groot Koerkamp
244 function sqm_trigger_imap_error($sErrNo,$sRequest,$sResponse, $sMessage, $aExtra=array()) {
246 'REQUEST' => $sRequest,
247 'RESPONSE' => $sResponse,
248 'MESSAGE' => $sMessage);
249 $aError = array_merge($aExtra,$aError);
250 sqm_trigger_error($sErrNo,$aError);
255 * @param string $sErrNo error string defined in errors.php
256 * @param array $aExtra optional associative array with extra error info
258 * @author Marc Groot Koerkamp
261 function sqm_trigger_error($sErrNo,$aExtra=array()) {
263 if (!isset($aErrors)) {
264 // Include the error definition file.
265 include_once(SM_PATH
.'include/errors.php');
268 $iPhpErr = E_USER_NOTICE
;
269 if (is_array($aErrors) && isset($aErrors[$sErrNo]['level'])) {
270 if (is_array($aExtra) && count($aExtra)) {
271 $aErrors[$sErrNo]['extra'] = $aExtra;
273 // because trigger_error can only handle a string argument for the error description
274 // we serialize the result.
275 $sErrString = serialize($aErrors[$sErrNo]);
276 $iPhpErr = $aErrors[$sErrNo]['level'];
278 sm_print_r($aErrors);
279 $sErrString = "Error <$sErrNo> does not exist, fix the code or update the errors.php file";
280 $iPhpErr = E_USER_ERROR
;
282 trigger_error($sErrString, $iPhpErr);