Compare commits

...

9 Commits

Author SHA1 Message Date
Simon Wisselink 9e443fdacd Fix regex matching for slightly different error message for php7.1 2022-11-24 09:28:59 +01:00
Simon Wisselink 3e82c271d4 Removed 2 tests that produce inconsistent results between PHP7.x versions. 2022-11-24 09:20:14 +01:00
Simon Wisselink dad8005b64 Treat undefined vars and array access of a null or false variables
equivalent across all supported PHP versions
2022-11-24 00:35:06 +01:00
Simon Wisselink dee0c3ffd4 Added test to see what changed exactly 2022-11-23 23:56:08 +01:00
Simon Wisselink 044647bd71 Also mute php7 notices for undefined array indexes when muteUndefinedOrNullWarnings is activated. (#829)
Fixes #736
2022-11-23 23:26:24 +01:00
Simon Wisselink c02e9e135e Merge branch 'release/4.3.0' 2022-11-22 22:47:32 +01:00
Simon Wisselink 67ab8f6879 version bump 2022-11-22 22:47:30 +01:00
Simon Wisselink 773b3b4b7c Change file permissions for directories and respect umask for written files. (#828)
Fixes #548
Fixes #819
2022-11-22 22:31:54 +01:00
Hunman 613c5d691c Make SmartyCompilerException play nicer with error handler libraries (#782)
* Make SmartyCompilerException play nicer with error handler libraries

Added a new constructor, which accepts a filename and a line number too
(similar to ErrorException, except no severity parameter)
This way error handlers will display the correct file's correct line as
the source of the exception, instead of the template's line in the
core of the parser (php file)

Kept the __toString() method, but removed $source, $desc, and $template

* Revert the breaking changes
2022-11-22 21:58:13 +01:00
7 changed files with 70 additions and 17 deletions
+11 -2
View File
@@ -6,14 +6,22 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Fixed
- `$smarty->muteUndefinedOrNullWarnings()` now also mutes PHP7 notices for undefined array indexes [#736](https://github.com/smarty-php/smarty/issues/736)
- `$smarty->muteUndefinedOrNullWarnings()` now treats undefined vars and array access of a null or false variables
equivalent across all supported PHP versions
## [4.3.0] - 2022-11-22
### Added
- PHP8.2 compatibility [#775](https://github.com/smarty-php/smarty/pull/775)
### Changed
- Include docs and demo in the releases [#799](https://github.com/smarty-php/smarty/issues/799)
- Using PHP functions as modifiers now triggers a deprecation notice because we will drop support for this in the next major release [#813](https://github.com/smarty-php/smarty/issues/813)
- Dropped remaining references to removed PHP-support in Smarty 4 from docs, lexer and security class. [#816](https://github.com/smarty-php/smarty/issues/816)
- Dropped remaining references to removed PHP-support in Smarty 4 from docs, lexer and security class. [#816](https://github.com/smarty-php/smarty/issues/816)
- Support umask when writing (template) files and set dir permissions to 777 [#548](https://github.com/smarty-php/smarty/issues/548) [#819](https://github.com/smarty-php/smarty/issues/819)
### Fixed
- Output buffer is now cleaned for internal PHP errors as well, not just for Exceptions [#514](https://github.com/smarty-php/smarty/issues/514)
- Fixed recursion and out of memory errors when caching in complicated template set-ups using inheritance and includes [#801](https://github.com/smarty-php/smarty/pull/801)
@@ -24,6 +32,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Adapt Smarty upper/lower functions to be codesafe (e.g. for Turkish locale) [#586](https://github.com/smarty-php/smarty/pull/586)
- Bug fix for underscore and limited length in template name in custom resources [#581](https://github.com/smarty-php/smarty/pull/581)
## [4.2.1] - 2022-09-14
### Security
+3 -4
View File
@@ -107,7 +107,7 @@ class Smarty extends Smarty_Internal_TemplateBase
/**
* smarty version
*/
const SMARTY_VERSION = '4.2.1';
const SMARTY_VERSION = '4.3.0';
/**
* define variable scopes
*/
@@ -1386,8 +1386,7 @@ class Smarty extends Smarty_Internal_TemplateBase
}
/**
* Activates PHP7 compatibility mode:
* - converts E_WARNINGS for "undefined array key" and "trying to read property of null" errors to E_NOTICE
* Mutes errors for "undefined index", "undefined array key" and "trying to read property of null".
*
* @void
*/
@@ -1396,7 +1395,7 @@ class Smarty extends Smarty_Internal_TemplateBase
}
/**
* Indicates if PHP7 compatibility mode is set.
* Indicates if Smarty will mute errors for "undefined index", "undefined array key" and "trying to read property of null".
* @bool
*/
public function isMutingUndefinedOrNullWarnings(): bool {
@@ -66,12 +66,16 @@ class Smarty_Internal_ErrorHandler
*/
public function handleError($errno, $errstr, $errfile, $errline, $errcontext = [])
{
if ($this->allowUndefinedVars && $errstr == 'Attempt to read property "value" on null') {
if ($this->allowUndefinedVars && preg_match(
'/^(Attempt to read property "value" on null|Trying to get property (\'value\' )?of non-object)/',
$errstr
)) {
return; // suppresses this error
}
if ($this->allowUndefinedArrayKeys && preg_match(
'/^(Undefined array key|Trying to access array offset on value of type null)/',
'/^(Undefined index|Undefined array key|Trying to access array offset on value of type (null|bool))/',
$errstr
)) {
return; // suppresses this error
@@ -29,7 +29,6 @@ class Smarty_Internal_Runtime_WriteFile
{
$_error_reporting = error_reporting();
error_reporting($_error_reporting & ~E_NOTICE & ~E_WARNING);
$old_umask = umask(0);
$_dirpath = dirname($_filepath);
// if subdirs, create dir structure
if ($_dirpath !== '.') {
@@ -37,7 +36,7 @@ class Smarty_Internal_Runtime_WriteFile
// loop if concurrency problem occurs
// see https://bugs.php.net/bug.php?id=35326
while (!is_dir($_dirpath)) {
if (@mkdir($_dirpath, 0771, true)) {
if (@mkdir($_dirpath, 0777, true)) {
break;
}
clearstatcache();
@@ -85,8 +84,7 @@ class Smarty_Internal_Runtime_WriteFile
throw new SmartyException("unable to write file {$_filepath}");
}
// set file permissions
chmod($_filepath, 0644);
umask($old_umask);
@chmod($_filepath, 0666 & ~umask());
error_reporting($_error_reporting);
return true;
}
@@ -1131,8 +1131,12 @@ abstract class Smarty_Internal_TemplateCompilerBase
echo ob_get_clean();
flush();
}
$e = new SmartyCompilerException($error_text);
$e->setLine($line);
$e = new SmartyCompilerException(
$error_text,
0,
$this->template->source->filepath,
$line
);
$e->source = trim(preg_replace('![\t\r\n]+!', ' ', $match[ $line - 1 ]));
$e->desc = $args;
$e->template = $this->template->source->filepath;
@@ -7,6 +7,33 @@
*/
class SmartyCompilerException extends SmartyException
{
/**
* The constructor of the exception
*
* @param string $message The Exception message to throw.
* @param int $code The Exception code.
* @param string|null $filename The filename where the exception is thrown.
* @param int|null $line The line number where the exception is thrown.
* @param Throwable|null $previous The previous exception used for the exception chaining.
*/
public function __construct(
string $message = "",
int $code = 0,
?string $filename = null,
?int $line = null,
Throwable $previous = null
) {
parent::__construct($message, $code, $previous);
// These are optional parameters, should be be overridden only when present!
if ($filename) {
$this->file = $filename;
}
if ($line) {
$this->line = $line;
}
}
/**
* @return string
*/
@@ -22,6 +49,7 @@ class SmartyCompilerException extends SmartyException
{
$this->line = $line;
}
/**
* The template source snippet relating to the error
*
@@ -88,14 +88,12 @@ class UndefinedTemplateVarTest extends PHPUnit_Smarty
}
public function testUndefinedSimpleVar() {
$this->smarty->setErrorReporting(E_ALL & ~E_NOTICE);
$this->smarty->muteUndefinedOrNullWarnings();
$tpl = $this->smarty->createTemplate('string:a{if $undef}def{/if}b');
$this->assertEquals("ab", $this->smarty->fetch($tpl));
}
public function testUndefinedArrayIndex() {
$this->smarty->setErrorReporting(E_ALL & ~E_NOTICE);
$this->smarty->muteUndefinedOrNullWarnings();
$tpl = $this->smarty->createTemplate('string:a{if $ar.undef}def{/if}b');
$tpl->assign('ar', []);
@@ -103,7 +101,6 @@ class UndefinedTemplateVarTest extends PHPUnit_Smarty
}
public function testUndefinedArrayIndexDeep() {
$this->smarty->setErrorReporting(E_ALL & ~E_NOTICE);
$this->smarty->muteUndefinedOrNullWarnings();
$tpl = $this->smarty->createTemplate('string:a{if $ar.undef.nope.neither}def{/if}b');
$tpl->assign('ar', []);
@@ -133,5 +130,19 @@ class UndefinedTemplateVarTest extends PHPUnit_Smarty
$this->assertTrue($exceptionThrown);
}
public function testUsingNullAsAnArrayIsMuted() {
$this->smarty->setErrorReporting(E_ALL);
$this->smarty->muteUndefinedOrNullWarnings();
$tpl = $this->smarty->createTemplate('string:a{if $undef.k}def{/if}b');
$this->assertEquals("ab", $this->smarty->fetch($tpl));
}
public function testUsingFalseAsAnArrayIsMuted() {
$this->smarty->setErrorReporting(E_ALL);
$this->smarty->muteUndefinedOrNullWarnings();
$tpl = $this->smarty->createTemplate('string:a{if $nottrue.k}def{/if}b');
$this->smarty->assign('nottrue', false);
$this->assertEquals("ab", $this->smarty->fetch($tpl));
}
}