4 * Configuration definition, defines directives and their defaults.
6 class HTMLPurifier_ConfigSchema {
9 * Defaults of the directives and namespaces.
10 * @note This shares the exact same structure as HTMLPurifier_Config::$conf
12 public $defaults = array();
15 * The default property list. Do not edit this property list.
20 * Definition of the directives. The structure of this is:
23 * 'Namespace' => array(
24 * 'Directive' => new stdclass(),
28 * The stdclass may have the following properties:
30 * - If isAlias isn't set:
31 * - type: Integer type of directive, see HTMLPurifier_VarParser for definitions
32 * - allow_null: If set, this directive allows null values
33 * - aliases: If set, an associative array of value aliases to real values
34 * - allowed: If set, a lookup array of allowed (string) values
35 * - If isAlias is set:
36 * - namespace: Namespace this directive aliases to
37 * - name: Directive name this directive aliases to
39 * In certain degenerate cases, stdclass will actually be an integer. In
40 * that case, the value is equivalent to an stdclass with the type
41 * property set to the integer. If the integer is negative, type is
42 * equal to the absolute value of integer, and allow_null is true.
44 * This class is friendly with HTMLPurifier_Config. If you need introspection
45 * about the schema, you're better of using the ConfigSchema_Interchange,
46 * which uses more memory but has much richer information.
48 public $info = array();
51 * Application-wide singleton
53 static protected $singleton;
55 public function __construct() {
56 $this->defaultPlist = new HTMLPurifier_PropertyList();
60 * Unserializes the default ConfigSchema.
62 public static function makeFromSerial() {
63 return unserialize(file_get_contents(HTMLPURIFIER_PREFIX . '/HTMLPurifier/ConfigSchema/schema.ser'));
67 * Retrieves an instance of the application-wide configuration definition.
69 public static function instance($prototype = null) {
70 if ($prototype !== null) {
71 HTMLPurifier_ConfigSchema::$singleton = $prototype;
72 } elseif (HTMLPurifier_ConfigSchema::$singleton === null || $prototype === true) {
73 HTMLPurifier_ConfigSchema::$singleton = HTMLPurifier_ConfigSchema::makeFromSerial();
75 return HTMLPurifier_ConfigSchema::$singleton;
79 * Defines a directive for configuration
80 * @warning Will fail of directive's namespace is defined.
81 * @warning This method's signature is slightly different from the legacy
82 * define() static method! Beware!
83 * @param $namespace Namespace the directive is in
84 * @param $name Key of directive
85 * @param $default Default value of directive
86 * @param $type Allowed type of the directive. See
87 * HTMLPurifier_DirectiveDef::$type for allowed values
88 * @param $allow_null Whether or not to allow null values
90 public function add($key, $default, $type, $allow_null) {
91 $obj = new stdclass();
92 $obj->type = is_int($type) ? $type : HTMLPurifier_VarParser::$types[$type];
93 if ($allow_null) $obj->allow_null = true;
94 $this->info[$key] = $obj;
95 $this->defaults[$key] = $default;
96 $this->defaultPlist->set($key, $default);
100 * Defines a directive value alias.
102 * Directive value aliases are convenient for developers because it lets
103 * them set a directive to several values and get the same result.
104 * @param $namespace Directive's namespace
105 * @param $name Name of Directive
106 * @param $aliases Hash of aliased values to the real alias
108 public function addValueAliases($key, $aliases) {
109 if (!isset($this->info[$key]->aliases)) {
110 $this->info[$key]->aliases = array();
112 foreach ($aliases as $alias => $real) {
113 $this->info[$key]->aliases[$alias] = $real;
118 * Defines a set of allowed values for a directive.
119 * @warning This is slightly different from the corresponding static
121 * @param $namespace Namespace of directive
122 * @param $name Name of directive
123 * @param $allowed Lookup array of allowed values
125 public function addAllowedValues($key, $allowed) {
126 $this->info[$key]->allowed = $allowed;
130 * Defines a directive alias for backwards compatibility
132 * @param $name Directive that will be aliased
133 * @param $new_namespace
134 * @param $new_name Directive that the alias will be to
136 public function addAlias($key, $new_key) {
138 $obj->key = $new_key;
139 $obj->isAlias = true;
140 $this->info[$key] = $obj;
144 * Replaces any stdclass that only has the type property with type integer.
146 public function postProcess() {
147 foreach ($this->info as $key => $v) {
148 if (count((array) $v) == 1) {
149 $this->info[$key] = $v->type;
150 } elseif (count((array) $v) == 2 && isset($v->allow_null)) {
151 $this->info[$key] = -$v->type;
158 // vim: et sw=4 sts=4