Blame | Letzte Änderung | Log anzeigen | RSS feed
Security {#advanced.features.security}========Security is good for situations when you have untrusted parties editingthe templates e.g. via ftp, and you want to reduce the risk of systemsecurity compromises through the template language.The settings of the security policy are defined by properties of aninstance of the Smarty\_Security class. These are the possible settings:- `$secure_dir` is an array of template directories that areconsidered secure. [`$template_dir`](#variable.template.dir)considered secure implicitly. The default is an empty array.- `$trusted_dir` is an array of all directories that are consideredtrusted. Trusted directories are where you keep php scripts that areexecuted directly from the templates with[`{insert}`](#language.function.insert.php). The default is anempty array.- `$trusted_uri` is an array of regular expressions matching URIs thatare considered trusted. This security directive used by[`{fetch}`](#language.function.fetch) and[`{html_image}`](#language.function.html.image). URIs passed tothese functions are reduced to `{$PROTOCOL}://{$HOSTNAME}` to allowsimple regular expressions (without having to deal with edge caseslike authentication-tokens).The expression `'#https?://.*smarty.net$#i'` would allow accessingthe following URIs:- `http://smarty.net/foo`- `http://smarty.net/foo`- `http://www.smarty.net/foo`- `http://smarty.net/foo`- `https://foo.bar.www.smarty.net/foo/bla?blubb=1`but deny access to these URIs:- `http://smarty.com/foo` (not matching top-level domain \"com\")- `ftp://www.smarty.net/foo` (not matching protocol \"ftp\")- `http://www.smarty.net.otherdomain.com/foo` (not matching end ofdomain \"smarty.net\")- `$static_classes` is an array of classes that are consideredtrusted. The default is an empty array which allows access to allstatic classes. To disable access to all static classes set\$static\_classes = null.- `$php_functions` is an array of PHP functions that are consideredtrusted and can be used from within template. To disable access toall PHP functions set \$php\_functions = null. An empty array (\$php\_functions = array() ) will allow all PHP functions. Thedefault is array(\'isset\', \'empty\', \'count\', \'sizeof\',\'in\_array\', \'is\_array\',\'time\',\'nl2br\').- `$php_modifiers` is an array of PHP functions that are consideredtrusted and can be used from within template as modifier. To disableaccess to all PHP modifier set \$php\_modifier = null. An emptyarray ( \$php\_modifier = array() ) will allow all PHP functions.The default is array(\'escape\',\'count\').- `$streams` is an array of streams that are considered trusted andcan be used from within template. To disable access to all streamsset \$streams = null. An empty array ( \$streams = array() ) willallow all streams. The default is array(\'file\').- `$allowed_modifiers` is an array of (registered / autoloaded)modifiers that should be accessible to the template. If this arrayis non-empty, only the herein listed modifiers may be used. This isa whitelist.- `$disabled_modifiers` is an array of (registered / autoloaded)modifiers that may not be accessible to the template.- `$allowed_tags` is a boolean flag which controls if constants canfunction-, block and filter plugins that should be accessible to thetemplate. If this array is non-empty, only the herein listedmodifiers may be used. This is a whitelist.- `$disabled_tags` is an array of (registered / autoloaded) function-,block and filter plugins that may not be accessible to the template.- `$allow_constants` is a boolean flag which controls if constants canbe accessed by the template. The default is \"true\".- `$allow_super_globals` is a boolean flag which controls if the PHPsuper globals can be accessed by the template. The default is\"true\".If security is enabled, no private methods, functions or properties ofstatic classes or assigned objects can be accessed (beginning with\'\_\') by the template.To customize the security policy settings you can extend theSmarty\_Security class or create an instance of it.<?phprequire 'Smarty.class.php';class My_Security_Policy extends Smarty_Security {// disable all PHP functionspublic $php_functions = null;// allow everthing as modifierpublic $php_modifiers = array();}$smarty = new Smarty();// enable security$smarty->enableSecurity('My_Security_Policy');?><?phprequire 'Smarty.class.php';$smarty = new Smarty();$my_security_policy = new Smarty_Security($smarty);// disable all PHP functions$my_security_policy->php_functions = null;// allow everthing as modifier$my_security_policy->php_modifiers = array();// enable security$smarty->enableSecurity($my_security_policy);?><?phprequire 'Smarty.class.php';$smarty = new Smarty();// enable default security$smarty->enableSecurity();?>> **Note**>> Most security policy settings are only checked when the template gets> compiled. For that reason you should delete all cached and compiled> template files when you change your security settings.