Yii - ChtmlPurifier - 정화 기 사용 (yii 필터 불량 코드)

2110 단어 Yii 기술
  • 컨트롤 러 에서 사용:
    public function actionCreate()
    {
    	$model=new News;
    	
    	$purifier = new CHtmlPurifier();
    	$purifier->options = array(
    		'URI.AllowedSchemes'=>array(
    							'http' => true,
    						   'https' => true,
    		),
    			  'HTML.Allowed'=>'div',
    	);
    
    	if(isset($_POST['News']))
    	{	
    		$model->attributes=$_POST['News'];
    		$model->attributes['content'] = $purifier->purify($model->attributes['content']);
    		if($model->save())
    			$this->redirect(array('view','id'=>$model->id));
    	}
    }
  • 모델 에서 의 사용:
    protected function beforeSave()
    {
    	$purifier = new CHtmlPurifier();
    	$purifier->options = array(
    		'URI.AllowedSchemes'=>array(
    							'http' => true,
    						   'https' => true,
    		),
    			  'HTML.Allowed'=>'div',
    	);
    
    	if(parent::beforeSave()){
    		if($this->isNewRecord){
    			$this->create_data = date('y-m-d H:m:s');
    			$this->content = $purifier->purify($this->content);
    		}
    		return true;
    	}else{
    		return false;
    	}
    }
  • 필터 에서 의 사용:
    public function filters()
    {
    	return array(
    		'accessControl', // perform access control for CRUD operations
    		'postOnly + delete', // we only allow deletion via POST request
    		'purifier + create', //              
    	);
    }
    
    public function filterPurifier($filterChain){
    	$purifier = new CHtmlPurifier();
    	$purifier->options = array(
    		'URI.AllowedSchemes'=>array(
    							'http' => true,
    						   'https' => true,
    		),
    			  'HTML.Allowed'=>'div',
    	);
    	if(isset($_POST['news']){
    		$_POST['news']['content'] = $purify($_POST['news']['content']);
    	}
          	$filterChain->run();
    }
  • 보기 에서 의 사용:
    beginWidget('CHtmlPurifier'); ?>  
    ...display user-entered content here...  
    endWidget(); ?>
  • 좋은 웹페이지 즐겨찾기