File indexing completed on 2024-12-22 05:36:21

0001 <?php
0002 
0003 /**
0004  * Configuration definition, defines directives and their defaults.
0005  */
0006 class HTMLPurifier_ConfigSchema
0007 {
0008     /**
0009      * Defaults of the directives and namespaces.
0010      * @type array
0011      * @note This shares the exact same structure as HTMLPurifier_Config::$conf
0012      */
0013     public $defaults = array();
0014 
0015     /**
0016      * The default property list. Do not edit this property list.
0017      * @type array
0018      */
0019     public $defaultPlist;
0020 
0021     /**
0022      * Definition of the directives.
0023      * The structure of this is:
0024      *
0025      *  array(
0026      *      'Namespace' => array(
0027      *          'Directive' => new stdClass(),
0028      *      )
0029      *  )
0030      *
0031      * The stdClass may have the following properties:
0032      *
0033      *  - If isAlias isn't set:
0034      *      - type: Integer type of directive, see HTMLPurifier_VarParser for definitions
0035      *      - allow_null: If set, this directive allows null values
0036      *      - aliases: If set, an associative array of value aliases to real values
0037      *      - allowed: If set, a lookup array of allowed (string) values
0038      *  - If isAlias is set:
0039      *      - namespace: Namespace this directive aliases to
0040      *      - name: Directive name this directive aliases to
0041      *
0042      * In certain degenerate cases, stdClass will actually be an integer. In
0043      * that case, the value is equivalent to an stdClass with the type
0044      * property set to the integer. If the integer is negative, type is
0045      * equal to the absolute value of integer, and allow_null is true.
0046      *
0047      * This class is friendly with HTMLPurifier_Config. If you need introspection
0048      * about the schema, you're better of using the ConfigSchema_Interchange,
0049      * which uses more memory but has much richer information.
0050      * @type array
0051      */
0052     public $info = array();
0053 
0054     /**
0055      * Application-wide singleton
0056      * @type HTMLPurifier_ConfigSchema
0057      */
0058     protected static $singleton;
0059 
0060     public function __construct()
0061     {
0062         $this->defaultPlist = new HTMLPurifier_PropertyList();
0063     }
0064 
0065     /**
0066      * Unserializes the default ConfigSchema.
0067      * @return HTMLPurifier_ConfigSchema
0068      */
0069     public static function makeFromSerial()
0070     {
0071         $contents = file_get_contents(HTMLPURIFIER_PREFIX . '/HTMLPurifier/ConfigSchema/schema.ser');
0072         $r = unserialize($contents);
0073         if (!$r) {
0074             $hash = sha1($contents);
0075             trigger_error("Unserialization of configuration schema failed, sha1 of file was $hash", E_USER_ERROR);
0076         }
0077         return $r;
0078     }
0079 
0080     /**
0081      * Retrieves an instance of the application-wide configuration definition.
0082      * @param HTMLPurifier_ConfigSchema $prototype
0083      * @return HTMLPurifier_ConfigSchema
0084      */
0085     public static function instance($prototype = null)
0086     {
0087         if ($prototype !== null) {
0088             HTMLPurifier_ConfigSchema::$singleton = $prototype;
0089         } elseif (HTMLPurifier_ConfigSchema::$singleton === null || $prototype === true) {
0090             HTMLPurifier_ConfigSchema::$singleton = HTMLPurifier_ConfigSchema::makeFromSerial();
0091         }
0092         return HTMLPurifier_ConfigSchema::$singleton;
0093     }
0094 
0095     /**
0096      * Defines a directive for configuration
0097      * @warning Will fail of directive's namespace is defined.
0098      * @warning This method's signature is slightly different from the legacy
0099      *          define() static method! Beware!
0100      * @param string $key Name of directive
0101      * @param mixed $default Default value of directive
0102      * @param string $type Allowed type of the directive. See
0103      *      HTMLPurifier_DirectiveDef::$type for allowed values
0104      * @param bool $allow_null Whether or not to allow null values
0105      */
0106     public function add($key, $default, $type, $allow_null)
0107     {
0108         $obj = new stdClass();
0109         $obj->type = is_int($type) ? $type : HTMLPurifier_VarParser::$types[$type];
0110         if ($allow_null) {
0111             $obj->allow_null = true;
0112         }
0113         $this->info[$key] = $obj;
0114         $this->defaults[$key] = $default;
0115         $this->defaultPlist->set($key, $default);
0116     }
0117 
0118     /**
0119      * Defines a directive value alias.
0120      *
0121      * Directive value aliases are convenient for developers because it lets
0122      * them set a directive to several values and get the same result.
0123      * @param string $key Name of Directive
0124      * @param array $aliases Hash of aliased values to the real alias
0125      */
0126     public function addValueAliases($key, $aliases)
0127     {
0128         if (!isset($this->info[$key]->aliases)) {
0129             $this->info[$key]->aliases = array();
0130         }
0131         foreach ($aliases as $alias => $real) {
0132             $this->info[$key]->aliases[$alias] = $real;
0133         }
0134     }
0135 
0136     /**
0137      * Defines a set of allowed values for a directive.
0138      * @warning This is slightly different from the corresponding static
0139      *          method definition.
0140      * @param string $key Name of directive
0141      * @param array $allowed Lookup array of allowed values
0142      */
0143     public function addAllowedValues($key, $allowed)
0144     {
0145         $this->info[$key]->allowed = $allowed;
0146     }
0147 
0148     /**
0149      * Defines a directive alias for backwards compatibility
0150      * @param string $key Directive that will be aliased
0151      * @param string $new_key Directive that the alias will be to
0152      */
0153     public function addAlias($key, $new_key)
0154     {
0155         $obj = new stdClass;
0156         $obj->key = $new_key;
0157         $obj->isAlias = true;
0158         $this->info[$key] = $obj;
0159     }
0160 
0161     /**
0162      * Replaces any stdClass that only has the type property with type integer.
0163      */
0164     public function postProcess()
0165     {
0166         foreach ($this->info as $key => $v) {
0167             if (count((array) $v) == 1) {
0168                 $this->info[$key] = $v->type;
0169             } elseif (count((array) $v) == 2 && isset($v->allow_null)) {
0170                 $this->info[$key] = -$v->type;
0171             }
0172         }
0173     }
0174 }
0175 
0176 // vim: et sw=4 sts=4