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