2009-05-19 19:32:06 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Configuration definition, defines directives and their defaults.
|
|
|
|
*/
|
|
|
|
class HTMLPurifier_ConfigSchema {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Defaults of the directives and namespaces.
|
|
|
|
* @note This shares the exact same structure as HTMLPurifier_Config::$conf
|
|
|
|
*/
|
|
|
|
public $defaults = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The default property list. Do not edit this property list.
|
|
|
|
*/
|
|
|
|
public $defaultPlist;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Definition of the directives. The structure of this is:
|
|
|
|
*
|
|
|
|
* array(
|
|
|
|
* 'Namespace' => array(
|
|
|
|
* 'Directive' => new stdclass(),
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
*
|
|
|
|
* The stdclass may have the following properties:
|
|
|
|
*
|
|
|
|
* - If isAlias isn't set:
|
|
|
|
* - type: Integer type of directive, see HTMLPurifier_VarParser for definitions
|
|
|
|
* - allow_null: If set, this directive allows null values
|
|
|
|
* - aliases: If set, an associative array of value aliases to real values
|
|
|
|
* - allowed: If set, a lookup array of allowed (string) values
|
|
|
|
* - If isAlias is set:
|
|
|
|
* - namespace: Namespace this directive aliases to
|
|
|
|
* - name: Directive name this directive aliases to
|
|
|
|
*
|
|
|
|
* In certain degenerate cases, stdclass will actually be an integer. In
|
|
|
|
* that case, the value is equivalent to an stdclass with the type
|
|
|
|
* property set to the integer. If the integer is negative, type is
|
|
|
|
* equal to the absolute value of integer, and allow_null is true.
|
|
|
|
*
|
|
|
|
* This class is friendly with HTMLPurifier_Config. If you need introspection
|
|
|
|
* about the schema, you're better of using the ConfigSchema_Interchange,
|
|
|
|
* which uses more memory but has much richer information.
|
|
|
|
*/
|
|
|
|
public $info = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Application-wide singleton
|
|
|
|
*/
|
|
|
|
static protected $singleton;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->defaultPlist = new HTMLPurifier_PropertyList();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unserializes the default ConfigSchema.
|
|
|
|
*/
|
|
|
|
public static function makeFromSerial() {
|
2011-03-28 13:46:24 +02:00
|
|
|
$contents = file_get_contents(HTMLPURIFIER_PREFIX . '/HTMLPurifier/ConfigSchema/schema.ser');
|
|
|
|
$r = unserialize($contents);
|
|
|
|
if (!$r) {
|
|
|
|
$hash = sha1($contents);
|
|
|
|
trigger_error("Unserialization of configuration schema failed, sha1 of file was $hash", E_USER_ERROR);
|
|
|
|
}
|
|
|
|
return $r;
|
2009-05-19 19:32:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves an instance of the application-wide configuration definition.
|
|
|
|
*/
|
|
|
|
public static function instance($prototype = null) {
|
|
|
|
if ($prototype !== null) {
|
|
|
|
HTMLPurifier_ConfigSchema::$singleton = $prototype;
|
|
|
|
} elseif (HTMLPurifier_ConfigSchema::$singleton === null || $prototype === true) {
|
|
|
|
HTMLPurifier_ConfigSchema::$singleton = HTMLPurifier_ConfigSchema::makeFromSerial();
|
|
|
|
}
|
|
|
|
return HTMLPurifier_ConfigSchema::$singleton;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Defines a directive for configuration
|
|
|
|
* @warning Will fail of directive's namespace is defined.
|
|
|
|
* @warning This method's signature is slightly different from the legacy
|
|
|
|
* define() static method! Beware!
|
|
|
|
* @param $namespace Namespace the directive is in
|
|
|
|
* @param $name Key of directive
|
|
|
|
* @param $default Default value of directive
|
|
|
|
* @param $type Allowed type of the directive. See
|
|
|
|
* HTMLPurifier_DirectiveDef::$type for allowed values
|
|
|
|
* @param $allow_null Whether or not to allow null values
|
|
|
|
*/
|
2009-11-27 10:37:41 +01:00
|
|
|
public function add($key, $default, $type, $allow_null) {
|
2009-05-19 19:32:06 +02:00
|
|
|
$obj = new stdclass();
|
|
|
|
$obj->type = is_int($type) ? $type : HTMLPurifier_VarParser::$types[$type];
|
|
|
|
if ($allow_null) $obj->allow_null = true;
|
2009-11-27 10:37:41 +01:00
|
|
|
$this->info[$key] = $obj;
|
|
|
|
$this->defaults[$key] = $default;
|
|
|
|
$this->defaultPlist->set($key, $default);
|
2009-05-19 19:32:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Defines a directive value alias.
|
|
|
|
*
|
|
|
|
* Directive value aliases are convenient for developers because it lets
|
|
|
|
* them set a directive to several values and get the same result.
|
|
|
|
* @param $namespace Directive's namespace
|
|
|
|
* @param $name Name of Directive
|
|
|
|
* @param $aliases Hash of aliased values to the real alias
|
|
|
|
*/
|
2009-11-27 10:37:41 +01:00
|
|
|
public function addValueAliases($key, $aliases) {
|
|
|
|
if (!isset($this->info[$key]->aliases)) {
|
|
|
|
$this->info[$key]->aliases = array();
|
2009-05-19 19:32:06 +02:00
|
|
|
}
|
|
|
|
foreach ($aliases as $alias => $real) {
|
2009-11-27 10:37:41 +01:00
|
|
|
$this->info[$key]->aliases[$alias] = $real;
|
2009-05-19 19:32:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Defines a set of allowed values for a directive.
|
|
|
|
* @warning This is slightly different from the corresponding static
|
|
|
|
* method definition.
|
|
|
|
* @param $namespace Namespace of directive
|
|
|
|
* @param $name Name of directive
|
|
|
|
* @param $allowed Lookup array of allowed values
|
|
|
|
*/
|
2009-11-27 10:37:41 +01:00
|
|
|
public function addAllowedValues($key, $allowed) {
|
|
|
|
$this->info[$key]->allowed = $allowed;
|
2009-05-19 19:32:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Defines a directive alias for backwards compatibility
|
|
|
|
* @param $namespace
|
|
|
|
* @param $name Directive that will be aliased
|
|
|
|
* @param $new_namespace
|
|
|
|
* @param $new_name Directive that the alias will be to
|
|
|
|
*/
|
2009-11-27 10:37:41 +01:00
|
|
|
public function addAlias($key, $new_key) {
|
2009-05-19 19:32:06 +02:00
|
|
|
$obj = new stdclass;
|
2009-11-27 10:37:41 +01:00
|
|
|
$obj->key = $new_key;
|
2009-05-19 19:32:06 +02:00
|
|
|
$obj->isAlias = true;
|
2009-11-27 10:37:41 +01:00
|
|
|
$this->info[$key] = $obj;
|
2009-05-19 19:32:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replaces any stdclass that only has the type property with type integer.
|
|
|
|
*/
|
|
|
|
public function postProcess() {
|
2009-11-27 10:37:41 +01:00
|
|
|
foreach ($this->info as $key => $v) {
|
|
|
|
if (count((array) $v) == 1) {
|
|
|
|
$this->info[$key] = $v->type;
|
|
|
|
} elseif (count((array) $v) == 2 && isset($v->allow_null)) {
|
|
|
|
$this->info[$key] = -$v->type;
|
2009-05-19 19:32:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// vim: et sw=4 sts=4
|