]> git.mxchange.org Git - friendica.git/blob - library/HTMLPurifier/ConfigSchema.php
make 'PHP "register_argc_argv"' easier to translate, may require fix for po2php
[friendica.git] / library / HTMLPurifier / ConfigSchema.php
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      * The default property list. Do not edit this property list.
16      */
17     public $defaultPlist;
18
19     /**
20      * Definition of the directives. The structure of this is:
21      *
22      *  array(
23      *      'Namespace' => array(
24      *          'Directive' => new stdclass(),
25      *      )
26      *  )
27      *
28      * The stdclass may have the following properties:
29      *
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
38      *
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.
43      *
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.
47      */
48     public $info = array();
49
50     /**
51      * Application-wide singleton
52      */
53     static protected $singleton;
54
55     public function __construct() {
56         $this->defaultPlist = new HTMLPurifier_PropertyList();
57     }
58
59     /**
60      * Unserializes the default ConfigSchema.
61      */
62     public static function makeFromSerial() {
63         return unserialize(file_get_contents(HTMLPURIFIER_PREFIX . '/HTMLPurifier/ConfigSchema/schema.ser'));
64     }
65
66     /**
67      * Retrieves an instance of the application-wide configuration definition.
68      */
69     public static function instance($prototype = null) {
70         if ($prototype !== null) {
71             HTMLPurifier_ConfigSchema::$singleton = $prototype;
72         } elseif (HTMLPurifier_ConfigSchema::$singleton === null || $prototype === true) {
73             HTMLPurifier_ConfigSchema::$singleton = HTMLPurifier_ConfigSchema::makeFromSerial();
74         }
75         return HTMLPurifier_ConfigSchema::$singleton;
76     }
77
78     /**
79      * Defines a directive for configuration
80      * @warning Will fail of directive's namespace is defined.
81      * @warning This method's signature is slightly different from the legacy
82      *          define() static method! Beware!
83      * @param $namespace Namespace the directive is in
84      * @param $name Key of directive
85      * @param $default Default value of directive
86      * @param $type Allowed type of the directive. See
87      *      HTMLPurifier_DirectiveDef::$type for allowed values
88      * @param $allow_null Whether or not to allow null values
89      */
90     public function add($key, $default, $type, $allow_null) {
91         $obj = new stdclass();
92         $obj->type = is_int($type) ? $type : HTMLPurifier_VarParser::$types[$type];
93         if ($allow_null) $obj->allow_null = true;
94         $this->info[$key] = $obj;
95         $this->defaults[$key] = $default;
96         $this->defaultPlist->set($key, $default);
97     }
98
99     /**
100      * Defines a directive value alias.
101      *
102      * Directive value aliases are convenient for developers because it lets
103      * them set a directive to several values and get the same result.
104      * @param $namespace Directive's namespace
105      * @param $name Name of Directive
106      * @param $aliases Hash of aliased values to the real alias
107      */
108     public function addValueAliases($key, $aliases) {
109         if (!isset($this->info[$key]->aliases)) {
110             $this->info[$key]->aliases = array();
111         }
112         foreach ($aliases as $alias => $real) {
113             $this->info[$key]->aliases[$alias] = $real;
114         }
115     }
116
117     /**
118      * Defines a set of allowed values for a directive.
119      * @warning This is slightly different from the corresponding static
120      *          method definition.
121      * @param $namespace Namespace of directive
122      * @param $name Name of directive
123      * @param $allowed Lookup array of allowed values
124      */
125     public function addAllowedValues($key, $allowed) {
126         $this->info[$key]->allowed = $allowed;
127     }
128
129     /**
130      * Defines a directive alias for backwards compatibility
131      * @param $namespace
132      * @param $name Directive that will be aliased
133      * @param $new_namespace
134      * @param $new_name Directive that the alias will be to
135      */
136     public function addAlias($key, $new_key) {
137         $obj = new stdclass;
138         $obj->key = $new_key;
139         $obj->isAlias = true;
140         $this->info[$key] = $obj;
141     }
142
143     /**
144      * Replaces any stdclass that only has the type property with type integer.
145      */
146     public function postProcess() {
147         foreach ($this->info as $key => $v) {
148             if (count((array) $v) == 1) {
149                 $this->info[$key] = $v->type;
150             } elseif (count((array) $v) == 2 && isset($v->allow_null)) {
151                 $this->info[$key] = -$v->type;
152             }
153         }
154     }
155
156 }
157
158 // vim: et sw=4 sts=4