1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
<?php
/**
* Smarty plugin
*
* @package Smarty
* @subpackage Security
* @author Uwe Tews
*/
/**
* This class does contain the security settings
*/
class Smarty_Security {
/**
* This determines how Smarty handles "<?php ... ?>" tags in templates.
* possible values:
* <ul>
* <li>Smarty::PHP_PASSTHRU -> echo PHP tags as they are</li>
* <li>Smarty::PHP_QUOTE -> escape tags as entities</li>
* <li>Smarty::PHP_REMOVE -> remove php tags</li>
* <li>Smarty::PHP_ALLOW -> execute php tags</li>
* </ul>
*
* @var integer
*/
public $php_handling = Smarty::PHP_PASSTHRU;
/**
* This is the list of template directories that are considered secure.
* $template_dir is in this list implicitly.
*
* @var array
*/
public $secure_dir = array();
/**
* This is an array of directories where trusted php scripts reside.
* {@link $security} is disabled during their inclusion/execution.
*
* @var array
*/
public $trusted_dir = array();
/**
* This is an array of trusted static classes.
*
* If empty access to all static classes is allowed.
* If set to 'none' none is allowed.
* @var array
*/
public $static_classes = array();
/**
* This is an array of trusted PHP functions.
*
* If empty all functions are allowed.
* To disable all PHP functions set $php_functions = null.
* @var array
*/
public $php_functions = array('isset', 'empty',
'count', 'sizeof','in_array', 'is_array','time','nl2br');
/**
* This is an array of trusted PHP modifers.
*
* If empty all modifiers are allowed.
* To disable all modifier set $modifiers = null.
* @var array
*/
public $php_modifiers = array('escape','count');
/**
* This is an array of trusted streams.
*
* If empty all streams are allowed.
* To disable all streams set $streams = null.
* @var array
*/
public $streams = array('file');
/**
* + flag if constants can be accessed from template
*/
public $allow_constants = true;
/**
* + flag if super globals can be accessed from template
*/
public $allow_super_globals = true;
/**
* + flag if the {php} and {include_php} tag can be executed
*/
public $allow_php_tag = false;
public function __construct($smarty)
{
$this->smarty = $smarty;
}
/**
* Check if PHP function is trusted.
*
* @param string $function_name
* @param object $compiler compiler object
* @return boolean true if function is trusted
*/
function isTrustedPhpFunction($function_name, $compiler)
{
if (isset($this->php_functions) && (empty($this->php_functions) || in_array($function_name, $this->php_functions))) {
return true;
} else {
$compiler->trigger_template_error ("PHP function '{$function_name}' not allowed by security setting");
return false;
}
}
/**
* Check if static class is trusted.
*
* @param string $class_name
* @param object $compiler compiler object
* @return boolean true if class is trusted
*/
function isTrustedStaticClass($class_name, $compiler)
{
if (isset($this->static_classes) && (empty($this->static_classes) || in_array($class_name, $this->static_classes))) {
return true;
} else {
$compiler->trigger_template_error ("access to static class '{$class_name}' not allowed by security setting");
return false;
}
}
/**
* Check if modifier is trusted.
*
* @param string $modifier_name
* @param object $compiler compiler object
* @return boolean true if modifier is trusted
*/
function isTrustedModifier($modifier_name, $compiler)
{
if (isset($this->php_modifiers) && (empty($this->php_modifiers) || in_array($modifier_name, $this->php_modifiers))) {
return true;
} else {
$compiler->trigger_template_error ("modifier '{$modifier_name}' not allowed by security setting");
return false;
}
}
/**
* Check if stream is trusted.
*
* @param string $stream_name
* @param object $compiler compiler object
* @return boolean true if stream is trusted
*/
function isTrustedStream($stream_name)
{
if (isset($this->streams) && (empty($this->streams) || in_array($stream_name, $this->streams))) {
return true;
} else {
throw new SmartyException ("stream '{$stream_name}' not allowed by security setting");
return false;
}
}
/**
* Check if directory of file resource is trusted.
*
* @param string $filepath
* @param object $compiler compiler object
* @return boolean true if directory is trusted
*/
function isTrustedResourceDir($filepath)
{
$_rp = realpath($filepath);
if (isset($this->smarty->template_dir)) {
foreach ((array)$this->smarty->template_dir as $curr_dir) {
if (($_cd = realpath($curr_dir)) !== false &&
strncmp($_rp, $_cd, strlen($_cd)) == 0 &&
(strlen($_rp) == strlen($_cd) || substr($_rp, strlen($_cd), 1) == DS)) {
return true;
}
}
}
if (!empty($this->smarty->security_policy->secure_dir)) {
foreach ((array)$this->smarty->security_policy->secure_dir as $curr_dir) {
if (($_cd = realpath($curr_dir)) !== false) {
if ($_cd == $_rp) {
return true;
} elseif (strncmp($_rp, $_cd, strlen($_cd)) == 0 &&
(strlen($_rp) == strlen($_cd) || substr($_rp, strlen($_cd), 1) == DS)) {
return true;
}
}
}
}
throw new SmartyException ("directory '{$_rp}' not allowed by security setting");
return false;
}
/**
* Check if directory of file resource is trusted.
*
* @param string $filepath
* @param object $compiler compiler object
* @return boolean true if directory is trusted
*/
function isTrustedPHPDir($filepath)
{
$_rp = realpath($filepath);
if (!empty($this->trusted_dir)) {
foreach ((array)$this->trusted_dir as $curr_dir) {
if (($_cd = realpath($curr_dir)) !== false) {
if ($_cd == $_rp) {
return true;
} elseif (strncmp($_rp, $_cd, strlen($_cd)) == 0 &&
substr($_rp, strlen($_cd), 1) == DS) {
return true;
}
}
}
}
throw new SmartyException ("directory '{$_rp}' not allowed by security setting");
return false;
}
}
?>
|