mirror of
				https://github.com/smarty-php/smarty.git
				synced 2025-11-04 06:11:37 +01:00 
			
		
		
		
	* Set $errcontext argument optional to support PHP 8 - Argument is optional and deprecated in PHP 7.2 * Getting ready for PHP8, handling changed error levels/handlers mostly * php5 compat syntax * Updated UndefinedTemplateVarTest for PHP8 (and disabled a check for PHP<5.6) and re-enabled php:nightly in travis config * Attempt to fix travis runs for (almost) all php versions supported * Fix unit tests for php8, force composer to think we are still php7 to pick a supported phpunit and being less specific about an error msg because PHP8 is in active development and the exact wording is changing. * Fixed a unit test that accidentally passed on phpunit < 7 because of sloppy string comparison. * changelog * run travis in xenial where possible for latest php versions. Fix unit tests from freakingo over inconsistent error messages in php8-beta. * Incorporated AnrDaemons suggestions, making composer figure out the required phpunit version instead of specifying it explicitly and removing a unneeded error supression (@). Co-authored-by: Jorge Sá Pereira <me@jorgesapereira.com>
		
			
				
	
	
		
			114 lines
		
	
	
		
			4.2 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			114 lines
		
	
	
		
			4.2 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
 | 
						|
/**
 | 
						|
 * Smarty error handler
 | 
						|
 *
 | 
						|
 * @package    Smarty
 | 
						|
 * @subpackage PluginsInternal
 | 
						|
 * @author     Uwe Tews
 | 
						|
 *
 | 
						|
 * @deprecated
 | 
						|
Smarty does no longer use @filemtime()
 | 
						|
 */
 | 
						|
class Smarty_Internal_ErrorHandler
 | 
						|
{
 | 
						|
    /**
 | 
						|
     * contains directories outside of SMARTY_DIR that are to be muted by muteExpectedErrors()
 | 
						|
     */
 | 
						|
    public static $mutedDirectories = array();
 | 
						|
 | 
						|
    /**
 | 
						|
     * error handler returned by set_error_handler() in self::muteExpectedErrors()
 | 
						|
     */
 | 
						|
    private static $previousErrorHandler = null;
 | 
						|
 | 
						|
    /**
 | 
						|
     * Enable error handler to mute expected messages
 | 
						|
     *
 | 
						|
     */
 | 
						|
    public static function muteExpectedErrors()
 | 
						|
    {
 | 
						|
        /*
 | 
						|
            error muting is done because some people implemented custom error_handlers using
 | 
						|
            http://php.net/set_error_handler and for some reason did not understand the following paragraph:
 | 
						|
 | 
						|
                It is important to remember that the standard PHP error handler is completely bypassed for the
 | 
						|
                error types specified by error_types unless the callback function returns FALSE.
 | 
						|
                error_reporting() settings will have no effect and your error handler will be called regardless -
 | 
						|
                however you are still able to read the current value of error_reporting and act appropriately.
 | 
						|
                Of particular note is that this value will be 0 if the statement that caused the error was
 | 
						|
                prepended by the @ error-control operator.
 | 
						|
 | 
						|
            Smarty deliberately uses @filemtime() over file_exists() and filemtime() in some places. Reasons include
 | 
						|
                - @filemtime() is almost twice as fast as using an additional file_exists()
 | 
						|
                - between file_exists() and filemtime() a possible race condition is opened,
 | 
						|
                  which does not exist using the simple @filemtime() approach.
 | 
						|
        */
 | 
						|
        $error_handler = array('Smarty_Internal_ErrorHandler', 'mutingErrorHandler');
 | 
						|
        $previous = set_error_handler($error_handler);
 | 
						|
        // avoid dead loops
 | 
						|
        if ($previous !== $error_handler) {
 | 
						|
            self::$previousErrorHandler = $previous;
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * Error Handler to mute expected messages
 | 
						|
     *
 | 
						|
     * @link http://php.net/set_error_handler
 | 
						|
     *
 | 
						|
     * @param integer $errno Error level
 | 
						|
     * @param         $errstr
 | 
						|
     * @param         $errfile
 | 
						|
     * @param         $errline
 | 
						|
     * @param         $errcontext
 | 
						|
     *
 | 
						|
     * @return bool
 | 
						|
     */
 | 
						|
    public static function mutingErrorHandler($errno, $errstr, $errfile, $errline, $errcontext = array())
 | 
						|
    {
 | 
						|
        $_is_muted_directory = false;
 | 
						|
        // add the SMARTY_DIR to the list of muted directories
 | 
						|
        if (!isset(self::$mutedDirectories[ SMARTY_DIR ])) {
 | 
						|
            $smarty_dir = realpath(SMARTY_DIR);
 | 
						|
            if ($smarty_dir !== false) {
 | 
						|
                self::$mutedDirectories[ SMARTY_DIR ] =
 | 
						|
                    array('file' => $smarty_dir, 'length' => strlen($smarty_dir),);
 | 
						|
            }
 | 
						|
        }
 | 
						|
        // walk the muted directories and test against $errfile
 | 
						|
        foreach (self::$mutedDirectories as $key => &$dir) {
 | 
						|
            if (!$dir) {
 | 
						|
                // resolve directory and length for speedy comparisons
 | 
						|
                $file = realpath($key);
 | 
						|
                if ($file === false) {
 | 
						|
                    // this directory does not exist, remove and skip it
 | 
						|
                    unset(self::$mutedDirectories[ $key ]);
 | 
						|
                    continue;
 | 
						|
                }
 | 
						|
                $dir = array('file' => $file, 'length' => strlen($file),);
 | 
						|
            }
 | 
						|
            if (!strncmp($errfile, $dir[ 'file' ], $dir[ 'length' ])) {
 | 
						|
                $_is_muted_directory = true;
 | 
						|
                break;
 | 
						|
            }
 | 
						|
        }
 | 
						|
        // pass to next error handler if this error did not occur inside SMARTY_DIR
 | 
						|
        // or the error was within smarty but masked to be ignored
 | 
						|
        if (!$_is_muted_directory || ($errno && $errno & error_reporting())) {
 | 
						|
            if (self::$previousErrorHandler) {
 | 
						|
                return call_user_func(
 | 
						|
                    self::$previousErrorHandler,
 | 
						|
                    $errno,
 | 
						|
                    $errstr,
 | 
						|
                    $errfile,
 | 
						|
                    $errline,
 | 
						|
                    $errcontext
 | 
						|
                );
 | 
						|
            } else {
 | 
						|
                return false;
 | 
						|
            }
 | 
						|
        }
 | 
						|
    }
 | 
						|
}
 |