]> git.mxchange.org Git - friendica.git/blob - library/Smarty/libs/sysplugins/smarty_internal_compile_include.php
Merge pull request #1124 from tobiasd/duepuntoderivs
[friendica.git] / library / Smarty / libs / sysplugins / smarty_internal_compile_include.php
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 /**
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     /**
20      * caching mode to create nocache code but no cache file
21      */
22     const CACHING_NOCACHE_CODE = 9999;
23     /**
24      * Attribute definition: Overwrites base class.
25      *
26      * @var array
27      * @see Smarty_Internal_CompileBase
28      */
29     public $required_attributes = array('file');
30     /**
31      * Attribute definition: Overwrites base class.
32      *
33      * @var array
34      * @see Smarty_Internal_CompileBase
35      */
36     public $shorttag_order = array('file');
37     /**
38      * Attribute definition: Overwrites base class.
39      *
40      * @var array
41      * @see Smarty_Internal_CompileBase
42      */
43     public $option_flags = array('nocache', 'inline', 'caching');
44     /**
45      * Attribute definition: Overwrites base class.
46      *
47      * @var array
48      * @see Smarty_Internal_CompileBase
49      */
50     public $optional_attributes = array('_any');
51
52     /**
53      * Compiles code for the {include} tag
54      *
55      * @param  array  $args      array with attributes from parser
56      * @param  object $compiler  compiler object
57      * @param  array  $parameter array with compilation parameter
58      *
59      * @return string compiled code
60      */
61     public function compile($args, $compiler, $parameter)
62     {
63         // check and get attributes
64         $_attr = $this->getAttributes($compiler, $args);
65         // save possible attributes
66         $include_file = $_attr['file'];
67
68         if (isset($_attr['assign'])) {
69             // output will be stored in a smarty variable instead of being displayed
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         // flag if included template code should be merged into caller
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         // set default when in nocache mode
91         //       if ($compiler->template->caching && ($compiler->nocache || $compiler->tag_nocache || $compiler->forceNocache == 2)) {
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         * if the {include} tag provides individual parameter for caching
97         * it will not be included into the common cache file and treated like
98         * a nocache section
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             // variable template name ?
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             // variable compile_id?
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             // we must observe different compile_id
164             $uid = sha1($_compile_id);
165             $tpl_name = null;
166             $nocache = false;
167             /** @var Smarty_Internal_Template $_smarty_tpl
168              * used in evaluated code
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                 // save unique function name
175                 $compiler->smarty->merged_templates_func[$tpl_name][$uid]['func'] = $tpl->properties['unifunc'] = 'content_' . str_replace(array('.', ','), '_', uniqid('', true));
176                 // use current nocache hash for inlined code
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                     // all code must be nocache
180                     $nocache = true;
181                 }
182                 if ($compiler->inheritance) {
183                     $tpl->compiler->inheritance = true;
184                 }
185                 // make sure whole chain gets compiled
186                 $tpl->mustCompile = true;
187                 if (!($tpl->source->uncompiled) && $tpl->source->exists) {
188
189                     // get compiled code
190                     $compiled_code = $tpl->compiler->compileTemplate($tpl, $nocache);
191                     // release compiler object to free memory
192                     unset($tpl->compiler);
193                     // merge compiled code for {function} tags
194                     $compiler->template->properties['function'] = array_merge($compiler->template->properties['function'], $tpl->properties['function']);
195                     // merge filedependency
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                     // remove header code
199                     $compiled_code = preg_replace("/(<\?php \/\*%%SmartyHeaderCode:{$tpl->properties['nocache_hash']}%%\*\/(.+?)\/\*\/%%SmartyHeaderCode%%\*\/\?>\n)/s", '', $compiled_code);
200                     if ($tpl->has_nocache_code) {
201                         // replace nocache_hash
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         // delete {include} standard attributes
214         unset($_attr['file'], $_attr['assign'], $_attr['cache_id'], $_attr['compile_id'], $_attr['cache_lifetime'], $_attr['nocache'], $_attr['caching'], $_attr['scope'], $_attr['inline']);
215         // remaining attributes must be assigned as smarty variable
216         if (!empty($_attr)) {
217             if ($_parent_scope == Smarty::SCOPE_LOCAL) {
218                 // create variables
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             // never call inline templates in nocache mode
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         // was there an assign attribute
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 }