WIP rewriting smarty_internal_method_*

This commit is contained in:
Simon Wisselink
2022-12-21 14:01:18 +01:00
parent 6ba059dea8
commit 5cac5e42f1
36 changed files with 1100 additions and 1892 deletions

2
TODO.txt Normal file
View File

@@ -0,0 +1,2 @@
- [ ] find ->ext-> calls, rewrite and remove first function call param
- [ ] review usages of ->_getSmartyObj and ->smarty: maybe change this so we can hide more

View File

@@ -102,9 +102,9 @@ class PrivateSpecialVariable extends Base {
// no break
case 'config':
if (isset($_index[2])) {
return "(is_array(\$tmp = \$_smarty_tpl->smarty->ext->configload->_getConfigVariable(\$_smarty_tpl, $_index[1])) ? \$tmp[$_index[2]] : null)";
return "(is_array(\$tmp = \$_smarty_tpl->getConfigVariable($_index[1])) ? \$tmp[$_index[2]] : null)";
} else {
return "\$_smarty_tpl->smarty->ext->configload->_getConfigVariable(\$_smarty_tpl, $_index[1])";
return "\$_smarty_tpl->getConfigVariable($_index[1])";
}
// no break
case 'ldelim':

View File

@@ -533,14 +533,12 @@ class Template {
// not a variable variable
$var = trim($variable, '\'');
$this->tag_nocache = $this->tag_nocache |
$this->template->ext->getTemplateVars->_getVariable(
$this->template,
$this->template->_getVariable(
$var,
null,
true,
false
)->nocache;
// todo $this->template->compiled->properties['variables'][$var] = $this->tag_nocache | $this->nocache;
}
return '$_smarty_tpl->tpl_vars[' . $variable . ']->value';
}
@@ -554,7 +552,7 @@ class Template {
*/
public function compileConfigVariable($variable) {
// return '$_smarty_tpl->config_vars[' . $variable . ']';
return '$_smarty_tpl->smarty->ext->configLoad->_getConfigVariable($_smarty_tpl, ' . $variable . ')';
return '$_smarty_tpl->getConfigVariable(' . $variable . ')';
}
/**

View File

@@ -13,27 +13,17 @@ namespace Smarty;
/**
* Base class with template and variable methods
*
* @package Smarty
* @subpackage Template
*
* @property int $scope
* @property Smarty $smarty
* The following methods will be dynamically loaded by the extension handler when they are called.
* They are located in a corresponding Smarty_Internal_Method_xxxx class
*
* @method mixed _getConfigVariable(string $varName, bool $errorEnable = true)
* @method mixed getConfigVariable(string $varName, bool $errorEnable = true)
* @method mixed getConfigVars(string $varName = null, bool $searchParents = true)
* @method mixed getGlobal(string $varName = null)
* @method mixed getStreamVariable(string $variable)
* @method Data clearAssign(mixed $tpl_var)
* @method Data clearAllAssign()
* @method Data clearConfig(string $varName = null)
* @method Data configLoad(string $config_file, mixed $sections = null, string $scope = 'local')
*/
abstract class Data
{
/**
* Global smarty instance
*
* @var Smarty
*/
public $smarty = null;
/**
* This object type (Smarty = 1, template = 2, data = 4)
*
@@ -129,15 +119,47 @@ abstract class Data
* @param bool $nocache if true any output of this variable will
* be not cached
*
* @return Data|\Smarty_Internal_Template|\Smarty
*@link https://www.smarty.net/docs/en/api.append.tpl
* @return Data
* @link https://www.smarty.net/docs/en/api.append.tpl
*
* @api Smarty::append()
*/
public function append($tpl_var, $value = null, $merge = false, $nocache = false)
{
return $this->ext->append->append($this, $tpl_var, $value, $merge, $nocache);
}
public function append($tpl_var, $value = null, $merge = false, $nocache = false)
{
if (is_array($tpl_var)) {
// $tpl_var is an array, ignore $value
foreach ($tpl_var as $_key => $_val) {
if ($_key !== '') {
$this->append($this, $_key, $_val, $merge, $nocache);
}
}
} else {
if ($tpl_var !== '' && isset($value)) {
if (!isset($this->tpl_vars[ $tpl_var ])) {
$tpl_var_inst = $this->_getVariable($tpl_var, null, true, false);
if ($tpl_var_inst instanceof Smarty_Undefined_Variable) {
$this->tpl_vars[ $tpl_var ] = new \Smarty\Variable(null, $nocache);
} else {
$this->tpl_vars[ $tpl_var ] = clone $tpl_var_inst;
}
}
if (!(is_array($this->tpl_vars[ $tpl_var ]->value)
|| $this->tpl_vars[ $tpl_var ]->value instanceof ArrayAccess)
) {
settype($this->tpl_vars[ $tpl_var ]->value, 'array');
}
if ($merge && is_array($value)) {
foreach ($value as $_mkey => $_mval) {
$this->tpl_vars[ $tpl_var ]->value[ $_mkey ] = $_mval;
}
} else {
$this->tpl_vars[ $tpl_var ]->value[] = $value;
}
}
$this->_updateScope($tpl_var);
}
return $this;
}
/**
* assigns a global Smarty variable
@@ -146,11 +168,19 @@ abstract class Data
* @param mixed $value the value to assign
* @param boolean $nocache if true any output of this variable will be not cached
*
* @return Data|\Smarty_Internal_Template|\Smarty
* @return Data
*/
public function assignGlobal($varName, $value = null, $nocache = false)
{
return $this->ext->assignGlobal->assignGlobal($this, $varName, $value, $nocache);
if ($varName !== '') {
Smarty::$global_tpl_vars[ $varName ] = new \Smarty\Variable($value, $nocache);
$ptr = $this;
while ($ptr->_isTplObj()) {
$ptr->tpl_vars[ $varName ] = clone Smarty::$global_tpl_vars[ $varName ];
$ptr = $ptr->parent;
}
}
return $this;
}
/**
@@ -160,12 +190,28 @@ abstract class Data
* @param mixed &$value the referenced value to append
* @param boolean $merge flag if array elements shall be merged
*
* @return Data|\Smarty_Internal_Template|\Smarty
* @return Data
*/
public function appendByRef($tpl_var, &$value, $merge = false)
{
return $this->ext->appendByRef->appendByRef($this, $tpl_var, $value, $merge);
}
public function appendByRef($tpl_var, &$value, $merge = false)
{
if ($tpl_var !== '' && isset($value)) {
if (!isset($this->tpl_vars[ $tpl_var ])) {
$this->tpl_vars[ $tpl_var ] = new \Smarty\Variable();
}
if (!is_array($this->tpl_vars[ $tpl_var ]->value)) {
settype($this->tpl_vars[ $tpl_var ]->value, 'array');
}
if ($merge && is_array($value)) {
foreach ($value as $_key => $_val) {
$this->tpl_vars[ $tpl_var ]->value[ $_key ] = &$value[ $_key ];
}
} else {
$this->tpl_vars[ $tpl_var ]->value[] = &$value;
}
$this->_updateScope($tpl_var);
}
return $this;
}
/**
* assigns values to template variables by reference
@@ -174,18 +220,26 @@ abstract class Data
* @param $value
* @param boolean $nocache if true any output of this variable will be not cached
*
* @return Data|\Smarty_Internal_Template|\Smarty
* @return Data
*/
public function assignByRef($tpl_var, &$value, $nocache = false)
{
return $this->ext->assignByRef->assignByRef($this, $tpl_var, $value, $nocache);
}
public function assignByRef($tpl_var, &$value, $nocache)
{
if ($tpl_var !== '') {
$this->tpl_vars[ $tpl_var ] = new \Smarty\Variable(null, $nocache);
$this->tpl_vars[ $tpl_var ]->value = &$value;
$this->_updateScope($tpl_var);
}
return $this;
}
protected function _updateScope($varName, $tagScope = 0) {
// implemented in Smarty_Internal_Template only
}
/**
* Returns a single or all template variables
*
* @param string $varName variable name or null
* @param Data|\Smarty_Internal_Template|\Smarty $_ptr optional pointer to data object
* @param bool $searchParents include parent templates?
*
* @return mixed variable value or or array of variables
@@ -195,9 +249,84 @@ abstract class Data
*/
public function getTemplateVars($varName = null, Data $_ptr = null, $searchParents = true)
{
return $this->ext->getTemplateVars->getTemplateVars($this, $varName, $_ptr, $searchParents);
if (isset($varName)) {
$_var = $this->_getVariable($varName, $_ptr, $searchParents, false);
if (is_object($_var)) {
return $_var->value;
} else {
return null;
}
} else {
$_result = array();
if ($_ptr === null) {
$_ptr = $this;
}
while ($_ptr !== null) {
foreach ($_ptr->tpl_vars as $key => $var) {
if (!array_key_exists($key, $_result)) {
$_result[ $key ] = $var->value;
}
}
// not found, try at parent
if ($searchParents && isset($_ptr->parent)) {
$_ptr = $_ptr->parent;
} else {
$_ptr = null;
}
}
if ($searchParents && isset(Smarty::$global_tpl_vars)) {
foreach (Smarty::$global_tpl_vars as $key => $var) {
if (!array_key_exists($key, $_result)) {
$_result[ $key ] = $var->value;
}
}
}
return $_result;
}
}
/**
* gets the object of a Smarty variable
*
* @param string $varName the name of the Smarty variable
* @param Data|null $_ptr optional pointer to data object
* @param bool $searchParents search also in parent data
* @param bool $errorEnable
*
* @return Variable
*/
public function _getVariable(
$varName,
Data $_ptr = null,
$searchParents = true,
$errorEnable = true
) {
if ($_ptr === null) {
$_ptr = $this;
}
while ($_ptr !== null) {
if (isset($_ptr->tpl_vars[ $varName ])) {
// found it, return it
return $_ptr->tpl_vars[ $varName ];
}
// not found, try at parent
if ($searchParents && isset($_ptr->parent)) {
$_ptr = $_ptr->parent;
} else {
$_ptr = null;
}
}
if (isset(Smarty::$global_tpl_vars[ $varName ])) {
// found it, return it
return Smarty::$global_tpl_vars[ $varName ];
}
if ($errorEnable && $this->_getSmartyObj()->error_unassigned) {
// force a notice
$x = $$varName;
}
return new Smarty_Undefined_Variable;
}
/**
* Follow the parent chain an merge template and config variables
*
@@ -262,4 +391,229 @@ abstract class Data
{
return $this->ext->_callExternalMethod($this, $name, $args);
}
/**
* clear the given assigned template variable(s).
*
* @param string|array $tpl_var the template variable(s) to clear
*
* @return Data
* @link https://www.smarty.net/docs/en/api.clear.assign.tpl
*
* @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
* @link https://www.smarty.net/docs/en/api.clear.all.assign.tpl
*
* @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
* @link https://www.smarty.net/docs/en/api.clear.config.tpl
*
* @api Smarty::clearConfig()
*/
public function clearConfig($name = null)
{
if (isset($name)) {
unset($this->config_vars[ $name ]);
} else {
$this->config_vars = array();
}
return $this;
}
/**
* 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
*
* @return Data
* @throws \Exception
*@api Smarty::configLoad()
* @link https://www.smarty.net/docs/en/api.config.load.tpl
*
*/
public function configLoad($config_file, $sections = null)
{
$this->_loadConfigFile($this, $config_file, $sections, null);
return $this;
}
/**
* 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
* @param int $scope scope into which config variables
* shall be loaded
*
* @throws \Exception
*@link https://www.smarty.net/docs/en/api.config.load.tpl
*
* @api Smarty::configLoad()
*/
public function _loadConfigFile($config_file, $sections = null, $scope = 0)
{
/* @var \Smarty $smarty */
$smarty = $this->_getSmartyObj();
/* @var \Smarty_Internal_Template $confObj */
$confObj = new Smarty_Internal_Template($config_file, $smarty, $this, null, null, null, null, true);
$confObj->caching = Smarty::CACHING_OFF;
$confObj->source->config_sections = $sections;
$confObj->source->scope = $scope;
$confObj->compiled = Smarty_Template_Compiled::load($confObj);
$confObj->compiled->render($confObj);
if ($this->_isTplObj()) {
$this->compiled->file_dependency[ $confObj->source->uid ] =
array($confObj->source->filepath, $confObj->source->getTimeStamp(), $confObj->source->type);
}
}
/**
* gets a config variable value
*
* @param string $varName the name of the config variable
* @param bool $errorEnable
*
* @return null|string the value of the config variable
*/
public function getConfigVariable($varName = null, $errorEnable = true)
{
$_ptr = $this;
while ($_ptr !== null) {
if (isset($_ptr->config_vars[ $varName ])) {
// found it, return it
return $_ptr->config_vars[ $varName ];
}
// not found, try at parent
$_ptr = $_ptr->parent;
}
if ($this->smarty->error_unassigned && $errorEnable) {
// force a notice
$x = $$varName;
}
return null;
}
/**
* Returns a single or all config variables
*
* @api Smarty::getConfigVars()
* @link https://www.smarty.net/docs/en/api.get.config.vars.tpl
*
* @param string $varname variable name or null
* @param bool $search_parents include parent templates?
*
* @return mixed variable value or or array of variables
*/
public function getConfigVars($varname = null, $search_parents = true)
{
$_ptr = $this;
$var_array = array();
while ($_ptr !== null) {
if (isset($varname)) {
if (isset($_ptr->config_vars[ $varname ])) {
return $_ptr->config_vars[ $varname ];
}
} else {
$var_array = array_merge($_ptr->config_vars, $var_array);
}
// not found, try at parent
if ($search_parents) {
$_ptr = $_ptr->parent;
} else {
$_ptr = null;
}
}
if (isset($varname)) {
return '';
} else {
return $var_array;
}
}
/**
* Returns a single or all global variables
*
* @api Smarty::getGlobal()
*
* @param string $varName variable name or null
*
* @return string|array variable value or or array of variables
*/
public function getGlobal($varName = null)
{
if (isset($varName)) {
if (isset(Smarty::$global_tpl_vars[ $varName ])) {
return Smarty::$global_tpl_vars[ $varName ]->value;
} else {
return '';
}
} else {
$_result = array();
foreach (Smarty::$global_tpl_vars as $key => $var) {
$_result[ $key ] = $var->value;
}
return $_result;
}
}
/**
* gets a stream variable
*
* @api Smarty::getStreamVariable()
*
* @param string $variable the stream of the variable
*
* @return mixed
* @throws \SmartyException
*/
public function getStreamVariable($variable)
{
$_result = '';
$fp = fopen($variable, 'r+');
if ($fp) {
while (!feof($fp) && ($current_line = fgets($fp)) !== false) {
$_result .= $current_line;
}
fclose($fp);
return $_result;
}
$smarty = $this->smarty ?? $this;
if ($smarty->error_unassigned) {
throw new SmartyException('Undefined stream variable "' . $variable . '"');
} else {
return null;
}
}
}

View File

@@ -1,42 +0,0 @@
<?php
/**
* Smarty Method AddDefaultModifiers
*
* Smarty::addDefaultModifiers() method
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*/
class Smarty_Internal_Method_AddDefaultModifiers
{
/**
* Valid for Smarty and template object
*
* @var int
*/
public $objMap = 3;
/**
* Add default modifiers
*
* @api Smarty::addDefaultModifiers()
*
* @param \Smarty_Internal_TemplateBase|\Smarty_Internal_Template|\Smarty $obj
* @param array|string $modifiers modifier or list of modifiers
* to add
*
* @return \Smarty|\Smarty_Internal_Template
*/
public function addDefaultModifiers(Smarty_Internal_TemplateBase $obj, $modifiers)
{
$smarty = $obj->_getSmartyObj();
if (is_array($modifiers)) {
$smarty->default_modifiers = array_merge($smarty->default_modifiers, $modifiers);
} else {
$smarty->default_modifiers[] = $modifiers;
}
return $obj;
}
}

View File

@@ -1,74 +0,0 @@
<?php
/**
* Smarty Method Append
*
* Smarty::append() method
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*/
class Smarty_Internal_Method_Append
{
/**
* Valid for all objects
*
* @var int
*/
public $objMap = 7;
/**
* appends values to template variables
*
* @api Smarty::append()
* @link https://www.smarty.net/docs/en/api.append.tpl
*
* @param \Smarty\Data|\Smarty_Internal_Template|\Smarty $data
* @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 \Smarty\Data|\Smarty_Internal_Template|\Smarty
*/
public function append(\Smarty\Data $data, $tpl_var, $value = null, $merge = false, $nocache = false)
{
if (is_array($tpl_var)) {
// $tpl_var is an array, ignore $value
foreach ($tpl_var as $_key => $_val) {
if ($_key !== '') {
$this->append($data, $_key, $_val, $merge, $nocache);
}
}
} else {
if ($tpl_var !== '' && isset($value)) {
if (!isset($data->tpl_vars[ $tpl_var ])) {
$tpl_var_inst = $data->ext->getTemplateVars->_getVariable($data, $tpl_var, null, true, false);
if ($tpl_var_inst instanceof Smarty_Undefined_Variable) {
$data->tpl_vars[ $tpl_var ] = new \Smarty\Variable(null, $nocache);
} else {
$data->tpl_vars[ $tpl_var ] = clone $tpl_var_inst;
}
}
if (!(is_array($data->tpl_vars[ $tpl_var ]->value)
|| $data->tpl_vars[ $tpl_var ]->value instanceof ArrayAccess)
) {
settype($data->tpl_vars[ $tpl_var ]->value, 'array');
}
if ($merge && is_array($value)) {
foreach ($value as $_mkey => $_mval) {
$data->tpl_vars[ $tpl_var ]->value[ $_mkey ] = $_mval;
}
} else {
$data->tpl_vars[ $tpl_var ]->value[] = $value;
}
}
if ($data->_isTplObj() && $data->scope) {
$data->ext->_updateScope->_updateScope($data, $tpl_var);
}
}
return $data;
}
}

View File

@@ -1,49 +0,0 @@
<?php
/**
* Smarty Method AppendByRef
*
* Smarty::appendByRef() method
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*/
class Smarty_Internal_Method_AppendByRef
{
/**
* appends values to template variables by reference
*
* @api Smarty::appendByRef()
* @link https://www.smarty.net/docs/en/api.append.by.ref.tpl
*
* @param \Smarty\Data|\Smarty_Internal_Template|\Smarty $data
* @param string $tpl_var the template variable name
* @param mixed &$value the referenced value to append
* @param bool $merge flag if array elements shall be merged
*
* @return \Smarty\Data|\Smarty_Internal_Template|\Smarty
*/
public static function appendByRef(\Smarty\Data $data, $tpl_var, &$value, $merge = false)
{
if ($tpl_var !== '' && isset($value)) {
if (!isset($data->tpl_vars[ $tpl_var ])) {
$data->tpl_vars[ $tpl_var ] = new \Smarty\Variable();
}
if (!is_array($data->tpl_vars[ $tpl_var ]->value)) {
settype($data->tpl_vars[ $tpl_var ]->value, 'array');
}
if ($merge && is_array($value)) {
foreach ($value as $_key => $_val) {
$data->tpl_vars[ $tpl_var ]->value[ $_key ] = &$value[ $_key ];
}
} else {
$data->tpl_vars[ $tpl_var ]->value[] = &$value;
}
if ($data->_isTplObj() && $data->scope) {
$data->ext->_updateScope->_updateScope($data, $tpl_var);
}
}
return $data;
}
}

View File

@@ -1,36 +0,0 @@
<?php
/**
* Smarty Method AssignByRef
*
* Smarty::assignByRef() method
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*/
class Smarty_Internal_Method_AssignByRef
{
/**
* assigns values to template variables by reference
*
* @param \Smarty\Data|\Smarty_Internal_Template|\Smarty $data
* @param string $tpl_var the template variable name
* @param $value
* @param boolean $nocache if true any output of this variable will
* be not cached
*
* @return \Smarty\Data|\Smarty_Internal_Template|\Smarty
*/
public function assignByRef(\Smarty\Data $data, $tpl_var, &$value, $nocache)
{
if ($tpl_var !== '') {
$data->tpl_vars[ $tpl_var ] = new \Smarty\Variable(null, $nocache);
$data->tpl_vars[ $tpl_var ]->value = &$value;
if ($data->_isTplObj() && $data->scope) {
$data->ext->_updateScope->_updateScope($data, $tpl_var);
}
}
return $data;
}
}

View File

@@ -1,44 +0,0 @@
<?php
/**
* Smarty Method AssignGlobal
*
* Smarty::assignGlobal() method
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*/
class Smarty_Internal_Method_AssignGlobal
{
/**
* Valid for all objects
*
* @var int
*/
public $objMap = 7;
/**
* assigns a global Smarty variable
*
* @param \Smarty\Data|\Smarty_Internal_Template|\Smarty $data
* @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 \Smarty\Data|\Smarty_Internal_Template|\Smarty
*/
public function assignGlobal(\Smarty\Data $data, $varName, $value = null, $nocache = false)
{
if ($varName !== '') {
\Smarty\Smarty::$global_tpl_vars[ $varName ] = new \Smarty\Variable($value, $nocache);
$ptr = $data;
while ($ptr->_isTplObj()) {
$ptr->tpl_vars[ $varName ] = clone \Smarty\Smarty::$global_tpl_vars[ $varName ];
$ptr = $ptr->parent;
}
}
return $data;
}
}

View File

@@ -1,36 +0,0 @@
<?php
/**
* Smarty Method ClearAllAssign
*
* Smarty::clearAllAssign() method
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*/
class Smarty_Internal_Method_ClearAllAssign
{
/**
* Valid for all objects
*
* @var int
*/
public $objMap = 7;
/**
* clear all the assigned template variables.
*
* @api Smarty::clearAllAssign()
* @link https://www.smarty.net/docs/en/api.clear.all.assign.tpl
*
* @param \Smarty\Data|\Smarty_Internal_Template|\Smarty $data
*
* @return \Smarty\Data|\Smarty_Internal_Template|\Smarty
*/
public function clearAllAssign(\Smarty\Data $data)
{
$data->tpl_vars = array();
return $data;
}
}

View File

@@ -1,41 +0,0 @@
<?php
/**
* Smarty Method ClearAllCache
*
* Smarty::clearAllCache() method
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*/
class Smarty_Internal_Method_ClearAllCache
{
/**
* Valid for Smarty object
*
* @var int
*/
public $objMap = 1;
/**
* Empty cache folder
*
* @api Smarty::clearAllCache()
* @link https://www.smarty.net/docs/en/api.clear.all.cache.tpl
*
* @param \Smarty $smarty
* @param integer $exp_time expiration time
* @param string $type resource type
*
* @return int number of cache files deleted
* @throws \SmartyException
*/
public function clearAllCache(Smarty $smarty, $exp_time = null, $type = null)
{
$smarty->_clearTemplateCache();
// load cache resource and call clearAll
$_cache_resource = \Smarty\Cacheresource\Base::load($smarty, $type);
return $_cache_resource->clearAll($smarty, $exp_time);
}
}

View File

@@ -1,43 +0,0 @@
<?php
/**
* Smarty Method ClearAssign
*
* Smarty::clearAssign() method
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*/
class Smarty_Internal_Method_ClearAssign
{
/**
* Valid for all objects
*
* @var int
*/
public $objMap = 7;
/**
* clear the given assigned template variable(s).
*
* @api Smarty::clearAssign()
* @link https://www.smarty.net/docs/en/api.clear.assign.tpl
*
* @param \Smarty\Data|\Smarty_Internal_Template|\Smarty $data
* @param string|array $tpl_var the template variable(s) to clear
*
* @return \Smarty\Data|\Smarty_Internal_Template|\Smarty
*/
public function clearAssign(\Smarty\Data $data, $tpl_var)
{
if (is_array($tpl_var)) {
foreach ($tpl_var as $curr_var) {
unset($data->tpl_vars[ $curr_var ]);
}
} else {
unset($data->tpl_vars[ $tpl_var ]);
}
return $data;
}
}

View File

@@ -1,50 +0,0 @@
<?php
/**
* Smarty Method ClearCache
*
* Smarty::clearCache() method
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*/
class Smarty_Internal_Method_ClearCache
{
/**
* Valid for Smarty object
*
* @var int
*/
public $objMap = 1;
/**
* Empty cache for a specific template
*
* @api Smarty::clearCache()
* @link https://www.smarty.net/docs/en/api.clear.cache.tpl
*
* @param \Smarty $smarty
* @param string $template_name template name
* @param string $cache_id cache id
* @param string $compile_id compile id
* @param integer $exp_time expiration time
* @param string $type resource type
*
* @return int number of cache files deleted
* @throws \SmartyException
*/
public function clearCache(
Smarty $smarty,
$template_name,
$cache_id = null,
$compile_id = null,
$exp_time = null,
$type = null
) {
$smarty->_clearTemplateCache();
// load cache resource and call clear
$_cache_resource = \Smarty\Cacheresource\Base::load($smarty, $type);
return $_cache_resource->clear($smarty, $template_name, $cache_id, $compile_id, $exp_time);
}
}

View File

@@ -1,131 +0,0 @@
<?php
/**
* Smarty Method ClearCompiledTemplate
*
* Smarty::clearCompiledTemplate() method
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*/
class Smarty_Internal_Method_ClearCompiledTemplate
{
/**
* Valid for Smarty object
*
* @var int
*/
public $objMap = 1;
/**
* Delete compiled template file
*
* @api Smarty::clearCompiledTemplate()
* @link https://www.smarty.net/docs/en/api.clear.compiled.template.tpl
*
* @param \Smarty $smarty
* @param string $resource_name template name
* @param string $compile_id compile id
* @param integer $exp_time expiration time
*
* @return int number of template files deleted
* @throws \SmartyException
*/
public function clearCompiledTemplate(Smarty $smarty, $resource_name = null, $compile_id = null, $exp_time = null)
{
// clear template objects cache
$smarty->_clearTemplateCache();
$_compile_dir = $smarty->getCompileDir();
if ($_compile_dir === '/') { //We should never want to delete this!
return 0;
}
$_compile_id = isset($compile_id) ? preg_replace('![^\w]+!', '_', $compile_id) : null;
$_dir_sep = $smarty->use_sub_dirs ? DIRECTORY_SEPARATOR : '^';
if (isset($resource_name)) {
$_save_stat = $smarty->caching;
$smarty->caching = \Smarty\Smarty::CACHING_OFF;
/* @var Smarty_Internal_Template $tpl */
$tpl = $smarty->createTemplate($resource_name);
$smarty->caching = $_save_stat;
if (!$tpl->source->handler->uncompiled && !$tpl->source->handler->recompiled && $tpl->source->exists) {
$_resource_part_1 = basename(str_replace('^', DIRECTORY_SEPARATOR, $tpl->compiled->filepath));
$_resource_part_1_length = strlen($_resource_part_1);
} else {
return 0;
}
$_resource_part_2 = str_replace('.php', '.cache.php', $_resource_part_1);
$_resource_part_2_length = strlen($_resource_part_2);
}
$_dir = $_compile_dir;
if ($smarty->use_sub_dirs && isset($_compile_id)) {
$_dir .= $_compile_id . $_dir_sep;
}
if (isset($_compile_id)) {
$_compile_id_part = $_compile_dir . $_compile_id . $_dir_sep;
$_compile_id_part_length = strlen($_compile_id_part);
}
$_count = 0;
try {
$_compileDirs = new RecursiveDirectoryIterator($_dir);
// NOTE: UnexpectedValueException thrown for PHP >= 5.3
} catch (Exception $e) {
return 0;
}
$_compile = new RecursiveIteratorIterator($_compileDirs, RecursiveIteratorIterator::CHILD_FIRST);
foreach ($_compile as $_file) {
if (substr(basename($_file->getPathname()), 0, 1) === '.') {
continue;
}
$_filepath = (string)$_file;
if ($_file->isDir()) {
if (!$_compile->isDot()) {
// delete folder if empty
@rmdir($_file->getPathname());
}
} else {
// delete only php files
if (substr($_filepath, -4) !== '.php') {
continue;
}
$unlink = false;
if ((!isset($_compile_id) ||
(isset($_filepath[ $_compile_id_part_length ]) &&
$a = !strncmp($_filepath, $_compile_id_part, $_compile_id_part_length)))
&& (!isset($resource_name) || (isset($_filepath[ $_resource_part_1_length ])
&& substr_compare(
$_filepath,
$_resource_part_1,
-$_resource_part_1_length,
$_resource_part_1_length
) === 0) || (isset($_filepath[ $_resource_part_2_length ])
&& substr_compare(
$_filepath,
$_resource_part_2,
-$_resource_part_2_length,
$_resource_part_2_length
) === 0))
) {
if (isset($exp_time)) {
if (is_file($_filepath) && time() - filemtime($_filepath) >= $exp_time) {
$unlink = true;
}
} else {
$unlink = true;
}
}
if ($unlink && is_file($_filepath) && @unlink($_filepath)) {
$_count++;
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;
}
}

View File

@@ -1,41 +0,0 @@
<?php
/**
* Smarty Method ClearConfig
*
* Smarty::clearConfig() method
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*/
class Smarty_Internal_Method_ClearConfig
{
/**
* Valid for all objects
*
* @var int
*/
public $objMap = 7;
/**
* clear a single or all config variables
*
* @api Smarty::clearConfig()
* @link https://www.smarty.net/docs/en/api.clear.config.tpl
*
* @param \Smarty\Data|\Smarty_Internal_Template|\Smarty $data
* @param string|null $name variable name or null
*
* @return \Smarty\Data|\Smarty_Internal_Template|\Smarty
*/
public function clearConfig(\Smarty\Data $data, $name = null)
{
if (isset($name)) {
unset($data->config_vars[ $name ]);
} else {
$data->config_vars = array();
}
return $data;
}
}

View File

@@ -1,36 +0,0 @@
<?php
/**
* Smarty Method CompileAllConfig
*
* Smarty::compileAllConfig() method
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*/
class Smarty_Internal_Method_CompileAllConfig extends Smarty_Internal_Method_CompileAllTemplates
{
/**
* Compile all config files
*
* @api Smarty::compileAllConfig()
*
* @param \Smarty $smarty passed smarty object
* @param string $extension file extension
* @param bool $force_compile force all to recompile
* @param int $time_limit
* @param int $max_errors
*
* @return int number of template files recompiled
*/
public function compileAllConfig(
Smarty $smarty,
$extension = '.conf',
$force_compile = false,
$time_limit = 0,
$max_errors = null
) {
return $this->compileAll($smarty, $extension, $force_compile, $time_limit, $max_errors, true);
}
}

View File

@@ -1,130 +0,0 @@
<?php
/**
* Smarty Method CompileAllTemplates
*
* Smarty::compileAllTemplates() method
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*/
class Smarty_Internal_Method_CompileAllTemplates
{
/**
* Valid for Smarty object
*
* @var int
*/
public $objMap = 1;
/**
* Compile all template files
*
* @api Smarty::compileAllTemplates()
*
* @param \Smarty $smarty passed smarty object
* @param string $extension file extension
* @param bool $force_compile force all to recompile
* @param int $time_limit
* @param int $max_errors
*
* @return integer number of template files recompiled
*/
public function compileAllTemplates(
Smarty $smarty,
$extension = '.tpl',
$force_compile = false,
$time_limit = 0,
$max_errors = null
) {
return $this->compileAll($smarty, $extension, $force_compile, $time_limit, $max_errors);
}
/**
* Compile all template or config files
*
* @param \Smarty $smarty
* @param string $extension template file name extension
* @param bool $force_compile force all to recompile
* @param int $time_limit set maximum execution time
* @param int $max_errors set maximum allowed errors
* @param bool $isConfig flag true if called for config files
*
* @return int number of template files compiled
*/
protected function compileAll(
Smarty $smarty,
$extension,
$force_compile,
$time_limit,
$max_errors,
$isConfig = false
) {
// switch off time limit
if (function_exists('set_time_limit')) {
@set_time_limit($time_limit);
}
$_count = 0;
$_error_count = 0;
$sourceDir = $isConfig ? $smarty->getConfigDir() : $smarty->getTemplateDir();
// loop over array of source directories
foreach ($sourceDir as $_dir) {
$_dir_1 = new RecursiveDirectoryIterator(
$_dir,
defined('FilesystemIterator::FOLLOW_SYMLINKS') ?
FilesystemIterator::FOLLOW_SYMLINKS : 0
);
$_dir_2 = new RecursiveIteratorIterator($_dir_1);
foreach ($_dir_2 as $_fileinfo) {
$_file = $_fileinfo->getFilename();
if (substr(basename($_fileinfo->getPathname()), 0, 1) === '.' || strpos($_file, '.svn') !== false) {
continue;
}
if (substr_compare($_file, $extension, -strlen($extension)) !== 0) {
continue;
}
if ($_fileinfo->getPath() !== substr($_dir, 0, -1)) {
$_file = substr($_fileinfo->getPath(), strlen($_dir)) . DIRECTORY_SEPARATOR . $_file;
}
echo "\n<br>", $_dir, '---', $_file;
flush();
$_start_time = microtime(true);
$_smarty = clone $smarty;
//
$_smarty->_cache = array();
$_smarty->ext = new Smarty_Internal_Extension_Handler();
$_smarty->ext->objType = $_smarty->_objType;
$_smarty->force_compile = $force_compile;
try {
/* @var Smarty_Internal_Template $_tpl */
$_tpl = new $smarty->template_class($_file, $_smarty);
$_tpl->caching = \Smarty\Smarty::CACHING_OFF;
$_tpl->source =
$isConfig ? Smarty_Template_Config::load($_tpl) : Smarty_Template_Source::load($_tpl);
if ($_tpl->mustCompile()) {
$_tpl->compileTemplateSource();
$_count++;
echo ' compiled in ', microtime(true) - $_start_time, ' seconds';
flush();
} else {
echo ' is up to date';
flush();
}
} catch (Exception $e) {
echo "\n<br> ------>Error: ", $e->getMessage(), "<br><br>\n";
$_error_count++;
}
// free memory
unset($_tpl);
$_smarty->_clearTemplateCache();
if ($max_errors !== null && $_error_count === $max_errors) {
echo "\n<br><br>too many errors\n";
exit(1);
}
}
}
echo "\n<br>";
return $_count;
}
}

View File

@@ -1,182 +0,0 @@
<?php
/**
* Smarty Method ConfigLoad
*
* Smarty::configLoad() method
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*/
class Smarty_Internal_Method_ConfigLoad
{
/**
* Valid for all objects
*
* @var int
*/
public $objMap = 7;
/**
* load a config file, optionally load just selected sections
*
* @api Smarty::configLoad()
* @link https://www.smarty.net/docs/en/api.config.load.tpl
*
* @param \Smarty\Data|\Smarty_Internal_Template|\Smarty $data
* @param string $config_file filename
* @param mixed $sections array of section names, single
* section or null
*
* @return \Smarty|\Smarty\Data|\Smarty_Internal_Template
* @throws \Exception
*/
public function configLoad(\Smarty\Data $data, $config_file, $sections = null)
{
$this->_loadConfigFile($data, $config_file, $sections, null);
return $data;
}
/**
* load a config file, optionally load just selected sections
*
* @api Smarty::configLoad()
* @link https://www.smarty.net/docs/en/api.config.load.tpl
*
* @param \Smarty|\Smarty\Data|\Smarty_Internal_Template $data
* @param string $config_file filename
* @param mixed $sections array of section names, single
* section or null
* @param int $scope scope into which config variables
* shall be loaded
*
* @throws \Exception
*/
public function _loadConfigFile(\Smarty\Data $data, $config_file, $sections = null, $scope = 0)
{
/* @var \Smarty $smarty */
$smarty = $data->_getSmartyObj();
/* @var \Smarty_Internal_Template $confObj */
$confObj = new Smarty_Internal_Template($config_file, $smarty, $data, null, null, null, null, true);
$confObj->caching = \Smarty\Smarty::CACHING_OFF;
$confObj->source->config_sections = $sections;
$confObj->source->scope = $scope;
$confObj->compiled = Smarty_Template_Compiled::load($confObj);
$confObj->compiled->render($confObj);
if ($data->_isTplObj()) {
$data->compiled->file_dependency[ $confObj->source->uid ] =
array($confObj->source->filepath, $confObj->source->getTimeStamp(), $confObj->source->type);
}
}
/**
* load config variables into template object
*
* @param \Smarty_Internal_Template $tpl
* @param array $new_config_vars
*/
public function _loadConfigVars(Smarty_Internal_Template $tpl, $new_config_vars)
{
$this->_assignConfigVars($tpl->parent->config_vars, $tpl, $new_config_vars);
$tagScope = $tpl->source->scope;
if ($tagScope >= 0) {
if ($tagScope === \Smarty\Smarty::SCOPE_LOCAL) {
$this->_updateVarStack($tpl, $new_config_vars);
$tagScope = 0;
if (!$tpl->scope) {
return;
}
}
if ($tpl->parent->_isTplObj() && ($tagScope || $tpl->parent->scope)) {
$mergedScope = $tagScope | $tpl->scope;
if ($mergedScope) {
// update scopes
/* @var \Smarty_Internal_Template|\Smarty|\Smarty\Data $ptr */
foreach ($tpl->smarty->ext->_updateScope->_getAffectedScopes($tpl->parent, $mergedScope) as $ptr) {
$this->_assignConfigVars($ptr->config_vars, $tpl, $new_config_vars);
if ($tagScope && $ptr->_isTplObj() && isset($tpl->_var_stack)) {
$this->_updateVarStack($tpl, $new_config_vars);
}
}
}
}
}
}
/**
* Assign all config variables in given scope
*
* @param array $config_vars config variables in scope
* @param \Smarty_Internal_Template $tpl
* @param array $new_config_vars loaded config variables
*/
public function _assignConfigVars(&$config_vars, Smarty_Internal_Template $tpl, $new_config_vars)
{
// copy global config vars
foreach ($new_config_vars[ 'vars' ] as $variable => $value) {
if ($tpl->smarty->config_overwrite || !isset($config_vars[ $variable ])) {
$config_vars[ $variable ] = $value;
} else {
$config_vars[ $variable ] = array_merge((array)$config_vars[ $variable ], (array)$value);
}
}
// scan sections
$sections = $tpl->source->config_sections;
if (!empty($sections)) {
foreach ((array)$sections as $tpl_section) {
if (isset($new_config_vars[ 'sections' ][ $tpl_section ])) {
foreach ($new_config_vars[ 'sections' ][ $tpl_section ][ 'vars' ] as $variable => $value) {
if ($tpl->smarty->config_overwrite || !isset($config_vars[ $variable ])) {
$config_vars[ $variable ] = $value;
} else {
$config_vars[ $variable ] = array_merge((array)$config_vars[ $variable ], (array)$value);
}
}
}
}
}
}
/**
* Update config variables in template local variable stack
*
* @param \Smarty_Internal_Template $tpl
* @param array $config_vars
*/
public function _updateVarStack(Smarty_Internal_Template $tpl, $config_vars)
{
$i = 0;
while (isset($tpl->_var_stack[ $i ])) {
$this->_assignConfigVars($tpl->_var_stack[ $i ][ 'config' ], $tpl, $config_vars);
$i++;
}
}
/**
* gets a config variable value
*
* @param \Smarty|\Smarty\Data|\Smarty_Internal_Template $data
* @param string $varName the name of the config variable
* @param bool $errorEnable
*
* @return null|string the value of the config variable
*/
public function _getConfigVariable(\Smarty\Data $data, $varName, $errorEnable = true)
{
$_ptr = $data;
while ($_ptr !== null) {
if (isset($_ptr->config_vars[ $varName ])) {
// found it, return it
return $_ptr->config_vars[ $varName ];
}
// not found, try at parent
$_ptr = $_ptr->parent;
}
if ($data->smarty->error_unassigned && $errorEnable) {
// force a notice
$x = $$varName;
}
return null;
}
}

View File

@@ -1,44 +0,0 @@
<?php
/**
* Smarty Method CreateData
*
* Smarty::createData() method
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*/
class Smarty_Internal_Method_CreateData
{
/**
* Valid for Smarty and template object
*
* @var int
*/
public $objMap = 3;
/**
* creates a data object
*
* @api Smarty::createData()
* @link https://www.smarty.net/docs/en/api.create.data.tpl
*
* @param \Smarty_Internal_TemplateBase|\Smarty_Internal_Template|\Smarty $obj
* @param \Smarty_Internal_Template|\Smarty\Data|\Smarty_Data|\Smarty $parent next higher level of Smarty
* variables
* @param string $name optional data block name
*
* @return \Smarty_Data data object
*/
public function createData(Smarty_Internal_TemplateBase $obj, \Smarty\Data $parent = null, $name = null)
{
/* @var Smarty $smarty */
$smarty = $obj->_getSmartyObj();
$dataObj = new Smarty_Data($parent, $smarty, $name);
if ($smarty->debugging) {
\Smarty\Debug::register_data($dataObj);
}
return $dataObj;
}
}

View File

@@ -1,34 +0,0 @@
<?php
/**
* Smarty Method GetConfigVariable
*
* Smarty::getConfigVariable() method
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*/
class Smarty_Internal_Method_GetConfigVariable
{
/**
* Valid for all objects
*
* @var int
*/
public $objMap = 7;
/**
* gets a config variable value
*
* @param \Smarty|\Smarty\Data|\Smarty_Internal_Template $data
* @param string $varName the name of the config variable
* @param bool $errorEnable
*
* @return null|string the value of the config variable
*/
public function getConfigVariable(\Smarty\Data $data, $varName = null, $errorEnable = true)
{
return $data->ext->configLoad->_getConfigVariable($data, $varName, $errorEnable);
}
}

View File

@@ -1,58 +0,0 @@
<?php
/**
* Smarty Method GetConfigVars
*
* Smarty::getConfigVars() method
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*/
class Smarty_Internal_Method_GetConfigVars
{
/**
* Valid for all objects
*
* @var int
*/
public $objMap = 7;
/**
* Returns a single or all config variables
*
* @api Smarty::getConfigVars()
* @link https://www.smarty.net/docs/en/api.get.config.vars.tpl
*
* @param \Smarty\Data|\Smarty_Internal_Template|\Smarty $data
* @param string $varname variable name or null
* @param bool $search_parents include parent templates?
*
* @return mixed variable value or or array of variables
*/
public function getConfigVars(\Smarty\Data $data, $varname = null, $search_parents = true)
{
$_ptr = $data;
$var_array = array();
while ($_ptr !== null) {
if (isset($varname)) {
if (isset($_ptr->config_vars[ $varname ])) {
return $_ptr->config_vars[ $varname ];
}
} else {
$var_array = array_merge($_ptr->config_vars, $var_array);
}
// not found, try at parent
if ($search_parents) {
$_ptr = $_ptr->parent;
} else {
$_ptr = null;
}
}
if (isset($varname)) {
return '';
} else {
return $var_array;
}
}
}

View File

@@ -1,35 +0,0 @@
<?php
/**
* Smarty Method GetDebugTemplate
*
* Smarty::getDebugTemplate() method
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*/
class Smarty_Internal_Method_GetDebugTemplate
{
/**
* Valid for Smarty and template object
*
* @var int
*/
public $objMap = 3;
/**
* return name of debugging template
*
* @api Smarty::getDebugTemplate()
*
* @param \Smarty_Internal_TemplateBase|\Smarty_Internal_Template|\Smarty $obj
*
* @return string
*/
public function getDebugTemplate(Smarty_Internal_TemplateBase $obj)
{
$smarty = $obj->_getSmartyObj();
return $smarty->debug_tpl;
}
}

View File

@@ -1,35 +0,0 @@
<?php
/**
* Smarty Method GetDefaultModifiers
*
* Smarty::getDefaultModifiers() method
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*/
class Smarty_Internal_Method_GetDefaultModifiers
{
/**
* Valid for Smarty and template object
*
* @var int
*/
public $objMap = 3;
/**
* Get default modifiers
*
* @api Smarty::getDefaultModifiers()
*
* @param \Smarty_Internal_TemplateBase|\Smarty_Internal_Template|\Smarty $obj
*
* @return array list of default modifiers
*/
public function getDefaultModifiers(Smarty_Internal_TemplateBase $obj)
{
$smarty = $obj->_getSmartyObj();
return $smarty->default_modifiers;
}
}

View File

@@ -1,47 +0,0 @@
<?php
/**
* Smarty Method GetGlobal
*
* Smarty::getGlobal() method
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*/
class Smarty_Internal_Method_GetGlobal
{
/**
* Valid for all objects
*
* @var int
*/
public $objMap = 7;
/**
* Returns a single or all global variables
*
* @api Smarty::getGlobal()
*
* @param \Smarty\Data $data
* @param string $varName variable name or null
*
* @return string|array variable value or or array of variables
*/
public function getGlobal(\Smarty\Data $data, $varName = null)
{
if (isset($varName)) {
if (isset(\Smarty\Smarty::$global_tpl_vars[ $varName ])) {
return \Smarty\Smarty::$global_tpl_vars[ $varName ]->value;
} else {
return '';
}
} else {
$_result = array();
foreach (\Smarty\Smarty::$global_tpl_vars as $key => $var) {
$_result[ $key ] = $var->value;
}
return $_result;
}
}
}

View File

@@ -1,44 +0,0 @@
<?php
/**
* Smarty Method GetRegisteredObject
*
* Smarty::getRegisteredObject() method
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*/
class Smarty_Internal_Method_GetRegisteredObject
{
/**
* Valid for Smarty and template object
*
* @var int
*/
public $objMap = 3;
/**
* return a reference to a registered object
*
* @api Smarty::getRegisteredObject()
* @link https://www.smarty.net/docs/en/api.get.registered.object.tpl
*
* @param \Smarty_Internal_TemplateBase|\Smarty_Internal_Template|\Smarty $obj
* @param string $object_name object name
*
* @return object
* @throws \SmartyException if no such object is found
*/
public function getRegisteredObject(Smarty_Internal_TemplateBase $obj, $object_name)
{
$smarty = $obj->_getSmartyObj();
if (!isset($smarty->registered_objects[ $object_name ])) {
throw new SmartyException("'$object_name' is not a registered object");
}
if (!is_object($smarty->registered_objects[ $object_name ][ 0 ])) {
throw new SmartyException("registered '$object_name' is not an object");
}
return $smarty->registered_objects[ $object_name ][ 0 ];
}
}

View File

@@ -1,50 +0,0 @@
<?php
/**
* Smarty Method GetStreamVariable
*
* Smarty::getStreamVariable() method
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*/
class Smarty_Internal_Method_GetStreamVariable
{
/**
* Valid for all objects
*
* @var int
*/
public $objMap = 7;
/**
* gets a stream variable
*
* @api Smarty::getStreamVariable()
*
* @param \Smarty\Data|\Smarty_Internal_Template|\Smarty $data
* @param string $variable the stream of the variable
*
* @return mixed
* @throws \SmartyException
*/
public function getStreamVariable(\Smarty\Data $data, $variable)
{
$_result = '';
$fp = fopen($variable, 'r+');
if ($fp) {
while (!feof($fp) && ($current_line = fgets($fp)) !== false) {
$_result .= $current_line;
}
fclose($fp);
return $_result;
}
$smarty = isset($data->smarty) ? $data->smarty : $data;
if ($smarty->error_unassigned) {
throw new SmartyException('Undefined stream variable "' . $variable . '"');
} else {
return null;
}
}
}

View File

@@ -1,119 +0,0 @@
<?php
/**
* Smarty Method GetTemplateVars
*
* Smarty::getTemplateVars() method
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*/
class Smarty_Internal_Method_GetTemplateVars
{
/**
* Valid for all objects
*
* @var int
*/
public $objMap = 7;
/**
* Returns a single or all template variables
*
* @api Smarty::getTemplateVars()
* @link https://www.smarty.net/docs/en/api.get.template.vars.tpl
*
* @param \Smarty\Data|\Smarty_Internal_Template|\Smarty $data
* @param string $varName variable name or null
* @param \Smarty\Data|\Smarty_Internal_Template|\Smarty $_ptr optional pointer to data object
* @param bool $searchParents include parent templates?
*
* @return mixed variable value or or array of variables
*/
public function getTemplateVars(
\Smarty\Data $data,
$varName = null,
\Smarty\Data $_ptr = null,
$searchParents = true
) {
if (isset($varName)) {
$_var = $this->_getVariable($data, $varName, $_ptr, $searchParents, false);
if (is_object($_var)) {
return $_var->value;
} else {
return null;
}
} else {
$_result = array();
if ($_ptr === null) {
$_ptr = $data;
}
while ($_ptr !== null) {
foreach ($_ptr->tpl_vars as $key => $var) {
if (!array_key_exists($key, $_result)) {
$_result[ $key ] = $var->value;
}
}
// not found, try at parent
if ($searchParents && isset($_ptr->parent)) {
$_ptr = $_ptr->parent;
} else {
$_ptr = null;
}
}
if ($searchParents && isset(\Smarty\Smarty::$global_tpl_vars)) {
foreach (\Smarty\Smarty::$global_tpl_vars as $key => $var) {
if (!array_key_exists($key, $_result)) {
$_result[ $key ] = $var->value;
}
}
}
return $_result;
}
}
/**
* gets the object of a Smarty variable
*
* @param \Smarty\Data|\Smarty_Internal_Template|\Smarty $data
* @param string $varName the name of the Smarty variable
* @param \Smarty\Data|\Smarty_Internal_Template|\Smarty $_ptr optional pointer to data object
* @param bool $searchParents search also in parent data
* @param bool $errorEnable
*
* @return \Smarty\Variable
*/
public function _getVariable(
\Smarty\Data $data,
$varName,
\Smarty\Data $_ptr = null,
$searchParents = true,
$errorEnable = true
) {
if ($_ptr === null) {
$_ptr = $data;
}
while ($_ptr !== null) {
if (isset($_ptr->tpl_vars[ $varName ])) {
// found it, return it
return $_ptr->tpl_vars[ $varName ];
}
// not found, try at parent
if ($searchParents && isset($_ptr->parent)) {
$_ptr = $_ptr->parent;
} else {
$_ptr = null;
}
}
if (isset(\Smarty\Smarty::$global_tpl_vars[ $varName ])) {
// found it, return it
return \Smarty\Smarty::$global_tpl_vars[ $varName ];
}
if ($errorEnable && $data->_getSmartyObj()->error_unassigned) {
// force a notice
$x = $$varName;
}
return new Smarty_Undefined_Variable;
}
}

View File

@@ -1,100 +0,0 @@
<?php
/**
* Smarty Method GetLiterals
*
* Smarty::getLiterals() method
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*/
class Smarty_Internal_Method_Literals
{
/**
* Valid for Smarty and template object
*
* @var int
*/
public $objMap = 3;
/**
* Get literals
*
* @api Smarty::getLiterals()
*
* @param \Smarty_Internal_TemplateBase|\Smarty_Internal_Template|\Smarty $obj
*
* @return array list of literals
*/
public function getLiterals(Smarty_Internal_TemplateBase $obj)
{
$smarty = $obj->_getSmartyObj();
return (array)$smarty->literals;
}
/**
* Add literals
*
* @api Smarty::addLiterals()
*
* @param \Smarty_Internal_TemplateBase|\Smarty_Internal_Template|\Smarty $obj
* @param array|string $literals literal or list of literals
* to addto add
*
* @return \Smarty|\Smarty_Internal_Template
* @throws \SmartyException
*/
public function addLiterals(Smarty_Internal_TemplateBase $obj, $literals = null)
{
if (isset($literals)) {
$this->set($obj->_getSmartyObj(), (array)$literals);
}
return $obj;
}
/**
* Set literals
*
* @api Smarty::setLiterals()
*
* @param \Smarty_Internal_TemplateBase|\Smarty_Internal_Template|\Smarty $obj
* @param array|string $literals literal or list of literals
* to setto set
*
* @return \Smarty|\Smarty_Internal_Template
* @throws \SmartyException
*/
public function setLiterals(Smarty_Internal_TemplateBase $obj, $literals = null)
{
$smarty = $obj->_getSmartyObj();
$smarty->literals = array();
if (!empty($literals)) {
$this->set($smarty, (array)$literals);
}
return $obj;
}
/**
* common setter for literals for easier handling of duplicates the
* Smarty::$literals array gets filled with identical key values
*
* @param \Smarty $smarty
* @param array $literals
*
* @throws \SmartyException
*/
private function set(Smarty $smarty, $literals)
{
$literals = array_combine($literals, $literals);
$error = isset($literals[ $smarty->left_delimiter ]) ? array($smarty->left_delimiter) : array();
$error = isset($literals[ $smarty->right_delimiter ]) ? $error[] = $smarty->right_delimiter : $error;
if (!empty($error)) {
throw new SmartyException(
'User defined literal(s) "' . $error .
'" may not be identical with left or right delimiter'
);
}
$smarty->literals = array_merge((array)$smarty->literals, (array)$literals);
}
}

View File

@@ -1,75 +0,0 @@
<?php
/**
* Smarty Method LoadFilter
*
* Smarty::loadFilter() method
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*/
class Smarty_Internal_Method_LoadFilter
{
/**
* Valid for Smarty and template object
*
* @var int
*/
public $objMap = 3;
/**
* Valid filter types
*
* @var array
*/
private $filterTypes = array('pre' => true, 'post' => true, 'output' => true, 'variable' => true);
/**
* load a filter of specified type and name
*
* @api Smarty::loadFilter()
*
* @link https://www.smarty.net/docs/en/api.load.filter.tpl
*
* @param \Smarty_Internal_TemplateBase|\Smarty_Internal_Template|\Smarty $obj
* @param string $type filter type
* @param string $name filter name
*
* @return bool
* @throws SmartyException if filter could not be loaded
*/
public function loadFilter(Smarty_Internal_TemplateBase $obj, $type, $name)
{
$smarty = $obj->_getSmartyObj();
$this->_checkFilterType($type);
$_plugin = "smarty_{$type}filter_{$name}";
$_filter_name = $_plugin;
if (is_callable($_plugin)) {
$smarty->registered_filters[ $type ][ $_filter_name ] = $_plugin;
return true;
}
if (class_exists($_plugin, false)) {
$_plugin = array($_plugin, 'execute');
}
if (is_callable($_plugin)) {
$smarty->registered_filters[ $type ][ $_filter_name ] = $_plugin;
return true;
}
throw new SmartyException("{$type}filter '{$name}' not found or callable");
}
/**
* Check if filter type is valid
*
* @param string $type
*
* @throws \SmartyException
*/
public function _checkFilterType($type)
{
if (!isset($this->filterTypes[ $type ])) {
throw new SmartyException("Illegal filter type '{$type}'");
}
}
}

View File

@@ -1,50 +0,0 @@
<?php
/**
* Smarty Method MustCompile
*
* Smarty_Internal_Template::mustCompile() method
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*/
class Smarty_Internal_Method_MustCompile
{
/**
* Valid for template object
*
* @var int
*/
public $objMap = 2;
/**
* Returns if the current template must be compiled by the Smarty compiler
* It does compare the timestamps of template source and the compiled templates and checks the force compile
* configuration
*
* @param \Smarty_Internal_Template $_template
*
* @return bool
* @throws \SmartyException
*/
public function mustCompile(Smarty_Internal_Template $_template)
{
if (!$_template->source->exists) {
if ($_template->_isSubTpl()) {
$parent_resource = " in '{$_template->parent->template_resource}'";
} else {
$parent_resource = '';
}
throw new SmartyException("Unable to load template {$_template->source->type} '{$_template->source->name}'{$parent_resource}");
}
if ($_template->mustCompile === null) {
$_template->mustCompile = (!$_template->source->handler->uncompiled &&
($_template->smarty->force_compile || $_template->source->handler->recompiled ||
!$_template->compiled->exists || ($_template->compile_check &&
$_template->compiled->getTimeStamp() <
$_template->source->getTimeStamp())));
}
return $_template->mustCompile;
}
}

View File

@@ -1,87 +0,0 @@
<?php
/**
* Smarty Method RegisterFilter
*
* Smarty::registerFilter() method
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*/
class Smarty_Internal_Method_RegisterFilter
{
/**
* Valid for Smarty and template object
*
* @var int
*/
public $objMap = 3;
/**
* Valid filter types
*
* @var array
*/
private $filterTypes = array('pre' => true, 'post' => true, 'output' => true, 'variable' => true);
/**
* Registers a filter function
*
* @api Smarty::registerFilter()
*
* @link https://www.smarty.net/docs/en/api.register.filter.tpl
*
* @param \Smarty_Internal_TemplateBase|\Smarty_Internal_Template|\Smarty $obj
* @param string $type filter type
* @param callback $callback
* @param string|null $name optional filter name
*
* @return \Smarty|\Smarty_Internal_Template
* @throws \SmartyException
*/
public function registerFilter(Smarty_Internal_TemplateBase $obj, $type, $callback, $name = null)
{
$smarty = $obj->_getSmartyObj();
$this->_checkFilterType($type);
$name = isset($name) ? $name : $this->_getFilterName($callback);
if (!is_callable($callback)) {
throw new SmartyException("{$type}filter '{$name}' not callable");
}
$smarty->registered_filters[ $type ][ $name ] = $callback;
return $obj;
}
/**
* Return internal filter name
*
* @param callback $function_name
*
* @return string internal filter name
*/
public function _getFilterName($function_name)
{
if (is_array($function_name)) {
$_class_name = (is_object($function_name[ 0 ]) ? get_class($function_name[ 0 ]) : $function_name[ 0 ]);
return $_class_name . '_' . $function_name[ 1 ];
} elseif (is_string($function_name)) {
return $function_name;
} else {
return 'closure';
}
}
/**
* Check if filter type is valid
*
* @param string $type
*
* @throws \SmartyException
*/
public function _checkFilterType($type)
{
if (!isset($this->filterTypes[ $type ])) {
throw new SmartyException("Illegal filter type '{$type}'");
}
}
}

View File

@@ -1,43 +0,0 @@
<?php
/**
* Smarty Method UnloadFilter
*
* Smarty::unloadFilter() method
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*/
class Smarty_Internal_Method_UnloadFilter extends Smarty_Internal_Method_LoadFilter
{
/**
* load a filter of specified type and name
*
* @api Smarty::unloadFilter()
*
* @link https://www.smarty.net/docs/en/api.unload.filter.tpl
*
* @param \Smarty_Internal_TemplateBase|\Smarty_Internal_Template|\Smarty $obj
* @param string $type filter type
* @param string $name filter name
*
* @return Smarty_Internal_TemplateBase
* @throws \SmartyException
*/
public function unloadFilter(Smarty_Internal_TemplateBase $obj, $type, $name)
{
$smarty = $obj->_getSmartyObj();
$this->_checkFilterType($type);
if (isset($smarty->registered_filters[ $type ])) {
$_filter_name = "smarty_{$type}filter_{$name}";
if (isset($smarty->registered_filters[ $type ][ $_filter_name ])) {
unset($smarty->registered_filters[ $type ][ $_filter_name ]);
if (empty($smarty->registered_filters[ $type ])) {
unset($smarty->registered_filters[ $type ]);
}
}
}
return $obj;
}
}

View File

@@ -1,43 +0,0 @@
<?php
/**
* Smarty Method UnregisterFilter
*
* Smarty::unregisterFilter() method
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*/
class Smarty_Internal_Method_UnregisterFilter extends Smarty_Internal_Method_RegisterFilter
{
/**
* Unregisters a filter function
*
* @api Smarty::unregisterFilter()
*
* @link https://www.smarty.net/docs/en/api.unregister.filter.tpl
*
* @param \Smarty_Internal_TemplateBase|\Smarty_Internal_Template|\Smarty $obj
* @param string $type filter type
* @param callback|string $callback
*
* @return \Smarty|\Smarty_Internal_Template
* @throws \SmartyException
*/
public function unregisterFilter(Smarty_Internal_TemplateBase $obj, $type, $callback)
{
$smarty = $obj->_getSmartyObj();
$this->_checkFilterType($type);
if (isset($smarty->registered_filters[ $type ])) {
$name = is_string($callback) ? $callback : $this->_getFilterName($callback);
if (isset($smarty->registered_filters[ $type ][ $name ])) {
unset($smarty->registered_filters[ $type ][ $name ]);
if (empty($smarty->registered_filters[ $type ])) {
unset($smarty->registered_filters[ $type ]);
}
}
}
return $obj;
}
}

View File

@@ -34,17 +34,6 @@ namespace Smarty;
/**
* This is the main Smarty class
*
* @package Smarty
*
* The following methods will be dynamically loaded by the extension handler when they are called.
* They are located in a corresponding Smarty_Internal_Method_xxxx class
*
* @method int clearAllCache(int $exp_time = null, string $type = null)
* @method int clearCache(string $template_name, string $cache_id = null, string $compile_id = null, int $exp_time = null, string $type = null)
* @method int compileAllTemplates(string $extension = '.tpl', bool $force_compile = false, int $time_limit = 0, $max_errors = null)
* @method int compileAllConfig(string $extension = '.conf', bool $force_compile = false, int $time_limit = 0, $max_errors = null)
* @method int clearCompiledTemplate($resource_name = null, $compile_id = null, $exp_time = null)
*/
class Smarty extends \Smarty_Internal_TemplateBase
{
@@ -1226,4 +1215,288 @@ class Smarty extends \Smarty_Internal_TemplateBase
return $this->isMutingUndefinedOrNullWarnings;
}
/**
* Empty cache for a specific template
*
* @api Smarty::clearCache()
* @link https://www.smarty.net/docs/en/api.clear.cache.tpl
*
* @param string $template_name template name
* @param string $cache_id cache id
* @param string $compile_id compile id
* @param integer $exp_time expiration time
* @param string $type resource type
*
* @return int number of cache files deleted
* @throws \SmartyException
*/
public function clearCache(
$template_name,
$cache_id = null,
$compile_id = null,
$exp_time = null,
$type = null
) {
$this->_clearTemplateCache();
// load cache resource and call clear
$_cache_resource = \Smarty\Cacheresource\Base::load($this, $type);
return $_cache_resource->clear($this, $template_name, $cache_id, $compile_id, $exp_time);
}
/**
* Empty cache folder
*
* @api Smarty::clearAllCache()
* @link https://www.smarty.net/docs/en/api.clear.all.cache.tpl
*
* @param integer $exp_time expiration time
* @param string $type resource type
*
* @return int number of cache files deleted
*/
public function clearAllCache($exp_time = null, $type = null)
{
$this->_clearTemplateCache();
// load cache resource and call clearAll
$_cache_resource = \Smarty\Cacheresource\Base::load($this, $type);
return $_cache_resource->clearAll($this, $exp_time);
}
/**
* Delete compiled template file
*
* @api Smarty::clearCompiledTemplate()
* @link https://www.smarty.net/docs/en/api.clear.compiled.template.tpl
*
* @param string $resource_name template name
* @param string $compile_id compile id
* @param integer $exp_time expiration time
*
* @return int number of template files deleted
* @throws \SmartyException
*/
public function clearCompiledTemplate($resource_name = null, $compile_id = null, $exp_time = null)
{
// clear template objects cache
$this->_clearTemplateCache();
$_compile_dir = $this->getCompileDir();
if ($_compile_dir === '/') { //We should never want to delete this!
return 0;
}
$_compile_id = isset($compile_id) ? preg_replace('![^\w]+!', '_', $compile_id) : null;
$_dir_sep = $this->use_sub_dirs ? DIRECTORY_SEPARATOR : '^';
if (isset($resource_name)) {
$_save_stat = $this->caching;
$this->caching = \Smarty\Smarty::CACHING_OFF;
/* @var Smarty_Internal_Template $tpl */
$tpl = $this->createTemplate($resource_name);
$this->caching = $_save_stat;
if (!$tpl->source->handler->uncompiled && !$tpl->source->handler->recompiled && $tpl->source->exists) {
$_resource_part_1 = basename(str_replace('^', DIRECTORY_SEPARATOR, $tpl->compiled->filepath));
$_resource_part_1_length = strlen($_resource_part_1);
} else {
return 0;
}
$_resource_part_2 = str_replace('.php', '.cache.php', $_resource_part_1);
$_resource_part_2_length = strlen($_resource_part_2);
}
$_dir = $_compile_dir;
if ($this->use_sub_dirs && isset($_compile_id)) {
$_dir .= $_compile_id . $_dir_sep;
}
if (isset($_compile_id)) {
$_compile_id_part = $_compile_dir . $_compile_id . $_dir_sep;
$_compile_id_part_length = strlen($_compile_id_part);
}
$_count = 0;
try {
$_compileDirs = new RecursiveDirectoryIterator($_dir);
// NOTE: UnexpectedValueException thrown for PHP >= 5.3
} catch (Exception $e) {
return 0;
}
$_compile = new RecursiveIteratorIterator($_compileDirs, RecursiveIteratorIterator::CHILD_FIRST);
foreach ($_compile as $_file) {
if (substr(basename($_file->getPathname()), 0, 1) === '.') {
continue;
}
$_filepath = (string)$_file;
if ($_file->isDir()) {
if (!$_compile->isDot()) {
// delete folder if empty
@rmdir($_file->getPathname());
}
} else {
// delete only php files
if (substr($_filepath, -4) !== '.php') {
continue;
}
$unlink = false;
if ((!isset($_compile_id) ||
(isset($_filepath[ $_compile_id_part_length ]) &&
$a = !strncmp($_filepath, $_compile_id_part, $_compile_id_part_length)))
&& (!isset($resource_name) || (isset($_filepath[ $_resource_part_1_length ])
&& substr_compare(
$_filepath,
$_resource_part_1,
-$_resource_part_1_length,
$_resource_part_1_length
) === 0) || (isset($_filepath[ $_resource_part_2_length ])
&& substr_compare(
$_filepath,
$_resource_part_2,
-$_resource_part_2_length,
$_resource_part_2_length
) === 0))
) {
if (isset($exp_time)) {
if (is_file($_filepath) && time() - filemtime($_filepath) >= $exp_time) {
$unlink = true;
}
} else {
$unlink = true;
}
}
if ($unlink && is_file($_filepath) && @unlink($_filepath)) {
$_count++;
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;
}
/**
* Compile all template files
*
* @api Smarty::compileAllTemplates()
*
* @param string $extension file extension
* @param bool $force_compile force all to recompile
* @param int $time_limit
* @param int $max_errors
*
* @return integer number of template files recompiled
*/
public function compileAllTemplates(
$extension = '.tpl',
$force_compile = false,
$time_limit = 0,
$max_errors = null
) {
return $this->compileAll($extension, $force_compile, $time_limit, $max_errors);
}
/**
* Compile all config files
*
* @api Smarty::compileAllConfig()
*
* @param string $extension file extension
* @param bool $force_compile force all to recompile
* @param int $time_limit
* @param int $max_errors
*
* @return int number of template files recompiled
*/
public function compileAllConfig(
$extension = '.conf',
$force_compile = false,
$time_limit = 0,
$max_errors = null
) {
return $this->compileAll($extension, $force_compile, $time_limit, $max_errors, true);
}
/**
* Compile all template or config files
*
* @param string $extension template file name extension
* @param bool $force_compile force all to recompile
* @param int $time_limit set maximum execution time
* @param int $max_errors set maximum allowed errors
* @param bool $isConfig flag true if called for config files
*
* @return int number of template files compiled
*/
protected function compileAll(
$extension,
$force_compile,
$time_limit,
$max_errors,
$isConfig = false
) {
// switch off time limit
if (function_exists('set_time_limit')) {
@set_time_limit($time_limit);
}
$_count = 0;
$_error_count = 0;
$sourceDir = $isConfig ? $this->getConfigDir() : $this->getTemplateDir();
// loop over array of source directories
foreach ($sourceDir as $_dir) {
$_dir_1 = new RecursiveDirectoryIterator(
$_dir,
defined('FilesystemIterator::FOLLOW_SYMLINKS') ?
FilesystemIterator::FOLLOW_SYMLINKS : 0
);
$_dir_2 = new RecursiveIteratorIterator($_dir_1);
foreach ($_dir_2 as $_fileinfo) {
$_file = $_fileinfo->getFilename();
if (substr(basename($_fileinfo->getPathname()), 0, 1) === '.' || strpos($_file, '.svn') !== false) {
continue;
}
if (substr_compare($_file, $extension, -strlen($extension)) !== 0) {
continue;
}
if ($_fileinfo->getPath() !== substr($_dir, 0, -1)) {
$_file = substr($_fileinfo->getPath(), strlen($_dir)) . DIRECTORY_SEPARATOR . $_file;
}
echo "\n<br>", $_dir, '---', $_file;
flush();
$_start_time = microtime(true);
$_smarty = clone $this;
//
$_smarty->_cache = array();
$_smarty->ext = new Smarty_Internal_Extension_Handler();
$_smarty->ext->objType = $_smarty->_objType;
$_smarty->force_compile = $force_compile;
try {
/* @var Smarty_Internal_Template $_tpl */
$_tpl = new $this->template_class($_file, $_smarty);
$_tpl->caching = self::CACHING_OFF;
$_tpl->source =
$isConfig ? Smarty_Template_Config::load($_tpl) : Smarty_Template_Source::load($_tpl);
if ($_tpl->mustCompile()) {
$_tpl->compileTemplateSource();
$_count++;
echo ' compiled in ', microtime(true) - $_start_time, ' seconds';
flush();
} else {
echo ' is up to date';
flush();
}
} catch (Exception $e) {
echo "\n<br> ------>Error: ", $e->getMessage(), "<br><br>\n";
$_error_count++;
}
// free memory
unset($_tpl);
$_smarty->_clearTemplateCache();
if ($max_errors !== null && $_error_count === $max_errors) {
echo "\n<br><br>too many errors\n";
exit(1);
}
}
}
echo "\n<br>";
return $_count;
}
}

View File

@@ -8,21 +8,15 @@
* @author Uwe Tews
*/
use Smarty\Data;
/**
* Main class with template data structures and methods
*
* @package Smarty
* @subpackage Template
*
* @property Smarty_Template_Compiled $compiled
* @property Smarty_Template_Cached $cached
* @property \Smarty\Compiler\Template $compiler
* @property mixed|\Smarty_Template_Cached registered_plugins
*
* The following methods will be dynamically loaded by the extension handler when they are called.
* They are located in a corresponding Smarty_Internal_Method_xxxx class
*
* @method bool mustCompile()
*/
#[\AllowDynamicProperties]
class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
@@ -57,13 +51,6 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
*/
public $_objType = 2;
/**
* Global smarty instance
*
* @var Smarty
*/
public $smarty = null;
/**
* Source instance
*
@@ -319,7 +306,7 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
$tpl->compile_id = $compile_id;
if (isset($uid)) {
// for inline templates we can get all resource information from file dependency
list($filepath, $timestamp, $type) = $tpl->compiled->file_dependency[ $uid ];
[$filepath, $timestamp, $type] = $tpl->compiled->file_dependency[ $uid ];
$tpl->source = new Smarty_Template_Source($smarty, $filepath, $type, $filepath);
$tpl->source->filepath = $filepath;
$tpl->source->timestamp = $timestamp;
@@ -508,7 +495,6 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
if ($is_valid) {
$resource->unifunc = $properties[ 'unifunc' ];
$resource->has_nocache_code = $properties[ 'has_nocache_code' ];
// $tpl->compiled->nocache_hash = $properties['nocache_hash'];
$resource->file_dependency = $properties[ 'file_dependency' ];
}
return $is_valid && !function_exists($properties[ 'unifunc' ]);
@@ -714,4 +700,115 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
$this->cached->handler->releaseLock($this->smarty, $this->cached);
}
}
/**
* load config variables into template object
*
* @param \Smarty_Internal_Template $tpl
* @param array $new_config_vars
*/
public function _loadConfigVars($new_config_vars)
{
$this->_assignConfigVars($this->parent->config_vars, $new_config_vars);
$tagScope = $this->source->scope;
if ($tagScope >= 0) {
if ($tagScope === \Smarty\Smarty::SCOPE_LOCAL) {
$this->_updateConfigVarStack($new_config_vars);
$tagScope = 0;
if (!$this->scope) {
return;
}
}
if ($this->parent->_isTplObj() && ($tagScope || $this->parent->scope)) {
$mergedScope = $tagScope | $this->scope;
if ($mergedScope) {
// update scopes
/* @var \Smarty_Internal_Template|\Smarty|Data $ptr */
foreach ($this->smarty->ext->_updateScope->_getAffectedScopes($this->parent, $mergedScope) as $ptr) {
$this->_assignConfigVars($ptr->config_vars, $new_config_vars);
if ($tagScope && $ptr->_isTplObj() && isset($this->_var_stack)) {
$this->_updateConfigVarStack($new_config_vars);
}
}
}
}
}
}
/**
* Assign all config variables in given scope
*
* @param array $config_vars config variables in scope
* @param array $new_config_vars loaded config variables
*/
private function _assignConfigVars(&$config_vars, $new_config_vars)
{
// copy global config vars
foreach ($new_config_vars[ 'vars' ] as $variable => $value) {
if ($this->smarty->config_overwrite || !isset($config_vars[ $variable ])) {
$config_vars[ $variable ] = $value;
} else {
$config_vars[ $variable ] = array_merge((array)$config_vars[ $variable ], (array)$value);
}
}
// scan sections
$sections = $this->source->config_sections;
if (!empty($sections)) {
foreach ((array)$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->smarty->config_overwrite || !isset($config_vars[ $variable ])) {
$config_vars[ $variable ] = $value;
} else {
$config_vars[ $variable ] = array_merge((array)$config_vars[ $variable ], (array)$value);
}
}
}
}
}
}
/**
* Update config variables in template local variable stack
*
* @param array $config_vars
*/
private function _updateConfigVarStack($config_vars)
{
$i = 0;
while (isset($this->_var_stack[ $i ])) {
$this->_assignConfigVars($this->_var_stack[ $i ][ 'config' ], $config_vars);
$i++;
}
}
/**
* Returns if the current template must be compiled by the Smarty compiler
* It does compare the timestamps of template source and the compiled templates and checks the force compile
* configuration
*
* @return bool
* @throws \SmartyException
*/
public function mustCompile()
{
if (!$this->source->exists) {
if ($this->_isSubTpl()) {
$parent_resource = " in '{$this->parent->template_resource}'";
} else {
$parent_resource = '';
}
throw new SmartyException("Unable to load template {$this->source->type} '{$this->source->name}'{$parent_resource}");
}
if ($this->mustCompile === null) {
$this->mustCompile = (!$this->source->handler->uncompiled &&
($this->smarty->force_compile || $this->source->handler->recompiled ||
!$this->compiled->exists || ($this->compile_check &&
$this->compiled->getTimeStamp() <
$this->source->getTimeStamp())));
}
return $this->mustCompile;
}
}

View File

@@ -8,24 +8,17 @@
* @author Uwe Tews
*/
use Smarty\Data;
use Smarty\Smarty;
/**
* Class with shared smarty/template methods
*
* @package Smarty
* @subpackage Template
*
* @property int $_objType
*
* The following methods will be dynamically loaded by the extension handler when they are called.
* They are located in a corresponding Smarty_Internal_Method_xxxx class
*
* @method Smarty_Internal_TemplateBase addDefaultModifiers(mixed $modifiers)
* @method Smarty_Internal_TemplateBase addLiterals(mixed $literals)
* @method Smarty_Internal_TemplateBase createData(\Smarty\Data $parent = null, string $name = null)
* @method string getDebugTemplate()
* @method array getDefaultModifier()
* @method array getLiterals()
* @method object getRegisteredObject(string $object_name)
* @method Smarty_Internal_TemplateBase registerCacheResource(string $name, \Smarty\Cacheresource\Base $resource_handler)
* @method Smarty_Internal_TemplateBase registerClass(string $class_name, string $class_impl)
* @method Smarty_Internal_TemplateBase registerDefaultConfigHandler(callback $callback)
@@ -34,15 +27,12 @@
* @method Smarty_Internal_TemplateBase registerResource(string $name, mixed $resource_handler)
* @method Smarty_Internal_TemplateBase setDebugTemplate(string $tpl_name)
* @method Smarty_Internal_TemplateBase setDefaultModifiers(mixed $modifiers)
* @method Smarty_Internal_TemplateBase setLiterals(mixed $literals)
* @method Smarty_Internal_TemplateBase unloadFilter(string $type, string $name)
* @method Smarty_Internal_TemplateBase unregisterCacheResource(string $name)
* @method Smarty_Internal_TemplateBase unregisterObject(string $object_name)
* @method Smarty_Internal_TemplateBase unregisterPlugin(string $type, string $name)
* @method Smarty_Internal_TemplateBase unregisterFilter(string $type, mixed $callback)
* @method Smarty_Internal_TemplateBase unregisterResource(string $name)
*/
abstract class Smarty_Internal_TemplateBase extends \Smarty\Data
abstract class Smarty_Internal_TemplateBase extends Data
{
/**
* Set this if you want different sets of cache files for the same
@@ -95,7 +85,15 @@ abstract class Smarty_Internal_TemplateBase extends \Smarty\Data
*/
public $_var_stack = null;
/**
/**
* Valid filter types
*
* @var array
*/
private $filterTypes = array('pre' => true, 'post' => true, 'output' => true, 'variable' => true);
/**
* fetches a rendered Smarty template
*
* @param string $template the resource handle of the template file or template object
@@ -302,27 +300,107 @@ abstract class Smarty_Internal_TemplateBase extends \Smarty\Data
*/
public function loadFilter($type, $name)
{
return $this->ext->loadFilter->loadFilter($this, $type, $name);
$smarty = $this->_getSmartyObj();
$this->_checkFilterType($type);
$_plugin = "smarty_{$type}filter_{$name}";
$_filter_name = $_plugin;
if (is_callable($_plugin)) {
$smarty->registered_filters[ $type ][ $_filter_name ] = $_plugin;
return true;
}
if (class_exists($_plugin, false)) {
$_plugin = array($_plugin, 'execute');
}
if (is_callable($_plugin)) {
$smarty->registered_filters[ $type ][ $_filter_name ] = $_plugin;
return true;
}
throw new SmartyException("{$type}filter '{$name}' not found or callable");
}
/**
* load a filter of specified type and name
*
* @api Smarty::unloadFilter()
*
* @link https://www.smarty.net/docs/en/api.unload.filter.tpl
*
* @param string $type filter type
* @param string $name filter name
*
* @return Smarty_Internal_TemplateBase
* @throws \SmartyException
*/
public function unloadFilter($type, $name)
{
$smarty = $this->_getSmartyObj();
$this->_checkFilterType($type);
if (isset($smarty->registered_filters[ $type ])) {
$_filter_name = "smarty_{$type}filter_{$name}";
if (isset($smarty->registered_filters[ $type ][ $_filter_name ])) {
unset($smarty->registered_filters[ $type ][ $_filter_name ]);
if (empty($smarty->registered_filters[ $type ])) {
unset($smarty->registered_filters[ $type ]);
}
}
}
return $this;
}
/**
* Registers a filter function
*
* @api Smarty::registerFilter()
* @link https://www.smarty.net/docs/en/api.register.filter.tpl
*
* @param string $type filter type
* @param callable $callback
* @param string|null $name optional filter name
*
* @return \Smarty|\Smarty_Internal_Template
* @return Smarty_Internal_TemplateBase
* @throws \SmartyException
*@link https://www.smarty.net/docs/en/api.register.filter.tpl
*
* @api Smarty::registerFilter()
*/
public function registerFilter($type, $callback, $name = null)
{
return $this->ext->registerFilter->registerFilter($this, $type, $callback, $name);
$smarty = $this->_getSmartyObj();
$this->_checkFilterType($type);
$name = isset($name) ? $name : $this->_getFilterName($callback);
if (!is_callable($callback)) {
throw new SmartyException("{$type}filter '{$name}' not callable");
}
$smarty->registered_filters[ $type ][ $name ] = $callback;
return $this;
}
/**
* Unregisters a filter function
*
* @param string $type filter type
* @param callback|string $callback
*
* @return Smarty_Internal_TemplateBase
* @throws \SmartyException
*@api Smarty::unregisterFilter()
*
* @link https://www.smarty.net/docs/en/api.unregister.filter.tpl
*
*/
public function unregisterFilter($type, $callback)
{
$smarty = $this->_getSmartyObj();
$this->_checkFilterType($type);
if (isset($smarty->registered_filters[ $type ])) {
$name = is_string($callback) ? $callback : $this->_getFilterName($callback);
if (isset($smarty->registered_filters[ $type ][ $name ])) {
unset($smarty->registered_filters[ $type ][ $name ]);
if (empty($smarty->registered_filters[ $type ])) {
unset($smarty->registered_filters[ $type ]);
}
}
}
return $this;
}
/**
* Registers object to be used in templates
*
@@ -394,4 +472,209 @@ abstract class Smarty_Internal_TemplateBase extends \Smarty\Data
{
$this->cache_id = $cache_id;
}
/**
* Add default modifiers
*
* @api Smarty::addDefaultModifiers()
*
* @param array|string $modifiers modifier or list of modifiers
* to add
*
* @return \Smarty|\Smarty_Internal_Template
*/
public function addDefaultModifiers($modifiers)
{
$smarty = $this->_getSmartyObj();
if (is_array($modifiers)) {
$smarty->default_modifiers = array_merge($smarty->default_modifiers, $modifiers);
} else {
$smarty->default_modifiers[] = $modifiers;
}
return $this;
}
/**
* creates a data object
*
* @param Data|null $parent next higher level of Smarty
* variables
* @param null $name optional data block name
*
* @return Smarty_Data data object
* @throws SmartyException
* @api Smarty::createData()
* @link https://www.smarty.net/docs/en/api.create.data.tpl
*
*/
public function createData(Data $parent = null, $name = null)
{
/* @var Smarty $smarty */
$smarty = $this->_getSmartyObj();
$dataObj = new Smarty_Data($parent, $smarty, $name);
if ($smarty->debugging) {
\Smarty\Debug::register_data($dataObj);
}
return $dataObj;
}
/**
* return name of debugging template
*
* @api Smarty::getDebugTemplate()
*
* @return string
*/
public function getDebugTemplate()
{
$smarty = $this->_getSmartyObj();
return $smarty->debug_tpl;
}
/**
* Get default modifiers
*
* @api Smarty::getDefaultModifiers()
*
* @return array list of default modifiers
*/
public function getDefaultModifiers()
{
$smarty = $this->_getSmartyObj();
return $smarty->default_modifiers;
}
/**
* return a reference to a registered object
*
* @api Smarty::getRegisteredObject()
* @link https://www.smarty.net/docs/en/api.get.registered.object.tpl
*
* @param string $object_name object name
*
* @return object
* @throws \SmartyException if no such object is found
*/
public function getRegisteredObject($object_name)
{
$smarty = $this->_getSmartyObj();
if (!isset($smarty->registered_objects[ $object_name ])) {
throw new SmartyException("'$object_name' is not a registered object");
}
if (!is_object($smarty->registered_objects[ $object_name ][ 0 ])) {
throw new SmartyException("registered '$object_name' is not an object");
}
return $smarty->registered_objects[ $object_name ][ 0 ];
}
/**
* Get literals
*
* @api Smarty::getLiterals()
*
* @return array list of literals
*/
public function getLiterals()
{
$smarty = $this->_getSmartyObj();
return (array)$smarty->literals;
}
/**
* Add literals
*
* @param array|string $literals literal or list of literals
* to addto add
*
* @return Smarty_Internal_TemplateBase
* @throws \SmartyException
* @api Smarty::addLiterals()
*
*/
public function addLiterals($literals = null)
{
if (isset($literals)) {
$this->_setLiterals($this->_getSmartyObj(), (array)$literals);
}
return $this;
}
/**
* Set literals
*
* @param array|string $literals literal or list of literals
* to setto set
*
* @return Smarty_Internal_TemplateBase
* @throws \SmartyException
* @api Smarty::setLiterals()
*
*/
public function setLiterals($literals = null)
{
$smarty = $this->_getSmartyObj();
$smarty->literals = array();
if (!empty($literals)) {
$this->_setLiterals($smarty, (array)$literals);
}
return $this;
}
/**
* common setter for literals for easier handling of duplicates the
* Smarty::$literals array gets filled with identical key values
*
* @param Smarty $smarty
* @param array $literals
*
* @throws \SmartyException
*/
private function _setLiterals(Smarty $smarty, $literals)
{
$literals = array_combine($literals, $literals);
$error = isset($literals[ $smarty->left_delimiter ]) ? array($smarty->left_delimiter) : array();
$error = isset($literals[ $smarty->right_delimiter ]) ? $error[] = $smarty->right_delimiter : $error;
if (!empty($error)) {
throw new SmartyException(
'User defined literal(s) "' . $error .
'" may not be identical with left or right delimiter'
);
}
$smarty->literals = array_merge((array)$smarty->literals, (array)$literals);
}
/**
* Check if filter type is valid
*
* @param string $type
*
* @throws \SmartyException
*/
private function _checkFilterType($type)
{
if (!isset($this->filterTypes[ $type ])) {
throw new SmartyException("Illegal filter type '{$type}'");
}
}
/**
* Return internal filter name
*
* @param callback $function_name
*
* @return string internal filter name
*/
private function _getFilterName($function_name)
{
if (is_array($function_name)) {
$_class_name = (is_object($function_name[ 0 ]) ? get_class($function_name[ 0 ]) : $function_name[ 0 ]);
return $_class_name . '_' . $function_name[ 1 ];
} elseif (is_string($function_name)) {
return $function_name;
} else {
return 'closure';
}
}
}