1 : <?php
2 :
3 : /**
4 : * Configuration definition, defines directives and their defaults.
5 : */
6 1 : 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 : * Definition of the directives. The structure of this is:
16 : *
17 : * array(
18 : * 'Namespace' => array(
19 : * 'Directive' => new stdclass(),
20 : * )
21 : * )
22 : *
23 : * The stdclass may have the following properties:
24 : *
25 : * - If isAlias isn't set:
26 : * - type: Integer type of directive, see HTMLPurifier_VarParser for definitions
27 : * - allow_null: If set, this directive allows null values
28 : * - aliases: If set, an associative array of value aliases to real values
29 : * - allowed: If set, a lookup array of allowed (string) values
30 : * - If isAlias is set:
31 : * - namespace: Namespace this directive aliases to
32 : * - name: Directive name this directive aliases to
33 : *
34 : * In certain degenerate cases, stdclass will actually be an integer. In
35 : * that case, the value is equivalent to an stdclass with the type
36 : * property set to the integer. If the integer is negative, type is
37 : * equal to the absolute value of integer, and allow_null is true.
38 : *
39 : * This class is friendly with HTMLPurifier_Config. If you need introspection
40 : * about the schema, you're better of using the ConfigSchema_Interchange,
41 : * which uses more memory but has much richer information.
42 : */
43 : public $info = array();
44 :
45 : /**
46 : * Application-wide singleton
47 : */
48 : static protected $singleton;
49 :
50 : /**
51 : * Unserializes the default ConfigSchema.
52 : */
53 : public static function makeFromSerial() {
54 1 : return unserialize(file_get_contents(HTMLPURIFIER_PREFIX . '/HTMLPurifier/ConfigSchema/schema.ser'));
55 : }
56 :
57 : /**
58 : * Retrieves an instance of the application-wide configuration definition.
59 : */
60 : public static function instance($prototype = null) {
61 2 : if ($prototype !== null) {
62 0 : HTMLPurifier_ConfigSchema::$singleton = $prototype;
63 2 : } elseif (HTMLPurifier_ConfigSchema::$singleton === null || $prototype === true) {
64 1 : HTMLPurifier_ConfigSchema::$singleton = HTMLPurifier_ConfigSchema::makeFromSerial();
65 1 : }
66 2 : return HTMLPurifier_ConfigSchema::$singleton;
67 : }
68 :
69 : /**
70 : * Defines a directive for configuration
71 : * @warning Will fail of directive's namespace is defined.
72 : * @warning This method's signature is slightly different from the legacy
73 : * define() static method! Beware!
74 : * @param $namespace Namespace the directive is in
75 : * @param $name Key of directive
76 : * @param $default Default value of directive
77 : * @param $type Allowed type of the directive. See
78 : * HTMLPurifier_DirectiveDef::$type for allowed values
79 : * @param $allow_null Whether or not to allow null values
80 : */
81 : public function add($namespace, $name, $default, $type, $allow_null) {
82 0 : $obj = new stdclass();
83 0 : $obj->type = is_int($type) ? $type : HTMLPurifier_VarParser::$types[$type];
84 0 : if ($allow_null) $obj->allow_null = true;
85 0 : $this->info[$namespace][$name] = $obj;
86 0 : $this->defaults[$namespace][$name] = $default;
87 0 : }
88 :
89 : /**
90 : * Defines a namespace for directives to be put into.
91 : * @warning This is slightly different from the corresponding static
92 : * method.
93 : * @param $namespace Namespace's name
94 : */
95 : public function addNamespace($namespace) {
96 0 : $this->info[$namespace] = array();
97 0 : $this->defaults[$namespace] = array();
98 0 : }
99 :
100 : /**
101 : * Defines a directive value alias.
102 : *
103 : * Directive value aliases are convenient for developers because it lets
104 : * them set a directive to several values and get the same result.
105 : * @param $namespace Directive's namespace
106 : * @param $name Name of Directive
107 : * @param $aliases Hash of aliased values to the real alias
108 : */
109 : public function addValueAliases($namespace, $name, $aliases) {
110 0 : if (!isset($this->info[$namespace][$name]->aliases)) {
111 0 : $this->info[$namespace][$name]->aliases = array();
112 0 : }
113 0 : foreach ($aliases as $alias => $real) {
114 0 : $this->info[$namespace][$name]->aliases[$alias] = $real;
115 0 : }
116 0 : }
117 :
118 : /**
119 : * Defines a set of allowed values for a directive.
120 : * @warning This is slightly different from the corresponding static
121 : * method definition.
122 : * @param $namespace Namespace of directive
123 : * @param $name Name of directive
124 : * @param $allowed Lookup array of allowed values
125 : */
126 : public function addAllowedValues($namespace, $name, $allowed) {
127 0 : $this->info[$namespace][$name]->allowed = $allowed;
128 0 : }
129 :
130 : /**
131 : * Defines a directive alias for backwards compatibility
132 : * @param $namespace
133 : * @param $name Directive that will be aliased
134 : * @param $new_namespace
135 : * @param $new_name Directive that the alias will be to
136 : */
137 : public function addAlias($namespace, $name, $new_namespace, $new_name) {
138 0 : $obj = new stdclass;
139 0 : $obj->namespace = $new_namespace;
140 0 : $obj->name = $new_name;
141 0 : $obj->isAlias = true;
142 0 : $this->info[$namespace][$name] = $obj;
143 0 : }
144 :
145 : /**
146 : * Replaces any stdclass that only has the type property with type integer.
147 : */
148 : public function postProcess() {
149 0 : foreach ($this->info as $namespace => $info) {
150 0 : foreach ($info as $directive => $v) {
151 0 : if (count((array) $v) == 1) {
152 0 : $this->info[$namespace][$directive] = $v->type;
153 0 : } elseif (count((array) $v) == 2 && isset($v->allow_null)) {
154 0 : $this->info[$namespace][$directive] = -$v->type;
155 0 : }
156 0 : }
157 0 : }
158 0 : }
159 :
160 : // DEPRECATED METHODS
161 :
162 : /** @see HTMLPurifier_ConfigSchema->set() */
163 : public static function define($namespace, $name, $default, $type, $description) {
164 0 : HTMLPurifier_ConfigSchema::deprecated(__METHOD__);
165 0 : $type_values = explode('/', $type, 2);
166 0 : $type = $type_values[0];
167 0 : $modifier = isset($type_values[1]) ? $type_values[1] : false;
168 0 : $allow_null = ($modifier === 'null');
169 0 : $def = HTMLPurifier_ConfigSchema::instance();
170 0 : $def->add($namespace, $name, $default, $type, $allow_null);
171 0 : }
172 :
173 : /** @see HTMLPurifier_ConfigSchema->addNamespace() */
174 : public static function defineNamespace($namespace, $description) {
175 0 : HTMLPurifier_ConfigSchema::deprecated(__METHOD__);
176 0 : $def = HTMLPurifier_ConfigSchema::instance();
177 0 : $def->addNamespace($namespace);
178 0 : }
179 :
180 : /** @see HTMLPurifier_ConfigSchema->addValueAliases() */
181 : public static function defineValueAliases($namespace, $name, $aliases) {
182 0 : HTMLPurifier_ConfigSchema::deprecated(__METHOD__);
183 0 : $def = HTMLPurifier_ConfigSchema::instance();
184 0 : $def->addValueAliases($namespace, $name, $aliases);
185 0 : }
186 :
187 : /** @see HTMLPurifier_ConfigSchema->addAllowedValues() */
188 : public static function defineAllowedValues($namespace, $name, $allowed_values) {
189 0 : HTMLPurifier_ConfigSchema::deprecated(__METHOD__);
190 0 : $allowed = array();
191 0 : foreach ($allowed_values as $value) {
192 0 : $allowed[$value] = true;
193 0 : }
194 0 : $def = HTMLPurifier_ConfigSchema::instance();
195 0 : $def->addAllowedValues($namespace, $name, $allowed);
196 0 : }
197 :
198 : /** @see HTMLPurifier_ConfigSchema->addAlias() */
199 : public static function defineAlias($namespace, $name, $new_namespace, $new_name) {
200 0 : HTMLPurifier_ConfigSchema::deprecated(__METHOD__);
201 0 : $def = HTMLPurifier_ConfigSchema::instance();
202 0 : $def->addAlias($namespace, $name, $new_namespace, $new_name);
203 0 : }
204 :
205 : /** @deprecated, use HTMLPurifier_VarParser->parse() */
206 : public function validate($a, $b, $c = false) {
207 0 : trigger_error("HTMLPurifier_ConfigSchema->validate deprecated, use HTMLPurifier_VarParser->parse instead", E_USER_NOTICE);
208 0 : $parser = new HTMLPurifier_VarParser();
209 0 : return $parser->parse($a, $b, $c);
210 : }
211 :
212 : /**
213 : * Throws an E_USER_NOTICE stating that a method is deprecated.
214 : */
215 : private static function deprecated($method) {
216 0 : trigger_error("Static HTMLPurifier_ConfigSchema::$method deprecated, use add*() method instead", E_USER_NOTICE);
217 0 : }
218 :
219 : }
220 :
221 :
|