]> git.mxchange.org Git - friendica.git/blob - vendor/smarty/smarty/libs/sysplugins/smarty_internal_runtime_tplfunction.php
Add Smarty to Composer
[friendica.git] / vendor / smarty / smarty / libs / sysplugins / smarty_internal_runtime_tplfunction.php
1 <?php
2
3 /**
4  * TplFunction Runtime Methods callTemplateFunction
5  *
6  * @package    Smarty
7  * @subpackage PluginsInternal
8  * @author     Uwe Tews
9  *
10  **/
11 class Smarty_Internal_Runtime_TplFunction
12 {
13     /**
14      * Call template function
15      *
16      * @param \Smarty_Internal_Template $tpl     template object
17      * @param string                    $name    template function name
18      * @param array                     $params  parameter array
19      * @param bool                      $nocache true if called nocache
20      *
21      * @throws \SmartyException
22      */
23     public function callTemplateFunction(Smarty_Internal_Template $tpl, $name, $params, $nocache)
24     {
25         $funcParam = isset($tpl->tplFunctions[ $name ]) ? $tpl->tplFunctions[ $name ] :
26             (isset($tpl->smarty->tplFunctions[ $name ]) ? $tpl->smarty->tplFunctions[ $name ] : null);
27         if (isset($funcParam)) {
28             if (!$tpl->caching || ($tpl->caching && $nocache)) {
29                 $function = $funcParam[ 'call_name' ];
30             } else {
31                 if (isset($funcParam[ 'call_name_caching' ])) {
32                     $function = $funcParam[ 'call_name_caching' ];
33                 } else {
34                     $function = $funcParam[ 'call_name' ];
35                 }
36             }
37             if (function_exists($function)) {
38                 $this->saveTemplateVariables($tpl, $name);
39                 $function ($tpl, $params);
40                 $this->restoreTemplateVariables($tpl, $name);
41                 return;
42             }
43             // try to load template function dynamically
44             if ($this->addTplFuncToCache($tpl, $name, $function)) {
45                 $this->saveTemplateVariables($tpl, $name);
46                 $function ($tpl, $params);
47                 $this->restoreTemplateVariables($tpl, $name);
48                 return;
49             }
50         }
51         throw new SmartyException("Unable to find template function '{$name}'");
52     }
53
54     /**
55      * Register template functions defined by template
56      *
57      * @param \Smarty|\Smarty_Internal_Template|\Smarty_Internal_TemplateBase $obj
58      * @param  array                                                          $tplFunctions source information array of template functions defined in template
59      * @param bool                                                            $override     if true replace existing functions with same name
60      */
61     public function registerTplFunctions(Smarty_Internal_TemplateBase $obj, $tplFunctions, $override = true)
62     {
63         $obj->tplFunctions =
64             $override ? array_merge($obj->tplFunctions, $tplFunctions) : array_merge($tplFunctions, $obj->tplFunctions);
65         // make sure that the template functions are known in parent templates
66         if ($obj->_isSubTpl()) {
67             $obj->smarty->ext->_tplFunction->registerTplFunctions($obj->parent, $tplFunctions, false);
68         } else {
69             $obj->smarty->tplFunctions = $override ? array_merge($obj->smarty->tplFunctions, $tplFunctions) :
70                 array_merge($tplFunctions, $obj->smarty->tplFunctions);
71         }
72     }
73
74     /**
75      * Return source parameter array for single or all template functions
76      *
77      * @param \Smarty_Internal_Template $tpl  template object
78      * @param null|string               $name template function name
79      *
80      * @return array|bool|mixed
81      */
82     public function getTplFunction(Smarty_Internal_Template $tpl, $name = null)
83     {
84         if (isset($name)) {
85             return isset($tpl->tplFunctions[ $name ]) ? $tpl->tplFunctions[ $name ] :
86                 (isset($tpl->smarty->tplFunctions[ $name ]) ? $tpl->smarty->tplFunctions[ $name ] : false);
87         } else {
88             return empty($tpl->tplFunctions) ? $tpl->smarty->tplFunctions : $tpl->tplFunctions;
89         }
90     }
91
92     /**
93      *
94      * Add template function to cache file for nocache calls
95      *
96      * @param Smarty_Internal_Template $tpl
97      * @param string                   $_name     template function name
98      * @param string                   $_function PHP function name
99      *
100      * @return bool
101      */
102     public function addTplFuncToCache(Smarty_Internal_Template $tpl, $_name, $_function)
103     {
104         $funcParam = $tpl->tplFunctions[ $_name ];
105         if (is_file($funcParam[ 'compiled_filepath' ])) {
106             // read compiled file
107             $code = file_get_contents($funcParam[ 'compiled_filepath' ]);
108             // grab template function
109             if (preg_match("/\/\* {$_function} \*\/([\S\s]*?)\/\*\/ {$_function} \*\//", $code, $match)) {
110                 // grab source info from file dependency
111                 preg_match("/\s*'{$funcParam['uid']}'([\S\s]*?)\),/", $code, $match1);
112                 unset($code);
113                 // make PHP function known
114                 eval($match[ 0 ]);
115                 if (function_exists($_function)) {
116                     // search cache file template
117                     $tplPtr = $tpl;
118                     while (!isset($tplPtr->cached) && isset($tplPtr->parent)) {
119                         $tplPtr = $tplPtr->parent;
120                     }
121                     // add template function code to cache file
122                     if (isset($tplPtr->cached)) {
123                         /* @var Smarty_Template_Cached $cache */
124                         $cache = $tplPtr->cached;
125                         $content = $cache->read($tplPtr);
126                         if ($content) {
127                             // check if we must update file dependency
128                             if (!preg_match("/'{$funcParam['uid']}'(.*?)'nocache_hash'/", $content, $match2)) {
129                                 $content = preg_replace("/('file_dependency'(.*?)\()/", "\\1{$match1[0]}", $content);
130                             }
131                             $tplPtr->smarty->ext->_updateCache->write($cache, $tplPtr,
132                                                                       preg_replace('/\s*\?>\s*$/', "\n", $content) .
133                                                                       "\n" . preg_replace(array('/^\s*<\?php\s+/',
134                                                                                                 '/\s*\?>\s*$/',), "\n",
135                                                                                           $match[ 0 ]));
136                         }
137                     }
138                     return true;
139                 }
140             }
141         }
142         return false;
143     }
144
145     /**
146      * Save current template variables on stack
147      *
148      * @param \Smarty_Internal_Template $tpl
149      * @param  string                   $name stack name
150      */
151     public function saveTemplateVariables(Smarty_Internal_Template $tpl, $name)
152     {
153         $tpl->_cache[ 'varStack' ][] =
154             array('tpl' => $tpl->tpl_vars, 'config' => $tpl->config_vars, 'name' => "_tplFunction_{$name}");
155     }
156
157     /**
158      * Restore saved variables into template objects
159      *
160      * @param \Smarty_Internal_Template $tpl
161      * @param  string                   $name stack name
162      */
163     public function restoreTemplateVariables(Smarty_Internal_Template $tpl, $name)
164     {
165         if (isset($tpl->_cache[ 'varStack' ])) {
166             $vars = array_pop($tpl->_cache[ 'varStack' ]);
167             $tpl->tpl_vars = $vars[ 'tpl' ];
168             $tpl->config_vars = $vars[ 'config' ];
169         }
170     }
171 }