2008-03-22 03:55:59 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class HTMLPurifier_ConfigSchema_InterchangeBuilder
|
|
|
|
{
|
|
|
|
|
2008-04-04 21:33:37 +00:00
|
|
|
/**
|
|
|
|
* Used for processing DEFAULT, nothing else.
|
|
|
|
*/
|
2008-03-22 03:55:59 +00:00
|
|
|
protected $varParser;
|
|
|
|
|
|
|
|
public function __construct($varParser = null) {
|
|
|
|
$this->varParser = $varParser ? $varParser : new HTMLPurifier_VarParser_Native();
|
|
|
|
}
|
|
|
|
|
2008-04-22 01:58:06 +00:00
|
|
|
public static function buildFromDirectory($dir = null) {
|
|
|
|
$parser = new HTMLPurifier_StringHashParser();
|
|
|
|
$builder = new HTMLPurifier_ConfigSchema_InterchangeBuilder();
|
|
|
|
$interchange = new HTMLPurifier_ConfigSchema_Interchange();
|
|
|
|
|
|
|
|
if (!$dir) $dir = realpath(dirname(__FILE__) . '/schema/');
|
|
|
|
$info = parse_ini_file($dir . 'info.ini');
|
|
|
|
$interchange->name = $info['name'];
|
|
|
|
|
|
|
|
$dh = opendir($dir);
|
|
|
|
while (false !== ($file = readdir($dh))) {
|
|
|
|
if (!$file || $file[0] == '.' || strrchr($file, '.') !== '.txt') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$builder->build(
|
|
|
|
$interchange,
|
|
|
|
new HTMLPurifier_StringHash( $parser->parseFile($dir . $file) )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
closedir($dh);
|
|
|
|
|
|
|
|
return $interchange;
|
|
|
|
}
|
|
|
|
|
2008-03-22 03:55:59 +00:00
|
|
|
/**
|
|
|
|
* Builds an interchange object based on a hash.
|
|
|
|
* @param $interchange HTMLPurifier_ConfigSchema_Interchange object to build
|
|
|
|
* @param $hash HTMLPurifier_ConfigSchema_StringHash source data
|
|
|
|
*/
|
|
|
|
public function build($interchange, $hash) {
|
2008-03-23 01:06:35 +00:00
|
|
|
if (!$hash instanceof HTMLPurifier_StringHash) {
|
|
|
|
$hash = new HTMLPurifier_StringHash($hash);
|
|
|
|
}
|
|
|
|
if (!isset($hash['ID'])) {
|
|
|
|
throw new HTMLPurifier_ConfigSchema_Exception('Hash does not have any ID');
|
|
|
|
}
|
2008-03-22 03:55:59 +00:00
|
|
|
if (strpos($hash['ID'], '.') === false) {
|
|
|
|
$this->buildNamespace($interchange, $hash);
|
|
|
|
} else {
|
|
|
|
$this->buildDirective($interchange, $hash);
|
|
|
|
}
|
|
|
|
$this->_findUnused($hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function buildNamespace($interchange, $hash) {
|
|
|
|
$namespace = new HTMLPurifier_ConfigSchema_Interchange_Namespace();
|
|
|
|
$namespace->namespace = $hash->offsetGet('ID');
|
2008-03-23 01:06:35 +00:00
|
|
|
if (isset($hash['DESCRIPTION'])) {
|
|
|
|
$namespace->description = $hash->offsetGet('DESCRIPTION');
|
|
|
|
}
|
2008-03-22 03:55:59 +00:00
|
|
|
$interchange->addNamespace($namespace);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function buildDirective($interchange, $hash) {
|
|
|
|
$directive = new HTMLPurifier_ConfigSchema_Interchange_Directive();
|
|
|
|
|
|
|
|
// These are required elements:
|
|
|
|
$directive->id = $this->id($hash->offsetGet('ID'));
|
2008-03-23 02:50:42 +00:00
|
|
|
$id = $directive->id->toString(); // convenience
|
2008-03-22 03:55:59 +00:00
|
|
|
|
2008-03-23 01:06:35 +00:00
|
|
|
if (isset($hash['TYPE'])) {
|
|
|
|
$type = explode('/', $hash->offsetGet('TYPE'));
|
|
|
|
if (isset($type[1])) $directive->typeAllowsNull = true;
|
|
|
|
$directive->type = $type[0];
|
2008-03-23 01:29:57 +00:00
|
|
|
} else {
|
2008-03-23 02:50:42 +00:00
|
|
|
throw new HTMLPurifier_ConfigSchema_Exception("TYPE in directive hash '$id' not defined");
|
2008-03-23 01:29:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($hash['DEFAULT'])) {
|
|
|
|
try {
|
|
|
|
$directive->default = $this->varParser->parse($hash->offsetGet('DEFAULT'), $directive->type, $directive->typeAllowsNull);
|
|
|
|
} catch (HTMLPurifier_VarParserException $e) {
|
2008-04-05 18:37:08 +00:00
|
|
|
throw new HTMLPurifier_ConfigSchema_Exception($e->getMessage() . " in DEFAULT in directive hash '$id'");
|
2008-03-23 01:29:57 +00:00
|
|
|
}
|
2008-03-23 01:06:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($hash['DESCRIPTION'])) {
|
|
|
|
$directive->description = $hash->offsetGet('DESCRIPTION');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($hash['ALLOWED'])) {
|
|
|
|
$directive->allowed = $this->lookup($this->evalArray($hash->offsetGet('ALLOWED')));
|
2008-03-22 03:55:59 +00:00
|
|
|
}
|
2008-03-23 01:06:35 +00:00
|
|
|
|
|
|
|
if (isset($hash['VALUE-ALIASES'])) {
|
2008-04-04 21:33:37 +00:00
|
|
|
$directive->valueAliases = $this->evalArray($hash->offsetGet('VALUE-ALIASES'));
|
2008-03-22 03:55:59 +00:00
|
|
|
}
|
2008-03-23 01:06:35 +00:00
|
|
|
|
|
|
|
if (isset($hash['ALIASES'])) {
|
2008-03-22 03:55:59 +00:00
|
|
|
$raw_aliases = trim($hash->offsetGet('ALIASES'));
|
|
|
|
$aliases = preg_split('/\s*,\s*/', $raw_aliases);
|
|
|
|
foreach ($aliases as $alias) {
|
2008-03-23 01:06:35 +00:00
|
|
|
$directive->aliases[] = $this->id($alias);
|
2008-03-22 03:55:59 +00:00
|
|
|
}
|
|
|
|
}
|
2008-03-23 01:06:35 +00:00
|
|
|
|
|
|
|
if (isset($hash['VERSION'])) {
|
|
|
|
$directive->version = $hash->offsetGet('VERSION');
|
2008-03-22 03:55:59 +00:00
|
|
|
}
|
2008-03-23 01:06:35 +00:00
|
|
|
|
|
|
|
if (isset($hash['DEPRECATED-USE'])) {
|
|
|
|
$directive->deprecatedUse = $this->id($hash->offsetGet('DEPRECATED-USE'));
|
2008-03-22 03:55:59 +00:00
|
|
|
}
|
2008-03-23 01:06:35 +00:00
|
|
|
|
|
|
|
if (isset($hash['DEPRECATED-VERSION'])) {
|
|
|
|
$directive->deprecatedVersion = $hash->offsetGet('DEPRECATED-VERSION');
|
2008-03-22 03:55:59 +00:00
|
|
|
}
|
|
|
|
|
2008-04-22 06:40:04 +00:00
|
|
|
if (isset($hash['EXTERNAL'])) {
|
|
|
|
$directive->external = preg_split('/\s*,\s*/', trim($hash->offsetGet('EXTERNAL')));
|
|
|
|
}
|
|
|
|
|
2008-03-22 03:55:59 +00:00
|
|
|
$interchange->addDirective($directive);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Evaluates an array PHP code string without array() wrapper
|
|
|
|
*/
|
|
|
|
protected function evalArray($contents) {
|
|
|
|
return eval('return array('. $contents .');');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts an array list into a lookup array.
|
|
|
|
*/
|
|
|
|
protected function lookup($array) {
|
|
|
|
$ret = array();
|
|
|
|
foreach ($array as $val) $ret[$val] = true;
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convenience function that creates an HTMLPurifier_ConfigSchema_Interchange_Id
|
|
|
|
* object based on a string Id.
|
|
|
|
*/
|
|
|
|
protected function id($id) {
|
|
|
|
return HTMLPurifier_ConfigSchema_Interchange_Id::make($id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Triggers errors for any unused keys passed in the hash; such keys
|
|
|
|
* may indicate typos, missing values, etc.
|
|
|
|
* @param $hash Instance of ConfigSchema_StringHash to check.
|
|
|
|
*/
|
|
|
|
protected function _findUnused($hash) {
|
|
|
|
$accessed = $hash->getAccessed();
|
|
|
|
foreach ($hash as $k => $v) {
|
|
|
|
if (!isset($accessed[$k])) {
|
|
|
|
trigger_error("String hash key '$k' not used by builder", E_USER_NOTICE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|