ConfigSchema.php 5.6 KB

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