smarty_internal_compile_include.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Compile Include
  4. *
  5. * Compiles the {include} tag
  6. *
  7. * @package Smarty
  8. * @subpackage Compiler
  9. * @author Uwe Tews
  10. */
  11. /**
  12. * Smarty Internal Plugin Compile Include Class
  13. *
  14. * @package Smarty
  15. * @subpackage Compiler
  16. */
  17. class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase {
  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. * @return string compiled code
  57. */
  58. public function compile($args, $compiler, $parameter)
  59. {
  60. // check and get attributes
  61. $_attr = $this->getAttributes($compiler, $args);
  62. // save posible attributes
  63. $include_file = $_attr['file'];
  64. if (isset($_attr['assign'])) {
  65. // output will be stored in a smarty variable instead of beind displayed
  66. $_assign = $_attr['assign'];
  67. }
  68. $_parent_scope = Smarty::SCOPE_LOCAL;
  69. if (isset($_attr['scope'])) {
  70. $_attr['scope'] = trim($_attr['scope'], "'\"");
  71. if ($_attr['scope'] == 'parent') {
  72. $_parent_scope = Smarty::SCOPE_PARENT;
  73. } elseif ($_attr['scope'] == 'root') {
  74. $_parent_scope = Smarty::SCOPE_ROOT;
  75. } elseif ($_attr['scope'] == 'global') {
  76. $_parent_scope = Smarty::SCOPE_GLOBAL;
  77. }
  78. }
  79. $_caching = 'null';
  80. if ($compiler->nocache || $compiler->tag_nocache) {
  81. $_caching = Smarty::CACHING_OFF;
  82. }
  83. // default for included templates
  84. if ($compiler->template->caching && !$compiler->nocache && !$compiler->tag_nocache) {
  85. $_caching = self::CACHING_NOCACHE_CODE;
  86. }
  87. /*
  88. * if the {include} tag provides individual parameter for caching
  89. * it will not be included into the common cache file and treated like
  90. * a nocache section
  91. */
  92. if (isset($_attr['cache_lifetime'])) {
  93. $_cache_lifetime = $_attr['cache_lifetime'];
  94. $compiler->tag_nocache = true;
  95. $_caching = Smarty::CACHING_LIFETIME_CURRENT;
  96. } else {
  97. $_cache_lifetime = 'null';
  98. }
  99. if (isset($_attr['cache_id'])) {
  100. $_cache_id = $_attr['cache_id'];
  101. $compiler->tag_nocache = true;
  102. $_caching = Smarty::CACHING_LIFETIME_CURRENT;
  103. } else {
  104. $_cache_id = '$_smarty_tpl->cache_id';
  105. }
  106. if (isset($_attr['compile_id'])) {
  107. $_compile_id = $_attr['compile_id'];
  108. } else {
  109. $_compile_id = '$_smarty_tpl->compile_id';
  110. }
  111. if ($_attr['caching'] === true) {
  112. $_caching = Smarty::CACHING_LIFETIME_CURRENT;
  113. }
  114. if ($_attr['nocache'] === true) {
  115. $compiler->tag_nocache = true;
  116. $_caching = Smarty::CACHING_OFF;
  117. }
  118. $has_compiled_template = false;
  119. if (($compiler->smarty->merge_compiled_includes || $_attr['inline'] === true) && !$compiler->template->source->recompiled
  120. && !($compiler->template->caching && ($compiler->tag_nocache || $compiler->nocache)) && $_caching != Smarty::CACHING_LIFETIME_CURRENT) {
  121. // check if compiled code can be merged (contains no variable part)
  122. if (!$compiler->has_variable_string && (substr_count($include_file, '"') == 2 or substr_count($include_file, "'") == 2)
  123. and substr_count($include_file, '(') == 0 and substr_count($include_file, '$_smarty_tpl->') == 0) {
  124. $tpl_name = null;
  125. eval("\$tpl_name = $include_file;");
  126. if (!isset($compiler->smarty->merged_templates_func[$tpl_name]) || $compiler->inheritance) {
  127. $tpl = new $compiler->smarty->template_class ($tpl_name, $compiler->smarty, $compiler->template, $compiler->template->cache_id, $compiler->template->compile_id);
  128. // save unique function name
  129. $compiler->smarty->merged_templates_func[$tpl_name]['func'] = $tpl->properties['unifunc'] = 'content_'. str_replace('.', '_', uniqid('', true));
  130. // use current nocache hash for inlined code
  131. $compiler->smarty->merged_templates_func[$tpl_name]['nocache_hash'] = $tpl->properties['nocache_hash'] = $compiler->template->properties['nocache_hash'];
  132. if ($compiler->template->caching) {
  133. // needs code for cached page but no cache file
  134. $tpl->caching = self::CACHING_NOCACHE_CODE;
  135. }
  136. // make sure whole chain gest compiled
  137. $tpl->mustCompile = true;
  138. if (!($tpl->source->uncompiled) && $tpl->source->exists) {
  139. // get compiled code
  140. $compiled_code = $tpl->compiler->compileTemplate($tpl);
  141. // release compiler object to free memory
  142. unset($tpl->compiler);
  143. // merge compiled code for {function} tags
  144. $compiler->template->properties['function'] = array_merge($compiler->template->properties['function'], $tpl->properties['function']);
  145. // merge filedependency
  146. $tpl->properties['file_dependency'][$tpl->source->uid] = array($tpl->source->filepath, $tpl->source->timestamp,$tpl->source->type);
  147. $compiler->template->properties['file_dependency'] = array_merge($compiler->template->properties['file_dependency'], $tpl->properties['file_dependency']);
  148. // remove header code
  149. $compiled_code = preg_replace("/(<\?php \/\*%%SmartyHeaderCode:{$tpl->properties['nocache_hash']}%%\*\/(.+?)\/\*\/%%SmartyHeaderCode%%\*\/\?>\n)/s", '', $compiled_code);
  150. if ($tpl->has_nocache_code) {
  151. // replace nocache_hash
  152. $compiled_code = str_replace("{$tpl->properties['nocache_hash']}", $compiler->template->properties['nocache_hash'], $compiled_code);
  153. $compiler->template->has_nocache_code = true;
  154. }
  155. $compiler->merged_templates[$tpl->properties['unifunc']] = $compiled_code;
  156. $has_compiled_template = true;
  157. }
  158. } else {
  159. $has_compiled_template = true;
  160. }
  161. }
  162. }
  163. // delete {include} standard attributes
  164. unset($_attr['file'], $_attr['assign'], $_attr['cache_id'], $_attr['compile_id'], $_attr['cache_lifetime'], $_attr['nocache'], $_attr['caching'], $_attr['scope'], $_attr['inline']);
  165. // remaining attributes must be assigned as smarty variable
  166. if (!empty($_attr)) {
  167. if ($_parent_scope == Smarty::SCOPE_LOCAL) {
  168. // create variables
  169. foreach ($_attr as $key => $value) {
  170. $_pairs[] = "'$key'=>$value";
  171. }
  172. $_vars = 'array('.join(',',$_pairs).')';
  173. $_has_vars = true;
  174. } else {
  175. $compiler->trigger_template_error('variable passing not allowed in parent/global scope', $compiler->lex->taglineno);
  176. }
  177. } else {
  178. $_vars = 'array()';
  179. $_has_vars = false;
  180. }
  181. if ($has_compiled_template) {
  182. $_hash = $compiler->smarty->merged_templates_func[$tpl_name]['nocache_hash'];
  183. $_output = "<?php /* Call merged included template \"" . $tpl_name . "\" */\n";
  184. $_output .= "\$_tpl_stack[] = \$_smarty_tpl;\n";
  185. $_output .= " \$_smarty_tpl = \$_smarty_tpl->setupInlineSubTemplate($include_file, $_cache_id, $_compile_id, $_caching, $_cache_lifetime, $_vars, $_parent_scope, '$_hash');\n";
  186. if (isset($_assign)) {
  187. $_output .= 'ob_start(); ';
  188. }
  189. $_output .= $compiler->smarty->merged_templates_func[$tpl_name]['func']. "(\$_smarty_tpl);\n";
  190. $_output .= "\$_smarty_tpl = array_pop(\$_tpl_stack); ";
  191. if (isset($_assign)) {
  192. $_output .= " \$_smarty_tpl->tpl_vars[$_assign] = new Smarty_variable(ob_get_clean());";
  193. }
  194. $_output .= "/* End of included template \"" . $tpl_name . "\" */?>";
  195. return $_output;
  196. }
  197. // was there an assign attribute
  198. if (isset($_assign)) {
  199. $_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";;
  200. } else {
  201. $_output = "<?php echo \$_smarty_tpl->getSubTemplate ($include_file, $_cache_id, $_compile_id, $_caching, $_cache_lifetime, $_vars, $_parent_scope);?>\n";
  202. }
  203. return $_output;
  204. }
  205. }
  206. ?>