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