Subversion Repositories ALCASAR

Rev

Details | Last modification | View Log

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