remove unneeded default which had a horrible backslash in a path
[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();
46 }
47
48 /**
49 * Sets the error template
50 * @since 1.5.1
51 */
52 function SetTemplateFile($sTemplateFile) {
53 $this->TemplateFile = $sTemplateFile;
54 }
55
56 /**
57 * Custom Error handler (set with set_error_handler() )
58 * @private
59 * @since 1.5.1
60 */
61 function SquirrelMailErrorhandler($iErrNo, $sErrStr, $sErrFile, $iErrLine, $aContext) {
62 $aError = array(
63 'type' => SQM_NOTICE,// Error type, notice, warning or fatal error;
64 'category' => NULL, // SquirrelMail error category;
65 'message' => NULL, // Error display message;
66 'extra' => NULL, // Key value based array with extra error info;
67 'link' => NULL, // Link to help location;
68 'tip' => NULL // User tip.
69 );
70 $iType = NULL;
71 $aErrorCategory = array();
1dbd1527 72
6b00bba4 73 /**
74 * Get current error reporting level.
75 *
cbed20a4 76 * PHP 4.1.2 does not return current error reporting level in ini_get (php 5.1b3 and
77 * 4.3.10 does). Retrieve current error reporting level while setting error reporting
6b00bba4 78 * to ini value and reset it to retrieved value.
79 */
80 $iCurErrLevel = error_reporting(ini_get('error_reporting'));
81 error_reporting($iCurErrLevel);
82
1dbd1527 83 /**
84 * Check error_reporting value before logging error.
85 * Don't log errors that are disabled by @ (error_reporting = 0). Some SquirrelMail scripts
86 * (sq_mb_list_encodings(), ldap function calls in functions/abook_ldap_server.php)
87 * handle errors themselves and @ is used to disable generic php error messages.
1455d7ec 88 */
cbed20a4 89 if ($iErrNo & $iCurErrLevel) {
1dbd1527 90 /*
91 * The following errors cannot be handled by a user defined error handler:
92 * E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING
93 */
94 switch ($iErrNo) {
95 case E_STRICT:
96 $iType = (is_null($iType)) ? SQM_STRICT : $iType;
1455d7ec 97 case E_NOTICE:
98 $iType = (is_null($iType)) ? SQM_NOTICE : $iType;
99 case E_WARNING:
100 $iType = (is_null($iType)) ? SQM_WARNING : $iType;
101 $aErrorCategory[] = 'PHP';
102 $aError['message'] = $sErrStr;
103 $aError['extra'] = array(
1dbd1527 104 'FILE' => $sErrFile,
105 'LINE' => $iErrLine) ;;
1455d7ec 106 // what todo with $aContext?
107 break;
108 case E_USER_ERROR:
109 $iType = (is_null($iType)) ? SQM_ERROR : $iType;
110 case E_USER_NOTICE:
111 $iType = (is_null($iType)) ? SQM_NOTICE : $iType;
112 case E_USER_WARNING:
113 $iType = (is_null($iType)) ? SQM_WARNING : $iType;
114 if ($sErrFile == __FILE__) { // Error is triggered in this file and probably by sqm_trigger_error
115 $aErrorTemp = @unserialize($sErrStr);
116 if (!is_array($aErrorTemp)) {
117 $aError['message'] = $sErrStr;
118 $aErrorCategory[] = 'UNDEFINED';
119 } else {
120 $aError = array_merge($aError,$aErrorTemp);
121 // special error handling below
122 if ($aError['category'] & SQM_ERROR_IMAP) {
1dbd1527 123 $aErrorCategory[] = 'IMAP';
124 // imap related error handling inside
1455d7ec 125 }
126 if ($aError['category'] & SQM_ERROR_FS) {
1dbd1527 127 $aErrorCategory[] = 'FILESYSTEM';
128 // filesystem related error handling inside
1455d7ec 129 }
130 if ($aError['category'] & SQM_ERROR_SMTP) {
1dbd1527 131 $aErrorCategory[] = 'SMTP';
132 // smtp related error handling inside
1455d7ec 133 }
134 if ($aError['category'] & SQM_ERROR_LDAP) {
1dbd1527 135 $aErrorCategory[] = 'LDAP';
136 // ldap related error handling inside
1455d7ec 137 }
138 if ($aError['category'] & SQM_ERROR_DB) {
1dbd1527 139 $aErrorCategory[] = 'DATABASE';
140 // db related error handling inside
1455d7ec 141 }
142 if ($aError['category'] & SQM_ERROR_PLUGIN) {
1dbd1527 143 $aErrorCategory[] = 'PLUGIN';
144 do_hook_function('error_handler_plugin',$aError);
145 // plugin related error handling inside
1455d7ec 146 }
147 //if ($aError['category'] & SQM_ERROR_X) {
148 // $aErrorCategory[] = 'X';
149 // place holder for a new category
150 //}
151 }
152 unset($aErrorTemp);
153 } else {
154 $aError['message'] = $sErrStr;
155 $aErrorCategory[] = 'SQM_NOTICE';
156 }
157 break;
158 default: break;
1dbd1527 159 }
160
161 $aErrorTpl = array(
162 'type' => $iType,
163 'category' => $aErrorCategory,
164 'message' => $aError['message'],
165 'link' => $aError['link'],
166 'tip' => $aError['tip'],
167 'extra' => $aError['extra']);
168 // Add the notice/warning/error to the existing list of notices/warnings
169 $this->aErrors[] = $aErrorTpl;
170 $this->Template->assign('aErrors',$this->aErrors);
1455d7ec 171 }
1dbd1527 172
1455d7ec 173 // Show the error immediate in case of fatal errors
174 if ($iType == SQM_ERROR) {
175 $this->DisplayErrors();
176 exit(_("Terminating SquirrelMail due to a fatal error"));
177 }
178 }
179
180 /**
181 * Display the error array in the error template
182 * @return void
183 * @since 1.5.1
184 */
185 function DisplayErrors() {
186 if (count($this->aErrors)) {
187 $this->Template->display($this->TemplateName);
188 }
189 }
190}
191
192/**
193 * Custom Error handler for PHP version < 4.3.0 (set with set_error_handler() )
194 * @author Marc Groot Koerkamp
195 * @since 1.5.1
196 */
197function SquirrelMailErrorhandler($iErrNo, $sErrStr, $sErrFile, $iErrLine, $aContext) {
198 global $oTemplate;
199 static $oErrorHandler;
200 if (!isset($oErrorHandler)) {
201 $oErrorHandler = new ErrorHandler($oTemplate,'error_message.tpl');
202 }
203 $oErrorHandler->SquirrelMailErrorhandler($iErrNo, $sErrStr, $sErrFile, $iErrLine, $aContext);
204}
205
206/**
207 * Triggers an imap error. Utility function for sqm_trigger_error()
208 * @param integer $iErrNo error number defined in errors.php
209 * @param string $sRequest imap request string
210 * @param string $sResponse tagged imap response
211 * @param string $sMessage tagged imap response message
212 * @param array $aExtra optional associative array with extra error info
213 * @return void
214 * @author Marc Groot Koerkamp
215 * @since 1.5.1
216 */
217function sqm_trigger_imap_error($iErrNo,$sRequest,$sResponse, $sMessage, $aExtra=array()) {
218 $aError = array(
219 'REQUEST' => $sRequest,
220 'RESPONSE' => $sResponse,
221 'MESSAGE' => $sMessage);
222 $aError = array_merge($aExtra,$aError);
223 sqm_trigger_error($iErrNo,$aError);
224}
225
226/**
227 * Trigger an error.
228 * @param integer $iErrNo error number defined in errors.php
229 * @param array $aExtra optional associative array with extra error info
230 * @return void
231 * @author Marc Groot Koerkamp
232 * @since 1.5.1
233 */
234function sqm_trigger_error($iErrNo,$aExtra=array()) {
235 // Include the error definition file.
236 include_once(SM_PATH.'include/errors.php');
237 $iPhpErr = E_USER_NOTICE;
238 if (is_array($aErrors) && isset($aErrors[$iErrNo]['level'])) {
239 if (is_array($aExtra) && count($aExtra)) {
240 $aErrors[$iErrNo]['extra'] = $aExtra;
241 }
242 // because trigger_error can only handle a string argument for the error description
243 // we serialize the result.
244 $sErrString = serialize($aErrors[$iErrNo]);
245 $iPhpErr = $aErrors[$iErrNo]['level'];
246 } else {
247 $sErrString = "Error number <$iErrNo> does not exist, fix the code or update the errors.php file";
248 $iPhpErr = E_USER_ERROR;
249 }
250 trigger_error($sErrString, $iPhpErr);
251}
252?>