removing global
[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 /**
66 * Sets the error template
67 * @since 1.5.1
68 */
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;
74 }
75
76
77 /**
1455d7ec 78 * Custom Error handler (set with set_error_handler() )
79 * @private
80 * @since 1.5.1
81 */
82 function SquirrelMailErrorhandler($iErrNo, $sErrStr, $sErrFile, $iErrLine, $aContext) {
83 $aError = array(
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.
90 );
91 $iType = NULL;
92 $aErrorCategory = array();
1dbd1527 93
94 /**
6b00bba4 95 * Get current error reporting level.
96 *
cbed20a4 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
6b00bba4 99 * to ini value and reset it to retrieved value.
100 */
101 $iCurErrLevel = error_reporting(ini_get('error_reporting'));
102 error_reporting($iCurErrLevel);
103
104 /**
1dbd1527 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.
1455d7ec 109 */
cbed20a4 110 if ($iErrNo & $iCurErrLevel) {
1dbd1527 111 /*
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
114 */
115 switch ($iErrNo) {
116 case E_STRICT:
117 $iType = (is_null($iType)) ? SQM_STRICT : $iType;
1455d7ec 118 case E_NOTICE:
119 $iType = (is_null($iType)) ? SQM_NOTICE : $iType;
120 case E_WARNING:
121 $iType = (is_null($iType)) ? SQM_WARNING : $iType;
122 $aErrorCategory[] = 'PHP';
123 $aError['message'] = $sErrStr;
124 $aError['extra'] = array(
1dbd1527 125 'FILE' => $sErrFile,
126 'LINE' => $iErrLine) ;;
1455d7ec 127 // what todo with $aContext?
128 break;
129 case E_USER_ERROR:
130 $iType = (is_null($iType)) ? SQM_ERROR : $iType;
131 case E_USER_NOTICE:
132 $iType = (is_null($iType)) ? SQM_NOTICE : $iType;
133 case E_USER_WARNING:
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';
140 } else {
141 $aError = array_merge($aError,$aErrorTemp);
142 // special error handling below
143 if ($aError['category'] & SQM_ERROR_IMAP) {
1dbd1527 144 $aErrorCategory[] = 'IMAP';
145 // imap related error handling inside
1455d7ec 146 }
147 if ($aError['category'] & SQM_ERROR_FS) {
1dbd1527 148 $aErrorCategory[] = 'FILESYSTEM';
149 // filesystem related error handling inside
1455d7ec 150 }
151 if ($aError['category'] & SQM_ERROR_SMTP) {
1dbd1527 152 $aErrorCategory[] = 'SMTP';
153 // smtp related error handling inside
1455d7ec 154 }
155 if ($aError['category'] & SQM_ERROR_LDAP) {
1dbd1527 156 $aErrorCategory[] = 'LDAP';
157 // ldap related error handling inside
1455d7ec 158 }
159 if ($aError['category'] & SQM_ERROR_DB) {
1dbd1527 160 $aErrorCategory[] = 'DATABASE';
161 // db related error handling inside
1455d7ec 162 }
163 if ($aError['category'] & SQM_ERROR_PLUGIN) {
1dbd1527 164 $aErrorCategory[] = 'PLUGIN';
165 do_hook_function('error_handler_plugin',$aError);
166 // plugin related error handling inside
1455d7ec 167 }
168 //if ($aError['category'] & SQM_ERROR_X) {
169 // $aErrorCategory[] = 'X';
170 // place holder for a new category
171 //}
172 }
173 unset($aErrorTemp);
174 } else {
175 $aError['message'] = $sErrStr;
176 $aErrorCategory[] = 'SQM_NOTICE';
177 }
178 break;
179 default: break;
1dbd1527 180 }
181
182 $aErrorTpl = array(
183 'type' => $iType,
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);
1455d7ec 192 }
1dbd1527 193
1455d7ec 194 // Show the error immediate in case of fatal errors
195 if ($iType == SQM_ERROR) {
81de00c0 196 if (!$this->header_sent) {
197 // replace this with template that can be assigned
198 displayHtmlHeader(_("Error"),'',false);
199 }
1455d7ec 200 $this->DisplayErrors();
201 exit(_("Terminating SquirrelMail due to a fatal error"));
202 }
203 }
204
205 /**
206 * Display the error array in the error template
207 * @return void
208 * @since 1.5.1
209 */
210 function DisplayErrors() {
211 if (count($this->aErrors)) {
212 $this->Template->display($this->TemplateName);
213 }
214 }
215}
216
217/**
218 * Custom Error handler for PHP version < 4.3.0 (set with set_error_handler() )
219 * @author Marc Groot Koerkamp
220 * @since 1.5.1
221 */
222function SquirrelMailErrorhandler($iErrNo, $sErrStr, $sErrFile, $iErrLine, $aContext) {
223 global $oTemplate;
224 static $oErrorHandler;
225 if (!isset($oErrorHandler)) {
226 $oErrorHandler = new ErrorHandler($oTemplate,'error_message.tpl');
227 }
228 $oErrorHandler->SquirrelMailErrorhandler($iErrNo, $sErrStr, $sErrFile, $iErrLine, $aContext);
229}
230
231/**
232 * Triggers an imap error. Utility function for sqm_trigger_error()
6d31e3a3 233 * @param string $sErrNo error string defined in errors.php
1455d7ec 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
238 * @return void
239 * @author Marc Groot Koerkamp
240 * @since 1.5.1
241 */
6d31e3a3 242function sqm_trigger_imap_error($sErrNo,$sRequest,$sResponse, $sMessage, $aExtra=array()) {
1455d7ec 243 $aError = array(
244 'REQUEST' => $sRequest,
245 'RESPONSE' => $sResponse,
246 'MESSAGE' => $sMessage);
247 $aError = array_merge($aExtra,$aError);
6d31e3a3 248 sqm_trigger_error($sErrNo,$aError);
1455d7ec 249}
250
251/**
252 * Trigger an error.
6d31e3a3 253 * @param string $sErrNo error string defined in errors.php
1455d7ec 254 * @param array $aExtra optional associative array with extra error info
255 * @return void
256 * @author Marc Groot Koerkamp
257 * @since 1.5.1
258 */
6d31e3a3 259function sqm_trigger_error($sErrNo,$aExtra=array()) {
260 static $aErrors;
261 if (!isset($aErrors)) {
262 // Include the error definition file.
263 include_once(SM_PATH.'include/errors.php');
264 }
265
1455d7ec 266 $iPhpErr = E_USER_NOTICE;
6d31e3a3 267 if (is_array($aErrors) && isset($aErrors[$sErrNo]['level'])) {
1455d7ec 268 if (is_array($aExtra) && count($aExtra)) {
6d31e3a3 269 $aErrors[$sErrNo]['extra'] = $aExtra;
1455d7ec 270 }
271 // because trigger_error can only handle a string argument for the error description
272 // we serialize the result.
6d31e3a3 273 $sErrString = serialize($aErrors[$sErrNo]);
274 $iPhpErr = $aErrors[$sErrNo]['level'];
1455d7ec 275 } else {
6d31e3a3 276 sm_print_r($aErrors);
277 $sErrString = "Error <$sErrNo> does not exist, fix the code or update the errors.php file";
1455d7ec 278 $iPhpErr = E_USER_ERROR;
279 }
280 trigger_error($sErrString, $iPhpErr);
281}
282?>