error.class.php 9.7 KB

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