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