PhpRiot
Follow phpriot on Twitter
Sponsored Link
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store
Free iPad/iPhone App
Available on the App Store

  • PHP manual
  • Zend Framework manual
  • Smarty manual
  • PHP articles
  • PHP training

Security

Security is good for situations when you have untrusted parties editing the templates eg via ftp, and you want to reduce the risk of system security compromises through the template language.

The settings of the security policy are defined by properties of an instance of the Smarty_Security class. These are the possible settings:

  • $php_handling determines how Smarty to handle PHP code embedded in templates. Possible values are:

    • Smarty::PHP_PASSTHRU -> echo PHP tags as they are

    • Smarty::PHP_QUOTE -> escape tags as entities

    • Smarty::PHP_REMOVE -> remove php tags

    • Smarty::PHP_ALLOW -> execute php tags

    The default value is Smarty::PHP_PASSTHRU.

    If security is enabled the $php_handling setting of the Smarty object is not checked for security.

  • $secure_dir is an array of template directories that are considered secure. $template_dir concidered secure implicitly. The default is an empty array.

  • $trusted_dir is an array of all directories that are considered trusted. Trusted directories are where you keep php scripts that are executed directly from the templates with {include_php}. The default is an empty array.

  • $static_classes is an array of classes that are considered trusted. The default is an empty array which allows access to all static classes. To disable access to all static classes set $static_classes = null.

  • $php_functions is an array of PHP functions that are considered trusted and can be used from within template. To disable access to all PHP functions set $php_functions = null. An empty array ( $php_functions = array() ) will allow all PHP functions. The default is array('isset', 'empty', 'count', 'sizeof', 'in_array', 'is_array','time','nl2br').

  • $php_modifiers is an array of PHP functions that are considered trusted and can be used from within template as modifier. To disable access to all PHP modifier set $php_modifier = null. An empty array ( $php_modifier = array() ) will allow all PHP functions. The default is array('escape','count').

  • $streams is an array of streams that are considered trusted and can be used from within template. To disable access to all streams set $streams = null. An empty array ( $streams = array() ) will allow all streams. The default is array('file').

  • $allow_constants is a boolean flag which controls if constants can be accessed by the template. The default is "true".

  • $allow_super_globals is a boolean flag which controls if the PHP super globals can be accessed by the template. The default is "true".

  • $allow_php_tag is a boolean flag which controls if {php} and {include_php} tags can be used by the template. The default is "false".

If security is enabled, no private methods, functions or properties of static classes or assigned objects can be accessed (beginningwith '_') by the template.

To customize the security policy settings you can extend the Smarty_Security class or create an instance of it.

Example 206. Setting security policy by extending the Smarty_Security class



<?php
require 'Smarty.class.php';

class 
My_Security_Policy extends Smarty_Security {
  
// disable all PHP functions
  
public $php_functions null;
  
// remove PHP tags
  
public $php_handling Smarty::PHP_REMOVE;
  
// allow everthing as modifier
  
public $modifiers = array();
}
$smarty = new Smarty();
// enable security
$smarty->enableSecurity('My_Security_Policy');
?>


Example 207. Setting security policy by instance of the Smarty_Security class



<?php
require 'Smarty.class.php';
$smarty = new Smarty();
$my_security_policy = new Smarty_Security($smarty);
// disable all PHP functions
$my_security_policy->php_functions null;
// remove PHP tags
$my_security_policy->php_handling Smarty::PHP_REMOVE;
// allow everthing as modifier
$my_security_policy->$modifiers = array();
// enable security
$smarty->enableSecurity($my_security_policy);
?>


Example 208. Enable security with the default settings



<?php
require 'Smarty.class.php';
$smarty = new Smarty();
// enable default security
$smarty->enableSecurity();
?>


Note

Must security policy settings are only checked when the template gets compiled. For that reasion you should delete all cached and compiled template files when you change your security settings.

Smarty Template Engine