0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2024-12-22 16:31:53 +00:00
htmlpurifier/extras/ConfigSchema/StringHashAdapter.php

84 lines
2.7 KiB
PHP

<?php
/**
* Takes an array of keys to strings, probably generated by
* ConfigSchema_StringHashParser
*/
class ConfigSchema_StringHashAdapter
{
/**
* Takes a string hash and calls the appropriate functions in $schema
* based on its values.
*/
public function adapt($hash, $schema) {
if (! $hash instanceof ConfigSchema_StringHash) {
$hash = new ConfigSchema_StringHash($hash);
}
if (!isset($hash['ID'])) {
trigger_error('Missing key ID in string hash');
return;
}
// Check namespace:
if (strpos($hash['ID'], '.') === false) {
// This will cause problems if we decide to support nested
// namespaces, but for now it's ok.
$schema->addNamespace($hash['ID'], $hash['DESCRIPTION']);
$this->_findUnused($hash);
return;
}
list($ns, $directive) = explode('.', $hash['ID'], 2);
if (isset($hash['DEFAULT'], $hash['DESCRIPTION'], $hash['TYPE'])) {
$raw_default = $hash['DEFAULT'];
$default = eval("return $raw_default;");
$description = $hash['DESCRIPTION'];
$type = $hash['TYPE'];
$schema->add($ns, $directive, $default, $type, $description);
}
if (isset($hash['VALUE-ALIASES'])) {
$raw_value_aliases = $hash['VALUE-ALIASES'];
$value_aliases = eval("return array($raw_value_aliases);");
$schema->addValueAliases($ns, $directive, $value_aliases);
}
if (isset($hash['ALLOWED'])) {
$raw_allowed = $hash['ALLOWED'];
$allowed = eval("return array($raw_allowed);");
$schema->addAllowedValues($ns, $directive, $allowed);
}
if (isset($hash['ALIASES'])) {
$raw_aliases = $hash['ALIASES'];
$aliases = preg_split('/\s*,\s*/', $raw_aliases);
foreach ($aliases as $alias) {
list($new_ns, $new_directive) = explode('.', $alias, 2);
$schema->addAlias($ns, $directive, $new_ns, $new_directive);
}
}
$this->_findUnused($hash);
}
/**
* 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 adapter", E_USER_NOTICE);
}
}
}
}