Initial checkin
This commit is contained in:
commit
d75eb444fc
4304 changed files with 369634 additions and 0 deletions
19
core/template/src/BlockHandler/Base.php
Normal file
19
core/template/src/BlockHandler/Base.php
Normal file
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
|
||||
namespace Smarty\BlockHandler;
|
||||
|
||||
use Smarty\Template;
|
||||
|
||||
abstract class Base implements BlockHandlerInterface {
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $cacheable = true;
|
||||
|
||||
abstract public function handle($params, $content, Template $template, &$repeat);
|
||||
|
||||
public function isCacheable(): bool {
|
||||
return $this->cacheable;
|
||||
}
|
||||
}
|
10
core/template/src/BlockHandler/BlockHandlerInterface.php
Normal file
10
core/template/src/BlockHandler/BlockHandlerInterface.php
Normal file
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
|
||||
namespace Smarty\BlockHandler;
|
||||
|
||||
use Smarty\Template;
|
||||
|
||||
interface BlockHandlerInterface {
|
||||
public function handle($params, $content, Template $template, &$repeat);
|
||||
public function isCacheable(): bool;
|
||||
}
|
19
core/template/src/BlockHandler/BlockPluginWrapper.php
Normal file
19
core/template/src/BlockHandler/BlockPluginWrapper.php
Normal file
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
|
||||
namespace Smarty\BlockHandler;
|
||||
|
||||
use Smarty\Template;
|
||||
|
||||
class BlockPluginWrapper extends Base {
|
||||
|
||||
private $callback;
|
||||
|
||||
public function __construct($callback, bool $cacheable = true) {
|
||||
$this->callback = $callback;
|
||||
$this->cacheable = $cacheable;
|
||||
}
|
||||
|
||||
public function handle($params, $content, Template $template, &$repeat) {
|
||||
return \call_user_func_array($this->callback, [$params, $content, &$template, &$repeat]);
|
||||
}
|
||||
}
|
110
core/template/src/BlockHandler/TextFormat.php
Normal file
110
core/template/src/BlockHandler/TextFormat.php
Normal file
|
@ -0,0 +1,110 @@
|
|||
<?php
|
||||
|
||||
namespace Smarty\BlockHandler;
|
||||
|
||||
use Smarty\Smarty;
|
||||
use Smarty\Template;
|
||||
|
||||
/**
|
||||
* Smarty {textformat}{/textformat} block plugin
|
||||
* Type: block function
|
||||
* Name: textformat
|
||||
* Purpose: format text a certain way with preset styles
|
||||
* or custom wrap/indent settings
|
||||
* Params:
|
||||
*
|
||||
* - style - string (email)
|
||||
* - indent - integer (0)
|
||||
* - wrap - integer (80)
|
||||
* - wrap_char - string ("\n")
|
||||
* - indent_char - string (" ")
|
||||
* - wrap_boundary - boolean (true)
|
||||
*
|
||||
* @param array $params parameters
|
||||
* @param string $content contents of the block
|
||||
* @param Template $template template object
|
||||
* @param boolean &$repeat repeat flag
|
||||
*
|
||||
* @return string content re-formatted
|
||||
* @author Monte Ohrt <monte at ohrt dot com>
|
||||
* @throws \Smarty\Exception
|
||||
*/
|
||||
class TextFormat implements BlockHandlerInterface {
|
||||
|
||||
public function handle($params, $content, Template $template, &$repeat) {
|
||||
if (is_null($content)) {
|
||||
return;
|
||||
}
|
||||
$style = null;
|
||||
$indent = 0;
|
||||
$indent_first = 0;
|
||||
$indent_char = ' ';
|
||||
$wrap = 80;
|
||||
$wrap_char = "\n";
|
||||
$wrap_cut = false;
|
||||
$assign = null;
|
||||
foreach ($params as $_key => $_val) {
|
||||
switch ($_key) {
|
||||
case 'style':
|
||||
case 'indent_char':
|
||||
case 'wrap_char':
|
||||
case 'assign':
|
||||
$$_key = (string)$_val;
|
||||
break;
|
||||
case 'indent':
|
||||
case 'indent_first':
|
||||
case 'wrap':
|
||||
$$_key = (int)$_val;
|
||||
break;
|
||||
case 'wrap_cut':
|
||||
$$_key = (bool)$_val;
|
||||
break;
|
||||
default:
|
||||
trigger_error("textformat: unknown attribute '{$_key}'");
|
||||
}
|
||||
}
|
||||
if ($style === 'email') {
|
||||
$wrap = 72;
|
||||
}
|
||||
// split into paragraphs
|
||||
$_paragraphs = preg_split('![\r\n]{2}!', $content);
|
||||
foreach ($_paragraphs as &$_paragraph) {
|
||||
if (!$_paragraph) {
|
||||
continue;
|
||||
}
|
||||
// convert mult. spaces & special chars to single space
|
||||
$_paragraph =
|
||||
preg_replace(
|
||||
array(
|
||||
'!\s+!' . Smarty::$_UTF8_MODIFIER,
|
||||
'!(^\s+)|(\s+$)!' . Smarty::$_UTF8_MODIFIER
|
||||
),
|
||||
array(
|
||||
' ',
|
||||
''
|
||||
),
|
||||
$_paragraph
|
||||
);
|
||||
// indent first line
|
||||
if ($indent_first > 0) {
|
||||
$_paragraph = str_repeat($indent_char, $indent_first) . $_paragraph;
|
||||
}
|
||||
// wordwrap sentences
|
||||
$_paragraph = smarty_mb_wordwrap($_paragraph, $wrap - $indent, $wrap_char, $wrap_cut);
|
||||
// indent lines
|
||||
if ($indent > 0) {
|
||||
$_paragraph = preg_replace('!^!m', str_repeat($indent_char, $indent), $_paragraph);
|
||||
}
|
||||
}
|
||||
$_output = implode($wrap_char . $wrap_char, $_paragraphs);
|
||||
if ($assign) {
|
||||
$template->assign($assign, $_output);
|
||||
} else {
|
||||
return $_output;
|
||||
}
|
||||
}
|
||||
|
||||
public function isCacheable(): bool {
|
||||
return true;
|
||||
}
|
||||
}
|
156
core/template/src/Cacheresource/Base.php
Normal file
156
core/template/src/Cacheresource/Base.php
Normal file
|
@ -0,0 +1,156 @@
|
|||
<?php
|
||||
|
||||
namespace Smarty\Cacheresource;
|
||||
|
||||
use Smarty\Exception;
|
||||
use Smarty\Smarty;
|
||||
use Smarty\Template;
|
||||
use Smarty\Template\Cached;
|
||||
|
||||
/**
|
||||
* Cache Handler API
|
||||
* @author Rodney Rehm
|
||||
*/
|
||||
abstract class Base
|
||||
{
|
||||
|
||||
/**
|
||||
* populate Cached Object with metadata from Resource
|
||||
*
|
||||
* @param Cached $cached cached object
|
||||
* @param Template $_template template object
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
abstract public function populate(Cached $cached, Template $_template);
|
||||
|
||||
/**
|
||||
* populate Cached Object with timestamp and exists from Resource
|
||||
*
|
||||
* @param Cached $cached
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
abstract public function populateTimestamp(Cached $cached);
|
||||
|
||||
/**
|
||||
* Read the cached template and process header
|
||||
*
|
||||
* @param Template $_template template object
|
||||
* @param Cached|null $cached cached object
|
||||
* @param boolean $update flag if called because cache update
|
||||
*
|
||||
* @return boolean true or false if the cached content does not exist
|
||||
*/
|
||||
abstract public function process(
|
||||
Template $_template,
|
||||
?Cached $cached = null,
|
||||
$update = false
|
||||
);
|
||||
|
||||
/**
|
||||
* Write the rendered template output to cache
|
||||
*
|
||||
* @param Template $_template template object
|
||||
* @param string $content content to cache
|
||||
*
|
||||
* @return boolean success
|
||||
*/
|
||||
abstract public function storeCachedContent(Template $_template, $content);
|
||||
|
||||
/**
|
||||
* Read cached template from cache
|
||||
*
|
||||
* @param Template $_template template object
|
||||
*
|
||||
* @return string content
|
||||
*/
|
||||
abstract public function retrieveCachedContent(Template $_template);
|
||||
|
||||
/**
|
||||
* Empty cache
|
||||
*
|
||||
* @param Smarty $smarty Smarty object
|
||||
* @param integer $exp_time expiration time (number of seconds, not timestamp)
|
||||
*
|
||||
* @return integer number of cache files deleted
|
||||
*/
|
||||
abstract public function clearAll(Smarty $smarty, $exp_time = null);
|
||||
|
||||
/**
|
||||
* Empty cache for a specific template
|
||||
*
|
||||
* @param Smarty $smarty Smarty object
|
||||
* @param string $resource_name template name
|
||||
* @param string $cache_id cache id
|
||||
* @param string $compile_id compile id
|
||||
* @param integer $exp_time expiration time (number of seconds, not timestamp)
|
||||
*
|
||||
* @return integer number of cache files deleted
|
||||
*/
|
||||
abstract public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time);
|
||||
|
||||
/**
|
||||
* @param Smarty $smarty
|
||||
* @param Cached $cached
|
||||
*
|
||||
* @return bool|null
|
||||
*/
|
||||
public function locked(Smarty $smarty, Cached $cached)
|
||||
{
|
||||
// theoretically locking_timeout should be checked against time_limit (max_execution_time)
|
||||
$start = microtime(true);
|
||||
$hadLock = null;
|
||||
while ($this->hasLock($smarty, $cached)) {
|
||||
$hadLock = true;
|
||||
if (microtime(true) - $start > $smarty->locking_timeout) {
|
||||
// abort waiting for lock release
|
||||
return false;
|
||||
}
|
||||
sleep(1);
|
||||
}
|
||||
return $hadLock;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check is cache is locked for this template
|
||||
*
|
||||
* @param Smarty $smarty
|
||||
* @param Cached $cached
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasLock(Smarty $smarty, Cached $cached)
|
||||
{
|
||||
// check if lock exists
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Lock cache for this template
|
||||
*
|
||||
* @param Smarty $smarty
|
||||
* @param Cached $cached
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function acquireLock(Smarty $smarty, Cached $cached)
|
||||
{
|
||||
// create lock
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unlock cache for this template
|
||||
*
|
||||
* @param Smarty $smarty
|
||||
* @param Cached $cached
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function releaseLock(Smarty $smarty, Cached $cached)
|
||||
{
|
||||
// release lock
|
||||
return true;
|
||||
}
|
||||
}
|
303
core/template/src/Cacheresource/Custom.php
Normal file
303
core/template/src/Cacheresource/Custom.php
Normal file
|
@ -0,0 +1,303 @@
|
|||
<?php
|
||||
|
||||
namespace Smarty\Cacheresource;
|
||||
|
||||
/**
|
||||
* Smarty Internal Plugin
|
||||
*
|
||||
|
||||
|
||||
*/
|
||||
|
||||
use Smarty\Smarty;
|
||||
use Smarty\Template;
|
||||
use Smarty\Template\Cached;
|
||||
|
||||
/**
|
||||
* Cache Handler API
|
||||
*
|
||||
|
||||
|
||||
* @author Rodney Rehm
|
||||
*/
|
||||
abstract class Custom extends Base
|
||||
{
|
||||
/**
|
||||
* fetch cached content and its modification time from data source
|
||||
*
|
||||
* @param string $id unique cache content identifier
|
||||
* @param string $name template name
|
||||
* @param string $cache_id cache id
|
||||
* @param string $compile_id compile id
|
||||
* @param string $content cached content
|
||||
* @param integer $mtime cache modification timestamp (epoch)
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
abstract protected function fetch($id, $name, $cache_id, $compile_id, &$content, &$mtime);
|
||||
|
||||
/**
|
||||
* Fetch cached content's modification timestamp from data source
|
||||
* {@internal implementing this method is optional.
|
||||
* Only implement it if modification times can be accessed faster than loading the complete cached content.}}
|
||||
*
|
||||
* @param string $id unique cache content identifier
|
||||
* @param string $name template name
|
||||
* @param string $cache_id cache id
|
||||
* @param string $compile_id compile id
|
||||
*
|
||||
* @return integer|boolean timestamp (epoch) the template was modified, or false if not found
|
||||
*/
|
||||
protected function fetchTimestamp($id, $name, $cache_id, $compile_id)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save content to cache
|
||||
*
|
||||
* @param string $id unique cache content identifier
|
||||
* @param string $name template name
|
||||
* @param string $cache_id cache id
|
||||
* @param string $compile_id compile id
|
||||
* @param integer|null $exp_time seconds till expiration or null
|
||||
* @param string $content content to cache
|
||||
*
|
||||
* @return boolean success
|
||||
*/
|
||||
abstract protected function save($id, $name, $cache_id, $compile_id, $exp_time, $content);
|
||||
|
||||
/**
|
||||
* Delete content from cache
|
||||
*
|
||||
* @param string|null $name template name
|
||||
* @param string|null $cache_id cache id
|
||||
* @param string|null $compile_id compile id
|
||||
* @param integer|null $exp_time seconds till expiration time in seconds or null
|
||||
*
|
||||
* @return integer number of deleted caches
|
||||
*/
|
||||
abstract protected function delete($name, $cache_id, $compile_id, $exp_time);
|
||||
|
||||
/**
|
||||
* populate Cached Object with metadata from Resource
|
||||
*
|
||||
* @param \Smarty\Template\Cached $cached cached object
|
||||
* @param Template $_template template object
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function populate(\Smarty\Template\Cached $cached, Template $_template)
|
||||
{
|
||||
$_cache_id = isset($cached->cache_id) ? preg_replace('![^\w\|]+!', '_', $cached->cache_id) : null;
|
||||
$_compile_id = isset($cached->compile_id) ? preg_replace('![^\w]+!', '_', $cached->compile_id) : null;
|
||||
$path = $cached->getSource()->uid . $_cache_id . $_compile_id;
|
||||
$cached->filepath = sha1($path);
|
||||
if ($_template->getSmarty()->cache_locking) {
|
||||
$cached->lock_id = sha1('lock.' . $path);
|
||||
}
|
||||
$this->populateTimestamp($cached);
|
||||
}
|
||||
|
||||
/**
|
||||
* populate Cached Object with timestamp and exists from Resource
|
||||
*
|
||||
* @param \Smarty\Template\Cached $cached
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function populateTimestamp(\Smarty\Template\Cached $cached)
|
||||
{
|
||||
$mtime =
|
||||
$this->fetchTimestamp($cached->filepath, $cached->getSource()->name, $cached->cache_id, $cached->compile_id);
|
||||
if ($mtime !== null) {
|
||||
$cached->timestamp = $mtime;
|
||||
$cached->exists = !!$cached->timestamp;
|
||||
return;
|
||||
}
|
||||
$timestamp = null;
|
||||
$this->fetch(
|
||||
$cached->filepath,
|
||||
$cached->getSource()->name,
|
||||
$cached->cache_id,
|
||||
$cached->compile_id,
|
||||
$cached->content,
|
||||
$timestamp
|
||||
);
|
||||
$cached->timestamp = isset($timestamp) ? $timestamp : false;
|
||||
$cached->exists = !!$cached->timestamp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the cached template and process the header
|
||||
*
|
||||
* @param Template $_smarty_tpl do not change variable name, is used by compiled template
|
||||
* @param Cached|null $cached cached object
|
||||
* @param boolean $update flag if called because cache update
|
||||
*
|
||||
* @return boolean true or false if the cached content does not exist
|
||||
*/
|
||||
public function process(
|
||||
Template $_smarty_tpl,
|
||||
?\Smarty\Template\Cached $cached = null,
|
||||
$update = false
|
||||
) {
|
||||
if (!$cached) {
|
||||
$cached = $_smarty_tpl->getCached();
|
||||
}
|
||||
$content = $cached->content ? $cached->content : null;
|
||||
$timestamp = $cached->timestamp ? $cached->timestamp : null;
|
||||
if ($content === null || !$timestamp) {
|
||||
$this->fetch(
|
||||
$_smarty_tpl->getCached()->filepath,
|
||||
$_smarty_tpl->getSource()->name,
|
||||
$_smarty_tpl->cache_id,
|
||||
$_smarty_tpl->compile_id,
|
||||
$content,
|
||||
$timestamp
|
||||
);
|
||||
}
|
||||
if (isset($content)) {
|
||||
eval('?>' . $content);
|
||||
$cached->content = null;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write the rendered template output to cache
|
||||
*
|
||||
* @param Template $_template template object
|
||||
* @param string $content content to cache
|
||||
*
|
||||
* @return boolean success
|
||||
*/
|
||||
public function storeCachedContent(Template $_template, $content)
|
||||
{
|
||||
return $this->save(
|
||||
$_template->getCached()->filepath,
|
||||
$_template->getSource()->name,
|
||||
$_template->cache_id,
|
||||
$_template->compile_id,
|
||||
$_template->cache_lifetime,
|
||||
$content
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read cached template from cache
|
||||
*
|
||||
* @param Template $_template template object
|
||||
*
|
||||
* @return string|boolean content
|
||||
*/
|
||||
public function retrieveCachedContent(Template $_template)
|
||||
{
|
||||
$content = $_template->getCached()->content ?: null;
|
||||
if ($content === null) {
|
||||
$timestamp = null;
|
||||
$this->fetch(
|
||||
$_template->getCached()->filepath,
|
||||
$_template->getSource()->name,
|
||||
$_template->cache_id,
|
||||
$_template->compile_id,
|
||||
$content,
|
||||
$timestamp
|
||||
);
|
||||
}
|
||||
if (isset($content)) {
|
||||
return $content;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Empty cache
|
||||
*
|
||||
* @param \Smarty\Smarty $smarty Smarty object
|
||||
* @param null $exp_time expiration time (number of seconds, not timestamp)
|
||||
*
|
||||
* @return integer number of cache files deleted
|
||||
*/
|
||||
public function clearAll(\Smarty\Smarty $smarty, $exp_time = null)
|
||||
{
|
||||
return $this->delete(null, null, null, $exp_time);
|
||||
}
|
||||
|
||||
/**
|
||||
* Empty cache for a specific template
|
||||
*
|
||||
* @param \Smarty\Smarty $smarty Smarty object
|
||||
* @param string $resource_name template name
|
||||
* @param string $cache_id cache id
|
||||
* @param string $compile_id compile id
|
||||
* @param integer $exp_time expiration time (number of seconds, not timestamp)
|
||||
*
|
||||
* @return int number of cache files deleted
|
||||
* @throws \Smarty\Exception
|
||||
*/
|
||||
public function clear(\Smarty\Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time)
|
||||
{
|
||||
$cache_name = null;
|
||||
if (isset($resource_name)) {
|
||||
$source = \Smarty\Template\Source::load(null, $smarty, $resource_name);
|
||||
if ($source->exists) {
|
||||
$cache_name = $source->name;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return $this->delete($cache_name, $cache_id, $compile_id, $exp_time);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check is cache is locked for this template
|
||||
*
|
||||
* @param Smarty $smarty Smarty object
|
||||
* @param Cached $cached cached object
|
||||
*
|
||||
* @return boolean true or false if cache is locked
|
||||
*/
|
||||
public function hasLock(\Smarty\Smarty $smarty, \Smarty\Template\Cached $cached)
|
||||
{
|
||||
$id = $cached->lock_id;
|
||||
$name = $cached->getSource()->name . '.lock';
|
||||
$mtime = $this->fetchTimestamp($id, $name, $cached->cache_id, $cached->compile_id);
|
||||
if ($mtime === null) {
|
||||
$this->fetch($id, $name, $cached->cache_id, $cached->compile_id, $content, $mtime);
|
||||
}
|
||||
return $mtime && ($t = time()) - $mtime < $smarty->locking_timeout;
|
||||
}
|
||||
|
||||
/**
|
||||
* Lock cache for this template
|
||||
*
|
||||
* @param \Smarty\Smarty $smarty Smarty object
|
||||
* @param \Smarty\Template\Cached $cached cached object
|
||||
*
|
||||
* @return bool|void
|
||||
*/
|
||||
public function acquireLock(\Smarty\Smarty $smarty, \Smarty\Template\Cached $cached)
|
||||
{
|
||||
$cached->is_locked = true;
|
||||
$id = $cached->lock_id;
|
||||
$name = $cached->getSource()->name . '.lock';
|
||||
$this->save($id, $name, $cached->cache_id, $cached->compile_id, $smarty->locking_timeout, '');
|
||||
}
|
||||
|
||||
/**
|
||||
* Unlock cache for this template
|
||||
*
|
||||
* @param \Smarty\Smarty $smarty Smarty object
|
||||
* @param \Smarty\Template\Cached $cached cached object
|
||||
*
|
||||
* @return bool|void
|
||||
*/
|
||||
public function releaseLock(\Smarty\Smarty $smarty, \Smarty\Template\Cached $cached)
|
||||
{
|
||||
$cached->is_locked = false;
|
||||
$name = $cached->getSource()->name . '.lock';
|
||||
$this->delete($name, $cached->cache_id, $cached->compile_id, null);
|
||||
}
|
||||
}
|
338
core/template/src/Cacheresource/File.php
Normal file
338
core/template/src/Cacheresource/File.php
Normal file
|
@ -0,0 +1,338 @@
|
|||
<?php
|
||||
|
||||
namespace Smarty\Cacheresource;
|
||||
|
||||
use RecursiveDirectoryIterator;
|
||||
use RecursiveIteratorIterator;
|
||||
use Smarty\Smarty;
|
||||
use Smarty\Template;
|
||||
use Smarty\Template\Cached;
|
||||
|
||||
/**
|
||||
* Smarty Internal Plugin CacheResource File
|
||||
*
|
||||
|
||||
|
||||
* @author Uwe Tews
|
||||
* @author Rodney Rehm
|
||||
*/
|
||||
|
||||
/**
|
||||
* This class does contain all necessary methods for the HTML cache on file system
|
||||
* Implements the file system as resource for the HTML cache Version using nocache inserts.
|
||||
*/
|
||||
class File extends Base
|
||||
{
|
||||
/**
|
||||
* populate Cached Object with metadata from Resource
|
||||
*
|
||||
* @param Cached $cached cached object
|
||||
* @param Template $_template template object
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function populate(Cached $cached, Template $_template)
|
||||
{
|
||||
$source = $_template->getSource();
|
||||
$smarty = $_template->getSmarty();
|
||||
$_compile_dir_sep = $smarty->use_sub_dirs ? DIRECTORY_SEPARATOR : '^';
|
||||
$_filepath = $source->uid;
|
||||
$cached->filepath = $smarty->getCacheDir();
|
||||
if (isset($_template->cache_id)) {
|
||||
$cached->filepath .= preg_replace(
|
||||
array(
|
||||
'![^\w|]+!',
|
||||
'![|]+!'
|
||||
),
|
||||
array(
|
||||
'_',
|
||||
$_compile_dir_sep
|
||||
),
|
||||
$_template->cache_id
|
||||
) . $_compile_dir_sep;
|
||||
}
|
||||
if (isset($_template->compile_id)) {
|
||||
$cached->filepath .= preg_replace('![^\w]+!', '_', $_template->compile_id) . $_compile_dir_sep;
|
||||
}
|
||||
// if use_sub_dirs, break file into directories
|
||||
if ($smarty->use_sub_dirs) {
|
||||
$cached->filepath .= $_filepath[ 0 ] . $_filepath[ 1 ] . DIRECTORY_SEPARATOR . $_filepath[ 2 ] .
|
||||
$_filepath[ 3 ] .
|
||||
DIRECTORY_SEPARATOR .
|
||||
$_filepath[ 4 ] . $_filepath[ 5 ] . DIRECTORY_SEPARATOR;
|
||||
}
|
||||
$cached->filepath .= $_filepath . '_' . $source->getBasename();
|
||||
|
||||
if ($smarty->cache_locking) {
|
||||
$cached->lock_id = $cached->filepath . '.lock';
|
||||
}
|
||||
$cached->filepath .= '.php';
|
||||
$cached->timestamp = $cached->exists = is_file($cached->filepath);
|
||||
if ($cached->exists) {
|
||||
$cached->timestamp = filemtime($cached->filepath);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* populate Cached Object with timestamp and exists from Resource
|
||||
*
|
||||
* @param Cached $cached cached object
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function populateTimestamp(Cached $cached)
|
||||
{
|
||||
$cached->timestamp = $cached->exists = is_file($cached->filepath);
|
||||
if ($cached->exists) {
|
||||
$cached->timestamp = filemtime($cached->filepath);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the cached template and process its header
|
||||
*
|
||||
* @param Template $_smarty_tpl do not change variable name, is used by compiled template
|
||||
* @param Cached|null $cached cached object
|
||||
* @param bool $update flag if called because cache update
|
||||
*
|
||||
* @return boolean true or false if the cached content does not exist
|
||||
*/
|
||||
public function process(
|
||||
Template $_smarty_tpl,
|
||||
?Cached $cached = null,
|
||||
$update = false
|
||||
) {
|
||||
$_smarty_tpl->getCached()->setValid(false);
|
||||
if ($update && defined('HHVM_VERSION')) {
|
||||
eval('?>' . file_get_contents($_smarty_tpl->getCached()->filepath));
|
||||
return true;
|
||||
} else {
|
||||
return @include $_smarty_tpl->getCached()->filepath;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Write the rendered template output to cache
|
||||
*
|
||||
* @param Template $_template template object
|
||||
* @param string $content content to cache
|
||||
*
|
||||
* @return bool success
|
||||
* @throws \Smarty\Exception
|
||||
*/
|
||||
public function storeCachedContent(Template $_template, $content)
|
||||
{
|
||||
if ($_template->getSmarty()->writeFile($_template->getCached()->filepath, $content) === true) {
|
||||
if (function_exists('opcache_invalidate')
|
||||
&& (!function_exists('ini_get') || strlen(ini_get('opcache.restrict_api'))) < 1
|
||||
) {
|
||||
opcache_invalidate($_template->getCached()->filepath, true);
|
||||
} elseif (function_exists('apc_compile_file')) {
|
||||
apc_compile_file($_template->getCached()->filepath);
|
||||
}
|
||||
$cached = $_template->getCached();
|
||||
$cached->timestamp = $cached->exists = is_file($cached->filepath);
|
||||
if ($cached->exists) {
|
||||
$cached->timestamp = filemtime($cached->filepath);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read cached template from cache
|
||||
*
|
||||
* @param Template $_template template object
|
||||
*
|
||||
* @return string content
|
||||
*/
|
||||
public function retrieveCachedContent(Template $_template)
|
||||
{
|
||||
if (is_file($_template->getCached()->filepath)) {
|
||||
return file_get_contents($_template->getCached()->filepath);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Empty cache
|
||||
*
|
||||
* @param Smarty $smarty
|
||||
* @param integer $exp_time expiration time (number of seconds, not timestamp)
|
||||
*
|
||||
* @return integer number of cache files deleted
|
||||
*/
|
||||
public function clearAll(Smarty $smarty, $exp_time = null)
|
||||
{
|
||||
return $this->clear($smarty, null, null, null, $exp_time);
|
||||
}
|
||||
|
||||
/**
|
||||
* Empty cache for a specific template
|
||||
*
|
||||
* @param Smarty $smarty
|
||||
* @param string $resource_name template name
|
||||
* @param string $cache_id cache id
|
||||
* @param string $compile_id compile id
|
||||
* @param integer $exp_time expiration time (number of seconds, not timestamp)
|
||||
*
|
||||
* @return integer number of cache files deleted
|
||||
*/
|
||||
public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time)
|
||||
{
|
||||
$_cache_id = isset($cache_id) ? preg_replace('![^\w\|]+!', '_', $cache_id) : null;
|
||||
$_compile_id = isset($compile_id) ? preg_replace('![^\w]+!', '_', $compile_id) : null;
|
||||
$_dir_sep = $smarty->use_sub_dirs ? '/' : '^';
|
||||
$_compile_id_offset = $smarty->use_sub_dirs ? 3 : 0;
|
||||
$_dir = $smarty->getCacheDir();
|
||||
if ($_dir === '/') { //We should never want to delete this!
|
||||
return 0;
|
||||
}
|
||||
$_dir_length = strlen($_dir);
|
||||
if (isset($_cache_id)) {
|
||||
$_cache_id_parts = explode('|', $_cache_id);
|
||||
$_cache_id_parts_count = count($_cache_id_parts);
|
||||
if ($smarty->use_sub_dirs) {
|
||||
foreach ($_cache_id_parts as $id_part) {
|
||||
$_dir .= $id_part . '/';
|
||||
}
|
||||
}
|
||||
}
|
||||
if (isset($resource_name)) {
|
||||
$_save_stat = $smarty->caching;
|
||||
$smarty->caching = \Smarty\Smarty::CACHING_LIFETIME_CURRENT;
|
||||
$tpl = $smarty->doCreateTemplate($resource_name);
|
||||
$smarty->caching = $_save_stat;
|
||||
// remove from template cache
|
||||
if ($tpl->getSource()->exists) {
|
||||
$_resourcename_parts = basename(str_replace('^', '/', $tpl->getCached()->filepath));
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
$_count = 0;
|
||||
$_time = time();
|
||||
if (file_exists($_dir)) {
|
||||
$_cacheDirs = new RecursiveDirectoryIterator($_dir);
|
||||
$_cache = new RecursiveIteratorIterator($_cacheDirs, RecursiveIteratorIterator::CHILD_FIRST);
|
||||
foreach ($_cache as $_file) {
|
||||
if (substr(basename($_file->getPathname()), 0, 1) === '.') {
|
||||
continue;
|
||||
}
|
||||
$_filepath = (string)$_file;
|
||||
// directory ?
|
||||
if ($_file->isDir()) {
|
||||
if (!$_cache->isDot()) {
|
||||
// delete folder if empty
|
||||
@rmdir($_file->getPathname());
|
||||
}
|
||||
} else {
|
||||
// delete only php files
|
||||
if (substr($_filepath, -4) !== '.php') {
|
||||
continue;
|
||||
}
|
||||
$_parts = explode($_dir_sep, str_replace('\\', '/', substr($_filepath, $_dir_length)));
|
||||
$_parts_count = count($_parts);
|
||||
// check name
|
||||
if (isset($resource_name)) {
|
||||
if ($_parts[ $_parts_count - 1 ] !== $_resourcename_parts) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
// check compile id
|
||||
if (isset($_compile_id) && (!isset($_parts[ $_parts_count - 2 - $_compile_id_offset ])
|
||||
|| $_parts[ $_parts_count - 2 - $_compile_id_offset ] !== $_compile_id)
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
// check cache id
|
||||
if (isset($_cache_id)) {
|
||||
// count of cache id parts
|
||||
$_parts_count = (isset($_compile_id)) ? $_parts_count - 2 - $_compile_id_offset :
|
||||
$_parts_count - 1 - $_compile_id_offset;
|
||||
if ($_parts_count < $_cache_id_parts_count) {
|
||||
continue;
|
||||
}
|
||||
for ($i = 0; $i < $_cache_id_parts_count; $i++) {
|
||||
if ($_parts[ $i ] !== $_cache_id_parts[ $i ]) {
|
||||
continue 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (is_file($_filepath)) {
|
||||
// expired ?
|
||||
if (isset($exp_time)) {
|
||||
if ($exp_time < 0) {
|
||||
preg_match('#\'cache_lifetime\' =>\s*(\d*)#', file_get_contents($_filepath), $match);
|
||||
if ($_time < (filemtime($_filepath) + $match[ 1 ])) {
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
if ($_time - filemtime($_filepath) < $exp_time) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
$_count += @unlink($_filepath) ? 1 : 0;
|
||||
if (function_exists('opcache_invalidate')
|
||||
&& (!function_exists('ini_get') || strlen(ini_get("opcache.restrict_api")) < 1)
|
||||
) {
|
||||
opcache_invalidate($_filepath, true);
|
||||
} elseif (function_exists('apc_delete_file')) {
|
||||
apc_delete_file($_filepath);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return $_count;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check is cache is locked for this template
|
||||
*
|
||||
* @param Smarty $smarty Smarty object
|
||||
* @param Cached $cached cached object
|
||||
*
|
||||
* @return boolean true or false if cache is locked
|
||||
*/
|
||||
public function hasLock(Smarty $smarty, Cached $cached)
|
||||
{
|
||||
clearstatcache(true, $cached->lock_id ?? '');
|
||||
if (null !== $cached->lock_id && is_file($cached->lock_id)) {
|
||||
$t = filemtime($cached->lock_id);
|
||||
return $t && (time() - $t < $smarty->locking_timeout);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Lock cache for this template
|
||||
*
|
||||
* @param Smarty $smarty Smarty object
|
||||
* @param Cached $cached cached object
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function acquireLock(Smarty $smarty, Cached $cached)
|
||||
{
|
||||
$cached->is_locked = true;
|
||||
touch($cached->lock_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unlock cache for this template
|
||||
*
|
||||
* @param Smarty $smarty Smarty object
|
||||
* @param Cached $cached cached object
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function releaseLock(Smarty $smarty, Cached $cached)
|
||||
{
|
||||
$cached->is_locked = false;
|
||||
@unlink($cached->lock_id);
|
||||
}
|
||||
}
|
541
core/template/src/Cacheresource/KeyValueStore.php
Normal file
541
core/template/src/Cacheresource/KeyValueStore.php
Normal file
|
@ -0,0 +1,541 @@
|
|||
<?php
|
||||
|
||||
namespace Smarty\Cacheresource;
|
||||
|
||||
use Smarty\Smarty;
|
||||
use Smarty\Template;
|
||||
use Smarty\Template\Cached;
|
||||
|
||||
/**
|
||||
* Smarty Internal Plugin
|
||||
*
|
||||
|
||||
|
||||
*/
|
||||
|
||||
/**
|
||||
* Smarty Cache Handler Base for Key/Value Storage Implementations
|
||||
* This class implements the functionality required to use simple key/value stores
|
||||
* for hierarchical cache groups. key/value stores like memcache or APC do not support
|
||||
* wildcards in keys, therefore a cache group cannot be cleared like "a|*" - which
|
||||
* is no problem to filesystem and RDBMS implementations.
|
||||
* This implementation is based on the concept of invalidation. While one specific cache
|
||||
* can be identified and cleared, any range of caches cannot be identified. For this reason
|
||||
* each level of the cache group hierarchy can have its own value in the store. These values
|
||||
* are nothing but microtimes, telling us when a particular cache group was cleared for the
|
||||
* last time. These keys are evaluated for every cache read to determine if the cache has
|
||||
* been invalidated since it was created and should hence be treated as inexistent.
|
||||
* Although deep hierarchies are possible, they are not recommended. Try to keep your
|
||||
* cache groups as shallow as possible. Anything up 3-5 parents should be ok. So
|
||||
* »a|b|c« is a good depth where »a|b|c|d|e|f|g|h|i|j|k« isn't. Try to join correlating
|
||||
* cache groups: if your cache groups look somewhat like »a|b|$page|$items|$whatever«
|
||||
* consider using »a|b|c|$page-$items-$whatever« instead.
|
||||
*
|
||||
|
||||
|
||||
* @author Rodney Rehm
|
||||
*/
|
||||
abstract class KeyValueStore extends Base
|
||||
{
|
||||
/**
|
||||
* cache for contents
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $contents = array();
|
||||
|
||||
/**
|
||||
* cache for timestamps
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $timestamps = array();
|
||||
|
||||
/**
|
||||
* populate Cached Object with meta data from Resource
|
||||
*
|
||||
* @param Cached $cached cached object
|
||||
* @param Template $_template template object
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function populate(Cached $cached, Template $_template)
|
||||
{
|
||||
$cached->filepath = $_template->getSource()->uid . '#' . $this->sanitize($cached->getSource()->resource) . '#' .
|
||||
$this->sanitize($cached->cache_id) . '#' . $this->sanitize($cached->compile_id);
|
||||
$this->populateTimestamp($cached);
|
||||
}
|
||||
|
||||
/**
|
||||
* populate Cached Object with timestamp and exists from Resource
|
||||
*
|
||||
* @param Cached $cached cached object
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function populateTimestamp(Cached $cached)
|
||||
{
|
||||
if (!$this->fetch(
|
||||
$cached->filepath,
|
||||
$cached->getSource()->name,
|
||||
$cached->cache_id,
|
||||
$cached->compile_id,
|
||||
$content,
|
||||
$timestamp,
|
||||
$cached->getSource()->uid
|
||||
)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
$cached->content = $content;
|
||||
$cached->timestamp = (int)$timestamp;
|
||||
$cached->exists = !!$cached->timestamp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the cached template and process the header
|
||||
*
|
||||
* @param Template $_smarty_tpl do not change variable name, is used by compiled template
|
||||
* @param Cached|null $cached cached object
|
||||
* @param boolean $update flag if called because cache update
|
||||
*
|
||||
* @return boolean true or false if the cached content does not exist
|
||||
*/
|
||||
public function process(
|
||||
Template $_smarty_tpl,
|
||||
?Cached $cached = null,
|
||||
$update = false
|
||||
) {
|
||||
if (!$cached) {
|
||||
$cached = $_smarty_tpl->getCached();
|
||||
}
|
||||
$content = $cached->content ?: null;
|
||||
$timestamp = $cached->timestamp ?: null;
|
||||
if ($content === null || !$timestamp) {
|
||||
if (!$this->fetch(
|
||||
$_smarty_tpl->getCached()->filepath,
|
||||
$_smarty_tpl->getSource()->name,
|
||||
$_smarty_tpl->cache_id,
|
||||
$_smarty_tpl->compile_id,
|
||||
$content,
|
||||
$timestamp,
|
||||
$_smarty_tpl->getSource()->uid
|
||||
)
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (isset($content)) {
|
||||
eval('?>' . $content);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write the rendered template output to cache
|
||||
*
|
||||
* @param Template $_template template object
|
||||
* @param string $content content to cache
|
||||
*
|
||||
* @return boolean success
|
||||
*/
|
||||
public function storeCachedContent(Template $_template, $content)
|
||||
{
|
||||
$this->addMetaTimestamp($content);
|
||||
return $this->write(array($_template->getCached()->filepath => $content), $_template->cache_lifetime);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read cached template from cache
|
||||
*
|
||||
* @param Template $_template template object
|
||||
*
|
||||
* @return string|false content
|
||||
*/
|
||||
public function retrieveCachedContent(Template $_template)
|
||||
{
|
||||
$content = $_template->getCached()->content ?: null;
|
||||
$timestamp = null;
|
||||
if ($content === null) {
|
||||
if (!$this->fetch(
|
||||
$_template->getCached()->filepath,
|
||||
$_template->getSource()->name,
|
||||
$_template->cache_id,
|
||||
$_template->compile_id,
|
||||
$content,
|
||||
$timestamp,
|
||||
$_template->getSource()->uid
|
||||
)
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (isset($content)) {
|
||||
return $content;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Empty cache
|
||||
* {@internal the $exp_time argument is ignored altogether }}
|
||||
*
|
||||
* @param Smarty $smarty Smarty object
|
||||
* @param integer $exp_time expiration time [being ignored]
|
||||
*
|
||||
* @return integer number of cache files deleted [always -1]
|
||||
* @uses purge() to clear the whole store
|
||||
* @uses invalidate() to mark everything outdated if purge() is inapplicable
|
||||
*/
|
||||
public function clearAll(Smarty $smarty, $exp_time = null)
|
||||
{
|
||||
if (!$this->purge()) {
|
||||
$this->invalidate(null);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Empty cache for a specific template
|
||||
* {@internal the $exp_time argument is ignored altogether}}
|
||||
*
|
||||
* @param Smarty $smarty Smarty object
|
||||
* @param string $resource_name template name
|
||||
* @param string $cache_id cache id
|
||||
* @param string $compile_id compile id
|
||||
* @param integer $exp_time expiration time [being ignored]
|
||||
*
|
||||
* @return int number of cache files deleted [always -1]
|
||||
* @throws \Smarty\Exception
|
||||
* @uses buildCachedFilepath() to generate the CacheID
|
||||
* @uses invalidate() to mark CacheIDs parent chain as outdated
|
||||
* @uses delete() to remove CacheID from cache
|
||||
*/
|
||||
public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time)
|
||||
{
|
||||
$uid = $this->getTemplateUid($smarty, $resource_name);
|
||||
$cid = $uid . '#' . $this->sanitize($resource_name) . '#' . $this->sanitize($cache_id) . '#' .
|
||||
$this->sanitize($compile_id);
|
||||
$this->delete(array($cid));
|
||||
$this->invalidate($cid, $resource_name, $cache_id, $compile_id, $uid);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get template's unique ID
|
||||
*
|
||||
* @param Smarty $smarty Smarty object
|
||||
* @param string $resource_name template name
|
||||
*
|
||||
* @return string filepath of cache file
|
||||
* @throws \Smarty\Exception
|
||||
*/
|
||||
protected function getTemplateUid(Smarty $smarty, $resource_name)
|
||||
{
|
||||
if (isset($resource_name)) {
|
||||
$source = \Smarty\Template\Source::load(null, $smarty, $resource_name);
|
||||
if ($source->exists) {
|
||||
return $source->uid;
|
||||
}
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitize CacheID components
|
||||
*
|
||||
* @param string $string CacheID component to sanitize
|
||||
*
|
||||
* @return string sanitized CacheID component
|
||||
*/
|
||||
protected function sanitize($string)
|
||||
{
|
||||
$string = trim((string)$string, '|');
|
||||
if (!$string) {
|
||||
return '';
|
||||
}
|
||||
return preg_replace('#[^\w\|]+#S', '_', $string);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch and prepare a cache object.
|
||||
*
|
||||
* @param string $cid CacheID to fetch
|
||||
* @param string $resource_name template name
|
||||
* @param string $cache_id cache id
|
||||
* @param string $compile_id compile id
|
||||
* @param string $content cached content
|
||||
* @param integer &$timestamp cached timestamp (epoch)
|
||||
* @param string $resource_uid resource's uid
|
||||
*
|
||||
* @return boolean success
|
||||
*/
|
||||
protected function fetch(
|
||||
$cid,
|
||||
$resource_name = null,
|
||||
$cache_id = null,
|
||||
$compile_id = null,
|
||||
&$content = null,
|
||||
&$timestamp = null,
|
||||
$resource_uid = null
|
||||
) {
|
||||
$t = $this->read(array($cid));
|
||||
$content = !empty($t[ $cid ]) ? $t[ $cid ] : null;
|
||||
$timestamp = null;
|
||||
if ($content && ($timestamp = $this->getMetaTimestamp($content))) {
|
||||
$invalidated =
|
||||
$this->getLatestInvalidationTimestamp($cid, $resource_name, $cache_id, $compile_id, $resource_uid);
|
||||
if ($invalidated > $timestamp) {
|
||||
$timestamp = null;
|
||||
$content = null;
|
||||
}
|
||||
}
|
||||
return !!$content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add current microtime to the beginning of $cache_content
|
||||
* {@internal the header uses 8 Bytes, the first 4 Bytes are the seconds, the second 4 Bytes are the microseconds}}
|
||||
*
|
||||
* @param string &$content the content to be cached
|
||||
*/
|
||||
protected function addMetaTimestamp(&$content)
|
||||
{
|
||||
$mt = explode(' ', microtime());
|
||||
$ts = pack('NN', $mt[ 1 ], (int)($mt[ 0 ] * 100000000));
|
||||
$content = $ts . $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract the timestamp the $content was cached
|
||||
*
|
||||
* @param string &$content the cached content
|
||||
*
|
||||
* @return float the microtime the content was cached
|
||||
*/
|
||||
protected function getMetaTimestamp(&$content)
|
||||
{
|
||||
extract(unpack('N1s/N1m/a*content', $content));
|
||||
/**
|
||||
* @var int $s
|
||||
* @var int $m
|
||||
*/
|
||||
return $s + ($m / 100000000);
|
||||
}
|
||||
|
||||
/**
|
||||
* Invalidate CacheID
|
||||
*
|
||||
* @param string $cid CacheID
|
||||
* @param string $resource_name template name
|
||||
* @param string $cache_id cache id
|
||||
* @param string $compile_id compile id
|
||||
* @param string $resource_uid source's uid
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function invalidate(
|
||||
$cid = null,
|
||||
$resource_name = null,
|
||||
$cache_id = null,
|
||||
$compile_id = null,
|
||||
$resource_uid = null
|
||||
) {
|
||||
$now = microtime(true);
|
||||
$key = null;
|
||||
// invalidate everything
|
||||
if (!$resource_name && !$cache_id && !$compile_id) {
|
||||
$key = 'IVK#ALL';
|
||||
} // invalidate all caches by template
|
||||
else {
|
||||
if ($resource_name && !$cache_id && !$compile_id) {
|
||||
$key = 'IVK#TEMPLATE#' . $resource_uid . '#' . $this->sanitize($resource_name);
|
||||
} // invalidate all caches by cache group
|
||||
else {
|
||||
if (!$resource_name && $cache_id && !$compile_id) {
|
||||
$key = 'IVK#CACHE#' . $this->sanitize($cache_id);
|
||||
} // invalidate all caches by compile id
|
||||
else {
|
||||
if (!$resource_name && !$cache_id && $compile_id) {
|
||||
$key = 'IVK#COMPILE#' . $this->sanitize($compile_id);
|
||||
} // invalidate by combination
|
||||
else {
|
||||
$key = 'IVK#CID#' . $cid;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
$this->write(array($key => $now));
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine the latest timestamp known to the invalidation chain
|
||||
*
|
||||
* @param string $cid CacheID to determine latest invalidation timestamp of
|
||||
* @param string $resource_name template name
|
||||
* @param string $cache_id cache id
|
||||
* @param string $compile_id compile id
|
||||
* @param string $resource_uid source's filepath
|
||||
*
|
||||
* @return float the microtime the CacheID was invalidated
|
||||
*/
|
||||
protected function getLatestInvalidationTimestamp(
|
||||
$cid,
|
||||
$resource_name = null,
|
||||
$cache_id = null,
|
||||
$compile_id = null,
|
||||
$resource_uid = null
|
||||
) {
|
||||
// abort if there are no InvalidationKeys to check
|
||||
if (!($_cid = $this->listInvalidationKeys($cid, $resource_name, $cache_id, $compile_id, $resource_uid))) {
|
||||
return 0;
|
||||
}
|
||||
// there are no InValidationKeys
|
||||
if (!($values = $this->read($_cid))) {
|
||||
return 0;
|
||||
}
|
||||
// make sure we're dealing with floats
|
||||
$values = array_map('floatval', $values);
|
||||
return max($values);
|
||||
}
|
||||
|
||||
/**
|
||||
* Translate a CacheID into the list of applicable InvalidationKeys.
|
||||
* Splits 'some|chain|into|an|array' into array( '#clearAll#', 'some', 'some|chain', 'some|chain|into', ... )
|
||||
*
|
||||
* @param string $cid CacheID to translate
|
||||
* @param string $resource_name template name
|
||||
* @param string $cache_id cache id
|
||||
* @param string $compile_id compile id
|
||||
* @param string $resource_uid source's filepath
|
||||
*
|
||||
* @return array list of InvalidationKeys
|
||||
* @uses $invalidationKeyPrefix to prepend to each InvalidationKey
|
||||
*/
|
||||
protected function listInvalidationKeys(
|
||||
$cid,
|
||||
$resource_name = null,
|
||||
$cache_id = null,
|
||||
$compile_id = null,
|
||||
$resource_uid = null
|
||||
) {
|
||||
$t = array('IVK#ALL');
|
||||
$_name = $_compile = '#';
|
||||
if ($resource_name) {
|
||||
$_name .= $resource_uid . '#' . $this->sanitize($resource_name);
|
||||
$t[] = 'IVK#TEMPLATE' . $_name;
|
||||
}
|
||||
if ($compile_id) {
|
||||
$_compile .= $this->sanitize($compile_id);
|
||||
$t[] = 'IVK#COMPILE' . $_compile;
|
||||
}
|
||||
$_name .= '#';
|
||||
$cid = trim((string)$cache_id, '|');
|
||||
if (!$cid) {
|
||||
return $t;
|
||||
}
|
||||
$i = 0;
|
||||
while (true) {
|
||||
// determine next delimiter position
|
||||
$i = strpos($cid, '|', $i);
|
||||
// add complete CacheID if there are no more delimiters
|
||||
if ($i === false) {
|
||||
$t[] = 'IVK#CACHE#' . $cid;
|
||||
$t[] = 'IVK#CID' . $_name . $cid . $_compile;
|
||||
$t[] = 'IVK#CID' . $_name . $_compile;
|
||||
break;
|
||||
}
|
||||
$part = substr($cid, 0, $i);
|
||||
// add slice to list
|
||||
$t[] = 'IVK#CACHE#' . $part;
|
||||
$t[] = 'IVK#CID' . $_name . $part . $_compile;
|
||||
// skip past delimiter position
|
||||
$i++;
|
||||
}
|
||||
return $t;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check is cache is locked for this template
|
||||
*
|
||||
* @param Smarty $smarty Smarty object
|
||||
* @param Cached $cached cached object
|
||||
*
|
||||
* @return boolean true or false if cache is locked
|
||||
*/
|
||||
public function hasLock(Smarty $smarty, Cached $cached)
|
||||
{
|
||||
$key = 'LOCK#' . $cached->filepath;
|
||||
$data = $this->read(array($key));
|
||||
return $data && time() - $data[ $key ] < $smarty->locking_timeout;
|
||||
}
|
||||
|
||||
/**
|
||||
* Lock cache for this template
|
||||
*
|
||||
* @param Smarty $smarty Smarty object
|
||||
* @param Cached $cached cached object
|
||||
*
|
||||
* @return bool|void
|
||||
*/
|
||||
public function acquireLock(Smarty $smarty, Cached $cached)
|
||||
{
|
||||
$cached->is_locked = true;
|
||||
$key = 'LOCK#' . $cached->filepath;
|
||||
$this->write(array($key => time()), $smarty->locking_timeout);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unlock cache for this template
|
||||
*
|
||||
* @param Smarty $smarty Smarty object
|
||||
* @param Cached $cached cached object
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function releaseLock(Smarty $smarty, Cached $cached)
|
||||
{
|
||||
$cached->is_locked = false;
|
||||
$key = 'LOCK#' . $cached->filepath;
|
||||
$this->delete(array($key));
|
||||
}
|
||||
|
||||
/**
|
||||
* Read values for a set of keys from cache
|
||||
*
|
||||
* @param array $keys list of keys to fetch
|
||||
*
|
||||
* @return array list of values with the given keys used as indexes
|
||||
*/
|
||||
abstract protected function read(array $keys);
|
||||
|
||||
/**
|
||||
* Save values for a set of keys to cache
|
||||
*
|
||||
* @param array $keys list of values to save
|
||||
* @param int $expire expiration time
|
||||
*
|
||||
* @return boolean true on success, false on failure
|
||||
*/
|
||||
abstract protected function write(array $keys, $expire = null);
|
||||
|
||||
/**
|
||||
* Remove values from cache
|
||||
*
|
||||
* @param array $keys list of keys to delete
|
||||
*
|
||||
* @return boolean true on success, false on failure
|
||||
*/
|
||||
abstract protected function delete(array $keys);
|
||||
|
||||
/**
|
||||
* Remove *all* values from cache
|
||||
*
|
||||
* @return boolean true on success, false on failure
|
||||
*/
|
||||
protected function purge()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
233
core/template/src/Compile/Base.php
Normal file
233
core/template/src/Compile/Base.php
Normal 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;
|
||||
}
|
228
core/template/src/Compile/BlockCompiler.php
Normal file
228
core/template/src/Compile/BlockCompiler.php
Normal 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;
|
||||
}
|
||||
|
||||
}
|
26
core/template/src/Compile/CompilerInterface.php
Normal file
26
core/template/src/Compile/CompilerInterface.php
Normal 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;
|
||||
}
|
29
core/template/src/Compile/DefaultHandlerBlockCompiler.php
Normal file
29
core/template/src/Compile/DefaultHandlerBlockCompiler.php
Normal 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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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";
|
||||
}
|
||||
}
|
79
core/template/src/Compile/FunctionCallCompiler.php
Normal file
79
core/template/src/Compile/FunctionCallCompiler.php
Normal 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;
|
||||
}
|
||||
}
|
19
core/template/src/Compile/Modifier/BCPluginWrapper.php
Normal file
19
core/template/src/Compile/Modifier/BCPluginWrapper.php
Normal 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);
|
||||
}
|
||||
}
|
49
core/template/src/Compile/Modifier/Base.php
Normal file
49
core/template/src/Compile/Modifier/Base.php
Normal 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;
|
||||
}
|
||||
|
||||
}
|
25
core/template/src/Compile/Modifier/CatModifierCompiler.php
Normal file
25
core/template/src/Compile/Modifier/CatModifierCompiler.php
Normal 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) . ')';
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -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) . '\')';
|
||||
}
|
||||
|
||||
}
|
|
@ -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)';
|
||||
}
|
||||
|
||||
}
|
|
@ -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)';
|
||||
}
|
||||
|
||||
}
|
|
@ -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)';
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
19
core/template/src/Compile/Modifier/EmptyModifierCompiler.php
Normal file
19
core/template/src/Compile/Modifier/EmptyModifierCompiler.php
Normal 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] . ')';
|
||||
}
|
||||
|
||||
}
|
|
@ -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) . ')';
|
||||
}
|
||||
}
|
|
@ -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 ] . ')';
|
||||
}
|
||||
|
||||
}
|
|
@ -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 ] . ')';
|
||||
}
|
||||
|
||||
}
|
|
@ -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] . ')';
|
||||
}
|
||||
|
||||
}
|
25
core/template/src/Compile/Modifier/IssetModifierCompiler.php
Normal file
25
core/template/src/Compile/Modifier/IssetModifierCompiler.php
Normal 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) . ')';
|
||||
}
|
||||
|
||||
}
|
|
@ -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] : '') . ')';
|
||||
}
|
||||
|
||||
}
|
19
core/template/src/Compile/Modifier/LowerModifierCompiler.php
Normal file
19
core/template/src/Compile/Modifier/LowerModifierCompiler.php
Normal 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) . '\')';
|
||||
}
|
||||
|
||||
}
|
|
@ -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);
|
||||
}
|
17
core/template/src/Compile/Modifier/Nl2brModifierCompiler.php
Normal file
17
core/template/src/Compile/Modifier/Nl2brModifierCompiler.php
Normal 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) . ')';
|
||||
}
|
||||
}
|
|
@ -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 "''";
|
||||
}
|
||||
|
||||
}
|
21
core/template/src/Compile/Modifier/RawModifierCompiler.php
Normal file
21
core/template/src/Compile/Modifier/RawModifierCompiler.php
Normal 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]);
|
||||
}
|
||||
}
|
18
core/template/src/Compile/Modifier/RoundModifierCompiler.php
Normal file
18
core/template/src/Compile/Modifier/RoundModifierCompiler.php
Normal 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) . ')';
|
||||
}
|
||||
|
||||
}
|
|
@ -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] . ')';
|
||||
}
|
||||
|
||||
}
|
|
@ -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 ] . ')';
|
||||
}
|
||||
|
||||
}
|
24
core/template/src/Compile/Modifier/StripModifierCompiler.php
Normal file
24
core/template/src/Compile/Modifier/StripModifierCompiler.php
Normal 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:" "}
|
||||
* 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]})";
|
||||
}
|
||||
|
||||
}
|
|
@ -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 ] . ')';
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -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] . ')';
|
||||
}
|
||||
|
||||
}
|
|
@ -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] : '') . ')';
|
||||
}
|
||||
|
||||
}
|
|
@ -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) . '")';
|
||||
}
|
||||
|
||||
}
|
|
@ -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 ];
|
||||
}
|
||||
}
|
||||
}
|
18
core/template/src/Compile/Modifier/UpperModifierCompiler.php
Normal file
18
core/template/src/Compile/Modifier/UpperModifierCompiler.php
Normal 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) . '\')';
|
||||
}
|
||||
|
||||
}
|
|
@ -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 ] . ')';
|
||||
}
|
||||
|
||||
}
|
95
core/template/src/Compile/ModifierCompiler.php
Normal file
95
core/template/src/Compile/ModifierCompiler.php
Normal 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);
|
||||
}
|
||||
}
|
44
core/template/src/Compile/ObjectMethodBlockCompiler.php
Normal file
44
core/template/src/Compile/ObjectMethodBlockCompiler.php
Normal 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;
|
||||
}
|
||||
|
||||
}
|
76
core/template/src/Compile/ObjectMethodCallCompiler.php
Normal file
76
core/template/src/Compile/ObjectMethodCallCompiler.php
Normal 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";
|
||||
}
|
||||
}
|
||||
}
|
96
core/template/src/Compile/PrintExpressionCompiler.php
Normal file
96
core/template/src/Compile/PrintExpressionCompiler.php
Normal 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;
|
||||
}
|
||||
|
||||
}
|
134
core/template/src/Compile/SpecialVariableCompiler.php
Normal file
134
core/template/src/Compile/SpecialVariableCompiler.php
Normal 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 '';
|
||||
}
|
||||
}
|
58
core/template/src/Compile/Tag/Append.php
Normal file
58
core/template/src/Compile/Tag/Append.php
Normal 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);
|
||||
}
|
||||
}
|
95
core/template/src/Compile/Tag/Assign.php
Normal file
95
core/template/src/Compile/Tag/Assign.php
Normal 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;
|
||||
}
|
||||
}
|
31
core/template/src/Compile/Tag/BCPluginWrapper.php
Normal file
31
core/template/src/Compile/Tag/BCPluginWrapper.php
Normal 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());
|
||||
}
|
||||
}
|
92
core/template/src/Compile/Tag/Block.php
Normal file
92
core/template/src/Compile/Tag/Block.php
Normal 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 '';
|
||||
}
|
||||
}
|
110
core/template/src/Compile/Tag/BlockClose.php
Normal file
110
core/template/src/Compile/Tag/BlockClose.php
Normal 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;
|
||||
}
|
||||
|
||||
}
|
123
core/template/src/Compile/Tag/BreakTag.php
Normal file
123
core/template/src/Compile/Tag/BreakTag.php
Normal 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];
|
||||
}
|
||||
}
|
81
core/template/src/Compile/Tag/Call.php
Normal file
81
core/template/src/Compile/Tag/Call.php
Normal 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;
|
||||
}
|
||||
}
|
72
core/template/src/Compile/Tag/Capture.php
Normal file
72
core/template/src/Compile/Tag/Capture.php
Normal 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);?>";
|
||||
}
|
||||
}
|
43
core/template/src/Compile/Tag/CaptureClose.php
Normal file
43
core/template/src/Compile/Tag/CaptureClose.php
Normal 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);?>";
|
||||
}
|
||||
}
|
77
core/template/src/Compile/Tag/ConfigLoad.php
Normal file
77
core/template/src/Compile/Tag/ConfigLoad.php
Normal 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";
|
||||
}
|
||||
}
|
27
core/template/src/Compile/Tag/ContinueTag.php
Normal file
27
core/template/src/Compile/Tag/ContinueTag.php
Normal 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';
|
||||
}
|
45
core/template/src/Compile/Tag/Debug.php
Normal file
45
core/template/src/Compile/Tag/Debug.php
Normal 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;
|
||||
}
|
||||
}
|
86
core/template/src/Compile/Tag/ElseIfTag.php
Normal file
86
core/template/src/Compile/Tag/ElseIfTag.php
Normal 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']}) {?>");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
29
core/template/src/Compile/Tag/ElseTag.php
Normal file
29
core/template/src/Compile/Tag/ElseTag.php
Normal 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 { ?>';
|
||||
}
|
||||
}
|
74
core/template/src/Compile/Tag/EvalTag.php
Normal file
74
core/template/src/Compile/Tag/EvalTag.php
Normal 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 ?>";
|
||||
}
|
||||
}
|
87
core/template/src/Compile/Tag/ExtendsTag.php
Normal file
87
core/template/src/Compile/Tag/ExtendsTag.php
Normal 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?>"
|
||||
);
|
||||
}
|
||||
}
|
51
core/template/src/Compile/Tag/ForClose.php
Normal file
51
core/template/src/Compile/Tag/ForClose.php
Normal 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;
|
||||
}
|
||||
}
|
30
core/template/src/Compile/Tag/ForElse.php
Normal file
30
core/template/src/Compile/Tag/ForElse.php
Normal 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 { ?>";
|
||||
}
|
||||
}
|
101
core/template/src/Compile/Tag/ForTag.php
Normal file
101
core/template/src/Compile/Tag/ForTag.php
Normal 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;
|
||||
}
|
||||
}
|
55
core/template/src/Compile/Tag/ForeachClose.php
Normal file
55
core/template/src/Compile/Tag/ForeachClose.php
Normal 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;
|
||||
}
|
||||
}
|
35
core/template/src/Compile/Tag/ForeachElse.php
Normal file
35
core/template/src/Compile/Tag/ForeachElse.php
Normal 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;
|
||||
}
|
||||
}
|
206
core/template/src/Compile/Tag/ForeachSection.php
Normal file
206
core/template/src/Compile/Tag/ForeachSection.php
Normal 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)";
|
||||
}
|
||||
}
|
286
core/template/src/Compile/Tag/ForeachTag.php
Normal file
286
core/template/src/Compile/Tag/ForeachTag.php
Normal 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});";
|
||||
}
|
||||
}
|
164
core/template/src/Compile/Tag/FunctionClose.php
Normal file
164
core/template/src/Compile/Tag/FunctionClose.php
Normal 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);
|
||||
}
|
||||
}
|
73
core/template/src/Compile/Tag/FunctionTag.php
Normal file
73
core/template/src/Compile/Tag/FunctionTag.php
Normal 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 '';
|
||||
}
|
||||
}
|
48
core/template/src/Compile/Tag/IfClose.php
Normal file
48
core/template/src/Compile/Tag/IfClose.php
Normal 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}?>";
|
||||
}
|
||||
}
|
70
core/template/src/Compile/Tag/IfTag.php
Normal file
70
core/template/src/Compile/Tag/IfTag.php
Normal 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']}) {?>";
|
||||
}
|
||||
}
|
||||
}
|
189
core/template/src/Compile/Tag/IncludeTag.php
Normal file
189
core/template/src/Compile/Tag/IncludeTag.php
Normal 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;
|
||||
}
|
||||
|
||||
}
|
54
core/template/src/Compile/Tag/Inheritance.php
Normal file
54
core/template/src/Compile/Tag/Inheritance.php
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
41
core/template/src/Compile/Tag/Ldelim.php
Normal file
41
core/template/src/Compile/Tag/Ldelim.php
Normal 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();
|
||||
}
|
||||
}
|
36
core/template/src/Compile/Tag/Nocache.php
Normal file
36
core/template/src/Compile/Tag/Nocache.php
Normal 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 '';
|
||||
}
|
||||
}
|
37
core/template/src/Compile/Tag/NocacheClose.php
Normal file
37
core/template/src/Compile/Tag/NocacheClose.php
Normal 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 '';
|
||||
}
|
||||
}
|
36
core/template/src/Compile/Tag/Rdelim.php
Normal file
36
core/template/src/Compile/Tag/Rdelim.php
Normal 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();
|
||||
}
|
||||
}
|
399
core/template/src/Compile/Tag/Section.php
Normal file
399
core/template/src/Compile/Tag/Section.php
Normal 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;
|
||||
}
|
||||
}
|
48
core/template/src/Compile/Tag/SectionClose.php
Normal file
48
core/template/src/Compile/Tag/SectionClose.php
Normal 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;
|
||||
}
|
||||
}
|
29
core/template/src/Compile/Tag/SectionElse.php
Normal file
29
core/template/src/Compile/Tag/SectionElse.php
Normal 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 ?>";
|
||||
}
|
||||
}
|
40
core/template/src/Compile/Tag/Setfilter.php
Normal file
40
core/template/src/Compile/Tag/Setfilter.php
Normal 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 '';
|
||||
}
|
||||
}
|
43
core/template/src/Compile/Tag/SetfilterClose.php
Normal file
43
core/template/src/Compile/Tag/SetfilterClose.php
Normal 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 '';
|
||||
}
|
||||
}
|
45
core/template/src/Compile/Tag/WhileClose.php
Normal file
45
core/template/src/Compile/Tag/WhileClose.php
Normal 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";
|
||||
}
|
||||
}
|
72
core/template/src/Compile/Tag/WhileTag.php
Normal file
72
core/template/src/Compile/Tag/WhileTag.php
Normal 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']}) {?>";
|
||||
}
|
||||
}
|
||||
}
|
23
core/template/src/Compiler/BaseCompiler.php
Normal file
23
core/template/src/Compiler/BaseCompiler.php
Normal file
|
@ -0,0 +1,23 @@
|
|||
<?php
|
||||
|
||||
namespace Smarty\Compiler;
|
||||
|
||||
use Smarty\Smarty;
|
||||
|
||||
abstract class BaseCompiler {
|
||||
|
||||
/**
|
||||
* Smarty object
|
||||
*
|
||||
* @var Smarty
|
||||
*/
|
||||
protected $smarty = null;
|
||||
|
||||
/**
|
||||
* @return Smarty|null
|
||||
*/
|
||||
public function getSmarty(): Smarty {
|
||||
return $this->smarty;
|
||||
}
|
||||
|
||||
}
|
126
core/template/src/Compiler/CodeFrame.php
Normal file
126
core/template/src/Compiler/CodeFrame.php
Normal file
|
@ -0,0 +1,126 @@
|
|||
<?php
|
||||
|
||||
namespace Smarty\Compiler;
|
||||
|
||||
use Smarty\Exception;
|
||||
|
||||
/**
|
||||
* Smarty Internal Extension
|
||||
* This file contains the Smarty template extension to create a code frame
|
||||
*
|
||||
* @author Uwe Tews
|
||||
*/
|
||||
|
||||
/**
|
||||
* Create code frame for compiled and cached templates
|
||||
*/
|
||||
class CodeFrame
|
||||
{
|
||||
|
||||
/**
|
||||
* @var \Smarty\Template
|
||||
*/
|
||||
private $_template;
|
||||
|
||||
public function __construct(\Smarty\Template $_template) {
|
||||
$this->_template = $_template;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create code frame for compiled and cached templates
|
||||
*
|
||||
* @param string $content optional template content
|
||||
* @param string $functions compiled template function and block code
|
||||
* @param bool $cache flag for cache file
|
||||
* @param Template|null $compiler
|
||||
*
|
||||
* @return string
|
||||
* @throws Exception
|
||||
*/
|
||||
public function create(
|
||||
$content = '',
|
||||
$functions = '',
|
||||
$cache = false,
|
||||
?\Smarty\Compiler\Template $compiler = null
|
||||
) {
|
||||
// build property code
|
||||
$properties[ 'version' ] = \Smarty\Smarty::SMARTY_VERSION;
|
||||
$properties[ 'unifunc' ] = 'content_' . str_replace(array('.', ','), '_', uniqid('', true));
|
||||
if (!$cache) {
|
||||
$properties[ 'has_nocache_code' ] = $this->_template->getCompiled()->getNocacheCode();
|
||||
$properties[ 'file_dependency' ] = $this->_template->getCompiled()->file_dependency;
|
||||
$properties[ 'includes' ] = $this->_template->getCompiled()->includes;
|
||||
} else {
|
||||
$properties[ 'has_nocache_code' ] = $this->_template->getCached()->getNocacheCode();
|
||||
$properties[ 'file_dependency' ] = $this->_template->getCached()->file_dependency;
|
||||
$properties[ 'cache_lifetime' ] = $this->_template->cache_lifetime;
|
||||
}
|
||||
$output = sprintf(
|
||||
"<?php\n/* Smarty version %s, created on %s\n from '%s' */\n\n",
|
||||
$properties[ 'version' ],
|
||||
date("Y-m-d H:i:s"),
|
||||
str_replace('*/', '* /', $this->_template->getSource()->getFullResourceName())
|
||||
);
|
||||
$output .= "/* @var \\Smarty\\Template \$_smarty_tpl */\n";
|
||||
$dec = "\$_smarty_tpl->" . ($cache ? "getCached()" : "getCompiled()");
|
||||
$dec .= "->isFresh(\$_smarty_tpl, " . var_export($properties, true) . ')';
|
||||
$output .= "if ({$dec}) {\n";
|
||||
$output .= "function {$properties['unifunc']} (\\Smarty\\Template \$_smarty_tpl) {\n";
|
||||
|
||||
$output .= $this->insertLocalVariables();
|
||||
|
||||
if (!$cache && !empty($compiler->tpl_function)) {
|
||||
$output .= '$_smarty_tpl->getSmarty()->getRuntime(\'TplFunction\')->registerTplFunctions($_smarty_tpl, ';
|
||||
$output .= var_export($compiler->tpl_function, true);
|
||||
$output .= ");\n";
|
||||
}
|
||||
if ($cache && $this->_template->getSmarty()->hasRuntime('TplFunction')) {
|
||||
if ($tplfunctions = $this->_template->getSmarty()->getRuntime('TplFunction')->getTplFunction($this->_template)) {
|
||||
$output .= "\$_smarty_tpl->getSmarty()->getRuntime('TplFunction')->registerTplFunctions(\$_smarty_tpl, " .
|
||||
var_export($tplfunctions, true) . ");\n";
|
||||
}
|
||||
}
|
||||
$output .= "?>";
|
||||
$output .= $content;
|
||||
$output .= "<?php }\n?>";
|
||||
$output .= $functions;
|
||||
$output .= "<?php }\n";
|
||||
// remove unneeded PHP tags
|
||||
if (preg_match('/\s*\?>[\n]?<\?php\s*/', $output)) {
|
||||
$curr_split = preg_split(
|
||||
'/\s*\?>[\n]?<\?php\s*/',
|
||||
$output
|
||||
);
|
||||
preg_match_all(
|
||||
'/\s*\?>[\n]?<\?php\s*/',
|
||||
$output,
|
||||
$curr_parts
|
||||
);
|
||||
$output = '';
|
||||
foreach ($curr_split as $idx => $curr_output) {
|
||||
$output .= $curr_output;
|
||||
if (isset($curr_parts[ 0 ][ $idx ])) {
|
||||
$output .= "\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
if (preg_match('/\?>\s*$/', $output)) {
|
||||
$curr_split = preg_split(
|
||||
'/\?>\s*$/',
|
||||
$output
|
||||
);
|
||||
$output = '';
|
||||
foreach ($curr_split as $idx => $curr_output) {
|
||||
$output .= $curr_output;
|
||||
}
|
||||
}
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function insertLocalVariables(): string {
|
||||
return '$_smarty_current_dir = ' . var_export(dirname($this->_template->getSource()->getFilepath() ?? '.'), true) . ";\n";
|
||||
}
|
||||
}
|
173
core/template/src/Compiler/Configfile.php
Normal file
173
core/template/src/Compiler/Configfile.php
Normal file
|
@ -0,0 +1,173 @@
|
|||
<?php
|
||||
/**
|
||||
* Smarty Internal Plugin Config File Compiler
|
||||
* This is the config file compiler class. It calls the lexer and parser to
|
||||
* perform the compiling.
|
||||
*
|
||||
|
||||
|
||||
* @author Uwe Tews
|
||||
*/
|
||||
|
||||
namespace Smarty\Compiler;
|
||||
use Smarty\Lexer\ConfigfileLexer;
|
||||
use Smarty\Parser\ConfigfileParser;
|
||||
use Smarty\Smarty;
|
||||
use Smarty\Template;
|
||||
use Smarty\CompilerException;
|
||||
|
||||
/**
|
||||
* Main config file compiler class
|
||||
*
|
||||
|
||||
|
||||
*/
|
||||
class Configfile extends BaseCompiler {
|
||||
|
||||
/**
|
||||
* Lexer object
|
||||
*
|
||||
* @var ConfigfileLexer
|
||||
*/
|
||||
public $lex;
|
||||
|
||||
/**
|
||||
* Parser object
|
||||
*
|
||||
* @var ConfigfileParser
|
||||
*/
|
||||
public $parser;
|
||||
|
||||
/**
|
||||
* Smarty object
|
||||
*
|
||||
* @var Smarty object
|
||||
*/
|
||||
public $smarty;
|
||||
|
||||
/**
|
||||
* Smarty object
|
||||
*
|
||||
* @var Template object
|
||||
*/
|
||||
public $template;
|
||||
|
||||
/**
|
||||
* Compiled config data sections and variables
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $config_data = [];
|
||||
|
||||
/**
|
||||
* Initialize compiler
|
||||
*
|
||||
* @param Smarty $smarty global instance
|
||||
*/
|
||||
public function __construct(Smarty $smarty) {
|
||||
$this->smarty = $smarty;
|
||||
$this->config_data['sections'] = [];
|
||||
$this->config_data['vars'] = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to compile Smarty config source.
|
||||
*
|
||||
* @param Template $template
|
||||
*
|
||||
* @return bool true if compiling succeeded, false if it failed
|
||||
* @throws \Smarty\Exception
|
||||
*/
|
||||
public function compileTemplate(Template $template) {
|
||||
$this->template = $template;
|
||||
$this->template->getCompiled()->file_dependency[$this->template->getSource()->uid] =
|
||||
[
|
||||
$this->template->getSource()->getResourceName(),
|
||||
$this->template->getSource()->getTimeStamp(),
|
||||
$this->template->getSource()->type,
|
||||
];
|
||||
if ($this->smarty->debugging) {
|
||||
$this->smarty->getDebug()->start_compile($this->template);
|
||||
}
|
||||
// init the lexer/parser to compile the config file
|
||||
/* @var ConfigfileLexer $this->lex */
|
||||
$this->lex = new ConfigfileLexer(
|
||||
str_replace(
|
||||
[
|
||||
"\r\n",
|
||||
"\r",
|
||||
],
|
||||
"\n",
|
||||
$template->getSource()->getContent()
|
||||
) . "\n",
|
||||
$this
|
||||
);
|
||||
|
||||
$this->parser = new ConfigfileParser($this->lex, $this);
|
||||
if ($this->smarty->_parserdebug) {
|
||||
$this->parser->PrintTrace();
|
||||
}
|
||||
// get tokens from lexer and parse them
|
||||
while ($this->lex->yylex()) {
|
||||
if ($this->smarty->_parserdebug) {
|
||||
echo "Parsing {$this->parser->yyTokenName[$this->lex->token]} Token {$this->lex->value} Line {$this->lex->line} \n";
|
||||
}
|
||||
$this->parser->doParse($this->lex->token, $this->lex->value);
|
||||
}
|
||||
// finish parsing process
|
||||
$this->parser->doParse(0, 0);
|
||||
if ($this->smarty->debugging) {
|
||||
$this->smarty->getDebug()->end_compile($this->template);
|
||||
}
|
||||
// template header code
|
||||
$template_header = sprintf(
|
||||
"<?php /* Smarty version %s, created on %s\n compiled from '%s' */ ?>\n",
|
||||
\Smarty\Smarty::SMARTY_VERSION,
|
||||
date("Y-m-d H:i:s"),
|
||||
str_replace('*/', '* /', $this->template->getSource()->getFullResourceName())
|
||||
);
|
||||
$code = '<?php $_smarty_tpl->parent->assignConfigVars(' .
|
||||
var_export($this->config_data, true) . ', $_smarty_tpl->getValue("sections")); ?>';
|
||||
return $template_header . $this->template->createCodeFrame($code);
|
||||
}
|
||||
|
||||
/**
|
||||
* display compiler error messages without dying
|
||||
* If parameter $args is empty it is a parser detected syntax error.
|
||||
* In this case the parser is called to obtain information about expected tokens.
|
||||
* If parameter $args contains a string this is used as error message
|
||||
*
|
||||
* @param string $args individual error message or null
|
||||
*
|
||||
* @throws CompilerException
|
||||
*/
|
||||
public function trigger_config_file_error($args = null) {
|
||||
// get config source line which has error
|
||||
$line = $this->lex->line;
|
||||
if (isset($args)) {
|
||||
// $line--;
|
||||
}
|
||||
$match = preg_split("/\n/", $this->lex->data);
|
||||
$error_text =
|
||||
"Syntax error in config file '{$this->template->getSource()->getFullResourceName()}' on line {$line} '{$match[$line - 1]}' ";
|
||||
if (isset($args)) {
|
||||
// individual error message
|
||||
$error_text .= $args;
|
||||
} else {
|
||||
// expected token from parser
|
||||
foreach ($this->parser->yy_get_expected_tokens($this->parser->yymajor) as $token) {
|
||||
$exp_token = $this->parser->yyTokenName[$token];
|
||||
if (isset($this->lex->smarty_token_names[$exp_token])) {
|
||||
// token type from lexer
|
||||
$expect[] = '"' . $this->lex->smarty_token_names[$exp_token] . '"';
|
||||
} else {
|
||||
// otherwise internal token name
|
||||
$expect[] = $this->parser->yyTokenName[$token];
|
||||
}
|
||||
}
|
||||
// output parser error message
|
||||
$error_text .= ' - Unexpected "' . $this->lex->value . '", expected one of: ' . implode(' , ', $expect);
|
||||
}
|
||||
throw new CompilerException($error_text);
|
||||
}
|
||||
}
|
1512
core/template/src/Compiler/Template.php
Normal file
1512
core/template/src/Compiler/Template.php
Normal file
File diff suppressed because it is too large
Load diff
73
core/template/src/CompilerException.php
Normal file
73
core/template/src/CompilerException.php
Normal file
|
@ -0,0 +1,73 @@
|
|||
<?php
|
||||
|
||||
namespace Smarty;
|
||||
|
||||
/**
|
||||
* Smarty compiler exception class
|
||||
*
|
||||
|
||||
*/
|
||||
class CompilerException extends Exception {
|
||||
|
||||
/**
|
||||
* The constructor of the exception
|
||||
*
|
||||
* @param string $message The Exception message to throw.
|
||||
* @param int $code The Exception code.
|
||||
* @param string|null $filename The filename where the exception is thrown.
|
||||
* @param int|null $line The line number where the exception is thrown.
|
||||
* @param \Throwable|null $previous The previous exception used for the exception chaining.
|
||||
*/
|
||||
public function __construct(
|
||||
string $message = "",
|
||||
int $code = 0,
|
||||
?string $filename = null,
|
||||
?int $line = null,
|
||||
?\Throwable $previous = null
|
||||
) {
|
||||
parent::__construct($message, $code, $previous);
|
||||
|
||||
// These are optional parameters, should be be overridden only when present!
|
||||
if ($filename) {
|
||||
$this->file = $filename;
|
||||
}
|
||||
if ($line) {
|
||||
$this->line = $line;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function __toString() {
|
||||
return ' --> Smarty Compiler: ' . $this->message . ' <-- ';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $line
|
||||
*/
|
||||
public function setLine($line) {
|
||||
$this->line = $line;
|
||||
}
|
||||
|
||||
/**
|
||||
* The template source snippet relating to the error
|
||||
*
|
||||
* @type string|null
|
||||
*/
|
||||
public $source = null;
|
||||
|
||||
/**
|
||||
* The raw text of the error message
|
||||
*
|
||||
* @type string|null
|
||||
*/
|
||||
public $desc = null;
|
||||
|
||||
/**
|
||||
* The resource identifier or template name
|
||||
*
|
||||
* @type string|null
|
||||
*/
|
||||
public $template = null;
|
||||
}
|
521
core/template/src/Data.php
Normal file
521
core/template/src/Data.php
Normal file
|
@ -0,0 +1,521 @@
|
|||
<?php
|
||||
|
||||
namespace Smarty;
|
||||
|
||||
/**
|
||||
* Smarty Internal Plugin Data
|
||||
* This file contains the basic properties and methods for holding config and template variables
|
||||
*/
|
||||
class Data
|
||||
{
|
||||
|
||||
/**
|
||||
* define variable scopes
|
||||
*/
|
||||
const SCOPE_LOCAL = 1;
|
||||
const SCOPE_PARENT = 2;
|
||||
const SCOPE_TPL_ROOT = 4;
|
||||
const SCOPE_ROOT = 8;
|
||||
const SCOPE_SMARTY = 16;
|
||||
const SCOPE_GLOBAL = 32;
|
||||
|
||||
/**
|
||||
* Global smarty instance
|
||||
*
|
||||
* @var Smarty
|
||||
*/
|
||||
protected $smarty = null;
|
||||
|
||||
/**
|
||||
* template variables
|
||||
*
|
||||
* @var Variable[]
|
||||
*/
|
||||
public $tpl_vars = array();
|
||||
|
||||
/**
|
||||
* parent data container (if any)
|
||||
*
|
||||
* @var Data
|
||||
*/
|
||||
public $parent = null;
|
||||
|
||||
/**
|
||||
* configuration settings
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
public $config_vars = array();
|
||||
|
||||
/**
|
||||
* This variable will hold a stack of template variables.
|
||||
*
|
||||
* @var null|array
|
||||
*/
|
||||
private $_var_stack = [];
|
||||
|
||||
/**
|
||||
* This variable will hold a stack of config variables.
|
||||
*
|
||||
* @var null|array
|
||||
*/
|
||||
private $_config_stack = [];
|
||||
|
||||
/**
|
||||
* Default scope for new variables
|
||||
* @var int
|
||||
*/
|
||||
protected $defaultScope = self::SCOPE_LOCAL;
|
||||
|
||||
/**
|
||||
* create Smarty data object
|
||||
*
|
||||
* @param Smarty|array $_parent parent template
|
||||
* @param Smarty|Template $smarty global smarty instance
|
||||
* @param string $name optional data block name
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __construct($_parent = null, $smarty = null, $name = null) {
|
||||
|
||||
$this->smarty = $smarty;
|
||||
if (is_object($_parent)) {
|
||||
// when object set up back pointer
|
||||
$this->parent = $_parent;
|
||||
} elseif (is_array($_parent)) {
|
||||
// set up variable values
|
||||
foreach ($_parent as $_key => $_val) {
|
||||
$this->assign($_key, $_val);
|
||||
}
|
||||
} elseif ($_parent !== null) {
|
||||
throw new Exception('Wrong type for template variables');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* assigns a Smarty variable
|
||||
*
|
||||
* @param array|string $tpl_var the template variable name(s)
|
||||
* @param mixed $value the value to assign
|
||||
* @param boolean $nocache if true any output of this variable will be not cached
|
||||
* @param int $scope one of self::SCOPE_* constants
|
||||
*
|
||||
* @return Data current Data (or Smarty or \Smarty\Template) instance for
|
||||
* chaining
|
||||
*/
|
||||
public function assign($tpl_var, $value = null, $nocache = false, $scope = null)
|
||||
{
|
||||
if (is_array($tpl_var)) {
|
||||
foreach ($tpl_var as $_key => $_val) {
|
||||
$this->assign($_key, $_val, $nocache, $scope);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
switch ($scope ?? $this->getDefaultScope()) {
|
||||
case self::SCOPE_GLOBAL:
|
||||
case self::SCOPE_SMARTY:
|
||||
$this->getSmarty()->assign($tpl_var, $value);
|
||||
break;
|
||||
case self::SCOPE_TPL_ROOT:
|
||||
$ptr = $this;
|
||||
while (isset($ptr->parent) && ($ptr->parent instanceof Template)) {
|
||||
$ptr = $ptr->parent;
|
||||
}
|
||||
$ptr->assign($tpl_var, $value);
|
||||
break;
|
||||
case self::SCOPE_ROOT:
|
||||
$ptr = $this;
|
||||
while (isset($ptr->parent) && !($ptr->parent instanceof Smarty)) {
|
||||
$ptr = $ptr->parent;
|
||||
}
|
||||
$ptr->assign($tpl_var, $value);
|
||||
break;
|
||||
case self::SCOPE_PARENT:
|
||||
if ($this->parent) {
|
||||
$this->parent->assign($tpl_var, $value);
|
||||
} else {
|
||||
// assign local as fallback
|
||||
$this->assign($tpl_var, $value);
|
||||
}
|
||||
break;
|
||||
case self::SCOPE_LOCAL:
|
||||
default:
|
||||
if (isset($this->tpl_vars[$tpl_var])) {
|
||||
$this->tpl_vars[$tpl_var]->setValue($value);
|
||||
if ($nocache) {
|
||||
$this->tpl_vars[$tpl_var]->setNocache(true);
|
||||
}
|
||||
} else {
|
||||
$this->tpl_vars[$tpl_var] = new Variable($value, $nocache);
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* appends values to template variables
|
||||
*
|
||||
* @param array|string $tpl_var the template variable name(s)
|
||||
* @param mixed $value the value to append
|
||||
* @param bool $merge flag if array elements shall be merged
|
||||
* @param bool $nocache if true any output of this variable will
|
||||
* be not cached
|
||||
*
|
||||
* @return Data
|
||||
* @api Smarty::append()
|
||||
*/
|
||||
public function append($tpl_var, $value = null, $merge = false, $nocache = false)
|
||||
{
|
||||
if (is_array($tpl_var)) {
|
||||
foreach ($tpl_var as $_key => $_val) {
|
||||
$this->append($_key, $_val, $merge, $nocache);
|
||||
}
|
||||
} else {
|
||||
|
||||
$newValue = $this->getValue($tpl_var) ?? [];
|
||||
if (!is_array($newValue)) {
|
||||
$newValue = (array) $newValue;
|
||||
}
|
||||
|
||||
if ($merge && is_array($value)) {
|
||||
foreach ($value as $_mkey => $_mval) {
|
||||
$newValue[$_mkey] = $_mval;
|
||||
}
|
||||
} else {
|
||||
$newValue[] = $value;
|
||||
}
|
||||
|
||||
$this->assign($tpl_var, $newValue, $nocache);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* assigns a global Smarty variable
|
||||
*
|
||||
* @param string $varName the global variable name
|
||||
* @param mixed $value the value to assign
|
||||
* @param boolean $nocache if true any output of this variable will be not cached
|
||||
*
|
||||
* @return Data
|
||||
* @deprecated since 5.0
|
||||
*/
|
||||
public function assignGlobal($varName, $value = null, $nocache = false)
|
||||
{
|
||||
trigger_error(__METHOD__ . " is deprecated. Use \\Smarty\\Smarty::assign() to assign a variable " .
|
||||
" at the Smarty level.", E_USER_DEPRECATED);
|
||||
return $this->getSmarty()->assign($varName, $value, $nocache);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a single or all template variables
|
||||
*
|
||||
* @param string $varName variable name or null
|
||||
* @param bool $searchParents include parent templates?
|
||||
*
|
||||
* @return mixed variable value or or array of variables
|
||||
* @api Smarty::getTemplateVars()
|
||||
*
|
||||
*/
|
||||
public function getTemplateVars($varName = null, $searchParents = true)
|
||||
{
|
||||
if (isset($varName)) {
|
||||
return $this->getValue($varName, $searchParents);
|
||||
}
|
||||
|
||||
return array_merge(
|
||||
$this->parent && $searchParents ? $this->parent->getTemplateVars() : [],
|
||||
array_map(function(Variable $var) { return $var->getValue(); }, $this->tpl_vars)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper for ::getVariable()
|
||||
*
|
||||
* @deprecated since 5.0
|
||||
*
|
||||
* @param $varName
|
||||
* @param $searchParents
|
||||
* @param $errorEnable
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function _getVariable($varName, $searchParents = true, $errorEnable = true) {
|
||||
trigger_error('Using ::_getVariable() to is deprecated and will be ' .
|
||||
'removed in a future release. Use getVariable() instead.', E_USER_DEPRECATED);
|
||||
return $this->getVariable($varName, $searchParents, $errorEnable);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the object of a Smarty variable
|
||||
*
|
||||
* @param string $varName the name of the Smarty variable
|
||||
* @param bool $searchParents search also in parent data
|
||||
* @param bool $errorEnable
|
||||
*
|
||||
* @return Variable
|
||||
*/
|
||||
public function getVariable($varName, $searchParents = true, $errorEnable = true) {
|
||||
if (isset($this->tpl_vars[$varName])) {
|
||||
return $this->tpl_vars[$varName];
|
||||
}
|
||||
|
||||
if ($searchParents && $this->parent) {
|
||||
return $this->parent->getVariable($varName, $searchParents, $errorEnable);
|
||||
}
|
||||
|
||||
if ($errorEnable && $this->getSmarty()->error_unassigned) {
|
||||
// force a notice
|
||||
$x = $$varName;
|
||||
}
|
||||
return new UndefinedVariable();
|
||||
}
|
||||
|
||||
/**
|
||||
* Directly sets a complete Variable object in the variable with the given name.
|
||||
* @param $varName
|
||||
* @param Variable $variableObject
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setVariable($varName, Variable $variableObject) {
|
||||
$this->tpl_vars[$varName] = $variableObject;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates if given variable has been set.
|
||||
* @param $varName
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasVariable($varName): bool {
|
||||
return !($this->getVariable($varName, true, false) instanceof UndefinedVariable);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value of the Smarty\Variable given by $varName, or null if the variable does not exist.
|
||||
*
|
||||
* @param $varName
|
||||
* @param bool $searchParents
|
||||
*
|
||||
* @return mixed|null
|
||||
*/
|
||||
public function getValue($varName, $searchParents = true) {
|
||||
$variable = $this->getVariable($varName, $searchParents);
|
||||
return isset($variable) ? $variable->getValue() : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* load config variables into template object
|
||||
*
|
||||
* @param array $new_config_vars
|
||||
*/
|
||||
public function assignConfigVars($new_config_vars, array $sections = []) {
|
||||
|
||||
// copy global config vars
|
||||
foreach ($new_config_vars['vars'] as $variable => $value) {
|
||||
if ($this->getSmarty()->config_overwrite || !isset($this->config_vars[$variable])) {
|
||||
$this->config_vars[$variable] = $value;
|
||||
} else {
|
||||
$this->config_vars[$variable] = array_merge((array)$this->config_vars[$variable], (array)$value);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($sections as $tpl_section) {
|
||||
if (isset($new_config_vars['sections'][$tpl_section])) {
|
||||
foreach ($new_config_vars['sections'][$tpl_section]['vars'] as $variable => $value) {
|
||||
if ($this->getSmarty()->config_overwrite || !isset($this->config_vars[$variable])) {
|
||||
$this->config_vars[$variable] = $value;
|
||||
} else {
|
||||
$this->config_vars[$variable] = array_merge((array)$this->config_vars[$variable], (array)$value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Smarty object
|
||||
*
|
||||
* @return Smarty
|
||||
*/
|
||||
public function getSmarty()
|
||||
{
|
||||
return $this->smarty;
|
||||
}
|
||||
|
||||
/**
|
||||
* clear the given assigned template variable(s).
|
||||
*
|
||||
* @param string|array $tpl_var the template variable(s) to clear
|
||||
*
|
||||
* @return Data
|
||||
*
|
||||
* @api Smarty::clearAssign()
|
||||
*/
|
||||
public function clearAssign($tpl_var)
|
||||
{
|
||||
if (is_array($tpl_var)) {
|
||||
foreach ($tpl_var as $curr_var) {
|
||||
unset($this->tpl_vars[ $curr_var ]);
|
||||
}
|
||||
} else {
|
||||
unset($this->tpl_vars[ $tpl_var ]);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* clear all the assigned template variables.
|
||||
*
|
||||
* @return Data
|
||||
*
|
||||
* @api Smarty::clearAllAssign()
|
||||
*/
|
||||
public function clearAllAssign()
|
||||
{
|
||||
$this->tpl_vars = array();
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* clear a single or all config variables
|
||||
*
|
||||
* @param string|null $name variable name or null
|
||||
*
|
||||
* @return Data
|
||||
*
|
||||
* @api Smarty::clearConfig()
|
||||
*/
|
||||
public function clearConfig($name = null)
|
||||
{
|
||||
if (isset($name)) {
|
||||
unset($this->config_vars[ $name ]);
|
||||
} else {
|
||||
$this->config_vars = array();
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a config variable value
|
||||
*
|
||||
* @param string $varName the name of the config variable
|
||||
*
|
||||
* @return mixed the value of the config variable
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getConfigVariable($varName)
|
||||
{
|
||||
|
||||
if (isset($this->config_vars[$varName])) {
|
||||
return $this->config_vars[$varName];
|
||||
}
|
||||
|
||||
$returnValue = $this->parent ? $this->parent->getConfigVariable($varName) : null;
|
||||
|
||||
if ($returnValue === null && $this->getSmarty()->error_unassigned) {
|
||||
throw new Exception("Undefined variable $varName");
|
||||
}
|
||||
|
||||
return $returnValue;
|
||||
}
|
||||
|
||||
public function hasConfigVariable($varName): bool {
|
||||
try {
|
||||
return $this->getConfigVariable($varName) !== null;
|
||||
} catch (Exception $e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a single or all config variables
|
||||
*
|
||||
* @param string $varname variable name or null
|
||||
*
|
||||
* @return mixed variable value or or array of variables
|
||||
* @throws Exception
|
||||
*
|
||||
* @api Smarty::getConfigVars()
|
||||
*/
|
||||
public function getConfigVars($varname = null)
|
||||
{
|
||||
if (isset($varname)) {
|
||||
return $this->getConfigVariable($varname);
|
||||
}
|
||||
|
||||
return array_merge($this->parent ? $this->parent->getConfigVars() : [], $this->config_vars);
|
||||
}
|
||||
|
||||
/**
|
||||
* load a config file, optionally load just selected sections
|
||||
*
|
||||
* @param string $config_file filename
|
||||
* @param mixed $sections array of section names, single
|
||||
* section or null
|
||||
|
||||
* @returns $this
|
||||
* @throws \Exception
|
||||
*
|
||||
* @api Smarty::configLoad()
|
||||
*/
|
||||
public function configLoad($config_file, $sections = null)
|
||||
{
|
||||
$template = $this->getSmarty()->doCreateTemplate($config_file, null, null, $this, null, null, true);
|
||||
$template->caching = Smarty::CACHING_OFF;
|
||||
$template->assign('sections', (array) $sections ?? []);
|
||||
// trigger a call to $this->assignConfigVars
|
||||
$template->fetch();
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the default scope for new variables assigned in this template.
|
||||
* @param int $scope
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function setDefaultScope(int $scope) {
|
||||
$this->defaultScope = $scope;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the default scope for new variables assigned in this template.
|
||||
* @return int
|
||||
*/
|
||||
public function getDefaultScope(): int {
|
||||
return $this->defaultScope;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Data|Smarty|null
|
||||
*/
|
||||
public function getParent() {
|
||||
return $this->parent;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Data|Smarty|null $parent
|
||||
*/
|
||||
public function setParent($parent): void {
|
||||
$this->parent = $parent;
|
||||
}
|
||||
|
||||
public function pushStack(): void {
|
||||
$stackList = [];
|
||||
foreach ($this->tpl_vars as $name => $variable) {
|
||||
$stackList[$name] = clone $variable; // variables are stored in Variable objects
|
||||
}
|
||||
$this->_var_stack[] = $this->tpl_vars;
|
||||
$this->tpl_vars = $stackList;
|
||||
|
||||
$this->_config_stack[] = $this->config_vars;
|
||||
}
|
||||
|
||||
public function popStack(): void {
|
||||
$this->tpl_vars = array_pop($this->_var_stack);
|
||||
$this->config_vars = array_pop($this->_config_stack);
|
||||
}
|
||||
}
|
370
core/template/src/Debug.php
Normal file
370
core/template/src/Debug.php
Normal file
|
@ -0,0 +1,370 @@
|
|||
<?php
|
||||
|
||||
namespace Smarty;
|
||||
|
||||
/**
|
||||
* Smarty Internal Plugin Debug
|
||||
* Class to collect data for the Smarty Debugging Console
|
||||
*
|
||||
* @author Uwe Tews
|
||||
*/
|
||||
class Debug extends Data
|
||||
{
|
||||
/**
|
||||
* template data
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $template_data = [];
|
||||
|
||||
/**
|
||||
* List of uid's which shall be ignored
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $ignore_uid = [];
|
||||
|
||||
/**
|
||||
* Index of display() and fetch() calls
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public $index = 0;
|
||||
|
||||
/**
|
||||
* Counter for window offset
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public $offset = 0;
|
||||
|
||||
/**
|
||||
* Start logging template
|
||||
*
|
||||
* @param Template $template template
|
||||
* @param null $mode true: display false: fetch null: subtemplate
|
||||
*/
|
||||
public function start_template(Template $template, $mode = null)
|
||||
{
|
||||
if (isset($mode) && !$template->_isSubTpl()) {
|
||||
$this->index++;
|
||||
$this->offset++;
|
||||
$this->template_data[ $this->index ] = null;
|
||||
}
|
||||
$key = $this->get_key($template);
|
||||
$this->template_data[ $this->index ][ $key ][ 'start_template_time' ] = microtime(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* End logging of cache time
|
||||
*
|
||||
* @param Template $template cached template
|
||||
*/
|
||||
public function end_template(Template $template)
|
||||
{
|
||||
$key = $this->get_key($template);
|
||||
$this->template_data[ $this->index ][ $key ][ 'total_time' ] +=
|
||||
microtime(true) - $this->template_data[ $this->index ][ $key ][ 'start_template_time' ];
|
||||
}
|
||||
|
||||
/**
|
||||
* Start logging of compile time
|
||||
*
|
||||
* @param Template $template
|
||||
*/
|
||||
public function start_compile(Template $template)
|
||||
{
|
||||
static $_is_stringy = array('string' => true, 'eval' => true);
|
||||
if (!empty($template->getCompiler()->trace_uid)) {
|
||||
$key = $template->getCompiler()->trace_uid;
|
||||
if (!isset($this->template_data[ $this->index ][ $key ])) {
|
||||
$this->saveTemplateData($_is_stringy, $template, $key);
|
||||
}
|
||||
} else {
|
||||
if (isset($this->ignore_uid[ $template->getSource()->uid ])) {
|
||||
return;
|
||||
}
|
||||
$key = $this->get_key($template);
|
||||
}
|
||||
$this->template_data[ $this->index ][ $key ][ 'start_time' ] = microtime(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* End logging of compile time
|
||||
*
|
||||
* @param Template $template
|
||||
*/
|
||||
public function end_compile(Template $template)
|
||||
{
|
||||
if (!empty($template->getCompiler()->trace_uid)) {
|
||||
$key = $template->getCompiler()->trace_uid;
|
||||
} else {
|
||||
if (isset($this->ignore_uid[ $template->getSource()->uid ])) {
|
||||
return;
|
||||
}
|
||||
$key = $this->get_key($template);
|
||||
}
|
||||
$this->template_data[ $this->index ][ $key ][ 'compile_time' ] +=
|
||||
microtime(true) - $this->template_data[ $this->index ][ $key ][ 'start_time' ];
|
||||
}
|
||||
|
||||
/**
|
||||
* Start logging of render time
|
||||
*
|
||||
* @param Template $template
|
||||
*/
|
||||
public function start_render(Template $template)
|
||||
{
|
||||
$key = $this->get_key($template);
|
||||
$this->template_data[ $this->index ][ $key ][ 'start_time' ] = microtime(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* End logging of compile time
|
||||
*
|
||||
* @param Template $template
|
||||
*/
|
||||
public function end_render(Template $template)
|
||||
{
|
||||
$key = $this->get_key($template);
|
||||
$this->template_data[ $this->index ][ $key ][ 'render_time' ] +=
|
||||
microtime(true) - $this->template_data[ $this->index ][ $key ][ 'start_time' ];
|
||||
}
|
||||
|
||||
/**
|
||||
* Start logging of cache time
|
||||
*
|
||||
* @param Template $template cached template
|
||||
*/
|
||||
public function start_cache(Template $template)
|
||||
{
|
||||
$key = $this->get_key($template);
|
||||
$this->template_data[ $this->index ][ $key ][ 'start_time' ] = microtime(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* End logging of cache time
|
||||
*
|
||||
* @param Template $template cached template
|
||||
*/
|
||||
public function end_cache(Template $template)
|
||||
{
|
||||
$key = $this->get_key($template);
|
||||
$this->template_data[ $this->index ][ $key ][ 'cache_time' ] +=
|
||||
microtime(true) - $this->template_data[ $this->index ][ $key ][ 'start_time' ];
|
||||
}
|
||||
|
||||
/**
|
||||
* Register template object
|
||||
*
|
||||
* @param Template $template cached template
|
||||
*/
|
||||
public function register_template(Template $template)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Register data object
|
||||
*
|
||||
* @param Data $data data object
|
||||
*/
|
||||
public static function register_data(Data $data)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens a window for the Smarty Debugging Console and display the data
|
||||
*
|
||||
* @param Template|Smarty $obj object to debug
|
||||
* @param bool $full
|
||||
*
|
||||
* @throws \Exception
|
||||
* @throws Exception
|
||||
*/
|
||||
public function display_debug($obj, bool $full = false)
|
||||
{
|
||||
if (!$full) {
|
||||
$this->offset++;
|
||||
$savedIndex = $this->index;
|
||||
$this->index = 9999;
|
||||
}
|
||||
$smarty = $obj->getSmarty();
|
||||
// create fresh instance of smarty for displaying the debug console
|
||||
// to avoid problems if the application did overload the Smarty class
|
||||
$debObj = new Smarty();
|
||||
// copy the working dirs from application
|
||||
$debObj->setCompileDir($smarty->getCompileDir());
|
||||
$debObj->compile_check = Smarty::COMPILECHECK_ON;
|
||||
$debObj->security_policy = null;
|
||||
$debObj->debugging = false;
|
||||
$debObj->debugging_ctrl = 'NONE';
|
||||
$debObj->error_reporting = E_ALL & ~E_NOTICE;
|
||||
$debObj->debug_tpl = $smarty->debug_tpl ?? 'file:' . __DIR__ . '/debug.tpl';
|
||||
$debObj->registered_resources = array();
|
||||
$debObj->escape_html = true;
|
||||
$debObj->caching = Smarty::CACHING_OFF;
|
||||
// prepare information of assigned variables
|
||||
$ptr = $this->get_debug_vars($obj);
|
||||
$_assigned_vars = $ptr->tpl_vars;
|
||||
ksort($_assigned_vars);
|
||||
$_config_vars = $ptr->config_vars;
|
||||
ksort($_config_vars);
|
||||
$debugging = $smarty->debugging;
|
||||
$templateName = $obj->getSource()->type . ':' . $obj->getSource()->name;
|
||||
$displayMode = $debugging === 2 || !$full;
|
||||
$offset = $this->offset * 50;
|
||||
$_template = $debObj->doCreateTemplate($debObj->debug_tpl);
|
||||
if ($obj instanceof Template) {
|
||||
$_template->assign('template_name', $templateName);
|
||||
} elseif ($obj instanceof Smarty || $full) {
|
||||
$_template->assign('template_data', $this->template_data[$this->index]);
|
||||
} else {
|
||||
$_template->assign('template_data', null);
|
||||
}
|
||||
$_template->assign('assigned_vars', $_assigned_vars);
|
||||
$_template->assign('config_vars', $_config_vars);
|
||||
$_template->assign('execution_time', microtime(true) - $smarty->start_time);
|
||||
$_template->assign('targetWindow', $displayMode ? md5("$offset$templateName") : '__Smarty__');
|
||||
$_template->assign('offset', $offset);
|
||||
echo $_template->fetch();
|
||||
if (isset($full)) {
|
||||
$this->index--;
|
||||
}
|
||||
if (!$full) {
|
||||
$this->index = $savedIndex;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursively gets variables from all template/data scopes
|
||||
*
|
||||
* @param \Smarty\Data $obj object to debug
|
||||
*
|
||||
* @return \StdClass
|
||||
*/
|
||||
private function get_debug_vars($obj)
|
||||
{
|
||||
$config_vars = array();
|
||||
foreach ($obj->config_vars as $key => $var) {
|
||||
$config_vars[$key]['value'] = $var;
|
||||
$config_vars[$key]['scope'] = get_class($obj) . ':' . spl_object_id($obj);
|
||||
}
|
||||
$tpl_vars = array();
|
||||
foreach ($obj->tpl_vars as $key => $var) {
|
||||
foreach ($var as $varkey => $varvalue) {
|
||||
if ($varkey === 'value') {
|
||||
$tpl_vars[ $key ][ $varkey ] = $varvalue;
|
||||
} else {
|
||||
if ($varkey === 'nocache') {
|
||||
if ($varvalue === true) {
|
||||
$tpl_vars[ $key ][ $varkey ] = $varvalue;
|
||||
}
|
||||
} else {
|
||||
if ($varkey !== 'scope' || $varvalue !== 0) {
|
||||
$tpl_vars[ $key ][ 'attributes' ][ $varkey ] = $varvalue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
$tpl_vars[$key]['scope'] = get_class($obj) . ':' . spl_object_id($obj);
|
||||
}
|
||||
if (isset($obj->parent)) {
|
||||
$parent = $this->get_debug_vars($obj->parent);
|
||||
foreach ($parent->tpl_vars as $name => $pvar) {
|
||||
if (isset($tpl_vars[ $name ]) && $tpl_vars[ $name ][ 'value' ] === $pvar[ 'value' ]) {
|
||||
$tpl_vars[ $name ][ 'scope' ] = $pvar[ 'scope' ];
|
||||
}
|
||||
}
|
||||
$tpl_vars = array_merge($parent->tpl_vars, $tpl_vars);
|
||||
foreach ($parent->config_vars as $name => $pvar) {
|
||||
if (isset($config_vars[ $name ]) && $config_vars[ $name ][ 'value' ] === $pvar[ 'value' ]) {
|
||||
$config_vars[ $name ][ 'scope' ] = $pvar[ 'scope' ];
|
||||
}
|
||||
}
|
||||
$config_vars = array_merge($parent->config_vars, $config_vars);
|
||||
}
|
||||
return (object)array('tpl_vars' => $tpl_vars, 'config_vars' => $config_vars);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return key into $template_data for template
|
||||
*
|
||||
* @param Template $template template object
|
||||
*
|
||||
* @return string key into $template_data
|
||||
*/
|
||||
private function get_key(Template $template)
|
||||
{
|
||||
static $_is_stringy = array('string' => true, 'eval' => true);
|
||||
|
||||
$key = $template->getSource()->uid;
|
||||
if (isset($this->template_data[ $this->index ][ $key ])) {
|
||||
return $key;
|
||||
} else {
|
||||
$this->saveTemplateData($_is_stringy, $template, $key);
|
||||
$this->template_data[ $this->index ][ $key ][ 'total_time' ] = 0;
|
||||
return $key;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ignore template
|
||||
*
|
||||
* @param Template $template
|
||||
*/
|
||||
public function ignore(Template $template)
|
||||
{
|
||||
$this->ignore_uid[$template->getSource()->uid] = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* handle 'URL' debugging mode
|
||||
*
|
||||
* @param Smarty $smarty
|
||||
*/
|
||||
public function debugUrl(Smarty $smarty)
|
||||
{
|
||||
if (isset($_SERVER[ 'QUERY_STRING' ])) {
|
||||
$_query_string = $_SERVER[ 'QUERY_STRING' ];
|
||||
} else {
|
||||
$_query_string = '';
|
||||
}
|
||||
if (false !== strpos($_query_string, $smarty->smarty_debug_id)) {
|
||||
if (false !== strpos($_query_string, $smarty->smarty_debug_id . '=on')) {
|
||||
// enable debugging for this browser session
|
||||
setcookie('SMARTY_DEBUG', true);
|
||||
$smarty->debugging = true;
|
||||
} elseif (false !== strpos($_query_string, $smarty->smarty_debug_id . '=off')) {
|
||||
// disable debugging for this browser session
|
||||
setcookie('SMARTY_DEBUG', false);
|
||||
$smarty->debugging = false;
|
||||
} else {
|
||||
// enable debugging for this page
|
||||
$smarty->debugging = true;
|
||||
}
|
||||
} else {
|
||||
if (isset($_COOKIE[ 'SMARTY_DEBUG' ])) {
|
||||
$smarty->debugging = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $_is_stringy
|
||||
* @param Template $template
|
||||
* @param string $key
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function saveTemplateData(array $_is_stringy, Template $template, string $key): void {
|
||||
if (isset($_is_stringy[$template->getSource()->type])) {
|
||||
$this->template_data[$this->index][$key]['name'] =
|
||||
'\'' . substr($template->getSource()->name, 0, 25) . '...\'';
|
||||
} else {
|
||||
$this->template_data[$this->index][$key]['name'] = $template->getSource()->getResourceName();
|
||||
}
|
||||
$this->template_data[$this->index][$key]['compile_time'] = 0;
|
||||
$this->template_data[$this->index][$key]['render_time'] = 0;
|
||||
$this->template_data[$this->index][$key]['cache_time'] = 0;
|
||||
}
|
||||
}
|
97
core/template/src/ErrorHandler.php
Normal file
97
core/template/src/ErrorHandler.php
Normal file
|
@ -0,0 +1,97 @@
|
|||
<?php
|
||||
|
||||
namespace Smarty;
|
||||
|
||||
/**
|
||||
* Smarty error handler to fix new error levels in PHP8 for backwards compatibility
|
||||
* @author Simon Wisselink
|
||||
*/
|
||||
class ErrorHandler
|
||||
{
|
||||
/**
|
||||
* Allows {$foo->propName} where propName is undefined.
|
||||
* @var bool
|
||||
*/
|
||||
public $allowUndefinedProperties = true;
|
||||
|
||||
/**
|
||||
* Allows {$foo.bar} where bar is unset and {$foo.bar1.bar2} where either bar1 or bar2 is unset.
|
||||
* @var bool
|
||||
*/
|
||||
public $allowUndefinedArrayKeys = true;
|
||||
|
||||
/**
|
||||
* Allows {$foo->bar} where bar is not an object (e.g. null or false).
|
||||
* @var bool
|
||||
*/
|
||||
public $allowDereferencingNonObjects = true;
|
||||
|
||||
private $previousErrorHandler = null;
|
||||
|
||||
/**
|
||||
* Enable error handler to intercept errors
|
||||
*/
|
||||
public function activate() {
|
||||
/*
|
||||
Error muting is done because some people implemented custom error_handlers using
|
||||
https://php.net/set_error_handler and for some reason did not understand the following paragraph:
|
||||
|
||||
It is important to remember that the standard PHP error handler is completely bypassed for the
|
||||
error types specified by error_types unless the callback function returns FALSE.
|
||||
error_reporting() settings will have no effect and your error handler will be called regardless -
|
||||
however you are still able to read the current value of error_reporting and act appropriately.
|
||||
Of particular note is that this value will be 0 if the statement that caused the error was
|
||||
prepended by the @ error-control operator.
|
||||
*/
|
||||
$this->previousErrorHandler = set_error_handler([$this, 'handleError']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable error handler
|
||||
*/
|
||||
public function deactivate() {
|
||||
restore_error_handler();
|
||||
$this->previousErrorHandler = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Error Handler to mute expected messages
|
||||
*
|
||||
* @link https://php.net/set_error_handler
|
||||
*
|
||||
* @param integer $errno Error level
|
||||
* @param $errstr
|
||||
* @param $errfile
|
||||
* @param $errline
|
||||
* @param $errcontext
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function handleError($errno, $errstr, $errfile, $errline, $errcontext = [])
|
||||
{
|
||||
if ($this->allowUndefinedProperties && preg_match(
|
||||
'/^(Undefined property)/',
|
||||
$errstr
|
||||
)) {
|
||||
return; // suppresses this error
|
||||
}
|
||||
|
||||
if ($this->allowUndefinedArrayKeys && preg_match(
|
||||
'/^(Undefined index|Undefined array key|Trying to access array offset on)/',
|
||||
$errstr
|
||||
)) {
|
||||
return; // suppresses this error
|
||||
}
|
||||
|
||||
if ($this->allowDereferencingNonObjects && preg_match(
|
||||
'/^Attempt to read property ".+?" on/',
|
||||
$errstr
|
||||
)) {
|
||||
return; // suppresses this error
|
||||
}
|
||||
|
||||
// pass all other errors through to the previous error handler or to the default PHP error handler
|
||||
return $this->previousErrorHandler ?
|
||||
call_user_func($this->previousErrorHandler, $errno, $errstr, $errfile, $errline, $errcontext) : false;
|
||||
}
|
||||
}
|
16
core/template/src/Exception.php
Normal file
16
core/template/src/Exception.php
Normal file
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
|
||||
namespace Smarty;
|
||||
|
||||
/**
|
||||
* Smarty exception class
|
||||
*/
|
||||
class Exception extends \Exception {
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function __toString() {
|
||||
return ' --> Smarty: ' . $this->message . ' <-- ';
|
||||
}
|
||||
}
|
229
core/template/src/Extension/BCPluginsAdapter.php
Normal file
229
core/template/src/Extension/BCPluginsAdapter.php
Normal file
|
@ -0,0 +1,229 @@
|
|||
<?php
|
||||
|
||||
namespace Smarty\Extension;
|
||||
|
||||
use Smarty\BlockHandler\BlockPluginWrapper;
|
||||
use Smarty\Compile\CompilerInterface;
|
||||
use Smarty\Compile\Modifier\BCPluginWrapper as ModifierCompilerPluginWrapper;
|
||||
use Smarty\Compile\Tag\BCPluginWrapper as TagPluginWrapper;
|
||||
use Smarty\Filter\FilterPluginWrapper;
|
||||
use Smarty\FunctionHandler\BCPluginWrapper as FunctionPluginWrapper;
|
||||
|
||||
class BCPluginsAdapter extends Base {
|
||||
|
||||
/**
|
||||
* @var \Smarty\Smarty
|
||||
*/
|
||||
private $smarty;
|
||||
|
||||
public function __construct(\Smarty\Smarty $smarty) {
|
||||
$this->smarty = $smarty;
|
||||
}
|
||||
|
||||
private function findPlugin($type, $name): ?array {
|
||||
if (null !== $plugin = $this->smarty->getRegisteredPlugin($type, $name)) {
|
||||
return $plugin;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getTagCompiler(string $tag): ?\Smarty\Compile\CompilerInterface {
|
||||
|
||||
$plugin = $this->findPlugin(\Smarty\Smarty::PLUGIN_COMPILER, $tag);
|
||||
if ($plugin === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (is_callable($plugin[0])) {
|
||||
$callback = $plugin[0];
|
||||
$cacheable = (bool) $plugin[1] ?? true;
|
||||
return new TagPluginWrapper($callback, $cacheable);
|
||||
} elseif (class_exists($plugin[0])) {
|
||||
$compiler = new $plugin[0];
|
||||
if ($compiler instanceof CompilerInterface) {
|
||||
return $compiler;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getFunctionHandler(string $functionName): ?\Smarty\FunctionHandler\FunctionHandlerInterface {
|
||||
$plugin = $this->findPlugin(\Smarty\Smarty::PLUGIN_FUNCTION, $functionName);
|
||||
if ($plugin === null) {
|
||||
return null;
|
||||
}
|
||||
$callback = $plugin[0];
|
||||
$cacheable = (bool) $plugin[1] ?? true;
|
||||
|
||||
return new FunctionPluginWrapper($callback, $cacheable);
|
||||
|
||||
}
|
||||
|
||||
public function getBlockHandler(string $blockTagName): ?\Smarty\BlockHandler\BlockHandlerInterface {
|
||||
$plugin = $this->findPlugin(\Smarty\Smarty::PLUGIN_BLOCK, $blockTagName);
|
||||
if ($plugin === null) {
|
||||
return null;
|
||||
}
|
||||
$callback = $plugin[0];
|
||||
$cacheable = (bool) $plugin[1] ?? true;
|
||||
|
||||
return new BlockPluginWrapper($callback, $cacheable);
|
||||
}
|
||||
|
||||
public function getModifierCallback(string $modifierName) {
|
||||
|
||||
$plugin = $this->findPlugin(\Smarty\Smarty::PLUGIN_MODIFIER, $modifierName);
|
||||
if ($plugin === null) {
|
||||
return null;
|
||||
}
|
||||
return $plugin[0];
|
||||
}
|
||||
|
||||
public function getModifierCompiler(string $modifier): ?\Smarty\Compile\Modifier\ModifierCompilerInterface {
|
||||
$plugin = $this->findPlugin(\Smarty\Smarty::PLUGIN_MODIFIERCOMPILER, $modifier);
|
||||
if ($plugin === null) {
|
||||
return null;
|
||||
}
|
||||
$callback = $plugin[0];
|
||||
|
||||
return new ModifierCompilerPluginWrapper($callback);
|
||||
}
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $preFilters = [];
|
||||
|
||||
public function getPreFilters(): array {
|
||||
return $this->preFilters;
|
||||
}
|
||||
|
||||
public function addPreFilter(\Smarty\Filter\FilterInterface $filter) {
|
||||
$this->preFilters[] = $filter;
|
||||
}
|
||||
|
||||
public function addCallableAsPreFilter(callable $callable, ?string $name = null) {
|
||||
if ($name === null) {
|
||||
$this->preFilters[] = new FilterPluginWrapper($callable);
|
||||
} else {
|
||||
$this->preFilters[$name] = new FilterPluginWrapper($callable);
|
||||
}
|
||||
}
|
||||
|
||||
public function removePrefilter(string $name) {
|
||||
unset($this->preFilters[$name]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $postFilters = [];
|
||||
|
||||
public function getPostFilters(): array {
|
||||
return $this->postFilters;
|
||||
}
|
||||
|
||||
public function addPostFilter(\Smarty\Filter\FilterInterface $filter) {
|
||||
$this->postFilters[] = $filter;
|
||||
}
|
||||
|
||||
public function addCallableAsPostFilter(callable $callable, ?string $name = null) {
|
||||
if ($name === null) {
|
||||
$this->postFilters[] = new FilterPluginWrapper($callable);
|
||||
} else {
|
||||
$this->postFilters[$name] = new FilterPluginWrapper($callable);
|
||||
}
|
||||
}
|
||||
|
||||
public function removePostFilter(string $name) {
|
||||
unset($this->postFilters[$name]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $outputFilters = [];
|
||||
|
||||
public function getOutputFilters(): array {
|
||||
return $this->outputFilters;
|
||||
}
|
||||
|
||||
public function addOutputFilter(\Smarty\Filter\FilterInterface $filter) {
|
||||
$this->outputFilters[] = $filter;
|
||||
}
|
||||
|
||||
public function addCallableAsOutputFilter(callable $callable, ?string $name = null) {
|
||||
if ($name === null) {
|
||||
$this->outputFilters[] = new FilterPluginWrapper($callable);
|
||||
} else {
|
||||
$this->outputFilters[$name] = new FilterPluginWrapper($callable);
|
||||
}
|
||||
}
|
||||
|
||||
public function removeOutputFilter(string $name) {
|
||||
unset($this->outputFilters[$name]);
|
||||
}
|
||||
|
||||
public function loadPluginsFromDir(string $path) {
|
||||
foreach([
|
||||
'function',
|
||||
'modifier',
|
||||
'block',
|
||||
'compiler',
|
||||
'prefilter',
|
||||
'postfilter',
|
||||
'outputfilter',
|
||||
'modifiercompiler',
|
||||
] as $type) {
|
||||
foreach (glob($path . $type . '.?*.php') as $filename) {
|
||||
$pluginName = $this->getPluginNameFromFilename($filename);
|
||||
if ($pluginName !== null) {
|
||||
require_once $filename;
|
||||
$functionOrClassName = 'smarty_' . $type . '_' . $pluginName;
|
||||
if (function_exists($functionOrClassName) || class_exists($functionOrClassName)) {
|
||||
$this->smarty->registerPlugin($type, $pluginName, $functionOrClassName, true, []);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$type = 'resource';
|
||||
foreach (glob($path . $type . '.?*.php') as $filename) {
|
||||
$pluginName = $this->getPluginNameFromFilename($filename);
|
||||
if ($pluginName !== null) {
|
||||
require_once $filename;
|
||||
if (class_exists($className = 'smarty_' . $type . '_' . $pluginName)) {
|
||||
$this->smarty->registerResource($pluginName, new $className());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$type = 'cacheresource';
|
||||
foreach (glob($path . $type . '.?*.php') as $filename) {
|
||||
$pluginName = $this->getPluginNameFromFilename($filename);
|
||||
if ($pluginName !== null) {
|
||||
require_once $filename;
|
||||
if (class_exists($className = 'smarty_' . $type . '_' . $pluginName)) {
|
||||
$this->smarty->registerCacheResource($pluginName, new $className());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $filename
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
private function getPluginNameFromFilename($filename) {
|
||||
if (!preg_match('/.*\.([a-z_A-Z0-9]+)\.php$/',$filename,$matches)) {
|
||||
return null;
|
||||
}
|
||||
return $matches[1];
|
||||
}
|
||||
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue