I am trying to get a working plugin that utilizes HTMLpurifier (
http://htmlpurifier.org/)
I started with the plugin found on their homepage but it seems to have some problems.
http://htmlpurifier.org/svnroot/htmlpurifier/trunk/plugins/modx.txt
I was able to modify it and get it working but now there are some weird things happening with the content when it gets purified.
The plugin is as follows:
//<?
// HTML Purifier plugin
//
// Config variables
// &encoding=Encoding;string;UTF-8 &doctype=Doctype;list;HTML 4.01 Transitional,HTML 4.01 Strict,XHTML 1.0 Transitional,XHTML 1.0 Strict,XHTML 1.1;XHTML 1.0 Transitional &autoparagraph=AutoParagraph;list;true,false;false &linkify=Linkify;list;true,false;false
//
$e = &$modx->Event;
global $content;
$encoding = isset($encoding)? $encoding: 'UTF-8';
$doctype = isset($doctype)? $doctype: 'HTML 4.01 Transitional';
$autoparagraph = isset($autoparagraph)? $autoparagraph: 'false';
$linkify = isset($linkify)? $linkify: 'false';
if ($e->name == 'OnBeforeDocFormSave')
{
include_once '../assets/plugins/htmlpurifier/library/HTMLPurifier.auto.php';
// Configuration
$config = HTMLPurifier_Config::createDefault();
$config->set('Core', 'Encoding', $encoding); // Encoding
$config->set('HTML', 'Doctype', $doctype); // Document Type
$config->set('AutoFormat', 'AutoParagraph', $autoparagraph); // Auto Paragraph
$config->set('AutoFormat', 'Linkify', $linkify); // Linkify
$purifier = new HTMLPurifier($config);
// Magic Quotes Hack
// this is an ugly hack because this hook hasn't had
// the backslashes removed yet when magic_quotes_gpc is on,
// but HTMLPurifier must not have the quotes slashed.
static $magic_quotes = null;
if ($magic_quotes === null)
{
$magic_quotes = get_magic_quotes_gpc();
}
if ($magic_quotes) $content = stripslashes($content);
$content = $purifier->purify($content); //
if ($magic_quotes) $content = addslashes($content);
}
//?>
Now what this plugin is having problems with has to do with line breaks. it seems that there are rn added before and after content which are line breaks. If I remove the magic quotes hack it seems to solve the problem but I want this plugin to be compatible with all installations so if someone has or doesn’t have magic quotes on I want it to function. If I remove the hack I believe it will break the plugin on certain servers. (
http://htmlpurifier.org/phorum/read.php?3,1866#msg-1878)
Next problem is if you have linkify set to true, it will convert the link into an <a> tag and the link works. If you edit the same content again and click save in the manager and look at the content it will be broken, it changes all links to the manager’s domain (
http://domain.com/\) and thus breaks all of the working links.
Both of these problems seem to happen becuase of how the manager handles the data, if I plug in the same content into the demo on their site it purifies the content just fine.
At this point I am kinda at a loss. Any help or guidance would be a appreciated. Once I get this plugin working properly I would like to post it to the repository.
I would also like this plugin to purify all RTE fields including template variables and I am not sure how I would go about doing that.
A more detailed description and my conversation over at
http://htmlpurifier.org/ can be found at:
http://htmlpurifier.org/phorum/read.php?3,1866
Thanks,
Brian