From ab5d8d4e07bb3c8230d0285ef8902ef1979fce51 Mon Sep 17 00:00:00 2001 From: emkael Date: Thu, 18 Oct 2018 02:39:34 +0200 Subject: Updating Smarty --- .../sysplugins/smarty_internal_compilebase.php | 137 ++++++++++++--------- 1 file changed, 82 insertions(+), 55 deletions(-) (limited to 'lib/smarty/sysplugins/smarty_internal_compilebase.php') diff --git a/lib/smarty/sysplugins/smarty_internal_compilebase.php b/lib/smarty/sysplugins/smarty_internal_compilebase.php index 5fb56f3..2a32e43 100644 --- a/lib/smarty/sysplugins/smarty_internal_compilebase.php +++ b/lib/smarty/sysplugins/smarty_internal_compilebase.php @@ -2,25 +2,26 @@ /** * Smarty Internal Plugin CompileBase * - * @package Smarty + * @package Smarty * @subpackage Compiler - * @author Uwe Tews + * @author Uwe Tews */ /** * This class does extend all internal compile plugins * - * @package Smarty + * @package Smarty * @subpackage Compiler */ -abstract class Smarty_Internal_CompileBase { - +abstract class Smarty_Internal_CompileBase +{ /** * Array of names of required attribute required by tag * * @var array */ public $required_attributes = array(); + /** * Array of names of optional attribute required by tag * use array('_any') if there is no restriction of attributes names @@ -28,12 +29,14 @@ abstract class Smarty_Internal_CompileBase { * @var array */ public $optional_attributes = array(); + /** * Shorttag attribute order defined by its names * * @var array */ public $shorttag_order = array(); + /** * Array of names of valid option flags * @@ -42,8 +45,21 @@ abstract class Smarty_Internal_CompileBase { public $option_flags = array('nocache'); /** - * This function checks if the attributes passed are valid + * Mapping array for boolean option value + * + * @var array + */ + public $optionMap = array(1 => true, 0 => false, 'true' => true, 'false' => false); + + /** + * Mapping array with attributes as key * + * @var array + */ + public $mapCache = array(); + + /** + * This function checks if the attributes passed are valid * The attributes passed for the tag to compile are checked against the list of required and * optional attributes. Required attributes must be present. Optional attributes are check against * the corresponding list. The keyword '_any' specifies that any attribute will be accepted @@ -51,87 +67,101 @@ abstract class Smarty_Internal_CompileBase { * * @param object $compiler compiler object * @param array $attributes attributes applied to the tag - * @return array of mapped attributes for further processing + * + * @return array of mapped attributes for further processing */ public function getAttributes($compiler, $attributes) { $_indexed_attr = array(); - // loop over attributes + if (!isset($this->mapCache[ 'option' ])) { + $this->mapCache[ 'option' ] = array_fill_keys($this->option_flags, true); + } foreach ($attributes as $key => $mixed) { // shorthand ? if (!is_array($mixed)) { // option flag ? - if (in_array(trim($mixed, '\'"'), $this->option_flags)) { - $_indexed_attr[trim($mixed, '\'"')] = true; + if (isset($this->mapCache[ 'option' ][ trim($mixed, '\'"') ])) { + $_indexed_attr[ trim($mixed, '\'"') ] = true; // shorthand attribute ? - } else if (isset($this->shorttag_order[$key])) { - $_indexed_attr[$this->shorttag_order[$key]] = $mixed; + } elseif (isset($this->shorttag_order[ $key ])) { + $_indexed_attr[ $this->shorttag_order[ $key ] ] = $mixed; } else { // too many shorthands - $compiler->trigger_template_error('too many shorthand attributes', $compiler->lex->taglineno); + $compiler->trigger_template_error('too many shorthand attributes', null, true); } // named attribute } else { - $kv = each($mixed); - // option flag? - if (in_array($kv['key'], $this->option_flags)) { - if (is_bool($kv['value'])) { - $_indexed_attr[$kv['key']] = $kv['value']; - } else if (is_string($kv['value']) && in_array(trim($kv['value'], '\'"'), array('true', 'false'))) { - if (trim($kv['value']) == 'true') { - $_indexed_attr[$kv['key']] = true; + foreach ($mixed as $k => $v) { + // option flag? + if (isset($this->mapCache[ 'option' ][ $k ])) { + if (is_bool($v)) { + $_indexed_attr[ $k ] = $v; } else { - $_indexed_attr[$kv['key']] = false; - } - } else if (is_numeric($kv['value']) && in_array($kv['value'], array(0, 1))) { - if ($kv['value'] == 1) { - $_indexed_attr[$kv['key']] = true; - } else { - $_indexed_attr[$kv['key']] = false; + if (is_string($v)) { + $v = trim($v, '\'" '); + } + if (isset($this->optionMap[ $v ])) { + $_indexed_attr[ $k ] = $this->optionMap[ $v ]; + } else { + $compiler->trigger_template_error( + "illegal value '" . var_export($v, true) . + "' for option flag '{$k}'", + null, + true + ); + } } + // must be named attribute } else { - $compiler->trigger_template_error("illegal value of option flag \"{$kv['key']}\"", $compiler->lex->taglineno); + $_indexed_attr[ $k ] = $v; } - // must be named attribute - } else { - reset($mixed); - $_indexed_attr[key($mixed)] = $mixed[key($mixed)]; } } } // check if all required attributes present foreach ($this->required_attributes as $attr) { - if (!array_key_exists($attr, $_indexed_attr)) { - $compiler->trigger_template_error("missing \"" . $attr . "\" attribute", $compiler->lex->taglineno); + if (!isset($_indexed_attr[ $attr ])) { + $compiler->trigger_template_error("missing '{$attr}' attribute", null, true); } } - // check for unallowed attributes - if ($this->optional_attributes != array('_any')) { - $tmp_array = array_merge($this->required_attributes, $this->optional_attributes, $this->option_flags); + // check for not allowed attributes + if ($this->optional_attributes !== array('_any')) { + if (!isset($this->mapCache[ 'all' ])) { + $this->mapCache[ 'all' ] = + array_fill_keys( + array_merge( + $this->required_attributes, + $this->optional_attributes, + $this->option_flags + ), + true + ); + } foreach ($_indexed_attr as $key => $dummy) { - if (!in_array($key, $tmp_array) && $key !== 0) { - $compiler->trigger_template_error("unexpected \"" . $key . "\" attribute", $compiler->lex->taglineno); + if (!isset($this->mapCache[ 'all' ][ $key ]) && $key !== 0) { + $compiler->trigger_template_error("unexpected '{$key}' attribute", null, true); } } } // default 'false' for all option flags not set foreach ($this->option_flags as $flag) { - if (!isset($_indexed_attr[$flag])) { - $_indexed_attr[$flag] = false; + if (!isset($_indexed_attr[ $flag ])) { + $_indexed_attr[ $flag ] = false; } } - + if (isset($_indexed_attr[ 'nocache' ]) && $_indexed_attr[ 'nocache' ]) { + $compiler->tag_nocache = true; + } return $_indexed_attr; } /** * Push opening tag name on stack - * * Optionally additional data can be saved on stack * - * @param object $compiler compiler object - * @param string $openTag the opening tag's name - * @param mixed $data optional data saved + * @param object $compiler compiler object + * @param string $openTag the opening tag's name + * @param mixed $data optional data saved */ public function openTag($compiler, $openTag, $data = null) { @@ -140,12 +170,12 @@ abstract class Smarty_Internal_CompileBase { /** * Pop closing tag - * * Raise an error if this stack-top doesn't match with expected opening tags * * @param object $compiler compiler object * @param array|string $expectedTag the expected opening tag names - * @return mixed any type the opening tag's name or saved data + * + * @return mixed any type the opening tag's name or saved data */ public function closeTag($compiler, $expectedTag) { @@ -153,7 +183,7 @@ abstract class Smarty_Internal_CompileBase { // get stacked info list($_openTag, $_data) = array_pop($compiler->_tag_stack); // open tag must match with the expected ones - if (in_array($_openTag, (array) $expectedTag)) { + if (in_array($_openTag, (array)$expectedTag)) { if (is_null($_data)) { // return opening tag return $_openTag; @@ -163,14 +193,11 @@ abstract class Smarty_Internal_CompileBase { } } // wrong nesting of tags - $compiler->trigger_template_error("unclosed {" . $_openTag . "} tag"); + $compiler->trigger_template_error("unclosed '{$compiler->smarty->left_delimiter}{$_openTag}{$compiler->smarty->right_delimiter}' tag"); return; } // wrong nesting of tags - $compiler->trigger_template_error("unexpected closing tag", $compiler->lex->taglineno); + $compiler->trigger_template_error('unexpected closing tag', null, true); return; } - } - -?> \ No newline at end of file -- cgit v1.2.3