/** * Removes broken HTML and XSS from text using [HTMLPurifier](http://htmlpurifier.org/). * * $text = Purifier::clean($dirty_html); * * The original content is returned with all broken HTML and XSS removed. * * @param mixed text to clean, or an array to clean recursively * @param mixed optional set of configuration options, as an array or a string denoting a set of options in the config file * @return mixed */ public static function clean($dirty, $config = null) { if (is_array($dirty)) { foreach ($dirty as $key => $value) { // Recursively clean arrays $clean[$key] = Purifier::clean($value, $config); } } else { // Load HTML Purifier $purifier = Purifier::instance(); // Clean the HTML and return it if (is_array($config)) { $c = HTMLPurifier_Config::inherit(Purifier::$configs['default']); $c->loadArray($config); $clean = $purifier->purify($dirty, $c); } else { if (is_string($config)) { if (isset(Purifier::$configs[$config])) { $c = Purifier::$configs[$config]; } else { $c = HTMLPurifier_Config::inherit(Purifier::$configs['default']); $c->loadArray(Config::get('purifier::config.settings.' . $config)); Purifier::$configs[$config] = $c; } $clean = $purifier->purify($dirty, $c); } else { $clean = $purifier->purify($dirty, Purifier::$configs['default']); } } } return $clean; }
/** * Returns the singleton instance of HTML Purifier. If no instance has * been created, a new instance will be created. * * $purifier = Purifier::instance(); * * @return \Mews\Purifier\HTMLPurifier * @static */ public static function instance() { return \Mews\Purifier\Purifier::instance(); }