ConfigSchema.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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_VarParser::$types 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