HtmlPurifier.php

Go to the documentation of this file.
00001 <?php
00002 
00033 class Robo47_Filter_HtmlPurifier implements Zend_Filter_Interface
00034 {
00035 
00040     protected $_purifier = null;
00045     protected static $_defaultPurifier;
00046 
00051     public function __construct($purifier = null)
00052     {
00053         $this->setPurifier($purifier);
00054     }
00055 
00060     public function setPurifier($purifier = null)
00061     {
00062         if (null === $purifier) {
00063             if (null === self::$_defaultPurifier) {
00064                 $purifier = new HtmlPurifier();
00065             } else {
00066                 $purifier = self::$_defaultPurifier;
00067             }
00068         } elseif (is_string($purifier)) {
00069             $purifier = $this->_purifierFromRegistry($purifier);
00070         }
00071         if (!$purifier instanceof HtmlPurifier) {
00072             $message = 'purifier is no instance of class HtmlPurifier';
00073             throw new Robo47_Filter_Exception($message);
00074         }
00075         $this->_purifier = $purifier;
00076         return $this;
00077     }
00078 
00085     protected function _purifierFromRegistry($key)
00086     {
00087         if (Zend_Registry::isRegistered($key)) {
00088             return Zend_Registry::get($key);
00089         } else {
00090             $message = 'Registry key "' . $key .
00091                 '" for HtmlPurifier is not registered.';
00092             throw new Robo47_Filter_Exception($message);
00093         }
00094     }
00095 
00101     public function getPurifier()
00102     {
00103         return $this->_purifier;
00104     }
00105 
00111     public static function setDefaultPurifier(HtmlPurifier $purifier = null)
00112     {
00113         self::$_defaultPurifier = $purifier;
00114     }
00115 
00121     public static function getDefaultPurifier()
00122     {
00123         return self::$_defaultPurifier;
00124     }
00125 
00133     public function filter($value)
00134     {
00135         return $this->_purifier->purify($value);
00136     }
00137 }