2010-09-08 23:14:17 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class HTMLPurifier_ConfigSchema_InterchangeBuilder
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used for processing DEFAULT, nothing else.
|
2016-02-09 05:06:17 -05:00
|
|
|
* @type HTMLPurifier_VarParser
|
2010-09-08 23:14:17 -04:00
|
|
|
*/
|
|
|
|
protected $varParser;
|
|
|
|
|
2016-02-09 05:06:17 -05:00
|
|
|
/**
|
|
|
|
* @param HTMLPurifier_VarParser $varParser
|
|
|
|
*/
|
|
|
|
public function __construct($varParser = null)
|
|
|
|
{
|
2010-09-08 23:14:17 -04:00
|
|
|
$this->varParser = $varParser ? $varParser : new HTMLPurifier_VarParser_Native();
|
|
|
|
}
|
|
|
|
|
2016-02-09 05:06:17 -05:00
|
|
|
/**
|
|
|
|
* @param string $dir
|
|
|
|
* @return HTMLPurifier_ConfigSchema_Interchange
|
|
|
|
*/
|
|
|
|
public static function buildFromDirectory($dir = null)
|
|
|
|
{
|
|
|
|
$builder = new HTMLPurifier_ConfigSchema_InterchangeBuilder();
|
2010-09-08 23:14:17 -04:00
|
|
|
$interchange = new HTMLPurifier_ConfigSchema_Interchange();
|
|
|
|
return $builder->buildDir($interchange, $dir);
|
|
|
|
}
|
|
|
|
|
2016-02-09 05:06:17 -05:00
|
|
|
/**
|
|
|
|
* @param HTMLPurifier_ConfigSchema_Interchange $interchange
|
|
|
|
* @param string $dir
|
|
|
|
* @return HTMLPurifier_ConfigSchema_Interchange
|
|
|
|
*/
|
|
|
|
public function buildDir($interchange, $dir = null)
|
|
|
|
{
|
|
|
|
if (!$dir) {
|
|
|
|
$dir = HTMLPURIFIER_PREFIX . '/HTMLPurifier/ConfigSchema/schema';
|
|
|
|
}
|
2010-09-08 23:14:17 -04:00
|
|
|
if (file_exists($dir . '/info.ini')) {
|
|
|
|
$info = parse_ini_file($dir . '/info.ini');
|
|
|
|
$interchange->name = $info['name'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$files = array();
|
|
|
|
$dh = opendir($dir);
|
|
|
|
while (false !== ($file = readdir($dh))) {
|
|
|
|
if (!$file || $file[0] == '.' || strrchr($file, '.') !== '.txt') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$files[] = $file;
|
|
|
|
}
|
|
|
|
closedir($dh);
|
|
|
|
|
|
|
|
sort($files);
|
|
|
|
foreach ($files as $file) {
|
|
|
|
$this->buildFile($interchange, $dir . '/' . $file);
|
|
|
|
}
|
|
|
|
return $interchange;
|
|
|
|
}
|
|
|
|
|
2016-02-09 05:06:17 -05:00
|
|
|
/**
|
|
|
|
* @param HTMLPurifier_ConfigSchema_Interchange $interchange
|
|
|
|
* @param string $file
|
|
|
|
*/
|
|
|
|
public function buildFile($interchange, $file)
|
|
|
|
{
|
2010-09-08 23:14:17 -04:00
|
|
|
$parser = new HTMLPurifier_StringHashParser();
|
|
|
|
$this->build(
|
|
|
|
$interchange,
|
2016-02-09 05:06:17 -05:00
|
|
|
new HTMLPurifier_StringHash($parser->parseFile($file))
|
2010-09-08 23:14:17 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds an interchange object based on a hash.
|
2016-02-09 05:06:17 -05:00
|
|
|
* @param HTMLPurifier_ConfigSchema_Interchange $interchange HTMLPurifier_ConfigSchema_Interchange object to build
|
|
|
|
* @param HTMLPurifier_StringHash $hash source data
|
|
|
|
* @throws HTMLPurifier_ConfigSchema_Exception
|
2010-09-08 23:14:17 -04:00
|
|
|
*/
|
2016-02-09 05:06:17 -05:00
|
|
|
public function build($interchange, $hash)
|
|
|
|
{
|
2010-09-08 23:14:17 -04: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');
|
|
|
|
}
|
|
|
|
if (strpos($hash['ID'], '.') === false) {
|
|
|
|
if (count($hash) == 2 && isset($hash['DESCRIPTION'])) {
|
|
|
|
$hash->offsetGet('DESCRIPTION'); // prevent complaining
|
|
|
|
} else {
|
|
|
|
throw new HTMLPurifier_ConfigSchema_Exception('All directives must have a namespace');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->buildDirective($interchange, $hash);
|
|
|
|
}
|
|
|
|
$this->_findUnused($hash);
|
|
|
|
}
|
|
|
|
|
2016-02-09 05:06:17 -05:00
|
|
|
/**
|
|
|
|
* @param HTMLPurifier_ConfigSchema_Interchange $interchange
|
|
|
|
* @param HTMLPurifier_StringHash $hash
|
|
|
|
* @throws HTMLPurifier_ConfigSchema_Exception
|
|
|
|
*/
|
|
|
|
public function buildDirective($interchange, $hash)
|
|
|
|
{
|
2010-09-08 23:14:17 -04:00
|
|
|
$directive = new HTMLPurifier_ConfigSchema_Interchange_Directive();
|
|
|
|
|
|
|
|
// These are required elements:
|
|
|
|
$directive->id = $this->id($hash->offsetGet('ID'));
|
|
|
|
$id = $directive->id->toString(); // convenience
|
|
|
|
|
|
|
|
if (isset($hash['TYPE'])) {
|
|
|
|
$type = explode('/', $hash->offsetGet('TYPE'));
|
2016-02-09 05:06:17 -05:00
|
|
|
if (isset($type[1])) {
|
|
|
|
$directive->typeAllowsNull = true;
|
|
|
|
}
|
2010-09-08 23:14:17 -04:00
|
|
|
$directive->type = $type[0];
|
|
|
|
} else {
|
|
|
|
throw new HTMLPurifier_ConfigSchema_Exception("TYPE in directive hash '$id' not defined");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($hash['DEFAULT'])) {
|
|
|
|
try {
|
2016-02-09 05:06:17 -05:00
|
|
|
$directive->default = $this->varParser->parse(
|
|
|
|
$hash->offsetGet('DEFAULT'),
|
|
|
|
$directive->type,
|
|
|
|
$directive->typeAllowsNull
|
|
|
|
);
|
2010-09-08 23:14:17 -04:00
|
|
|
} catch (HTMLPurifier_VarParserException $e) {
|
|
|
|
throw new HTMLPurifier_ConfigSchema_Exception($e->getMessage() . " in DEFAULT in directive hash '$id'");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($hash['DESCRIPTION'])) {
|
|
|
|
$directive->description = $hash->offsetGet('DESCRIPTION');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($hash['ALLOWED'])) {
|
|
|
|
$directive->allowed = $this->lookup($this->evalArray($hash->offsetGet('ALLOWED')));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($hash['VALUE-ALIASES'])) {
|
|
|
|
$directive->valueAliases = $this->evalArray($hash->offsetGet('VALUE-ALIASES'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($hash['ALIASES'])) {
|
|
|
|
$raw_aliases = trim($hash->offsetGet('ALIASES'));
|
|
|
|
$aliases = preg_split('/\s*,\s*/', $raw_aliases);
|
|
|
|
foreach ($aliases as $alias) {
|
|
|
|
$directive->aliases[] = $this->id($alias);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($hash['VERSION'])) {
|
|
|
|
$directive->version = $hash->offsetGet('VERSION');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($hash['DEPRECATED-USE'])) {
|
|
|
|
$directive->deprecatedUse = $this->id($hash->offsetGet('DEPRECATED-USE'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($hash['DEPRECATED-VERSION'])) {
|
|
|
|
$directive->deprecatedVersion = $hash->offsetGet('DEPRECATED-VERSION');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($hash['EXTERNAL'])) {
|
|
|
|
$directive->external = preg_split('/\s*,\s*/', trim($hash->offsetGet('EXTERNAL')));
|
|
|
|
}
|
|
|
|
|
|
|
|
$interchange->addDirective($directive);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Evaluates an array PHP code string without array() wrapper
|
2016-02-09 05:06:17 -05:00
|
|
|
* @param string $contents
|
2010-09-08 23:14:17 -04:00
|
|
|
*/
|
2016-02-09 05:06:17 -05:00
|
|
|
protected function evalArray($contents)
|
|
|
|
{
|
|
|
|
return eval('return array(' . $contents . ');');
|
2010-09-08 23:14:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts an array list into a lookup array.
|
2016-02-09 05:06:17 -05:00
|
|
|
* @param array $array
|
|
|
|
* @return array
|
2010-09-08 23:14:17 -04:00
|
|
|
*/
|
2016-02-09 05:06:17 -05:00
|
|
|
protected function lookup($array)
|
|
|
|
{
|
2010-09-08 23:14:17 -04:00
|
|
|
$ret = array();
|
2016-02-09 05:06:17 -05:00
|
|
|
foreach ($array as $val) {
|
|
|
|
$ret[$val] = true;
|
|
|
|
}
|
2010-09-08 23:14:17 -04:00
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convenience function that creates an HTMLPurifier_ConfigSchema_Interchange_Id
|
|
|
|
* object based on a string Id.
|
2016-02-09 05:06:17 -05:00
|
|
|
* @param string $id
|
|
|
|
* @return HTMLPurifier_ConfigSchema_Interchange_Id
|
2010-09-08 23:14:17 -04:00
|
|
|
*/
|
2016-02-09 05:06:17 -05:00
|
|
|
protected function id($id)
|
|
|
|
{
|
2010-09-08 23:14:17 -04:00
|
|
|
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.
|
2016-02-09 05:06:17 -05:00
|
|
|
* @param HTMLPurifier_StringHash $hash Hash to check.
|
2010-09-08 23:14:17 -04:00
|
|
|
*/
|
2016-02-09 05:06:17 -05:00
|
|
|
protected function _findUnused($hash)
|
|
|
|
{
|
2010-09-08 23:14:17 -04:00
|
|
|
$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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// vim: et sw=4 sts=4
|