╃巡洋艦㊣ 2011-03-09 07:57:00 7265次浏览 1条评论 2 1 0

在这篇文章里,我们将描述一个基于WEB应用下避免不合法的内容注入。

我们要在一个行为里使用htmlpurifier类,用这种行为可以加强任何模型并表明各属性我们想让它们XSS安全。

我写了以下行为:

class CSafeContentBehavior extends CActiveRecordBehavior
{
    public $attributes =array();
    protected $purifier;
 
    function __construct(){
        $this->purifier = new CHtmlPurifier;
    }
 
    public function beforeSave($event)
    {
        foreach($this->attributes as $attribute){
            $this->getOwner()->{$attribute} = $this->purifier->purify($this->getOwner()->{$attribute});
        }
    }
}

把这个类放在你的应用程序目录,例如:application/behaviors/CSafeContentBehavior.php。现在你在模型的行为中这样去写:

class Post extends CActiveRecord
{
 
public function behaviors(){
    return array(
        'CSafeContentBehavor' => array( 
            'class' => 'application.behaviors.CSafeContentBehavior',
            'attributes' => array('title', 'body'),
        ),
    );
}

现在我们可以开始了。我们的post模型在每个保存操作中将净化标题和内容列。

原文地址:http://www.yiiframework.com/wiki/67/xss-safe-model-content/

觉得很赞
您需要登录后才可以评论。登录 | 立即注册