Improve extension handler

This commit is contained in:
Uwe Tews
2017-10-07 08:20:18 +02:00
parent deb9e12aaa
commit b0ea1cb5df
6 changed files with 386 additions and 293 deletions

View File

@@ -3,6 +3,29 @@
This file contains a brief description of new features which have been added to Smarty 3.1 This file contains a brief description of new features which have been added to Smarty 3.1
Smarty 3.1.32 Smarty 3.1.32
Using literals containing Smarty's left and right delimiter
===========================================================
New Methods
$smarty->setLiterals(array $literals)
$smarty->addLiterals(array $literals)
to define lietrals containing Smarty delimiter. This can avoid the need for extreme usage
of {literal} {/literal} tags.
A) Treat '{{' and '}}' as literal
If Smarty::$auto_literal is enabled
{{ foo }}
will be treated now as literal. (This does apply for any number of delimiter repeatations).
However {{foo}} is not an literal but will be interpreted as a recursive Smarty tag.
If you use
$smarty->setLiteral(array('{{','}}'));
{{foo}} is now a literal as well.
NOTE: In the last example nested Smarty tags starting with '{{' or ending with '}}' will not
work any longer, but this should be very very raw occouring restriction.
B) Example 2
Assume your delimiter are '<-' , '->' and '<--' , '-->' shall be lietrals
$smarty->setLiteral(array('<--','-->'));
The capture buffers can now be accessed as array The capture buffers can now be accessed as array
================================================ ================================================
{capture name='foo'} {capture name='foo'}

View File

@@ -1,4 +1,8 @@
===== 3.1.32 - dev === ===== 3.1.32 - dev ===
07.10.2017
- bugfix modification of 9.8.2017 did fail on some recursive
tag nesting. https://github.com/smarty-php/smarty/issues/389
26.8.2017 26.8.2017
- bugfix chained modifier failed when last modifier parameter is a signed value - bugfix chained modifier failed when last modifier parameter is a signed value
https://github.com/smarty-php/smarty/issues/327 https://github.com/smarty-php/smarty/issues/327

View File

@@ -81,6 +81,13 @@ class Smarty_Internal_Templatelexer
*/ */
public $ldel = ''; public $ldel = '';
/**
* escaped left delimiter with space
*
* @var string
*/
public $ldel_q = '';
/** /**
* escaped left delimiter length * escaped left delimiter length
* *
@@ -123,19 +130,6 @@ class Smarty_Internal_Templatelexer
*/ */
public $compiler = null; public $compiler = null;
/**
* literal tag nesting level
*
* @var int
*/
private $literal_cnt = 0;
/**
* PHP start tag string
*
* @var string
*/
/** /**
* trace file * trace file
* *
@@ -164,21 +158,6 @@ class Smarty_Internal_Templatelexer
*/ */
public $state_name = array(1 => 'TEXT', 2 => 'TAG', 3 => 'TAGBODY', 4 => 'LITERAL', 5 => 'DOUBLEQUOTEDSTRING',); public $state_name = array(1 => 'TEXT', 2 => 'TAG', 3 => 'TAGBODY', 4 => 'LITERAL', 5 => 'DOUBLEQUOTEDSTRING',);
/**
* storage for assembled token patterns
*
* @var string
*/
private $yy_global_pattern1 = null;
private $yy_global_pattern2 = null;
private $yy_global_pattern3 = null;
private $yy_global_pattern4 = null;
private $yy_global_pattern5 = null;
/** /**
* token names * token names
* *
@@ -223,16 +202,65 @@ class Smarty_Internal_Templatelexer
'SCOND' => '"is even" ... if condition', 'SCOND' => '"is even" ... if condition',
); );
/**
* preg string of user defined litereals
*
* @var string
*/
public $literals = '';
/**
* literal tag nesting level
*
* @var int
*/
private $literal_cnt = 0;
/**
* preg token pattern for state TEXT
*
* @var string
*/
private $yy_global_pattern1 = null;
/**
* preg token pattern for state TAG
*
* @var string
*/
private $yy_global_pattern2 = null;
/**
* preg token pattern for state TAGBODY
*
* @var string
*/
private $yy_global_pattern3 = null;
/**
* preg token pattern for state LITERAL
*
* @var string
*/
private $yy_global_pattern4 = null;
/**
* preg token pattern for state DOUBLEQUOTEDSTRING
*
* @var null
*/
private $yy_global_pattern5 = null;
/** /**
* constructor * constructor
* *
* @param string $data template source * @param string $source template source
* @param Smarty_Internal_TemplateCompilerBase $compiler * @param Smarty_Internal_TemplateCompilerBase $compiler
*/ */
function __construct($data, Smarty_Internal_TemplateCompilerBase $compiler) function __construct($source, Smarty_Internal_TemplateCompilerBase $compiler)
{ {
$this->data = $data; $this->data = $source;
$this->dataLength = strlen($data); $this->dataLength = strlen($this->data);
$this->counter = 0; $this->counter = 0;
if (preg_match('/^\xEF\xBB\xBF/i', $this->data, $match)) { if (preg_match('/^\xEF\xBB\xBF/i', $this->data, $match)) {
$this->counter += strlen($match[0]); $this->counter += strlen($match[0]);
@@ -240,30 +268,62 @@ class Smarty_Internal_Templatelexer
$this->line = 1; $this->line = 1;
$this->smarty = $compiler->smarty; $this->smarty = $compiler->smarty;
$this->compiler = $compiler; $this->compiler = $compiler;
$this->pldel = preg_quote($this->smarty->left_delimiter, '/'); $this->ldel = preg_quote($this->smarty->left_delimiter, '/') . ($this->smarty->auto_literal ? '' : '\\s*');
$this->ldel = $this->pldel . ($this->smarty->auto_literal ? '(?!\\s+)' : '\\s*');
$this->ldel_length = strlen($this->smarty->left_delimiter); $this->ldel_length = strlen($this->smarty->left_delimiter);
$this->rdel = preg_quote($this->smarty->right_delimiter, '/'); $this->rdel = preg_quote($this->smarty->right_delimiter, '/');
$this->rdel_length = strlen($this->smarty->right_delimiter); $this->rdel_length = strlen($this->smarty->right_delimiter);
$this->smarty_token_names['LDEL'] = $this->smarty->left_delimiter; $this->smarty_token_names['LDEL'] = $this->smarty->left_delimiter;
$this->smarty_token_names['RDEL'] = $this->smarty->right_delimiter; $this->smarty_token_names['RDEL'] = $this->smarty->right_delimiter;
$literals = $this->smarty->getLiterals();
if (!empty($literals)) {
foreach ($literals as $key => $literal) {
$literals[$key] = preg_quote($literal, '/');
}
} }
if ($this->smarty->auto_literal) {
$literals[] = $this->ldel . '{1,}\\s+';
}
if (!empty($literals)) {
$this->literals = implode('|', $literals);
} else {
$this->literals = preg_quote('^$', '/');
}
}
/**
* open lexer/parser trace file
*
*/
public function PrintTrace() public function PrintTrace()
{ {
$this->yyTraceFILE = fopen('php://output', 'w'); $this->yyTraceFILE = fopen('php://output', 'w');
$this->yyTracePrompt = '<br>'; $this->yyTracePrompt = '<br>';
} }
/*
* Check if this tag is autoliteral /**
* replace placeholders with runtime preg code
*
* @param string $input
*
* @return string
*/
public function replace($input)
{
return str_replace(array('SMARTYldel', 'SMARTYliteral', 'SMARTYrdel'),
array($this->ldel, $this->literals, $this->rdel),
$input);
}
/**
* check if current value is an autoliteral left delimiter
*
* @return bool
*/ */
public function isAutoLiteral() public function isAutoLiteral()
{ {
return $this->smarty->auto_literal && isset($this->value[$this->ldel_length]) ? strpos(" \n\t\r", $this->value[$this->ldel_length]) !== false : false; return $this->smarty->auto_literal && isset($this->value[ $this->ldel_length ]) ?
} strpos(" \n\t\r", $this->value[ $this->ldel_length ]) !== false : false;
public function replace ($input) {
return str_replace(array('SMARTYldel','SMARTYrawldel','SMARTYrdel'),array($this->ldel,$this->pldel,$this->rdel),$input);
} }
/*!lex2php /*!lex2php
@@ -272,15 +332,13 @@ class Smarty_Internal_Templatelexer
%token $this->token %token $this->token
%value $this->value %value $this->value
%line $this->line %line $this->line
linebreak = ~[\t ]*[\r\n]+[\t ]*~ userliteral = ~SMARTYliteral~
ldelrepeat = ~SMARTYrawldel{2,}~ char = ~[\S\s]~
text = ~[\S\s]~ textdoublequoted = ~([^"\\]*?)((?:\\.[^"\\]*?)*?)(?=(SMARTYliteral|SMARTYldel|\$|`\$|"))~
textdoublequoted = ~([^"\\]*?)((?:\\.[^"\\]*?)*?)(?=(SMARTYldel|\$|`\$|"))~
namespace = ~([0-9]*[a-zA-Z_]\w*)?(\\[0-9]*[a-zA-Z_]\w*)+~ namespace = ~([0-9]*[a-zA-Z_]\w*)?(\\[0-9]*[a-zA-Z_]\w*)+~
all = ~[\S\s]+~
emptyjava = ~[{][}]~ emptyjava = ~[{][}]~
phptag = ~(SMARTYldelphp([ ].*?)?SMARTYrdel)|(SMARTYldel[/]phpSMARTYrdel)~ phptag = ~(SMARTYldelphp([ ].*?)?SMARTYrdel)|(SMARTYldel[/]phpSMARTYrdel)~
phpstart = ~(<[?]((php\s+|=)|\s+))|(<[%])|(<[?]xml\s+)|(<script\s+language\s*=\s*["']?\s*php\s*["']?\s*>)|([?][>])|([%][>])~ phpstart = ~([<][?]((php\s+|=)|\s+))|([<][%])|([<][?]xml\s+)|([<]script\s+language\s*=\s*["']?\s*php\s*["']?\s*[>])|([?][>])|([%][>])~
slash = ~[/]~ slash = ~[/]~
ldel = ~SMARTYldel~ ldel = ~SMARTYldel~
rdel = ~\s*SMARTYrdel~ rdel = ~\s*SMARTYrdel~
@@ -336,7 +394,9 @@ class Smarty_Internal_Templatelexer
not = ~([!]\s*)|(not\s+)~ not = ~([!]\s*)|(not\s+)~
typecast = ~[(](int(eger)?|bool(ean)?|float|double|real|string|binary|array|object)[)]\s*~ typecast = ~[(](int(eger)?|bool(ean)?|float|double|real|string|binary|array|object)[)]\s*~
double_quote = ~["]~ double_quote = ~["]~
single_quote = ~[']~ text = ~((.*?)(?=(SMARTYliteral|[{]|([<][?]((php\s+|=)|\s+))|([<][%])|([<][?]xml\s+)|([<]script\s+language\s*=\s*["']?\s*php\s*["']?\s*[>])|([?][>])|([%][>]))))|(.*)~
literaltext = ~(.*?)(?=SMARTYldel[/]?literalSMARTYrdel)~
anytext = ~.*~
*/ */
/*!lex2php /*!lex2php
%statename TEXT %statename TEXT
@@ -356,7 +416,7 @@ class Smarty_Internal_Templatelexer
phptag { phptag {
$this->compiler->getTagCompiler('private_php')->parsePhp($this); $this->compiler->getTagCompiler('private_php')->parsePhp($this);
} }
ldelrepeat { userliteral {
$this->token = Smarty_Internal_Templateparser::TP_TEXT; $this->token = Smarty_Internal_Templateparser::TP_TEXT;
} }
ldel literal rdel { ldel literal rdel {
@@ -375,12 +435,6 @@ class Smarty_Internal_Templatelexer
$this->compiler->getTagCompiler('private_php')->parsePhp($this); $this->compiler->getTagCompiler('private_php')->parsePhp($this);
} }
text { text {
$to = $this->dataLength;
preg_match("/((?<!$this->pldel){$this->ldel})|(<[?]((php\s+|=)|\s+))|(<[%])|(<[?]xml\s+)|(<script\s+language\s*=\s*[\"']?\s*php\s*[\"']?\s*>)|([?][>])|([%][>])/i",$this->data,$match,PREG_OFFSET_CAPTURE,$this->counter);
if (isset($match[0][1])) {
$to = $match[0][1];
}
$this->value = substr($this->data,$this->counter,$to-$this->counter);
$this->token = Smarty_Internal_Templateparser::TP_TEXT; $this->token = Smarty_Internal_Templateparser::TP_TEXT;
} }
*/ */
@@ -590,7 +644,7 @@ class Smarty_Internal_Templatelexer
space { space {
$this->token = Smarty_Internal_Templateparser::TP_SPACE; $this->token = Smarty_Internal_Templateparser::TP_SPACE;
} }
text { char {
$this->token = Smarty_Internal_Templateparser::TP_TEXT; $this->token = Smarty_Internal_Templateparser::TP_TEXT;
} }
*/ */
@@ -610,21 +664,16 @@ class Smarty_Internal_Templatelexer
$this->yypopstate(); $this->yypopstate();
} }
} }
text { literaltext {
$to = $this->dataLength; $this->token = Smarty_Internal_Templateparser::TP_LITERAL;
preg_match("/{$this->ldel}[\/]?literal{$this->rdel}/i",$this->data,$match,PREG_OFFSET_CAPTURE,$this->counter);
if (isset($match[0][1])) {
$to = $match[0][1];
} else {
$this->compiler->trigger_template_error ("missing or misspelled literal closing tag");
} }
$this->value = substr($this->data,$this->counter,$to-$this->counter); anytext {
$this->token = Smarty_Internal_Templateparser::TP_LITERAL; $this->token = Smarty_Internal_Templateparser::TP_LITERAL;
} }
*/ */
/*!lex2php /*!lex2php
%statename DOUBLEQUOTEDSTRING %statename DOUBLEQUOTEDSTRING
ldelrepeat { userliteral {
$this->token = Smarty_Internal_Templateparser::TP_TEXT; $this->token = Smarty_Internal_Templateparser::TP_TEXT;
} }
ldel literal rdel { ldel literal rdel {
@@ -665,11 +714,6 @@ class Smarty_Internal_Templatelexer
textdoublequoted { textdoublequoted {
$this->token = Smarty_Internal_Templateparser::TP_TEXT; $this->token = Smarty_Internal_Templateparser::TP_TEXT;
} }
text {
$to = $this->dataLength;
$this->value = substr($this->data,$this->counter,$to-$this->counter);
$this->token = Smarty_Internal_Templateparser::TP_TEXT;
}
*/ */
} }

View File

@@ -21,9 +21,9 @@ class Smarty_Internal_Templateparser
} }
%include_class %include_class
{ {
const Err1 = "Security error: Call to private object member not allowed"; const Err1 = 'Security error: Call to private object member not allowed';
const Err2 = "Security error: Call to dynamic object member not allowed"; const Err2 = 'Security error: Call to dynamic object member not allowed';
const Err3 = "PHP in template not allowed. Use SmartyBC to enable it"; const Err3 = 'PHP in template not allowed. Use SmartyBC to enable it';
/** /**
* result status * result status
@@ -209,7 +209,8 @@ class Smarty_Internal_Templateparser
$this->compiler->trigger_template_error("Stack overflow in template parser"); $this->compiler->trigger_template_error("Stack overflow in template parser");
} }
%left VERT.
%right VERT.
%left COLON. %left COLON.
%left UNIMATH. %left UNIMATH.
@@ -493,19 +494,6 @@ tag(res) ::= LDELFOR statement(st) TO expr(v) STEP expr(v2) attributes(a). {
} }
// {foreach} tag // {foreach} tag
tag(res) ::= LDELFOREACH attributes(a). {
res = $this->compiler->compileTag('foreach',a);
}
// {foreach $array as $var} tag
tag(res) ::= LDELFOREACH SPACE value(v1) AS varvar(v0) attributes(a). {
res = $this->compiler->compileTag('foreach',array_merge(a,array(array('from'=>v1),array('item'=>v0))));
}
tag(res) ::= LDELFOREACH SPACE value(v1) AS varvar(v2) APTR varvar(v0) attributes(a). {
res = $this->compiler->compileTag('foreach',array_merge(a,array(array('from'=>v1),array('item'=>v0),array('key'=>v2))));
}
tag(res) ::= LDELFOREACH SPACE expr(e) AS varvar(v0) attributes(a). { tag(res) ::= LDELFOREACH SPACE expr(e) AS varvar(v0) attributes(a). {
res = $this->compiler->compileTag('foreach',array_merge(a,array(array('from'=>e),array('item'=>v0)))); res = $this->compiler->compileTag('foreach',array_merge(a,array(array('from'=>e),array('item'=>v0))));
} }
@@ -513,6 +501,9 @@ tag(res) ::= LDELFOREACH SPACE expr(e) AS varvar(v0) attributes(a). {
tag(res) ::= LDELFOREACH SPACE expr(e) AS varvar(v1) APTR varvar(v0) attributes(a). { tag(res) ::= LDELFOREACH SPACE expr(e) AS varvar(v1) APTR varvar(v0) attributes(a). {
res = $this->compiler->compileTag('foreach',array_merge(a,array(array('from'=>e),array('item'=>v0),array('key'=>v1)))); res = $this->compiler->compileTag('foreach',array_merge(a,array(array('from'=>e),array('item'=>v0),array('key'=>v1))));
} }
tag(res) ::= LDELFOREACH attributes(a). {
res = $this->compiler->compileTag('foreach',a);
}
// {setfilter} // {setfilter}
tag(res) ::= LDELSETFILTER ID(m) modparameters(p). { tag(res) ::= LDELSETFILTER ID(m) modparameters(p). {

View File

@@ -108,7 +108,7 @@ class Smarty extends Smarty_Internal_TemplateBase
/** /**
* smarty version * smarty version
*/ */
const SMARTY_VERSION = '3.1.32-dev-22'; const SMARTY_VERSION = '3.1.32-dev-23';
/** /**
* define variable scopes * define variable scopes
@@ -456,6 +456,13 @@ class Smarty extends Smarty_Internal_TemplateBase
*/ */
public $right_delimiter = "}"; public $right_delimiter = "}";
/**
* array of strings which shall be treated as literal by compiler
*
* @var array string
*/
public $literals = array();
/**#@+ /**#@+
* security * security
*/ */

View File

@@ -49,7 +49,7 @@ class Smarty_Internal_Extension_Handler
*/ */
private $_property_info = array('AutoloadFilters' => 0, 'DefaultModifiers' => 0, 'ConfigVars' => 0, private $_property_info = array('AutoloadFilters' => 0, 'DefaultModifiers' => 0, 'ConfigVars' => 0,
'DebugTemplate' => 0, 'RegisteredObject' => 0, 'StreamVariable' => 0, 'DebugTemplate' => 0, 'RegisteredObject' => 0, 'StreamVariable' => 0,
'TemplateVars' => 0,);# 'TemplateVars' => 0, 'Literals' => 'Literals',);#
private $resolvedProperties = array(); private $resolvedProperties = array();
@@ -68,36 +68,60 @@ class Smarty_Internal_Extension_Handler
/* @var Smarty $data ->smarty */ /* @var Smarty $data ->smarty */
$smarty = isset($data->smarty) ? $data->smarty : $data; $smarty = isset($data->smarty) ? $data->smarty : $data;
if (!isset($smarty->ext->$name)) { if (!isset($smarty->ext->$name)) {
$class = 'Smarty_Internal_Method_' . $this->upperCase($name); if (preg_match('/^((set|get)|(.*?))([A-Z].*)$/', $name, $match)) {
if (preg_match('/^(set|get)([A-Z].*)$/', $name, $match)) { $basename = $this->upperCase($match[4]);
$pn = ''; if (!isset($smarty->ext->$basename) && isset($this->_property_info[ $basename ]) &&
if (!isset($this->_property_info[ $prop = $match[ 2 ] ])) { is_string($this->_property_info[ $basename ])) {
// convert camel case to underscored name $class = 'Smarty_Internal_Method_' . $this->_property_info[ $basename ];
$this->resolvedProperties[ $prop ] = $pn = strtolower(join('_',
preg_split('/([A-Z][^A-Z]*)/', $prop,
- 1, PREG_SPLIT_NO_EMPTY |
PREG_SPLIT_DELIM_CAPTURE)));
$this->_property_info[ $prop ] =
property_exists($data, $pn) ? 1 : ($data->_isTplObj() && property_exists($smarty, $pn) ? 2 : 0);
}
if ($this->_property_info[ $prop ]) {
$pn = $this->resolvedProperties[ $prop ];
if ($match[ 1 ] == 'get') {
return $this->_property_info[ $prop ] == 1 ? $data->$pn : $data->smarty->$pn;
} else {
return $this->_property_info[ $prop ] == 1 ? $data->$pn = $args[ 0 ] :
$data->smarty->$pn = $args[ 0 ];
}
} elseif (!class_exists($class)) {
throw new SmartyException("property '$pn' does not exist.");
}
}
if (class_exists($class)) { if (class_exists($class)) {
$callback = array($smarty->ext->$name = new $class(), $name); $classObj = new $class();
$methodes = get_class_methods($classObj);
foreach ($methodes as $method) {
$smarty->ext->$method = $classObj;
}
}
}
if (!empty($match[2]) && !isset($smarty->ext->$name)) {
$class = 'Smarty_Internal_Method_' . $this->upperCase($name);
if (!class_exists($class)) {
$objType = $data->_objType;
$propertyType = false;
if (!isset($this->resolvedProperties[ $match[0] ][ $objType ])) {
$property = isset($this->resolvedProperties['property'][ $basename ]) ?
$this->resolvedProperties['property'][ $basename ] :
$property = $this->resolvedProperties['property'][ $basename ] = strtolower(join('_',
preg_split('/([A-Z][^A-Z]*)/',
$basename,
-1,
PREG_SPLIT_NO_EMPTY |
PREG_SPLIT_DELIM_CAPTURE)));
if ($property !== false) {
if (property_exists($data, $property)) {
$propertyType = $this->resolvedProperties[ $match[0] ][ $objType ] = 1;
} else if (property_exists($smarty, $property)) {
$propertyType = $this->resolvedProperties[ $match[0] ][ $objType ] = 2;
} else {
$this->resolvedProperties['property'][ $basename ] = $property = false;
}
} }
} else { } else {
$callback = array($smarty->ext->$name, $name); $propertyType = $this->resolvedProperties[ $match[0] ][ $objType ];
$property = $this->resolvedProperties['property'][ $basename ];
} }
if ($propertyType) {
$obj = $propertyType === 1 ? $data : $smarty;
if ($match[2] == 'get') {
return $obj->$property;
} else if ($match[2] == 'set') {
return $obj->$property = $args[0];
}
}
}
}
}
}
$callback = array($smarty->ext->$name, $name);
array_unshift($args, $data); array_unshift($args, $data);
if (isset($callback) && $callback[0]->objMap | $data->_objType) { if (isset($callback) && $callback[0]->objMap | $data->_objType) {
return call_user_func_array($callback, $args); return call_user_func_array($callback, $args);
@@ -119,19 +143,6 @@ class Smarty_Internal_Extension_Handler
return implode('_', $_name); return implode('_', $_name);
} }
/**
* set extension property
*
* @param string $property_name property name
* @param mixed $value value
*
* @throws SmartyException
*/
public function __set($property_name, $value)
{
$this->$property_name = $value;
}
/** /**
* get extension object * get extension object
* *
@@ -154,6 +165,19 @@ class Smarty_Internal_Extension_Handler
return $this->$property_name = new $class(); return $this->$property_name = new $class();
} }
/**
* set extension property
*
* @param string $property_name property name
* @param mixed $value value
*
* @throws SmartyException
*/
public function __set($property_name, $value)
{
$this->$property_name = $value;
}
/** /**
* Call error handler for undefined method * Call error handler for undefined method
* *