Introduce error handling
[squirrelmail.git] / class / error.class.php
1 <?php
2 /**
3 * error.class.php
4 *
5 * Copyright (c) 2005 The SquirrelMail Project Team
6 * Licensed under the GNU GPL. For full terms see the file COPYING.
7 *
8 * This contains the custom error handler for SquirrelMail.
9 *
10 * @version $Id$
11 * @package squirrelmail
12 */
13
14 define('SQM_NOTICE',0);
15 define('SQM_WARNING',1);
16 define('SQM_ERROR',2);
17
18 /**
19 * Error Handler class
20 *
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
23 *
24 * @author Marc Groot Koerkamp
25 * @package squirrelmail
26 */
27 class ErrorHandler {
28
29 /**
30 * Constructor
31 * @param object $oTemplate Template object
32 * @param string $sTemplateFile Template containing the error template
33 * @since 1.5.1
34 */
35 function ErrorHandler(&$oTemplate, $sTemplateFile) {
36 $this->TemplateName = $sTemplateFile;
37 $this->Template =& $oTemplate;
38 $this->aErrors = array();
39 }
40
41 /**
42 * Sets the error template
43 * @since 1.5.1
44 */
45 function SetTemplateFile($sTemplateFile) {
46 $this->TemplateFile = $sTemplateFile;
47 }
48
49 /**
50 * Custom Error handler (set with set_error_handler() )
51 * @private
52 * @since 1.5.1
53 */
54 function SquirrelMailErrorhandler($iErrNo, $sErrStr, $sErrFile, $iErrLine, $aContext) {
55 $aError = array(
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.
62 );
63 $iType = NULL;
64 $aErrorCategory = array();
65 /*
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
68 */
69 switch ($iErrNo) {
70 case E_NOTICE:
71 $iType = (is_null($iType)) ? SQM_NOTICE : $iType;
72 case E_WARNING:
73 $iType = (is_null($iType)) ? SQM_WARNING : $iType;
74 $aErrorCategory[] = 'PHP';
75 $aError['message'] = $sErrStr;
76 $aError['extra'] = array(
77 'FILE' => $sErrFile,
78 'LINE' => $iErrLine) ;;
79 // what todo with $aContext?
80 break;
81 case E_USER_ERROR:
82 $iType = (is_null($iType)) ? SQM_ERROR : $iType;
83 case E_USER_NOTICE:
84 $iType = (is_null($iType)) ? SQM_NOTICE : $iType;
85 case E_USER_WARNING:
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';
92 } else {
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
98 }
99 if ($aError['category'] & SQM_ERROR_FS) {
100 $aErrorCategory[] = 'FILESYSTEM';
101 // filesystem related error handling inside
102 }
103 if ($aError['category'] & SQM_ERROR_SMTP) {
104 $aErrorCategory[] = 'SMTP';
105 // smtp related error handling inside
106 }
107 if ($aError['category'] & SQM_ERROR_LDAP) {
108 $aErrorCategory[] = 'LDAP';
109 // ldap related error handling inside
110 }
111 if ($aError['category'] & SQM_ERROR_DB) {
112 $aErrorCategory[] = 'DATABASE';
113 // db related error handling inside
114 }
115 if ($aError['category'] & SQM_ERROR_PLUGIN) {
116 $aErrorCategory[] = 'PLUGIN';
117 do_hook_function('error_handler_plugin',$aError);
118 // plugin related error handling inside
119 }
120 //if ($aError['category'] & SQM_ERROR_X) {
121 // $aErrorCategory[] = 'X';
122 // place holder for a new category
123 //}
124 }
125 unset($aErrorTemp);
126 } else {
127 $aError['message'] = $sErrStr;
128 $aErrorCategory[] = 'SQM_NOTICE';
129 }
130 break;
131 default: break;
132 }
133 $aErrorTpl = array(
134 'type' => $iType,
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"));
147 }
148 }
149
150 /**
151 * Display the error array in the error template
152 * @return void
153 * @since 1.5.1
154 */
155 function DisplayErrors() {
156 if (count($this->aErrors)) {
157 $this->Template->display($this->TemplateName);
158 }
159 }
160 }
161
162 /**
163 * Custom Error handler for PHP version < 4.3.0 (set with set_error_handler() )
164 * @author Marc Groot Koerkamp
165 * @since 1.5.1
166 */
167 function SquirrelMailErrorhandler($iErrNo, $sErrStr, $sErrFile, $iErrLine, $aContext) {
168 global $oTemplate;
169 static $oErrorHandler;
170 if (!isset($oErrorHandler)) {
171 $oErrorHandler = new ErrorHandler($oTemplate,'error_message.tpl');
172 }
173 $oErrorHandler->SquirrelMailErrorhandler($iErrNo, $sErrStr, $sErrFile, $iErrLine, $aContext);
174 }
175
176 /**
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
183 * @return void
184 * @author Marc Groot Koerkamp
185 * @since 1.5.1
186 */
187 function sqm_trigger_imap_error($iErrNo,$sRequest,$sResponse, $sMessage, $aExtra=array()) {
188 $aError = array(
189 'REQUEST' => $sRequest,
190 'RESPONSE' => $sResponse,
191 'MESSAGE' => $sMessage);
192 $aError = array_merge($aExtra,$aError);
193 sqm_trigger_error($iErrNo,$aError);
194 }
195
196 /**
197 * Trigger an error.
198 * @param integer $iErrNo error number defined in errors.php
199 * @param array $aExtra optional associative array with extra error info
200 * @return void
201 * @author Marc Groot Koerkamp
202 * @since 1.5.1
203 */
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;
211 }
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'];
216 } else {
217 $sErrString = "Error number <$iErrNo> does not exist, fix the code or update the errors.php file";
218 $iPhpErr = E_USER_ERROR;
219 }
220 trigger_error($sErrString, $iPhpErr);
221 }
222 ?>