]> git.mxchange.org Git - friendica.git/blob - library/Smarty/libs/sysplugins/smarty_internal_compile_include.php
Merge remote branch 'upstream/master'
[friendica.git] / library / Smarty / libs / sysplugins / smarty_internal_compile_include.php
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 /**
13 * Smarty Internal Plugin Compile Include Class
14 *
15 * @package Smarty
16 * @subpackage Compiler
17 */
18 class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase {
19
20     /**
21     * caching mode to create nocache code but no cache file
22     */
23     const CACHING_NOCACHE_CODE = 9999;
24     /**
25     * Attribute definition: Overwrites base class.
26     *
27     * @var array
28     * @see Smarty_Internal_CompileBase
29     */
30     public $required_attributes = array('file');
31     /**
32     * Attribute definition: Overwrites base class.
33     *
34     * @var array
35     * @see Smarty_Internal_CompileBase
36     */
37     public $shorttag_order = array('file');
38     /**
39     * Attribute definition: Overwrites base class.
40     *
41     * @var array
42     * @see Smarty_Internal_CompileBase
43     */
44     public $option_flags = array('nocache', 'inline', 'caching');
45     /**
46     * Attribute definition: Overwrites base class.
47     *
48     * @var array
49     * @see Smarty_Internal_CompileBase
50     */
51     public $optional_attributes = array('_any');
52
53     /**
54     * Compiles code for the {include} tag
55     *
56      * @param array $args array with attributes from parser
57      * @param object $compiler compiler object
58      * @param array $parameter array with compilation parameter
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 posible attributes
66         $include_file = $_attr['file'];
67
68         if (isset($_attr['assign'])) {
69             // output will be stored in a smarty variable instead of beind 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         $_caching = 'null';
85         if ($compiler->nocache || $compiler->tag_nocache) {
86             $_caching = Smarty::CACHING_OFF;
87         }
88         // default for included templates
89         if ($compiler->template->caching && !$compiler->nocache && !$compiler->tag_nocache) {
90             $_caching = self::CACHING_NOCACHE_CODE;
91         }
92         /*
93         * if the {include} tag provides individual parameter for caching
94         * it will not be included into the common cache file and treated like
95         * a nocache section
96         */
97         if (isset($_attr['cache_lifetime'])) {
98             $_cache_lifetime = $_attr['cache_lifetime'];
99             $compiler->tag_nocache = true;
100             $_caching = Smarty::CACHING_LIFETIME_CURRENT;
101         } else {
102             $_cache_lifetime = 'null';
103         }
104         if (isset($_attr['cache_id'])) {
105             $_cache_id = $_attr['cache_id'];
106             $compiler->tag_nocache = true;
107             $_caching = Smarty::CACHING_LIFETIME_CURRENT;
108         } else {
109             $_cache_id = '$_smarty_tpl->cache_id';
110         }
111         if (isset($_attr['compile_id'])) {
112             $_compile_id = $_attr['compile_id'];
113         } else {
114             $_compile_id = '$_smarty_tpl->compile_id';
115         }
116         if ($_attr['caching'] === true) {
117             $_caching = Smarty::CACHING_LIFETIME_CURRENT;
118         }
119         if ($_attr['nocache'] === true) {
120             $compiler->tag_nocache = true;
121             $_caching = Smarty::CACHING_OFF;
122         }
123
124         $has_compiled_template = false;
125         if (($compiler->smarty->merge_compiled_includes || $_attr['inline'] === true) && !$compiler->template->source->recompiled
126             && !($compiler->template->caching && ($compiler->tag_nocache || $compiler->nocache)) && $_caching != Smarty::CACHING_LIFETIME_CURRENT) {
127             // check if compiled code can be merged (contains no variable part)
128             if (!$compiler->has_variable_string && (substr_count($include_file, '"') == 2 or substr_count($include_file, "'") == 2)
129                and substr_count($include_file, '(') == 0 and substr_count($include_file, '$_smarty_tpl->') == 0) {
130                 $tpl_name = null;
131                 eval("\$tpl_name = $include_file;");
132                 if (!isset($compiler->smarty->merged_templates_func[$tpl_name]) || $compiler->inheritance) {
133                     $tpl = new $compiler->smarty->template_class ($tpl_name, $compiler->smarty, $compiler->template, $compiler->template->cache_id, $compiler->template->compile_id);
134                     // save unique function name
135                     $compiler->smarty->merged_templates_func[$tpl_name]['func'] = $tpl->properties['unifunc'] = 'content_'. str_replace('.', '_', uniqid('', true));
136                     // use current nocache hash for inlined code
137                     $compiler->smarty->merged_templates_func[$tpl_name]['nocache_hash'] = $tpl->properties['nocache_hash'] = $compiler->template->properties['nocache_hash'];
138                     if ($compiler->template->caching) {
139                         // needs code for cached page but no cache file
140                         $tpl->caching = self::CACHING_NOCACHE_CODE;
141                     }
142                     // make sure whole chain gest compiled
143                     $tpl->mustCompile = true;
144                     if (!($tpl->source->uncompiled) && $tpl->source->exists) {
145                         // get compiled code
146                         $compiled_code = $tpl->compiler->compileTemplate($tpl);
147                         // release compiler object to free memory
148                         unset($tpl->compiler);
149                         // merge compiled code for {function} tags
150                         $compiler->template->properties['function'] = array_merge($compiler->template->properties['function'], $tpl->properties['function']);
151                         // merge filedependency
152                         $tpl->properties['file_dependency'][$tpl->source->uid] = array($tpl->source->filepath, $tpl->source->timestamp,$tpl->source->type);
153                         $compiler->template->properties['file_dependency'] = array_merge($compiler->template->properties['file_dependency'], $tpl->properties['file_dependency']);
154                         // remove header code
155                         $compiled_code = preg_replace("/(<\?php \/\*%%SmartyHeaderCode:{$tpl->properties['nocache_hash']}%%\*\/(.+?)\/\*\/%%SmartyHeaderCode%%\*\/\?>\n)/s", '', $compiled_code);
156                         if ($tpl->has_nocache_code) {
157                             // replace nocache_hash
158                             $compiled_code = str_replace("{$tpl->properties['nocache_hash']}", $compiler->template->properties['nocache_hash'], $compiled_code);
159                             $compiler->template->has_nocache_code = true;
160                         }
161                         $compiler->merged_templates[$tpl->properties['unifunc']] = $compiled_code;
162                         $has_compiled_template = true;
163                     }
164                 } else {
165                     $has_compiled_template = true;
166                 }
167             }
168         }
169         // delete {include} standard attributes
170         unset($_attr['file'], $_attr['assign'], $_attr['cache_id'], $_attr['compile_id'], $_attr['cache_lifetime'], $_attr['nocache'], $_attr['caching'], $_attr['scope'], $_attr['inline']);
171         // remaining attributes must be assigned as smarty variable
172         if (!empty($_attr)) {
173             if ($_parent_scope == Smarty::SCOPE_LOCAL) {
174                 // create variables
175                 foreach ($_attr as $key => $value) {
176                     $_pairs[] = "'$key'=>$value";
177                 }
178                 $_vars = 'array('.join(',',$_pairs).')';
179                 $_has_vars = true;
180             } else {
181                 $compiler->trigger_template_error('variable passing not allowed in parent/global scope', $compiler->lex->taglineno);
182             }
183         } else {
184             $_vars = 'array()';
185             $_has_vars = false;
186         }
187         if ($has_compiled_template) {
188             $_hash = $compiler->smarty->merged_templates_func[$tpl_name]['nocache_hash'];
189             $_output = "<?php /*  Call merged included template \"" . $tpl_name . "\" */\n";
190             $_output .= "\$_tpl_stack[] = \$_smarty_tpl;\n";
191             $_output .= " \$_smarty_tpl = \$_smarty_tpl->setupInlineSubTemplate($include_file, $_cache_id, $_compile_id, $_caching, $_cache_lifetime, $_vars, $_parent_scope, '$_hash');\n";
192             if (isset($_assign)) {
193                 $_output .= 'ob_start(); ';
194             }
195             $_output .= $compiler->smarty->merged_templates_func[$tpl_name]['func']. "(\$_smarty_tpl);\n";
196             $_output .= "\$_smarty_tpl = array_pop(\$_tpl_stack); ";
197             if (isset($_assign)) {
198                 $_output .= " \$_smarty_tpl->tpl_vars[$_assign] = new Smarty_variable(ob_get_clean());";
199             }
200             $_output .= "/*  End of included template \"" . $tpl_name . "\" */?>";
201             return $_output;
202         }
203
204         // was there an assign attribute
205         if (isset($_assign)) {
206             $_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";;
207         } else {
208             $_output = "<?php echo \$_smarty_tpl->getSubTemplate ($include_file, $_cache_id, $_compile_id, $_caching, $_cache_lifetime, $_vars, $_parent_scope);?>\n";
209         }
210         return $_output;
211     }
212
213 }
214
215 ?>