input The suite you need to clean the input coming from users

  1. Note
  2. Requirements
  3. Usage
  4. Available methods
  5. Installation:

This component is everything you need in order to be sure that the input coming from your users is santitized and the data is safe.
It wraps HtmlPurifier and Codeigniter Security class in a single component that does an awesome job.
Regarding Codeigniter Security class, it has the best xss filter that i know of(excepting html purifier) and it is very fast (comparing to html purifier).
Why including both you may ask, well, the answer is simple, the HtmlPurifier being a large and kind of slow library, it is used only for HTML content cleaning, say a textarea from CKEDITOR or TINYMCE, while Codeigniter XSS filter is used for any other input type because it is way faster and does the job as it should.


This extension is not meant to protect you against sql injection, use Yii's param binding feature for that.


Yii 1.1.x


Say we have a model having a CKEDITOR textarea and other text fields that doesn't allow any html content. We need to use html purifier to be sure the CKEDITOR textarea is treated correctly, but we also need to be sure that the other fields are stripped of html and also cleaned so that the cannot contain dangerous data.

    foreach($_POST['Example'] AS $key=>$value)  
        if(in_array($key, $purify))  

In case our model doesn't have a textarea having HTML content, we can just clean everything from one move:

    // this will apply stripTags() then xssClean() methods to the array.  

    // also you can do:  


Handling $_GET and $_POST. Say you want to get a $_GET/$_POST variable and use it:


In both cases, the $_POST['username'] and $_GET['username'] are sanitized and returned.

Available methods

*   1) HtmlPurifier Filter.  
*   Method: purify($str);
*   2) XssClean Filter  
*   Method: xssClean($str);
*   This will use the Codeigniter Xss Filter to clean the content   
*   3) stripTags($str, $encode=false)  
*   This is a wrapper for strip_tags but it also supports an array as it's param  
*   4) stripClean($str)  
*   This will use the above stripTags() method, then the xssClean() method.  

*   5) stripCleanEncode($str)  
*   Similar to the above stripClean() method, only it also encodes the input  

*   6) encode($str)  
*   A wrapper for CHtml::encode() but also can take an array as param  

*   7) get($str, $defaultValue='', $xssClean=true)  
*   The get() method will fetch a $_GET item, clean and return it

*   8) post($str, $defaultValue='', $xssClean=true)  
*   The post() method will fetch a $_POST item, clean and return it

*   9) getPost($str, $defaultValue='', $xssClean=true)  
*   The getPost() method will try to fetch a $_GET item, if it doesn't exists, it'll try a $_POST item.  
*   The returned content is cleaned.


Extract the archive and paste the contents of the "protected" folder from within the archive over your project protected folder.
The archive protected folder contains :

Note on htmlpurifier.php from config folder.
This file is the configuration for HtmlPurifier, so if you want to add extra configuration for the purifier, do it in this file.

Open main.php and make it look like:

'preload'=>array('log', 'input'),  
            'class'         => 'CmsInput',  
            'cleanPost'     => false,  
            'cleanGet'      => false,   

The component has the ability to be used as a global cleaning tool, so that if you set:

            'class'         => 'CmsInput',  
            'cleanPost'     => true,  
            'cleanGet'      => true,  

The $_GET/$_POST will be cleaned right when the application starts, at the onBeginRequest event.
In case you use this approach (i wouldn't recommend it, but there are cases when you might need it) you should know that the $_GET/$_POST are cleaned using Yii::app()->input->xssClean() which might remove HTML markup that you want to keep. If this happens, you have following methods available to access the original global arrays:

Yii::app()->input->getOriginalPost();//returns the original, uncleaned $_POST array()  
Yii::app()->input->getOriginalGet();//returns the original, uncleaned $_GET array()   


  • Added public $cleanMethod, which allows to specify which filter should be used to clean globals.You can use the following methods for cleaning:

->stripClean(); //default one

  • Added stripEncode method, which will strip tags and encode.
  • Added cleanEncode method that will xssClean and encode
  • Added decode method to decode a string/array
  • post/get methods can now retrieve the entire array at once
  • getOriginalPost()/getOriginalGet() can retrieve a single key or the entire array
  • Fixed a bug in the encode method
  • Added logging for global filtering
  • The cleaning of the globals is now set to true by default, it is safer this way
  • Other various changes.
Changelog for 1.2
  • added getQuery(), a wrapper for get() to be more yii like.
  • getPost will now retrieve a value from $_POST, being a post() wrapper to be more yii like.
  • fixed a bug in get/post where if the $defaultValue was set and the variable didn't existed  it would return an empty string(thanks to Wiseon3 [] who pointed it out)
  • logging will occur just in debug mode from now on.
  • changed the default cleaning method to from stripCleanEncode to stripClean
Changelog for 1.3
  • Updated CodeIgniter Security class
  • Improved the stripTags method so that encoded chars won't go through anymore (thanks Jos Wetzels (

This is a pretty large component, so it might take a while to fully understand how it works, but in the end, it is a tool that is needed to be sure the user input is sanitized and clean.

Also, worth taking a look inside the CmsInput.php file to understand it better.

36 0
3 445 downloads
Yii Version: 1.1
License: (not set)
Category: Security
Developed by: twisted1919
Created on: Jun 4, 2011
Last updated: 9 years ago


show all

Related Extensions