views:

33

answers:

1

I am trying to pass some XML tags (abcdef>) through htmlpurifier. Since the tags itself are not supported, I am trying to add an element first and then adding it to allowedElements. However this is not working, i'm just getting a blank page. Any ideas please on what I am doing wrong, or if there is an easier way to achieve what i am looking for.

$config = HTMLPurifier_Config::createDefault();
$config->set('Core', 'Encoding', "UTF-8");
$config->set('HTML', 'DefinitionID', 'pinaki-test');
$config->set('HTML', 'DefinitionRev', 3);
$config->set('Cache', 'DefinitionImpl', null); // remove this later!
$config->set('Cache', 'SerializerPath', "/var/cache/htmlpurify");
$def = $config->getHTMLDefinition(true);
$def->addElement("tag1", false, 'Empty', 'Common', array());
$def->addElement("tag2", false, 'Empty', 'Common', array());
$config->set('HTML', 'AllowedElements', array("tag1", "tag2"));

Let know if anyone needs any other details.

Note: The library is working fine without adding the elements.

+1  A: 

You should turn on error reporting; makes dev a lot easier!

ini_set('display_errors', true);
error_reporting(E_ALL & ~E_NOTICE); // or E_ALL if you're feeling good

Fixing a bunch of errors (the "cannot edit configuration after finalization means all your configs need to be before you getHTMLDefinition; deprecated API means that you should change your config set format but is harmless), then you get a blank string. Then you need to make sure your new elements are in the allowed elements of someone else, an easy way to do this is mark them Inline. I doubt the AllowedElements attribute is what you want, because it will exclude all other elements...

<?php
require_once 'library/HTMLPurifier.auto.php';
$config = HTMLPurifier_Config::createDefault();
$config->set('Core.Encoding', "UTF-8");
$config->set('HTML.DefinitionID', 'pinaki-test');
$config->set('HTML.DefinitionRev', 3);
$config->set('Cache.DefinitionImpl', null); // remove this later!
$config->set('Cache.SerializerPath', "/var/cache/htmlpurify");
$config->set('HTML.AllowedElements', array("tag1", "tag2"));
$def = $config->getHTMLDefinition(true);
$def->addElement("tag1", 'Inline', 'Empty', 'Common', array());
$def->addElement("tag2", 'Inline', 'Empty', 'Common', array());
$purifier = new HTMLPurifier($config);
echo $purifier->purify('<tag1>asf');
Edward Z. Yang
I do have errors turned on. Can you please explain what you mean by "it will exclude all other elements."... i ask this since i used the $config->set('HTML.AllowedElements', array("tag1", "tag2")) without addElements() and it didnt seem to ignore other tags. The documentation also says that it is subtractive, i.e will only act as a whitelist. Am i missing something?
pinaki
I disbelieve that you have errors turned on: if they were, you wouldn't be getting a blank page. Remember that display_errors AND error_reporting both control errors.The docs are a bit confusing in that respect. What it means by subtractive is that HTML Purifier has a built in set of allowed elements, and you can't magically add support for other elements by adding it to the allowed elements. However, AllowedElements itself acts like a whitelist, so anything not on a list you give it will be excluded.
Edward Z. Yang
what would you suggest then as an alternate method? thanks for the detailed explanation.
pinaki
Adding the tag to whitelist is working fine for me (as mentioned in the documentation); so i am marking this as the correct answer. Do let me know if you see any issues with this approach. Thanks for the help.
pinaki
Should be fine.
Edward Z. Yang