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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. $compiler->loopNesting++;
  52. // check and get attributes
  53. $_attr = $this->getAttributes($compiler, $args);
  54. if ($_attr['nocache'] === true) {
  55. $compiler->trigger_template_error('nocache option not allowed', null, true);
  56. }
  57. unset($_attr['nocache']);
  58. $_name = trim($_attr['name'], "'\"");
  59. $compiler->parent_compiler->tpl_function[$_name] = $compiler->parent_compiler->template->tpl_function[$_name] = array();
  60. $save = array($_attr, $compiler->parser->current_buffer, $compiler->template->compiled->has_nocache_code,
  61. $compiler->template->caching);
  62. $this->openTag($compiler, 'function', $save);
  63. // Init temporary context
  64. $compiler->parser->current_buffer = new Smarty_Internal_ParseTree_Template();
  65. $compiler->template->compiled->has_nocache_code = false;
  66. return true;
  67. }
  68. }
  69. /**
  70. * Smarty Internal Plugin Compile Functionclose Class
  71. *
  72. * @package Smarty
  73. * @subpackage Compiler
  74. */
  75. class Smarty_Internal_Compile_Functionclose extends Smarty_Internal_CompileBase
  76. {
  77. /**
  78. * Compiler object
  79. *
  80. * @var object
  81. */
  82. private $compiler = null;
  83. /**
  84. * Compiles code for the {/function} tag
  85. *
  86. * @param array $args array with attributes from parser
  87. * @param object|\Smarty_Internal_TemplateCompilerBase $compiler compiler object
  88. * @param array $parameter array with compilation parameter
  89. *
  90. * @return bool true
  91. */
  92. public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
  93. {
  94. $compiler->loopNesting--;
  95. $this->compiler = $compiler;
  96. $saved_data = $this->closeTag($compiler, array('function'));
  97. $_attr = $saved_data[0];
  98. $_name = trim($_attr['name'], "'\"");
  99. $compiler->parent_compiler->tpl_function[$_name]['called_functions'] = $compiler->parent_compiler->template->tpl_function[$_name]['called_functions'] = $compiler->called_functions;
  100. $compiler->parent_compiler->tpl_function[$_name]['compiled_filepath'] = $compiler->parent_compiler->template->tpl_function[$_name]['compiled_filepath'] = $compiler->parent_compiler->template->compiled->filepath;
  101. $compiler->parent_compiler->tpl_function[$_name]['uid'] = $compiler->parent_compiler->template->tpl_function[$_name]['uid'] = $compiler->template->source->uid;
  102. $compiler->called_functions = array();
  103. $_parameter = $_attr;
  104. unset($_parameter['name']);
  105. // default parameter
  106. $_paramsArray = array();
  107. foreach ($_parameter as $_key => $_value) {
  108. if (is_int($_key)) {
  109. $_paramsArray[] = "$_key=>$_value";
  110. } else {
  111. $_paramsArray[] = "'$_key'=>$_value";
  112. }
  113. }
  114. if (!empty($_paramsArray)) {
  115. $_params = 'array(' . implode(",", $_paramsArray) . ')';
  116. $_paramsCode = "\$params = array_merge($_params, \$params);\n";
  117. } else {
  118. $_paramsCode = '';
  119. }
  120. $_functionCode = $compiler->parser->current_buffer;
  121. // setup buffer for template function code
  122. $compiler->parser->current_buffer = new Smarty_Internal_ParseTree_Template();
  123. $_funcName = "smarty_template_function_{$_name}_{$compiler->template->compiled->nocache_hash}";
  124. $_funcNameCaching = $_funcName . '_nocache';
  125. if ($compiler->template->compiled->has_nocache_code) {
  126. $compiler->parent_compiler->tpl_function[$_name]['call_name_caching'] = $compiler->parent_compiler->template->tpl_function[$_name]['call_name_caching'] = $_funcNameCaching;
  127. $output = "<?php\n";
  128. $output .= "/* {$_funcNameCaching} */\n";
  129. $output .= "if (!function_exists('{$_funcNameCaching}')) {\n";
  130. $output .= "function {$_funcNameCaching} (\$_smarty_tpl,\$params) {\n";
  131. $output .= "ob_start();\n";
  132. $output .= "\$_smarty_tpl->compiled->has_nocache_code = true;\n";
  133. $output .= $_paramsCode;
  134. $output .= "\$_smarty_tpl->_cache['saved_tpl_vars'][] = \$_smarty_tpl->tpl_vars;\n";
  135. $output .= "foreach (\$params as \$key => \$value) {\n\$_smarty_tpl->tpl_vars[\$key] = new Smarty_Variable(\$value);\n}";
  136. $output .= "\$params = var_export(\$params, true);\n";
  137. $output .= "echo \"/*%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%*/<?php ";
  138. $output .= "\\\$saved_tpl_vars = \\\$_smarty_tpl->tpl_vars;\nforeach (\$params as \\\$key => \\\$value) {\n\\\$_smarty_tpl->tpl_vars[\\\$key] = new Smarty_Variable(\\\$value);\n}\n?>";
  139. $output .= "/*/%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%*/\n\";?>";
  140. $compiler->parser->current_buffer->append_subtree($compiler->parser, new Smarty_Internal_ParseTree_Tag($compiler->parser, $output));
  141. $compiler->parser->current_buffer->append_subtree($compiler->parser, $_functionCode);
  142. $output = "<?php echo \"/*%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%*/<?php ";
  143. $output .= "foreach (Smarty::\\\$global_tpl_vars as \\\$key => \\\$value){\n";
  144. $output .= "if (!isset(\\\$_smarty_tpl->tpl_vars[\\\$key]) || \\\$_smarty_tpl->tpl_vars[\\\$key] === \\\$value) \\\$saved_tpl_vars[\\\$key] = \\\$value;\n}\n";
  145. $output .= "\\\$_smarty_tpl->tpl_vars = \\\$saved_tpl_vars;?>\n";
  146. $output .= "/*/%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%*/\";\n?>";
  147. $output .= "<?php echo str_replace('{$compiler->template->compiled->nocache_hash}', \$_smarty_tpl->compiled->nocache_hash, ob_get_clean());\n";
  148. $output .= "\$_smarty_tpl->tpl_vars = array_pop(\$_smarty_tpl->_cache['saved_tpl_vars']);\n}\n}\n";
  149. $output .= "/*/ {$_funcName}_nocache */\n\n";
  150. $output .= "?>\n";
  151. $compiler->parser->current_buffer->append_subtree($compiler->parser, new Smarty_Internal_ParseTree_Tag($compiler->parser, $output));
  152. $_functionCode = new Smarty_Internal_ParseTree_Tag($compiler->parser, preg_replace_callback("/((<\?php )?echo '\/\*%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%\*\/([\S\s]*?)\/\*\/%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%\*\/';(\?>\n)?)/", array($this,
  153. 'removeNocache'), $_functionCode->to_smarty_php($compiler->parser)));
  154. }
  155. $compiler->parent_compiler->tpl_function[$_name]['call_name'] = $compiler->parent_compiler->template->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 .= "\$saved_tpl_vars = \$_smarty_tpl->tpl_vars;\n";
  161. $output .= $_paramsCode;
  162. $output .= "foreach (\$params as \$key => \$value) {\n\$_smarty_tpl->tpl_vars[\$key] = new Smarty_Variable(\$value);\n}?>";
  163. $compiler->parser->current_buffer->append_subtree($compiler->parser, new Smarty_Internal_ParseTree_Tag($compiler->parser, $output));
  164. $compiler->parser->current_buffer->append_subtree($compiler->parser, $_functionCode);
  165. $output = "<?php foreach (Smarty::\$global_tpl_vars as \$key => \$value){\n";
  166. $output .= "if (!isset(\$_smarty_tpl->tpl_vars[\$key]) || \$_smarty_tpl->tpl_vars[\$key] === \$value) \$saved_tpl_vars[\$key] = \$value;\n}\n";
  167. $output .= "\$_smarty_tpl->tpl_vars = \$saved_tpl_vars;\n}\n}\n";
  168. $output .= "/*/ {$_funcName} */\n\n";
  169. $output .= "?>\n";
  170. $compiler->parser->current_buffer->append_subtree($compiler->parser, new Smarty_Internal_ParseTree_Tag($compiler->parser, $output));
  171. $compiler->parent_compiler->blockOrFunctionCode .= $compiler->parser->current_buffer->to_smarty_php($compiler->parser);
  172. // nocache plugins must be copied
  173. if (!empty($compiler->template->compiled->required_plugins['nocache'])) {
  174. foreach ($compiler->template->compiled->required_plugins['nocache'] as $plugin => $tmp) {
  175. foreach ($tmp as $type => $data) {
  176. $compiler->parent_compiler->template->compiled->required_plugins['compiled'][$plugin][$type] = $data;
  177. }
  178. }
  179. }
  180. // restore old buffer
  181. $compiler->parser->current_buffer = $saved_data[1];
  182. // restore old status
  183. $compiler->template->compiled->has_nocache_code = $saved_data[2];
  184. $compiler->template->caching = $saved_data[3];
  185. return true;
  186. }
  187. /**
  188. * @param $match
  189. *
  190. * @return mixed
  191. */
  192. function removeNocache($match)
  193. {
  194. $code = preg_replace("/((<\?php )?echo '\/\*%%SmartyNocache:{$this->compiler->template->compiled->nocache_hash}%%\*\/)|(\/\*\/%%SmartyNocache:{$this->compiler->template->compiled->nocache_hash}%%\*\/';(\?>\n)?)/", '', $match[0]);
  195. $code = str_replace(array('\\\'', '\\\\\''), array('\'', '\\\''), $code);
  196. return $code;
  197. }
  198. }