We need to use the htmlpurifier class in an action, which can enhance any model and indicate the attributes we want to make them XSS secure.
We need to use the htmlpurifier class in an action, which can enhance any model and indicate the attributes we want to make them XSS secure.
I wrote the following behavior:
?
12345678910111213141516 |
Detail {public $ attributes = array (); protected $ purifier; function _ construct () {$ this-> purifier = newCHtmlPurifier;} publicfunctionbeforeSave ($ event) {foreach ($ this-> attributesas $ attribute) {$ this-> getOwner () ->{$ attribute }=$ this-> purifier-> purify ($ this-> getOwner ()->{$ attribute });}}} |
Put this class in your application directory, such as application/behaviors/CSafeContentBehavior. php. Now you write the following in the Model behavior:
?
123456789101112 |
ClassPostextendsCActiveRecord {publicfunctionbehaviors () {returnarray ('csafecontentbehavor' => array ('class' => 'application. behaviors. CSafeContentBehavior ', 'bubuckets' => array ('title', 'body '),),);}} |
Now we can start. Our post model clears the title and content columns in each save operation.