]>
Commit | Line | Data |
---|---|---|
010efc9b AD |
1 | <?php |
2 | ||
3 | /** | |
4 | * Configuration definition, defines directives and their defaults. | |
5 | */ | |
6 | class HTMLPurifier_ConfigSchema { | |
7 | ||
8 | /** | |
9 | * Defaults of the directives and namespaces. | |
10 | * @note This shares the exact same structure as HTMLPurifier_Config::$conf | |
11 | */ | |
12 | public $defaults = array(); | |
13 | ||
14 | /** | |
15 | * The default property list. Do not edit this property list. | |
16 | */ | |
17 | public $defaultPlist; | |
18 | ||
19 | /** | |
20 | * Definition of the directives. The structure of this is: | |
21 | * | |
22 | * array( | |
23 | * 'Namespace' => array( | |
24 | * 'Directive' => new stdclass(), | |
25 | * ) | |
26 | * ) | |
27 | * | |
28 | * The stdclass may have the following properties: | |
29 | * | |
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 | |
38 | * | |
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. | |
43 | * | |
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. | |
47 | */ | |
48 | public $info = array(); | |
49 | ||
50 | /** | |
51 | * Application-wide singleton | |
52 | */ | |
53 | static protected $singleton; | |
54 | ||
55 | public function __construct() { | |
56 | $this->defaultPlist = new HTMLPurifier_PropertyList(); | |
57 | } | |
58 | ||
59 | /** | |
60 | * Unserializes the default ConfigSchema. | |
61 | */ | |
62 | public static function makeFromSerial() { | |
63 | $contents = file_get_contents(HTMLPURIFIER_PREFIX . '/HTMLPurifier/ConfigSchema/schema.ser'); | |
64 | $r = unserialize($contents); | |
65 | if (!$r) { | |
66 | $hash = sha1($contents); | |
67 | trigger_error("Unserialization of configuration schema failed, sha1 of file was $hash", E_USER_ERROR); | |
68 | } | |
69 | return $r; | |
70 | } | |
71 | ||
72 | /** | |
73 | * Retrieves an instance of the application-wide configuration definition. | |
74 | */ | |
75 | public static function instance($prototype = null) { | |
76 | if ($prototype !== null) { | |
77 | HTMLPurifier_ConfigSchema::$singleton = $prototype; | |
78 | } elseif (HTMLPurifier_ConfigSchema::$singleton === null || $prototype === true) { | |
79 | HTMLPurifier_ConfigSchema::$singleton = HTMLPurifier_ConfigSchema::makeFromSerial(); | |
80 | } | |
81 | return HTMLPurifier_ConfigSchema::$singleton; | |
82 | } | |
83 | ||
84 | /** | |
85 | * Defines a directive for configuration | |
86 | * @warning Will fail of directive's namespace is defined. | |
87 | * @warning This method's signature is slightly different from the legacy | |
88 | * define() static method! Beware! | |
89 | * @param $namespace Namespace the directive is in | |
90 | * @param $name Key of directive | |
91 | * @param $default Default value of directive | |
92 | * @param $type Allowed type of the directive. See | |
93 | * HTMLPurifier_DirectiveDef::$type for allowed values | |
94 | * @param $allow_null Whether or not to allow null values | |
95 | */ | |
96 | public function add($key, $default, $type, $allow_null) { | |
97 | $obj = new stdclass(); | |
98 | $obj->type = is_int($type) ? $type : HTMLPurifier_VarParser::$types[$type]; | |
99 | if ($allow_null) $obj->allow_null = true; | |
100 | $this->info[$key] = $obj; | |
101 | $this->defaults[$key] = $default; | |
102 | $this->defaultPlist->set($key, $default); | |
103 | } | |
104 | ||
105 | /** | |
106 | * Defines a directive value alias. | |
107 | * | |
108 | * Directive value aliases are convenient for developers because it lets | |
109 | * them set a directive to several values and get the same result. | |
110 | * @param $namespace Directive's namespace | |
111 | * @param $name Name of Directive | |
112 | * @param $aliases Hash of aliased values to the real alias | |
113 | */ | |
114 | public function addValueAliases($key, $aliases) { | |
115 | if (!isset($this->info[$key]->aliases)) { | |
116 | $this->info[$key]->aliases = array(); | |
117 | } | |
118 | foreach ($aliases as $alias => $real) { | |
119 | $this->info[$key]->aliases[$alias] = $real; | |
120 | } | |
121 | } | |
122 | ||
123 | /** | |
124 | * Defines a set of allowed values for a directive. | |
125 | * @warning This is slightly different from the corresponding static | |
126 | * method definition. | |
127 | * @param $namespace Namespace of directive | |
128 | * @param $name Name of directive | |
129 | * @param $allowed Lookup array of allowed values | |
130 | */ | |
131 | public function addAllowedValues($key, $allowed) { | |
132 | $this->info[$key]->allowed = $allowed; | |
133 | } | |
134 | ||
135 | /** | |
136 | * Defines a directive alias for backwards compatibility | |
137 | * @param $namespace | |
138 | * @param $name Directive that will be aliased | |
139 | * @param $new_namespace | |
140 | * @param $new_name Directive that the alias will be to | |
141 | */ | |
142 | public function addAlias($key, $new_key) { | |
143 | $obj = new stdclass; | |
144 | $obj->key = $new_key; | |
145 | $obj->isAlias = true; | |
146 | $this->info[$key] = $obj; | |
147 | } | |
148 | ||
149 | /** | |
150 | * Replaces any stdclass that only has the type property with type integer. | |
151 | */ | |
152 | public function postProcess() { | |
153 | foreach ($this->info as $key => $v) { | |
154 | if (count((array) $v) == 1) { | |
155 | $this->info[$key] = $v->type; | |
156 | } elseif (count((array) $v) == 2 && isset($v->allow_null)) { | |
157 | $this->info[$key] = -$v->type; | |
158 | } | |
159 | } | |
160 | } | |
161 | ||
162 | } | |
163 | ||
164 | // vim: et sw=4 sts=4 |