You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

smarty_internal_errorhandler.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * Smarty error handler
  4. *
  5. *
  6. * @package Smarty
  7. * @subpackage PluginsInternal
  8. * @author Uwe Tews
  9. *
  10. * @deprecated
  11. Smarty does no longer use @filemtime()
  12. */
  13. class Smarty_Internal_ErrorHandler
  14. {
  15. /**
  16. * contains directories outside of SMARTY_DIR that are to be muted by muteExpectedErrors()
  17. */
  18. public static $mutedDirectories = array();
  19. /**
  20. * error handler returned by set_error_handler() in self::muteExpectedErrors()
  21. */
  22. private static $previousErrorHandler = null;
  23. /**
  24. * Enable error handler to mute expected messages
  25. *
  26. * @return boolean
  27. */
  28. public static function muteExpectedErrors()
  29. {
  30. /*
  31. error muting is done because some people implemented custom error_handlers using
  32. http://php.net/set_error_handler and for some reason did not understand the following paragraph:
  33. It is important to remember that the standard PHP error handler is completely bypassed for the
  34. error types specified by error_types unless the callback function returns FALSE.
  35. error_reporting() settings will have no effect and your error handler will be called regardless -
  36. however you are still able to read the current value of error_reporting and act appropriately.
  37. Of particular note is that this value will be 0 if the statement that caused the error was
  38. prepended by the @ error-control operator.
  39. Smarty deliberately uses @filemtime() over file_exists() and filemtime() in some places. Reasons include
  40. - @filemtime() is almost twice as fast as using an additional file_exists()
  41. - between file_exists() and filemtime() a possible race condition is opened,
  42. which does not exist using the simple @filemtime() approach.
  43. */
  44. $error_handler = array('Smarty_Internal_ErrorHandler', 'mutingErrorHandler');
  45. $previous = set_error_handler($error_handler);
  46. // avoid dead loops
  47. if ($previous !== $error_handler) {
  48. self::$previousErrorHandler = $previous;
  49. }
  50. }
  51. /**
  52. * Error Handler to mute expected messages
  53. *
  54. * @link http://php.net/set_error_handler
  55. *
  56. * @param integer $errno Error level
  57. * @param $errstr
  58. * @param $errfile
  59. * @param $errline
  60. * @param $errcontext
  61. *
  62. * @return bool
  63. */
  64. public static function mutingErrorHandler($errno, $errstr, $errfile, $errline, $errcontext)
  65. {
  66. $_is_muted_directory = false;
  67. // add the SMARTY_DIR to the list of muted directories
  68. if (!isset(self::$mutedDirectories[ SMARTY_DIR ])) {
  69. $smarty_dir = realpath(SMARTY_DIR);
  70. if ($smarty_dir !== false) {
  71. self::$mutedDirectories[ SMARTY_DIR ] =
  72. array('file' => $smarty_dir, 'length' => strlen($smarty_dir),);
  73. }
  74. }
  75. // walk the muted directories and test against $errfile
  76. foreach (self::$mutedDirectories as $key => &$dir) {
  77. if (!$dir) {
  78. // resolve directory and length for speedy comparisons
  79. $file = realpath($key);
  80. if ($file === false) {
  81. // this directory does not exist, remove and skip it
  82. unset(self::$mutedDirectories[ $key ]);
  83. continue;
  84. }
  85. $dir = array('file' => $file, 'length' => strlen($file),);
  86. }
  87. if (!strncmp($errfile, $dir[ 'file' ], $dir[ 'length' ])) {
  88. $_is_muted_directory = true;
  89. break;
  90. }
  91. }
  92. // pass to next error handler if this error did not occur inside SMARTY_DIR
  93. // or the error was within smarty but masked to be ignored
  94. if (!$_is_muted_directory || ($errno && $errno & error_reporting())) {
  95. if (self::$previousErrorHandler) {
  96. return call_user_func(self::$previousErrorHandler,
  97. $errno,
  98. $errstr,
  99. $errfile,
  100. $errline,
  101. $errcontext);
  102. } else {
  103. return false;
  104. }
  105. }
  106. }
  107. }