error.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <?php
  2. /**
  3. * error.class.php
  4. *
  5. * This contains the custom error handler for SquirrelMail.
  6. *
  7. * @copyright &copy; 2005-2007 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. # echo 'init error handler...';
  39. $this->TemplateName = $sTemplateFile;
  40. $this->Template =& $oTemplate;
  41. $this->aErrors = array();
  42. $this->header_sent = false;
  43. $this->delayed_errors = false;
  44. $this->Template->assign('delayed_errors', $this->delayed_errors);
  45. }
  46. /**
  47. * Sets the error template
  48. * @since 1.5.1
  49. */
  50. function SetTemplateFile($sTemplateFile) {
  51. $this->TemplateFile = $sTemplateFile;
  52. }
  53. /**
  54. * Sets if the page header is already sent
  55. * @since 1.5.1
  56. */
  57. function HeaderSent() {
  58. $this->header_sent = true;
  59. $this->Template->assign('header_sent', true);
  60. }
  61. /**
  62. * Turn on/off delayed error handling
  63. * @since 1.5.2
  64. */
  65. function setDelayedErrors ($val = true) {
  66. $this->delayed_errors = $val===true;
  67. $this->Template->assign('delayed_errors', $this->delayed_errors);
  68. }
  69. /**
  70. * Store errors generated in a previous script but couldn't be displayed
  71. * due to a header redirect. This requires storing of aDelayedErrors in the session
  72. * @param array $aDelayedErrors array with errors stored in the $this->aErrors format.
  73. * @since 1.5.1
  74. */
  75. function AssignDelayedErrors(&$aDelayedErrors) {
  76. $aErrors = array_merge($this->aErrors,$aDelayedErrors);
  77. $this->aErrors = $aErrors;
  78. $this->Template->assign('aErrors',$this->aErrors);
  79. $aDelayedErrors = false;
  80. }
  81. /**
  82. * Custom Error handler (set with set_error_handler() )
  83. * @private
  84. * @since 1.5.1
  85. */
  86. function SquirrelMailErrorhandler($iErrNo, $sErrStr, $sErrFile, $iErrLine, $aContext) {
  87. $aError = array(
  88. 'type' => SQM_NOTICE,// Error type, notice, warning or fatal error;
  89. 'category' => NULL, // SquirrelMail error category;
  90. 'message' => NULL, // Error display message;
  91. 'extra' => NULL, // Key value based array with extra error info;
  92. 'link' => NULL, // Link to help location;
  93. 'tip' => NULL // User tip.
  94. );
  95. $iType = NULL;
  96. $aErrorCategory = array();
  97. /**
  98. * Get current error reporting level.
  99. *
  100. * PHP 4.1.2 does not return current error reporting level in ini_get (php 5.1b3 and
  101. * 4.3.10 does). Retrieve current error reporting level while setting error reporting
  102. * to ini value and reset it to retrieved value.
  103. */
  104. $iCurErrLevel = error_reporting(ini_get('error_reporting'));
  105. error_reporting($iCurErrLevel);
  106. /**
  107. * Check error_reporting value before logging error.
  108. * Don't log errors that are disabled by @ (error_reporting = 0). Some SquirrelMail scripts
  109. * (sq_mb_list_encodings(), ldap function calls in functions/abook_ldap_server.php)
  110. * handle errors themselves and @ is used to disable generic php error messages.
  111. */
  112. if ($iErrNo & $iCurErrLevel) {
  113. /*
  114. * The following errors cannot be handled by a user defined error handler:
  115. * E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING
  116. */
  117. switch ($iErrNo) {
  118. case E_STRICT:
  119. $iType = (is_null($iType)) ? SQM_STRICT : $iType;
  120. case E_NOTICE:
  121. $iType = (is_null($iType)) ? SQM_NOTICE : $iType;
  122. case E_WARNING:
  123. $iType = (is_null($iType)) ? SQM_WARNING : $iType;
  124. $aErrorCategory[] = 'PHP';
  125. $aError['message'] = $sErrStr;
  126. $aError['extra'] = array(
  127. 'FILE' => $sErrFile,
  128. 'LINE' => $iErrLine) ;
  129. // what todo with $aContext?
  130. break;
  131. case E_USER_ERROR:
  132. $iType = (is_null($iType)) ? SQM_ERROR : $iType;
  133. case E_USER_NOTICE:
  134. $iType = (is_null($iType)) ? SQM_NOTICE : $iType;
  135. case E_USER_WARNING:
  136. $iType = (is_null($iType)) ? SQM_WARNING : $iType;
  137. if ($sErrFile == __FILE__) { // Error is triggered in this file and probably by sqm_trigger_error
  138. $aErrorTemp = @unserialize($sErrStr);
  139. if (!is_array($aErrorTemp)) {
  140. $aError['message'] = $sErrStr;
  141. $aErrorCategory[] = 'UNDEFINED';
  142. } else {
  143. $aError = array_merge($aError,$aErrorTemp);
  144. // special error handling below
  145. if ($aError['category'] & SQM_ERROR_IMAP) {
  146. $aErrorCategory[] = 'IMAP';
  147. // imap related error handling inside
  148. }
  149. if ($aError['category'] & SQM_ERROR_FS) {
  150. $aErrorCategory[] = 'FILESYSTEM';
  151. // filesystem related error handling inside
  152. }
  153. if ($aError['category'] & SQM_ERROR_SMTP) {
  154. $aErrorCategory[] = 'SMTP';
  155. // smtp related error handling inside
  156. }
  157. if ($aError['category'] & SQM_ERROR_LDAP) {
  158. $aErrorCategory[] = 'LDAP';
  159. // ldap related error handling inside
  160. }
  161. if ($aError['category'] & SQM_ERROR_DB) {
  162. $aErrorCategory[] = 'DATABASE';
  163. // db related error handling inside
  164. }
  165. if ($aError['category'] & SQM_ERROR_PLUGIN) {
  166. $aErrorCategory[] = 'PLUGIN';
  167. do_hook('error_handler_plugin', $aError);
  168. // plugin related error handling inside
  169. }
  170. //if ($aError['category'] & SQM_ERROR_X) {
  171. // $aErrorCategory[] = 'X';
  172. // place holder for a new category
  173. //}
  174. }
  175. unset($aErrorTemp);
  176. } else {
  177. $aError['message'] = $sErrStr;
  178. $aErrorCategory[] = 'SQM_NOTICE';
  179. }
  180. break;
  181. default: break;
  182. }
  183. /**
  184. * If delayed error handling is enabled, always record the location
  185. * and tag the error is delayed to make debugging easier.
  186. */
  187. if (isset($this->Template->values['delayed_errors']) && $this->Template->values['delayed_errors']) {
  188. $aErrorCategory[] = 'Delayed';
  189. $aError['extra'] = array(
  190. 'FILE' => $sErrFile,
  191. 'LINE' => $iErrLine) ;
  192. }
  193. $aErrorTpl = array(
  194. 'type' => $iType,
  195. 'category' => $aErrorCategory,
  196. 'message' => $aError['message'],
  197. 'link' => $aError['link'],
  198. 'tip' => $aError['tip'],
  199. 'extra' => $aError['extra']);
  200. // Add the notice/warning/error to the existing list of notices/warnings
  201. $this->aErrors[] = $aErrorTpl;
  202. $this->Template->assign('aErrors',$this->aErrors);
  203. }
  204. // Show the error immediate in case of fatal errors
  205. if ($iType == SQM_ERROR) {
  206. if (isset($this->Template->values['header_sent']) && !$this->Template->values['header_sent']) {
  207. // TODO replace this with template that can be assigned
  208. // UPDATE: displayHtmlHeader() no longer sends anything
  209. // directly to the browser itself and instead
  210. // displays all output through the template file
  211. // "protocol_header" as well as calls to the
  212. // template's header() method, so perhaps the
  213. // above TODO is alleviated?? (however, I don't fully
  214. // understand the problem behind the TODO comment myself (Paul))
  215. displayHtmlHeader(_("Error"),'',false);
  216. }
  217. $this->DisplayErrors();
  218. exit(_("Terminating SquirrelMail due to a fatal error"));
  219. }
  220. }
  221. /**
  222. * Force the delayed errors to be stored in the session in case
  223. * $this->displayErrors() never gets called, e.g. in compose.php
  224. */
  225. function saveDelayedErrors () {
  226. if($this->delayed_errors) {
  227. // Check for previous delayed errors...
  228. sqgetGlobalVar('delayed_errors', $delayed_errors, SQ_SESSION);
  229. if (is_array($delayed_errors)) {
  230. $this->AssignDelayedErrors($delayed_errors);
  231. sqsession_unregister("delayed_errors");
  232. }
  233. if (count($this->aErrors) > 0) {
  234. sqsession_register($this->aErrors,"delayed_errors");
  235. }
  236. }
  237. }
  238. /**
  239. * Display the error array in the error template
  240. * @return void
  241. * @since 1.5.1
  242. */
  243. function DisplayErrors() {
  244. // Check for delayed errors...
  245. if (!$this->delayed_errors) {
  246. sqgetGlobalVar('delayed_errors', $delayed_errors, SQ_SESSION);
  247. if (is_array($delayed_errors)) {
  248. $this->AssignDelayedErrors($delayed_errors);
  249. sqsession_unregister("delayed_errors");
  250. }
  251. }
  252. if (isset($this->Template->values['aErrors']) && count($this->Template->values['aErrors']) > 0) {
  253. foreach ($this->Template->values['aErrors'] as $err) {
  254. if (!in_array($err, $this->aErrors, true)) {
  255. $this->aErrors[] = $err;
  256. }
  257. }
  258. $this->Template->assign('aErrors',$this->aErrors);
  259. }
  260. if (count($this->aErrors) > 0) {
  261. if ($this->delayed_errors) {
  262. sqsession_register($this->aErrors,"delayed_errors");
  263. } else {
  264. $this->Template->display($this->TemplateName);
  265. }
  266. }
  267. }
  268. }
  269. /**
  270. * Custom Error handler for PHP version < 4.3.0 (set with set_error_handler() )
  271. * @author Marc Groot Koerkamp
  272. * @since 1.5.1
  273. */
  274. function SquirrelMailErrorhandler($iErrNo, $sErrStr, $sErrFile, $iErrLine, $aContext) {
  275. global $oTemplate;
  276. static $oErrorHandler;
  277. if (!isset($oErrorHandler)) {
  278. $oErrorHandler = new ErrorHandler($oTemplate,'error_message.tpl');
  279. }
  280. $oErrorHandler->SquirrelMailErrorhandler($iErrNo, $sErrStr, $sErrFile, $iErrLine, $aContext);
  281. }
  282. /**
  283. * Triggers an imap error. Utility function for sqm_trigger_error()
  284. * @param string $sErrNo error string defined in errors.php
  285. * @param string $sRequest imap request string
  286. * @param string $sResponse tagged imap response
  287. * @param string $sMessage tagged imap response message
  288. * @param array $aExtra optional associative array with extra error info
  289. * @return void
  290. * @author Marc Groot Koerkamp
  291. * @since 1.5.1
  292. */
  293. function sqm_trigger_imap_error($sErrNo,$sRequest,$sResponse, $sMessage, $aExtra=array()) {
  294. $aError = array(
  295. 'REQUEST' => $sRequest,
  296. 'RESPONSE' => $sResponse,
  297. 'MESSAGE' => $sMessage);
  298. $aError = array_merge($aExtra,$aError);
  299. sqm_trigger_error($sErrNo,$aError);
  300. }
  301. /**
  302. * Trigger an error.
  303. * @param string $sErrNo error string defined in errors.php
  304. * @param array $aExtra optional associative array with extra error info
  305. * @return void
  306. * @author Marc Groot Koerkamp
  307. * @since 1.5.1
  308. */
  309. function sqm_trigger_error($sErrNo,$aExtra=array()) {
  310. static $aErrors;
  311. if (!isset($aErrors)) {
  312. // Include the error definition file.
  313. include_once(SM_PATH.'include/errors.php');
  314. }
  315. $iPhpErr = E_USER_NOTICE;
  316. if (is_array($aErrors) && isset($aErrors[$sErrNo]['level'])) {
  317. if (is_array($aExtra) && count($aExtra)) {
  318. $aErrors[$sErrNo]['extra'] = $aExtra;
  319. }
  320. // because trigger_error can only handle a string argument for the error description
  321. // we serialize the result.
  322. $sErrString = serialize($aErrors[$sErrNo]);
  323. $iPhpErr = $aErrors[$sErrNo]['level'];
  324. } else {
  325. sm_print_r($aErrors);
  326. $sErrString = "Error <$sErrNo> does not exist, fix the code or update the errors.php file";
  327. $iPhpErr = E_USER_ERROR;
  328. }
  329. trigger_error($sErrString, $iPhpErr);
  330. }