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_compile_function.php 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. *
  45. * @return bool true
  46. * @throws \SmartyCompilerException
  47. */
  48. public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler)
  49. {
  50. // check and get attributes
  51. $_attr = $this->getAttributes($compiler, $args);
  52. if ($_attr[ 'nocache' ] === true) {
  53. $compiler->trigger_template_error('nocache option not allowed', null, true);
  54. }
  55. unset($_attr[ 'nocache' ]);
  56. $_name = trim($_attr[ 'name' ], '\'"');
  57. $compiler->parent_compiler->tpl_function[ $_name ] = array();
  58. $save = array($_attr, $compiler->parser->current_buffer, $compiler->template->compiled->has_nocache_code,
  59. $compiler->template->caching);
  60. $this->openTag($compiler, 'function', $save);
  61. // Init temporary context
  62. $compiler->parser->current_buffer = new Smarty_Internal_ParseTree_Template();
  63. $compiler->template->compiled->has_nocache_code = false;
  64. $compiler->saveRequiredPlugins(true);
  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. *
  88. * @return bool true
  89. */
  90. public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler)
  91. {
  92. $this->compiler = $compiler;
  93. $saved_data = $this->closeTag($compiler, array('function'));
  94. $_attr = $saved_data[ 0 ];
  95. $_name = trim($_attr[ 'name' ], '\'"');
  96. $compiler->parent_compiler->tpl_function[ $_name ][ 'compiled_filepath' ] =
  97. $compiler->parent_compiler->template->compiled->filepath;
  98. $compiler->parent_compiler->tpl_function[ $_name ][ 'uid' ] = $compiler->template->source->uid;
  99. $_parameter = $_attr;
  100. unset($_parameter[ 'name' ]);
  101. // default parameter
  102. $_paramsArray = array();
  103. foreach ($_parameter as $_key => $_value) {
  104. if (is_int($_key)) {
  105. $_paramsArray[] = "$_key=>$_value";
  106. } else {
  107. $_paramsArray[] = "'$_key'=>$_value";
  108. }
  109. }
  110. if (!empty($_paramsArray)) {
  111. $_params = 'array(' . implode(',', $_paramsArray) . ')';
  112. $_paramsCode = "\$params = array_merge($_params, \$params);\n";
  113. } else {
  114. $_paramsCode = '';
  115. }
  116. $_functionCode = $compiler->parser->current_buffer;
  117. // setup buffer for template function code
  118. $compiler->parser->current_buffer = new Smarty_Internal_ParseTree_Template();
  119. $_funcName = "smarty_template_function_{$_name}_{$compiler->template->compiled->nocache_hash}";
  120. $_funcNameCaching = $_funcName . '_nocache';
  121. if ($compiler->template->compiled->has_nocache_code) {
  122. $compiler->parent_compiler->tpl_function[ $_name ][ 'call_name_caching' ] = $_funcNameCaching;
  123. $output = "<?php\n";
  124. $output .= "/* {$_funcNameCaching} */\n";
  125. $output .= "if (!function_exists('{$_funcNameCaching}')) {\n";
  126. $output .= "function {$_funcNameCaching} (Smarty_Internal_Template \$_smarty_tpl,\$params) {\n";
  127. $output .= "ob_start();\n";
  128. $output .= $compiler->compileRequiredPlugins();
  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}\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}%%*/\";?>";
  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_Internal_Template \$_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}\n";
  162. $output .= $compiler->compileCheckPlugins(array_merge($compiler->required_plugins[ 'compiled' ], $compiler->required_plugins[ 'nocache' ]));
  163. $output .= "?>\n";
  164. $compiler->parser->current_buffer->append_subtree($compiler->parser,
  165. new Smarty_Internal_ParseTree_Tag($compiler->parser,
  166. $output));
  167. $compiler->parser->current_buffer->append_subtree($compiler->parser, $_functionCode);
  168. $output = "<?php\n}}\n";
  169. $output .= "/*/ {$_funcName} */\n\n";
  170. $output .= "?>\n";
  171. $compiler->parser->current_buffer->append_subtree($compiler->parser,
  172. new Smarty_Internal_ParseTree_Tag($compiler->parser,
  173. $output));
  174. $compiler->parent_compiler->blockOrFunctionCode .= $compiler->parser->current_buffer->to_smarty_php($compiler->parser);
  175. // restore old buffer
  176. $compiler->parser->current_buffer = $saved_data[ 1 ];
  177. // restore old status
  178. $compiler->restoreRequiredPlugins();
  179. $compiler->template->compiled->has_nocache_code = $saved_data[ 2 ];
  180. $compiler->template->caching = $saved_data[ 3 ];
  181. return true;
  182. }
  183. /**
  184. * Remove nocache code
  185. *
  186. * @param $match
  187. *
  188. * @return string
  189. */
  190. function removeNocache($match)
  191. {
  192. $code =
  193. preg_replace("/((<\?php )?echo '\/\*%%SmartyNocache:{$this->compiler->template->compiled->nocache_hash}%%\*\/)|(\/\*\/%%SmartyNocache:{$this->compiler->template->compiled->nocache_hash}%%\*\/';(\?>\n)?)/",
  194. '', $match[ 0 ]);
  195. $code = str_replace(array('\\\'', '\\\\\''), array('\'', '\\\''), $code);
  196. return $code;
  197. }
  198. }