| [21] | 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 | * Definition of the directives. |
|---|
| 16 | */ |
|---|
| 17 | public $info = array(); |
|---|
| 18 | |
|---|
| 19 | /** |
|---|
| 20 | * Application-wide singleton |
|---|
| 21 | */ |
|---|
| 22 | static protected $singleton; |
|---|
| 23 | |
|---|
| 24 | /** |
|---|
| 25 | * Variable parser. |
|---|
| 26 | */ |
|---|
| 27 | protected $parser; |
|---|
| 28 | |
|---|
| 29 | public function __construct() { |
|---|
| 30 | $this->parser = new HTMLPurifier_VarParser_Flexible(); |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | /** |
|---|
| 34 | * Unserializes the default ConfigSchema. |
|---|
| 35 | */ |
|---|
| 36 | public static function makeFromSerial() { |
|---|
| 37 | return unserialize(file_get_contents(HTMLPURIFIER_PREFIX . '/HTMLPurifier/ConfigSchema/schema.ser')); |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | /** |
|---|
| 41 | * Retrieves an instance of the application-wide configuration definition. |
|---|
| 42 | */ |
|---|
| 43 | public static function instance($prototype = null) { |
|---|
| 44 | if ($prototype !== null) { |
|---|
| 45 | HTMLPurifier_ConfigSchema::$singleton = $prototype; |
|---|
| 46 | } elseif (HTMLPurifier_ConfigSchema::$singleton === null || $prototype === true) { |
|---|
| 47 | HTMLPurifier_ConfigSchema::$singleton = HTMLPurifier_ConfigSchema::makeFromSerial(); |
|---|
| 48 | } |
|---|
| 49 | return HTMLPurifier_ConfigSchema::$singleton; |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | /** |
|---|
| 53 | * Defines a directive for configuration |
|---|
| 54 | * @warning Will fail of directive's namespace is defined. |
|---|
| 55 | * @warning This method's signature is slightly different from the legacy |
|---|
| 56 | * define() static method! Beware! |
|---|
| 57 | * @param $namespace Namespace the directive is in |
|---|
| 58 | * @param $name Key of directive |
|---|
| 59 | * @param $default Default value of directive |
|---|
| 60 | * @param $type Allowed type of the directive. See |
|---|
| 61 | * HTMLPurifier_DirectiveDef::$type for allowed values |
|---|
| 62 | * @param $allow_null Whether or not to allow null values |
|---|
| 63 | */ |
|---|
| 64 | public function add($namespace, $name, $default, $type, $allow_null) { |
|---|
| 65 | $default = $this->parser->parse($default, $type, $allow_null); |
|---|
| 66 | $this->info[$namespace][$name] = new HTMLPurifier_ConfigDef_Directive(); |
|---|
| 67 | $this->info[$namespace][$name]->type = $type; |
|---|
| 68 | $this->info[$namespace][$name]->allow_null = $allow_null; |
|---|
| 69 | $this->defaults[$namespace][$name] = $default; |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | /** |
|---|
| 73 | * Defines a namespace for directives to be put into. |
|---|
| 74 | * @warning This is slightly different from the corresponding static |
|---|
| 75 | * method. |
|---|
| 76 | * @param $namespace Namespace's name |
|---|
| 77 | */ |
|---|
| 78 | public function addNamespace($namespace) { |
|---|
| 79 | $this->info[$namespace] = array(); |
|---|
| 80 | $this->defaults[$namespace] = array(); |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | /** |
|---|
| 84 | * Defines a directive value alias. |
|---|
| 85 | * |
|---|
| 86 | * Directive value aliases are convenient for developers because it lets |
|---|
| 87 | * them set a directive to several values and get the same result. |
|---|
| 88 | * @param $namespace Directive's namespace |
|---|
| 89 | * @param $name Name of Directive |
|---|
| 90 | * @param $aliases Hash of aliased values to the real alias |
|---|
| 91 | */ |
|---|
| 92 | public function addValueAliases($namespace, $name, $aliases) { |
|---|
| 93 | foreach ($aliases as $alias => $real) { |
|---|
| 94 | $this->info[$namespace][$name]->aliases[$alias] = $real; |
|---|
| 95 | } |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | /** |
|---|
| 99 | * Defines a set of allowed values for a directive. |
|---|
| 100 | * @warning This is slightly different from the corresponding static |
|---|
| 101 | * method definition. |
|---|
| 102 | * @param $namespace Namespace of directive |
|---|
| 103 | * @param $name Name of directive |
|---|
| 104 | * @param $allowed Lookup array of allowed values |
|---|
| 105 | */ |
|---|
| 106 | public function addAllowedValues($namespace, $name, $allowed) { |
|---|
| 107 | $type = $this->info[$namespace][$name]->type; |
|---|
| 108 | $this->info[$namespace][$name]->allowed = $allowed; |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | /** |
|---|
| 112 | * Defines a directive alias for backwards compatibility |
|---|
| 113 | * @param $namespace |
|---|
| 114 | * @param $name Directive that will be aliased |
|---|
| 115 | * @param $new_namespace |
|---|
| 116 | * @param $new_name Directive that the alias will be to |
|---|
| 117 | */ |
|---|
| 118 | public function addAlias($namespace, $name, $new_namespace, $new_name) { |
|---|
| 119 | $this->info[$namespace][$name] = new HTMLPurifier_ConfigDef_DirectiveAlias($new_namespace, $new_name); |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | // DEPRECATED METHODS |
|---|
| 123 | |
|---|
| 124 | /** @see HTMLPurifier_ConfigSchema->set() */ |
|---|
| 125 | public static function define($namespace, $name, $default, $type, $description) { |
|---|
| 126 | HTMLPurifier_ConfigSchema::deprecated(__METHOD__); |
|---|
| 127 | // process modifiers (OPTIMIZE!) |
|---|
| 128 | $type_values = explode('/', $type, 2); |
|---|
| 129 | $type = $type_values[0]; |
|---|
| 130 | $modifier = isset($type_values[1]) ? $type_values[1] : false; |
|---|
| 131 | $allow_null = ($modifier === 'null'); |
|---|
| 132 | $def = HTMLPurifier_ConfigSchema::instance(); |
|---|
| 133 | $def->add($namespace, $name, $default, $type, $allow_null); |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | /** @see HTMLPurifier_ConfigSchema->addNamespace() */ |
|---|
| 137 | public static function defineNamespace($namespace, $description) { |
|---|
| 138 | HTMLPurifier_ConfigSchema::deprecated(__METHOD__); |
|---|
| 139 | $def = HTMLPurifier_ConfigSchema::instance(); |
|---|
| 140 | $def->addNamespace($namespace); |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | /** @see HTMLPurifier_ConfigSchema->addValueAliases() */ |
|---|
| 144 | public static function defineValueAliases($namespace, $name, $aliases) { |
|---|
| 145 | HTMLPurifier_ConfigSchema::deprecated(__METHOD__); |
|---|
| 146 | $def = HTMLPurifier_ConfigSchema::instance(); |
|---|
| 147 | $def->addValueAliases($namespace, $name, $aliases); |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | /** @see HTMLPurifier_ConfigSchema->addAllowedValues() */ |
|---|
| 151 | public static function defineAllowedValues($namespace, $name, $allowed_values) { |
|---|
| 152 | HTMLPurifier_ConfigSchema::deprecated(__METHOD__); |
|---|
| 153 | $allowed = array(); |
|---|
| 154 | foreach ($allowed_values as $value) { |
|---|
| 155 | $allowed[$value] = true; |
|---|
| 156 | } |
|---|
| 157 | $def = HTMLPurifier_ConfigSchema::instance(); |
|---|
| 158 | $def->addAllowedValues($namespace, $name, $allowed); |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | /** @see HTMLPurifier_ConfigSchema->addAlias() */ |
|---|
| 162 | public static function defineAlias($namespace, $name, $new_namespace, $new_name) { |
|---|
| 163 | HTMLPurifier_ConfigSchema::deprecated(__METHOD__); |
|---|
| 164 | $def = HTMLPurifier_ConfigSchema::instance(); |
|---|
| 165 | $def->addAlias($namespace, $name, $new_namespace, $new_name); |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | /** @deprecated, use HTMLPurifier_VarParser->parse() */ |
|---|
| 169 | public function validate($a, $b, $c = false) { |
|---|
| 170 | trigger_error("HTMLPurifier_ConfigSchema->validate deprecated, use HTMLPurifier_VarParser->parse instead", E_USER_NOTICE); |
|---|
| 171 | return $this->parser->parse($a, $b, $c); |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | /** |
|---|
| 175 | * Throws an E_USER_NOTICE stating that a method is deprecated. |
|---|
| 176 | */ |
|---|
| 177 | private static function deprecated($method) { |
|---|
| 178 | trigger_error("Static HTMLPurifier_ConfigSchema::$method deprecated, use add*() method instead", E_USER_NOTICE); |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | |
|---|