You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

177 lines
5.8KB

  1. <?php
  2. /**
  3. * Configuration definition, defines directives and their defaults.
  4. */
  5. class HTMLPurifier_ConfigSchema
  6. {
  7. /**
  8. * Defaults of the directives and namespaces.
  9. * @type array
  10. * @note This shares the exact same structure as HTMLPurifier_Config::$conf
  11. */
  12. public $defaults = array();
  13. /**
  14. * The default property list. Do not edit this property list.
  15. * @type array
  16. */
  17. public $defaultPlist;
  18. /**
  19. * Definition of the directives.
  20. * 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. * @type array
  48. */
  49. public $info = array();
  50. /**
  51. * Application-wide singleton
  52. * @type HTMLPurifier_ConfigSchema
  53. */
  54. protected static $singleton;
  55. public function __construct()
  56. {
  57. $this->defaultPlist = new HTMLPurifier_PropertyList();
  58. }
  59. /**
  60. * Unserializes the default ConfigSchema.
  61. * @return HTMLPurifier_ConfigSchema
  62. */
  63. public static function makeFromSerial()
  64. {
  65. $contents = file_get_contents(HTMLPURIFIER_PREFIX . '/HTMLPurifier/ConfigSchema/schema.ser');
  66. $r = unserialize($contents);
  67. if (!$r) {
  68. $hash = sha1($contents);
  69. trigger_error("Unserialization of configuration schema failed, sha1 of file was $hash", E_USER_ERROR);
  70. }
  71. return $r;
  72. }
  73. /**
  74. * Retrieves an instance of the application-wide configuration definition.
  75. * @param HTMLPurifier_ConfigSchema $prototype
  76. * @return HTMLPurifier_ConfigSchema
  77. */
  78. public static function instance($prototype = null)
  79. {
  80. if ($prototype !== null) {
  81. HTMLPurifier_ConfigSchema::$singleton = $prototype;
  82. } elseif (HTMLPurifier_ConfigSchema::$singleton === null || $prototype === true) {
  83. HTMLPurifier_ConfigSchema::$singleton = HTMLPurifier_ConfigSchema::makeFromSerial();
  84. }
  85. return HTMLPurifier_ConfigSchema::$singleton;
  86. }
  87. /**
  88. * Defines a directive for configuration
  89. * @warning Will fail of directive's namespace is defined.
  90. * @warning This method's signature is slightly different from the legacy
  91. * define() static method! Beware!
  92. * @param string $key Name of directive
  93. * @param mixed $default Default value of directive
  94. * @param string $type Allowed type of the directive. See
  95. * HTMLPurifier_DirectiveDef::$type for allowed values
  96. * @param bool $allow_null Whether or not to allow null values
  97. */
  98. public function add($key, $default, $type, $allow_null)
  99. {
  100. $obj = new stdclass();
  101. $obj->type = is_int($type) ? $type : HTMLPurifier_VarParser::$types[$type];
  102. if ($allow_null) {
  103. $obj->allow_null = true;
  104. }
  105. $this->info[$key] = $obj;
  106. $this->defaults[$key] = $default;
  107. $this->defaultPlist->set($key, $default);
  108. }
  109. /**
  110. * Defines a directive value alias.
  111. *
  112. * Directive value aliases are convenient for developers because it lets
  113. * them set a directive to several values and get the same result.
  114. * @param string $key Name of Directive
  115. * @param array $aliases Hash of aliased values to the real alias
  116. */
  117. public function addValueAliases($key, $aliases)
  118. {
  119. if (!isset($this->info[$key]->aliases)) {
  120. $this->info[$key]->aliases = array();
  121. }
  122. foreach ($aliases as $alias => $real) {
  123. $this->info[$key]->aliases[$alias] = $real;
  124. }
  125. }
  126. /**
  127. * Defines a set of allowed values for a directive.
  128. * @warning This is slightly different from the corresponding static
  129. * method definition.
  130. * @param string $key Name of directive
  131. * @param array $allowed Lookup array of allowed values
  132. */
  133. public function addAllowedValues($key, $allowed)
  134. {
  135. $this->info[$key]->allowed = $allowed;
  136. }
  137. /**
  138. * Defines a directive alias for backwards compatibility
  139. * @param string $key Directive that will be aliased
  140. * @param string $new_key Directive that the alias will be to
  141. */
  142. public function addAlias($key, $new_key)
  143. {
  144. $obj = new stdclass;
  145. $obj->key = $new_key;
  146. $obj->isAlias = true;
  147. $this->info[$key] = $obj;
  148. }
  149. /**
  150. * Replaces any stdclass that only has the type property with type integer.
  151. */
  152. public function postProcess()
  153. {
  154. foreach ($this->info as $key => $v) {
  155. if (count((array) $v) == 1) {
  156. $this->info[$key] = $v->type;
  157. } elseif (count((array) $v) == 2 && isset($v->allow_null)) {
  158. $this->info[$key] = -$v->type;
  159. }
  160. }
  161. }
  162. }
  163. // vim: et sw=4 sts=4