1<?php
2/**
3 * Smarty Internal Plugin Compile Special Smarty Variable
4 * Compiles the special $smarty variables
5 *
6 * @package    Smarty
7 * @subpackage Compiler
8 * @author     Uwe Tews
9 */
10
11/**
12 * Smarty Internal Plugin Compile special Smarty Variable Class
13 *
14 * @package    Smarty
15 * @subpackage Compiler
16 */
17class Smarty_Internal_Compile_Private_Special_Variable extends Smarty_Internal_CompileBase
18{
19    /**
20     * Compiles code for the special $smarty variables
21     *
22     * @param array                                 $args     array with attributes from parser
23     * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
24     * @param                                       $parameter
25     *
26     * @return string compiled code
27     * @throws \SmartyCompilerException
28     */
29    public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
30    {
31        $_index = preg_split("/\]\[/", substr($parameter, 1, strlen($parameter) - 2));
32        $variable = strtolower($compiler->getId($_index[ 0 ]));
33        if ($variable === false) {
34            $compiler->trigger_template_error("special \$Smarty variable name index can not be variable", null, true);
35        }
36        if (!isset($compiler->smarty->security_policy)
37            || $compiler->smarty->security_policy->isTrustedSpecialSmartyVar($variable, $compiler)
38        ) {
39            switch ($variable) {
40                case 'foreach':
41                case 'section':
42                    if (!isset(Smarty_Internal_TemplateCompilerBase::$_tag_objects[ $variable ])) {
43                        $class = 'Smarty_Internal_Compile_' . ucfirst($variable);
44                        Smarty_Internal_TemplateCompilerBase::$_tag_objects[ $variable ] = new $class;
45                    }
46                    return Smarty_Internal_TemplateCompilerBase::$_tag_objects[ $variable ]->compileSpecialVariable(
47                        array(),
48                        $compiler,
49                        $_index
50                    );
51                case 'capture':
52                    if (class_exists('Smarty_Internal_Compile_Capture')) {
53                        return Smarty_Internal_Compile_Capture::compileSpecialVariable(array(), $compiler, $_index);
54                    }
55                    return '';
56                case 'now':
57                    return 'time()';
58                case 'cookies':
59                    if (isset($compiler->smarty->security_policy)
60                        && !$compiler->smarty->security_policy->allow_super_globals
61                    ) {
62                        $compiler->trigger_template_error("(secure mode) super globals not permitted");
63                        break;
64                    }
65                    $compiled_ref = '$_COOKIE';
66                    break;
67                case 'get':
68                case 'post':
69                case 'env':
70                case 'server':
71                case 'session':
72                case 'request':
73                    if (isset($compiler->smarty->security_policy)
74                        && !$compiler->smarty->security_policy->allow_super_globals
75                    ) {
76                        $compiler->trigger_template_error("(secure mode) super globals not permitted");
77                        break;
78                    }
79                    $compiled_ref = '$_' . strtoupper($variable);
80                    break;
81                case 'template':
82                    return 'basename($_smarty_tpl->source->filepath)';
83                case 'template_object':
84                    return '$_smarty_tpl';
85                case 'current_dir':
86                    return 'dirname($_smarty_tpl->source->filepath)';
87                case 'version':
88                    return "Smarty::SMARTY_VERSION";
89                case 'const':
90                    if (isset($compiler->smarty->security_policy)
91                        && !$compiler->smarty->security_policy->allow_constants
92                    ) {
93                        $compiler->trigger_template_error("(secure mode) constants not permitted");
94                        break;
95                    }
96                    if (strpos($_index[ 1 ], '$') === false && strpos($_index[ 1 ], '\'') === false) {
97                        return "@constant('{$_index[1]}')";
98                    } else {
99                        return "@constant({$_index[1]})";
100                    }
101                // no break
102                case 'config':
103                    if (isset($_index[ 2 ])) {
104                        return "(is_array(\$tmp = \$_smarty_tpl->smarty->ext->configload->_getConfigVariable(\$_smarty_tpl, $_index[1])) ? \$tmp[$_index[2]] : null)";
105                    } else {
106                        return "\$_smarty_tpl->smarty->ext->configload->_getConfigVariable(\$_smarty_tpl, $_index[1])";
107                    }
108                // no break
109                case 'ldelim':
110                    return "\$_smarty_tpl->smarty->left_delimiter";
111                case 'rdelim':
112                    return "\$_smarty_tpl->smarty->right_delimiter";
113                default:
114                    $compiler->trigger_template_error('$smarty.' . trim($_index[ 0 ], "'") . ' is not defined');
115                    break;
116            }
117            if (isset($_index[ 1 ])) {
118                array_shift($_index);
119                foreach ($_index as $_ind) {
120                    $compiled_ref = $compiled_ref . "[$_ind]";
121                }
122            }
123            return $compiled_ref;
124        }
125    }
126}
127