| 
									
										
										
										
											2015-10-24 05:02:24 +02:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2016-10-08 06:07:14 +02:00
										 |  |  |  * TplFunction Runtime Methods callTemplateFunction | 
					
						
							| 
									
										
										
										
											2015-10-24 05:02:24 +02:00
										 |  |  |  * | 
					
						
							|  |  |  |  * @package    Smarty | 
					
						
							|  |  |  |  * @subpackage PluginsInternal | 
					
						
							|  |  |  |  * @author     Uwe Tews | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  **/ | 
					
						
							|  |  |  | class Smarty_Internal_Runtime_TplFunction | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Call template function
 | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param \Smarty_Internal_Template $tpl     template object | 
					
						
							|  |  |  |      * @param string                    $name    template function name | 
					
						
							|  |  |  |      * @param array                     $params  parameter array | 
					
						
							|  |  |  |      * @param bool                      $nocache true if called nocache | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @throws \SmartyException | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2015-12-19 20:10:45 +01:00
										 |  |  |     public function callTemplateFunction(Smarty_Internal_Template $tpl, $name, $params, $nocache) | 
					
						
							| 
									
										
										
										
											2015-10-24 05:02:24 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-10-08 06:07:14 +02:00
										 |  |  |         if (isset($tpl->tplFunctions[ $name ])) { | 
					
						
							| 
									
										
										
										
											2015-10-24 05:02:24 +02:00
										 |  |  |             if (!$tpl->caching || ($tpl->caching && $nocache)) { | 
					
						
							| 
									
										
										
										
											2016-10-08 06:07:14 +02:00
										 |  |  |                 $function = $tpl->tplFunctions[ $name ][ 'call_name' ]; | 
					
						
							| 
									
										
										
										
											2015-10-24 05:02:24 +02:00
										 |  |  |             } else { | 
					
						
							| 
									
										
										
										
											2016-10-08 06:07:14 +02:00
										 |  |  |                 if (isset($tpl->tplFunctions[ $name ][ 'call_name_caching' ])) { | 
					
						
							|  |  |  |                     $function = $tpl->tplFunctions[ $name ][ 'call_name_caching' ]; | 
					
						
							| 
									
										
										
										
											2015-10-24 05:02:24 +02:00
										 |  |  |                 } else { | 
					
						
							| 
									
										
										
										
											2016-10-08 06:07:14 +02:00
										 |  |  |                     $function = $tpl->tplFunctions[ $name ][ 'call_name' ]; | 
					
						
							| 
									
										
										
										
											2015-10-24 05:02:24 +02:00
										 |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if (function_exists($function)) { | 
					
						
							| 
									
										
										
										
											2016-02-09 23:27:07 +01:00
										 |  |  |                 $this->saveTemplateVariables($tpl, $name); | 
					
						
							| 
									
										
										
										
											2015-10-24 05:02:24 +02:00
										 |  |  |                 $function ($tpl, $params); | 
					
						
							| 
									
										
										
										
											2016-02-09 23:27:07 +01:00
										 |  |  |                 $this->restoreTemplateVariables($tpl, $name); | 
					
						
							| 
									
										
										
										
											2015-10-24 05:02:24 +02:00
										 |  |  |                 return; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             // try to load template function dynamically
 | 
					
						
							|  |  |  |             if ($this->addTplFuncToCache($tpl, $name, $function)) { | 
					
						
							| 
									
										
										
										
											2016-02-09 23:27:07 +01:00
										 |  |  |                 $this->saveTemplateVariables($tpl, $name); | 
					
						
							| 
									
										
										
										
											2015-10-24 05:02:24 +02:00
										 |  |  |                 $function ($tpl, $params); | 
					
						
							| 
									
										
										
										
											2016-02-09 23:27:07 +01:00
										 |  |  |                 $this->restoreTemplateVariables($tpl, $name); | 
					
						
							| 
									
										
										
										
											2015-10-24 05:02:24 +02:00
										 |  |  |                 return; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         throw new SmartyException("Unable to find template function '{$name}'"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-04 23:10:26 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Register template functions defined by template | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param \Smarty_Internal_Template $tpl | 
					
						
							|  |  |  |      * @param  array                    $tplFunctions source information array of template functions defined in template | 
					
						
							| 
									
										
										
										
											2016-10-08 06:07:14 +02:00
										 |  |  |      * @param bool                      $override if true replace existing functions with same name | 
					
						
							| 
									
										
										
										
											2016-08-04 23:10:26 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-10-08 06:07:14 +02:00
										 |  |  |     public function registerTplFunctions(Smarty_Internal_Template $tpl, $tplFunctions, $override = true) | 
					
						
							| 
									
										
										
										
											2016-08-04 23:10:26 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-10-08 06:07:14 +02:00
										 |  |  |         $tpl->tplFunctions = $override ? array_merge($tpl->tplFunctions, $tplFunctions) : array_merge($tplFunctions, $tpl->tplFunctions); | 
					
						
							| 
									
										
										
										
											2016-08-04 23:10:26 +02:00
										 |  |  |         // make sure that the template functions are known in parent templates
 | 
					
						
							| 
									
										
										
										
											2016-10-08 06:07:14 +02:00
										 |  |  |         if ($tpl->_isSubTpl()) { | 
					
						
							|  |  |  |             $tpl->smarty->ext->_tplFunction->registerTplFunctions($tpl->parent,$tplFunctions, false); | 
					
						
							| 
									
										
										
										
											2016-08-04 23:10:26 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Return source parameter array for single or all template functions | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-10-08 06:07:14 +02:00
										 |  |  |      * @param \Smarty_Internal_Template $tpl template object | 
					
						
							|  |  |  |      * @param null|string               $name template function name | 
					
						
							| 
									
										
										
										
											2016-08-04 23:10:26 +02:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return array|bool|mixed | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-10-08 06:07:14 +02:00
										 |  |  |     public function getTplFunction(Smarty_Internal_Template $tpl, $name = null) | 
					
						
							| 
									
										
										
										
											2016-08-04 23:10:26 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         if (isset($name)) { | 
					
						
							| 
									
										
										
										
											2016-10-08 06:07:14 +02:00
										 |  |  |             return isset($tpl->tplFunctions[ $name ]) ? $tpl->tplFunctions[ $name ] : false; | 
					
						
							| 
									
										
										
										
											2016-08-04 23:10:26 +02:00
										 |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2016-10-08 06:07:14 +02:00
										 |  |  |             return $tpl->tplFunctions; | 
					
						
							| 
									
										
										
										
											2016-08-04 23:10:26 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-24 05:02:24 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * Add template function to cache file for nocache calls | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param Smarty_Internal_Template $tpl | 
					
						
							|  |  |  |      * @param string                   $_name     template function name | 
					
						
							|  |  |  |      * @param string                   $_function PHP function name | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function addTplFuncToCache(Smarty_Internal_Template $tpl, $_name, $_function) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-10-08 06:07:14 +02:00
										 |  |  |         $funcParam = $tpl->tplFunctions[ $_name ]; | 
					
						
							| 
									
										
										
										
											2016-02-09 01:27:15 +01:00
										 |  |  |         if (is_file($funcParam[ 'compiled_filepath' ])) { | 
					
						
							| 
									
										
										
										
											2015-10-24 05:02:24 +02:00
										 |  |  |             // read compiled file
 | 
					
						
							| 
									
										
										
										
											2016-02-09 01:27:15 +01:00
										 |  |  |             $code = file_get_contents($funcParam[ 'compiled_filepath' ]); | 
					
						
							| 
									
										
										
										
											2015-10-24 05:02:24 +02:00
										 |  |  |             // grab template function
 | 
					
						
							|  |  |  |             if (preg_match("/\/\* {$_function} \*\/([\S\s]*?)\/\*\/ {$_function} \*\//", $code, $match)) { | 
					
						
							|  |  |  |                 // grab source info from file dependency
 | 
					
						
							|  |  |  |                 preg_match("/\s*'{$funcParam['uid']}'([\S\s]*?)\),/", $code, $match1); | 
					
						
							|  |  |  |                 unset($code); | 
					
						
							|  |  |  |                 // make PHP function known
 | 
					
						
							| 
									
										
										
										
											2016-02-09 01:27:15 +01:00
										 |  |  |                 eval($match[ 0 ]); | 
					
						
							| 
									
										
										
										
											2015-10-24 05:02:24 +02:00
										 |  |  |                 if (function_exists($_function)) { | 
					
						
							|  |  |  |                     // search cache file template
 | 
					
						
							|  |  |  |                     $tplPtr = $tpl; | 
					
						
							|  |  |  |                     while (!isset($tplPtr->cached) && isset($tplPtr->parent)) { | 
					
						
							|  |  |  |                         $tplPtr = $tplPtr->parent; | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                     // add template function code to cache file
 | 
					
						
							|  |  |  |                     if (isset($tplPtr->cached)) { | 
					
						
							| 
									
										
										
										
											2016-10-08 06:07:14 +02:00
										 |  |  |                         /* @var Smarty_Template_Cached $cache */ | 
					
						
							| 
									
										
										
										
											2015-10-24 05:02:24 +02:00
										 |  |  |                         $cache = $tplPtr->cached; | 
					
						
							|  |  |  |                         $content = $cache->read($tplPtr); | 
					
						
							|  |  |  |                         if ($content) { | 
					
						
							|  |  |  |                             // check if we must update file dependency
 | 
					
						
							|  |  |  |                             if (!preg_match("/'{$funcParam['uid']}'(.*?)'nocache_hash'/", $content, $match2)) { | 
					
						
							|  |  |  |                                 $content = preg_replace("/('file_dependency'(.*?)\()/", "\\1{$match1[0]}", $content); | 
					
						
							|  |  |  |                             } | 
					
						
							| 
									
										
										
										
											2016-02-09 01:27:15 +01:00
										 |  |  |                             $tplPtr->smarty->ext->_updateCache->write($cache, $tplPtr, | 
					
						
							|  |  |  |                                                                       preg_replace('/\s*\?>\s*$/', "\n", $content) . | 
					
						
							|  |  |  |                                                                       "\n" . preg_replace(array('/^\s*<\?php\s+/', | 
					
						
							| 
									
										
										
										
											2016-02-09 23:27:07 +01:00
										 |  |  |                                                                                                 '/\s*\?>\s*$/',), "\n", | 
					
						
							| 
									
										
										
										
											2016-02-09 01:27:15 +01:00
										 |  |  |                                                                                           $match[ 0 ])); | 
					
						
							| 
									
										
										
										
											2015-10-24 05:02:24 +02:00
										 |  |  |                         } | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                     return true; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-02-09 23:27:07 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Save current template variables on stack | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param \Smarty_Internal_Template $tpl | 
					
						
							|  |  |  |      * @param  string                   $name stack name | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function saveTemplateVariables(Smarty_Internal_Template $tpl, $name) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $tpl->_cache[ 'varStack' ][] = | 
					
						
							|  |  |  |             array('tpl' => $tpl->tpl_vars, 'config' => $tpl->config_vars, 'name' => "_tplFunction_{$name}"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Restore saved variables into template objects | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param \Smarty_Internal_Template $tpl | 
					
						
							|  |  |  |      * @param  string                   $name stack 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' ]; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-10-24 05:02:24 +02:00
										 |  |  | } |