smarty_internal_compile_function.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Compile Function
  4. * Compiles the {function} {/function} tags
  5. *
  6. * @package Smarty
  7. * @subpackage Compiler
  8. * @author Uwe Tews
  9. */
  10. /**
  11. * Smarty Internal Plugin Compile Function Class
  12. *
  13. * @package Smarty
  14. * @subpackage Compiler
  15. */
  16. class Smarty_Internal_Compile_Function extends Smarty_Internal_CompileBase
  17. {
  18. /**
  19. * Attribute definition: Overwrites base class.
  20. *
  21. * @var array
  22. * @see Smarty_Internal_CompileBase
  23. */
  24. public $required_attributes = array('name');
  25. /**
  26. * Attribute definition: Overwrites base class.
  27. *
  28. * @var array
  29. * @see Smarty_Internal_CompileBase
  30. */
  31. public $shorttag_order = array('name');
  32. /**
  33. * Attribute definition: Overwrites base class.
  34. *
  35. * @var array
  36. * @see Smarty_Internal_CompileBase
  37. */
  38. public $optional_attributes = array('_any');
  39. /**
  40. * Compiles code for the {function} tag
  41. *
  42. * @param array $args array with attributes from parser
  43. * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
  44. * @param array $parameter array with compilation parameter
  45. *
  46. * @return bool true
  47. * @throws \SmartyCompilerException
  48. */
  49. public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
  50. {
  51. // check and get attributes
  52. $_attr = $this->getAttributes($compiler, $args);
  53. if ($_attr[ 'nocache' ] === true) {
  54. $compiler->trigger_template_error('nocache option not allowed', null, true);
  55. }
  56. unset($_attr[ 'nocache' ]);
  57. $_name = trim($_attr[ 'name' ], "'\"");
  58. $compiler->parent_compiler->tpl_function[ $_name ] = array();
  59. $save = array($_attr, $compiler->parser->current_buffer, $compiler->template->compiled->has_nocache_code,
  60. $compiler->template->caching);
  61. $this->openTag($compiler, 'function', $save);
  62. // Init temporary context
  63. $compiler->parser->current_buffer = new Smarty_Internal_ParseTree_Template();
  64. $compiler->template->compiled->has_nocache_code = false;
  65. return true;
  66. }
  67. }
  68. /**
  69. * Smarty Internal Plugin Compile Functionclose Class
  70. *
  71. * @package Smarty
  72. * @subpackage Compiler
  73. */
  74. class Smarty_Internal_Compile_Functionclose extends Smarty_Internal_CompileBase
  75. {
  76. /**
  77. * Compiler object
  78. *
  79. * @var object
  80. */
  81. private $compiler = null;
  82. /**
  83. * Compiles code for the {/function} tag
  84. *
  85. * @param array $args array with attributes from parser
  86. * @param object|\Smarty_Internal_TemplateCompilerBase $compiler compiler object
  87. * @param array $parameter array with compilation parameter
  88. *
  89. * @return bool true
  90. */
  91. public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
  92. {
  93. $this->compiler = $compiler;
  94. $saved_data = $this->closeTag($compiler, array('function'));
  95. $_attr = $saved_data[ 0 ];
  96. $_name = trim($_attr[ 'name' ], "'\"");
  97. $compiler->parent_compiler->tpl_function[ $_name ][ 'compiled_filepath' ] =
  98. $compiler->parent_compiler->template->compiled->filepath;
  99. $compiler->parent_compiler->tpl_function[ $_name ][ 'uid' ] = $compiler->template->source->uid;
  100. $_parameter = $_attr;
  101. unset($_parameter[ 'name' ]);
  102. // default parameter
  103. $_paramsArray = array();
  104. foreach ($_parameter as $_key => $_value) {
  105. if (is_int($_key)) {
  106. $_paramsArray[] = "$_key=>$_value";
  107. } else {
  108. $_paramsArray[] = "'$_key'=>$_value";
  109. }
  110. }
  111. if (!empty($_paramsArray)) {
  112. $_params = 'array(' . implode(",", $_paramsArray) . ')';
  113. $_paramsCode = "\$params = array_merge($_params, \$params);\n";
  114. } else {
  115. $_paramsCode = '';
  116. }
  117. $_functionCode = $compiler->parser->current_buffer;
  118. // setup buffer for template function code
  119. $compiler->parser->current_buffer = new Smarty_Internal_ParseTree_Template();
  120. $_funcName = "smarty_template_function_{$_name}_{$compiler->template->compiled->nocache_hash}";
  121. $_funcNameCaching = $_funcName . '_nocache';
  122. if ($compiler->template->compiled->has_nocache_code) {
  123. $compiler->parent_compiler->tpl_function[ $_name ][ 'call_name_caching' ] = $_funcNameCaching;
  124. $output = "<?php\n";
  125. $output .= "/* {$_funcNameCaching} */\n";
  126. $output .= "if (!function_exists('{$_funcNameCaching}')) {\n";
  127. $output .= "function {$_funcNameCaching} (\$_smarty_tpl,\$params) {\n";
  128. $output .= "ob_start();\n";
  129. $output .= "\$_smarty_tpl->compiled->has_nocache_code = true;\n";
  130. $output .= $_paramsCode;
  131. $output .= "foreach (\$params as \$key => \$value) {\n\$_smarty_tpl->tpl_vars[\$key] = new Smarty_Variable(\$value, \$_smarty_tpl->isRenderingCache);\n}";
  132. $output .= "\$params = var_export(\$params, true);\n";
  133. $output .= "echo \"/*%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%*/<?php ";
  134. $output .= "\\\$_smarty_tpl->smarty->ext->_tplFunction->saveTemplateVariables(\\\$_smarty_tpl, '{$_name}');\nforeach (\$params as \\\$key => \\\$value) {\n\\\$_smarty_tpl->tpl_vars[\\\$key] = new Smarty_Variable(\\\$value, \\\$_smarty_tpl->isRenderingCache);\n}\n?>";
  135. $output .= "/*/%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%*/\n\";?>";
  136. $compiler->parser->current_buffer->append_subtree($compiler->parser,
  137. new Smarty_Internal_ParseTree_Tag($compiler->parser,
  138. $output));
  139. $compiler->parser->current_buffer->append_subtree($compiler->parser, $_functionCode);
  140. $output = "<?php echo \"/*%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%*/<?php ";
  141. $output .= "\\\$_smarty_tpl->smarty->ext->_tplFunction->restoreTemplateVariables(\\\$_smarty_tpl, '{$_name}');?>\n";
  142. $output .= "/*/%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%*/\";\n?>";
  143. $output .= "<?php echo str_replace('{$compiler->template->compiled->nocache_hash}', \$_smarty_tpl->compiled->nocache_hash, ob_get_clean());\n";
  144. $output .= "}\n}\n";
  145. $output .= "/*/ {$_funcName}_nocache */\n\n";
  146. $output .= "?>\n";
  147. $compiler->parser->current_buffer->append_subtree($compiler->parser,
  148. new Smarty_Internal_ParseTree_Tag($compiler->parser,
  149. $output));
  150. $_functionCode = new Smarty_Internal_ParseTree_Tag($compiler->parser,
  151. preg_replace_callback("/((<\?php )?echo '\/\*%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%\*\/([\S\s]*?)\/\*\/%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%\*\/';(\?>\n)?)/",
  152. array($this, 'removeNocache'),
  153. $_functionCode->to_smarty_php($compiler->parser)));
  154. }
  155. $compiler->parent_compiler->tpl_function[ $_name ][ 'call_name' ] = $_funcName;
  156. $output = "<?php\n";
  157. $output .= "/* {$_funcName} */\n";
  158. $output .= "if (!function_exists('{$_funcName}')) {\n";
  159. $output .= "function {$_funcName}(\$_smarty_tpl,\$params) {\n";
  160. $output .= $_paramsCode;
  161. $output .= "foreach (\$params as \$key => \$value) {\n\$_smarty_tpl->tpl_vars[\$key] = new Smarty_Variable(\$value, \$_smarty_tpl->isRenderingCache);\n}?>";
  162. $compiler->parser->current_buffer->append_subtree($compiler->parser,
  163. new Smarty_Internal_ParseTree_Tag($compiler->parser,
  164. $output));
  165. $compiler->parser->current_buffer->append_subtree($compiler->parser, $_functionCode);
  166. $output = "<?php\n}}\n";
  167. $output .= "/*/ {$_funcName} */\n\n";
  168. $output .= "?>\n";
  169. $compiler->parser->current_buffer->append_subtree($compiler->parser,
  170. new Smarty_Internal_ParseTree_Tag($compiler->parser,
  171. $output));
  172. $compiler->parent_compiler->blockOrFunctionCode .= $compiler->parser->current_buffer->to_smarty_php($compiler->parser);
  173. // nocache plugins must be copied
  174. if (!empty($compiler->template->compiled->required_plugins[ 'nocache' ])) {
  175. foreach ($compiler->template->compiled->required_plugins[ 'nocache' ] as $plugin => $tmp) {
  176. foreach ($tmp as $type => $data) {
  177. $compiler->parent_compiler->template->compiled->required_plugins[ 'compiled' ][ $plugin ][ $type ] =
  178. $data;
  179. }
  180. }
  181. }
  182. // restore old buffer
  183. $compiler->parser->current_buffer = $saved_data[ 1 ];
  184. // restore old status
  185. $compiler->template->compiled->has_nocache_code = $saved_data[ 2 ];
  186. $compiler->template->caching = $saved_data[ 3 ];
  187. return true;
  188. }
  189. /**
  190. * Remove nocache code
  191. *
  192. * @param $match
  193. *
  194. * @return string
  195. */
  196. function removeNocache($match)
  197. {
  198. $code =
  199. preg_replace("/((<\?php )?echo '\/\*%%SmartyNocache:{$this->compiler->template->compiled->nocache_hash}%%\*\/)|(\/\*\/%%SmartyNocache:{$this->compiler->template->compiled->nocache_hash}%%\*\/';(\?>\n)?)/",
  200. '', $match[ 0 ]);
  201. $code = str_replace(array('\\\'', '\\\\\''), array('\'', '\\\''), $code);
  202. return $code;
  203. }
  204. }