Call the new function displayErrors() to display errors rather than refering to ...
[squirrelmail.git] / class / error.class.php
CommitLineData
1455d7ec 1<?php
4b4abf93 2
1455d7ec 3/**
4 * error.class.php
5 *
1455d7ec 6 * This contains the custom error handler for SquirrelMail.
7 *
47ccfad4 8 * @copyright &copy; 2005-2006 The SquirrelMail Project Team
4b4abf93 9 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
1455d7ec 10 * @version $Id$
11 * @package squirrelmail
12 */
13
57311df7 14/** Used defines */
1455d7ec 15define('SQM_NOTICE',0);
16define('SQM_WARNING',1);
17define('SQM_ERROR',2);
1dbd1527 18define('SQM_STRICT',3);
57311df7 19
1dbd1527 20// php5 E_STRICT constant (compatibility with php4)
21if (! defined('E_STRICT')) define('E_STRICT',2048);
22// Set docref_root (fixes URLs that link to php manual)
23if (ini_get('docref_root')=='') ini_set('docref_root','http://www.php.net/');
1455d7ec 24
25/**
26 * Error Handler class
27 *
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
30 *
31 * @author Marc Groot Koerkamp
32 * @package squirrelmail
33 */
34class ErrorHandler {
35
36 /**
37 * Constructor
38 * @param object $oTemplate Template object
39 * @param string $sTemplateFile Template containing the error template
40 * @since 1.5.1
41 */
42 function ErrorHandler(&$oTemplate, $sTemplateFile) {
43 $this->TemplateName = $sTemplateFile;
44 $this->Template =& $oTemplate;
45 $this->aErrors = array();
81de00c0 46 $this->header_sent = false;
1455d7ec 47 }
48
49 /**
50 * Sets the error template
51 * @since 1.5.1
52 */
53 function SetTemplateFile($sTemplateFile) {
54 $this->TemplateFile = $sTemplateFile;
55 }
56
57 /**
81de00c0 58 * Sets if the page header is already sent
59 * @since 1.5.1
60 */
61 function HeaderSent() {
62 $this->header_sent = true;
63 }
64
65 /**
85a1f578 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.
81de00c0 69 * @since 1.5.1
70 */
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;
76 }
77
78
79 /**
1455d7ec 80 * Custom Error handler (set with set_error_handler() )
81 * @private
82 * @since 1.5.1
83 */
84 function SquirrelMailErrorhandler($iErrNo, $sErrStr, $sErrFile, $iErrLine, $aContext) {
85 $aError = array(
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.
92 );
93 $iType = NULL;
94 $aErrorCategory = array();
1dbd1527 95
96 /**
6b00bba4 97 * Get current error reporting level.
98 *
cbed20a4 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
6b00bba4 101 * to ini value and reset it to retrieved value.
102 */
103 $iCurErrLevel = error_reporting(ini_get('error_reporting'));
104 error_reporting($iCurErrLevel);
105
106 /**
1dbd1527 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.
1455d7ec 111 */
cbed20a4 112 if ($iErrNo & $iCurErrLevel) {
1dbd1527 113 /*
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
116 */
117 switch ($iErrNo) {
118 case E_STRICT:
119 $iType = (is_null($iType)) ? SQM_STRICT : $iType;
1455d7ec 120 case E_NOTICE:
121 $iType = (is_null($iType)) ? SQM_NOTICE : $iType;
122 case E_WARNING:
123 $iType = (is_null($iType)) ? SQM_WARNING : $iType;
124 $aErrorCategory[] = 'PHP';
125 $aError['message'] = $sErrStr;
126 $aError['extra'] = array(
1dbd1527 127 'FILE' => $sErrFile,
128 'LINE' => $iErrLine) ;;
1455d7ec 129 // what todo with $aContext?
130 break;
131 case E_USER_ERROR:
132 $iType = (is_null($iType)) ? SQM_ERROR : $iType;
133 case E_USER_NOTICE:
134 $iType = (is_null($iType)) ? SQM_NOTICE : $iType;
135 case E_USER_WARNING:
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';
142 } else {
143 $aError = array_merge($aError,$aErrorTemp);
144 // special error handling below
145 if ($aError['category'] & SQM_ERROR_IMAP) {
1dbd1527 146 $aErrorCategory[] = 'IMAP';
147 // imap related error handling inside
1455d7ec 148 }
149 if ($aError['category'] & SQM_ERROR_FS) {
1dbd1527 150 $aErrorCategory[] = 'FILESYSTEM';
151 // filesystem related error handling inside
1455d7ec 152 }
153 if ($aError['category'] & SQM_ERROR_SMTP) {
1dbd1527 154 $aErrorCategory[] = 'SMTP';
155 // smtp related error handling inside
1455d7ec 156 }
157 if ($aError['category'] & SQM_ERROR_LDAP) {
1dbd1527 158 $aErrorCategory[] = 'LDAP';
159 // ldap related error handling inside
1455d7ec 160 }
161 if ($aError['category'] & SQM_ERROR_DB) {
1dbd1527 162 $aErrorCategory[] = 'DATABASE';
163 // db related error handling inside
1455d7ec 164 }
165 if ($aError['category'] & SQM_ERROR_PLUGIN) {
1dbd1527 166 $aErrorCategory[] = 'PLUGIN';
167 do_hook_function('error_handler_plugin',$aError);
168 // plugin related error handling inside
1455d7ec 169 }
170 //if ($aError['category'] & SQM_ERROR_X) {
171 // $aErrorCategory[] = 'X';
172 // place holder for a new category
173 //}
174 }
175 unset($aErrorTemp);
176 } else {
177 $aError['message'] = $sErrStr;
178 $aErrorCategory[] = 'SQM_NOTICE';
179 }
180 break;
181 default: break;
1dbd1527 182 }
183
184 $aErrorTpl = array(
185 'type' => $iType,
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);
1455d7ec 194 }
1dbd1527 195
1455d7ec 196 // Show the error immediate in case of fatal errors
197 if ($iType == SQM_ERROR) {
81de00c0 198 if (!$this->header_sent) {
85a1f578 199 // TODO replace this with template that can be assigned
81de00c0 200 displayHtmlHeader(_("Error"),'',false);
201 }
1455d7ec 202 $this->DisplayErrors();
203 exit(_("Terminating SquirrelMail due to a fatal error"));
204 }
205 }
206
207 /**
208 * Display the error array in the error template
209 * @return void
210 * @since 1.5.1
211 */
212 function DisplayErrors() {
213 if (count($this->aErrors)) {
214 $this->Template->display($this->TemplateName);
215 }
216 }
217}
218
219/**
220 * Custom Error handler for PHP version < 4.3.0 (set with set_error_handler() )
221 * @author Marc Groot Koerkamp
222 * @since 1.5.1
223 */
224function SquirrelMailErrorhandler($iErrNo, $sErrStr, $sErrFile, $iErrLine, $aContext) {
225 global $oTemplate;
226 static $oErrorHandler;
227 if (!isset($oErrorHandler)) {
228 $oErrorHandler = new ErrorHandler($oTemplate,'error_message.tpl');
229 }
230 $oErrorHandler->SquirrelMailErrorhandler($iErrNo, $sErrStr, $sErrFile, $iErrLine, $aContext);
231}
232
233/**
234 * Triggers an imap error. Utility function for sqm_trigger_error()
6d31e3a3 235 * @param string $sErrNo error string defined in errors.php
1455d7ec 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
240 * @return void
241 * @author Marc Groot Koerkamp
242 * @since 1.5.1
243 */
6d31e3a3 244function sqm_trigger_imap_error($sErrNo,$sRequest,$sResponse, $sMessage, $aExtra=array()) {
1455d7ec 245 $aError = array(
246 'REQUEST' => $sRequest,
247 'RESPONSE' => $sResponse,
248 'MESSAGE' => $sMessage);
249 $aError = array_merge($aExtra,$aError);
6d31e3a3 250 sqm_trigger_error($sErrNo,$aError);
1455d7ec 251}
252
253/**
254 * Trigger an error.
6d31e3a3 255 * @param string $sErrNo error string defined in errors.php
1455d7ec 256 * @param array $aExtra optional associative array with extra error info
257 * @return void
258 * @author Marc Groot Koerkamp
259 * @since 1.5.1
260 */
6d31e3a3 261function sqm_trigger_error($sErrNo,$aExtra=array()) {
262 static $aErrors;
263 if (!isset($aErrors)) {
264 // Include the error definition file.
265 include_once(SM_PATH.'include/errors.php');
266 }
267
1455d7ec 268 $iPhpErr = E_USER_NOTICE;
6d31e3a3 269 if (is_array($aErrors) && isset($aErrors[$sErrNo]['level'])) {
1455d7ec 270 if (is_array($aExtra) && count($aExtra)) {
6d31e3a3 271 $aErrors[$sErrNo]['extra'] = $aExtra;
1455d7ec 272 }
273 // because trigger_error can only handle a string argument for the error description
274 // we serialize the result.
6d31e3a3 275 $sErrString = serialize($aErrors[$sErrNo]);
276 $iPhpErr = $aErrors[$sErrNo]['level'];
1455d7ec 277 } else {
6d31e3a3 278 sm_print_r($aErrors);
279 $sErrString = "Error <$sErrNo> does not exist, fix the code or update the errors.php file";
1455d7ec 280 $iPhpErr = E_USER_ERROR;
281 }
282 trigger_error($sErrString, $iPhpErr);
283}