Efficient way to handle multiple HTMLPurifier configs.

Posted by anomareh on Stack Overflow See other posts from Stack Overflow or by anomareh
Published on 2010-04-06T01:12:32Z Indexed on 2010/04/06 1:53 UTC
Read the original article Hit count: 397

Filed under:
|
|

I'm using HTMLPurifier in a current project and I'm not sure about the most efficient way to go about handling multiple configs. For the most part the only major thing changing is the allowed tags.

Currently I have a private method, in each class using HTMLPurifier, that gets called when a config is needed and it creates one from the default. I'm not really happy with this as if 2 classes are using the same config, that's duplicating code, and what if a class needs 2 configs? It just feels messy.

The one upside to it, is it's lazy in the sense that it's not creating anything until it's needed. The various classes could be used and never have to purify anything. So I don't want to be creating a bunch of config objects that might not even be used.

I just recently found out that you can create a new instance of HTMLPurifier and directly set config options like so:

$purifier = new HTMLPurifier();
$purifier->config->set('HTML.Allowed', '');

I'm not sure if that's bad form at all or not, and if it isn't I'm not really sure of a good way to put it to use.

My latest idea was to create a config handler class that would just return an HTMLPurifier config for use in subsequent purify calls. At some point it could probably be expanded to allow the setting of configs, but just from the start I figured they'd just be hardcoded and run a method parameter through a switch to grab the requested config. Perhaps I could just send the stored purifier instance as an argument and have the method directly set the config on it like shown above?

This to me seems the best of the few ways I thought of, though I'm not sure if such a task warrants me creating a class to handle it, and if so, if I'm handling it in the best way.

I'm not too familiar with the inner workings of HTMLPurifier so I'm not sure if there are any better mechanisms in place for handling multiple configs.

Thanks for any insight anyone can offer.

© Stack Overflow or respective owner

Related posts about php

Related posts about htmlpurifier