From 2d1905cd886ad18da65346d439333e9d430edbcf Mon Sep 17 00:00:00 2001 From: uwetews Date: Mon, 3 Sep 2018 01:18:26 +0200 Subject: [PATCH] - bugfix {foreach} using new style property access like {$item@property} on Smarty 2 style named foreach loop could produce errors https://github.com/smarty-php/smarty/issues/484 --- change_log.txt | 6 +- libs/Smarty.class.php | 2 +- .../smarty_internal_compile_foreach.php | 5 +- ...nternal_compile_private_foreachsection.php | 68 +++++++++---------- 4 files changed, 42 insertions(+), 39 deletions(-) diff --git a/change_log.txt b/change_log.txt index 49850832..539c09bd 100644 --- a/change_log.txt +++ b/change_log.txt @@ -1,4 +1,8 @@ -===== 3.1.33-dev-10 ===== +===== 3.1.33-dev-12 ===== +03.09.2018 + - bugfix {foreach} using new style property access like {$item@property} on + Smarty 2 style named foreach loop could produce errors https://github.com/smarty-php/smarty/issues/484 + 31.08.2018 - bugfix some custom left and right delimiters like '{^' '^}' did not work https://github.com/smarty-php/smarty/issues/450 https://github.com/smarty-php/smarty/pull/482 diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index 9a05d0bc..348d6b53 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -112,7 +112,7 @@ class Smarty extends Smarty_Internal_TemplateBase /** * smarty version */ - const SMARTY_VERSION = '3.1.33-dev-11'; + const SMARTY_VERSION = '3.1.33-dev-12'; /** * define variable scopes */ diff --git a/libs/sysplugins/smarty_internal_compile_foreach.php b/libs/sysplugins/smarty_internal_compile_foreach.php index 45965788..78b45ea7 100644 --- a/libs/sysplugins/smarty_internal_compile_foreach.php +++ b/libs/sysplugins/smarty_internal_compile_foreach.php @@ -325,8 +325,9 @@ class Smarty_Internal_Compile_Foreachclose extends Smarty_Internal_CompileBase if ($compiler->nocache) { $compiler->tag_nocache = true; } - list($openTag, $compiler->nocache, $local, $itemVar, $restore) = - $this->closeTag($compiler, array('foreach', 'foreachelse')); + list( + $openTag, $compiler->nocache, $local, $itemVar, $restore + ) = $this->closeTag($compiler, array('foreach', 'foreachelse')); $output = "propertyPreg = '~('; - $this->startOffset = 0; + $this->startOffset = 1; $this->resultOffsets = array(); $this->matchResults = array('named' => array(), 'item' => array()); - if ($this->isNamed) { + if (isset($attributes[ 'name' ])) { $this->buildPropertyPreg(true, $attributes); } if (isset($this->itemProperties)) { @@ -111,21 +111,19 @@ class Smarty_Internal_Compile_Private_ForeachSection extends Smarty_Internal_Com public function buildPropertyPreg($named, $attributes) { if ($named) { - $this->resultOffsets[ 'named' ] = $this->startOffset + 4; + $this->resultOffsets[ 'named' ] = $this->startOffset = $this->startOffset + 3; $this->propertyPreg .= "(([\$]smarty[.]{$this->tagName}[.]" . - ($this->tagName === 'section' ? "|[\[]\s*" : '') - . + ($this->tagName === 'section' ? "|[\[]\s*" : '') . "){$attributes['name']}[.]("; $properties = $this->nameProperties; } else { - $this->resultOffsets[ 'item' ] = $this->startOffset + 3; + $this->resultOffsets[ 'item' ] = $this->startOffset = $this->startOffset + 2; $this->propertyPreg .= "([\$]{$attributes['item']}[@]("; $properties = $this->itemProperties; } - $this->startOffset += count($properties) + 2; $propName = reset($properties); while ($propName) { - $this->propertyPreg .= "({$propName})"; + $this->propertyPreg .= "{$propName}"; $propName = next($properties); if ($propName) { $this->propertyPreg .= '|'; @@ -141,11 +139,11 @@ class Smarty_Internal_Compile_Private_ForeachSection extends Smarty_Internal_Com */ public function matchProperty($source) { - preg_match_all($this->propertyPreg, $source, $match, PREG_SET_ORDER); + preg_match_all($this->propertyPreg, $source, $match); foreach ($this->resultOffsets as $key => $offset) { - foreach ($match as $m) { - if (isset($m[ $offset ]) && !empty($m[ $offset ])) { - $this->matchResults[ $key ][ strtolower($m[ $offset ]) ] = true; + foreach ($match[ $offset ] as $m) { + if (!empty($m)) { + $this->matchResults[ $key ][ strtolower($m) ] = true; } } } @@ -179,8 +177,8 @@ class Smarty_Internal_Compile_Private_ForeachSection extends Smarty_Internal_Com $_content = $nextCompiler->template->source->getContent(); if ($_content !== '') { // run pre filter if required - if ((isset($nextCompiler->smarty->autoload_filters[ 'pre' ]) - || isset($nextCompiler->smarty->registered_filters[ 'pre' ])) + if ((isset($nextCompiler->smarty->autoload_filters[ 'pre' ]) || + isset($nextCompiler->smarty->registered_filters[ 'pre' ])) ) { $_content = $nextCompiler->smarty->ext->_filterHandler->runFilter( 'pre',