Skip to content

Instantly share code, notes, and snippets.

@empirefx
Created September 16, 2014 03:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save empirefx/c4decc446f654656192b to your computer and use it in GitHub Desktop.
Save empirefx/c4decc446f654656192b to your computer and use it in GitHub Desktop.
HTMLpurifier
<?php if (! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Codeigniter HTMLPurifier Helper
*
* Purify input using the HTMLPurifier standalone class.
* Easily use multiple purifier configurations.
*
* @author Tyler Brownell <tyler@bluefoxstudio.ca>
* @copyright Public Domain
* @license http://bluefoxstudio.ca/release.html
*
* @access public
* @param string or array
* @param string
* @return string or array
*/
if (! function_exists('html_purify'))
{
function html_purify($dirty_html, $config = FALSE)
{
require_once APPPATH . 'third_party/htmlpurifier-4.5.0-standalone/HTMLPurifier.standalone.php';
if (is_array($dirty_html))
{
foreach ($dirty_html as $key => $val)
{
$clean_html[$key] = html_purify($val, $config);
}
}
else
{
switch ($config)
{
case 'comment':
$config = HTMLPurifier_Config::createDefault();
$config->set('HTML.Allowed', 'p,a[href],abbr[title],b,blockquote[cite],i,h1,h2,h3,h4,h5,h6,cite,br,del,hr,ol,ul,li,strong,em');
break;
case 'shout':
$config = HTMLPurifier_Config::createDefault();
$config->set('HTML.Allowed', 'p,b,i,del,strong,em');
break;
case 'mod':
$config = HTMLPurifier_Config::createDefault();
$config->set('HTML.Allowed', 'p,a[href],abbr[title],b,blockquote[cite],i,h1,h2,h3,h4,h5,h6,cite,br,del,hr,ol,li,code,strong,em');
break;
case 'cleanup':
$config = HTMLPurifier_Config::createDefault();
break;
case FALSE:
$config = HTMLPurifier_Config::createDefault();
$config->set('Core.Encoding', 'utf-8');
$config->set('HTML.Doctype', 'XHTML 1.0 Strict');
break;
default:
show_error('The HTMLPurifier configuration labeled "' . htmlentities($config, ENT_QUOTES, 'UTF-8') . '" could not be found.');
}
$purifier = new HTMLPurifier($config);
$clean_html = $purifier->purify($dirty_html);
}
return $clean_html;
}
}
/* End of htmlpurifier_helper.php */
/* Location: ./application/helpers/htmlpurifier_helper.php */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment