Welcome! » Log In » Create A New Profile

HTML Purifer - Codeigniter Plugin

Posted by Chamyto 
HTML Purifer - Codeigniter Plugin
February 28, 2009 06:33AM

I was thinking that since the Codeigniter Plugin link on the home page doesn't work (Page not Found), I'd write one.

This plugin was created due to a need to use a wysiwyg and I didn’t trust it to validate.

First, you will need download the HTMLPurifier class. Extract and and rename the folder, ‘library’ which contain the main files to htmlpurifier.

Create a file. Name it htmlpurifier_pi.php and insert the code below.

function purify($dirty_html)
{
	if (is_array($dirty_html))
	{
		foreach ($dirty_html as $key => $val)
		{
			$dirty_html[$key] = purify($val);
		}

		return $dirty_html;
	}
        // to prevent further processing of 'nothing'...
	if (trim($dirty_html) === '')
	{
		return $dirty_html;
	}

	require_once(APPPATH."plugins/htmlpurifier/HTMLPurifier.auto.php"); 
	require_once(APPPATH."plugins/htmlpurifier/HTMLPurifier.func.php");

	$config = HTMLPurifier_Config::createDefault();

	$config->set('HTML', 'Doctype', 'XHTML 1.0 Strict');

	return HTMLPurifier($dirty_html, $config);

}

Save the file in your plugins folder located in the application folder ('application/plugins/'). If it doesn't exist. Create one. Save htmlpurifier_pi.php in that folder.

Now, to use the plugin, you will need to load it using the controller.

function save()
{
$this->load->plugin(’htmlpurifier’);
$clean_html = purify($this->input->post(’html_content’));
$this->content_model->save($clean_html);
}

Happy coding :P

From: HTMLPurifier Plugin for CodeIgniter by Thorpe Obazee

Edit: Added support for arrays.

Pinoy Tech: Web Development and CodeIgniter

Edited 4 time(s). Last edit at 02/28/2009 10:33PM by Chamyto.

Re: HTML Purifer - Codeigniter Plugin
February 28, 2009 01:09PM

Good catch. I probably should update the link.

HTML Purifier, Standards-Compliant HTML Filtering

ci newbie
Re: HTML Purifer - Codeigniter Plugin
October 09, 2009 06:17AM

In version 4.0 user:

$config->set('HTML.Doctype', 'XHTML 1.0 Strict');

in stead of:

$config->set('HTML', 'Doctype', 'XHTML 1.0 Strict');
Re: HTML Purifer - Codeigniter Plugin
November 12, 2009 07:16PM

@ ci newbie. Nice catch. I sure hope the link an updated tutorial is updated on the homepage though..

Pinoy Tech: Web Development and CodeIgniter

Author:
Your Email:

Subject:

HTML input is enabled. Make sure you escape all HTML and angled brackets with < and >.

Auto-paragraphing is enabled. Double newlines will be converted to paragraphs; for single newlines, use the pre tag.

Allowed tags: a, abbr, acronym, b, blockquote, caption, cite, code, dd, del, dfn, div, dl, dt, em, i, ins, kbd, li, ol, p, pre, s, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, var.

For inputting literal code such as HTML and PHP for display, use CDATA tags to auto-escape your angled brackets, and pre to preserve newlines:

<pre><![CDATA[
Place code here
]]></pre>

Power users, you can hide this notice with:

.htmlpurifier-help {display:none;}

Message: