1
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2025-07-31 03:10:09 +02:00

Implement ReverseAdapter. Also, debug some order of execution things in the Adapter.

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1532 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang
2008-02-07 17:41:40 +00:00
parent dea117032f
commit 3ba42106ba
5 changed files with 170 additions and 19 deletions

View File

@@ -33,32 +33,33 @@ class ConfigSchema_StringHashAdapter
list($ns, $directive) = explode('.', $hash['ID'], 2);
if (isset($hash['DEFAULT'], $hash['DESCRIPTION'], $hash['TYPE'])) {
if (isset($hash['TYPE'], $hash['DEFAULT'], $hash['DESCRIPTION'])) {
$type = $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);
}
// This must be after ALLOWED
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['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);
list($alias_ns, $alias_directive) = explode('.', $alias, 2);
$schema->addAlias($alias_ns, $alias_directive, $ns, $directive);
}
}

View File

@@ -0,0 +1,89 @@
<?php
/**
* Converts HTMLPurifier_ConfigSchema into a StringHash which can be
* easily saved to a file.
*/
class ConfigSchema_StringHashReverseAdapter
{
protected $schema;
/**
* @param $schema Instance of HTMLPurifier_ConfigSchema to generate
* string hashes from.
*/
public function __construct($schema) {
$this->schema = $schema;
}
/**
* Retrieves a string hash from a specific ID, could be a directive
* or a namespace.
* @param $ns string namespace
* @param $directive string directive name
*/
public function get($ns, $directive = null) {
$ret = array();
if ($directive === null) {
if (!isset($this->schema->info_namespace[$ns])) {
trigger_error("Namespace '$ns' doesn't exist in schema");
return;
}
$def = $this->schema->info_namespace[$ns];
$ret['ID'] = $ns;
$ret['DESCRIPTION'] = $def->description;
return $ret;
}
if (!isset($this->schema->info[$ns][$directive])) {
trigger_error("Directive '$ns.$directive' doesn't exist in schema");
return;
}
$def = $this->schema->info[$ns][$directive];
$ret['ID'] = "$ns.$directive";
$ret['TYPE'] = $def->type;
$ret['DEFAULT'] = $this->export($this->schema->defaults[$ns][$directive]);
$ret['DESCRIPTION'] = $def->description;
if ($def->allowed !== null) {
$ret['ALLOWED'] = $this->exportLookup($def->allowed);
}
if (!empty($def->aliases)) {
$ret['VALUE-ALIASES'] = $this->exportHash($def->aliases);
}
if (!empty($def->directiveAliases)) {
$ret['ALIASES'] = implode(', ', $def->directiveAliases);
}
return $ret;
}
/**
* Exports a variable into a PHP-readable format
*/
protected function export($var) {
return var_export($var, true);
}
/**
* Exports a lookup array into the form 'key1', 'key2', ...
*/
protected function exportLookup($lookup) {
$keys = array_map(array($this, 'export'), array_keys($lookup));
return implode(', ', $keys);
}
/**
* Exports a hash into the form 'key' => 'val',\n ...
*/
protected function exportHash($hash) {
$code = $this->export($hash);
$lines = explode("\n", $code);
$ret = '';
foreach ($lines as $line) {
if ($line == 'array (') continue;
if ($line == ')') continue;
$ret .= substr($line, 2) . "\n";
}
return $ret;
}
}