PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/library/vendors/htmlpurifier/standalone/HTMLPurifier/ConfigSchema/InterchangeBuilder.php

https://github.com/mischka/Garden
PHP | 176 lines | 122 code | 29 blank | 25 comment | 24 complexity | 98fb07379ce527e1fcc2f826a42270c6 MD5 | raw file
  1. <?php
  2. class HTMLPurifier_ConfigSchema_InterchangeBuilder
  3. {
  4. /**
  5. * Used for processing DEFAULT, nothing else.
  6. */
  7. protected $varParser;
  8. public function __construct($varParser = null) {
  9. $this->varParser = $varParser ? $varParser : new HTMLPurifier_VarParser_Native();
  10. }
  11. public static function buildFromDirectory($dir = null) {
  12. $parser = new HTMLPurifier_StringHashParser();
  13. $builder = new HTMLPurifier_ConfigSchema_InterchangeBuilder();
  14. $interchange = new HTMLPurifier_ConfigSchema_Interchange();
  15. if (!$dir) $dir = HTMLPURIFIER_PREFIX . '/HTMLPurifier/ConfigSchema/schema/';
  16. $info = parse_ini_file($dir . 'info.ini');
  17. $interchange->name = $info['name'];
  18. $files = array();
  19. $dh = opendir($dir);
  20. while (false !== ($file = readdir($dh))) {
  21. if (!$file || $file[0] == '.' || strrchr($file, '.') !== '.txt') {
  22. continue;
  23. }
  24. $files[] = $file;
  25. }
  26. closedir($dh);
  27. sort($files);
  28. foreach ($files as $file) {
  29. $builder->build(
  30. $interchange,
  31. new HTMLPurifier_StringHash( $parser->parseFile($dir . $file) )
  32. );
  33. }
  34. return $interchange;
  35. }
  36. /**
  37. * Builds an interchange object based on a hash.
  38. * @param $interchange HTMLPurifier_ConfigSchema_Interchange object to build
  39. * @param $hash HTMLPurifier_ConfigSchema_StringHash source data
  40. */
  41. public function build($interchange, $hash) {
  42. if (!$hash instanceof HTMLPurifier_StringHash) {
  43. $hash = new HTMLPurifier_StringHash($hash);
  44. }
  45. if (!isset($hash['ID'])) {
  46. throw new HTMLPurifier_ConfigSchema_Exception('Hash does not have any ID');
  47. }
  48. if (strpos($hash['ID'], '.') === false) {
  49. $this->buildNamespace($interchange, $hash);
  50. } else {
  51. $this->buildDirective($interchange, $hash);
  52. }
  53. $this->_findUnused($hash);
  54. }
  55. public function buildNamespace($interchange, $hash) {
  56. $namespace = new HTMLPurifier_ConfigSchema_Interchange_Namespace();
  57. $namespace->namespace = $hash->offsetGet('ID');
  58. if (isset($hash['DESCRIPTION'])) {
  59. $namespace->description = $hash->offsetGet('DESCRIPTION');
  60. }
  61. $interchange->addNamespace($namespace);
  62. }
  63. public function buildDirective($interchange, $hash) {
  64. $directive = new HTMLPurifier_ConfigSchema_Interchange_Directive();
  65. // These are required elements:
  66. $directive->id = $this->id($hash->offsetGet('ID'));
  67. $id = $directive->id->toString(); // convenience
  68. if (isset($hash['TYPE'])) {
  69. $type = explode('/', $hash->offsetGet('TYPE'));
  70. if (isset($type[1])) $directive->typeAllowsNull = true;
  71. $directive->type = $type[0];
  72. } else {
  73. throw new HTMLPurifier_ConfigSchema_Exception("TYPE in directive hash '$id' not defined");
  74. }
  75. if (isset($hash['DEFAULT'])) {
  76. try {
  77. $directive->default = $this->varParser->parse($hash->offsetGet('DEFAULT'), $directive->type, $directive->typeAllowsNull);
  78. } catch (HTMLPurifier_VarParserException $e) {
  79. throw new HTMLPurifier_ConfigSchema_Exception($e->getMessage() . " in DEFAULT in directive hash '$id'");
  80. }
  81. }
  82. if (isset($hash['DESCRIPTION'])) {
  83. $directive->description = $hash->offsetGet('DESCRIPTION');
  84. }
  85. if (isset($hash['ALLOWED'])) {
  86. $directive->allowed = $this->lookup($this->evalArray($hash->offsetGet('ALLOWED')));
  87. }
  88. if (isset($hash['VALUE-ALIASES'])) {
  89. $directive->valueAliases = $this->evalArray($hash->offsetGet('VALUE-ALIASES'));
  90. }
  91. if (isset($hash['ALIASES'])) {
  92. $raw_aliases = trim($hash->offsetGet('ALIASES'));
  93. $aliases = preg_split('/\s*,\s*/', $raw_aliases);
  94. foreach ($aliases as $alias) {
  95. $directive->aliases[] = $this->id($alias);
  96. }
  97. }
  98. if (isset($hash['VERSION'])) {
  99. $directive->version = $hash->offsetGet('VERSION');
  100. }
  101. if (isset($hash['DEPRECATED-USE'])) {
  102. $directive->deprecatedUse = $this->id($hash->offsetGet('DEPRECATED-USE'));
  103. }
  104. if (isset($hash['DEPRECATED-VERSION'])) {
  105. $directive->deprecatedVersion = $hash->offsetGet('DEPRECATED-VERSION');
  106. }
  107. if (isset($hash['EXTERNAL'])) {
  108. $directive->external = preg_split('/\s*,\s*/', trim($hash->offsetGet('EXTERNAL')));
  109. }
  110. $interchange->addDirective($directive);
  111. }
  112. /**
  113. * Evaluates an array PHP code string without array() wrapper
  114. */
  115. protected function evalArray($contents) {
  116. return eval('return array('. $contents .');');
  117. }
  118. /**
  119. * Converts an array list into a lookup array.
  120. */
  121. protected function lookup($array) {
  122. $ret = array();
  123. foreach ($array as $val) $ret[$val] = true;
  124. return $ret;
  125. }
  126. /**
  127. * Convenience function that creates an HTMLPurifier_ConfigSchema_Interchange_Id
  128. * object based on a string Id.
  129. */
  130. protected function id($id) {
  131. return HTMLPurifier_ConfigSchema_Interchange_Id::make($id);
  132. }
  133. /**
  134. * Triggers errors for any unused keys passed in the hash; such keys
  135. * may indicate typos, missing values, etc.
  136. * @param $hash Instance of ConfigSchema_StringHash to check.
  137. */
  138. protected function _findUnused($hash) {
  139. $accessed = $hash->getAccessed();
  140. foreach ($hash as $k => $v) {
  141. if (!isset($accessed[$k])) {
  142. trigger_error("String hash key '$k' not used by builder", E_USER_NOTICE);
  143. }
  144. }
  145. }
  146. }
  147. // vim: et sw=4 sts=4