1: <?php
2: 3: 4: 5: 6: 7: 8: 9:
10:
11: 12: 13: 14: 15: 16:
17: class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase
18: {
19: 20: 21:
22: const CACHING_NOCACHE_CODE = 9999;
23: 24: 25: 26: 27: 28:
29: public $required_attributes = array('file');
30: 31: 32: 33: 34: 35:
36: public $shorttag_order = array('file');
37: 38: 39: 40: 41: 42:
43: public $option_flags = array('nocache', 'inline', 'caching');
44: 45: 46: 47: 48: 49:
50: public $optional_attributes = array('_any');
51:
52: 53: 54: 55: 56: 57: 58: 59: 60:
61: public function compile($args, $compiler, $parameter)
62: {
63:
64: $_attr = $this->getAttributes($compiler, $args);
65:
66: $include_file = $_attr['file'];
67:
68: if (isset($_attr['assign'])) {
69:
70: $_assign = $_attr['assign'];
71: }
72:
73: $_parent_scope = Smarty::SCOPE_LOCAL;
74: if (isset($_attr['scope'])) {
75: $_attr['scope'] = trim($_attr['scope'], "'\"");
76: if ($_attr['scope'] == 'parent') {
77: $_parent_scope = Smarty::SCOPE_PARENT;
78: } elseif ($_attr['scope'] == 'root') {
79: $_parent_scope = Smarty::SCOPE_ROOT;
80: } elseif ($_attr['scope'] == 'global') {
81: $_parent_scope = Smarty::SCOPE_GLOBAL;
82: }
83: }
84:
85: $_caching = Smarty::CACHING_OFF;
86:
87:
88: $merge_compiled_includes = ($compiler->smarty->merge_compiled_includes || ($compiler->inheritance && $compiler->smarty->inheritance_merge_compiled_includes) || $_attr['inline'] === true) && !$compiler->template->source->recompiled;
89:
90:
91:
92: if ($compiler->template->caching && ((!$compiler->inheritance && !$compiler->nocache && !$compiler->tag_nocache) || ($compiler->inheritance && ($compiler->nocache || $compiler->tag_nocache)))) {
93: $_caching = self::CACHING_NOCACHE_CODE;
94: }
95: 96: 97: 98: 99:
100: if (isset($_attr['cache_lifetime'])) {
101: $_cache_lifetime = $_attr['cache_lifetime'];
102: $compiler->tag_nocache = true;
103: $_caching = Smarty::CACHING_LIFETIME_CURRENT;
104: } else {
105: $_cache_lifetime = 'null';
106: }
107: if (isset($_attr['cache_id'])) {
108: $_cache_id = $_attr['cache_id'];
109: $compiler->tag_nocache = true;
110: $_caching = Smarty::CACHING_LIFETIME_CURRENT;
111: } else {
112: $_cache_id = '$_smarty_tpl->cache_id';
113: }
114: if (isset($_attr['compile_id'])) {
115: $_compile_id = $_attr['compile_id'];
116: } else {
117: $_compile_id = '$_smarty_tpl->compile_id';
118: }
119: if ($_attr['caching'] === true) {
120: $_caching = Smarty::CACHING_LIFETIME_CURRENT;
121: }
122: if ($_attr['nocache'] === true) {
123: $compiler->tag_nocache = true;
124: if ($merge_compiled_includes) {
125: $_caching = self::CACHING_NOCACHE_CODE;
126: } else {
127: $_caching = Smarty::CACHING_OFF;
128: }
129: }
130:
131: $has_compiled_template = false;
132: if ($merge_compiled_includes && $_attr['inline'] !== true) {
133:
134: if ($compiler->has_variable_string || !((substr_count($include_file, '"') == 2 || substr_count($include_file, "'") == 2))
135: || substr_count($include_file, '(') != 0 || substr_count($include_file, '$_smarty_tpl->') != 0
136: ) {
137: $merge_compiled_includes = false;
138: if ($compiler->inheritance && $compiler->smarty->inheritance_merge_compiled_includes) {
139: $compiler->trigger_template_error(' variable template file names not allow within {block} tags');
140: }
141: }
142:
143: if (isset($_attr['compile_id'])) {
144: if (!((substr_count($_attr['compile_id'], '"') == 2 || substr_count($_attr['compile_id'], "'") == 2))
145: || substr_count($_attr['compile_id'], '(') != 0 || substr_count($_attr['compile_id'], '$_smarty_tpl->') != 0
146: ) {
147: $merge_compiled_includes = false;
148: if ($compiler->inheritance && $compiler->smarty->inheritance_merge_compiled_includes) {
149: $compiler->trigger_template_error(' variable compile_id not allow within {block} tags');
150: }
151: }
152: }
153: }
154: if ($merge_compiled_includes) {
155: if ($compiler->template->caching && ($compiler->tag_nocache || $compiler->nocache) && $_caching != self::CACHING_NOCACHE_CODE) {
156: $merge_compiled_includes = false;
157: if ($compiler->inheritance && $compiler->smarty->inheritance_merge_compiled_includes) {
158: $compiler->trigger_template_error(' invalid caching mode of subtemplate within {block} tags');
159: }
160: }
161: }
162: if ($merge_compiled_includes) {
163:
164: $uid = sha1($_compile_id);
165: $tpl_name = null;
166: $nocache = false;
167: 168: 169:
170: $_smarty_tpl = $compiler->template;
171: eval("\$tpl_name = $include_file;");
172: if (!isset($compiler->smarty->merged_templates_func[$tpl_name][$uid])) {
173: $tpl = new $compiler->smarty->template_class ($tpl_name, $compiler->smarty, $compiler->template, $compiler->template->cache_id, $compiler->template->compile_id);
174:
175: $compiler->smarty->merged_templates_func[$tpl_name][$uid]['func'] = $tpl->properties['unifunc'] = 'content_' . str_replace(array('.', ','), '_', uniqid('', true));
176:
177: $compiler->smarty->merged_templates_func[$tpl_name][$uid]['nocache_hash'] = $tpl->properties['nocache_hash'] = $compiler->template->properties['nocache_hash'];
178: if ($compiler->template->caching && $_caching == self::CACHING_NOCACHE_CODE) {
179:
180: $nocache = true;
181: }
182: if ($compiler->inheritance) {
183: $tpl->compiler->inheritance = true;
184: }
185:
186: $tpl->mustCompile = true;
187: if (!($tpl->source->uncompiled) && $tpl->source->exists) {
188:
189:
190: $compiled_code = $tpl->compiler->compileTemplate($tpl, $nocache);
191:
192: unset($tpl->compiler);
193:
194: $compiler->template->properties['function'] = array_merge($compiler->template->properties['function'], $tpl->properties['function']);
195:
196: $tpl->properties['file_dependency'][$tpl->source->uid] = array($tpl->source->filepath, $tpl->source->timestamp, $tpl->source->type);
197: $compiler->template->properties['file_dependency'] = array_merge($compiler->template->properties['file_dependency'], $tpl->properties['file_dependency']);
198:
199: $compiled_code = preg_replace("/(<\?php \/\*%%SmartyHeaderCode:{$tpl->properties['nocache_hash']}%%\*\/(.+?)\/\*\/%%SmartyHeaderCode%%\*\/\?>\n)/s", '', $compiled_code);
200: if ($tpl->has_nocache_code) {
201:
202: $compiled_code = str_replace("{$tpl->properties['nocache_hash']}", $compiler->template->properties['nocache_hash'], $compiled_code);
203: $compiler->template->has_nocache_code = true;
204: }
205: $compiler->merged_templates[$tpl->properties['unifunc']] = $compiled_code;
206: $has_compiled_template = true;
207: unset ($tpl);
208: }
209: } else {
210: $has_compiled_template = true;
211: }
212: }
213:
214: unset($_attr['file'], $_attr['assign'], $_attr['cache_id'], $_attr['compile_id'], $_attr['cache_lifetime'], $_attr['nocache'], $_attr['caching'], $_attr['scope'], $_attr['inline']);
215:
216: if (!empty($_attr)) {
217: if ($_parent_scope == Smarty::SCOPE_LOCAL) {
218:
219: $nccode = '';
220: foreach ($_attr as $key => $value) {
221: $_pairs[] = "'$key'=>$value";
222: $nccode .= "\$_smarty_tpl->tpl_vars['$key'] = new Smarty_variable($value);\n";
223: }
224: $_vars = 'array(' . join(',', $_pairs) . ')';
225: } else {
226: $compiler->trigger_template_error('variable passing not allowed in parent/global scope', $compiler->lex->taglineno);
227: }
228: } else {
229: $_vars = 'array()';
230: }
231: if ($has_compiled_template) {
232:
233: $compiler->suppressNocacheProcessing = true;
234: $_hash = $compiler->smarty->merged_templates_func[$tpl_name][$uid]['nocache_hash'];
235: $_output = "<?php /* Call merged included template \"" . $tpl_name . "\" */\n";
236: $_output .= "\$_tpl_stack[] = \$_smarty_tpl;\n";
237: if (!empty($nccode) && $_caching == 9999 && $_smarty_tpl->caching) {
238: $compiler->suppressNocacheProcessing = false;
239: $_output .= substr($compiler->processNocacheCode('<?php ' .$nccode . "?>\n", true), 6, -3);
240: $compiler->suppressNocacheProcessing = true;
241: }
242: $_output .= " \$_smarty_tpl = \$_smarty_tpl->setupInlineSubTemplate($include_file, $_cache_id, $_compile_id, $_caching, $_cache_lifetime, $_vars, $_parent_scope, '$_hash');\n";
243: if (isset($_assign)) {
244: $_output .= 'ob_start(); ';
245: }
246: $_output .= $compiler->smarty->merged_templates_func[$tpl_name][$uid]['func'] . "(\$_smarty_tpl);\n";
247: $_output .= "\$_smarty_tpl = array_pop(\$_tpl_stack); ";
248: if (isset($_assign)) {
249: $_output .= " \$_smarty_tpl->tpl_vars[$_assign] = new Smarty_variable(ob_get_clean());";
250: }
251: $_output .= "\n/* End of included template \"" . $tpl_name . "\" */?>";
252:
253: return $_output;
254: }
255:
256:
257: if (isset($_assign)) {
258: $_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";;
259: } else {
260: $_output = "<?php echo \$_smarty_tpl->getSubTemplate ($include_file, $_cache_id, $_compile_id, $_caching, $_cache_lifetime, $_vars, $_parent_scope);?>\n";
261: }
262:
263: return $_output;
264: }
265: }
266: