error.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <?php
  2. /**
  3. * error.class.php
  4. *
  5. * This contains the custom error handler for SquirrelMail.
  6. *
  7. * @copyright &copy; 2005-2006 The SquirrelMail Project Team
  8. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  9. * @version $Id$
  10. * @package squirrelmail
  11. */
  12. /** Used defines */
  13. define('SQM_NOTICE',0);
  14. define('SQM_WARNING',1);
  15. define('SQM_ERROR',2);
  16. define('SQM_STRICT',3);
  17. // php5 E_STRICT constant (compatibility with php4)
  18. if (! defined('E_STRICT')) define('E_STRICT',2048);
  19. // Set docref_root (fixes URLs that link to php manual)
  20. if (ini_get('docref_root')=='') ini_set('docref_root','http://www.php.net/');
  21. /**
  22. * Error Handler class
  23. *
  24. * This class contains a custom error handler in order to display
  25. * user notices/warnings/errors and php notices and warnings in a template
  26. *
  27. * @author Marc Groot Koerkamp
  28. * @package squirrelmail
  29. */
  30. class ErrorHandler {
  31. /**
  32. * Constructor
  33. * @param object $oTemplate Template object
  34. * @param string $sTemplateFile Template containing the error template
  35. * @since 1.5.1
  36. */
  37. function ErrorHandler(&$oTemplate, $sTemplateFile) {
  38. $this->TemplateName = $sTemplateFile;
  39. $this->Template =& $oTemplate;
  40. $this->aErrors = array();
  41. $this->header_sent = false;
  42. }
  43. /**
  44. * Sets the error template
  45. * @since 1.5.1
  46. */
  47. function SetTemplateFile($sTemplateFile) {
  48. $this->TemplateFile = $sTemplateFile;
  49. }
  50. /**
  51. * Sets if the page header is already sent
  52. * @since 1.5.1
  53. */
  54. function HeaderSent() {
  55. $this->header_sent = true;
  56. }
  57. /**
  58. * Store errors generated in a previous script but couldn't be displayed
  59. * due to a header redirect. This requires storing of aDelayedErrors in the session
  60. * @param array $aDelayedErrors array with errors stored in the $this->aErrors format.
  61. * @since 1.5.1
  62. */
  63. function AssignDelayedErrors(&$aDelayedErrors) {
  64. $aErrors = array_merge($this->aErrors,$aDelayedErrors);
  65. $this->aErrors = $aErrors;
  66. $this->Template->assign('aErrors',$this->aErrors);
  67. $aDelayedErrors = false;
  68. }
  69. /**
  70. * Custom Error handler (set with set_error_handler() )
  71. * @private
  72. * @since 1.5.1
  73. */
  74. function SquirrelMailErrorhandler($iErrNo, $sErrStr, $sErrFile, $iErrLine, $aContext) {
  75. $aError = array(
  76. 'type' => SQM_NOTICE,// Error type, notice, warning or fatal error;
  77. 'category' => NULL, // SquirrelMail error category;
  78. 'message' => NULL, // Error display message;
  79. 'extra' => NULL, // Key value based array with extra error info;
  80. 'link' => NULL, // Link to help location;
  81. 'tip' => NULL // User tip.
  82. );
  83. $iType = NULL;
  84. $aErrorCategory = array();
  85. /**
  86. * Get current error reporting level.
  87. *
  88. * PHP 4.1.2 does not return current error reporting level in ini_get (php 5.1b3 and
  89. * 4.3.10 does). Retrieve current error reporting level while setting error reporting
  90. * to ini value and reset it to retrieved value.
  91. */
  92. $iCurErrLevel = error_reporting(ini_get('error_reporting'));
  93. error_reporting($iCurErrLevel);
  94. /**
  95. * Check error_reporting value before logging error.
  96. * Don't log errors that are disabled by @ (error_reporting = 0). Some SquirrelMail scripts
  97. * (sq_mb_list_encodings(), ldap function calls in functions/abook_ldap_server.php)
  98. * handle errors themselves and @ is used to disable generic php error messages.
  99. */
  100. if ($iErrNo & $iCurErrLevel) {
  101. /*
  102. * The following errors cannot be handled by a user defined error handler:
  103. * E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING
  104. */
  105. switch ($iErrNo) {
  106. case E_STRICT:
  107. $iType = (is_null($iType)) ? SQM_STRICT : $iType;
  108. case E_NOTICE:
  109. $iType = (is_null($iType)) ? SQM_NOTICE : $iType;
  110. case E_WARNING:
  111. $iType = (is_null($iType)) ? SQM_WARNING : $iType;
  112. $aErrorCategory[] = 'PHP';
  113. $aError['message'] = $sErrStr;
  114. $aError['extra'] = array(
  115. 'FILE' => $sErrFile,
  116. 'LINE' => $iErrLine) ;;
  117. // what todo with $aContext?
  118. break;
  119. case E_USER_ERROR:
  120. $iType = (is_null($iType)) ? SQM_ERROR : $iType;
  121. case E_USER_NOTICE:
  122. $iType = (is_null($iType)) ? SQM_NOTICE : $iType;
  123. case E_USER_WARNING:
  124. $iType = (is_null($iType)) ? SQM_WARNING : $iType;
  125. if ($sErrFile == __FILE__) { // Error is triggered in this file and probably by sqm_trigger_error
  126. $aErrorTemp = @unserialize($sErrStr);
  127. if (!is_array($aErrorTemp)) {
  128. $aError['message'] = $sErrStr;
  129. $aErrorCategory[] = 'UNDEFINED';
  130. } else {
  131. $aError = array_merge($aError,$aErrorTemp);
  132. // special error handling below
  133. if ($aError['category'] & SQM_ERROR_IMAP) {
  134. $aErrorCategory[] = 'IMAP';
  135. // imap related error handling inside
  136. }
  137. if ($aError['category'] & SQM_ERROR_FS) {
  138. $aErrorCategory[] = 'FILESYSTEM';
  139. // filesystem related error handling inside
  140. }
  141. if ($aError['category'] & SQM_ERROR_SMTP) {
  142. $aErrorCategory[] = 'SMTP';
  143. // smtp related error handling inside
  144. }
  145. if ($aError['category'] & SQM_ERROR_LDAP) {
  146. $aErrorCategory[] = 'LDAP';
  147. // ldap related error handling inside
  148. }
  149. if ($aError['category'] & SQM_ERROR_DB) {
  150. $aErrorCategory[] = 'DATABASE';
  151. // db related error handling inside
  152. }
  153. if ($aError['category'] & SQM_ERROR_PLUGIN) {
  154. $aErrorCategory[] = 'PLUGIN';
  155. do_hook_function('error_handler_plugin',$aError);
  156. // plugin related error handling inside
  157. }
  158. //if ($aError['category'] & SQM_ERROR_X) {
  159. // $aErrorCategory[] = 'X';
  160. // place holder for a new category
  161. //}
  162. }
  163. unset($aErrorTemp);
  164. } else {
  165. $aError['message'] = $sErrStr;
  166. $aErrorCategory[] = 'SQM_NOTICE';
  167. }
  168. break;
  169. default: break;
  170. }
  171. $aErrorTpl = array(
  172. 'type' => $iType,
  173. 'category' => $aErrorCategory,
  174. 'message' => $aError['message'],
  175. 'link' => $aError['link'],
  176. 'tip' => $aError['tip'],
  177. 'extra' => $aError['extra']);
  178. // Add the notice/warning/error to the existing list of notices/warnings
  179. $this->aErrors[] = $aErrorTpl;
  180. $this->Template->assign('aErrors',$this->aErrors);
  181. }
  182. // Show the error immediate in case of fatal errors
  183. if ($iType == SQM_ERROR) {
  184. if (!$this->header_sent) {
  185. // TODO replace this with template that can be assigned
  186. displayHtmlHeader(_("Error"),'',false);
  187. }
  188. $this->DisplayErrors();
  189. exit(_("Terminating SquirrelMail due to a fatal error"));
  190. }
  191. }
  192. /**
  193. * Display the error array in the error template
  194. * @return void
  195. * @since 1.5.1
  196. */
  197. function DisplayErrors() {
  198. if (count($this->aErrors)) {
  199. $this->Template->display($this->TemplateName);
  200. }
  201. }
  202. }
  203. /**
  204. * Custom Error handler for PHP version < 4.3.0 (set with set_error_handler() )
  205. * @author Marc Groot Koerkamp
  206. * @since 1.5.1
  207. */
  208. function SquirrelMailErrorhandler($iErrNo, $sErrStr, $sErrFile, $iErrLine, $aContext) {
  209. global $oTemplate;
  210. static $oErrorHandler;
  211. if (!isset($oErrorHandler)) {
  212. $oErrorHandler = new ErrorHandler($oTemplate,'error_message.tpl');
  213. }
  214. $oErrorHandler->SquirrelMailErrorhandler($iErrNo, $sErrStr, $sErrFile, $iErrLine, $aContext);
  215. }
  216. /**
  217. * Triggers an imap error. Utility function for sqm_trigger_error()
  218. * @param string $sErrNo error string defined in errors.php
  219. * @param string $sRequest imap request string
  220. * @param string $sResponse tagged imap response
  221. * @param string $sMessage tagged imap response message
  222. * @param array $aExtra optional associative array with extra error info
  223. * @return void
  224. * @author Marc Groot Koerkamp
  225. * @since 1.5.1
  226. */
  227. function sqm_trigger_imap_error($sErrNo,$sRequest,$sResponse, $sMessage, $aExtra=array()) {
  228. $aError = array(
  229. 'REQUEST' => $sRequest,
  230. 'RESPONSE' => $sResponse,
  231. 'MESSAGE' => $sMessage);
  232. $aError = array_merge($aExtra,$aError);
  233. sqm_trigger_error($sErrNo,$aError);
  234. }
  235. /**
  236. * Trigger an error.
  237. * @param string $sErrNo error string defined in errors.php
  238. * @param array $aExtra optional associative array with extra error info
  239. * @return void
  240. * @author Marc Groot Koerkamp
  241. * @since 1.5.1
  242. */
  243. function sqm_trigger_error($sErrNo,$aExtra=array()) {
  244. static $aErrors;
  245. if (!isset($aErrors)) {
  246. // Include the error definition file.
  247. include_once(SM_PATH.'include/errors.php');
  248. }
  249. $iPhpErr = E_USER_NOTICE;
  250. if (is_array($aErrors) && isset($aErrors[$sErrNo]['level'])) {
  251. if (is_array($aExtra) && count($aExtra)) {
  252. $aErrors[$sErrNo]['extra'] = $aExtra;
  253. }
  254. // because trigger_error can only handle a string argument for the error description
  255. // we serialize the result.
  256. $sErrString = serialize($aErrors[$sErrNo]);
  257. $iPhpErr = $aErrors[$sErrNo]['level'];
  258. } else {
  259. sm_print_r($aErrors);
  260. $sErrString = "Error <$sErrNo> does not exist, fix the code or update the errors.php file";
  261. $iPhpErr = E_USER_ERROR;
  262. }
  263. trigger_error($sErrString, $iPhpErr);
  264. }
  265. ?>