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