You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

smarty_internal_runtime_tplfunction.php 3.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * Tplfunc Runtime Methods callTemplateFunction
  4. *
  5. * @package Smarty
  6. * @subpackage PluginsInternal
  7. * @author Uwe Tews
  8. *
  9. **/
  10. class Smarty_Internal_Runtime_TplFunction
  11. {
  12. /**
  13. * Call template function
  14. *
  15. * @param \Smarty_Internal_Template $tpl template object
  16. * @param string $name template function name
  17. * @param array $params parameter array
  18. * @param bool $nocache true if called nocache
  19. *
  20. * @throws \SmartyException
  21. */
  22. public function callTemplateFunction(Smarty_Internal_Template $tpl, $name, $params, $nocache)
  23. {
  24. if (isset($tpl->tpl_function[$name])) {
  25. if (!$tpl->caching || ($tpl->caching && $nocache)) {
  26. $function = $tpl->tpl_function[$name]['call_name'];
  27. } else {
  28. if (isset($tpl->tpl_function[$name]['call_name_caching'])) {
  29. $function = $tpl->tpl_function[$name]['call_name_caching'];
  30. } else {
  31. $function = $tpl->tpl_function[$name]['call_name'];
  32. }
  33. }
  34. if (function_exists($function)) {
  35. $function ($tpl, $params);
  36. return;
  37. }
  38. // try to load template function dynamically
  39. if ($this->addTplFuncToCache($tpl, $name, $function)) {
  40. $function ($tpl, $params);
  41. return;
  42. }
  43. }
  44. throw new SmartyException("Unable to find template function '{$name}'");
  45. }
  46. /**
  47. *
  48. * Add template function to cache file for nocache calls
  49. *
  50. * @param Smarty_Internal_Template $tpl
  51. * @param string $_name template function name
  52. * @param string $_function PHP function name
  53. *
  54. * @return bool
  55. */
  56. public function addTplFuncToCache(Smarty_Internal_Template $tpl, $_name, $_function)
  57. {
  58. $funcParam = $tpl->tpl_function[$_name];
  59. if (is_file($funcParam['compiled_filepath'])) {
  60. // read compiled file
  61. $code = file_get_contents($funcParam['compiled_filepath']);
  62. // grab template function
  63. if (preg_match("/\/\* {$_function} \*\/([\S\s]*?)\/\*\/ {$_function} \*\//", $code, $match)) {
  64. // grab source info from file dependency
  65. preg_match("/\s*'{$funcParam['uid']}'([\S\s]*?)\),/", $code, $match1);
  66. unset($code);
  67. // make PHP function known
  68. eval($match[0]);
  69. if (function_exists($_function)) {
  70. // search cache file template
  71. $tplPtr = $tpl;
  72. while (!isset($tplPtr->cached) && isset($tplPtr->parent)) {
  73. $tplPtr = $tplPtr->parent;
  74. }
  75. // add template function code to cache file
  76. if (isset($tplPtr->cached)) {
  77. $cache = $tplPtr->cached;
  78. $content = $cache->read($tplPtr);
  79. if ($content) {
  80. // check if we must update file dependency
  81. if (!preg_match("/'{$funcParam['uid']}'(.*?)'nocache_hash'/", $content, $match2)) {
  82. $content = preg_replace("/('file_dependency'(.*?)\()/", "\\1{$match1[0]}", $content);
  83. }
  84. $tplPtr->smarty->ext->_updateCache->write($cache, $tplPtr, preg_replace('/\s*\?>\s*$/', "\n", $content) . "\n" .
  85. preg_replace(array('/^\s*<\?php\s+/', '/\s*\?>\s*$/'), "\n",
  86. $match[0]));
  87. }
  88. }
  89. return true;
  90. }
  91. }
  92. }
  93. return false;
  94. }
  95. }