mirror of
https://github.com/smarty-php/smarty.git
synced 2025-11-01 21:01:37 +01:00
Feature/php8 support (#629)
Adds support for PHP8.0, dropping support for PHP7.0 and below.
Backwards incompatible changes:
- Dropped support for php asp tags in templates (removed from php since php7.0)
- Dropped deprecated API calls that where only accessible through SmartyBC
- Dropped support for {php} and {include_php} tags and embedded PHP in templates. Embedded PHP will now be passed through as is.
- Removed all PHP_VERSION_ID and compare_version checks and conditional code blocks that are now no longer required
- Dropped deprecated SMARTY_RESOURCE_CHAR_SET and SMARTY_RESOURCE_DATE_FORMAT constants
- Dropped deprecated Smarty::muteExpectedErrors and Smarty::unmuteExpectedErrors API methods
- Dropped deprecated $smarty->getVariable() method. Use $smarty->getTemplateVars() instead.
- $smarty->registerResource() no longer accepts an array of callback functions
See the changelog for more details.
Switched CI from Travis to Github CI.
This commit is contained in:
@@ -1,61 +1,60 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Smarty error handler
|
||||
* Smarty error handler to fix new error levels in PHP8 for backwards compatibility
|
||||
*
|
||||
* @package Smarty
|
||||
* @subpackage PluginsInternal
|
||||
* @author Uwe Tews
|
||||
* @author Simon Wisselink
|
||||
*
|
||||
* @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()
|
||||
* Allows {$foo} where foo is unset.
|
||||
* @var bool
|
||||
*/
|
||||
private static $previousErrorHandler = null;
|
||||
public $allowUndefinedVars = true;
|
||||
|
||||
/**
|
||||
* Enable error handler to mute expected messages
|
||||
*
|
||||
* Allows {$foo.bar} where bar is unset and {$foo.bar1.bar2} where either bar1 or bar2 is unset.
|
||||
* @var bool
|
||||
*/
|
||||
public static function muteExpectedErrors()
|
||||
{
|
||||
public $allowUndefinedArrayKeys = true;
|
||||
|
||||
private $previousErrorHandler = null;
|
||||
|
||||
/**
|
||||
* Enable error handler to intercept errors
|
||||
*/
|
||||
public function activate() {
|
||||
/*
|
||||
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:
|
||||
Error muting is done because some people implemented custom error_handlers using
|
||||
https://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.
|
||||
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.
|
||||
*/
|
||||
$error_handler = array('Smarty_Internal_ErrorHandler', 'mutingErrorHandler');
|
||||
$previous = set_error_handler($error_handler);
|
||||
// avoid dead loops
|
||||
if ($previous !== $error_handler) {
|
||||
self::$previousErrorHandler = $previous;
|
||||
}
|
||||
$this->previousErrorHandler = set_error_handler([$this, 'handleError']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable error handler
|
||||
*/
|
||||
public function deactivate() {
|
||||
restore_error_handler();
|
||||
$this->previousErrorHandler = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Error Handler to mute expected messages
|
||||
*
|
||||
* @link http://php.net/set_error_handler
|
||||
* @link https://php.net/set_error_handler
|
||||
*
|
||||
* @param integer $errno Error level
|
||||
* @param $errstr
|
||||
@@ -65,49 +64,21 @@ class Smarty_Internal_ErrorHandler
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function mutingErrorHandler($errno, $errstr, $errfile, $errline, $errcontext = array())
|
||||
public function handleError($errno, $errstr, $errfile, $errline, $errcontext = [])
|
||||
{
|
||||
$_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),);
|
||||
}
|
||||
if ($this->allowUndefinedVars && $errstr == 'Attempt to read property "value" on null') {
|
||||
return; // suppresses this error
|
||||
}
|
||||
// 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;
|
||||
}
|
||||
|
||||
if ($this->allowUndefinedArrayKeys && preg_match(
|
||||
'/^(Undefined array key|Trying to access array offset on value of type null)/',
|
||||
$errstr
|
||||
)) {
|
||||
return; // suppresses this error
|
||||
}
|
||||
|
||||
// pass all other errors through to the previous error handler or to the default PHP error handler
|
||||
return $this->previousErrorHandler ?
|
||||
call_user_func($this->previousErrorHandler, $errno, $errstr, $errfile, $errline, $errcontext) : false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user