選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

smarty_internal_compile_include.php 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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', 'bubble_up');
  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. * Valid scope names
  52. *
  53. * @var array
  54. */
  55. public $valid_scopes = array('local' => true, 'parent' => true, 'root' => true, 'global' => true,
  56. 'smarty' => true, 'tpl_root' => true);
  57. /**
  58. * Compiles code for the {include} tag
  59. *
  60. * @param array $args array with attributes from parser
  61. * @param Smarty_Internal_SmartyTemplateCompiler $compiler compiler object
  62. * @param array $parameter array with compilation parameter
  63. *
  64. * @throws SmartyCompilerException
  65. * @return string compiled code
  66. */
  67. public function compile($args, Smarty_Internal_SmartyTemplateCompiler $compiler, $parameter)
  68. {
  69. // check and get attributes
  70. $_attr = $this->getAttributes($compiler, $args);
  71. $hashResourceName = $fullResourceName = $source_resource = $_attr['file'];
  72. $variable_template = false;
  73. $cache_tpl = false;
  74. // parse resource_name
  75. if (preg_match('/^([\'"])(([A-Za-z0-9_\-]{2,})[:])?(([^$()]+)|(.+))\1$/', $source_resource, $match)) {
  76. $type = !empty($match[3]) ? $match[3] : $compiler->template->smarty->default_resource_type;
  77. $name = !empty($match[5]) ? $match[5] : $match[6];
  78. $handler = Smarty_Resource::load($compiler->smarty, $type);
  79. if ($handler->recompiled || $handler->uncompiled) {
  80. $variable_template = true;
  81. }
  82. if (!$variable_template) {
  83. if ($type != 'string') {
  84. $fullResourceName = "{$type}:{$name}";
  85. $compiled = $compiler->parent_compiler->template->compiled;
  86. if (isset($compiled->includes[$fullResourceName])) {
  87. $compiled->includes[$fullResourceName] ++;
  88. $cache_tpl = true;
  89. } else {
  90. $compiled->includes[$fullResourceName] = 1;
  91. }
  92. $fullResourceName = '"' . $fullResourceName . '"';
  93. }
  94. }
  95. if (empty($match[5])) {
  96. $variable_template = true;
  97. }
  98. } else {
  99. $variable_template = true;
  100. }
  101. if (isset($_attr['assign'])) {
  102. // output will be stored in a smarty variable instead of being displayed
  103. $_assign = $_attr['assign'];
  104. }
  105. // scope setup
  106. $_scope = Smarty::SCOPE_LOCAL;
  107. if (isset($_attr['scope'])) {
  108. $_attr['scope'] = trim($_attr['scope'], "'\"");
  109. if (!isset($this->valid_scopes[$_attr['scope']])) {
  110. $compiler->trigger_template_error("illegal value '{$_attr['scope']}' for \"scope\" attribute", null, true);
  111. }
  112. if ($_attr['scope'] != 'local') {
  113. if ($_attr['scope'] == 'parent') {
  114. $_scope = Smarty::SCOPE_PARENT;
  115. } elseif ($_attr['scope'] == 'root') {
  116. $_scope = Smarty::SCOPE_ROOT;
  117. } elseif ($_attr['scope'] == 'global') {
  118. $_scope = Smarty::SCOPE_GLOBAL;
  119. } elseif ($_attr['scope'] == 'smarty') {
  120. $_scope = Smarty::SCOPE_SMARTY;
  121. } elseif ($_attr['scope'] == 'tpl_root') {
  122. $_scope = Smarty::SCOPE_TPL_ROOT;
  123. }
  124. if ($_attr['bubble_up'] === true) {
  125. $_scope = $_scope + Smarty::SCOPE_BUBBLE_UP;
  126. }
  127. }
  128. }
  129. // set flag to cache subtemplate object when called within loop or template name is variable.
  130. if ($cache_tpl || $variable_template || $compiler->loopNesting > 0) {
  131. $_cache_tpl = 'true';
  132. } else {
  133. $_cache_tpl = 'false';
  134. }
  135. // assume caching is off
  136. $_caching = Smarty::CACHING_OFF;
  137. if ($_attr['nocache'] === true) {
  138. $compiler->tag_nocache = true;
  139. }
  140. $call_nocache = $compiler->tag_nocache || $compiler->nocache;
  141. // caching was on and {include} is not in nocache mode
  142. if ($compiler->template->caching && !$compiler->nocache && !$compiler->tag_nocache) {
  143. $_caching = self::CACHING_NOCACHE_CODE;
  144. }
  145. // flag if included template code should be merged into caller
  146. $merge_compiled_includes = ($compiler->smarty->merge_compiled_includes || $_attr['inline'] === true) &&
  147. !$compiler->template->source->handler->recompiled;
  148. if ($merge_compiled_includes && $_attr['inline'] !== true) {
  149. // variable template name ?
  150. if ($variable_template) {
  151. $merge_compiled_includes = false;
  152. if ($compiler->template->caching) {
  153. // must use individual cache file
  154. //$_attr['caching'] = 1;
  155. }
  156. }
  157. // variable compile_id?
  158. if (isset($_attr['compile_id'])) {
  159. if (!((substr_count($_attr['compile_id'], '"') == 2 || substr_count($_attr['compile_id'], "'") == 2 ||
  160. is_numeric($_attr['compile_id']))) || substr_count($_attr['compile_id'], '(') != 0 ||
  161. substr_count($_attr['compile_id'], '$_smarty_tpl->') != 0
  162. ) {
  163. $merge_compiled_includes = false;
  164. if ($compiler->template->caching) {
  165. // must use individual cache file
  166. //$_attr['caching'] = 1;
  167. }
  168. }
  169. }
  170. }
  171. /*
  172. * if the {include} tag provides individual parameter for caching or compile_id
  173. * the subtemplate must not be included into the common cache file and is treated like
  174. * a call in nocache mode.
  175. *
  176. */
  177. if ($_attr['nocache'] !== true && $_attr['caching']) {
  178. $_caching = $_new_caching = (int) $_attr['caching'];
  179. $call_nocache = true;
  180. } else {
  181. $_new_caching = Smarty::CACHING_LIFETIME_CURRENT;
  182. }
  183. if (isset($_attr['cache_lifetime'])) {
  184. $_cache_lifetime = $_attr['cache_lifetime'];
  185. $call_nocache = true;
  186. $_caching = $_new_caching;
  187. } else {
  188. $_cache_lifetime = '$_smarty_tpl->cache_lifetime';
  189. }
  190. if (isset($_attr['cache_id'])) {
  191. $_cache_id = $_attr['cache_id'];
  192. $call_nocache = true;
  193. $_caching = $_new_caching;
  194. } else {
  195. $_cache_id = '$_smarty_tpl->cache_id';
  196. }
  197. if (isset($_attr['compile_id'])) {
  198. $_compile_id = $_attr['compile_id'];
  199. } else {
  200. $_compile_id = '$_smarty_tpl->compile_id';
  201. }
  202. // if subtemplate will be called in nocache mode do not merge
  203. if ($compiler->template->caching && $call_nocache) {
  204. $merge_compiled_includes = false;
  205. }
  206. $has_compiled_template = false;
  207. if ($merge_compiled_includes) {
  208. $c_id = isset($_attr['compile_id']) ? $_attr['compile_id'] : $compiler->template->compile_id;
  209. // we must observe different compile_id and caching
  210. $t_hash = sha1($c_id . ($_caching ? '--caching' : '--nocaching'));
  211. if (!isset($compiler->parent_compiler->mergedSubTemplatesData[$hashResourceName][$t_hash])) {
  212. $has_compiled_template =
  213. $this->compileInlineTemplate($compiler, $fullResourceName, $_caching, $hashResourceName, $t_hash,
  214. $c_id);
  215. } else {
  216. $has_compiled_template = true;
  217. }
  218. }
  219. // delete {include} standard attributes
  220. unset($_attr['file'], $_attr['assign'], $_attr['cache_id'], $_attr['compile_id'], $_attr['cache_lifetime'], $_attr['nocache'], $_attr['caching'], $_attr['scope'], $_attr['inline'], $_attr['bubble_up']);
  221. // remaining attributes must be assigned as smarty variable
  222. $_vars_nc = '';
  223. if (!empty($_attr)) {
  224. if ($_scope == Smarty::SCOPE_LOCAL) {
  225. $_pairs = array();
  226. // create variables
  227. foreach ($_attr as $key => $value) {
  228. $_pairs[] = "'$key'=>$value";
  229. $_vars_nc .= "\$_smarty_tpl->tpl_vars['$key'] = new Smarty_Variable($value);\n";
  230. }
  231. $_vars = 'array(' . join(',', $_pairs) . ')';
  232. } else {
  233. $compiler->trigger_template_error('variable passing not allowed in parent/global scope', null, true);
  234. }
  235. } else {
  236. $_vars = 'array()';
  237. }
  238. $update_compile_id = $compiler->template->caching && !$compiler->tag_nocache && !$compiler->nocache &&
  239. $_compile_id != '$_smarty_tpl->compile_id';
  240. if ($has_compiled_template && !$call_nocache) {
  241. $_output = "<?php\n";
  242. if ($update_compile_id) {
  243. $_output .= $compiler->makeNocacheCode("\$_compile_id_save[] = \$_smarty_tpl->compile_id;\n\$_smarty_tpl->compile_id = {$_compile_id};\n");
  244. }
  245. if (!empty($_vars_nc) && $_caching == 9999 && $compiler->template->caching) {
  246. //$compiler->suppressNocacheProcessing = false;
  247. $_output .= substr($compiler->processNocacheCode('<?php ' . $_vars_nc . "?>\n", true), 6, - 3);
  248. //$compiler->suppressNocacheProcessing = true;
  249. }
  250. if (isset($_assign)) {
  251. $_output .= "ob_start();\n";
  252. }
  253. $_output .= "\$_smarty_tpl->smarty->ext->_subtemplate->render(\$_smarty_tpl, {$fullResourceName}, {$_cache_id}, {$_compile_id}, {$_caching}, {$_cache_lifetime}, {$_vars}, {$_scope}, {$_cache_tpl}, '{$compiler->parent_compiler->mergedSubTemplatesData[$hashResourceName][$t_hash]['uid']}', '{$compiler->parent_compiler->mergedSubTemplatesData[$hashResourceName][$t_hash]['func']}');\n";
  254. if (isset($_assign)) {
  255. $_output .= "\$_smarty_tpl->assign({$_assign}, ob_get_clean());\n";
  256. }
  257. if ($update_compile_id) {
  258. $_output .= $compiler->makeNocacheCode("\$_smarty_tpl->compile_id = array_pop(\$_compile_id_save);\n");
  259. }
  260. $_output .= "?>\n";
  261. return $_output;
  262. }
  263. if ($call_nocache) {
  264. $compiler->tag_nocache = true;
  265. }
  266. $_output = "<?php ";
  267. if ($update_compile_id) {
  268. $_output .= "\$_compile_id_save[] = \$_smarty_tpl->compile_id;\n\$_smarty_tpl->compile_id = {$_compile_id};\n";
  269. }
  270. // was there an assign attribute
  271. if (isset($_assign)) {
  272. $_output .= "ob_start();\n";
  273. }
  274. $_output .= "\$_smarty_tpl->smarty->ext->_subtemplate->render(\$_smarty_tpl, {$fullResourceName}, $_cache_id, $_compile_id, $_caching, $_cache_lifetime, $_vars, $_scope, {$_cache_tpl});\n";
  275. if (isset($_assign)) {
  276. $_output .= "\$_smarty_tpl->assign({$_assign}, ob_get_clean());\n";
  277. }
  278. if ($update_compile_id) {
  279. $_output .= "\$_smarty_tpl->compile_id = array_pop(\$_compile_id_save);\n";
  280. }
  281. $_output .= "?>\n";
  282. return $_output;
  283. }
  284. /**
  285. * Compile inline sub template
  286. *
  287. * @param \Smarty_Internal_SmartyTemplateCompiler $compiler
  288. * @param $fullResourceName
  289. * @param $_caching
  290. * @param $hashResourceName
  291. * @param $t_hash
  292. * @param $c_id
  293. *
  294. * @return bool
  295. */
  296. public function compileInlineTemplate(Smarty_Internal_SmartyTemplateCompiler $compiler, $fullResourceName,
  297. $_caching, $hashResourceName, $t_hash, $c_id)
  298. {
  299. $compiler->smarty->allow_ambiguous_resources = true;
  300. /* @var Smarty_Internal_Template $tpl */
  301. $tpl =
  302. new $compiler->smarty->template_class (trim($fullResourceName, '"\''), $compiler->smarty, $compiler->template,
  303. $compiler->template->cache_id, $c_id, $_caching);
  304. if (!($tpl->source->handler->uncompiled) && $tpl->source->exists) {
  305. $compiler->parent_compiler->mergedSubTemplatesData[$hashResourceName][$t_hash]['uid'] = $tpl->source->uid;
  306. if (isset($compiler->template->_inheritance)) {
  307. $tpl->_inheritance = clone $compiler->template->_inheritance;
  308. }
  309. $tpl->compiled = new Smarty_Template_Compiled();
  310. $tpl->compiled->nocache_hash = $compiler->parent_compiler->template->compiled->nocache_hash;
  311. $tpl->loadCompiler();
  312. // save unique function name
  313. $compiler->parent_compiler->mergedSubTemplatesData[$hashResourceName][$t_hash]['func'] =
  314. $tpl->compiled->unifunc = 'content_' . str_replace(array('.', ','), '_', uniqid('', true));
  315. // make sure whole chain gets compiled
  316. $tpl->mustCompile = true;
  317. $compiler->parent_compiler->mergedSubTemplatesData[$hashResourceName][$t_hash]['nocache_hash'] =
  318. $tpl->compiled->nocache_hash;
  319. // get compiled code
  320. $compiled_code = "<?php\n\n";
  321. $compiled_code .= "/* Start inline template \"{$tpl->source->type}:{$tpl->source->name}\" =============================*/\n";
  322. $compiled_code .= "function {$tpl->compiled->unifunc} (\$_smarty_tpl) {\n";
  323. $compiled_code .= "?>\n" . $tpl->compiler->compileTemplateSource($tpl, null, $compiler->parent_compiler);
  324. $compiled_code .= "<?php\n";
  325. $compiled_code .= "}\n?>\n";
  326. $compiled_code .= $tpl->compiler->postFilter($tpl->compiler->blockOrFunctionCode);
  327. $compiled_code .= "<?php\n\n";
  328. $compiled_code .= "/* End inline template \"{$tpl->source->type}:{$tpl->source->name}\" =============================*/\n";
  329. $compiled_code .= "?>";
  330. unset($tpl->compiler);
  331. if ($tpl->compiled->has_nocache_code) {
  332. // replace nocache_hash
  333. $compiled_code =
  334. str_replace("{$tpl->compiled->nocache_hash}", $compiler->template->compiled->nocache_hash,
  335. $compiled_code);
  336. $compiler->template->compiled->has_nocache_code = true;
  337. }
  338. $compiler->parent_compiler->mergedSubTemplatesCode[$tpl->compiled->unifunc] = $compiled_code;
  339. return true;
  340. } else {
  341. return false;
  342. }
  343. }
  344. }