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: */
17: class 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: if (isset($compiler->smarty->security_policy)) {
85: $compiler->trigger_template_error("(secure mode) template_object not permitted");
86: break;
87: }
88: return '$_smarty_tpl';
89: case 'current_dir':
90: return 'dirname($_smarty_tpl->source->filepath)';
91: case 'version':
92: return "Smarty::SMARTY_VERSION";
93: case 'const':
94: if (isset($compiler->smarty->security_policy)
95: && !$compiler->smarty->security_policy->allow_constants
96: ) {
97: $compiler->trigger_template_error("(secure mode) constants not permitted");
98: break;
99: }
100: if (strpos($_index[ 1 ], '$') === false && strpos($_index[ 1 ], '\'') === false) {
101: return "(defined('{$_index[1]}') ? constant('{$_index[1]}') : null)";
102: } else {
103: return "(defined({$_index[1]}) ? constant({$_index[1]}) : null)";
104: }
105: // no break
106: case 'config':
107: if (isset($_index[ 2 ])) {
108: return "(is_array(\$tmp = \$_smarty_tpl->smarty->ext->configload->_getConfigVariable(\$_smarty_tpl, $_index[1])) ? \$tmp[$_index[2]] : null)";
109: } else {
110: return "\$_smarty_tpl->smarty->ext->configload->_getConfigVariable(\$_smarty_tpl, $_index[1])";
111: }
112: // no break
113: case 'ldelim':
114: return "\$_smarty_tpl->smarty->left_delimiter";
115: case 'rdelim':
116: return "\$_smarty_tpl->smarty->right_delimiter";
117: default:
118: $compiler->trigger_template_error('$smarty.' . trim($_index[ 0 ], "'") . ' is not defined');
119: break;
120: }
121: if (isset($_index[ 1 ])) {
122: array_shift($_index);
123: foreach ($_index as $_ind) {
124: $compiled_ref = $compiled_ref . "[$_ind]";
125: }
126: }
127: return $compiled_ref;
128: }
129: }
130: }
131: