Fix two minor bugs, updating Phorum and removing unused $dir variable.
[htmlpurifier.git] / library / HTMLPurifier / ConfigSchema / InterchangeBuilder.php
bloba8db5c085512d8a2909c1106158b6087f02c9ccc
1 <?php
3 class HTMLPurifier_ConfigSchema_InterchangeBuilder
6 /**
7 * Used for processing DEFAULT, nothing else.
8 */
9 protected $varParser;
11 public function __construct($varParser = null) {
12 $this->varParser = $varParser ? $varParser : new HTMLPurifier_VarParser_Native();
15 public static function buildFromDirectory($dir = null) {
16 $builder = new HTMLPurifier_ConfigSchema_InterchangeBuilder();
17 $interchange = new HTMLPurifier_ConfigSchema_Interchange();
19 if (!$dir) $dir = HTMLPURIFIER_PREFIX . '/HTMLPurifier/ConfigSchema/schema/';
20 $info = parse_ini_file($dir . 'info.ini');
21 $interchange->name = $info['name'];
23 $files = array();
24 $dh = opendir($dir);
25 while (false !== ($file = readdir($dh))) {
26 if (!$file || $file[0] == '.' || strrchr($file, '.') !== '.txt') {
27 continue;
29 $files[] = $file;
31 closedir($dh);
33 sort($files);
34 foreach ($files as $file) {
35 $builder->buildFile($interchange, $dir . $file);
38 return $interchange;
41 public function buildFile($interchange, $file) {
42 $parser = new HTMLPurifier_StringHashParser();
43 $this->build(
44 $interchange,
45 new HTMLPurifier_StringHash( $parser->parseFile($file) )
49 /**
50 * Builds an interchange object based on a hash.
51 * @param $interchange HTMLPurifier_ConfigSchema_Interchange object to build
52 * @param $hash HTMLPurifier_ConfigSchema_StringHash source data
54 public function build($interchange, $hash) {
55 if (!$hash instanceof HTMLPurifier_StringHash) {
56 $hash = new HTMLPurifier_StringHash($hash);
58 if (!isset($hash['ID'])) {
59 throw new HTMLPurifier_ConfigSchema_Exception('Hash does not have any ID');
61 if (strpos($hash['ID'], '.') === false) {
62 if (count($hash) == 2 && isset($hash['DESCRIPTION'])) {
63 $hash->offsetGet('DESCRIPTION'); // prevent complaining
64 } else {
65 throw new HTMLPurifier_ConfigSchema_Exception('All directives must have a namespace');
67 } else {
68 $this->buildDirective($interchange, $hash);
70 $this->_findUnused($hash);
73 public function buildDirective($interchange, $hash) {
74 $directive = new HTMLPurifier_ConfigSchema_Interchange_Directive();
76 // These are required elements:
77 $directive->id = $this->id($hash->offsetGet('ID'));
78 $id = $directive->id->toString(); // convenience
80 if (isset($hash['TYPE'])) {
81 $type = explode('/', $hash->offsetGet('TYPE'));
82 if (isset($type[1])) $directive->typeAllowsNull = true;
83 $directive->type = $type[0];
84 } else {
85 throw new HTMLPurifier_ConfigSchema_Exception("TYPE in directive hash '$id' not defined");
88 if (isset($hash['DEFAULT'])) {
89 try {
90 $directive->default = $this->varParser->parse($hash->offsetGet('DEFAULT'), $directive->type, $directive->typeAllowsNull);
91 } catch (HTMLPurifier_VarParserException $e) {
92 throw new HTMLPurifier_ConfigSchema_Exception($e->getMessage() . " in DEFAULT in directive hash '$id'");
96 if (isset($hash['DESCRIPTION'])) {
97 $directive->description = $hash->offsetGet('DESCRIPTION');
100 if (isset($hash['ALLOWED'])) {
101 $directive->allowed = $this->lookup($this->evalArray($hash->offsetGet('ALLOWED')));
104 if (isset($hash['VALUE-ALIASES'])) {
105 $directive->valueAliases = $this->evalArray($hash->offsetGet('VALUE-ALIASES'));
108 if (isset($hash['ALIASES'])) {
109 $raw_aliases = trim($hash->offsetGet('ALIASES'));
110 $aliases = preg_split('/\s*,\s*/', $raw_aliases);
111 foreach ($aliases as $alias) {
112 $directive->aliases[] = $this->id($alias);
116 if (isset($hash['VERSION'])) {
117 $directive->version = $hash->offsetGet('VERSION');
120 if (isset($hash['DEPRECATED-USE'])) {
121 $directive->deprecatedUse = $this->id($hash->offsetGet('DEPRECATED-USE'));
124 if (isset($hash['DEPRECATED-VERSION'])) {
125 $directive->deprecatedVersion = $hash->offsetGet('DEPRECATED-VERSION');
128 if (isset($hash['EXTERNAL'])) {
129 $directive->external = preg_split('/\s*,\s*/', trim($hash->offsetGet('EXTERNAL')));
132 $interchange->addDirective($directive);
136 * Evaluates an array PHP code string without array() wrapper
138 protected function evalArray($contents) {
139 return eval('return array('. $contents .');');
143 * Converts an array list into a lookup array.
145 protected function lookup($array) {
146 $ret = array();
147 foreach ($array as $val) $ret[$val] = true;
148 return $ret;
152 * Convenience function that creates an HTMLPurifier_ConfigSchema_Interchange_Id
153 * object based on a string Id.
155 protected function id($id) {
156 return HTMLPurifier_ConfigSchema_Interchange_Id::make($id);
160 * Triggers errors for any unused keys passed in the hash; such keys
161 * may indicate typos, missing values, etc.
162 * @param $hash Instance of ConfigSchema_StringHash to check.
164 protected function _findUnused($hash) {
165 $accessed = $hash->getAccessed();
166 foreach ($hash as $k => $v) {
167 if (!isset($accessed[$k])) {
168 trigger_error("String hash key '$k' not used by builder", E_USER_NOTICE);
175 // vim: et sw=4 sts=4