| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 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 |
* 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 |
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 |
if ($prototype !== null) { |
|---|
| 62 |
HTMLPurifier_ConfigSchema::$singleton = $prototype; |
|---|
| 63 |
} elseif (HTMLPurifier_ConfigSchema::$singleton === null || $prototype === true) { |
|---|
| 64 |
HTMLPurifier_ConfigSchema::$singleton = HTMLPurifier_ConfigSchema::makeFromSerial(); |
|---|
| 65 |
} |
|---|
| 66 |
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 |
$obj = new stdclass(); |
|---|
| 83 |
$obj->type = is_int($type) ? $type : HTMLPurifier_VarParser::$types[$type]; |
|---|
| 84 |
if ($allow_null) $obj->allow_null = true; |
|---|
| 85 |
$this->info[$namespace][$name] = $obj; |
|---|
| 86 |
$this->defaults[$namespace][$name] = $default; |
|---|
| 87 |
} |
|---|
| 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 |
$this->info[$namespace] = array(); |
|---|
| 97 |
$this->defaults[$namespace] = array(); |
|---|
| 98 |
} |
|---|
| 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 |
if (!isset($this->info[$namespace][$name]->aliases)) { |
|---|
| 111 |
$this->info[$namespace][$name]->aliases = array(); |
|---|
| 112 |
} |
|---|
| 113 |
foreach ($aliases as $alias => $real) { |
|---|
| 114 |
$this->info[$namespace][$name]->aliases[$alias] = $real; |
|---|
| 115 |
} |
|---|
| 116 |
} |
|---|
| 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 |
$this->info[$namespace][$name]->allowed = $allowed; |
|---|
| 128 |
} |
|---|
| 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 |
$obj = new stdclass; |
|---|
| 139 |
$obj->namespace = $new_namespace; |
|---|
| 140 |
$obj->name = $new_name; |
|---|
| 141 |
$obj->isAlias = true; |
|---|
| 142 |
$this->info[$namespace][$name] = $obj; |
|---|
| 143 |
} |
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 |
* Replaces any stdclass that only has the type property with type integer. |
|---|
| 147 |
*/ |
|---|
| 148 |
public function postProcess() { |
|---|
| 149 |
foreach ($this->info as $namespace => $info) { |
|---|
| 150 |
foreach ($info as $directive => $v) { |
|---|
| 151 |
if (count((array) $v) == 1) { |
|---|
| 152 |
$this->info[$namespace][$directive] = $v->type; |
|---|
| 153 |
} elseif (count((array) $v) == 2 && isset($v->allow_null)) { |
|---|
| 154 |
$this->info[$namespace][$directive] = -$v->type; |
|---|
| 155 |
} |
|---|
| 156 |
} |
|---|
| 157 |
} |
|---|
| 158 |
} |
|---|
| 159 |
|
|---|
| 160 |
|
|---|
| 161 |
|
|---|
| 162 |
/** @see HTMLPurifier_ConfigSchema->set() */ |
|---|
| 163 |
public static function define($namespace, $name, $default, $type, $description) { |
|---|
| 164 |
HTMLPurifier_ConfigSchema::deprecated(__METHOD__); |
|---|
| 165 |
$type_values = explode('/', $type, 2); |
|---|
| 166 |
$type = $type_values[0]; |
|---|
| 167 |
$modifier = isset($type_values[1]) ? $type_values[1] : false; |
|---|
| 168 |
$allow_null = ($modifier === 'null'); |
|---|
| 169 |
$def = HTMLPurifier_ConfigSchema::instance(); |
|---|
| 170 |
$def->add($namespace, $name, $default, $type, $allow_null); |
|---|
| 171 |
} |
|---|
| 172 |
|
|---|
| 173 |
|
|---|
| 174 |
public static function defineNamespace($namespace, $description) { |
|---|
| 175 |
HTMLPurifier_ConfigSchema::deprecated(__METHOD__); |
|---|
| 176 |
$def = HTMLPurifier_ConfigSchema::instance(); |
|---|
| 177 |
$def->addNamespace($namespace); |
|---|
| 178 |
} |
|---|
| 179 |
|
|---|
| 180 |
|
|---|
| 181 |
public static function defineValueAliases($namespace, $name, $aliases) { |
|---|
| 182 |
HTMLPurifier_ConfigSchema::deprecated(__METHOD__); |
|---|
| 183 |
$def = HTMLPurifier_ConfigSchema::instance(); |
|---|
| 184 |
$def->addValueAliases($namespace, $name, $aliases); |
|---|
| 185 |
} |
|---|
| 186 |
|
|---|
| 187 |
|
|---|
| 188 |
public static function defineAllowedValues($namespace, $name, $allowed_values) { |
|---|
| 189 |
HTMLPurifier_ConfigSchema::deprecated(__METHOD__); |
|---|
| 190 |
$allowed = array(); |
|---|
| 191 |
foreach ($allowed_values as $value) { |
|---|
| 192 |
$allowed[$value] = true; |
|---|
| 193 |
} |
|---|
| 194 |
$def = HTMLPurifier_ConfigSchema::instance(); |
|---|
| 195 |
$def->addAllowedValues($namespace, $name, $allowed); |
|---|
| 196 |
} |
|---|
| 197 |
|
|---|
| 198 |
|
|---|
| 199 |
public static function defineAlias($namespace, $name, $new_namespace, $new_name) { |
|---|
| 200 |
HTMLPurifier_ConfigSchema::deprecated(__METHOD__); |
|---|
| 201 |
$def = HTMLPurifier_ConfigSchema::instance(); |
|---|
| 202 |
$def->addAlias($namespace, $name, $new_namespace, $new_name); |
|---|
| 203 |
} |
|---|
| 204 |
|
|---|
| 205 |
|
|---|
| 206 |
public function validate($a, $b, $c = false) { |
|---|
| 207 |
trigger_error("HTMLPurifier_ConfigSchema->validate deprecated, use HTMLPurifier_VarParser->parse instead", E_USER_NOTICE); |
|---|
| 208 |
$parser = new HTMLPurifier_VarParser(); |
|---|
| 209 |
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 |
trigger_error("Static HTMLPurifier_ConfigSchema::$method deprecated, use add*() method instead", E_USER_NOTICE); |
|---|
| 217 |
} |
|---|
| 218 |
|
|---|
| 219 |
} |
|---|
| 220 |
|
|---|
| 221 |
|
|---|
| 222 |
|
|---|