Skip to content

Instantly share code, notes, and snippets.

@uhnomoli
Created April 6, 2010 22:13
Show Gist options
  • Save uhnomoli/358187 to your computer and use it in GitHub Desktop.
Save uhnomoli/358187 to your computer and use it in GitHub Desktop.
<?php
/**
* purifierConfig is an HTMLPurifier config handler.
*
* It manages HTMLPurifier configs.
*
*
* Accepts an array of configs at initialization resembling something like:
*
* $configs = array(
* 'HTML.Doctype' => 'HTML 4.01 Strict',
*
* 'posts' => array(
* 'HTML.Allowed' => 'p,a[href],img[src]'
* ),
* 'comments' => array(
* 'HTML.Allowed' => 'p'
* ),
*
* 'default' => array(
* 'HTML.Allowed' => ''
* )
* );
*
* Directives put directly into the array are global and will be applied to all configs.
* If the same directive is set inside a named config, the value in the named config will be used.
*
* Configs can easily be grabbed like so:
*
* $purifierConfig = new purifierConfig($configs);
* $commentsConfig = $purifierConfig['comments'];
*
* or verbosely:
*
* $commentsConfig = $purifierConfig->getConfig('comments');
*
* If the requested config does not exist or no config is specified in the call,
* the default config will be returned.
*
*
* @package purifierConfig
* @author Andrew Fricke <Zonezero5@gmail.com>
* @version v0.1
*/
class purifierConfig implements ArrayAccess {
/**
* @var array An array of global HTMLPurifier directives.
* @var array An array of HTMLPurifier configs.
*
* @var array An array of HTMLPurifier_Config objects.
*/
protected $_globals = array();
protected $_configs = array();
protected static $_objects = array();
/**
* Constructor.
*
* @param array An array of HTMLPurifier configs.
*
* @throws InvalidArgumentException A default config must be defined.
*/
public function __construct(array $configs) {
if (!array_key_exists('default', $configs)) {
throw new InvalidArgumentException('You must define a default config.');
}
foreach ($configs as $name => $config) {
if (is_array($config)) {
$this->_configs[$name] = $config;
} else {
$this->_globals[$name] = $config;
}
}
}
/**
* Returns an HTMLPurifier_Config object.
*
* @param string Config name.
*
* @return object An HTMLPurifier_Config object.
*/
public function getConfig($name = NULL) {
if (isset(self::$_objects[$name])) {
return self::$_objects[$name];
}
if (isset($this->_configs[$name])) {
self::$_objects[$name] = HTMLPurifier_Config::createDefault();
self::$_objects[$name]->loadArray(
array_merge($this->_globals, $this->_configs[$name]);
);
return self::$_objects[$name];
}
if (isset(self::$_objects['default'])) {
return self::$_objects['default'];
}
self::$_objects['default'] = HTMLPurifier_Config::createDefault();
self::$_objects['default']->loadArray(
array_merge($this->_globals, $this->_configs['default']);
);
return self::$_objects['default'];
}
/**
* Checks if a given config object exists. (ArrayAccess)
*
* @param string Config name.
*
* @return bool TRUE if the config object exists, FALSE otherwise.
*/
public function offsetExists($name) {
return isset(self::$_objects[$name]);
}
/**
* Sets a config. (ArrayAccess)
*
* @param string Config name.
* @param array Config array.
*
* @throws LogicException If a config is attempted to be set.
*/
public function offsetSet($name, $value) {
throw new LogicException('You can not set a config after initialization.');
}
/**
* Retrieves a given config object. (ArrayAccess)
*
* @param string Config name.
*
* @return object An HTMLPurifier_Config object.
*/
public function offsetGet($name) {
return $this->getConfig($name);
}
/**
* Unsets a given config. (ArrayAccess)
*
* @param string Config name.
*
* @throws LogicException If a config is attempted to be unset.
*/
public function offsetUnset($name) {
throw new LogicException('You can not unset a config.');
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment