From 29388b8baab5829d6f2a4bc65393950a3a42586c Mon Sep 17 00:00:00 2001 From: uwetews Date: Mon, 17 Apr 2017 05:04:44 +0200 Subject: [PATCH] - correct generated code on empty() and isset() call, observe change PHP behaviour since PHP 5.5 https://github.com/smarty-php/smarty/issues/347 (reverted from commit cfd8bf32b0680d4390a5e6d4f498aa2b387f4dda) --- change_log.txt | 4 - libs/Smarty.class.php | 2 +- .../smarty_internal_templatecompilerbase.php | 111 +++++++----------- 3 files changed, 44 insertions(+), 73 deletions(-) diff --git a/change_log.txt b/change_log.txt index 999ea925..d20da48e 100644 --- a/change_log.txt +++ b/change_log.txt @@ -1,8 +1,4 @@ ===== 3.1.32 - dev === -17.4.2017 - - correct generated code on empty() and isset() call, observe change PHP behaviour since PHP 5.5 - https://github.com/smarty-php/smarty/issues/347 - 14.4.2017 - merge pull requests https://github.com/smarty-php/smarty/pull/349, https://github.com/smarty-php/smarty/pull/322 and https://github.com/smarty-php/smarty/pull/337 to fix spelling and annotation diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index 76e0489c..549ab96e 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -108,7 +108,7 @@ class Smarty extends Smarty_Internal_TemplateBase /** * smarty version */ - const SMARTY_VERSION = '3.1.32-dev-1'; + const SMARTY_VERSION = '3.1.32-dev'; /** * define variable scopes diff --git a/libs/sysplugins/smarty_internal_templatecompilerbase.php b/libs/sysplugins/smarty_internal_templatecompilerbase.php index f7749698..682942ab 100644 --- a/libs/sysplugins/smarty_internal_templatecompilerbase.php +++ b/libs/sysplugins/smarty_internal_templatecompilerbase.php @@ -280,10 +280,7 @@ abstract class Smarty_Internal_TemplateCompilerBase * * @var array */ - public $plugin_search_order = array('function', - 'block', - 'compiler', - 'class'); + public $plugin_search_order = array('function', 'block', 'compiler', 'class'); /** * General storage area for tag compiler plugins @@ -317,8 +314,7 @@ abstract class Smarty_Internal_TemplateCompilerBase public function __construct(Smarty $smarty) { $this->smarty = $smarty; - $this->nocache_hash = str_replace(array('.', - ','), '_', uniqid(rand(), true)); + $this->nocache_hash = str_replace(array('.', ','), '_', uniqid(rand(), true)); } /** @@ -391,8 +387,7 @@ abstract class Smarty_Internal_TemplateCompilerBase // add file dependency if ($this->smarty->merge_compiled_includes || $this->template->source->handler->checkTimestamps()) { $this->parent_compiler->template->compiled->file_dependency[ $this->template->source->uid ] = - array($this->template->source->filepath, - $this->template->source->getTimeStamp(), + array($this->template->source->filepath, $this->template->source->getTimeStamp(), $this->template->source->type,); } $this->smarty->_current_file = $this->template->source->filepath; @@ -458,7 +453,7 @@ abstract class Smarty_Internal_TemplateCompilerBase public function preFilter($_content) { // run pre filter if required - if ($_content !== '' && + if ($_content != '' && ((isset($this->smarty->autoload_filters[ 'pre' ]) || isset($this->smarty->registered_filters[ 'pre' ]))) ) { return $this->smarty->ext->_filterHandler->runFilter('pre', $_content, $this->template); @@ -513,8 +508,7 @@ abstract class Smarty_Internal_TemplateCompilerBase $this->has_output = false; // log tag/attributes if (isset($this->smarty->_cache[ 'get_used_tags' ])) { - $this->template->_cache[ 'used_tags' ][] = array($tag, - $args); + $this->template->_cache[ 'used_tags' ][] = array($tag, $args); } // check nocache option flag foreach ($args as $arg) { @@ -524,7 +518,7 @@ abstract class Smarty_Internal_TemplateCompilerBase } } else { foreach ($arg as $k => $v) { - if (($k === "'nocache'" || $k === 'nocache') && (trim($v, "'\" ") === 'true')) { + if (($k === "'nocache'" || $k === 'nocache') && (trim($v, "'\" ") == 'true')) { $this->tag_nocache = true; } } @@ -533,9 +527,7 @@ abstract class Smarty_Internal_TemplateCompilerBase // compile the smarty tag (required compile classes to compile the tag are auto loaded) if (($_output = $this->callTagCompiler($tag, $args, $parameter)) === false) { if (isset($this->parent_compiler->tpl_function[ $tag ]) || - (isset ($this->template->smarty->ext->_tplFunction) && - $this->template->smarty->ext->_tplFunction->getTplFunction($this->template, $tag) !== false) - ) { + (isset ($this->template->smarty->ext->_tplFunction) && $this->template->smarty->ext->_tplFunction->getTplFunction($this->template, $tag) !== false)) { // template defined by {template} tag $args[ '_attr' ][ 'name' ] = "'" . $tag . "'"; $_output = $this->callTagCompiler('call', $args, $parameter); @@ -565,7 +557,7 @@ abstract class Smarty_Internal_TemplateCompilerBase } } // not an internal compiler tag - if (strlen($tag) < 6 || substr($tag, - 5) !== 'close') { + if (strlen($tag) < 6 || substr($tag, - 5) != 'close') { // check if tag is a registered object if (isset($this->smarty->registered_objects[ $tag ]) && isset($parameter[ 'object_method' ])) { $method = $parameter[ 'object_method' ]; @@ -584,12 +576,11 @@ abstract class Smarty_Internal_TemplateCompilerBase } } // check if tag is registered - foreach (array(Smarty::PLUGIN_COMPILER, - Smarty::PLUGIN_FUNCTION, - Smarty::PLUGIN_BLOCK,) as $plugin_type) { + foreach (array(Smarty::PLUGIN_COMPILER, Smarty::PLUGIN_FUNCTION, Smarty::PLUGIN_BLOCK,) as $plugin_type) + { if (isset($this->smarty->registered_plugins[ $plugin_type ][ $tag ])) { // if compiler function plugin call it now - if ($plugin_type === Smarty::PLUGIN_COMPILER) { + if ($plugin_type == Smarty::PLUGIN_COMPILER) { $new_args = array(); foreach ($args as $key => $mixed) { if (is_array($mixed)) { @@ -602,11 +593,10 @@ abstract class Smarty_Internal_TemplateCompilerBase $this->tag_nocache = true; } return call_user_func_array($this->smarty->registered_plugins[ $plugin_type ][ $tag ][ 0 ], - array($new_args, - $this)); + array($new_args, $this)); } // compile registered function or block function - if ($plugin_type === Smarty::PLUGIN_FUNCTION || $plugin_type === Smarty::PLUGIN_BLOCK) { + if ($plugin_type == Smarty::PLUGIN_FUNCTION || $plugin_type == Smarty::PLUGIN_BLOCK) { return $this->callTagCompiler('private_registered_' . $plugin_type, $args, $parameter, $tag); } @@ -614,7 +604,7 @@ abstract class Smarty_Internal_TemplateCompilerBase } // check plugins from plugins folder foreach ($this->plugin_search_order as $plugin_type) { - if ($plugin_type === Smarty::PLUGIN_COMPILER && + if ($plugin_type == Smarty::PLUGIN_COMPILER && $this->smarty->loadPlugin('smarty_compiler_' . $tag) && (!isset($this->smarty->security_policy) || $this->smarty->security_policy->isTrustedTag($tag, $this)) @@ -671,7 +661,7 @@ abstract class Smarty_Internal_TemplateCompilerBase } if ($found) { // if compiler function plugin call it now - if ($plugin_type === Smarty::PLUGIN_COMPILER) { + if ($plugin_type == Smarty::PLUGIN_COMPILER) { $new_args = array(); foreach ($args as $key => $mixed) { if (is_array($mixed)) { @@ -681,8 +671,7 @@ abstract class Smarty_Internal_TemplateCompilerBase } } return call_user_func_array($this->default_handler_plugins[ $plugin_type ][ $tag ][ 0 ], - array($new_args, - $this)); + array($new_args, $this)); } else { return $this->callTagCompiler('private_registered_' . $plugin_type, $args, $parameter, $tag); @@ -734,8 +723,7 @@ abstract class Smarty_Internal_TemplateCompilerBase $this->tag_nocache = true; } return call_user_func_array($this->smarty->registered_plugins[ Smarty::PLUGIN_COMPILER ][ $tag ][ 0 ], - array($args, - $this)); + array($args, $this)); } if ($this->smarty->loadPlugin('smarty_compiler_' . $tag)) { $plugin = 'smarty_compiler_' . $tag; @@ -764,7 +752,7 @@ abstract class Smarty_Internal_TemplateCompilerBase */ public function compileVariable($variable) { - if (strpos($variable, '(') === 0) { + if (strpos($variable, '(') == 0) { // not a variable variable $var = trim($variable, '\''); $this->tag_nocache = $this->tag_nocache | @@ -805,32 +793,28 @@ abstract class Smarty_Internal_TemplateCompilerBase $func_name = strtolower($name); $par = implode(',', $parameter); $parHasFuction = strpos($par, '(') !== false; - if ($func_name === 'isset') { - if (count($parameter) === 0) { + if ($func_name == 'isset') { + if (count($parameter) == 0) { $this->trigger_template_error('Illegal number of paramer in "isset()"'); } if ($parHasFuction) { - $pa = array(); - foreach ($parameter as $p) { - $pa[] = (strpos($p, '(') === false) ? ('isset(' . $p . ')') : ('(' . $p . ' !== null )'); - } - return "(" . implode(' && ', $pa) . ")"; + $prefixVar = $this->getNewPrefixVariable(); + $this->appendPrefixCode("'); + $isset_par = $prefixVar; } else { $isset_par = str_replace("')->value", "',null,true,false)->value", $par); } return $name . "(" . $isset_par . ")"; - } elseif (in_array($func_name, array('empty', - 'reset', - 'current', - 'end', - 'prev', - 'next'))) { - if (count($parameter) !== 1) { - $this->trigger_template_error("Illegal number of paramer in '{$func_name()}'"); + } elseif (in_array($func_name, array('empty', 'reset', 'current', 'end', 'prev', 'next'))) { + if (count($parameter) != 1) { + $this->trigger_template_error('Illegal number of paramer in "empty()"'); } - if ($func_name === 'empty') { - if ($parHasFuction && version_compare(PHP_VERSION, '5.5.0', '<')) { - return '(' . $parameter[ 0 ] . ' == false )'; + if ($func_name == 'empty') { + if ($parHasFuction) { + $prefixVar = $this->getNewPrefixVariable(); + $this->appendPrefixCode("'); + + return $func_name . '(' . $prefixVar . ')'; } else { return $func_name . '(' . str_replace("')->value", "',null,true,false)->value", $parameter[ 0 ]) . ')'; @@ -858,7 +842,7 @@ abstract class Smarty_Internal_TemplateCompilerBase */ public function processText($text) { - if ((string) $text !== '') { + if ((string) $text != '') { $store = array(); $_store = 0; if ($this->parser->strip) { @@ -874,7 +858,7 @@ abstract class Smarty_Internal_TemplateCompilerBase $text = substr_replace($text, $replace, $match[ 0 ][ 1 ] - $_offset, $_length); $_offset += $_length - strlen($replace); - ++ $_store; + $_store ++; } } $expressions = array(// replace multiple spaces between tags by a single space @@ -883,8 +867,7 @@ abstract class Smarty_Internal_TemplateCompilerBase '#(:SMARTY@!@|>)[\040\011]*[\n]\s*(?=@!@SMARTY:|<)#s' => '\1\2', // remove multiple spaces between attributes (but not in attribute values!) '#(([a-z0-9]\s*=\s*("[^"]*?")|(\'[^\']*?\'))|<[a-z0-9_]+)\s+([a-z/>])#is' => '\1 \5', - '#>[\040\011]+$#Ss' => '> ', - '#>[\040\011]*[\n]\s*$#Ss' => '>', + '#>[\040\011]+$#Ss' => '> ', '#>[\040\011]*[\n]\s*$#Ss' => '>', $this->stripRegEx => '',); $text = preg_replace(array_keys($expressions), array_values($expressions), $text); @@ -897,7 +880,7 @@ abstract class Smarty_Internal_TemplateCompilerBase $text = substr_replace($text, $replace, $match[ 0 ][ 1 ] + $_offset, $_length); $_offset += strlen($replace) - $_length; - ++ $_store; + $_store ++; } } } else { @@ -925,7 +908,6 @@ abstract class Smarty_Internal_TemplateCompilerBase */ public function callTagCompiler($tag, $args, $param1 = null, $param2 = null, $param3 = null) { - /* @var Smarty_Internal_CompileBase $tagCompiler */ $tagCompiler = $this->getTagCompiler($tag); // compile this tag return $tagCompiler === false ? false : $tagCompiler->compile($args, $this, $param1, $param2, $param3); @@ -993,7 +975,7 @@ abstract class Smarty_Internal_TemplateCompilerBase } } if (isset($function)) { - if ($plugin_type === 'modifier') { + if ($plugin_type == 'modifier') { $this->modifier_plugins[ $plugin_name ] = true; } @@ -1015,7 +997,7 @@ abstract class Smarty_Internal_TemplateCompilerBase $this->parent_compiler->template->compiled->required_plugins[ 'compiled' ][ $plugin_name ][ $plugin_type ][ 'function' ] = $function; } - if ($plugin_type === 'modifier') { + if ($plugin_type == 'modifier') { $this->modifier_plugins[ $plugin_name ] = true; } @@ -1042,12 +1024,8 @@ abstract class Smarty_Internal_TemplateCompilerBase $callback = null; $script = null; $cacheable = true; - $result = call_user_func_array($this->smarty->default_plugin_handler_func, array($tag, - $plugin_type, - $this->template, - &$callback, - &$script, - &$cacheable,)); + $result = call_user_func_array($this->smarty->default_plugin_handler_func, + array($tag, $plugin_type, $this->template, &$callback, &$script, &$cacheable,)); if ($result) { $this->tag_nocache = $this->tag_nocache || !$cacheable; if ($script !== null) { @@ -1069,9 +1047,7 @@ abstract class Smarty_Internal_TemplateCompilerBase } } if (is_callable($callback)) { - $this->default_handler_plugins[ $plugin_type ][ $tag ] = array($callback, - true, - array()); + $this->default_handler_plugins[ $plugin_type ][ $tag ] = array($callback, true, array()); return true; } else { @@ -1260,8 +1236,7 @@ abstract class Smarty_Internal_TemplateCompilerBase $line = (int) $line; } - if (in_array($this->template->source->type, array('eval', - 'string'))) { + if (in_array($this->template->source->type, array('eval', 'string'))) { $templateName = $this->template->source->type . ':' . trim(preg_replace('![\t\r\n]+!', ' ', strlen($lex->data) > 40 ? substr($lex->data, 0, 40) . @@ -1350,7 +1325,7 @@ abstract class Smarty_Internal_TemplateCompilerBase */ public function getNewPrefixVariable() { - ++ self::$prefixVariableNumber; + self::$prefixVariableNumber ++; return $this->getPrefixVariable(); }