PageRenderTime 24ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/smarty/sysplugins/smarty_internal_compile_include.php

https://gitlab.com/staging06/myproject
PHP | 265 lines | 207 code | 4 blank | 54 comment | 13 complexity | 2fd4f0850b43db458732f96e4c030a81 MD5 | raw file
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Compile Include
  4. * Compiles the {include} tag
  5. *
  6. * @package Smarty
  7. * @subpackage Compiler
  8. * @author Uwe Tews
  9. */
  10. /**
  11. * Smarty Internal Plugin Compile Include Class
  12. *
  13. * @package Smarty
  14. * @subpackage Compiler
  15. */
  16. class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase
  17. {
  18. /**
  19. * caching mode to create nocache code but no cache file
  20. */
  21. const CACHING_NOCACHE_CODE = 9999;
  22. /**
  23. * Attribute definition: Overwrites base class.
  24. *
  25. * @var array
  26. * @see Smarty_Internal_CompileBase
  27. */
  28. public $required_attributes = array('file');
  29. /**
  30. * Attribute definition: Overwrites base class.
  31. *
  32. * @var array
  33. * @see Smarty_Internal_CompileBase
  34. */
  35. public $shorttag_order = array('file');
  36. /**
  37. * Attribute definition: Overwrites base class.
  38. *
  39. * @var array
  40. * @see Smarty_Internal_CompileBase
  41. */
  42. public $option_flags = array('nocache', 'inline', 'caching');
  43. /**
  44. * Attribute definition: Overwrites base class.
  45. *
  46. * @var array
  47. * @see Smarty_Internal_CompileBase
  48. */
  49. public $optional_attributes = array('_any');
  50. /**
  51. * Compiles code for the {include} tag
  52. *
  53. * @param array $args array with attributes from parser
  54. * @param object $compiler compiler object
  55. * @param array $parameter array with compilation parameter
  56. *
  57. * @return string compiled code
  58. */
  59. public function compile($args, $compiler, $parameter)
  60. {
  61. // check and get attributes
  62. $_attr = $this->getAttributes($compiler, $args);
  63. // save possible attributes
  64. $include_file = $_attr['file'];
  65. if (isset($_attr['assign'])) {
  66. // output will be stored in a smarty variable instead of being displayed
  67. $_assign = $_attr['assign'];
  68. }
  69. $_parent_scope = Smarty::SCOPE_LOCAL;
  70. if (isset($_attr['scope'])) {
  71. $_attr['scope'] = trim($_attr['scope'], "'\"");
  72. if ($_attr['scope'] == 'parent') {
  73. $_parent_scope = Smarty::SCOPE_PARENT;
  74. } elseif ($_attr['scope'] == 'root') {
  75. $_parent_scope = Smarty::SCOPE_ROOT;
  76. } elseif ($_attr['scope'] == 'global') {
  77. $_parent_scope = Smarty::SCOPE_GLOBAL;
  78. }
  79. }
  80. $_caching = Smarty::CACHING_OFF;
  81. // flag if included template code should be merged into caller
  82. $merge_compiled_includes = ($compiler->smarty->merge_compiled_includes || ($compiler->inheritance && $compiler->smarty->inheritance_merge_compiled_includes) || $_attr['inline'] === true) && !$compiler->template->source->recompiled;
  83. // set default when in nocache mode
  84. // if ($compiler->template->caching && ($compiler->nocache || $compiler->tag_nocache || $compiler->forceNocache == 2)) {
  85. if ($compiler->template->caching && ((!$compiler->inheritance && !$compiler->nocache && !$compiler->tag_nocache) || ($compiler->inheritance && ($compiler->nocache || $compiler->tag_nocache)))) {
  86. $_caching = self::CACHING_NOCACHE_CODE;
  87. }
  88. /*
  89. * if the {include} tag provides individual parameter for caching
  90. * it will not be included into the common cache file and treated like
  91. * a nocache section
  92. */
  93. if (isset($_attr['cache_lifetime'])) {
  94. $_cache_lifetime = $_attr['cache_lifetime'];
  95. $compiler->tag_nocache = true;
  96. $_caching = Smarty::CACHING_LIFETIME_CURRENT;
  97. } else {
  98. $_cache_lifetime = 'null';
  99. }
  100. if (isset($_attr['cache_id'])) {
  101. $_cache_id = $_attr['cache_id'];
  102. $compiler->tag_nocache = true;
  103. $_caching = Smarty::CACHING_LIFETIME_CURRENT;
  104. } else {
  105. $_cache_id = '$_smarty_tpl->cache_id';
  106. }
  107. if (isset($_attr['compile_id'])) {
  108. $_compile_id = $_attr['compile_id'];
  109. } else {
  110. $_compile_id = '$_smarty_tpl->compile_id';
  111. }
  112. if ($_attr['caching'] === true) {
  113. $_caching = Smarty::CACHING_LIFETIME_CURRENT;
  114. }
  115. if ($_attr['nocache'] === true) {
  116. $compiler->tag_nocache = true;
  117. if ($merge_compiled_includes) {
  118. $_caching = self::CACHING_NOCACHE_CODE;
  119. } else {
  120. $_caching = Smarty::CACHING_OFF;
  121. }
  122. }
  123. $has_compiled_template = false;
  124. if ($merge_compiled_includes && $_attr['inline'] !== true) {
  125. // variable template name ?
  126. if ($compiler->has_variable_string || !((substr_count($include_file, '"') == 2 || substr_count($include_file, "'") == 2))
  127. || substr_count($include_file, '(') != 0 || substr_count($include_file, '$_smarty_tpl->') != 0
  128. ) {
  129. $merge_compiled_includes = false;
  130. if ($compiler->inheritance && $compiler->smarty->inheritance_merge_compiled_includes) {
  131. $compiler->trigger_template_error(' variable template file names not allow within {block} tags');
  132. }
  133. }
  134. // variable compile_id?
  135. if (isset($_attr['compile_id'])) {
  136. if (!((substr_count($_attr['compile_id'], '"') == 2 || substr_count($_attr['compile_id'], "'") == 2))
  137. || substr_count($_attr['compile_id'], '(') != 0 || substr_count($_attr['compile_id'], '$_smarty_tpl->') != 0
  138. ) {
  139. $merge_compiled_includes = false;
  140. if ($compiler->inheritance && $compiler->smarty->inheritance_merge_compiled_includes) {
  141. $compiler->trigger_template_error(' variable compile_id not allow within {block} tags');
  142. }
  143. }
  144. }
  145. }
  146. if ($merge_compiled_includes) {
  147. if ($compiler->template->caching && ($compiler->tag_nocache || $compiler->nocache) && $_caching != self::CACHING_NOCACHE_CODE) {
  148. $merge_compiled_includes = false;
  149. if ($compiler->inheritance && $compiler->smarty->inheritance_merge_compiled_includes) {
  150. $compiler->trigger_template_error(' invalid caching mode of subtemplate within {block} tags');
  151. }
  152. }
  153. }
  154. if ($merge_compiled_includes) {
  155. // we must observe different compile_id
  156. $uid = sha1($_compile_id);
  157. $tpl_name = null;
  158. $nocache = false;
  159. /** @var Smarty_Internal_Template $_smarty_tpl
  160. * used in evaluated code
  161. */
  162. $_smarty_tpl = $compiler->template;
  163. eval("\$tpl_name = $include_file;");
  164. if (!isset($compiler->smarty->merged_templates_func[$tpl_name][$uid])) {
  165. $tpl = new $compiler->smarty->template_class($tpl_name, $compiler->smarty, $compiler->template, $compiler->template->cache_id, $compiler->template->compile_id);
  166. // save unique function name
  167. $compiler->smarty->merged_templates_func[$tpl_name][$uid]['func'] = $tpl->properties['unifunc'] = 'content_' . str_replace(array('.', ','), '_', uniqid('', true));
  168. // use current nocache hash for inlined code
  169. $compiler->smarty->merged_templates_func[$tpl_name][$uid]['nocache_hash'] = $tpl->properties['nocache_hash'] = $compiler->template->properties['nocache_hash'];
  170. if ($compiler->template->caching && $_caching == self::CACHING_NOCACHE_CODE) {
  171. // all code must be nocache
  172. $nocache = true;
  173. }
  174. if ($compiler->inheritance) {
  175. $tpl->compiler->inheritance = true;
  176. }
  177. // make sure whole chain gets compiled
  178. $tpl->mustCompile = true;
  179. if (!($tpl->source->uncompiled) && $tpl->source->exists) {
  180. // get compiled code
  181. $compiled_code = $tpl->compiler->compileTemplate($tpl, $nocache);
  182. // release compiler object to free memory
  183. unset($tpl->compiler);
  184. // merge compiled code for {function} tags
  185. $compiler->template->properties['function'] = array_merge($compiler->template->properties['function'], $tpl->properties['function']);
  186. // merge filedependency
  187. $tpl->properties['file_dependency'][$tpl->source->uid] = array($tpl->source->filepath, $tpl->source->timestamp, $tpl->source->type);
  188. $compiler->template->properties['file_dependency'] = array_merge($compiler->template->properties['file_dependency'], $tpl->properties['file_dependency']);
  189. // remove header code
  190. $compiled_code = preg_replace("/(<\?php \/\*%%SmartyHeaderCode:{$tpl->properties['nocache_hash']}%%\*\/(.+?)\/\*\/%%SmartyHeaderCode%%\*\/\?>\n)/s", '', $compiled_code);
  191. if ($tpl->has_nocache_code) {
  192. // replace nocache_hash
  193. $compiled_code = str_replace("{$tpl->properties['nocache_hash']}", $compiler->template->properties['nocache_hash'], $compiled_code);
  194. $compiler->template->has_nocache_code = true;
  195. }
  196. $compiler->merged_templates[$tpl->properties['unifunc']] = $compiled_code;
  197. $has_compiled_template = true;
  198. unset($tpl);
  199. }
  200. } else {
  201. $has_compiled_template = true;
  202. }
  203. }
  204. // delete {include} standard attributes
  205. unset($_attr['file'], $_attr['assign'], $_attr['cache_id'], $_attr['compile_id'], $_attr['cache_lifetime'], $_attr['nocache'], $_attr['caching'], $_attr['scope'], $_attr['inline']);
  206. // remaining attributes must be assigned as smarty variable
  207. if (!empty($_attr)) {
  208. if ($_parent_scope == Smarty::SCOPE_LOCAL) {
  209. // create variables
  210. $nccode = '';
  211. foreach ($_attr as $key => $value) {
  212. $_pairs[] = "'$key'=>$value";
  213. $nccode .= "\$_smarty_tpl->tpl_vars['$key'] = new Smarty_variable($value);\n";
  214. }
  215. $_vars = 'array(' . join(',', $_pairs) . ')';
  216. } else {
  217. $compiler->trigger_template_error('variable passing not allowed in parent/global scope', $compiler->lex->taglineno);
  218. }
  219. } else {
  220. $_vars = 'array()';
  221. }
  222. if ($has_compiled_template) {
  223. // never call inline templates in nocache mode
  224. $compiler->suppressNocacheProcessing = true;
  225. $_hash = $compiler->smarty->merged_templates_func[$tpl_name][$uid]['nocache_hash'];
  226. $_output = "<?php /* Call merged included template \"" . $tpl_name . "\" */\n";
  227. $_output .= "\$_tpl_stack[] = \$_smarty_tpl;\n";
  228. if (!empty($nccode) && $_caching == 9999 && $_smarty_tpl->caching) {
  229. $compiler->suppressNocacheProcessing = false;
  230. $_output .= substr($compiler->processNocacheCode('<?php ' .$nccode . "?>\n", true), 6, -3);
  231. $compiler->suppressNocacheProcessing = true;
  232. }
  233. $_output .= " \$_smarty_tpl = \$_smarty_tpl->setupInlineSubTemplate($include_file, $_cache_id, $_compile_id, $_caching, $_cache_lifetime, $_vars, $_parent_scope, '$_hash');\n";
  234. if (isset($_assign)) {
  235. $_output .= 'ob_start(); ';
  236. }
  237. $_output .= $compiler->smarty->merged_templates_func[$tpl_name][$uid]['func'] . "(\$_smarty_tpl);\n";
  238. $_output .= "\$_smarty_tpl = array_pop(\$_tpl_stack); ";
  239. if (isset($_assign)) {
  240. $_output .= " \$_smarty_tpl->tpl_vars[$_assign] = new Smarty_variable(ob_get_clean());";
  241. }
  242. $_output .= "\n/* End of included template \"" . $tpl_name . "\" */?>";
  243. return $_output;
  244. }
  245. // was there an assign attribute
  246. if (isset($_assign)) {
  247. $_output = "<?php \$_smarty_tpl->tpl_vars[$_assign] = new Smarty_variable(\$_smarty_tpl->getSubTemplate ($include_file, $_cache_id, $_compile_id, $_caching, $_cache_lifetime, $_vars, $_parent_scope));?>\n";;
  248. } else {
  249. $_output = "<?php echo \$_smarty_tpl->getSubTemplate ($include_file, $_cache_id, $_compile_id, $_caching, $_cache_lifetime, $_vars, $_parent_scope);?>\n";
  250. }
  251. return $_output;
  252. }
  253. }