]> git.mxchange.org Git - friendica.git/blob - vendor/smarty/smarty/libs/sysplugins/smarty_internal_compile_insert.php
Add Smarty to Composer
[friendica.git] / vendor / smarty / smarty / libs / sysplugins / smarty_internal_compile_insert.php
1 <?php
2
3 /**
4  * Smarty Internal Plugin Compile Insert
5  * Compiles the {insert} tag
6  *
7  * @package    Smarty
8  * @subpackage Compiler
9  * @author     Uwe Tews
10  */
11
12 /**
13  * Smarty Internal Plugin Compile Insert Class
14  *
15  * @package    Smarty
16  * @subpackage Compiler
17  */
18 class Smarty_Internal_Compile_Insert extends Smarty_Internal_CompileBase
19 {
20     /**
21      * Attribute definition: Overwrites base class.
22      *
23      * @var array
24      * @see Smarty_Internal_CompileBase
25      */
26     public $required_attributes = array('name');
27
28     /**
29      * Attribute definition: Overwrites base class.
30      *
31      * @var array
32      * @see Smarty_Internal_CompileBase
33      */
34     public $shorttag_order = array('name');
35
36     /**
37      * Attribute definition: Overwrites base class.
38      *
39      * @var array
40      * @see Smarty_Internal_CompileBase
41      */
42     public $optional_attributes = array('_any');
43
44     /**
45      * Compiles code for the {insert} tag
46      *
47      * @param  array                                $args     array with attributes from parser
48      * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
49      *
50      * @return string compiled code
51      * @throws \SmartyCompilerException
52      */
53     public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler)
54     {
55         // check and get attributes
56         $_attr = $this->getAttributes($compiler, $args);
57         //Does tag create output
58         $compiler->has_output = isset($_attr[ 'assign' ]) ? false : true;
59
60         $nocacheParam = $compiler->template->caching && ($compiler->tag_nocache || $compiler->nocache);
61         if (!$nocacheParam) {
62             // do not compile as nocache code
63             $compiler->suppressNocacheProcessing = true;
64         }
65         $compiler->tag_nocache = true;
66         $_smarty_tpl = $compiler->template;
67         $_name = null;
68         $_script = null;
69
70         $_output = '<?php ';
71         // save possible attributes
72         eval('$_name = @' . $_attr[ 'name' ] . ';');
73         if (isset($_attr[ 'assign' ])) {
74             // output will be stored in a smarty variable instead of being displayed
75             $_assign = $_attr[ 'assign' ];
76             // create variable to make sure that the compiler knows about its nocache status
77             $var = trim($_attr[ 'assign' ], "'");
78             if (isset($compiler->template->tpl_vars[ $var ])) {
79                 $compiler->template->tpl_vars[ $var ]->nocache = true;
80             } else {
81                 $compiler->template->tpl_vars[ $var ] = new Smarty_Variable(null, true);
82             }
83         }
84         if (isset($_attr[ 'script' ])) {
85             // script which must be included
86             $_function = "smarty_insert_{$_name}";
87             $_smarty_tpl = $compiler->template;
88             $_filepath = false;
89             eval('$_script = @' . $_attr[ 'script' ] . ';');
90             if (!isset($compiler->smarty->security_policy) && file_exists($_script)) {
91                 $_filepath = $_script;
92             } else {
93                 if (isset($compiler->smarty->security_policy)) {
94                     $_dir = $compiler->smarty->security_policy->trusted_dir;
95                 } else {
96                     $_dir = $compiler->smarty instanceof SmartyBC ? $compiler->smarty->trusted_dir : null;
97                 }
98                 if (!empty($_dir)) {
99                     foreach ((array) $_dir as $_script_dir) {
100                         $_script_dir = rtrim($_script_dir, '/\\') . $compiler->smarty->ds;
101                         if (file_exists($_script_dir . $_script)) {
102                             $_filepath = $_script_dir . $_script;
103                             break;
104                         }
105                     }
106                 }
107             }
108             if ($_filepath == false) {
109                 $compiler->trigger_template_error("{insert} missing script file '{$_script}'", null, true);
110             }
111             // code for script file loading
112             $_output .= "require_once '{$_filepath}' ;";
113             require_once $_filepath;
114             if (!is_callable($_function)) {
115                 $compiler->trigger_template_error(" {insert} function '{$_function}' is not callable in script file '{$_script}'",
116                                                   null, true);
117             }
118         } else {
119             $_filepath = 'null';
120             $_function = "insert_{$_name}";
121             // function in PHP script ?
122             if (!is_callable($_function)) {
123                 // try plugin
124                 if (!$_function = $compiler->getPlugin($_name, 'insert')) {
125                     $compiler->trigger_template_error("{insert} no function or plugin found for '{$_name}'", null,
126                                                       true);
127                 }
128             }
129         }
130         // delete {insert} standard attributes
131         unset($_attr[ 'name' ], $_attr[ 'assign' ], $_attr[ 'script' ], $_attr[ 'nocache' ]);
132         // convert attributes into parameter array string
133         $_paramsArray = array();
134         foreach ($_attr as $_key => $_value) {
135             $_paramsArray[] = "'$_key' => $_value";
136         }
137         $_params = 'array(' . implode(", ", $_paramsArray) . ')';
138         // call insert
139         if (isset($_assign)) {
140             if ($_smarty_tpl->caching && !$nocacheParam) {
141                 $_output .= "echo Smarty_Internal_Nocache_Insert::compile ('{$_function}',{$_params}, \$_smarty_tpl, '{$_filepath}',{$_assign});?>";
142             } else {
143                 $_output .= "\$_smarty_tpl->assign({$_assign} , {$_function} ({$_params},\$_smarty_tpl), true);?>";
144             }
145         } else {
146             if ($_smarty_tpl->caching && !$nocacheParam) {
147                 $_output .= "echo Smarty_Internal_Nocache_Insert::compile ('{$_function}',{$_params}, \$_smarty_tpl, '{$_filepath}');?>";
148             } else {
149                 $_output .= "echo {$_function}({$_params},\$_smarty_tpl);?>";
150             }
151         }
152
153         return $_output;
154     }
155 }