Fix for Msarty and PHP7.2

This commit is contained in:
Rob Lensen
2018-07-08 21:25:23 +02:00
committed by Pierrick Le Gall
parent 063711240a
commit 68f83a5902
@@ -44,6 +44,20 @@ abstract class Smarty_Internal_CompileBase
*/ */
public $option_flags = array('nocache'); public $option_flags = array('nocache');
/**
* 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 * 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 * The attributes passed for the tag to compile are checked against the list of required and
@@ -59,71 +73,75 @@ abstract class Smarty_Internal_CompileBase
public function getAttributes($compiler, $attributes) public function getAttributes($compiler, $attributes)
{ {
$_indexed_attr = array(); $_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) { foreach ($attributes as $key => $mixed) {
// shorthand ? // shorthand ?
if (!is_array($mixed)) { if (!is_array($mixed)) {
// option flag ? // option flag ?
if (in_array(trim($mixed, '\'"'), $this->option_flags)) { if (isset($this->mapCache[ 'option' ][ trim($mixed, '\'"') ])) {
$_indexed_attr[trim($mixed, '\'"')] = true; $_indexed_attr[ trim($mixed, '\'"') ] = true;
// shorthand attribute ? // shorthand attribute ?
} elseif (isset($this->shorttag_order[$key])) { } elseif (isset($this->shorttag_order[ $key ])) {
$_indexed_attr[$this->shorttag_order[$key]] = $mixed; $_indexed_attr[ $this->shorttag_order[ $key ] ] = $mixed;
} else { } else {
// too many shorthands // too many shorthands
$compiler->trigger_template_error('too many shorthand attributes', null, true); $compiler->trigger_template_error('too many shorthand attributes', null, true);
} }
// named attribute // named attribute
} else { } else {
$kv = each($mixed); foreach ($mixed as $k => $v) {
// option flag? // option flag?
if (in_array($kv['key'], $this->option_flags)) { if (isset($this->mapCache[ 'option' ][ $k ])) {
if (is_bool($kv['value'])) { if (is_bool($v)) {
$_indexed_attr[$kv['key']] = $kv['value']; $_indexed_attr[ $k ] = $v;
} elseif (is_string($kv['value']) && in_array(trim($kv['value'], '\'"'), array('true', 'false'))) {
if (trim($kv['value']) == 'true') {
$_indexed_attr[$kv['key']] = true;
} else { } else {
$_indexed_attr[$kv['key']] = false; if (is_string($v)) {
} $v = trim($v, '\'" ');
} elseif (is_numeric($kv['value']) && in_array($kv['value'], array(0, 1))) { }
if ($kv['value'] == 1) { if (isset($this->optionMap[ $v ])) {
$_indexed_attr[$kv['key']] = true; $_indexed_attr[ $k ] = $this->optionMap[ $v ];
} else { } else {
$_indexed_attr[$kv['key']] = false; $compiler->trigger_template_error("illegal value '" . var_export($v, true) .
"' for option flag '{$k}'", null, true);
}
} }
// must be named attribute
} else { } else {
$compiler->trigger_template_error("illegal value of option flag \"{$kv['key']}\"", null, true); $_indexed_attr[ $k ] = $v;
} }
// must be named attribute
} else {
reset($mixed);
$_indexed_attr[key($mixed)] = $mixed[key($mixed)];
} }
} }
} }
// check if all required attributes present // check if all required attributes present
foreach ($this->required_attributes as $attr) { foreach ($this->required_attributes as $attr) {
if (!array_key_exists($attr, $_indexed_attr)) { if (!isset($_indexed_attr[ $attr ])) {
$compiler->trigger_template_error("missing \"" . $attr . "\" attribute", null, true); $compiler->trigger_template_error("missing '{$attr}' attribute", null, true);
} }
} }
// check for not allowed attributes // check for not allowed attributes
if ($this->optional_attributes != array('_any')) { if ($this->optional_attributes !== array('_any')) {
$tmp_array = array_merge($this->required_attributes, $this->optional_attributes, $this->option_flags); 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) { foreach ($_indexed_attr as $key => $dummy) {
if (!in_array($key, $tmp_array) && $key !== 0) { if (!isset($this->mapCache[ 'all' ][ $key ]) && $key !== 0) {
$compiler->trigger_template_error("unexpected \"" . $key . "\" attribute", null, true); $compiler->trigger_template_error("unexpected '{$key}' attribute", null, true);
} }
} }
} }
// default 'false' for all option flags not set // default 'false' for all option flags not set
foreach ($this->option_flags as $flag) { foreach ($this->option_flags as $flag) {
if (!isset($_indexed_attr[$flag])) { if (!isset($_indexed_attr[ $flag ])) {
$_indexed_attr[$flag] = false; $_indexed_attr[ $flag ] = false;
} }
} }
if (isset($_indexed_attr[ 'nocache' ]) && $_indexed_attr[ 'nocache' ]) {
$compiler->tag_nocache = true;
}
return $_indexed_attr; return $_indexed_attr;
} }
@@ -165,13 +183,12 @@ abstract class Smarty_Internal_CompileBase
} }
} }
// wrong nesting of tags // wrong nesting of tags
$compiler->trigger_template_error("unclosed {$compiler->smarty->left_delimiter}" . $_openTag . $compiler->trigger_template_error("unclosed '{$compiler->smarty->left_delimiter}{$_openTag}{$compiler->smarty->right_delimiter}' tag");
"{$compiler->smarty->right_delimiter} tag");
return; return;
} }
// wrong nesting of tags // wrong nesting of tags
$compiler->trigger_template_error("unexpected closing tag", null, true); $compiler->trigger_template_error('unexpected closing tag', null, true);
return; return;
} }