123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- <?php
- class Smarty_Internal_Runtime_TplFunction
- {
-
- private $tplFunctions = array();
-
- public function callTemplateFunction(Smarty_Internal_Template $tpl, $name, $params, $nocache)
- {
- if (isset($this->tplFunctions[ $name ])) {
- if (!$tpl->caching || ($tpl->caching && $nocache)) {
- $function = $this->tplFunctions[ $name ][ 'call_name' ];
- } else {
- if (isset($this->tplFunctions[ $name ][ 'call_name_caching' ])) {
- $function = $this->tplFunctions[ $name ][ 'call_name_caching' ];
- } else {
- $function = $this->tplFunctions[ $name ][ 'call_name' ];
- }
- }
- if (function_exists($function)) {
- $this->saveTemplateVariables($tpl, $name);
- $function ($tpl, $params);
- $this->restoreTemplateVariables($tpl, $name);
- return;
- }
-
- if ($this->addTplFuncToCache($tpl, $name, $function)) {
- $this->saveTemplateVariables($tpl, $name);
- $function ($tpl, $params);
- $this->restoreTemplateVariables($tpl, $name);
- return;
- }
- }
- throw new SmartyException("Unable to find template function '{$name}'");
- }
-
- public function registerTplFunctions(Smarty_Internal_Template $tpl, $tplFunctions)
- {
- $this->tplFunctions = array_merge($this->tplFunctions, $tplFunctions);
- $ptr = $tpl;
-
- while (isset($ptr->parent) && $ptr->parent->_objType === 2 && !isset($ptr->ext->_tplFunction)) {
- $ptr->ext->_tplFunction = $this;
- $ptr = $ptr->parent;
- }
- }
-
- public function getTplFunction($name = null)
- {
- if (isset($name)) {
- return $this->tplFunctions[ $name ] ? $this->tplFunctions[ $name ] : false;
- } else {
- return $this->tplFunctions;
- }
- }
-
- public function addTplFuncToCache(Smarty_Internal_Template $tpl, $_name, $_function)
- {
- $funcParam = $this->tplFunctions[ $_name ];
- if (is_file($funcParam[ 'compiled_filepath' ])) {
-
- $code = file_get_contents($funcParam[ 'compiled_filepath' ]);
-
- if (preg_match("/\/\* {$_function} \*\/([\S\s]*?)\/\*\/ {$_function} \*\//", $code, $match)) {
-
- preg_match("/\s*'{$funcParam['uid']}'([\S\s]*?)\),/", $code, $match1);
- unset($code);
-
- eval($match[ 0 ]);
- if (function_exists($_function)) {
-
- $tplPtr = $tpl;
- while (!isset($tplPtr->cached) && isset($tplPtr->parent)) {
- $tplPtr = $tplPtr->parent;
- }
-
- if (isset($tplPtr->cached)) {
-
- $cache = $tplPtr->cached;
- $content = $cache->read($tplPtr);
- if ($content) {
-
- if (!preg_match("/'{$funcParam['uid']}'(.*?)'nocache_hash'/", $content, $match2)) {
- $content = preg_replace("/('file_dependency'(.*?)\()/", "\\1{$match1[0]}", $content);
- }
- $tplPtr->smarty->ext->_updateCache->write($cache, $tplPtr,
- preg_replace('/\s*\?>\s*$/', "\n", $content) .
- "\n" . preg_replace(array('/^\s*<\?php\s+/',
- '/\s*\?>\s*$/',), "\n",
- $match[ 0 ]));
- }
- }
- return true;
- }
- }
- }
- return false;
- }
-
- public function saveTemplateVariables(Smarty_Internal_Template $tpl, $name)
- {
- $tpl->_cache[ 'varStack' ][] =
- array('tpl' => $tpl->tpl_vars, 'config' => $tpl->config_vars, 'name' => "_tplFunction_{$name}");
- }
-
- public function restoreTemplateVariables(Smarty_Internal_Template $tpl, $name)
- {
- if (isset($tpl->_cache[ 'varStack' ])) {
- $vars = array_pop($tpl->_cache[ 'varStack' ]);
- $tpl->tpl_vars = $vars[ 'tpl' ];
- $tpl->config_vars = $vars[ 'config' ];
- }
- }
- }
|