Initial checkin

This commit is contained in:
genuineparts 2025-06-20 19:10:23 +02:00
commit d75eb444fc
4304 changed files with 369634 additions and 0 deletions

View file

@ -0,0 +1,233 @@
<?php
/**
* Smarty Internal Compile Plugin Base
* @author Uwe Tews
*/
namespace Smarty\Compile;
use Smarty\Compiler\Template;
use Smarty\Data;
use Smarty\Exception;
/**
* This class does extend all internal compile plugins
*
*/
abstract class Base implements CompilerInterface {
/**
* Array of names of required attribute required by tag
*
* @var array
*/
protected $required_attributes = [];
/**
* Array of names of optional attribute required by tag
* use array('_any') if there is no restriction of attributes names
*
* @var array
*/
protected $optional_attributes = [];
/**
* Shorttag attribute order defined by its names
*
* @var array
*/
protected $shorttag_order = [];
/**
* Array of names of valid option flags
*
* @var array
*/
protected $option_flags = ['nocache'];
/**
* @var bool
*/
protected $cacheable = true;
public function isCacheable(): bool {
return $this->cacheable;
}
/**
* Converts attributes into parameter array strings
*
* @param array $_attr
*
* @return array
*/
protected function formatParamsArray(array $_attr): array {
$_paramsArray = [];
foreach ($_attr as $_key => $_value) {
$_paramsArray[] = var_export($_key, true) . "=>" . $_value;
}
return $_paramsArray;
}
/**
* 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
* as valid
*
* @param object $compiler compiler object
* @param array $attributes attributes applied to the tag
*
* @return array of mapped attributes for further processing
*/
protected function getAttributes($compiler, $attributes) {
$_indexed_attr = [];
$options = array_fill_keys($this->option_flags, true);
foreach ($attributes as $key => $mixed) {
// shorthand ?
if (!is_array($mixed)) {
// options flag ?
if (isset($options[trim($mixed, '\'"')])) {
$_indexed_attr[trim($mixed, '\'"')] = true;
// shorthand attribute ?
} 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', null, true);
}
// named attribute
} else {
foreach ($mixed as $k => $v) {
// options flag?
if (isset($options[$k])) {
if (is_bool($v)) {
$_indexed_attr[$k] = $v;
} else {
if (is_string($v)) {
$v = trim($v, '\'" ');
}
// Mapping array for boolean option value
static $optionMap = [1 => true, 0 => false, 'true' => true, 'false' => false];
if (isset($optionMap[$v])) {
$_indexed_attr[$k] = $optionMap[$v];
} else {
$compiler->trigger_template_error(
"illegal value '" . var_export($v, true) .
"' for options flag '{$k}'",
null,
true
);
}
}
// must be named attribute
} else {
$_indexed_attr[$k] = $v;
}
}
}
}
// check if all required attributes present
foreach ($this->required_attributes as $attr) {
if (!isset($_indexed_attr[$attr])) {
$compiler->trigger_template_error("missing '{$attr}' attribute", null, true);
}
}
// check for not allowed attributes
if ($this->optional_attributes !== ['_any']) {
$allowedAttributes = array_fill_keys(
array_merge(
$this->required_attributes,
$this->optional_attributes,
$this->option_flags
),
true
);
foreach ($_indexed_attr as $key => $dummy) {
if (!isset($allowedAttributes[$key]) && $key !== 0) {
$compiler->trigger_template_error("unexpected '{$key}' attribute", null, true);
}
}
}
// default 'false' for all options flags not set
foreach ($this->option_flags as $flag) {
if (!isset($_indexed_attr[$flag])) {
$_indexed_attr[$flag] = false;
}
}
return $_indexed_attr;
}
/**
* Push opening tag name on stack
* Optionally additional data can be saved on stack
*
* @param Template $compiler compiler object
* @param string $openTag the opening tag's name
* @param mixed $data optional data saved
*/
protected function openTag(Template $compiler, $openTag, $data = null) {
$compiler->openTag($openTag, $data);
}
/**
* Pop closing tag
* Raise an error if this stack-top doesn't match with expected opening tags
*
* @param Template $compiler compiler object
* @param array|string $expectedTag the expected opening tag names
*
* @return mixed any type the opening tag's name or saved data
*/
protected function closeTag(Template $compiler, $expectedTag) {
return $compiler->closeTag($expectedTag);
}
/**
* @param mixed $scope
* @param array $invalidScopes
*
* @return int
* @throws Exception
*/
protected function convertScope($scope): int {
static $scopes = [
'local' => Data::SCOPE_LOCAL, // current scope
'parent' => Data::SCOPE_PARENT, // parent scope (definition unclear)
'tpl_root' => Data::SCOPE_TPL_ROOT, // highest template (keep going up until parent is not a template)
'root' => Data::SCOPE_ROOT, // highest scope (definition unclear)
'global' => Data::SCOPE_GLOBAL, // smarty object
'smarty' => Data::SCOPE_SMARTY, // @deprecated alias of 'global'
];
$_scopeName = trim($scope, '\'"');
if (is_numeric($_scopeName) && in_array($_scopeName, $scopes)) {
return (int) $_scopeName;
}
if (isset($scopes[$_scopeName])) {
return $scopes[$_scopeName];
}
$err = var_export($_scopeName, true);
throw new Exception("illegal value '{$err}' for \"scope\" attribute");
}
/**
* Compiles code for the tag
*
* @param array $args array with attributes from parser
* @param Template $compiler compiler object
* @param array $parameter array with compilation parameter
*
* @return string compiled code as a string
* @throws \Smarty\CompilerException
*/
abstract public function compile($args, Template $compiler, $parameter = array(), $tag = null, $function = null): string;
}

View file

@ -0,0 +1,228 @@
<?php
/**
* Smarty Internal Plugin Compile Block Plugin
* Compiles code for the execution of block plugin
*
* @author Uwe Tews
*/
namespace Smarty\Compile;
use Smarty\Compiler\Template;
use Smarty\CompilerException;
use Smarty\Exception;
use Smarty\Smarty;
/**
* Smarty Internal Plugin Compile Block Plugin Class
*
*/
class BlockCompiler extends Base {
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BasePlugin
*/
protected $optional_attributes = ['_any'];
/**
* nesting level
*
* @var int
*/
private $nesting = 0;
/**
* Compiles code for the execution of block plugin
*
* @param array $args array with attributes from parser
* @param Template $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 CompilerException
* @throws Exception
*/
public function compile($args, Template $compiler, $parameter = [], $tag = null, $function = null): string
{
if (!isset($tag[5]) || substr($tag, -5) !== 'close') {
$output = $this->compileOpeningTag($compiler, $args, $tag, $function);
} else {
$output = $this->compileClosingTag($compiler, $tag, $parameter, $function);
}
return $output;
}
/**
* Compiles code for the {$smarty.block.child} property
*
* @param Template $compiler compiler object
*
* @return string compiled code
* @throws CompilerException
*/
public function compileChild(\Smarty\Compiler\Template $compiler) {
if (!isset($compiler->_cache['blockNesting'])) {
$compiler->trigger_template_error(
"'{\$smarty.block.child}' used outside {block} tags ",
$compiler->getParser()->lex->taglineno
);
}
$compiler->_cache['blockParams'][$compiler->_cache['blockNesting']]['callsChild'] = true;
$compiler->suppressNocacheProcessing = true;
$output = "<?php \n";
$output .= '$_smarty_tpl->getInheritance()->callChild($_smarty_tpl, $this' . ");\n";
$output .= "?>\n";
return $output;
}
/**
* Compiles code for the {$smarty.block.parent} property
*
* @param Template $compiler compiler object
*
* @return string compiled code
* @throws CompilerException
*/
public function compileParent(\Smarty\Compiler\Template $compiler) {
if (!isset($compiler->_cache['blockNesting'])) {
$compiler->trigger_template_error(
"'{\$smarty.block.parent}' used outside {block} tags ",
$compiler->getParser()->lex->taglineno
);
}
$compiler->suppressNocacheProcessing = true;
$output = "<?php \n";
$output .= '$_smarty_tpl->getInheritance()->callParent($_smarty_tpl, $this' . ");\n";
$output .= "?>\n";
return $output;
}
/**
* Returns true if this block is cacheable.
*
* @param Smarty $smarty
* @param $function
*
* @return bool
*/
protected function blockIsCacheable(\Smarty\Smarty $smarty, $function): bool {
return $smarty->getBlockHandler($function)->isCacheable();
}
/**
* Returns the code used for the isset check
*
* @param string $tag tag name
* @param string $function base tag or method name
*
* @return string
*/
protected function getIsCallableCode($tag, $function): string {
return "\$_smarty_tpl->getSmarty()->getBlockHandler(" . var_export($function, true) . ")";
}
/**
* Returns the full code used to call the callback
*
* @param string $tag tag name
* @param string $function base tag or method name
*
* @return string
*/
protected function getFullCallbackCode($tag, $function): string {
return "\$_smarty_tpl->getSmarty()->getBlockHandler(" . var_export($function, true) . ")->handle";
}
/**
* @param Template $compiler
* @param array $args
* @param string|null $tag
* @param string|null $function
*
* @return string
*/
private function compileOpeningTag(Template $compiler, array $args, ?string $tag, ?string $function): string {
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
$this->nesting++;
unset($_attr['nocache']);
$_params = 'array(' . implode(',', $this->formatParamsArray($_attr)) . ')';
if (!$this->blockIsCacheable($compiler->getSmarty(), $function)) {
$compiler->tag_nocache = true;
}
if ($compiler->tag_nocache) {
// push a {nocache} tag onto the stack to prevent caching of this block
$this->openTag($compiler, 'nocache');
}
$this->openTag($compiler, $tag, [$_params, $compiler->tag_nocache]);
// compile code
$output = "<?php \$_block_repeat=true;
if (!" . $this->getIsCallableCode($tag, $function) .") {\nthrow new \\Smarty\\Exception('block tag \'{$tag}\' not callable or registered');\n}\n
echo " . $this->getFullCallbackCode($tag, $function) . "({$_params}, null, \$_smarty_tpl, \$_block_repeat);
while (\$_block_repeat) {
ob_start();
?>";
return $output;
}
/**
* @param Template $compiler
* @param string $tag
* @param array $parameter
* @param string|null $function
*
* @return string
* @throws CompilerException
* @throws Exception
*/
private function compileClosingTag(Template $compiler, string $tag, array $parameter, ?string $function): string {
// closing tag of block plugin, restore nocache
$base_tag = substr($tag, 0, -5);
[$_params, $nocache_pushed] = $this->closeTag($compiler, $base_tag);
// 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->compileModifier($parameter['modifier_list'], 'ob_get_clean()')
. ";\n";
}
$output = "<?php {$mod_content}\$_block_repeat=false;\n{$mod_pre}";
$callback = $this->getFullCallbackCode($base_tag, $function);
$output .= "echo {$callback}({$_params}, {$mod_content2}, \$_smarty_tpl, \$_block_repeat);\n";
$output .= "{$mod_post}}\n?>";
if ($nocache_pushed) {
// pop the pushed virtual nocache tag
$this->closeTag($compiler, 'nocache');
$compiler->tag_nocache = true;
}
return $output;
}
}

View file

@ -0,0 +1,26 @@
<?php
namespace Smarty\Compile;
/**
* This class does extend all internal compile plugins
*
*/
interface CompilerInterface {
/**
* Compiles code for the tag
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
* @param array $parameter array with compilation parameter
*
* @return string compiled code as a string
* @throws \Smarty\CompilerException
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string;
public function isCacheable(): bool;
}

View file

@ -0,0 +1,29 @@
<?php
namespace Smarty\Compile;
class DefaultHandlerBlockCompiler extends BlockCompiler {
/**
* @inheritDoc
*/
protected function getIsCallableCode($tag, $function): string {
return "\$_smarty_tpl->getSmarty()->getRuntime('DefaultPluginHandler')->hasPlugin(" .
var_export($function, true) . ", 'block')";
}
/**
* @inheritDoc
*/
protected function getFullCallbackCode($tag, $function): string {
return "\$_smarty_tpl->getSmarty()->getRuntime('DefaultPluginHandler')->getCallback(" .
var_export($function, true) . ", 'block')";
}
/**
* @inheritDoc
*/
protected function blockIsCacheable(\Smarty\Smarty $smarty, $function): bool {
return true;
}
}

View file

@ -0,0 +1,47 @@
<?php
namespace Smarty\Compile;
use Smarty\Compiler\Template;
class DefaultHandlerFunctionCallCompiler extends Base {
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BasePlugin
*/
public $optional_attributes = ['_any'];
/**
* Compiles code for the execution of a registered function
*
* @param array $args array with attributes from parser
* @param Template $compiler compiler object
* @param array $parameter array with compilation parameter
* @param string $tag name of tag
* @param string $function name of function
*
* @return string compiled code
* @throws \Smarty\CompilerException
* @throws \Smarty\Exception
*/
public function compile($args, Template $compiler, $parameter = [], $tag = null, $function = null): string
{
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
unset($_attr['nocache']);
$_paramsArray = $this->formatParamsArray($_attr);
$_params = 'array(' . implode(',', $_paramsArray) . ')';
$output = "\$_smarty_tpl->getSmarty()->getRuntime('DefaultPluginHandler')->getCallback(" . var_export($function, true) .
",'function')($_params, \$_smarty_tpl)";
if (!empty($parameter['modifierlist'])) {
$output = $compiler->compileModifier($parameter['modifierlist'], $output);
}
return "<?php echo {$output};?>\n";
}
}

View file

@ -0,0 +1,79 @@
<?php
/**
* Smarty Internal Plugin Compile Registered Function
* Compiles code for the execution of a registered function
*
* @author Uwe Tews
*/
namespace Smarty\Compile;
use Smarty\Compiler\Template;
use Smarty\CompilerException;
/**
* Smarty Internal Plugin Compile Registered Function Class
*
*/
class FunctionCallCompiler extends Base {
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BasePlugin
*/
public $optional_attributes = ['_any'];
/**
* Shorttag attribute order defined by its names
*
* @var array
*/
protected $shorttag_order = [];
/**
* Compiles code for the execution of a registered function
*
* @param array $args array with attributes from parser
* @param Template $compiler compiler object
* @param array $parameter array with compilation parameter
* @param string $tag name of tag
* @param string $function name of function
*
* @return string compiled code
* @throws \Smarty\CompilerException
* @throws \Smarty\Exception
*/
public function compile($args, Template $compiler, $parameter = [], $tag = null, $function = null): string
{
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
unset($_attr['nocache']);
$_paramsArray = $this->formatParamsArray($_attr);
$_params = 'array(' . implode(',', $_paramsArray) . ')';
if ($functionHandler = $compiler->getSmarty()->getFunctionHandler($function)) {
// not cacheable?
$compiler->tag_nocache = $compiler->tag_nocache || !$functionHandler->isCacheable();
$output = "\$_smarty_tpl->getSmarty()->getFunctionHandler(" . var_export($function, true) . ")";
$output .= "->handle($_params, \$_smarty_tpl)";
} else {
$compiler->trigger_template_error("unknown function '{$function}'", null, true);
}
if (!empty($parameter['modifierlist'])) {
$output = $compiler->compileModifier($parameter['modifierlist'], $output);
}
return $output;
}
}

View file

@ -0,0 +1,19 @@
<?php
namespace Smarty\Compile\Modifier;
class BCPluginWrapper extends Base {
private $callback;
public function __construct($callback) {
$this->callback = $callback;
}
/**
* @inheritDoc
*/
public function compile($params, \Smarty\Compiler\Template $compiler) {
return call_user_func($this->callback, $params, $compiler);
}
}

View file

@ -0,0 +1,49 @@
<?php
namespace Smarty\Compile\Modifier;
use Smarty\Exception;
abstract class Base implements ModifierCompilerInterface {
/**
* Compiles code for the modifier
*
* @param array $params array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
*
* @return string compiled code
* @throws \Smarty\CompilerException
*/
abstract public function compile($params, \Smarty\Compiler\Template $compiler);
/**
* evaluate compiler parameter
*
* @param array $params parameter array as given to the compiler function
* @param integer $index array index of the parameter to convert
* @param mixed $default value to be returned if the parameter is not present
*
* @return mixed evaluated value of parameter or $default
* @throws Exception if parameter is not a literal (but an expression, variable, )
* @author Rodney Rehm
*/
protected function literal_compiler_param($params, $index, $default = null)
{
// not set, go default
if (!isset($params[ $index ])) {
return $default;
}
// test if param is a literal
if (!preg_match('/^([\'"]?)[a-zA-Z0-9-]+(\\1)$/', $params[ $index ])) {
throw new Exception(
'$param[' . $index .
'] is not a literal and is thus not evaluatable at compile time'
);
}
$t = null;
eval("\$t = " . $params[ $index ] . ";");
return $t;
}
}

View file

@ -0,0 +1,25 @@
<?php
namespace Smarty\Compile\Modifier;
/**
* Smarty cat modifier plugin
* Type: modifier
* Name: cat
* Date: Feb 24, 2003
* Purpose: catenate a value to a variable
* Input: string to catenate
* Example: {$var|cat:"foo"}
*
* @author Uwe Tews
*/
class CatModifierCompiler extends Base {
public function compile($params, \Smarty\Compiler\Template $compiler) {
return '(' . implode(').(', $params) . ')';
}
}

View file

@ -0,0 +1,21 @@
<?php
namespace Smarty\Compile\Modifier;
/**
* Smarty count_characters modifier plugin
* Type: modifier
* Name: count_characters
* Purpose: count the number of characters in a text
*
* @author Uwe Tews
*/
class CountCharactersModifierCompiler extends Base {
public function compile($params, \Smarty\Compiler\Template $compiler) {
if (!isset($params[ 1 ]) || $params[ 1 ] !== 'true') {
return 'preg_match_all(\'/[^\s]/' . \Smarty\Smarty::$_UTF8_MODIFIER . '\',' . $params[ 0 ] . ', $tmp)';
}
return 'mb_strlen((string) ' . $params[ 0 ] . ', \'' . addslashes(\Smarty\Smarty::$_CHARSET) . '\')';
}
}

View file

@ -0,0 +1,19 @@
<?php
namespace Smarty\Compile\Modifier;
/**
* Smarty count_paragraphs modifier plugin
* Type: modifier
* Name: count_paragraphs
* Purpose: count the number of paragraphs in a text
*
* @author Uwe Tews
*/
class CountParagraphsModifierCompiler extends Base {
public function compile($params, \Smarty\Compiler\Template $compiler) {
// count \r or \n characters
return '(preg_match_all(\'#[\r\n]+#\', ' . $params[ 0 ] . ', $tmp)+1)';
}
}

View file

@ -0,0 +1,19 @@
<?php
namespace Smarty\Compile\Modifier;
/**
* Smarty count_sentences modifier plugin
* Type: modifier
* Name: count_sentences
* Purpose: count the number of sentences in a text
*
* @author Uwe Tews
*/
class CountSentencesModifierCompiler extends Base {
public function compile($params, \Smarty\Compiler\Template $compiler) {
// find periods, question marks, exclamation marks with a word before but not after.
return 'preg_match_all("#\w[\.\?\!](\W|$)#S' . \Smarty\Smarty::$_UTF8_MODIFIER . '", ' . $params[ 0 ] . ', $tmp)';
}
}

View file

@ -0,0 +1,20 @@
<?php
namespace Smarty\Compile\Modifier;
/**
* Smarty count_words modifier plugin
* Type: modifier
* Name: count_words
* Purpose: count the number of words in a text
*
* @author Uwe Tews
*/
class CountWordsModifierCompiler extends Base {
public function compile($params, \Smarty\Compiler\Template $compiler) {
// expression taken from http://de.php.net/manual/en/function.str-word-count.php#85592
return 'preg_match_all(\'/\p{L}[\p{L}\p{Mn}\p{Pd}\\\'\x{2019}]*/' . \Smarty\Smarty::$_UTF8_MODIFIER . '\', ' .
$params[ 0 ] . ', $tmp)';
}
}

View file

@ -0,0 +1,26 @@
<?php
namespace Smarty\Compile\Modifier;
/**
* Smarty default modifier plugin
* Type: modifier
* Name: default
* Purpose: designate default value for empty variables
*
* @author Uwe Tews
*/
class DefaultModifierCompiler extends Base {
public function compile($params, \Smarty\Compiler\Template $compiler) {
$output = $params[ 0 ];
if (!isset($params[ 1 ])) {
$params[ 1 ] = "''";
}
array_shift($params);
foreach ($params as $param) {
$output = '(($tmp = ' . $output . ' ?? null)===null||$tmp===\'\' ? ' . $param . ' ?? null : $tmp)';
}
return $output;
}
}

View file

@ -0,0 +1,19 @@
<?php
namespace Smarty\Compile\Modifier;
use Smarty\CompilerException;
/**
* Smarty empty modifier plugin
*/
class EmptyModifierCompiler extends Base {
public function compile($params, \Smarty\Compiler\Template $compiler) {
if (count($params) !== 1) {
throw new CompilerException("Invalid number of arguments for empty. empty expects exactly 1 parameter.");
}
return 'empty(' . $params[0] . ')';
}
}

View file

@ -0,0 +1,66 @@
<?php
namespace Smarty\Compile\Modifier;
use Smarty\Exception;
/**
* Smarty escape modifier plugin
* Type: modifier
* Name: escape
* Purpose: escape string for output
*
* @author Rodney Rehm
*/
class EscapeModifierCompiler extends Base {
public function compile($params, \Smarty\Compiler\Template $compiler) {
try {
$esc_type = $this->literal_compiler_param($params, 1, 'html');
$char_set = $this->literal_compiler_param($params, 2, \Smarty\Smarty::$_CHARSET);
$double_encode = $this->literal_compiler_param($params, 3, true);
if (!$char_set) {
$char_set = \Smarty\Smarty::$_CHARSET;
}
switch ($esc_type) {
case 'html':
case 'force':
// in case of auto-escaping, and without the 'force' option, no double-escaping
if ($compiler->getSmarty()->escape_html && $esc_type != 'force')
return $params[0];
// otherwise, escape the variable
return 'htmlspecialchars((string)' . $params[ 0 ] . ', ENT_QUOTES, ' . var_export($char_set, true) . ', ' .
var_export($double_encode, true) . ')';
// no break
case 'htmlall':
$compiler->setRawOutput(true);
return 'htmlentities(mb_convert_encoding((string)' . $params[ 0 ] . ', \'UTF-8\', ' .
var_export($char_set, true) . '), ENT_QUOTES, \'UTF-8\', ' .
var_export($double_encode, true) . ')';
// no break
case 'url':
$compiler->setRawOutput(true);
return 'rawurlencode((string)' . $params[ 0 ] . ')';
case 'urlpathinfo':
$compiler->setRawOutput(true);
return 'str_replace("%2F", "/", rawurlencode((string)' . $params[ 0 ] . '))';
case 'quotes':
$compiler->setRawOutput(true);
// escape unescaped single quotes
return 'preg_replace("%(?<!\\\\\\\\)\'%", "\\\'", (string)' . $params[ 0 ] . ')';
case 'javascript':
$compiler->setRawOutput(true);
// escape quotes and backslashes, newlines, etc.
// see https://html.spec.whatwg.org/multipage/scripting.html#restrictions-for-contents-of-script-elements
return 'strtr((string)' .
$params[ 0 ] .
', array("\\\\" => "\\\\\\\\", "\'" => "\\\\\'", "\"" => "\\\\\"", "\\r" => "\\\\r",
"\\n" => "\\\n", "</" => "<\/", "<!--" => "<\!--", "<s" => "<\s", "<S" => "<\S",
"`" => "\\\\`", "\${" => "\\\\\\$\\{"))';
}
} catch (Exception $e) {
// pass through to regular plugin fallback
}
return '$_smarty_tpl->getSmarty()->getModifierCallback(\'escape\')(' . join(', ', $params) . ')';
}
}

View file

@ -0,0 +1,21 @@
<?php
namespace Smarty\Compile\Modifier;
/**
* Smarty from_charset modifier plugin
* Type: modifier
* Name: from_charset
* Purpose: convert character encoding from $charset to internal encoding
*
* @author Rodney Rehm
*/
class FromCharsetModifierCompiler extends Base {
public function compile($params, \Smarty\Compiler\Template $compiler) {
if (!isset($params[ 1 ])) {
$params[ 1 ] = '"ISO-8859-1"';
}
return 'mb_convert_encoding(' . $params[ 0 ] . ', "' . addslashes(\Smarty\Smarty::$_CHARSET) . '", ' . $params[ 1 ] . ')';
}
}

View file

@ -0,0 +1,24 @@
<?php
namespace Smarty\Compile\Modifier;
/**
* Smarty indent modifier plugin
* Type: modifier
* Name: indent
* Purpose: indent lines of text
*
* @author Uwe Tews
*/
class IndentModifierCompiler extends Base {
public function compile($params, \Smarty\Compiler\Template $compiler) {
if (!isset($params[ 1 ])) {
$params[ 1 ] = 4;
}
if (!isset($params[ 2 ])) {
$params[ 2 ] = "' '";
}
return 'preg_replace(\'!^!m\',str_repeat(' . $params[ 2 ] . ',' . $params[ 1 ] . '),' . $params[ 0 ] . ')';
}
}

View file

@ -0,0 +1,19 @@
<?php
namespace Smarty\Compile\Modifier;
use Smarty\CompilerException;
/**
* Smarty is_array modifier plugin
*/
class IsArrayModifierCompiler extends Base {
public function compile($params, \Smarty\Compiler\Template $compiler) {
if (count($params) !== 1) {
throw new CompilerException("Invalid number of arguments for is_array. is_array expects exactly 1 parameter.");
}
return 'is_array(' . $params[0] . ')';
}
}

View file

@ -0,0 +1,25 @@
<?php
namespace Smarty\Compile\Modifier;
use Smarty\CompilerException;
/**
* Smarty isset modifier plugin
*/
class IssetModifierCompiler extends Base {
public function compile($params, \Smarty\Compiler\Template $compiler) {
$params = array_filter($params, function($v) { return !empty($v); });
if (count($params) < 1) {
throw new CompilerException("Invalid number of arguments for isset. isset expects at least one parameter.");
}
$tests = [];
foreach ($params as $param) {
$tests[] = 'null !== (' . $param . ' ?? null)';
}
return '(' . implode(' && ', $tests) . ')';
}
}

View file

@ -0,0 +1,14 @@
<?php
namespace Smarty\Compile\Modifier;
/**
* Smarty json_encode modifier plugin
*/
class JsonEncodeModifierCompiler extends Base {
public function compile($params, \Smarty\Compiler\Template $compiler) {
return 'json_encode(' . $params[0] . (isset($params[1]) ? ', (int) ' . $params[1] : '') . ')';
}
}

View file

@ -0,0 +1,19 @@
<?php
namespace Smarty\Compile\Modifier;
/**
* Smarty lower modifier plugin
* Type: modifier
* Name: lower
* Purpose: convert string to lowercase
*
* @author Monte Ohrt <monte at ohrt dot com>
* @author Uwe Tews
*/
class LowerModifierCompiler extends Base {
public function compile($params, \Smarty\Compiler\Template $compiler) {
return 'mb_strtolower((string) ' . $params[ 0 ] . ', \'' . addslashes(\Smarty\Smarty::$_CHARSET) . '\')';
}
}

View file

@ -0,0 +1,17 @@
<?php
namespace Smarty\Compile\Modifier;
interface ModifierCompilerInterface {
/**
* Compiles code for the modifier
*
* @param array $params array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
*
* @return string compiled code
* @throws \Smarty\CompilerException
*/
public function compile($params, \Smarty\Compiler\Template $compiler);
}

View file

@ -0,0 +1,17 @@
<?php
namespace Smarty\Compile\Modifier;
/**
* Smarty nl2br modifier plugin
* Type: modifier
* Name: nl2br
* Purpose: insert HTML line breaks before all newlines in a string
*
*/
class Nl2brModifierCompiler extends Base {
public function compile($params, \Smarty\Compiler\Template $compiler) {
return 'nl2br((string) ' . $params[0] . ', (bool) ' . ($params[1] ?? true) . ')';
}
}

View file

@ -0,0 +1,18 @@
<?php
namespace Smarty\Compile\Modifier;
/**
* Smarty noprint modifier plugin
* Type: modifier
* Name: noprint
* Purpose: return an empty string
*
* @author Uwe Tews
*/
class NoPrintModifierCompiler extends Base {
public function compile($params, \Smarty\Compiler\Template $compiler) {
return "''";
}
}

View file

@ -0,0 +1,21 @@
<?php
namespace Smarty\Compile\Modifier;
use Smarty\Exception;
/**
* Smarty raw modifier plugin
* Type: modifier
* Name: raw
* Purpose: when escaping is enabled by default, generates a raw output of a variable
*
* @author Amaury Bouchard
*/
class RawModifierCompiler extends Base {
public function compile($params, \Smarty\Compiler\Template $compiler) {
$compiler->setRawOutput(true);
return ($params[0]);
}
}

View file

@ -0,0 +1,18 @@
<?php
namespace Smarty\Compile\Modifier;
/**
* Smarty round modifier plugin
* Type: modifier
* Name: round
* Purpose: Returns the rounded value of num to specified precision (number of digits after the decimal point)
*
*/
class RoundModifierCompiler extends Base {
public function compile($params, \Smarty\Compiler\Template $compiler) {
return 'round((float) ' . $params[0] . ', (int) ' . ($params[1] ?? 0) . ', (int) ' . ($params[2] ?? PHP_ROUND_HALF_UP) . ')';
}
}

View file

@ -0,0 +1,17 @@
<?php
namespace Smarty\Compile\Modifier;
/**
* Smarty str_repeat modifier plugin
* Type: modifier
* Name: str_repeat
* Purpose: returns string repeated times times
*
*/
class StrRepeatModifierCompiler extends Base {
public function compile($params, \Smarty\Compiler\Template $compiler) {
return 'str_repeat((string) ' . $params[0] . ', (int) ' . $params[1] . ')';
}
}

View file

@ -0,0 +1,18 @@
<?php
namespace Smarty\Compile\Modifier;
/**
* Smarty string_format modifier plugin
* Type: modifier
* Name: string_format
* Purpose: format strings via sprintf
*
* @author Uwe Tews
*/
class StringFormatModifierCompiler extends Base {
public function compile($params, \Smarty\Compiler\Template $compiler) {
return 'sprintf(' . $params[ 1 ] . ',' . $params[ 0 ] . ')';
}
}

View file

@ -0,0 +1,24 @@
<?php
namespace Smarty\Compile\Modifier;
/**
* Smarty strip modifier plugin
* Type: modifier
* Name: strip
* Purpose: Replace all repeated spaces, newlines, tabs
* with a single space or supplied replacement string.
* Example: {$var|strip} {$var|strip:"&nbsp;"}
* Date: September 25th, 2002
*
* @author Uwe Tews
*/
class StripModifierCompiler extends Base {
public function compile($params, \Smarty\Compiler\Template $compiler) {
if (!isset($params[ 1 ])) {
$params[ 1 ] = "' '";
}
return "preg_replace('!\s+!" . \Smarty\Smarty::$_UTF8_MODIFIER . "', {$params[1]},{$params[0]})";
}
}

View file

@ -0,0 +1,22 @@
<?php
namespace Smarty\Compile\Modifier;
/**
* Smarty strip_tags modifier plugin
* Type: modifier
* Name: strip_tags
* Purpose: strip html tags from text
*
* @author Uwe Tews
*/
class StripTagsModifierCompiler extends Base {
public function compile($params, \Smarty\Compiler\Template $compiler) {
if (!isset($params[ 1 ]) || $params[ 1 ] === true || trim($params[ 1 ], '"') === 'true') {
return "preg_replace('!<[^>]*?>!', ' ', (string) {$params[0]})";
} else {
return 'strip_tags((string) ' . $params[ 0 ] . ')';
}
}
}

View file

@ -0,0 +1,18 @@
<?php
namespace Smarty\Compile\Modifier;
/**
* Smarty strlen modifier plugin
* Type: modifier
* Name: strlen
* Purpose: return the length of the given string
*
*/
class StrlenModifierCompiler extends Base {
public function compile($params, \Smarty\Compiler\Template $compiler) {
return 'strlen((string) ' . $params[0] . ')';
}
}

View file

@ -0,0 +1,15 @@
<?php
namespace Smarty\Compile\Modifier;
/**
* Smarty substr modifier plugin
*/
class SubstrModifierCompiler extends Base {
public function compile($params, \Smarty\Compiler\Template $compiler) {
return 'substr((string) ' . $params[0] . ', (int) ' . $params[1] .
(isset($params[2]) ? ', (int) ' . $params[2] : '') . ')';
}
}

View file

@ -0,0 +1,21 @@
<?php
namespace Smarty\Compile\Modifier;
/**
* Smarty to_charset modifier plugin
* Type: modifier
* Name: to_charset
* Purpose: convert character encoding from internal encoding to $charset
*
* @author Rodney Rehm
*/
class ToCharsetModifierCompiler extends Base {
public function compile($params, \Smarty\Compiler\Template $compiler) {
if (!isset($params[ 1 ])) {
$params[ 1 ] = '"ISO-8859-1"';
}
return 'mb_convert_encoding(' . $params[ 0 ] . ', ' . $params[ 1 ] . ', "' . addslashes(\Smarty\Smarty::$_CHARSET) . '")';
}
}

View file

@ -0,0 +1,34 @@
<?php
namespace Smarty\Compile\Modifier;
/**
* Smarty unescape modifier plugin
* Type: modifier
* Name: unescape
* Purpose: unescape html entities
*
* @author Rodney Rehm
*/
class UnescapeModifierCompiler extends Base {
public function compile($params, \Smarty\Compiler\Template $compiler) {
$esc_type = $this->literal_compiler_param($params, 1, 'html');
if (!isset($params[ 2 ])) {
$params[ 2 ] = '\'' . addslashes(\Smarty\Smarty::$_CHARSET) . '\'';
}
switch ($esc_type) {
case 'entity':
case 'htmlall':
return 'html_entity_decode(mb_convert_encoding(' . $params[ 0 ] . ', ' . $params[ 2 ] . ', \'UTF-8\'), ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, ' . $params[ 2 ] . ')';
case 'html':
return 'htmlspecialchars_decode(' . $params[ 0 ] . ', ENT_QUOTES)';
case 'url':
return 'rawurldecode(' . $params[ 0 ] . ')';
default:
return $params[ 0 ];
}
}
}

View file

@ -0,0 +1,18 @@
<?php
namespace Smarty\Compile\Modifier;
/**
* Smarty upper modifier plugin
* Type: modifier
* Name: lower
* Purpose: convert string to uppercase
*
* @author Uwe Tews
*/
class UpperModifierCompiler extends Base {
public function compile($params, \Smarty\Compiler\Template $compiler) {
return 'mb_strtoupper((string) ' . $params[ 0 ] . ' ?? \'\', \'' . addslashes(\Smarty\Smarty::$_CHARSET) . '\')';
}
}

View file

@ -0,0 +1,27 @@
<?php
namespace Smarty\Compile\Modifier;
/**
* Smarty wordwrap modifier plugin
* Type: modifier
* Name: wordwrap
* Purpose: wrap a string of text at a given length
*
* @author Uwe Tews
*/
class WordWrapModifierCompiler extends Base {
public function compile($params, \Smarty\Compiler\Template $compiler) {
if (!isset($params[ 1 ])) {
$params[ 1 ] = 80;
}
if (!isset($params[ 2 ])) {
$params[ 2 ] = '"\n"';
}
if (!isset($params[ 3 ])) {
$params[ 3 ] = 'false';
}
return 'smarty_mb_wordwrap(' . $params[ 0 ] . ',' . $params[ 1 ] . ',' . $params[ 2 ] . ',' . $params[ 3 ] . ')';
}
}

View file

@ -0,0 +1,95 @@
<?php
/**
* Smarty Internal Plugin Compile Modifier
* Compiles code for modifier execution
*
* @author Uwe Tews
*/
namespace Smarty\Compile;
use Smarty\Compile\Base;
use Smarty\Compiler\Template;
use Smarty\CompilerException;
/**
* Smarty Internal Plugin Compile Modifier Class
*
*/
class ModifierCompiler extends Base {
/**
* Compiles code for modifier execution
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
* @param array $parameter array with compilation parameter
*
* @return string compiled code
* @throws \Smarty\CompilerException
* @throws \Smarty\Exception
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
$output = $parameter['value'];
// loop over list of modifiers
foreach ($parameter['modifierlist'] as $single_modifier) {
/* @var string $modifier */
$modifier = $single_modifier[0];
$modifier_params = array_values($single_modifier);
$modifier_params[0] = $output;
$params = implode(',', $modifier_params);
if (!is_object($compiler->getSmarty()->security_policy)
|| $compiler->getSmarty()->security_policy->isTrustedModifier($modifier, $compiler)
) {
if ($handler = $compiler->getModifierCompiler($modifier)) {
$output = $handler->compile($modifier_params, $compiler);
} elseif ($compiler->getSmarty()->getModifierCallback($modifier)) {
$output = sprintf(
'$_smarty_tpl->getSmarty()->getModifierCallback(%s)(%s)',
var_export($modifier, true),
$params
);
} elseif ($callback = $compiler->getPluginFromDefaultHandler($modifier, \Smarty\Smarty::PLUGIN_MODIFIERCOMPILER)) {
$output = (new \Smarty\Compile\Modifier\BCPluginWrapper($callback))->compile($modifier_params, $compiler);
} elseif ($function = $compiler->getPluginFromDefaultHandler($modifier, \Smarty\Smarty::PLUGIN_MODIFIER)) {
if (!is_array($function)) {
$output = "{$function}({$params})";
} else {
$operator = is_object($function[0]) ? '->' : '::';
$output = $function[0] . $operator . $function[1] . '(' . $params . ')';
}
} else {
$compiler->trigger_template_error("unknown modifier '{$modifier}'", null, true);
}
}
}
return (string)$output;
}
/**
* Wether this class will be able to compile the given modifier.
* @param string $modifier
* @param Template $compiler
*
* @return bool
* @throws CompilerException
*/
public function canCompileForModifier(string $modifier, \Smarty\Compiler\Template $compiler): bool {
return $compiler->getModifierCompiler($modifier)
|| $compiler->getSmarty()->getModifierCallback($modifier)
|| $compiler->getPluginFromDefaultHandler($modifier, \Smarty\Smarty::PLUGIN_MODIFIERCOMPILER)
|| $compiler->getPluginFromDefaultHandler($modifier, \Smarty\Smarty::PLUGIN_MODIFIER);
}
}

View file

@ -0,0 +1,44 @@
<?php
/**
* Smarty Internal Plugin Compile Object Block Function
* Compiles code for registered objects as block function
*
* @author Uwe Tews
*/
namespace Smarty\Compile;
/**
* Smarty Internal Plugin Compile Object Block Function Class
*
*/
class ObjectMethodBlockCompiler extends BlockCompiler {
/**
* @inheritDoc
*/
protected function getIsCallableCode($tag, $function): string {
$callbackObject = "\$_smarty_tpl->getSmarty()->registered_objects['{$tag}'][0]";
return "(isset({$callbackObject}) && is_callable(array({$callbackObject}, '{$function}')))";
}
/**
* @inheritDoc
*/
protected function getFullCallbackCode($tag, $function): string {
$callbackObject = "\$_smarty_tpl->getSmarty()->registered_objects['{$tag}'][0]";
return "{$callbackObject}->{$function}";
}
/**
* @inheritDoc
*/
protected function blockIsCacheable(\Smarty\Smarty $smarty, $function): bool {
return true;
}
}

View file

@ -0,0 +1,76 @@
<?php
/**
* Smarty Internal Plugin Compile Object Function
* Compiles code for registered objects as function
*
* @author Uwe Tews
*/
namespace Smarty\Compile;
/**
* Smarty Internal Plugin Compile Object Function Class
*
*/
class ObjectMethodCallCompiler extends Base {
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BasePlugin
*/
protected $optional_attributes = ['_any'];
/**
* Compiles code for the execution of function plugin
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $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 \Smarty\CompilerException
* @throws \Smarty\Exception
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
// 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->getSmarty()->registered_objects[$tag][0], $function])) {
// convert attributes into parameter array string
if ($compiler->getSmarty()->registered_objects[$tag][2]) {
$_paramsArray = $this->formatParamsArray($_attr);
$_params = 'array(' . implode(',', $_paramsArray) . ')';
$output = "\$_smarty_tpl->getSmarty()->registered_objects['{$tag}'][0]->{$function}({$_params},\$_smarty_tpl)";
} else {
$_params = implode(',', $_attr);
$output = "\$_smarty_tpl->getSmarty()->registered_objects['{$tag}'][0]->{$function}({$_params})";
}
} else {
// object property
$output = "\$_smarty_tpl->getSmarty()->registered_objects['{$tag}'][0]->{$function}";
}
if (!empty($parameter['modifierlist'])) {
$output = $compiler->compileModifier($parameter['modifierlist'], $output);
}
if (empty($_assign)) {
return "<?php echo {$output};?>\n";
} else {
return "<?php \$_smarty_tpl->assign({$_assign},{$output});?>\n";
}
}
}

View file

@ -0,0 +1,96 @@
<?php
/**
* Smarty Internal Plugin Compile Print Expression
* Compiles any tag which will output an expression or variable
*
* @author Uwe Tews
*/
namespace Smarty\Compile;
use Smarty\Compile\Base;
use Smarty\Compiler\BaseCompiler;
/**
* Smarty Internal Plugin Compile Print Expression Class
*
*/
class PrintExpressionCompiler extends Base {
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BaseCompiler
*/
public $optional_attributes = ['assign'];
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BaseCompiler
*/
protected $option_flags = ['nocache', 'nofilter'];
/**
* Compiles code for generating output from any expression
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
* @param array $parameter array with compilation parameter
*
* @return string
* @throws \Smarty\Exception
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
$output = $parameter['value'];
// tag modifier
if (!empty($parameter['modifierlist'])) {
$output = $compiler->compileModifier($parameter['modifierlist'], $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 ($compiler->getSmarty()->getDefaultModifiers()) {
$modifierlist = [];
foreach ($compiler->getSmarty()->getDefaultModifiers() 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];
}
}
}
$output = $compiler->compileModifier($modifierlist, $output);
}
if ($compiler->getTemplate()->getSmarty()->escape_html && !$compiler->isRawOutput()) {
$output = "htmlspecialchars((string) ({$output}), ENT_QUOTES, '" . addslashes(\Smarty\Smarty::$_CHARSET) . "')";
}
}
$output = "<?php echo {$output};?>\n";
$compiler->setRawOutput(false);
}
return $output;
}
}

View file

@ -0,0 +1,134 @@
<?php
/**
* Smarty Internal Plugin Compile Special Smarty Variable
* Compiles the special $smarty variables
*
* @author Uwe Tews
*/
namespace Smarty\Compile;
use Smarty\Compile\Base;
use Smarty\Compile\Tag\Capture;
use Smarty\Compile\Tag\ForeachTag;
use Smarty\Compile\Tag\Section;
use Smarty\Compiler\Template;
use Smarty\CompilerException;
/**
* Smarty Internal Plugin Compile special Smarty Variable Class
*
*/
class SpecialVariableCompiler extends Base {
/**
* Compiles code for the special $smarty variables
*
* @param array $args array with attributes from parser
* @param Template $compiler compiler object
* @param array $parameter
* @param null $tag
* @param null $function
*
* @return string compiled code
* @throws CompilerException
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
$_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->getSmarty()->security_policy)
|| $compiler->getSmarty()->security_policy->isTrustedSpecialSmartyVar($variable, $compiler)
) {
switch ($variable) {
case 'foreach':
return (new ForeachTag())->compileSpecialVariable($compiler, $_index);
case 'section':
return (new Section())->compileSpecialVariable($compiler, $_index);
case 'capture':
return (new Capture())->compileSpecialVariable($compiler, $_index);
case 'now':
return 'time()';
case 'cookies':
if (isset($compiler->getSmarty()->security_policy)
&& !$compiler->getSmarty()->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->getSmarty()->security_policy)
&& !$compiler->getSmarty()->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 '$_smarty_tpl->template_resource';
case 'template_object':
if (isset($compiler->getSmarty()->security_policy)) {
$compiler->trigger_template_error("(secure mode) template_object not permitted");
break;
}
return '$_smarty_tpl';
case 'current_dir':
return '$_smarty_current_dir';
case 'version':
return "\\Smarty\\Smarty::SMARTY_VERSION";
case 'const':
if (isset($compiler->getSmarty()->security_policy)
&& !$compiler->getSmarty()->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->getConfigVariable($_index[1])) ? \$tmp[$_index[2]] : null)";
} else {
return "\$_smarty_tpl->getConfigVariable($_index[1])";
}
// no break
case 'ldelim':
return "\$_smarty_tpl->getLeftDelimiter()";
case 'rdelim':
return "\$_smarty_tpl->getRightDelimiter()";
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;
}
return '';
}
}

View file

@ -0,0 +1,58 @@
<?php
namespace Smarty\Compile\Tag;
/**
* Smarty Internal Plugin Compile Append
* Compiles the {append} tag
*
* @author Uwe Tews
*/
/**
* Smarty Internal Plugin Compile Append Class
*
*/
class Append extends Assign
{
/**
* @inheritdoc
*/
protected $optional_attributes = ['scope', 'index'];
/**
* Compiles code for the {append} tag
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
* @param array $parameter array with compilation parameter
*
* @return string compiled code
* @throws \Smarty\CompilerException
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = array(), $tag = null, $function = null): string
{
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
// map to compile assign attributes
if (isset($_attr[ 'index' ])) {
$_params[ 'smarty_internal_index' ] = '[' . $_attr[ 'index' ] . ']';
unset($_attr[ 'index' ]);
} else {
$_params[ 'smarty_internal_index' ] = '[]';
}
$_new_attr = array();
foreach ($_attr as $key => $value) {
$_new_attr[] = array($key => $value);
}
// call compile assign
return parent::compile($_new_attr, $compiler, $_params);
}
}

View file

@ -0,0 +1,95 @@
<?php
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
use Smarty\Smarty;
/**
* Smarty Internal Plugin Compile Assign
* Compiles the {assign} tag
*
* @author Uwe Tews
*/
/**
* Smarty Internal Plugin Compile Assign Class
*
*/
class Assign extends Base
{
/**
* @inheritdoc
*/
protected $required_attributes = ['var', 'value'];
/**
* @inheritdoc
*/
protected $optional_attributes = ['scope'];
/**
* @inheritdoc
*/
protected $shorttag_order = ['var', 'value'];
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BasePlugin
*/
protected $option_flags = array('nocache', 'noscope');
/**
* Compiles code for the {assign} tag
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
* @param array $parameter array with compilation parameter
*
* @return string compiled code
* @throws \Smarty\CompilerException
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = array(), $tag = null, $function = null): string
{
$_nocache = false;
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
if ($_var = $compiler->getId($_attr[ 'var' ])) {
$_var = "'{$_var}'";
} else {
$_var = $_attr[ 'var' ];
}
if ($compiler->tag_nocache || $compiler->isNocacheActive()) {
$_nocache = true;
// create nocache var to make it know for further compiling
$compiler->setNocacheInVariable($_attr[ 'var' ]);
}
// scope setup
if ($_attr[ 'noscope' ]) {
$_scope = -1;
} else {
$_scope = isset($_attr['scope']) ? $this->convertScope($_attr['scope']) : null;
}
if (isset($parameter[ 'smarty_internal_index' ])) {
$output =
"<?php \$_tmp_array = \$_smarty_tpl->getValue({$_var}) ?? [];\n";
$output .= "if (!(is_array(\$_tmp_array) || \$_tmp_array instanceof ArrayAccess)) {\n";
$output .= "settype(\$_tmp_array, 'array');\n";
$output .= "}\n";
$output .= "\$_tmp_array{$parameter['smarty_internal_index']} = {$_attr['value']};\n";
$output .= "\$_smarty_tpl->assign({$_var}, \$_tmp_array, " . var_export($_nocache, true) . ", " . var_export($_scope, true) . ");?>";
} else {
$output = "<?php \$_smarty_tpl->assign({$_var}, {$_attr['value']}, " . var_export($_nocache, true) . ", " . var_export($_scope, true) . ");?>";
}
return $output;
}
}

View file

@ -0,0 +1,31 @@
<?php
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
class BCPluginWrapper extends Base {
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see Smarty_Internal_CompileBase
*/
public $optional_attributes = array('_any');
private $callback;
public function __construct($callback, bool $cacheable = true) {
$this->callback = $callback;
$this->cacheable = $cacheable;
}
/**
* @inheritDoc
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
return call_user_func($this->callback, $this->getAttributes($compiler, $args), $compiler->getSmarty());
}
}

View file

@ -0,0 +1,92 @@
<?php
/**
* This file is part of Smarty.
*
* (c) 2015 Uwe Tews
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Smarty\Compile\Tag;
use Smarty\ParseTree\Template;
/**
* Smarty Internal Plugin Compile Block Class
*
* @author Uwe Tews <uwe.tews@googlemail.com>
*/
class Block extends Inheritance {
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BasePlugin
*/
public $required_attributes = ['name'];
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BasePlugin
*/
public $shorttag_order = ['name'];
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BasePlugin
*/
protected $option_flags = ['hide', 'nocache'];
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BasePlugin
*/
public $optional_attributes = ['assign'];
/**
* Compiles code for the {block} tag
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
* @param array $parameter array with compilation parameter
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = array(), $tag = null, $function = null): string
{
if (!isset($compiler->_cache['blockNesting'])) {
$compiler->_cache['blockNesting'] = 0;
}
if ($compiler->_cache['blockNesting'] === 0) {
// make sure that inheritance gets initialized in template code
$this->registerInit($compiler);
$this->option_flags = ['hide', 'nocache', 'append', 'prepend'];
} else {
$this->option_flags = ['hide', 'nocache'];
}
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
++$compiler->_cache['blockNesting'];
$_className = 'Block_' . preg_replace('![^\w]+!', '_', uniqid(mt_rand(), true));
$this->openTag(
$compiler,
'block',
[
$_attr, $compiler->tag_nocache, $compiler->getParser()->current_buffer,
$compiler->getTemplate()->getCompiled()->getNocacheCode(), $_className
]
);
$compiler->getParser()->current_buffer = new Template();
$compiler->getTemplate()->getCompiled()->setNocacheCode(false);
$compiler->suppressNocacheProcessing = true;
return '';
}
}

View file

@ -0,0 +1,110 @@
<?php
namespace Smarty\Compile\Tag;
use Smarty\ParseTree\Template;
/**
* Smarty Internal Plugin Compile BlockClose Class
*/
class BlockClose extends Inheritance {
/**
* Compiles code for the {/block} tag
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
* @param array $parameter array with compilation parameter
*
* @return bool true
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = array(), $tag = null, $function = null): string
{
[$_attr, $_nocache, $_buffer, $_has_nocache_code, $_className] = $this->closeTag($compiler, ['block']);
$_block = [];
if (isset($compiler->_cache['blockParams'])) {
$_block = $compiler->_cache['blockParams'][$compiler->_cache['blockNesting']] ?? [];
unset($compiler->_cache['blockParams'][$compiler->_cache['blockNesting']]);
}
$_name = $_attr['name'];
$_assign = $_attr['assign'] ?? null;
unset($_attr[ 'assign' ], $_attr[ 'name' ]);
foreach ($_attr as $name => $stat) {
if ((is_bool($stat) && $stat !== false) || (!is_bool($stat) && $stat !== 'false')) {
$_block[ $name ] = 'true';
}
}
// get compiled block code
$_functionCode = $compiler->getParser()->current_buffer;
// setup buffer for template function code
$compiler->getParser()->current_buffer = new Template();
$output = "<?php\n";
$output .= $compiler->cStyleComment(" {block {$_name}} ") . "\n";
$output .= "class {$_className} extends \\Smarty\\Runtime\\Block\n";
$output .= "{\n";
foreach ($_block as $property => $value) {
$output .= "public \${$property} = " . var_export($value, true) . ";\n";
}
$output .= "public function callBlock(\\Smarty\\Template \$_smarty_tpl) {\n";
$output .= (new \Smarty\Compiler\CodeFrame($compiler->getTemplate()))->insertLocalVariables();
if ($compiler->getTemplate()->getCompiled()->getNocacheCode()) {
$output .= "\$_smarty_tpl->getCached()->hashes['{$compiler->getTemplate()->getCompiled()->nocache_hash}'] = true;\n";
}
if (isset($_assign)) {
$output .= "ob_start();\n";
}
$output .= "?>\n";
$compiler->getParser()->current_buffer->append_subtree(
$compiler->getParser(),
new \Smarty\ParseTree\Tag(
$compiler->getParser(),
$output
)
);
$compiler->getParser()->current_buffer->append_subtree($compiler->getParser(), $_functionCode);
$output = "<?php\n";
if (isset($_assign)) {
$output .= "\$_smarty_tpl->assign({$_assign}, ob_get_clean());\n";
}
$output .= "}\n";
$output .= "}\n";
$output .= $compiler->cStyleComment(" {/block {$_name}} ") . "\n\n";
$output .= "?>\n";
$compiler->getParser()->current_buffer->append_subtree(
$compiler->getParser(),
new \Smarty\ParseTree\Tag(
$compiler->getParser(),
$output
)
);
$compiler->blockOrFunctionCode .= $compiler->getParser()->current_buffer->to_smarty_php($compiler->getParser());
$compiler->getParser()->current_buffer = new Template();
// restore old status
$compiler->getTemplate()->getCompiled()->setNocacheCode($_has_nocache_code);
$compiler->tag_nocache = $_nocache;
$compiler->getParser()->current_buffer = $_buffer;
$output = "<?php \n";
if ($compiler->_cache['blockNesting'] === 1) {
$output .= "\$_smarty_tpl->getInheritance()->instanceBlock(\$_smarty_tpl, '$_className', $_name);\n";
} else {
$output .= "\$_smarty_tpl->getInheritance()->instanceBlock(\$_smarty_tpl, '$_className', $_name, \$this->tplIndex);\n";
}
$output .= "?>\n";
--$compiler->_cache['blockNesting'];
if ($compiler->_cache['blockNesting'] === 0) {
unset($compiler->_cache['blockNesting']);
}
$compiler->suppressNocacheProcessing = true;
return $output;
}
}

View file

@ -0,0 +1,123 @@
<?php
/**
* Smarty Internal Plugin Compile Break
* Compiles the {break} tag
*
* @author Uwe Tews
*/
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile Break Class
*
*/
class BreakTag extends Base {
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BasePlugin
*/
protected $optional_attributes = ['levels'];
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BasePlugin
*/
protected $shorttag_order = ['levels'];
/**
* Tag name may be overloaded by ContinueTag
*
* @var string
*/
protected $tag = 'break';
/**
* Compiles code for the {break} tag
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
*
* @return string compiled code
* @throws \Smarty\CompilerException
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = array(), $tag = null, $function = null): string
{
[$levels, $foreachLevels] = $this->checkLevels($args, $compiler);
$output = "<?php ";
if ($foreachLevels > 0 && $this->tag === 'continue') {
$foreachLevels--;
}
if ($foreachLevels > 0) {
/* @var ForeachTag $foreachCompiler */
$foreachCompiler = $compiler->getTagCompiler('foreach');
$output .= $foreachCompiler->compileRestore($foreachLevels);
}
$output .= "{$this->tag} {$levels};?>";
return $output;
}
/**
* check attributes and return array of break and foreach levels
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
*
* @return array
* @throws \Smarty\CompilerException
*/
public function checkLevels($args, \Smarty\Compiler\Template $compiler) {
static $_is_loopy = ['for' => true, 'foreach' => true, 'while' => true, 'section' => true];
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
if ($_attr['nocache'] === true) {
$compiler->trigger_template_error('nocache option not allowed', null, true);
}
if (isset($_attr['levels'])) {
if (!is_numeric($_attr['levels'])) {
$compiler->trigger_template_error('level attribute must be a numeric constant', null, true);
}
$levels = $_attr['levels'];
} else {
$levels = 1;
}
$level_count = $levels;
$tagStack = $compiler->getTagStack();
$stack_count = count($tagStack) - 1;
$foreachLevels = 0;
$lastTag = '';
while ($level_count > 0 && $stack_count >= 0) {
if (isset($_is_loopy[$tagStack[$stack_count][0]])) {
$lastTag = $tagStack[$stack_count][0];
if ($level_count === 0) {
break;
}
$level_count--;
if ($tagStack[$stack_count][0] === 'foreach') {
$foreachLevels++;
}
}
$stack_count--;
}
if ($level_count !== 0) {
$compiler->trigger_template_error("cannot {$this->tag} {$levels} level(s)", null, true);
}
if ($lastTag === 'foreach' && $this->tag === 'break' && $foreachLevels > 0) {
$foreachLevels--;
}
return [$levels, $foreachLevels];
}
}

View file

@ -0,0 +1,81 @@
<?php
/**
* Smarty Internal Plugin Compile Function_Call
* Compiles the calls of user defined tags defined by {function}
*
* @author Uwe Tews
*/
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile Function_Call Class
*/
class Call extends Base {
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BaseCompiler
*/
public $required_attributes = ['name'];
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BaseCompiler
*/
public $shorttag_order = ['name'];
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BaseCompiler
*/
public $optional_attributes = ['_any'];
/**
* Compiles the calls of user defined tags defined by {function}
*
* @param array $args array with attributes from parser
* @param object $compiler compiler object
*
* @return string compiled code
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
// save possible attributes
if (isset($_attr['assign'])) {
// output will be stored in a smarty variable instead of being displayed
$_assign = $_attr['assign'];
}
//$_name = trim($_attr['name'], "''");
$_name = $_attr['name'];
unset($_attr['name'], $_attr['assign'], $_attr['nocache']);
// set flag (compiled code of {function} must be included in cache file
if (!$compiler->getTemplate()->caching || $compiler->isNocacheActive() || $compiler->tag_nocache) {
$_nocache = 'true';
} else {
$_nocache = 'false';
}
$_paramsArray = $this->formatParamsArray($_attr);
$_params = 'array(' . implode(',', $_paramsArray) . ')';
//$compiler->suppressNocacheProcessing = true;
// was there an assign attribute
if (isset($_assign)) {
$_output =
"<?php ob_start();\n\$_smarty_tpl->getSmarty()->getRuntime('TplFunction')->callTemplateFunction(\$_smarty_tpl, {$_name}, {$_params}, {$_nocache});\n\$_smarty_tpl->assign({$_assign}, ob_get_clean());?>\n";
} else {
$_output =
"<?php \$_smarty_tpl->getSmarty()->getRuntime('TplFunction')->callTemplateFunction(\$_smarty_tpl, {$_name}, {$_params}, {$_nocache});?>\n";
}
return $_output;
}
}

View file

@ -0,0 +1,72 @@
<?php
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile Capture Class
*
*/
class Capture extends Base {
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BasePlugin
*/
public $shorttag_order = ['name'];
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BasePlugin
*/
public $optional_attributes = ['name', 'assign', 'append'];
/**
* Compiles code for the {$smarty.capture.xxx}
*
* @param \Smarty\Compiler\Template $compiler compiler object
* @param array $parameter array with compilation parameter
*
* @return string compiled code
*/
public static function compileSpecialVariable(
\Smarty\Compiler\Template $compiler,
$parameter = null
) {
return '$_smarty_tpl->getSmarty()->getRuntime(\'Capture\')->getBuffer($_smarty_tpl' .
(isset($parameter[1]) ? ", {$parameter[ 1 ]})" : ')');
}
/**
* Compiles code for the {capture} tag
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
* @param null $parameter
*
* @return string compiled code
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
$buffer = $_attr['name'] ?? "'default'";
$assign = $_attr['assign'] ?? 'null';
$append = $_attr['append'] ?? 'null';
$compiler->_cache['capture_stack'][] = $compiler->tag_nocache;
if ($compiler->tag_nocache) {
// push a virtual {nocache} tag onto the stack.
$compiler->openTag('nocache');
}
return "<?php \$_smarty_tpl->getSmarty()->getRuntime('Capture')->open(\$_smarty_tpl, $buffer, $assign, $append);?>";
}
}

View file

@ -0,0 +1,43 @@
<?php
/**
* Smarty Internal Plugin Compile Capture
* Compiles the {capture} tag
*
* @author Uwe Tews
*/
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile Captureclose Class
*
*/
class CaptureClose extends Base {
/**
* Compiles code for the {/capture} tag
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
* @param null $parameter
*
* @return string compiled code
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
if (array_pop($compiler->_cache['capture_stack'])) {
// pop the virtual {nocache} tag from the stack.
$compiler->closeTag('nocache');
$compiler->tag_nocache = true;
}
return "<?php \$_smarty_tpl->getSmarty()->getRuntime('Capture')->close(\$_smarty_tpl);?>";
}
}

View file

@ -0,0 +1,77 @@
<?php
/**
* Smarty Internal Plugin Compile Config Load
* Compiles the {config load} tag
*
* @author Uwe Tews
*/
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
use Smarty\Smarty;
/**
* Smarty Internal Plugin Compile Config Load Class
*
*/
class ConfigLoad extends Base {
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BasePlugin
*/
protected $required_attributes = ['file'];
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BasePlugin
*/
protected $shorttag_order = ['file', 'section'];
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BasePlugin
*/
protected $optional_attributes = ['section'];
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BasePlugin
*/
protected $option_flags = [];
/**
* Compiles code for the {config_load} tag
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
*
* @return string compiled code
* @throws \Smarty\CompilerException
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
// save possible attributes
$conf_file = $_attr['file'];
$section = $_attr['section'] ?? 'null';
// create config object
return "<?php\n\$_smarty_tpl->configLoad({$conf_file}, {$section});\n?>\n";
}
}

View file

@ -0,0 +1,27 @@
<?php
/**
* Smarty Internal Plugin Compile Continue
* Compiles the {continue} tag
*
* @author Uwe Tews
*/
namespace Smarty\Compile\Tag;
/**
* Smarty Internal Plugin Compile Continue Class
*
*/
class ContinueTag extends BreakTag {
/**
* Tag name
*
* @var string
*/
protected $tag = 'continue';
}

View file

@ -0,0 +1,45 @@
<?php
/**
* Smarty Internal Plugin Compile Debug
* Compiles the {debug} tag.
* It opens a window the the Smarty Debugging Console.
*
* @author Uwe Tews
*/
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile Debug Class
*
*/
class Debug extends Base {
/**
* Compiles code for the {debug} tag
*
* @param array $args array with attributes from parser
* @param object $compiler compiler object
*
* @return string compiled code
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
// check and get attributes, may trigger errors
$this->getAttributes($compiler, $args);
// compile always as nocache
$compiler->tag_nocache = true;
// display debug template
$_output =
"<?php \$_smarty_debug = new \\Smarty\\Debug;\n \$_smarty_debug->display_debug(\$_smarty_tpl);\n";
$_output .= "unset(\$_smarty_debug);\n?>";
return $_output;
}
}

View file

@ -0,0 +1,86 @@
<?php
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile ElseIf Class
*
*/
class ElseIfTag extends Base {
/**
* Compiles code for the {elseif} tag
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
* @param array $parameter array with compilation parameter
*
* @return string compiled code
* @throws \Smarty\CompilerException
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
[$nesting, $nocache_pushed] = $this->closeTag($compiler, ['if', 'elseif']);
if (!isset($parameter['if condition'])) {
$compiler->trigger_template_error('missing elseif condition', null, true);
}
$assignCode = '';
$var = '';
if (is_array($parameter['if condition'])) {
$condition_by_assign = true;
if (is_array($parameter['if condition']['var'])) {
$var = $parameter['if condition']['var']['var'];
} else {
$var = $parameter['if condition']['var'];
}
if ($compiler->isNocacheActive()) {
// create nocache var to make it know for further compiling
$compiler->setNocacheInVariable($var);
}
$prefixVar = $compiler->getNewPrefixVariable();
$assignCode = "<?php {$prefixVar} = {$parameter[ 'if condition' ][ 'value' ]};?>\n";
$assignCompiler = new Assign();
$assignAttr = [];
$assignAttr[]['value'] = $prefixVar;
if (is_array($parameter['if condition']['var'])) {
$assignAttr[]['var'] = $parameter['if condition']['var']['var'];
$assignCode .= $assignCompiler->compile(
$assignAttr,
$compiler,
['smarty_internal_index' => $parameter['if condition']['var']['smarty_internal_index']]
);
} else {
$assignAttr[]['var'] = $parameter['if condition']['var'];
$assignCode .= $assignCompiler->compile($assignAttr, $compiler, []);
}
} else {
$condition_by_assign = false;
}
$prefixCode = $compiler->getPrefixCode();
if (empty($prefixCode)) {
if ($condition_by_assign) {
$this->openTag($compiler, 'elseif', [$nesting + 1, $compiler->tag_nocache]);
$_output = $compiler->appendCode("<?php } else {\n?>", $assignCode);
return $compiler->appendCode($_output, "<?php if ({$prefixVar}) {?>");
} else {
$this->openTag($compiler, 'elseif', [$nesting, $nocache_pushed]);
return "<?php } elseif ({$parameter['if condition']}) {?>";
}
} else {
$_output = $compiler->appendCode("<?php } else {\n?>", $prefixCode);
$this->openTag($compiler, 'elseif', [$nesting + 1, $nocache_pushed]);
if ($condition_by_assign) {
$_output = $compiler->appendCode($_output, $assignCode);
return $compiler->appendCode($_output, "<?php if ({$prefixVar}) {?>");
} else {
return $compiler->appendCode($_output, "<?php if ({$parameter['if condition']}) {?>");
}
}
}
}

View file

@ -0,0 +1,29 @@
<?php
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile Else Class
*
*/
class ElseTag extends Base {
/**
* Compiles code for the {else} tag
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
*
* @return string compiled code
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
[$nesting, $compiler->tag_nocache] = $this->closeTag($compiler, ['if', 'elseif']);
$this->openTag($compiler, 'else', [$nesting, $compiler->tag_nocache]);
return '<?php } else { ?>';
}
}

View file

@ -0,0 +1,74 @@
<?php
/**
* Smarty Internal Plugin Compile Eval
* Compiles the {eval} tag.
*
* @author Uwe Tews
*/
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile Eval Class
*
*/
class EvalTag extends Base {
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BaseCompiler
*/
public $required_attributes = ['var'];
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BaseCompiler
*/
public $optional_attributes = ['assign'];
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BaseCompiler
*/
public $shorttag_order = ['var', 'assign'];
/**
* Compiles code for the {eval} tag
*
* @param array $args array with attributes from parser
* @param object $compiler compiler object
*
* @return string compiled code
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
if (isset($_attr['assign'])) {
// output will be stored in a smarty variable instead of being displayed
$_assign = $_attr['assign'];
}
// create template object
$_output =
"\$_template = new \\Smarty\\Template('eval:'.{$_attr[ 'var' ]}, \$_smarty_tpl->getSmarty(), \$_smarty_tpl);";
//was there an assign attribute?
if (isset($_assign)) {
$_output .= "\$_smarty_tpl->assign($_assign,\$_template->fetch());";
} else {
$_output .= 'echo $_template->fetch();';
}
return "<?php $_output ?>";
}
}

View file

@ -0,0 +1,87 @@
<?php
/**
* Smarty Internal Plugin Compile extend
* Compiles the {extends} tag
*
* @author Uwe Tews
*/
namespace Smarty\Compile\Tag;
/**
* Smarty Internal Plugin Compile extend Class
*
*/
class ExtendsTag extends Inheritance {
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BasePlugin
*/
protected $required_attributes = ['file'];
/**
* Array of names of optional attribute required by tag
* use array('_any') if there is no restriction of attributes names
*
* @var array
*/
protected $optional_attributes = [];
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BasePlugin
*/
protected $shorttag_order = ['file'];
/**
* Compiles code for the {extends} tag extends: resource
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
*
* @return string compiled code
* @throws \Smarty\CompilerException
* @throws \Smarty\Exception
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
if ($_attr['nocache'] === true) {
$compiler->trigger_template_error('nocache option not allowed', $compiler->getParser()->lex->line - 1);
}
if (strpos($_attr['file'], '$_tmp') !== false) {
$compiler->trigger_template_error('illegal value for file attribute', $compiler->getParser()->lex->line - 1);
}
// add code to initialize inheritance
$this->registerInit($compiler, true);
$this->compileEndChild($compiler, $_attr['file']);
return '';
}
/**
* Add code for inheritance endChild() method to end of template
*
* @param \Smarty\Compiler\Template $compiler
* @param null|string $template optional inheritance parent template
*
* @throws \Smarty\CompilerException
* @throws \Smarty\Exception
*/
private function compileEndChild(\Smarty\Compiler\Template $compiler, $template = null) {
$compiler->getParser()->template_postfix[] = new \Smarty\ParseTree\Tag(
$compiler->getParser(),
'<?php $_smarty_tpl->getInheritance()->endChild($_smarty_tpl' .
(isset($template) ? ", {$template}, \$_smarty_current_dir" : '') . ");\n?>"
);
}
}

View file

@ -0,0 +1,51 @@
<?php
/**
* Smarty Internal Plugin Compile For
* Compiles the {for} {forelse} {/for} tags
*
* @author Uwe Tews
*/
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile Forclose Class
*
*/
class ForClose extends Base {
/**
* Compiles code for the {/for} tag
*
* @param array $args array with attributes from parser
* @param object $compiler compiler object
* @param array $parameter array with compilation parameter
*
* @return string compiled code
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
$compiler->loopNesting--;
[$openTag, $nocache_pushed] = $this->closeTag($compiler, ['for', 'forelse']);
$output = "<?php }\n";
if ($openTag !== 'forelse') {
$output .= "}\n";
}
$output .= "?>";
if ($nocache_pushed) {
// pop the pushed virtual nocache tag
$this->closeTag($compiler, 'nocache');
$compiler->tag_nocache = true;
}
return $output;
}
}

View file

@ -0,0 +1,30 @@
<?php
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile Forelse Class
*
*/
class ForElse extends Base {
/**
* Compiles code for the {forelse} tag
*
* @param array $args array with attributes from parser
* @param object $compiler compiler object
* @param array $parameter array with compilation parameter
*
* @return string compiled code
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
[$tagName, $nocache_pushed] = $this->closeTag($compiler, ['for']);
$this->openTag($compiler, 'forelse', ['forelse', $nocache_pushed]);
return "<?php }} else { ?>";
}
}

View file

@ -0,0 +1,101 @@
<?php
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile For Class
*
*/
class ForTag extends Base {
/**
* Compiles code for the {for} tag
* Smarty supports two different syntax's:
* - {for $var in $array}
* For looping over arrays or iterators
* - {for $x=0; $x<$y; $x++}
* For general loops
* The parser is generating different sets of attribute by which this compiler can
* determine which syntax is used.
*
* @param array $args array with attributes from parser
* @param object $compiler compiler object
* @param array $parameter array with compilation parameter
*
* @return string compiled code
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
$compiler->loopNesting++;
if ($parameter === 0) {
$this->required_attributes = ['start', 'to'];
$this->optional_attributes = ['max', 'step'];
} else {
$this->required_attributes = ['start', 'ifexp', 'var', 'step'];
$this->optional_attributes = [];
}
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
$output = "<?php\n";
if ($parameter === 1) {
foreach ($_attr['start'] as $_statement) {
if (is_array($_statement['var'])) {
$var = $_statement['var']['var'];
$index = $_statement['var']['smarty_internal_index'];
} else {
$var = $_statement['var'];
$index = '';
}
$output .= "\$_smarty_tpl->assign($var, null);\n";
$output .= "\$_smarty_tpl->tpl_vars[$var]->value{$index} = {$_statement['value']};\n";
}
if (is_array($_attr['var'])) {
$var = $_attr['var']['var'];
$index = $_attr['var']['smarty_internal_index'];
} else {
$var = $_attr['var'];
$index = '';
}
$output .= "if ($_attr[ifexp]) {\nfor (\$_foo=true;$_attr[ifexp]; \$_smarty_tpl->tpl_vars[$var]->value{$index}$_attr[step]) {\n";
} else {
$_statement = $_attr['start'];
if (is_array($_statement['var'])) {
$var = $_statement['var']['var'];
$index = $_statement['var']['smarty_internal_index'];
} else {
$var = $_statement['var'];
$index = '';
}
$output .= "\$_smarty_tpl->assign($var, null);";
if (isset($_attr['step'])) {
$output .= "\$_smarty_tpl->tpl_vars[$var]->step = $_attr[step];";
} else {
$output .= "\$_smarty_tpl->tpl_vars[$var]->step = 1;";
}
if (isset($_attr['max'])) {
$output .= "\$_smarty_tpl->tpl_vars[$var]->total = (int) min(ceil((\$_smarty_tpl->tpl_vars[$var]->step > 0 ? $_attr[to]+1 - ($_statement[value]) : $_statement[value]-($_attr[to])+1)/abs(\$_smarty_tpl->tpl_vars[$var]->step)),$_attr[max]);\n";
} else {
$output .= "\$_smarty_tpl->tpl_vars[$var]->total = (int) ceil((\$_smarty_tpl->tpl_vars[$var]->step > 0 ? $_attr[to]+1 - ($_statement[value]) : $_statement[value]-($_attr[to])+1)/abs(\$_smarty_tpl->tpl_vars[$var]->step));\n";
}
$output .= "if (\$_smarty_tpl->tpl_vars[$var]->total > 0) {\n";
$output .= "for (\$_smarty_tpl->tpl_vars[$var]->value{$index} = $_statement[value], \$_smarty_tpl->tpl_vars[$var]->iteration = 1;\$_smarty_tpl->tpl_vars[$var]->iteration <= \$_smarty_tpl->tpl_vars[$var]->total;\$_smarty_tpl->tpl_vars[$var]->value{$index} += \$_smarty_tpl->tpl_vars[$var]->step, \$_smarty_tpl->tpl_vars[$var]->iteration++) {\n";
$output .= "\$_smarty_tpl->tpl_vars[$var]->first = \$_smarty_tpl->tpl_vars[$var]->iteration === 1;";
$output .= "\$_smarty_tpl->tpl_vars[$var]->last = \$_smarty_tpl->tpl_vars[$var]->iteration === \$_smarty_tpl->tpl_vars[$var]->total;";
}
$output .= '?>';
if ($compiler->tag_nocache) {
// push a {nocache} tag onto the stack to prevent caching of this for loop
$this->openTag($compiler, 'nocache');
}
$this->openTag($compiler, 'for', ['for', $compiler->tag_nocache]);
return $output;
}
}

View file

@ -0,0 +1,55 @@
<?php
/**
* Smarty Internal Plugin Compile Foreach
* Compiles the {foreach} {foreachelse} {/foreach} tags
*
* @author Uwe Tews
*/
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile Foreachclose Class
*
*/
class ForeachClose extends Base {
/**
* Compiles code for the {/foreach} tag
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
*
* @return string compiled code
* @throws \Smarty\CompilerException
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
$compiler->loopNesting--;
[$openTag, $nocache_pushed, $localVariablePrefix, $item, $restore] = $this->closeTag($compiler, ['foreach', 'foreachelse']);
if ($nocache_pushed) {
// pop the pushed virtual nocache tag
$this->closeTag($compiler, 'nocache');
$compiler->tag_nocache = true;
}
$output = "<?php\n";
if ($restore) {
$output .= "\$_smarty_tpl->setVariable('{$item}', {$localVariablePrefix}Backup);\n";
}
$output .= "}\n";
/* @var \Smarty\Compile\Tag\ForeachTag $foreachCompiler */
$foreachCompiler = $compiler->getTagCompiler('foreach');
$output .= $foreachCompiler->compileRestore(1);
$output .= "?>";
return $output;
}
}

View file

@ -0,0 +1,35 @@
<?php
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile Foreachelse Class
*
*/
class ForeachElse extends Base {
/**
* Compiles code for the {foreachelse} tag
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
*
* @return string compiled code
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
[$openTag, $nocache_pushed, $localVariablePrefix, $item, $restore] = $this->closeTag($compiler, ['foreach']);
$this->openTag($compiler, 'foreachelse', ['foreachelse', $nocache_pushed, $localVariablePrefix, $item, false]);
$output = "<?php\n";
if ($restore) {
$output .= "\$_smarty_tpl->setVariable('{$item}', {$localVariablePrefix}Backup);\n";
}
$output .= "}\nif ({$localVariablePrefix}DoElse) {\n?>";
return $output;
}
}

View file

@ -0,0 +1,206 @@
<?php
/**
* Smarty Internal Plugin Compile ForeachSection
* Shared methods for {foreach} {section} tags
*
* @author Uwe Tews
*/
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile ForeachSection Class
*
*/
abstract class ForeachSection extends Base {
/**
* Name of this tag
*
* @var string
*/
protected $tagName = '';
/**
* Valid properties of $smarty.xxx variable
*
* @var array
*/
protected $nameProperties = [];
/**
* {section} tag has no item properties
*
* @var array
*/
protected $itemProperties = null;
/**
* {section} tag has always name attribute
*
* @var bool
*/
protected $isNamed = true;
/**
* @var array
*/
protected $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\Compiler\Template $compiler
*
* @throws \Smarty\Exception
*/
protected function scanForProperties($attributes, \Smarty\Compiler\Template $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\Compiler\Template $compiler
*/
private function matchTemplateSource(\Smarty\Compiler\Template $compiler) {
$this->matchProperty($compiler->getParser()->lex->data);
}
/**
* Find matches in all parent template source
*
* @param \Smarty\Compiler\Template $compiler
*
* @throws \Smarty\Exception
*/
private function matchParentTemplateSource(\Smarty\Compiler\Template $compiler) {
// search parent compiler template source
$nextCompiler = $compiler;
while ($nextCompiler !== $nextCompiler->getParentCompiler()) {
$nextCompiler = $nextCompiler->getParentCompiler();
if ($compiler !== $nextCompiler) {
// get template source
$_content = $nextCompiler->getTemplate()->getSource()->getContent();
if ($_content !== '') {
// run pre filter if required
$_content = $nextCompiler->getSmarty()->runPreFilters($_content, $nextCompiler->getTemplate());
$this->matchProperty($_content);
}
}
}
}
/**
* Compiles code for the {$smarty.foreach.xxx} or {$smarty.section.xxx}tag
*
* @param \Smarty\Compiler\Template $compiler compiler object
* @param array $parameter array with compilation parameter
*
* @return string compiled code
* @throws \Smarty\CompilerException
*/
public function compileSpecialVariable(\Smarty\Compiler\Template $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 "(\$_smarty_tpl->getValue({$tagVar})['{$property}'] ?? null)";
}
}

View file

@ -0,0 +1,286 @@
<?php
namespace Smarty\Compile\Tag;
/**
* Smarty Internal Plugin Compile Foreach Class
*
*/
class ForeachTag extends ForeachSection {
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BasePlugin
*/
protected $required_attributes = ['from', 'item'];
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BasePlugin
*/
protected $optional_attributes = ['name', 'key', 'properties'];
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BasePlugin
*/
protected $shorttag_order = ['from', 'item', 'key', 'name'];
/**
* counter
*
* @var int
*/
private static $counter = 0;
/**
* Name of this tag
*
* @var string
*/
protected $tagName = 'foreach';
/**
* Valid properties of $smarty.foreach.name.xxx variable
*
* @var array
*/
protected $nameProperties = ['first', 'last', 'index', 'iteration', 'show', 'total'];
/**
* Valid properties of $item@xxx variable
*
* @var array
*/
protected $itemProperties = ['first', 'last', 'index', 'iteration', 'show', 'total', 'key'];
/**
* Flag if tag had name attribute
*
* @var bool
*/
protected $isNamed = false;
/**
* Compiles code for the {foreach} tag
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
*
* @return string compiled code
* @throws \Smarty\CompilerException
* @throws \Smarty\Exception
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
$compiler->loopNesting++;
// init
$this->isNamed = false;
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
$from = $_attr['from'];
$item = $compiler->getId($_attr['item']);
if ($item === false) {
$item = $this->getVariableName($_attr['item']);
}
$key = $name = null;
$attributes = ['item' => $item];
if (isset($_attr['key'])) {
$key = $compiler->getId($_attr['key']);
if ($key === false) {
$key = $this->getVariableName($_attr['key']);
}
$attributes['key'] = $key;
}
if (isset($_attr['name'])) {
$this->isNamed = true;
$name = $attributes['name'] = $compiler->getId($_attr['name']);
}
foreach ($attributes as $a => $v) {
if ($v === false) {
$compiler->trigger_template_error("'{$a}' attribute/variable has illegal value", null, true);
}
}
$fromName = $this->getVariableName($_attr['from']);
if ($fromName) {
foreach (['item', 'key'] as $a) {
if (isset($attributes[$a]) && $attributes[$a] === $fromName) {
$compiler->trigger_template_error(
"'{$a}' and 'from' may not have same variable name '{$fromName}'",
null,
true
);
}
}
}
$itemVar = "\$_smarty_tpl->getVariable('{$item}')";
$localVariablePrefix = '$foreach' . self::$counter++;
// search for used tag attributes
$itemAttr = [];
$namedAttr = [];
$this->scanForProperties($attributes, $compiler);
if (!empty($this->matchResults['item'])) {
$itemAttr = $this->matchResults['item'];
}
if (!empty($this->matchResults['named'])) {
$namedAttr = $this->matchResults['named'];
}
if (isset($_attr['properties']) && preg_match_all('/[\'](.*?)[\']/', $_attr['properties'], $match)) {
foreach ($match[1] as $prop) {
if (in_array($prop, $this->itemProperties)) {
$itemAttr[$prop] = true;
} else {
$compiler->trigger_template_error("Invalid property '{$prop}'", null, true);
}
}
if ($this->isNamed) {
foreach ($match[1] as $prop) {
if (in_array($prop, $this->nameProperties)) {
$nameAttr[$prop] = true;
} else {
$compiler->trigger_template_error("Invalid property '{$prop}'", null, true);
}
}
}
}
if (isset($itemAttr['first'])) {
$itemAttr['index'] = true;
}
if (isset($namedAttr['first'])) {
$namedAttr['index'] = true;
}
if (isset($namedAttr['last'])) {
$namedAttr['iteration'] = true;
$namedAttr['total'] = true;
}
if (isset($itemAttr['last'])) {
$itemAttr['iteration'] = true;
$itemAttr['total'] = true;
}
if (isset($namedAttr['show'])) {
$namedAttr['total'] = true;
}
if (isset($itemAttr['show'])) {
$itemAttr['total'] = true;
}
$keyTerm = '';
if (isset($attributes['key'])) {
$keyTerm = "\$_smarty_tpl->getVariable('{$key}')->value => ";
}
if (isset($itemAttr['key'])) {
$keyTerm = "{$itemVar}->key => ";
}
if ($this->isNamed) {
$foreachVar = "\$_smarty_tpl->tpl_vars['__smarty_foreach_{$attributes['name']}']";
}
$needTotal = isset($itemAttr['total']);
if ($compiler->tag_nocache) {
// push a {nocache} tag onto the stack to prevent caching of this block
$this->openTag($compiler, 'nocache');
}
// Register tag
$this->openTag(
$compiler,
'foreach',
['foreach', $compiler->tag_nocache, $localVariablePrefix, $item, !empty($itemAttr)]
);
// generate output code
$output = "<?php\n";
$output .= "\$_from = \$_smarty_tpl->getSmarty()->getRuntime('Foreach')->init(\$_smarty_tpl, $from, " .
var_export($item, true);
if ($name || $needTotal || $key) {
$output .= ', ' . var_export($needTotal, true);
}
if ($name || $key) {
$output .= ', ' . var_export($key, true);
}
if ($name) {
$output .= ', ' . var_export($name, true) . ', ' . var_export($namedAttr, true);
}
$output .= ");\n";
if (isset($itemAttr['show'])) {
$output .= "{$itemVar}->show = ({$itemVar}->total > 0);\n";
}
if (isset($itemAttr['iteration'])) {
$output .= "{$itemVar}->iteration = 0;\n";
}
if (isset($itemAttr['index'])) {
$output .= "{$itemVar}->index = -1;\n";
}
$output .= "{$localVariablePrefix}DoElse = true;\n";
$output .= "foreach (\$_from ?? [] as {$keyTerm}{$itemVar}->value) {\n";
$output .= "{$localVariablePrefix}DoElse = false;\n";
if (isset($attributes['key']) && isset($itemAttr['key'])) {
$output .= "\$_smarty_tpl->assign('{$key}', {$itemVar}->key);\n";
}
if (isset($itemAttr['iteration'])) {
$output .= "{$itemVar}->iteration++;\n";
}
if (isset($itemAttr['index'])) {
$output .= "{$itemVar}->index++;\n";
}
if (isset($itemAttr['first'])) {
$output .= "{$itemVar}->first = !{$itemVar}->index;\n";
}
if (isset($itemAttr['last'])) {
$output .= "{$itemVar}->last = {$itemVar}->iteration === {$itemVar}->total;\n";
}
if (isset($foreachVar)) {
if (isset($namedAttr['iteration'])) {
$output .= "{$foreachVar}->value['iteration']++;\n";
}
if (isset($namedAttr['index'])) {
$output .= "{$foreachVar}->value['index']++;\n";
}
if (isset($namedAttr['first'])) {
$output .= "{$foreachVar}->value['first'] = !{$foreachVar}->value['index'];\n";
}
if (isset($namedAttr['last'])) {
$output .= "{$foreachVar}->value['last'] = {$foreachVar}->value['iteration'] === {$foreachVar}->value['total'];\n";
}
}
if (!empty($itemAttr)) {
$output .= "{$localVariablePrefix}Backup = clone \$_smarty_tpl->getVariable('{$item}');\n";
}
$output .= '?>';
return $output;
}
/**
* Get variable name from string
*
* @param string $input
*
* @return bool|string
*/
private function getVariableName($input) {
if (preg_match('~^[$]_smarty_tpl->getValue\([\'"]*([0-9]*[a-zA-Z_]\w*)[\'"]*\]\)$~', $input, $match)) {
return $match[1];
}
return false;
}
/**
* Compiles code for to restore saved template variables
*
* @param int $levels number of levels to restore
*
* @return string compiled code
*/
public function compileRestore($levels) {
return "\$_smarty_tpl->getSmarty()->getRuntime('Foreach')->restore(\$_smarty_tpl, {$levels});";
}
}

View file

@ -0,0 +1,164 @@
<?php
/**
* Smarty Internal Plugin Compile Function
* Compiles the {function} {/function} tags
*
* @author Uwe Tews
*/
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile Functionclose Class
*
*/
class FunctionClose extends Base {
/**
* Compiler object
*
* @var object
*/
private $compiler = null;
/**
* Compiles code for the {/function} tag
*
* @param array $args array with attributes from parser
* @param object|\Smarty\Compiler\Template $compiler compiler object
*
* @return string compiled code
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
$this->compiler = $compiler;
$saved_data = $this->closeTag($compiler, ['function']);
$_attr = $saved_data[0];
$_name = trim($_attr['name'], '\'"');
$parentCompiler = $compiler->getParentCompiler();
$parentCompiler->tpl_function[$_name]['compiled_filepath'] =
$parentCompiler->getTemplate()->getCompiled()->filepath;
$parentCompiler->tpl_function[$_name]['uid'] = $compiler->getTemplate()->getSource()->uid;
$_parameter = $_attr;
unset($_parameter['name']);
// default parameter
$_paramsArray = $this->formatParamsArray($_attr);
$_paramsCode = (new \Smarty\Compiler\CodeFrame($compiler->getTemplate()))->insertLocalVariables();
if (!empty($_paramsArray)) {
$_params = 'array(' . implode(',', $_paramsArray) . ')';
$_paramsCode .= "\$params = array_merge($_params, \$params);\n";
}
$_functionCode = $compiler->getParser()->current_buffer;
// setup buffer for template function code
$compiler->getParser()->current_buffer = new \Smarty\ParseTree\Template();
$_funcName = "smarty_template_function_{$_name}_{$compiler->getTemplate()->getCompiled()->nocache_hash}";
$_funcNameCaching = $_funcName . '_nocache';
if ($compiler->getTemplate()->getCompiled()->getNocacheCode()) {
$parentCompiler->tpl_function[$_name]['call_name_caching'] = $_funcNameCaching;
$output = "<?php\n";
$output .= $compiler->cStyleComment(" {$_funcNameCaching} ") . "\n";
$output .= "if (!function_exists('{$_funcNameCaching}')) {\n";
$output .= "function {$_funcNameCaching} (\\Smarty\\Template \$_smarty_tpl,\$params) {\n";
$output .= "ob_start();\n";
$output .= "\$_smarty_tpl->getCompiled()->setNocacheCode(true);\n";
$output .= $_paramsCode;
$output .= "foreach (\$params as \$key => \$value) {\n\$_smarty_tpl->assign(\$key, \$value);\n}\n";
$output .= "\$params = var_export(\$params, true);\n";
$output .= "echo \"/*%%SmartyNocache:{$compiler->getTemplate()->getCompiled()->nocache_hash}%%*/<?php ";
$output .= "\\\$_smarty_tpl->pushStack();\nforeach (\$params as \\\$key => \\\$value) {\n\\\$_smarty_tpl->assign(\\\$key, \\\$value);\n}\n?>";
$output .= "/*/%%SmartyNocache:{$compiler->getTemplate()->getCompiled()->nocache_hash}%%*/\";?>";
$compiler->getParser()->current_buffer->append_subtree(
$compiler->getParser(),
new \Smarty\ParseTree\Tag(
$compiler->getParser(),
$output
)
);
$compiler->getParser()->current_buffer->append_subtree($compiler->getParser(), $_functionCode);
$output = "<?php echo \"/*%%SmartyNocache:{$compiler->getTemplate()->getCompiled()->nocache_hash}%%*/<?php ";
$output .= "\\\$_smarty_tpl->popStack();?>\n";
$output .= "/*/%%SmartyNocache:{$compiler->getTemplate()->getCompiled()->nocache_hash}%%*/\";\n?>";
$output .= "<?php echo str_replace('{$compiler->getTemplate()->getCompiled()->nocache_hash}', \$_smarty_tpl->getCompiled()->nocache_hash ?? '', ob_get_clean());\n";
$output .= "}\n}\n";
$output .= $compiler->cStyleComment("/ {$_funcName}_nocache ") . "\n\n";
$output .= "?>\n";
$compiler->getParser()->current_buffer->append_subtree(
$compiler->getParser(),
new \Smarty\ParseTree\Tag(
$compiler->getParser(),
$output
)
);
$_functionCode = new \Smarty\ParseTree\Tag(
$compiler->getParser(),
preg_replace_callback(
"/((<\?php )?echo '\/\*%%SmartyNocache:{$compiler->getTemplate()->getCompiled()->nocache_hash}%%\*\/([\S\s]*?)\/\*\/%%SmartyNocache:{$compiler->getTemplate()->getCompiled()->nocache_hash}%%\*\/';(\?>\n)?)/",
[$this, 'removeNocache'],
$_functionCode->to_smarty_php($compiler->getParser())
)
);
}
$parentCompiler->tpl_function[$_name]['call_name'] = $_funcName;
$output = "<?php\n";
$output .= $compiler->cStyleComment(" {$_funcName} ") . "\n";
$output .= "if (!function_exists('{$_funcName}')) {\n";
$output .= "function {$_funcName}(\\Smarty\\Template \$_smarty_tpl,\$params) {\n";
$output .= $_paramsCode;
$output .= "foreach (\$params as \$key => \$value) {\n\$_smarty_tpl->assign(\$key, \$value);\n}\n";
$output .= "?>\n";
$compiler->getParser()->current_buffer->append_subtree(
$compiler->getParser(),
new \Smarty\ParseTree\Tag(
$compiler->getParser(),
$output
)
);
$compiler->getParser()->current_buffer->append_subtree($compiler->getParser(), $_functionCode);
$output = "<?php\n}}\n";
$output .= $compiler->cStyleComment("/ {$_funcName} ") . "\n\n";
$output .= "?>\n";
$compiler->getParser()->current_buffer->append_subtree(
$compiler->getParser(),
new \Smarty\ParseTree\Tag(
$compiler->getParser(),
$output
)
);
$parentCompiler->blockOrFunctionCode .= $compiler->getParser()->current_buffer->to_smarty_php($compiler->getParser());
// restore old buffer
$compiler->getParser()->current_buffer = $saved_data[1];
// restore old status
$compiler->getTemplate()->getCompiled()->setNocacheCode($saved_data[2]);
$compiler->getTemplate()->caching = $saved_data[3];
return '';
}
/**
* Remove nocache code
*
* @param $match
*
* @return string
*/
public function removeNocache($match) {
$hash = $this->compiler->getTemplate()->getCompiled()->nocache_hash;
$code =
preg_replace(
"/((<\?php )?echo '\/\*%%SmartyNocache:{$hash}%%\*\/)|(\/\*\/%%SmartyNocache:{$hash}%%\*\/';(\?>\n)?)/",
'',
$match[0]
);
return str_replace(['\\\'', '\\\\\''], ['\'', '\\\''], $code);
}
}

View file

@ -0,0 +1,73 @@
<?php
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile Function Class
*
*/
class FunctionTag extends Base {
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BasePlugin
*/
protected $required_attributes = ['name'];
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BasePlugin
*/
protected $shorttag_order = ['name'];
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BasePlugin
*/
protected $optional_attributes = ['_any'];
/**
* Compiles code for the {function} tag
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
*
* @return string compiled code
* @throws \Smarty\CompilerException
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
if ($_attr['nocache'] === true) {
$compiler->trigger_template_error('nocache option not allowed', null, true);
}
unset($_attr['nocache']);
$_name = trim($_attr['name'], '\'"');
if (!preg_match('/^[a-zA-Z0-9_\x80-\xff]+$/', $_name)) {
$compiler->trigger_template_error("Function name contains invalid characters: {$_name}", null, true);
}
$compiler->getParentCompiler()->tpl_function[$_name] = [];
$save = [
$_attr, $compiler->getParser()->current_buffer, $compiler->getTemplate()->getCompiled()->getNocacheCode(),
$compiler->getTemplate()->caching,
];
$this->openTag($compiler, 'function', $save);
// Init temporary context
$compiler->getParser()->current_buffer = new \Smarty\ParseTree\Template();
$compiler->getTemplate()->getCompiled()->setNocacheCode(false);
return '';
}
}

View file

@ -0,0 +1,48 @@
<?php
/**
* Smarty Internal Plugin Compile If
* Compiles the {if} {else} {elseif} {/if} tags
*
* @author Uwe Tews
*/
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile Ifclose Class
*
*/
class IfClose extends Base {
/**
* Compiles code for the {/if} tag
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
*
* @return string compiled code
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
[$nesting, $nocache_pushed] = $this->closeTag($compiler, ['if', 'else', 'elseif']);
if ($nocache_pushed) {
// pop the pushed virtual nocache tag
$this->closeTag($compiler, 'nocache');
$compiler->tag_nocache = true;
}
$tmp = '';
for ($i = 0; $i < $nesting; $i++) {
$tmp .= '}';
}
return "<?php {$tmp}?>";
}
}

View file

@ -0,0 +1,70 @@
<?php
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile If Class
*
*/
class IfTag extends Base {
/**
* Compiles code for the {if} tag
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
* @param array $parameter array with compilation parameter
*
* @return string compiled code
* @throws \Smarty\CompilerException
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
if ($compiler->tag_nocache) {
// push a {nocache} tag onto the stack to prevent caching of this block
$this->openTag($compiler, 'nocache');
}
$this->openTag($compiler, 'if', [1, $compiler->tag_nocache]);
if (!isset($parameter['if condition'])) {
$compiler->trigger_template_error('missing if condition', null, true);
}
if (is_array($parameter['if condition'])) {
if (is_array($parameter['if condition']['var'])) {
$var = $parameter['if condition']['var']['var'];
} else {
$var = $parameter['if condition']['var'];
}
if ($compiler->isNocacheActive()) {
// create nocache var to make it know for further compiling
$compiler->setNocacheInVariable($var);
}
$prefixVar = $compiler->getNewPrefixVariable();
$_output = "<?php {$prefixVar} = {$parameter[ 'if condition' ][ 'value' ]};?>\n";
$assignAttr = [];
$assignAttr[]['value'] = $prefixVar;
$assignCompiler = new Assign();
if (is_array($parameter['if condition']['var'])) {
$assignAttr[]['var'] = $parameter['if condition']['var']['var'];
$_output .= $assignCompiler->compile(
$assignAttr,
$compiler,
['smarty_internal_index' => $parameter['if condition']['var']['smarty_internal_index']]
);
} else {
$assignAttr[]['var'] = $parameter['if condition']['var'];
$_output .= $assignCompiler->compile($assignAttr, $compiler, []);
}
$_output .= "<?php if ({$prefixVar}) {?>";
return $_output;
} else {
return "<?php if ({$parameter['if condition']}) {?>";
}
}
}

View file

@ -0,0 +1,189 @@
<?php
/**
* Smarty Internal Plugin Compile Include
* Compiles the {include} tag
*
* @author Uwe Tews
*/
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
use Smarty\Compiler\Template;
use Smarty\Data;
use Smarty\Smarty;
use Smarty\Template\Compiled;
/**
* Smarty Internal Plugin Compile Include Class
*
*/
class IncludeTag extends Base {
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BaseCompiler
*/
protected $required_attributes = ['file'];
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BaseCompiler
*/
protected $shorttag_order = ['file'];
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BaseCompiler
*/
protected $option_flags = ['nocache', 'inline', 'caching'];
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BaseCompiler
*/
protected $optional_attributes = ['_any'];
/**
* Compiles code for the {include} tag
*
* @param array $args array with attributes from parser
* @param Template $compiler compiler object
*
* @return string
* @throws \Exception
* @throws \Smarty\CompilerException
* @throws \Smarty\Exception
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
$uid = $t_hash = null;
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
$fullResourceName = $source_resource = $_attr['file'];
$variable_template = false;
// parse resource_name
if (preg_match('/^([\'"])(([A-Za-z0-9_\-]{2,})[:])?(([^$()]+)|(.+))\1$/', $source_resource, $match)) {
$type = !empty($match[3]) ? $match[3] : $compiler->getTemplate()->getSmarty()->default_resource_type;
$name = !empty($match[5]) ? $match[5] : $match[6];
$handler = \Smarty\Resource\BasePlugin::load($compiler->getSmarty(), $type);
if ($handler->recompiled) {
$variable_template = true;
}
if (!$variable_template) {
if ($type !== 'string') {
$fullResourceName = "{$type}:{$name}";
$compiled = $compiler->getParentCompiler()->getTemplate()->getCompiled();
if (isset($compiled->includes[$fullResourceName])) {
$compiled->includes[$fullResourceName]++;
} else {
if ("{$compiler->getTemplate()->getSource()->type}:{$compiler->getTemplate()->getSource()->name}" ==
$fullResourceName
) {
// recursive call of current template
$compiled->includes[$fullResourceName] = 2;
} else {
$compiled->includes[$fullResourceName] = 1;
}
}
$fullResourceName = $match[1] . $fullResourceName . $match[1];
}
}
}
// scope setup
$_scope = isset($_attr['scope']) ? $this->convertScope($_attr['scope']) : 0;
// assume caching is off
$_caching = Smarty::CACHING_OFF;
// caching was on and {include} is not in nocache mode
if ($compiler->getTemplate()->caching && !$compiler->isNocacheActive()) {
$_caching = \Smarty\Template::CACHING_NOCACHE_CODE;
}
/*
* if the {include} tag provides individual parameter for caching or compile_id
* the subtemplate must not be included into the common cache file and is treated like
* a call in nocache mode.
*
*/
$call_nocache = $compiler->isNocacheActive();
if ($_attr['nocache'] !== true && $_attr['caching']) {
$_caching = $_new_caching = (int)$_attr['caching'];
$call_nocache = true;
} else {
$_new_caching = Smarty::CACHING_LIFETIME_CURRENT;
}
if (isset($_attr['cache_lifetime'])) {
$_cache_lifetime = $_attr['cache_lifetime'];
$call_nocache = true;
$_caching = $_new_caching;
} else {
$_cache_lifetime = '$_smarty_tpl->cache_lifetime';
}
if (isset($_attr['cache_id'])) {
$_cache_id = $_attr['cache_id'];
$call_nocache = true;
$_caching = $_new_caching;
} else {
$_cache_id = '$_smarty_tpl->cache_id';
}
// assign attribute
if (isset($_attr['assign'])) {
// output will be stored in a smarty variable instead of being displayed
if ($_assign = $compiler->getId($_attr['assign'])) {
$_assign = "'{$_assign}'";
if ($call_nocache) {
// create nocache var to make it know for further compiling
$compiler->setNocacheInVariable($_attr['assign']);
}
} else {
$_assign = $_attr['assign'];
}
}
$has_compiled_template = false;
// delete {include} standard attributes
unset($_attr['file'], $_attr['assign'], $_attr['cache_id'], $_attr['cache_lifetime'], $_attr['nocache'], $_attr['caching'], $_attr['scope'], $_attr['inline']);
// remaining attributes must be assigned as smarty variable
$_vars = 'array()';
if (!empty($_attr)) {
$_pairs = [];
// create variables
foreach ($_attr as $key => $value) {
$_pairs[] = "'$key'=>$value";
}
$_vars = 'array(' . join(',', $_pairs) . ')';
}
if ($call_nocache) {
$compiler->tag_nocache = true;
}
$_output = "<?php ";
// was there an assign attribute
if (isset($_assign)) {
$_output .= "ob_start();\n";
}
$_output .= "\$_smarty_tpl->renderSubTemplate({$fullResourceName}, $_cache_id, \$_smarty_tpl->compile_id, " .
"$_caching, $_cache_lifetime, $_vars, (int) {$_scope}, \$_smarty_current_dir);\n";
if (isset($_assign)) {
$_output .= "\$_smarty_tpl->assign({$_assign}, ob_get_clean(), false, {$_scope});\n";
}
$_output .= "?>";
return $_output;
}
}

View file

@ -0,0 +1,54 @@
<?php
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile Shared Inheritance
* Shared methods for {extends} and {block} tags
*
* @author Uwe Tews
*/
/**
* Smarty Internal Plugin Compile Shared Inheritance Class
*
*/
abstract class Inheritance extends Base
{
/**
* Compile inheritance initialization code as prefix
*
* @param \Smarty\Compiler\Template $compiler
* @param bool|false $initChildSequence if true force child template
*/
public static function postCompile(\Smarty\Compiler\Template $compiler, $initChildSequence = false)
{
$compiler->prefixCompiledCode .= "<?php \$_smarty_tpl->getInheritance()->init(\$_smarty_tpl, " .
var_export($initChildSequence, true) . ");\n?>\n";
}
/**
* Register post compile callback to compile inheritance initialization code
*
* @param \Smarty\Compiler\Template $compiler
* @param bool|false $initChildSequence if true force child template
*/
public function registerInit(\Smarty\Compiler\Template $compiler, $initChildSequence = false)
{
if ($initChildSequence || !isset($compiler->_cache[ 'inheritanceInit' ])) {
$compiler->registerPostCompileCallback(
array(self::class, 'postCompile'),
array($initChildSequence),
'inheritanceInit',
$initChildSequence
);
$compiler->_cache[ 'inheritanceInit' ] = true;
}
}
}

View file

@ -0,0 +1,41 @@
<?php
/**
* Smarty Internal Plugin Compile Ldelim
* Compiles the {ldelim} tag
*
* @author Uwe Tews
*/
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile Ldelim Class
*
*/
class Ldelim extends Base {
/**
* Compiles code for the {ldelim} tag
* This tag does output the left delimiter
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
*
* @return string compiled code
* @throws \Smarty\CompilerException
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
$_attr = $this->getAttributes($compiler, $args);
if ($_attr['nocache'] === true) {
$compiler->trigger_template_error('nocache option not allowed', null, true);
}
return $compiler->getTemplate()->getLeftDelimiter();
}
}

View file

@ -0,0 +1,36 @@
<?php
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile Nocache Class
*
*/
class Nocache extends Base {
/**
* Array of names of valid option flags
*
* @var array
*/
protected $option_flags = [];
/**
* Compiles code for the {nocache} tag
* This tag does not generate compiled output. It only sets a compiler flag.
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
*
* @return string
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
$this->openTag($compiler, 'nocache');
return '';
}
}

View file

@ -0,0 +1,37 @@
<?php
/**
* Smarty Internal Plugin Compile Nocache
* Compiles the {nocache} {/nocache} tags.
*
* @author Uwe Tews
*/
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile Nocacheclose Class
*
*/
class NocacheClose extends Base {
/**
* Compiles code for the {/nocache} tag
* This tag does not generate compiled output. It only sets a compiler flag.
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
*
* @return string
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
$this->closeTag($compiler, ['nocache']);
return '';
}
}

View file

@ -0,0 +1,36 @@
<?php
/**
* Smarty Internal Plugin Compile Rdelim
* Compiles the {rdelim} tag
*
* @author Uwe Tews
*/
namespace Smarty\Compile\Tag;
/**
* Smarty Internal Plugin Compile Rdelim Class
*
*/
class Rdelim extends Ldelim {
/**
* Compiles code for the {rdelim} tag
* This tag does output the right delimiter.
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
*
* @return string compiled code
* @throws \Smarty\CompilerException
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
parent::compile($args, $compiler);
return $compiler->getTemplate()->getRightDelimiter();
}
}

View file

@ -0,0 +1,399 @@
<?php
namespace Smarty\Compile\Tag;
/**
* Smarty Internal Plugin Compile Section Class
*
*/
class Section extends ForeachSection {
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BasePlugin
*/
protected $required_attributes = ['name', 'loop'];
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BasePlugin
*/
protected $shorttag_order = ['name', 'loop'];
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BasePlugin
*/
protected $optional_attributes = ['start', 'step', 'max', 'show', 'properties'];
/**
* counter
*
* @var int
*/
protected $counter = 0;
/**
* Name of this tag
*
* @var string
*/
protected $tagName = 'section';
/**
* Valid properties of $smarty.section.name.xxx variable
*
* @var array
*/
protected $nameProperties = [
'first', 'last', 'index', 'iteration', 'show', 'total', 'rownum', 'index_prev',
'index_next', 'loop',
];
/**
* {section} tag has no item properties
*
* @var array
*/
protected $itemProperties = null;
/**
* {section} tag has always name attribute
*
* @var bool
*/
protected $isNamed = true;
/**
* Compiles code for the {section} tag
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
*
* @return string compiled code
* @throws \Smarty\CompilerException
* @throws \Smarty\Exception
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
$compiler->loopNesting++;
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
$attributes = ['name' => $compiler->getId($_attr['name'])];
unset($_attr['name']);
foreach ($attributes as $a => $v) {
if ($v === false) {
$compiler->trigger_template_error("'{$a}' attribute/variable has illegal value", null, true);
}
}
$local = "\$__section_{$attributes['name']}_" . $this->counter++ . '_';
$sectionVar = "\$_smarty_tpl->tpl_vars['__smarty_section_{$attributes['name']}']";
if ($compiler->tag_nocache) {
// push a {nocache} tag onto the stack to prevent caching of this block
$this->openTag($compiler, 'nocache');
}
$this->openTag($compiler, 'section', ['section', $compiler->tag_nocache]);
$initLocal = [];
$initNamedProperty = [];
$initFor = [];
$incFor = [];
$cmpFor = [];
$propValue = [
'index' => "{$sectionVar}->value['index']", 'show' => 'true', 'step' => 1,
'iteration' => "{$local}iteration",
];
$propType = ['index' => 2, 'iteration' => 2, 'show' => 0, 'step' => 0,];
// search for used tag attributes
$this->scanForProperties($attributes, $compiler);
if (!empty($this->matchResults['named'])) {
$namedAttr = $this->matchResults['named'];
}
if (isset($_attr['properties']) && preg_match_all("/['](.*?)[']/", $_attr['properties'], $match)) {
foreach ($match[1] as $prop) {
if (in_array($prop, $this->nameProperties)) {
$namedAttr[$prop] = true;
} else {
$compiler->trigger_template_error("Invalid property '{$prop}'", null, true);
}
}
}
$namedAttr['index'] = true;
$output = "<?php\n";
foreach ($_attr as $attr_name => $attr_value) {
switch ($attr_name) {
case 'loop':
if (is_numeric($attr_value)) {
$v = (int)$attr_value;
$t = 0;
} else {
$v = "(is_array(@\$_loop=$attr_value) ? count(\$_loop) : max(0, (int) \$_loop))";
$t = 1;
}
if ($t === 1) {
$initLocal['loop'] = $v;
$v = "{$local}loop";
}
break;
case 'show':
if (is_bool($attr_value)) {
$v = $attr_value ? 'true' : 'false';
$t = 0;
} else {
$v = "(bool) $attr_value";
$t = 3;
}
break;
case 'step':
if (is_numeric($attr_value)) {
$v = (int)$attr_value;
$v = ($v === 0) ? 1 : $v;
$t = 0;
break;
}
$initLocal['step'] = "((int)@$attr_value) === 0 ? 1 : (int)@$attr_value";
$v = "{$local}step";
$t = 2;
break;
case 'max':
case 'start':
if (is_numeric($attr_value)) {
$v = (int)$attr_value;
$t = 0;
break;
}
$v = "(int)@$attr_value";
$t = 3;
break;
}
if ($t === 3 && $compiler->getId($attr_value)) {
$t = 1;
}
$propValue[$attr_name] = $v;
$propType[$attr_name] = $t;
}
if (isset($namedAttr['step'])) {
$initNamedProperty['step'] = $propValue['step'];
}
if (isset($namedAttr['iteration'])) {
$propValue['iteration'] = "{$sectionVar}->value['iteration']";
}
$incFor['iteration'] = "{$propValue['iteration']}++";
$initFor['iteration'] = "{$propValue['iteration']} = 1";
if ($propType['step'] === 0) {
if ($propValue['step'] === 1) {
$incFor['index'] = "{$sectionVar}->value['index']++";
} elseif ($propValue['step'] > 1) {
$incFor['index'] = "{$sectionVar}->value['index'] += {$propValue['step']}";
} else {
$incFor['index'] = "{$sectionVar}->value['index'] -= " . -$propValue['step'];
}
} else {
$incFor['index'] = "{$sectionVar}->value['index'] += {$propValue['step']}";
}
if (!isset($propValue['max'])) {
$propValue['max'] = $propValue['loop'];
$propType['max'] = $propType['loop'];
} elseif ($propType['max'] !== 0) {
$propValue['max'] = "{$propValue['max']} < 0 ? {$propValue['loop']} : {$propValue['max']}";
$propType['max'] = 1;
} else {
if ($propValue['max'] < 0) {
$propValue['max'] = $propValue['loop'];
$propType['max'] = $propType['loop'];
}
}
if (!isset($propValue['start'])) {
$start_code =
[1 => "{$propValue['step']} > 0 ? ", 2 => '0', 3 => ' : ', 4 => $propValue['loop'], 5 => ' - 1'];
if ($propType['loop'] === 0) {
$start_code[5] = '';
$start_code[4] = $propValue['loop'] - 1;
}
if ($propType['step'] === 0) {
if ($propValue['step'] > 0) {
$start_code = [1 => '0'];
$propType['start'] = 0;
} else {
$start_code[1] = $start_code[2] = $start_code[3] = '';
$propType['start'] = $propType['loop'];
}
} else {
$propType['start'] = 1;
}
$propValue['start'] = join('', $start_code);
} else {
$start_code =
[
1 => "{$propValue['start']} < 0 ? ", 2 => 'max(', 3 => "{$propValue['step']} > 0 ? ", 4 => '0',
5 => ' : ', 6 => '-1', 7 => ', ', 8 => "{$propValue['start']} + {$propValue['loop']}", 10 => ')',
11 => ' : ', 12 => 'min(', 13 => $propValue['start'], 14 => ', ',
15 => "{$propValue['step']} > 0 ? ", 16 => $propValue['loop'], 17 => ' : ',
18 => $propType['loop'] === 0 ? $propValue['loop'] - 1 : "{$propValue['loop']} - 1",
19 => ')',
];
if ($propType['step'] === 0) {
$start_code[3] = $start_code[5] = $start_code[15] = $start_code[17] = '';
if ($propValue['step'] > 0) {
$start_code[6] = $start_code[18] = '';
} else {
$start_code[4] = $start_code[16] = '';
}
}
if ($propType['start'] === 0) {
if ($propType['loop'] === 0) {
$start_code[8] = $propValue['start'] + $propValue['loop'];
}
$propType['start'] = $propType['step'] + $propType['loop'];
$start_code[1] = '';
if ($propValue['start'] < 0) {
for ($i = 11; $i <= 19; $i++) {
$start_code[$i] = '';
}
if ($propType['start'] === 0) {
$start_code = [
max(
$propValue['step'] > 0 ? 0 : -1,
$propValue['start'] + $propValue['loop']
),
];
}
} else {
for ($i = 1; $i <= 11; $i++) {
$start_code[$i] = '';
}
if ($propType['start'] === 0) {
$start_code =
[
min(
$propValue['step'] > 0 ? $propValue['loop'] : $propValue['loop'] - 1,
$propValue['start']
),
];
}
}
}
$propValue['start'] = join('', $start_code);
}
if ($propType['start'] !== 0) {
$initLocal['start'] = $propValue['start'];
$propValue['start'] = "{$local}start";
}
$initFor['index'] = "{$sectionVar}->value['index'] = {$propValue['start']}";
if (!isset($_attr['start']) && !isset($_attr['step']) && !isset($_attr['max'])) {
$propValue['total'] = $propValue['loop'];
$propType['total'] = $propType['loop'];
} else {
$propType['total'] =
$propType['start'] + $propType['loop'] + $propType['step'] + $propType['max'];
if ($propType['total'] === 0) {
$propValue['total'] =
min(
ceil(
($propValue['step'] > 0 ? $propValue['loop'] - $propValue['start'] :
(int)$propValue['start'] + 1) / abs($propValue['step'])
),
$propValue['max']
);
} else {
$total_code = [
1 => 'min(', 2 => 'ceil(', 3 => '(', 4 => "{$propValue['step']} > 0 ? ",
5 => $propValue['loop'], 6 => ' - ', 7 => $propValue['start'], 8 => ' : ',
9 => $propValue['start'], 10 => '+ 1', 11 => ')', 12 => '/ ', 13 => 'abs(',
14 => $propValue['step'], 15 => ')', 16 => ')', 17 => ", {$propValue['max']})",
];
if (!isset($propValue['max'])) {
$total_code[1] = $total_code[17] = '';
}
if ($propType['loop'] + $propType['start'] === 0) {
$total_code[5] = $propValue['loop'] - $propValue['start'];
$total_code[6] = $total_code[7] = '';
}
if ($propType['start'] === 0) {
$total_code[9] = (int)$propValue['start'] + 1;
$total_code[10] = '';
}
if ($propType['step'] === 0) {
$total_code[13] = $total_code[15] = '';
if ($propValue['step'] === 1 || $propValue['step'] === -1) {
$total_code[2] = $total_code[12] = $total_code[14] = $total_code[16] = '';
} elseif ($propValue['step'] < 0) {
$total_code[14] = -$propValue['step'];
}
$total_code[4] = '';
if ($propValue['step'] > 0) {
$total_code[8] = $total_code[9] = $total_code[10] = '';
} else {
$total_code[5] = $total_code[6] = $total_code[7] = $total_code[8] = '';
}
}
$propValue['total'] = join('', $total_code);
}
}
if (isset($namedAttr['loop'])) {
$initNamedProperty['loop'] = "'loop' => {$propValue['loop']}";
}
if (isset($namedAttr['total'])) {
$initNamedProperty['total'] = "'total' => {$propValue['total']}";
if ($propType['total'] > 0) {
$propValue['total'] = "{$sectionVar}->value['total']";
}
} elseif ($propType['total'] > 0) {
$initLocal['total'] = $propValue['total'];
$propValue['total'] = "{$local}total";
}
$cmpFor['iteration'] = "{$propValue['iteration']} <= {$propValue['total']}";
foreach ($initLocal as $key => $code) {
$output .= "{$local}{$key} = {$code};\n";
}
$_vars = 'array(' . join(', ', $initNamedProperty) . ')';
$output .= "{$sectionVar} = new \\Smarty\\Variable({$_vars});\n";
$cond_code = "{$propValue['total']} !== 0";
if ($propType['total'] === 0) {
if ($propValue['total'] === 0) {
$cond_code = 'false';
} else {
$cond_code = 'true';
}
}
if ($propType['show'] > 0) {
$output .= "{$local}show = {$propValue['show']} ? {$cond_code} : false;\n";
$output .= "if ({$local}show) {\n";
} elseif ($propValue['show'] === 'true') {
$output .= "if ({$cond_code}) {\n";
} else {
$output .= "if (false) {\n";
}
$jinit = join(', ', $initFor);
$jcmp = join(', ', $cmpFor);
$jinc = join(', ', $incFor);
$output .= "for ({$jinit}; {$jcmp}; {$jinc}){\n";
if (isset($namedAttr['rownum'])) {
$output .= "{$sectionVar}->value['rownum'] = {$propValue['iteration']};\n";
}
if (isset($namedAttr['index_prev'])) {
$output .= "{$sectionVar}->value['index_prev'] = {$propValue['index']} - {$propValue['step']};\n";
}
if (isset($namedAttr['index_next'])) {
$output .= "{$sectionVar}->value['index_next'] = {$propValue['index']} + {$propValue['step']};\n";
}
if (isset($namedAttr['first'])) {
$output .= "{$sectionVar}->value['first'] = ({$propValue['iteration']} === 1);\n";
}
if (isset($namedAttr['last'])) {
$output .= "{$sectionVar}->value['last'] = ({$propValue['iteration']} === {$propValue['total']});\n";
}
$output .= '?>';
return $output;
}
}

View file

@ -0,0 +1,48 @@
<?php
/**
* Smarty Internal Plugin Compile Section
* Compiles the {section} {sectionelse} {/section} tags
*
* @author Uwe Tews
*/
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile Sectionclose Class
*/
class SectionClose extends Base {
/**
* Compiles code for the {/section} tag
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
*
* @return string compiled code
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
$compiler->loopNesting--;
[$openTag, $nocache_pushed] = $this->closeTag($compiler, ['section', 'sectionelse']);
if ($nocache_pushed) {
// pop the pushed virtual nocache tag
$this->closeTag($compiler, 'nocache');
}
$output = "<?php\n";
if ($openTag === 'sectionelse') {
$output .= "}\n";
} else {
$output .= "}\n}\n";
}
$output .= '?>';
return $output;
}
}

View file

@ -0,0 +1,29 @@
<?php
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile Sectionelse Class
*
*/
class SectionElse extends Base {
/**
* Compiles code for the {sectionelse} tag
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
*
* @return string compiled code
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
[$openTag, $nocache_pushed] = $this->closeTag($compiler, ['section']);
$this->openTag($compiler, 'sectionelse', ['sectionelse', $nocache_pushed]);
return "<?php }} else {\n ?>";
}
}

View file

@ -0,0 +1,40 @@
<?php
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile Setfilter Class
*
*/
class Setfilter extends Base {
/**
* Compiles code for setfilter tag
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
* @param array $parameter array with compilation parameter
*
* @return string compiled code
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
$compiler->variable_filter_stack[] = $compiler->getSmarty()->getDefaultModifiers();
// The modifier_list is passed as an array of array's. The inner arrays have the modifier at index 0,
// and, possibly, parameters at subsequent indexes, e.g. [ ['escape','"mail"'] ]
// We will collapse them so the syntax is OK for ::setDefaultModifiers() as follows: [ 'escape:"mail"' ]
$newList = [];
foreach($parameter['modifier_list'] as $modifier) {
$newList[] = implode(':', $modifier);
}
$compiler->getSmarty()->setDefaultModifiers($newList);
return '';
}
}

View file

@ -0,0 +1,43 @@
<?php
/**
* Smarty Internal Plugin Compile Setfilter
* Compiles code for setfilter tag
*
* @author Uwe Tews
*/
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile Setfilterclose Class
*
*/
class SetfilterClose extends Base {
/**
* Compiles code for the {/setfilter} tag
* This tag does not generate compiled output. It resets variable filter.
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
*
* @return string compiled code
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
$this->getAttributes($compiler, $args);
// reset variable filter to previous state
$compiler->getSmarty()->setDefaultModifiers(
count($compiler->variable_filter_stack) ? array_pop($compiler->variable_filter_stack) : []
);
return '';
}
}

View file

@ -0,0 +1,45 @@
<?php
/**
* Smarty Internal Plugin Compile While
* Compiles the {while} tag
*
* @author Uwe Tews
*/
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile Whileclose Class
*
*/
class WhileClose extends Base {
/**
* Compiles code for the {/while} tag
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
*
* @return string compiled code
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
$compiler->loopNesting--;
$nocache_pushed = $this->closeTag($compiler, ['while']);
if ($nocache_pushed) {
// pop the pushed virtual nocache tag
$this->closeTag($compiler, 'nocache');
$compiler->tag_nocache = true;
}
return "<?php }?>\n";
}
}

View file

@ -0,0 +1,72 @@
<?php
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile While Class
*
*/
class WhileTag extends Base {
/**
* Compiles code for the {while} tag
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
* @param array $parameter array with compilation parameter
*
* @return string compiled code
* @throws \Smarty\CompilerException
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
$compiler->loopNesting++;
if ($compiler->tag_nocache) {
// push a {nocache} tag onto the stack to prevent caching of this block
$this->openTag($compiler, 'nocache');
}
$this->openTag($compiler, 'while', $compiler->tag_nocache);
if (!array_key_exists('if condition', $parameter)) {
$compiler->trigger_template_error('missing while condition', null, true);
}
if (is_array($parameter['if condition'])) {
if ($compiler->isNocacheActive()) {
// create nocache var to make it know for further compiling
if (is_array($parameter['if condition']['var'])) {
$var = $parameter['if condition']['var']['var'];
} else {
$var = $parameter['if condition']['var'];
}
$compiler->setNocacheInVariable($var);
}
$prefixVar = $compiler->getNewPrefixVariable();
$assignCompiler = new Assign();
$assignAttr = [];
$assignAttr[]['value'] = $prefixVar;
if (is_array($parameter['if condition']['var'])) {
$assignAttr[]['var'] = $parameter['if condition']['var']['var'];
$_output = "<?php while ({$prefixVar} = {$parameter[ 'if condition' ][ 'value' ]}) {?>";
$_output .= $assignCompiler->compile(
$assignAttr,
$compiler,
['smarty_internal_index' => $parameter['if condition']['var']['smarty_internal_index']]
);
} else {
$assignAttr[]['var'] = $parameter['if condition']['var'];
$_output = "<?php while ({$prefixVar} = {$parameter[ 'if condition' ][ 'value' ]}) {?>";
$_output .= $assignCompiler->compile($assignAttr, $compiler, []);
}
return $_output;
} else {
return "<?php\n while ({$parameter['if condition']}) {?>";
}
}
}