Get Smarty

Donate

Donate Bitcoin Bitcoin
Paypal

Smarty Icon

You may use the Smarty logo according to the trademark notice.

Smarty Template Engine Smarty Template Engine

For sponsorship, advertising, news or other inquiries, contact us at:

Sites Using Smarty

Buy cheap eyeglasses from Cheapglasses123.com and save up to 80%.

Buy prescription glasses from www.australiaglasses.com and save.

Cheap Glasses Now On Sale at GlassesPeople.com. Starts At $7.95.

Where to buy discount wedding dresses and cheap smart dresses free shipping - Weddingdresstrend.com

Brautkleider auf Topwedding.de

Find free files to download on allwhatyouwant.net

Looking For Affordable Wedding Dresses 2015 at Best Prices On TDBridal.com

Shop high quality cheap prom dresses on Dresswe.co.uk

Buy New Arrival Cheap Prom Dresses 2015 at JDBRIDAL Prom Dress Store

Advertisement

Chapter 17. 高级特性

安全

当你有非可信任的第三方来编辑模板时,比如说通过FTP编辑,安全机制可以帮助你通过 模板语言的限制,来减少可能对系统安全产生的危害。

安全机制可以通过Smarty_Security对象的属性来进行设置。 这里是允许的设置:

  • $php_handling明确定义了Smarty如何处理内嵌到模板的PHP代码: 可用值是:

    • Smarty::PHP_PASSTHRU -> 原样显示PHP代码

    • Smarty::PHP_QUOTE -> 将PHP代码转换成HTML实体显示

    • Smarty::PHP_REMOVE -> 删除PHP代码

    • Smarty::PHP_ALLOW -> 执行PHP代码

    默认值是Smarty::PHP_PASSTHRU。

    如果安全机制开启,Smarty对象的$php_handling 设置将不会被检查。

  • $secure_dir是安全模板目录的数组。 $template_dir 同样也被看作安全的目录。 默认这是空的数组。

  • $trusted_dir是可信任目录的数组。 信任目录可以允许你存放PHP文件,这些PHP文件可以通过 {include_php}包含到模板中, 并且直接运行。 默认这是空的数组。

  • $trusted_uri是一个包含了匹配URL的正则表达式的数组,这些URL都被认为是可信的。 这个安全功能用于 {fetch}{html_image}。 在这些函数内使用的URL,必须类似{$PROTOCOL}://{$HOSTNAME}的格式, 并且可以使用一些简单的正则表达式。 (不包括一些极端的例子如authentication-tokens)

    正则表达式'#https?://.*smarty.net$#i'将允许下面的URL:

    • 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

    但不允许以下的URL:

    • http://smarty.com/foo (不匹配顶级域名"com")

    • ftp://www.smarty.net/foo (不匹配协议"ftp")

    • http://www.smarty.net.otherdomain.com/foo (不匹配域名"smarty.net")

  • $static_classes是一个包含了可信的静态类的数组。 默认是空数组,表示信任任何的静态类。 设置不信任全部静态类可以设置$static_classes = null。

  • $php_functions是一个包含了可用的PHP函数的数组,这些函数可以在模板内直接使用。 空数组 ( $php_functions = array() ) 表示全部PHP函数都是可用的。 设置全部函数不可用,需要设置$php_functions = null。 默认值是array('isset', 'empty', 'count', 'sizeof', 'in_array', 'is_array','time','nl2br')。

  • $php_modifiers是一个包含了可用的PHP函数的数组,这些函数可以在模板内当作修饰器来使用。 空数组 ( $php_functions = array() ) 表示全部PHP函数修饰器都是可用的。 设置全部函数修饰器不可用,需要设置$php_modifier = null。 默认值是array('escape','count')。

  • $streams是一个包含了可用数据流的数组,这些数据流可以直接在模板内使用。 空数组 ( $streams = array() ) 表示全部数据流都是可用的。 设置全部数据流不可用,需要设置$streams = null。 默认值是array('file')。

  • $allowed_modifiers是一个包含了可用的(注册的/自动加载的)修饰器数组。 如果非空,则只有列表上的修饰器才能使用。这是一个白名单。

  • $disabled_modifiers是一个包含了不可用的(注册的/自动加载的)修饰器数组。

  • $allowed_tags是一个控制哪些区块标签、函数和过滤器能够在模板内使用的数组。 如果非空,则只有列表上的标签才能使用。这是一个白名单。

  • $disabled_tags是一个包含了不可用的区块标签、函数和过滤器的数组。

  • $allow_constants布尔值,代表了是否允许在模板内使用常量。 默认是true。

  • $allow_super_globals布尔值,代表了是否允许在模板内使用PHP的全局变量。 默认是true。

  • $allow_php_tag布尔值,代表了是否允许在模板内使用{php} 和 {include_php}标签。 默认是false。

如果安全机制开启,私有的成员方法、静态类/对象的私有函数或者私有属性都不能在模板内使用。

自定义你自己的安全策略,可以扩展继承Smarty_Security类或者是建立它的实例。

Example 17.1. 扩展Smarty_Security 类来设置安全策略


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

class My_Security_Policy extends Smarty_Security {
  // 关闭全部PHP函数
  public $php_functions = null;
  // 删除PHP标签
  public $php_handling = Smarty::PHP_REMOVE;
  // 允许任何函数成为修饰器
  public $modifiers = array();
}
$smarty = new Smarty();
// 开启自定义安全机制
$smarty->enableSecurity('My_Security_Policy');
?>


Example 17.2. 通过Smarty_Security类的实例来自定义安全策略


<?php
require 'Smarty.class.php';
$smarty = new Smarty();
$my_security_policy = new Smarty_Security($smarty);
// 关闭全部PHP函数
$my_security_policy->php_functions = null;
// 删除PHP标签
$my_security_policy->php_handling = Smarty::PHP_REMOVE;
// 允许任何函数成为修饰器
$my_security_policy->$modifiers = array();
// 开启自定义安全机制
$smarty->enableSecurity($my_security_policy);
?>


Example 17.3. 开启默认的安全设置


<?php
require 'Smarty.class.php';
$smarty = new Smarty();
// 开启默认安全设置
$smarty->enableSecurity();
?>


Note

安全机制只会在模板编译时进行检查。 所以当你修改了安全设置后,必须删除全部缓存和编译文件并重新编译。

Comments
No comments for this page.
Post a Comment
All comments are moderated. Support questions are ignored, use the forums instead.
Author:
Email: (not shown)
What is 14 plus 9? (Are you human?)

Advertisement