WIP making compile classes PSR-4

This commit is contained in:
Simon Wisselink
2022-12-01 23:44:58 +01:00
parent d0319bdc87
commit 164a89a1fd
26 changed files with 1043 additions and 1055 deletions

View File

@@ -8,6 +8,8 @@
* @author Uwe Tews
*/
namespace Smarty\Compile;
use Smarty\Compile\BreakTag;
/**
@@ -16,8 +18,8 @@ use Smarty\Compile\BreakTag;
* @package Smarty
* @subpackage Compiler
*/
class Smarty_Internal_Compile_Continue extends BreakTag
{
class ContinueTag extends BreakTag {
/**
* Tag name
*

View File

@@ -0,0 +1,213 @@
<?php
/**
* Smarty Internal Plugin Compile ForeachSection
* Shared methods for {foreach} {section} tags
*
* @package Smarty
* @subpackage Compiler
* @author Uwe Tews
*/
namespace Smarty\Compile;
use Smarty\Compile\Base;
use Smarty_Internal_TemplateCompilerBase;
/**
* Smarty Internal Plugin Compile ForeachSection Class
*
* @package Smarty
* @subpackage Compiler
*/
abstract class ForeachSection extends Base {
/**
* Name of this tag
*
* @var string
*/
public $tagName = '';
/**
* Valid properties of $smarty.xxx variable
*
* @var array
*/
public $nameProperties = [];
/**
* {section} tag has no item properties
*
* @var array
*/
public $itemProperties = null;
/**
* {section} tag has always name attribute
*
* @var bool
*/
public $isNamed = true;
/**
* @var array
*/
public $matchResults = [];
/**
* Preg search pattern
*
* @var string
*/
private $propertyPreg = '';
/**
* Offsets in preg match result
*
* @var array
*/
private $resultOffsets = [];
/**
* Start offset
*
* @var int
*/
private $startOffset = 0;
/**
* Scan sources for used tag attributes
*
* @param array $attributes
* @param \Smarty_Internal_TemplateCompilerBase $compiler
*
* @throws \SmartyException
*/
protected function scanForProperties($attributes, Smarty_Internal_TemplateCompilerBase $compiler) {
$this->propertyPreg = '~(';
$this->startOffset = 1;
$this->resultOffsets = [];
$this->matchResults = ['named' => [], 'item' => []];
if (isset($attributes['name'])) {
$this->buildPropertyPreg(true, $attributes);
}
if (isset($this->itemProperties)) {
if ($this->isNamed) {
$this->propertyPreg .= '|';
}
$this->buildPropertyPreg(false, $attributes);
}
$this->propertyPreg .= ')\W~i';
// Template source
$this->matchTemplateSource($compiler);
// Parent template source
$this->matchParentTemplateSource($compiler);
}
/**
* Build property preg string
*
* @param bool $named
* @param array $attributes
*/
private function buildPropertyPreg($named, $attributes) {
if ($named) {
$this->resultOffsets['named'] = $this->startOffset = $this->startOffset + 3;
$this->propertyPreg .= "(([\$]smarty[.]{$this->tagName}[.]" .
($this->tagName === 'section' ? "|[\[]\s*" : '') .
"){$attributes['name']}[.](";
$properties = $this->nameProperties;
} else {
$this->resultOffsets['item'] = $this->startOffset = $this->startOffset + 2;
$this->propertyPreg .= "([\$]{$attributes['item']}[@](";
$properties = $this->itemProperties;
}
$propName = reset($properties);
while ($propName) {
$this->propertyPreg .= "{$propName}";
$propName = next($properties);
if ($propName) {
$this->propertyPreg .= '|';
}
}
$this->propertyPreg .= '))';
}
/**
* Find matches in source string
*
* @param string $source
*/
private function matchProperty($source) {
preg_match_all($this->propertyPreg, $source, $match);
foreach ($this->resultOffsets as $key => $offset) {
foreach ($match[$offset] as $m) {
if (!empty($m)) {
$this->matchResults[$key][smarty_strtolower_ascii($m)] = true;
}
}
}
}
/**
* Find matches in template source
*
* @param \Smarty_Internal_TemplateCompilerBase $compiler
*/
private function matchTemplateSource(Smarty_Internal_TemplateCompilerBase $compiler) {
$this->matchProperty($compiler->parser->lex->data);
}
/**
* Find matches in all parent template source
*
* @param \Smarty_Internal_TemplateCompilerBase $compiler
*
* @throws \SmartyException
*/
private function matchParentTemplateSource(Smarty_Internal_TemplateCompilerBase $compiler) {
// search parent compiler template source
$nextCompiler = $compiler;
while ($nextCompiler !== $nextCompiler->parent_compiler) {
$nextCompiler = $nextCompiler->parent_compiler;
if ($compiler !== $nextCompiler) {
// get template source
$_content = $nextCompiler->template->source->getContent();
if ($_content !== '') {
// run pre filter if required
if (isset($nextCompiler->smarty->registered_filters['pre'])) {
$_content = $nextCompiler->smarty->ext->_filterHandler->runFilter(
'pre',
$_content,
$nextCompiler->template
);
}
$this->matchProperty($_content);
}
}
}
}
/**
* Compiles code for the {$smarty.foreach.xxx} or {$smarty.section.xxx}tag
*
* @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
* @param array $parameter array with compilation parameter
*
* @return string compiled code
* @throws \SmartyCompilerException
*/
public function compileSpecialVariable(Smarty_Internal_TemplateCompilerBase $compiler, $parameter) {
$tag = smarty_strtolower_ascii(trim($parameter[0], '"\''));
$name = isset($parameter[1]) ? $compiler->getId($parameter[1]) : false;
if (!$name) {
$compiler->trigger_template_error("missing or illegal \$smarty.{$tag} name attribute", null, true);
}
$property = isset($parameter[2]) ? smarty_strtolower_ascii($compiler->getId($parameter[2])) : false;
if (!$property || !in_array($property, $this->nameProperties)) {
$compiler->trigger_template_error("missing or illegal \$smarty.{$tag} property attribute", null, true);
}
$tagVar = "'__smarty_{$tag}_{$name}'";
return "(isset(\$_smarty_tpl->tpl_vars[{$tagVar}]->value['{$property}']) ? \$_smarty_tpl->tpl_vars[{$tagVar}]->value['{$property}'] : null)";
}
}

View File

@@ -0,0 +1,66 @@
<?php
/**
* Smarty Internal Plugin Compile Make_Nocache
* Compiles the {make_nocache} tag
*
* @package Smarty
* @subpackage Compiler
* @author Uwe Tews
*/
namespace Smarty\Compile;
use Smarty\Compile\Base;
use Smarty_Internal_TemplateCompilerBase;
/**
* Smarty Internal Plugin Compile Make_Nocache Class
*
* @package Smarty
* @subpackage Compiler
*/
class MakeNocache extends Base {
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see Base
*/
protected $option_flags = [];
/**
* Array of names of required attribute required by tag
*
* @var array
*/
protected $required_attributes = ['var'];
/**
* Shorttag attribute order defined by its names
*
* @var array
*/
protected $shorttag_order = ['var'];
/**
* Compiles code for the {make_nocache} tag
*
* @param array $args array with attributes from parser
* @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
*
* @return string compiled code
*/
public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter = [], $tag = null, $function = null) {
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
if ($compiler->template->caching) {
$output = "<?php \$_smarty_tpl->smarty->ext->_make_nocache->save(\$_smarty_tpl, {$_attr[ 'var' ]});\n?>\n";
$compiler->template->compiled->has_nocache_code = true;
$compiler->suppressNocacheProcessing = true;
return $output;
} else {
return true;
}
}
}

View File

@@ -0,0 +1,125 @@
<?php
/**
* Smarty Internal Plugin Compile Block Plugin
* Compiles code for the execution of block plugin
*
* @package Smarty
* @subpackage Compiler
* @author Uwe Tews
*/
namespace Smarty\Compile;
use Smarty_Internal_TemplateCompilerBase;
/**
* Smarty Internal Plugin Compile Block Plugin Class
*
* @package Smarty
* @subpackage Compiler
*/
class PrivateBlockPlugin extends Base {
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see Base
*/
protected $optional_attributes = ['_any'];
/**
* nesting level
*
* @var int
*/
public $nesting = 0;
/**
* Compiles code for the execution of block plugin
*
* @param array $args array with attributes from parser
* @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
* @param array $parameter array with compilation parameter
* @param string $tag name of block plugin
* @param string $function PHP function name
*
* @return string compiled code
* @throws \SmartyCompilerException
* @throws \SmartyException
*/
public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter = [], $tag = null, $function = null) {
if (!isset($tag[5]) || substr($tag, -5) !== 'close') {
// opening tag of block plugin
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
$this->nesting++;
unset($_attr['nocache']);
[$callback, $_paramsArray, $callable] = $this->setup($compiler, $_attr, $tag, $function);
$_params = 'array(' . implode(',', $_paramsArray) . ')';
// compile code
$output = "<?php ";
if (is_array($callback)) {
$output .= "\$_block_plugin{$this->nesting} = isset({$callback[0]}) ? {$callback[0]} : null;\n";
$callback = "\$_block_plugin{$this->nesting}{$callback[1]}";
}
if (isset($callable)) {
$output .= "if (!is_callable({$callable})) {\nthrow new SmartyException('block tag \'{$tag}\' not callable or registered');\n}\n";
}
$output .= "\$_block_repeat=true;\necho {$callback}({$_params}, null, \$_smarty_tpl, \$_block_repeat);\nwhile (\$_block_repeat) {\nob_start();?>";
$this->openTag($compiler, $tag, [$_params, $compiler->nocache, $callback]);
// maybe nocache because of nocache variables or nocache plugin
$compiler->nocache = $compiler->nocache | $compiler->tag_nocache;
} else {
// must endblock be nocache?
if ($compiler->nocache) {
$compiler->tag_nocache = true;
}
// closing tag of block plugin, restore nocache
[$_params, $compiler->nocache, $callback] = $this->closeTag($compiler, substr($tag, 0, -5));
// compile code
if (!isset($parameter['modifier_list'])) {
$mod_pre = $mod_post = $mod_content = '';
$mod_content2 = 'ob_get_clean()';
} else {
$mod_content2 = "\$_block_content{$this->nesting}";
$mod_content = "\$_block_content{$this->nesting} = ob_get_clean();\n";
$mod_pre = "ob_start();\n";
$mod_post = 'echo ' . $compiler->compileTag(
'private_modifier',
[],
[
'modifierlist' => $parameter['modifier_list'],
'value' => 'ob_get_clean()',
]
) . ";\n";
}
$output =
"<?php {$mod_content}\$_block_repeat=false;\n{$mod_pre}echo {$callback}({$_params}, {$mod_content2}, \$_smarty_tpl, \$_block_repeat);\n{$mod_post}}\n";
$output .= '?>';
}
return $output;
}
/**
* Setup callback and parameter array
*
* @param \Smarty_Internal_TemplateCompilerBase $compiler
* @param array $_attr attributes
* @param string $tag
* @param string $function
*
* @return array
*/
protected function setup(Smarty_Internal_TemplateCompilerBase $compiler, $_attr, $tag, $function) {
$_paramsArray = [];
foreach ($_attr as $_key => $_value) {
if (is_int($_key)) {
$_paramsArray[] = "$_key=>$_value";
} else {
$_paramsArray[] = "'$_key'=>$_value";
}
}
return [$function, $_paramsArray, null];
}
}

View File

@@ -0,0 +1,82 @@
<?php
/**
* Smarty Internal Plugin Compile Function Plugin
* Compiles code for the execution of function plugin
*
* @package Smarty
* @subpackage Compiler
* @author Uwe Tews
*/
namespace Smarty\Compile;
use Smarty\Compile\Base;
use Smarty_Internal_TemplateCompilerBase;
/**
* Smarty Internal Plugin Compile Function Plugin Class
*
* @package Smarty
* @subpackage Compiler
*/
class PrivateFunctionPlugin extends Base {
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see Base
*/
protected $required_attributes = [];
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see Base
*/
protected $optional_attributes = ['_any'];
/**
* Compiles code for the execution of function plugin
*
* @param array $args array with attributes from parser
* @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
* @param array $parameter array with compilation parameter
* @param string $tag name of function plugin
* @param string $function PHP function name
*
* @return string compiled code
* @throws \SmartyCompilerException
* @throws \SmartyException
*/
public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter = [], $tag = null, $function = null) {
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
unset($_attr['nocache']);
// convert attributes into parameter array string
$_paramsArray = [];
foreach ($_attr as $_key => $_value) {
if (is_int($_key)) {
$_paramsArray[] = "$_key=>$_value";
} else {
$_paramsArray[] = "'$_key'=>$_value";
}
}
$_params = 'array(' . implode(',', $_paramsArray) . ')';
// compile code
$output = "{$function}({$_params},\$_smarty_tpl)";
if (!empty($parameter['modifierlist'])) {
$output = $compiler->compileTag(
'private_modifier',
[],
[
'modifierlist' => $parameter['modifierlist'],
'value' => $output,
]
);
}
$output = "<?php echo {$output};?>\n";
return $output;
}
}

View File

@@ -0,0 +1,46 @@
<?php
/**
* Smarty Internal Plugin Compile Object Block Function
* Compiles code for registered objects as block function
*
* @package Smarty
* @subpackage Compiler
* @author Uwe Tews
*/
namespace Smarty\Compile;
use Smarty\Compile\PrivateBlockPlugin;
use Smarty_Internal_TemplateCompilerBase;
/**
* Smarty Internal Plugin Compile Object Block Function Class
*
* @package Smarty
* @subpackage Compiler
*/
class PrivateObjectBlockFunction extends PrivateBlockPlugin {
/**
* Setup callback and parameter array
*
* @param \Smarty_Internal_TemplateCompilerBase $compiler
* @param array $_attr attributes
* @param string $tag
* @param string $function
*
* @return array
*/
protected function setup(Smarty_Internal_TemplateCompilerBase $compiler, $_attr, $tag, $function) {
$_paramsArray = [];
foreach ($_attr as $_key => $_value) {
if (is_int($_key)) {
$_paramsArray[] = "$_key=>$_value";
} else {
$_paramsArray[] = "'$_key'=>$_value";
}
}
$callback = ["\$_smarty_tpl->smarty->registered_objects['{$tag}'][0]", "->{$function}"];
return [$callback, $_paramsArray, "array(\$_block_plugin{$this->nesting}, '{$function}')"];
}
}

View File

@@ -0,0 +1,88 @@
<?php
/**
* Smarty Internal Plugin Compile Object Function
* Compiles code for registered objects as function
*
* @package Smarty
* @subpackage Compiler
* @author Uwe Tews
*/
namespace Smarty\Compile;
use Smarty_Internal_TemplateCompilerBase;
/**
* Smarty Internal Plugin Compile Object Function Class
*
* @package Smarty
* @subpackage Compiler
*/
class PrivateObjectFunction extends Base {
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see Base
*/
protected $optional_attributes = ['_any'];
/**
* Compiles code for the execution of function plugin
*
* @param array $args array with attributes from parser
* @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
* @param array $parameter array with compilation parameter
* @param string $tag name of function
* @param string $function name of method to call
*
* @return string compiled code
* @throws \SmartyCompilerException
* @throws \SmartyException
*/
public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter = [], $tag = null, $function = null) {
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
unset($_attr['nocache']);
$_assign = null;
if (isset($_attr['assign'])) {
$_assign = $_attr['assign'];
unset($_attr['assign']);
}
// method or property ?
if (is_callable([$compiler->smarty->registered_objects[$tag][0], $function])) {
// convert attributes into parameter array string
if ($compiler->smarty->registered_objects[$tag][2]) {
$_paramsArray = [];
foreach ($_attr as $_key => $_value) {
if (is_int($_key)) {
$_paramsArray[] = "$_key=>$_value";
} else {
$_paramsArray[] = "'$_key'=>$_value";
}
}
$_params = 'array(' . implode(',', $_paramsArray) . ')';
$output = "\$_smarty_tpl->smarty->registered_objects['{$tag}'][0]->{$function}({$_params},\$_smarty_tpl)";
} else {
$_params = implode(',', $_attr);
$output = "\$_smarty_tpl->smarty->registered_objects['{$tag}'][0]->{$function}({$_params})";
}
} else {
// object property
$output = "\$_smarty_tpl->smarty->registered_objects['{$tag}'][0]->{$function}";
}
if (!empty($parameter['modifierlist'])) {
$output = $compiler->compileTag(
'private_modifier',
[],
['modifierlist' => $parameter['modifierlist'], 'value' => $output]
);
}
if (empty($_assign)) {
return "<?php echo {$output};?>\n";
} else {
return "<?php \$_smarty_tpl->assign({$_assign},{$output});?>\n";
}
}
}

View File

@@ -0,0 +1,152 @@
<?php
/**
* Smarty Internal Plugin Compile Print Expression
* Compiles any tag which will output an expression or variable
*
* @package Smarty
* @subpackage Compiler
* @author Uwe Tews
*/
namespace Smarty\Compile;
use Smarty\Compile\Base;
use Smarty_Internal_TemplateCompilerBase;
/**
* Smarty Internal Plugin Compile Print Expression Class
*
* @package Smarty
* @subpackage Compiler
*/
class PrivatePrintExpression extends Base {
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see Base
*/
public $optional_attributes = ['assign'];
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see Base
*/
public $option_flags = ['nocache', 'nofilter'];
/**
* Compiles code for generating output from any expression
*
* @param array $args array with attributes from parser
* @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
* @param array $parameter array with compilation parameter
*
* @return string
* @throws \SmartyException
*/
public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter = [], $tag = null, $function = null) {
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
$output = $parameter['value'];
// tag modifier
if (!empty($parameter['modifierlist'])) {
$output = $compiler->compileTag(
'private_modifier',
[],
[
'modifierlist' => $parameter['modifierlist'],
'value' => $output,
]
);
}
if (isset($_attr['assign'])) {
// assign output to variable
return "<?php \$_smarty_tpl->assign({$_attr['assign']},{$output});?>";
} else {
// display value
if (!$_attr['nofilter']) {
// default modifier
if (!empty($compiler->smarty->default_modifiers)) {
if (empty($compiler->default_modifier_list)) {
$modifierlist = [];
foreach ($compiler->smarty->default_modifiers as $key => $single_default_modifier) {
preg_match_all(
'/(\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\'|"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"|:|[^:]+)/',
$single_default_modifier,
$mod_array
);
for ($i = 0, $count = count($mod_array[0]); $i < $count; $i++) {
if ($mod_array[0][$i] !== ':') {
$modifierlist[$key][] = $mod_array[0][$i];
}
}
}
$compiler->default_modifier_list = $modifierlist;
}
$output = $compiler->compileTag(
'private_modifier',
[],
[
'modifierlist' => $compiler->default_modifier_list,
'value' => $output,
]
);
}
// autoescape html
if ($compiler->template->smarty->escape_html) {
$output = "htmlspecialchars((string) {$output}, ENT_QUOTES, '" . addslashes(\Smarty\Smarty::$_CHARSET) . "')";
}
// loop over registered filters
if (!empty($compiler->template->smarty->registered_filters[\Smarty\Smarty::FILTER_VARIABLE])) {
foreach ($compiler->template->smarty->registered_filters[\Smarty\Smarty::FILTER_VARIABLE] as $key =>
$function) {
if (!is_array($function)) {
$output = "{$function}({$output},\$_smarty_tpl)";
} elseif (is_object($function[0])) {
$output =
"\$_smarty_tpl->smarty->registered_filters[\Smarty\Smarty::FILTER_VARIABLE]['{$key}'][0]->{$function[1]}({$output},\$_smarty_tpl)";
} else {
$output = "{$function[0]}::{$function[1]}({$output},\$_smarty_tpl)";
}
}
}
foreach ($compiler->variable_filters as $filter) {
if (count($filter) === 1
&& ($result = $this->compile_variable_filter($compiler, $filter[0], $output)) !== false
) {
$output = $result;
} else {
$output = $compiler->compileTag(
'private_modifier',
[],
['modifierlist' => [$filter], 'value' => $output]
);
}
}
}
$output = "<?php echo {$output};?>\n";
}
return $output;
}
/**
* @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
* @param string $name name of variable filter
* @param string $output embedded output
*
* @return string
* @throws \SmartyException
*/
private function compile_variable_filter(Smarty_Internal_TemplateCompilerBase $compiler, $name, $output) {
$function = $compiler->getPlugin($name, 'variablefilter');
if ($function) {
return "{$function}({$output},\$_smarty_tpl)";
} else {
// not found
return false;
}
}
}

View File

@@ -0,0 +1,94 @@
<?php
/**
* Smarty Internal Plugin Compile Registered Function
* Compiles code for the execution of a registered function
*
* @package Smarty
* @subpackage Compiler
* @author Uwe Tews
*/
namespace Smarty\Compile;
use Smarty_Internal_TemplateCompilerBase;
/**
* Smarty Internal Plugin Compile Registered Function Class
*
* @package Smarty
* @subpackage Compiler
*/
class PrivateRegisteredFunction extends Base {
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see Base
*/
public $optional_attributes = ['_any'];
/**
* Compiles code for the execution of a registered function
*
* @param array $args array with attributes from parser
* @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
* @param array $parameter array with compilation parameter
* @param string $tag name of function
*
* @return string compiled code
* @throws \SmartyCompilerException
* @throws \SmartyException
*/
public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter = [], $tag = null, $function = null) {
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
unset($_attr['nocache']);
if (isset($compiler->smarty->registered_plugins[\Smarty\Smarty::PLUGIN_FUNCTION][$tag])) {
$tag_info = $compiler->smarty->registered_plugins[\Smarty\Smarty::PLUGIN_FUNCTION][$tag];
$is_registered = true;
} else {
$tag_info = $compiler->default_handler_plugins[\Smarty\Smarty::PLUGIN_FUNCTION][$tag];
$is_registered = false;
}
// not cacheable?
$compiler->tag_nocache = $compiler->tag_nocache || !$tag_info[1];
// convert attributes into parameter array string
$_paramsArray = [];
foreach ($_attr as $_key => $_value) {
if (is_int($_key)) {
$_paramsArray[] = "$_key=>$_value";
} elseif ($compiler->template->caching && in_array($_key, $tag_info[2])) {
$_value = str_replace('\'', "^#^", $_value);
$_paramsArray[] = "'$_key'=>^#^.var_export($_value,true).^#^";
} else {
$_paramsArray[] = "'$_key'=>$_value";
}
}
$_params = 'array(' . implode(',', $_paramsArray) . ')';
// compile code
if ($is_registered) {
$output =
"call_user_func_array( \$_smarty_tpl->smarty->registered_plugins[\\Smarty\\Smarty::PLUGIN_FUNCTION]['{$tag}'][0], array( {$_params},\$_smarty_tpl ) )";
} else {
$function = $tag_info[0];
if (!is_array($function)) {
$output = "{$function}({$_params},\$_smarty_tpl)";
} else {
$output = "{$function[0]}::{$function[1]}({$_params},\$_smarty_tpl)";
}
}
if (!empty($parameter['modifierlist'])) {
$output = $compiler->compileTag(
'private_modifier',
[],
[
'modifierlist' => $parameter['modifierlist'],
'value' => $output,
]
);
}
$output = "<?php echo {$output};?>\n";
return $output;
}
}

View File

@@ -0,0 +1,127 @@
<?php
/**
* Smarty Internal Plugin Compile Special Smarty Variable
* Compiles the special $smarty variables
*
* @package Smarty
* @subpackage Compiler
* @author Uwe Tews
*/
namespace Smarty\Compile;
use _Capture;
use Smarty\Compile\Base;
use Smarty_Internal_Compile_Foreach;
use Smarty_Internal_Compile_Section;
use Smarty_Internal_TemplateCompilerBase;
/**
* Smarty Internal Plugin Compile special Smarty Variable Class
*
* @package Smarty
* @subpackage Compiler
*/
class PrivateSpecialVariable extends Base {
/**
* Compiles code for the special $smarty variables
*
* @param array $args array with attributes from parser
* @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
* @param $parameter
*
* @return string compiled code
* @throws \SmartyCompilerException
*/
public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter = [], $tag = null, $function = null) {
$_index = preg_split("/\]\[/", substr($parameter, 1, strlen($parameter) - 2));
$variable = smarty_strtolower_ascii($compiler->getId($_index[0]));
if ($variable === false) {
$compiler->trigger_template_error("special \$Smarty variable name index can not be variable", null, true);
}
if (!isset($compiler->smarty->security_policy)
|| $compiler->smarty->security_policy->isTrustedSpecialSmartyVar($variable, $compiler)
) {
switch ($variable) {
case 'foreach':
return (new Smarty_Internal_Compile_Foreach())->compileSpecialVariable($compiler, $_index);
case 'section':
return (new Smarty_Internal_Compile_Section())->compileSpecialVariable($compiler, $_index);
case 'capture':
return (new _Capture())->compileSpecialVariable($compiler, $_index);
case 'now':
return 'time()';
case 'cookies':
if (isset($compiler->smarty->security_policy)
&& !$compiler->smarty->security_policy->allow_super_globals
) {
$compiler->trigger_template_error("(secure mode) super globals not permitted");
break;
}
$compiled_ref = '$_COOKIE';
break;
case 'get':
case 'post':
case 'env':
case 'server':
case 'session':
case 'request':
if (isset($compiler->smarty->security_policy)
&& !$compiler->smarty->security_policy->allow_super_globals
) {
$compiler->trigger_template_error("(secure mode) super globals not permitted");
break;
}
$compiled_ref = '$_' . smarty_strtoupper_ascii($variable);
break;
case 'template':
return 'basename($_smarty_tpl->source->filepath)';
case 'template_object':
if (isset($compiler->smarty->security_policy)) {
$compiler->trigger_template_error("(secure mode) template_object not permitted");
break;
}
return '$_smarty_tpl';
case 'current_dir':
return 'dirname($_smarty_tpl->source->filepath)';
case 'version':
return "\\Smarty\\Smarty::SMARTY_VERSION";
case 'const':
if (isset($compiler->smarty->security_policy)
&& !$compiler->smarty->security_policy->allow_constants
) {
$compiler->trigger_template_error("(secure mode) constants not permitted");
break;
}
if (strpos($_index[1], '$') === false && strpos($_index[1], '\'') === false) {
return "(defined('{$_index[1]}') ? constant('{$_index[1]}') : null)";
} else {
return "(defined({$_index[1]}) ? constant({$_index[1]}) : null)";
}
// no break
case 'config':
if (isset($_index[2])) {
return "(is_array(\$tmp = \$_smarty_tpl->smarty->ext->configload->_getConfigVariable(\$_smarty_tpl, $_index[1])) ? \$tmp[$_index[2]] : null)";
} else {
return "\$_smarty_tpl->smarty->ext->configload->_getConfigVariable(\$_smarty_tpl, $_index[1])";
}
// no break
case 'ldelim':
return "\$_smarty_tpl->smarty->left_delimiter";
case 'rdelim':
return "\$_smarty_tpl->smarty->right_delimiter";
default:
$compiler->trigger_template_error('$smarty.' . trim($_index[0], "'") . ' is not defined');
break;
}
if (isset($_index[1])) {
array_shift($_index);
foreach ($_index as $_ind) {
$compiled_ref = $compiled_ref . "[$_ind]";
}
}
return $compiled_ref;
}
}
}

View File

@@ -1,64 +0,0 @@
<?php
/**
* Smarty Internal Plugin Compile Make_Nocache
* Compiles the {make_nocache} tag
*
* @package Smarty
* @subpackage Compiler
* @author Uwe Tews
*/
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile Make_Nocache Class
*
* @package Smarty
* @subpackage Compiler
*/
class _Make_Nocache extends Base
{
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see Base
*/
public $option_flags = array();
/**
* Array of names of required attribute required by tag
*
* @var array
*/
public $required_attributes = array('var');
/**
* Shorttag attribute order defined by its names
*
* @var array
*/
public $shorttag_order = array('var');
/**
* Compiles code for the {make_nocache} tag
*
* @param array $args array with attributes from parser
* @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
*
* @return string compiled code
*/
public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter = array(), $tag = null, $function = null)
{
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
if ($compiler->template->caching) {
$output = "<?php \$_smarty_tpl->smarty->ext->_make_nocache->save(\$_smarty_tpl, {$_attr[ 'var' ]});\n?>\n";
$compiler->template->compiled->has_nocache_code = true;
$compiler->suppressNocacheProcessing = true;
return $output;
} else {
return true;
}
}
}

View File

@@ -1,125 +0,0 @@
<?php
/**
* Smarty Internal Plugin Compile Block Plugin
* Compiles code for the execution of block plugin
*
* @package Smarty
* @subpackage Compiler
* @author Uwe Tews
*/
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile Block Plugin Class
*
* @package Smarty
* @subpackage Compiler
*/
class _Private_Block_Plugin extends Base
{
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see Base
*/
public $optional_attributes = array('_any');
/**
* nesting level
*
* @var int
*/
public $nesting = 0;
/**
* Compiles code for the execution of block plugin
*
* @param array $args array with attributes from parser
* @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
* @param array $parameter array with compilation parameter
* @param string $tag name of block plugin
* @param string $function PHP function name
*
* @return string compiled code
* @throws \SmartyCompilerException
* @throws \SmartyException
*/
public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter = array(), $tag = null, $function = null)
{
if (!isset($tag[ 5 ]) || substr($tag, -5) !== 'close') {
// opening tag of block plugin
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
$this->nesting++;
unset($_attr[ 'nocache' ]);
[$callback, $_paramsArray, $callable] = $this->setup($compiler, $_attr, $tag, $function);
$_params = 'array(' . implode(',', $_paramsArray) . ')';
// compile code
$output = "<?php ";
if (is_array($callback)) {
$output .= "\$_block_plugin{$this->nesting} = isset({$callback[0]}) ? {$callback[0]} : null;\n";
$callback = "\$_block_plugin{$this->nesting}{$callback[1]}";
}
if (isset($callable)) {
$output .= "if (!is_callable({$callable})) {\nthrow new SmartyException('block tag \'{$tag}\' not callable or registered');\n}\n";
}
$output .= "\$_block_repeat=true;\necho {$callback}({$_params}, null, \$_smarty_tpl, \$_block_repeat);\nwhile (\$_block_repeat) {\nob_start();?>";
$this->openTag($compiler, $tag, array($_params, $compiler->nocache, $callback));
// maybe nocache because of nocache variables or nocache plugin
$compiler->nocache = $compiler->nocache | $compiler->tag_nocache;
} else {
// must endblock be nocache?
if ($compiler->nocache) {
$compiler->tag_nocache = true;
}
// closing tag of block plugin, restore nocache
[$_params, $compiler->nocache, $callback] = $this->closeTag($compiler, substr($tag, 0, -5));
// compile code
if (!isset($parameter[ 'modifier_list' ])) {
$mod_pre = $mod_post = $mod_content = '';
$mod_content2 = 'ob_get_clean()';
} else {
$mod_content2 = "\$_block_content{$this->nesting}";
$mod_content = "\$_block_content{$this->nesting} = ob_get_clean();\n";
$mod_pre = "ob_start();\n";
$mod_post = 'echo ' . $compiler->compileTag(
'private_modifier',
array(),
array(
'modifierlist' => $parameter[ 'modifier_list' ],
'value' => 'ob_get_clean()'
)
) . ";\n";
}
$output =
"<?php {$mod_content}\$_block_repeat=false;\n{$mod_pre}echo {$callback}({$_params}, {$mod_content2}, \$_smarty_tpl, \$_block_repeat);\n{$mod_post}}\n";
$output .= '?>';
}
return $output;
}
/**
* Setup callback and parameter array
*
* @param \Smarty_Internal_TemplateCompilerBase $compiler
* @param array $_attr attributes
* @param string $tag
* @param string $function
*
* @return array
*/
public function setup(Smarty_Internal_TemplateCompilerBase $compiler, $_attr, $tag, $function)
{
$_paramsArray = array();
foreach ($_attr as $_key => $_value) {
if (is_int($_key)) {
$_paramsArray[] = "$_key=>$_value";
} else {
$_paramsArray[] = "'$_key'=>$_value";
}
}
return array($function, $_paramsArray, null);
}
}

View File

@@ -1,228 +0,0 @@
<?php
/**
* Smarty Internal Plugin Compile ForeachSection
* Shared methods for {foreach} {section} tags
*
* @package Smarty
* @subpackage Compiler
* @author Uwe Tews
*/
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile ForeachSection Class
*
* @package Smarty
* @subpackage Compiler
*/
class _Private_ForeachSection extends Base
{
/**
* Name of this tag
*
* @var string
*/
public $tagName = '';
/**
* Valid properties of $smarty.xxx variable
*
* @var array
*/
public $nameProperties = array();
/**
* {section} tag has no item properties
*
* @var array
*/
public $itemProperties = null;
/**
* {section} tag has always name attribute
*
* @var bool
*/
public $isNamed = true;
/**
* @var array
*/
public $matchResults = array();
/**
* Preg search pattern
*
* @var string
*/
private $propertyPreg = '';
/**
* Offsets in preg match result
*
* @var array
*/
private $resultOffsets = array();
/**
* Start offset
*
* @var int
*/
private $startOffset = 0;
/**
* Scan sources for used tag attributes
*
* @param array $attributes
* @param \Smarty_Internal_TemplateCompilerBase $compiler
*
* @throws \SmartyException
*/
public function scanForProperties($attributes, Smarty_Internal_TemplateCompilerBase $compiler)
{
$this->propertyPreg = '~(';
$this->startOffset = 1;
$this->resultOffsets = array();
$this->matchResults = array('named' => array(), 'item' => array());
if (isset($attributes[ 'name' ])) {
$this->buildPropertyPreg(true, $attributes);
}
if (isset($this->itemProperties)) {
if ($this->isNamed) {
$this->propertyPreg .= '|';
}
$this->buildPropertyPreg(false, $attributes);
}
$this->propertyPreg .= ')\W~i';
// Template source
$this->matchTemplateSource($compiler);
// Parent template source
$this->matchParentTemplateSource($compiler);
// {block} source
$this->matchBlockSource($compiler);
}
/**
* Build property preg string
*
* @param bool $named
* @param array $attributes
*/
public function buildPropertyPreg($named, $attributes)
{
if ($named) {
$this->resultOffsets[ 'named' ] = $this->startOffset = $this->startOffset + 3;
$this->propertyPreg .= "(([\$]smarty[.]{$this->tagName}[.]" .
($this->tagName === 'section' ? "|[\[]\s*" : '') .
"){$attributes['name']}[.](";
$properties = $this->nameProperties;
} else {
$this->resultOffsets[ 'item' ] = $this->startOffset = $this->startOffset + 2;
$this->propertyPreg .= "([\$]{$attributes['item']}[@](";
$properties = $this->itemProperties;
}
$propName = reset($properties);
while ($propName) {
$this->propertyPreg .= "{$propName}";
$propName = next($properties);
if ($propName) {
$this->propertyPreg .= '|';
}
}
$this->propertyPreg .= '))';
}
/**
* Find matches in source string
*
* @param string $source
*/
public function matchProperty($source)
{
preg_match_all($this->propertyPreg, $source, $match);
foreach ($this->resultOffsets as $key => $offset) {
foreach ($match[ $offset ] as $m) {
if (!empty($m)) {
$this->matchResults[ $key ][ smarty_strtolower_ascii($m) ] = true;
}
}
}
}
/**
* Find matches in template source
*
* @param \Smarty_Internal_TemplateCompilerBase $compiler
*/
public function matchTemplateSource(Smarty_Internal_TemplateCompilerBase $compiler)
{
$this->matchProperty($compiler->parser->lex->data);
}
/**
* Find matches in all parent template source
*
* @param \Smarty_Internal_TemplateCompilerBase $compiler
*
* @throws \SmartyException
*/
public function matchParentTemplateSource(Smarty_Internal_TemplateCompilerBase $compiler)
{
// search parent compiler template source
$nextCompiler = $compiler;
while ($nextCompiler !== $nextCompiler->parent_compiler) {
$nextCompiler = $nextCompiler->parent_compiler;
if ($compiler !== $nextCompiler) {
// get template source
$_content = $nextCompiler->template->source->getContent();
if ($_content !== '') {
// run pre filter if required
if (isset($nextCompiler->smarty->registered_filters[ 'pre' ])) {
$_content = $nextCompiler->smarty->ext->_filterHandler->runFilter(
'pre',
$_content,
$nextCompiler->template
);
}
$this->matchProperty($_content);
}
}
}
}
/**
* Find matches in {block} tag source
*
* @param \Smarty_Internal_TemplateCompilerBase $compiler
*/
public function matchBlockSource(Smarty_Internal_TemplateCompilerBase $compiler)
{
}
/**
* Compiles code for the {$smarty.foreach.xxx} or {$smarty.section.xxx}tag
*
* @param array $args array with attributes from parser
* @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
* @param array $parameter array with compilation parameter
*
* @return string compiled code
* @throws \SmartyCompilerException
*/
public function compileSpecialVariable($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
{
$tag = smarty_strtolower_ascii(trim($parameter[ 0 ], '"\''));
$name = isset($parameter[ 1 ]) ? $compiler->getId($parameter[ 1 ]) : false;
if (!$name) {
$compiler->trigger_template_error("missing or illegal \$smarty.{$tag} name attribute", null, true);
}
$property = isset($parameter[ 2 ]) ? smarty_strtolower_ascii($compiler->getId($parameter[ 2 ])) : false;
if (!$property || !in_array($property, $this->nameProperties)) {
$compiler->trigger_template_error("missing or illegal \$smarty.{$tag} property attribute", null, true);
}
$tagVar = "'__smarty_{$tag}_{$name}'";
return "(isset(\$_smarty_tpl->tpl_vars[{$tagVar}]->value['{$property}']) ? \$_smarty_tpl->tpl_vars[{$tagVar}]->value['{$property}'] : null)";
}
}

View File

@@ -1,80 +0,0 @@
<?php
/**
* Smarty Internal Plugin Compile Function Plugin
* Compiles code for the execution of function plugin
*
* @package Smarty
* @subpackage Compiler
* @author Uwe Tews
*/
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile Function Plugin Class
*
* @package Smarty
* @subpackage Compiler
*/
class _Private_Function_Plugin extends Base
{
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see Base
*/
public $required_attributes = array();
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see Base
*/
public $optional_attributes = array('_any');
/**
* Compiles code for the execution of function plugin
*
* @param array $args array with attributes from parser
* @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
* @param array $parameter array with compilation parameter
* @param string $tag name of function plugin
* @param string $function PHP function name
*
* @return string compiled code
* @throws \SmartyCompilerException
* @throws \SmartyException
*/
public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter = array(), $tag = null, $function = null)
{
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
unset($_attr[ 'nocache' ]);
// convert attributes into parameter array string
$_paramsArray = array();
foreach ($_attr as $_key => $_value) {
if (is_int($_key)) {
$_paramsArray[] = "$_key=>$_value";
} else {
$_paramsArray[] = "'$_key'=>$_value";
}
}
$_params = 'array(' . implode(',', $_paramsArray) . ')';
// compile code
$output = "{$function}({$_params},\$_smarty_tpl)";
if (!empty($parameter[ 'modifierlist' ])) {
$output = $compiler->compileTag(
'private_modifier',
array(),
array(
'modifierlist' => $parameter[ 'modifierlist' ],
'value' => $output
)
);
}
$output = "<?php echo {$output};?>\n";
return $output;
}
}

View File

@@ -1,87 +0,0 @@
<?php
/**
* Smarty Internal Plugin Compile Object Function
* Compiles code for registered objects as function
*
* @package Smarty
* @subpackage Compiler
* @author Uwe Tews
*/
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile Object Function Class
*
* @package Smarty
* @subpackage Compiler
*/
class _Private_Object_Function extends Base
{
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see Base
*/
public $optional_attributes = array('_any');
/**
* Compiles code for the execution of function plugin
*
* @param array $args array with attributes from parser
* @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
* @param array $parameter array with compilation parameter
* @param string $tag name of function
* @param string $function name of method to call
*
* @return string compiled code
* @throws \SmartyCompilerException
* @throws \SmartyException
*/
public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter = array(), $tag = null, $function = null)
{
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
unset($_attr[ 'nocache' ]);
$_assign = null;
if (isset($_attr[ 'assign' ])) {
$_assign = $_attr[ 'assign' ];
unset($_attr[ 'assign' ]);
}
// method or property ?
if (is_callable(array($compiler->smarty->registered_objects[ $tag ][ 0 ], $function))) {
// convert attributes into parameter array string
if ($compiler->smarty->registered_objects[ $tag ][ 2 ]) {
$_paramsArray = array();
foreach ($_attr as $_key => $_value) {
if (is_int($_key)) {
$_paramsArray[] = "$_key=>$_value";
} else {
$_paramsArray[] = "'$_key'=>$_value";
}
}
$_params = 'array(' . implode(',', $_paramsArray) . ')';
$output = "\$_smarty_tpl->smarty->registered_objects['{$tag}'][0]->{$function}({$_params},\$_smarty_tpl)";
} else {
$_params = implode(',', $_attr);
$output = "\$_smarty_tpl->smarty->registered_objects['{$tag}'][0]->{$function}({$_params})";
}
} else {
// object property
$output = "\$_smarty_tpl->smarty->registered_objects['{$tag}'][0]->{$function}";
}
if (!empty($parameter[ 'modifierlist' ])) {
$output = $compiler->compileTag(
'private_modifier',
array(),
array('modifierlist' => $parameter[ 'modifierlist' ], 'value' => $output)
);
}
if (empty($_assign)) {
return "<?php echo {$output};?>\n";
} else {
return "<?php \$_smarty_tpl->assign({$_assign},{$output});?>\n";
}
}
}

View File

@@ -1,151 +0,0 @@
<?php
/**
* Smarty Internal Plugin Compile Print Expression
* Compiles any tag which will output an expression or variable
*
* @package Smarty
* @subpackage Compiler
* @author Uwe Tews
*/
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile Print Expression Class
*
* @package Smarty
* @subpackage Compiler
*/
class _Private_Print_Expression extends Base
{
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see Base
*/
public $optional_attributes = array('assign');
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see Base
*/
public $option_flags = array('nocache', 'nofilter');
/**
* Compiles code for generating output from any expression
*
* @param array $args array with attributes from parser
* @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
* @param array $parameter array with compilation parameter
*
* @return string
* @throws \SmartyException
*/
public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter = array(), $tag = null, $function = null)
{
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
$output = $parameter[ 'value' ];
// tag modifier
if (!empty($parameter[ 'modifierlist' ])) {
$output = $compiler->compileTag(
'private_modifier',
array(),
array(
'modifierlist' => $parameter[ 'modifierlist' ],
'value' => $output
)
);
}
if (isset($_attr[ 'assign' ])) {
// assign output to variable
return "<?php \$_smarty_tpl->assign({$_attr['assign']},{$output});?>";
} else {
// display value
if (!$_attr[ 'nofilter' ]) {
// default modifier
if (!empty($compiler->smarty->default_modifiers)) {
if (empty($compiler->default_modifier_list)) {
$modifierlist = array();
foreach ($compiler->smarty->default_modifiers as $key => $single_default_modifier) {
preg_match_all(
'/(\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\'|"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"|:|[^:]+)/',
$single_default_modifier,
$mod_array
);
for ($i = 0, $count = count($mod_array[ 0 ]); $i < $count; $i++) {
if ($mod_array[ 0 ][ $i ] !== ':') {
$modifierlist[ $key ][] = $mod_array[ 0 ][ $i ];
}
}
}
$compiler->default_modifier_list = $modifierlist;
}
$output = $compiler->compileTag(
'private_modifier',
array(),
array(
'modifierlist' => $compiler->default_modifier_list,
'value' => $output
)
);
}
// autoescape html
if ($compiler->template->smarty->escape_html) {
$output = "htmlspecialchars((string) {$output}, ENT_QUOTES, '" . addslashes(\Smarty\Smarty::$_CHARSET) . "')";
}
// loop over registered filters
if (!empty($compiler->template->smarty->registered_filters[ \Smarty\Smarty::FILTER_VARIABLE ])) {
foreach ($compiler->template->smarty->registered_filters[ \Smarty\Smarty::FILTER_VARIABLE ] as $key =>
$function) {
if (!is_array($function)) {
$output = "{$function}({$output},\$_smarty_tpl)";
} elseif (is_object($function[ 0 ])) {
$output =
"\$_smarty_tpl->smarty->registered_filters[\Smarty\Smarty::FILTER_VARIABLE]['{$key}'][0]->{$function[1]}({$output},\$_smarty_tpl)";
} else {
$output = "{$function[0]}::{$function[1]}({$output},\$_smarty_tpl)";
}
}
}
foreach ($compiler->variable_filters as $filter) {
if (count($filter) === 1
&& ($result = $this->compile_variable_filter($compiler, $filter[ 0 ], $output)) !== false
) {
$output = $result;
} else {
$output = $compiler->compileTag(
'private_modifier',
array(),
array('modifierlist' => array($filter), 'value' => $output)
);
}
}
}
$output = "<?php echo {$output};?>\n";
}
return $output;
}
/**
* @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
* @param string $name name of variable filter
* @param string $output embedded output
*
* @return string
* @throws \SmartyException
*/
private function compile_variable_filter(Smarty_Internal_TemplateCompilerBase $compiler, $name, $output)
{
$function = $compiler->getPlugin($name, 'variablefilter');
if ($function) {
return "{$function}({$output},\$_smarty_tpl)";
} else {
// not found
return false;
}
}
}

View File

@@ -1,93 +0,0 @@
<?php
/**
* Smarty Internal Plugin Compile Registered Function
* Compiles code for the execution of a registered function
*
* @package Smarty
* @subpackage Compiler
* @author Uwe Tews
*/
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile Registered Function Class
*
* @package Smarty
* @subpackage Compiler
*/
class _Private_Registered_Function extends Base
{
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see Base
*/
public $optional_attributes = array('_any');
/**
* Compiles code for the execution of a registered function
*
* @param array $args array with attributes from parser
* @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
* @param array $parameter array with compilation parameter
* @param string $tag name of function
*
* @return string compiled code
* @throws \SmartyCompilerException
* @throws \SmartyException
*/
public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter = array(), $tag = null, $function = null)
{
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
unset($_attr[ 'nocache' ]);
if (isset($compiler->smarty->registered_plugins[ \Smarty\Smarty::PLUGIN_FUNCTION ][ $tag ])) {
$tag_info = $compiler->smarty->registered_plugins[ \Smarty\Smarty::PLUGIN_FUNCTION ][ $tag ];
$is_registered = true;
} else {
$tag_info = $compiler->default_handler_plugins[ \Smarty\Smarty::PLUGIN_FUNCTION ][ $tag ];
$is_registered = false;
}
// not cacheable?
$compiler->tag_nocache = $compiler->tag_nocache || !$tag_info[ 1 ];
// convert attributes into parameter array string
$_paramsArray = array();
foreach ($_attr as $_key => $_value) {
if (is_int($_key)) {
$_paramsArray[] = "$_key=>$_value";
} elseif ($compiler->template->caching && in_array($_key, $tag_info[ 2 ])) {
$_value = str_replace('\'', "^#^", $_value);
$_paramsArray[] = "'$_key'=>^#^.var_export($_value,true).^#^";
} else {
$_paramsArray[] = "'$_key'=>$_value";
}
}
$_params = 'array(' . implode(',', $_paramsArray) . ')';
// compile code
if ($is_registered) {
$output =
"call_user_func_array( \$_smarty_tpl->smarty->registered_plugins[\\Smarty\\Smarty::PLUGIN_FUNCTION]['{$tag}'][0], array( {$_params},\$_smarty_tpl ) )";
} else {
$function = $tag_info[ 0 ];
if (!is_array($function)) {
$output = "{$function}({$_params},\$_smarty_tpl)";
} else {
$output = "{$function[0]}::{$function[1]}({$_params},\$_smarty_tpl)";
}
}
if (!empty($parameter[ 'modifierlist' ])) {
$output = $compiler->compileTag(
'private_modifier',
array(),
array(
'modifierlist' => $parameter[ 'modifierlist' ],
'value' => $output
)
);
}
$output = "<?php echo {$output};?>\n";
return $output;
}
}

View File

@@ -1,132 +0,0 @@
<?php
/**
* Smarty Internal Plugin Compile Special Smarty Variable
* Compiles the special $smarty variables
*
* @package Smarty
* @subpackage Compiler
* @author Uwe Tews
*/
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile special Smarty Variable Class
*
* @package Smarty
* @subpackage Compiler
*/
class _Private_Special_Variable extends Base
{
/**
* Compiles code for the special $smarty variables
*
* @param array $args array with attributes from parser
* @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
* @param $parameter
*
* @return string compiled code
* @throws \SmartyCompilerException
*/
public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter = array(), $tag = null, $function = null)
{
$_index = preg_split("/\]\[/", substr($parameter, 1, strlen($parameter) - 2));
$variable = smarty_strtolower_ascii($compiler->getId($_index[ 0 ]));
if ($variable === false) {
$compiler->trigger_template_error("special \$Smarty variable name index can not be variable", null, true);
}
if (!isset($compiler->smarty->security_policy)
|| $compiler->smarty->security_policy->isTrustedSpecialSmartyVar($variable, $compiler)
) {
switch ($variable) {
case 'foreach':
case 'section':
if (!isset(Smarty_Internal_TemplateCompilerBase::$_tag_objects[ $variable ])) {
$class = 'Smarty_Internal_Compile_' . smarty_ucfirst_ascii($variable);
Smarty_Internal_TemplateCompilerBase::$_tag_objects[ $variable ] = new $class;
}
return Smarty_Internal_TemplateCompilerBase::$_tag_objects[ $variable ]->compileSpecialVariable(
array(),
$compiler,
$_index
);
case 'capture':
if (class_exists('_Capture')) {
return _Capture::compileSpecialVariable(array(), $compiler, $_index);
}
return '';
case 'now':
return 'time()';
case 'cookies':
if (isset($compiler->smarty->security_policy)
&& !$compiler->smarty->security_policy->allow_super_globals
) {
$compiler->trigger_template_error("(secure mode) super globals not permitted");
break;
}
$compiled_ref = '$_COOKIE';
break;
case 'get':
case 'post':
case 'env':
case 'server':
case 'session':
case 'request':
if (isset($compiler->smarty->security_policy)
&& !$compiler->smarty->security_policy->allow_super_globals
) {
$compiler->trigger_template_error("(secure mode) super globals not permitted");
break;
}
$compiled_ref = '$_' . smarty_strtoupper_ascii($variable);
break;
case 'template':
return 'basename($_smarty_tpl->source->filepath)';
case 'template_object':
if (isset($compiler->smarty->security_policy)) {
$compiler->trigger_template_error("(secure mode) template_object not permitted");
break;
}
return '$_smarty_tpl';
case 'current_dir':
return 'dirname($_smarty_tpl->source->filepath)';
case 'version':
return "\\Smarty\\Smarty::SMARTY_VERSION";
case 'const':
if (isset($compiler->smarty->security_policy)
&& !$compiler->smarty->security_policy->allow_constants
) {
$compiler->trigger_template_error("(secure mode) constants not permitted");
break;
}
if (strpos($_index[ 1 ], '$') === false && strpos($_index[ 1 ], '\'') === false) {
return "(defined('{$_index[1]}') ? constant('{$_index[1]}') : null)";
} else {
return "(defined({$_index[1]}) ? constant({$_index[1]}) : null)";
}
// no break
case 'config':
if (isset($_index[ 2 ])) {
return "(is_array(\$tmp = \$_smarty_tpl->smarty->ext->configload->_getConfigVariable(\$_smarty_tpl, $_index[1])) ? \$tmp[$_index[2]] : null)";
} else {
return "\$_smarty_tpl->smarty->ext->configload->_getConfigVariable(\$_smarty_tpl, $_index[1])";
}
// no break
case 'ldelim':
return "\$_smarty_tpl->smarty->left_delimiter";
case 'rdelim':
return "\$_smarty_tpl->smarty->right_delimiter";
default:
$compiler->trigger_template_error('$smarty.' . trim($_index[ 0 ], "'") . ' is not defined');
break;
}
if (isset($_index[ 1 ])) {
array_shift($_index);
foreach ($_index as $_ind) {
$compiled_ref = $compiled_ref . "[$_ind]";
}
}
return $compiled_ref;
}
}
}

View File

@@ -8,13 +8,15 @@
* @author Uwe Tews
*/
use Smarty\Compile\PrivateBlockPlugin;
/**
* Smarty Internal Plugin Compile Registered Block Class
*
* @package Smarty
* @subpackage Compiler
*/
class Smarty_Internal_Compile_Private_Registered_Block extends _Private_Block_Plugin
class Smarty_Internal_Compile__Registered_Private_Block extends PrivateBlockPlugin
{
/**
* Setup callback, parameter array and nocache mode
@@ -26,7 +28,7 @@ class Smarty_Internal_Compile_Private_Registered_Block extends _Private_Block_Pl
*
* @return array
*/
public function setup(Smarty_Internal_TemplateCompilerBase $compiler, $_attr, $tag, $function)
protected function setup(Smarty_Internal_TemplateCompilerBase $compiler, $_attr, $tag, $function)
{
if (isset($compiler->smarty->registered_plugins[ \Smarty\Smarty::PLUGIN_BLOCK ][ $tag ])) {
$tag_info = $compiler->smarty->registered_plugins[ \Smarty\Smarty::PLUGIN_BLOCK ][ $tag ];

View File

@@ -37,14 +37,12 @@ class _Capture extends Base
/**
* Compiles code for the {$smarty.capture.xxx}
*
* @param array $args array with attributes from parser
* @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
* @param array $parameter array with compilation parameter
*
* @return string compiled code
*/
public static function compileSpecialVariable(
$args,
Smarty_Internal_TemplateCompilerBase $compiler,
$parameter = null
) {

View File

@@ -8,6 +8,7 @@
* @author Uwe Tews
*/
use Smarty\Compile\ForeachSection;
use Smarty\Compile\Base;
/**
@@ -16,7 +17,7 @@ use Smarty\Compile\Base;
* @package Smarty
* @subpackage Compiler
*/
class Smarty_Internal_Compile_Foreach extends _Private_ForeachSection
class Smarty_Internal_Compile_Foreach extends ForeachSection
{
/**
* Attribute definition: Overwrites base class.

View File

@@ -1,42 +0,0 @@
<?php
/**
* Smarty Internal Plugin Compile Object Block Function
* Compiles code for registered objects as block function
*
* @package Smarty
* @subpackage Compiler
* @author Uwe Tews
*/
/**
* Smarty Internal Plugin Compile Object Block Function Class
*
* @package Smarty
* @subpackage Compiler
*/
class Smarty_Internal_Compile_Private_Object_Block_Function extends _Private_Block_Plugin
{
/**
* Setup callback and parameter array
*
* @param \Smarty_Internal_TemplateCompilerBase $compiler
* @param array $_attr attributes
* @param string $tag
* @param string $method
*
* @return array
*/
public function setup(Smarty_Internal_TemplateCompilerBase $compiler, $_attr, $tag, $method)
{
$_paramsArray = array();
foreach ($_attr as $_key => $_value) {
if (is_int($_key)) {
$_paramsArray[] = "$_key=>$_value";
} else {
$_paramsArray[] = "'$_key'=>$_value";
}
}
$callback = array("\$_smarty_tpl->smarty->registered_objects['{$tag}'][0]", "->{$method}");
return array($callback, $_paramsArray, "array(\$_block_plugin{$this->nesting}, '{$method}')");
}
}

View File

@@ -8,6 +8,7 @@
* @author Uwe Tews
*/
use Smarty\Compile\ForeachSection;
use Smarty\Compile\Base;
/**
@@ -16,7 +17,7 @@ use Smarty\Compile\Base;
* @package Smarty
* @subpackage Compiler
*/
class Smarty_Internal_Compile_Section extends _Private_ForeachSection
class Smarty_Internal_Compile_Section extends ForeachSection
{
/**
* Attribute definition: Overwrites base class.

View File

@@ -23,12 +23,6 @@ use Smarty\Compile\Base;
*/
abstract class Smarty_Internal_TemplateCompilerBase
{
/**
* compile tag objects cache
*
* @var array
*/
public static $_tag_objects = array();
/**
* counter for prefix variable number
@@ -729,40 +723,44 @@ abstract class Smarty_Internal_TemplateCompilerBase
*
* @param string $tag tag name
*
* @return bool|\Smarty\Compile\Base tag compiler object or false if not found
* @return bool|Base tag compiler object or false if not found or untrusted by security policy
*/
public function getTagCompiler($tag)
{
static $map = [
'break' => \Smarty\Compile\BreakTag::class,
'config_load' => \Smarty\Compile\ConfigLoad::class,
'eval' => \Smarty\Compile\EvalTag::class,
'include' => \Smarty\Compile\IncludeTag::class,
'while' => \Smarty\Compile\WhileTag::class,
'private_modifier' => \Smarty\Compile\PrivateModifier::class,
];
// re-use object if already exists
if (!isset(self::$_tag_objects[ $tag ])) {
if (isset($map[$tag])) {
$class_name = $map[$tag];
} else {
$_tag = explode('_', $tag);
$_tag = array_map('smarty_ucfirst_ascii', $_tag);
$class_name = '\\Smarty\\Compile\\' . implode('_', $_tag);
if (isset($this->smarty->security_policy) && !$this->smarty->security_policy->isTrustedTag($tag, $this)) {
return false;
}
if (class_exists($class_name)
&& (!isset($this->smarty->security_policy) || $this->smarty->security_policy->isTrustedTag($tag, $this))
) {
self::$_tag_objects[ $tag ] = new $class_name;
} else {
self::$_tag_objects[ $tag ] = false;
switch ($tag) {
case 'append': return new \Smarty\Compile\Append();
case 'assign': return new \Smarty\Compile\Assign();
case 'block': return new \Smarty\Compile\Block();
case 'blockclose': return new \Smarty\Compile\Blockclose();
case 'break': return new \Smarty\Compile\BreakTag();
case 'call': return new \Smarty\Compile\Call();
case 'child': return new \Smarty\Compile\Child();
case 'config_load': return new \Smarty\Compile\ConfigLoad();
case 'continue': return new \Smarty\Compile\ContinueTag();
case 'debug': return new \Smarty\Compile\Debug();
case 'eval': return new \Smarty\Compile\EvalTag();
case 'include': return new \Smarty\Compile\IncludeTag();
case 'insert': return new \Smarty\Compile\Inser();
case 'ldelim': return new \Smarty\Compile\Ldelim();
case 'make_nocache': return new \Smarty\Compile\MakeNocache();
case 'private_block_plugin': return new \Smarty\Compile\PrivateBlockPlugin();
case 'private_function_plugin': return new \Smarty\Compile\PrivateFunctionPlugin();
case 'private_modifier': return new \Smarty\Compile\PrivateModifier();
case 'private_object_function': return new \Smarty\Compile\PrivateObjectFunction();
case 'private_object_block_function': return new \Smarty\Compile\PrivateObjectBlockFunction();
case 'private_print_expression': return new \Smarty\Compile\PrivatePrintExpression();
case 'private_registered_function': return new \Smarty\Compile\PrivateRegisteredFunction();
case 'private_special_variable': return new \Smarty\Compile\PrivateSpecialVariable();
case 'while': return new \Smarty\Compile\WhileTag();
case 'whileclose': return new \Smarty\Compile\Whileclose();
}
}
return self::$_tag_objects[ $tag ];
return false;
}
/**

View File

@@ -650,11 +650,6 @@ KEY `name` (`name`)
*/
protected function tearDown(): void
{
if (class_exists('Smarty_Internal_TemplateCompilerBase') &&
isset(Smarty_Internal_TemplateCompilerBase::$_tag_objects)
) {
Smarty_Internal_TemplateCompilerBase::$_tag_objects = array();
}
if (isset($this->smarty->smarty)) {
$this->smarty->smarty = null;
}