mirror of
https://github.com/ezyang/htmlpurifier.git
synced 2025-08-03 20:58:11 +02:00
[3.1.0] Move ConfigSchema to HTMLPurifier core
git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1576 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
37
library/HTMLPurifier/ConfigSchema/StringHash.php
Normal file
37
library/HTMLPurifier/ConfigSchema/StringHash.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This is in almost every respect equivalent to an array except
|
||||
* that it keeps track of which keys were accessed.
|
||||
*
|
||||
* @warning For the sake of backwards compatibility with early versions
|
||||
* of PHP 5, you must not use the $hash[$key] syntax; if you do
|
||||
* our version of offsetGet is never called.
|
||||
*/
|
||||
class HTMLPurifier_ConfigSchema_StringHash extends ArrayObject
|
||||
{
|
||||
protected $accessed = array();
|
||||
|
||||
/**
|
||||
* Retrieves a value, and logs the access.
|
||||
*/
|
||||
public function offsetGet($index) {
|
||||
$this->accessed[$index] = true;
|
||||
return parent::offsetGet($index);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a lookup array of all array indexes that have been accessed.
|
||||
* @return Array in form array($index => true).
|
||||
*/
|
||||
public function getAccessed() {
|
||||
return $this->accessed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets the access array.
|
||||
*/
|
||||
public function resetAccessed() {
|
||||
$this->accessed = array();
|
||||
}
|
||||
}
|
89
library/HTMLPurifier/ConfigSchema/StringHashAdapter.php
Normal file
89
library/HTMLPurifier/ConfigSchema/StringHashAdapter.php
Normal file
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Takes an array of keys to strings, probably generated by
|
||||
* HTMLPurifier_ConfigSchema_StringHashParser
|
||||
*/
|
||||
class HTMLPurifier_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 HTMLPurifier_ConfigSchema_StringHash) {
|
||||
$hash = new HTMLPurifier_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->offsetGet('ID'), $hash->offsetGet('DESCRIPTION'));
|
||||
$this->_findUnused($hash);
|
||||
return;
|
||||
}
|
||||
|
||||
list($ns, $directive) = explode('.', $hash->offsetGet('ID'), 2);
|
||||
|
||||
if (isset($hash['TYPE'], $hash['DEFAULT'], $hash['DESCRIPTION'])) {
|
||||
$type = $hash->offsetGet('TYPE');
|
||||
$raw_default = $hash->offsetGet('DEFAULT');
|
||||
$default = eval("return $raw_default;");
|
||||
$description = $hash->offsetGet('DESCRIPTION');
|
||||
$schema->add($ns, $directive, $default, $type, $description);
|
||||
}
|
||||
|
||||
if (isset($hash['ALLOWED'])) {
|
||||
$raw_allowed = $hash->offsetGet('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->offsetGet('VALUE-ALIASES');
|
||||
$value_aliases = eval("return array($raw_value_aliases);");
|
||||
$schema->addValueAliases($ns, $directive, $value_aliases);
|
||||
}
|
||||
|
||||
if (isset($hash['ALIASES'])) {
|
||||
$raw_aliases = trim($hash->offsetGet('ALIASES'));
|
||||
$aliases = preg_split('/\s*,\s*/', $raw_aliases);
|
||||
foreach ($aliases as $alias) {
|
||||
list($alias_ns, $alias_directive) = explode('.', $alias, 2);
|
||||
$schema->addAlias($alias_ns, $alias_directive, $ns, $directive);
|
||||
}
|
||||
}
|
||||
|
||||
// We don't use these yet, but there being used
|
||||
if (isset($hash['VERSION'])) $hash->offsetGet('VERSION');
|
||||
if (isset($hash['DEPRECATED-USE'])) $hash->offsetGet('DEPRECATED-USE');
|
||||
if (isset($hash['DEPRECATED-VERSION'])) $hash->offsetGet('DEPRECATED-VERSION');
|
||||
|
||||
$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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
70
library/HTMLPurifier/ConfigSchema/StringHashParser.php
Normal file
70
library/HTMLPurifier/ConfigSchema/StringHashParser.php
Normal file
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Parses string hash files. File format is as such:
|
||||
*
|
||||
* DefaultKeyValue
|
||||
* KEY: Value
|
||||
* KEY2: Value2
|
||||
* --MULTILINE-KEY--
|
||||
* Multiline
|
||||
* value.
|
||||
*
|
||||
* Which would output something similar to:
|
||||
*
|
||||
* array(
|
||||
* 'ID' => 'DefaultKeyValue',
|
||||
* 'KEY' => 'Value',
|
||||
* 'KEY2' => 'Value2',
|
||||
* 'MULTILINE-KEY' => "Multiline\nvalue.\n",
|
||||
* )
|
||||
*
|
||||
* We use this as an easy to use file-format for configuration schema
|
||||
* files.
|
||||
*
|
||||
* @todo
|
||||
* Put this in its own class hierarchy or something; this class
|
||||
* is usage agnostic.
|
||||
*/
|
||||
class HTMLPurifier_ConfigSchema_StringHashParser
|
||||
{
|
||||
|
||||
public $default = 'ID';
|
||||
|
||||
public function parseFile($file) {
|
||||
if (!file_exists($file)) throw new Exception('File does not exist');
|
||||
$fh = fopen($file, 'r');
|
||||
$state = false;
|
||||
$single = false;
|
||||
$ret = array();
|
||||
while (($line = fgets($fh)) !== false) {
|
||||
$line = rtrim($line, "\n\r");
|
||||
if (!$state && $line === '') continue;
|
||||
if (strncmp('--', $line, 2) === 0) {
|
||||
// Multiline declaration
|
||||
$state = trim($line, '- ');
|
||||
continue;
|
||||
} elseif (!$state) {
|
||||
$single = true;
|
||||
if (strpos($line, ':') !== false) {
|
||||
// Single-line declaration
|
||||
list($state, $line) = explode(': ', $line, 2);
|
||||
} else {
|
||||
// Use default declaration
|
||||
$state = $this->default;
|
||||
}
|
||||
}
|
||||
if ($single) {
|
||||
$ret[$state] = $line;
|
||||
$single = false;
|
||||
$state = false;
|
||||
} else {
|
||||
if (!isset($ret[$state])) $ret[$state] = '';
|
||||
$ret[$state] .= "$line\n";
|
||||
}
|
||||
}
|
||||
fclose($fh);
|
||||
return $ret;
|
||||
}
|
||||
|
||||
}
|
127
library/HTMLPurifier/ConfigSchema/StringHashReverseAdapter.php
Normal file
127
library/HTMLPurifier/ConfigSchema/StringHashReverseAdapter.php
Normal file
@@ -0,0 +1,127 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Converts HTMLPurifier_ConfigSchema into a StringHash which can be
|
||||
* easily saved to a file.
|
||||
*/
|
||||
class HTMLPurifier_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];
|
||||
|
||||
if ($def instanceof HTMLPurifier_ConfigDef_DirectiveAlias) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$ret['ID'] = "$ns.$directive";
|
||||
$ret['TYPE'] = $def->type;
|
||||
|
||||
// Attempt to extract version information from description.
|
||||
$description = $this->normalize($def->description);
|
||||
list($description, $version) = $this->extractVersion($description);
|
||||
|
||||
if ($version) $ret['VERSION'] = $version;
|
||||
$ret['DEFAULT'] = $this->export($this->schema->defaults[$ns][$directive]);
|
||||
$ret['DESCRIPTION'] = wordwrap($description, 75, "\n");
|
||||
|
||||
if ($def->allowed !== true) {
|
||||
$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
|
||||
*/
|
||||
public function export($var) {
|
||||
if ($var === array()) return 'array()'; // single-line format
|
||||
return var_export($var, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Exports a lookup array into the form 'key1', 'key2', ...
|
||||
*/
|
||||
public function exportLookup($lookup) {
|
||||
if (!is_array($lookup)) return $this->export($lookup);
|
||||
if (empty($lookup)) return '';
|
||||
$keys = array_map(array($this, 'export'), array_keys($lookup));
|
||||
return implode(', ', $keys);
|
||||
}
|
||||
|
||||
/**
|
||||
* Exports a hash into the form 'key' => 'val',\n ...
|
||||
*/
|
||||
public function exportHash($hash) {
|
||||
if (!is_array($hash)) return $this->export($hash);
|
||||
if (empty($hash)) return '';
|
||||
$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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalizes a string to Unix style newlines
|
||||
*/
|
||||
protected function normalize($string) {
|
||||
return str_replace(array("\r\n", "\r"), "\n", $string);
|
||||
}
|
||||
|
||||
public function extractVersion($description) {
|
||||
$regex = '/This directive (?:has been|was) available since (\d+\.\d+\.\d+)\./';
|
||||
$regex = str_replace(' ', '\s+', $regex); // allow any number of spaces between statements
|
||||
$ok = preg_match($regex, $description, $matches);
|
||||
if ($ok) {
|
||||
$version = $matches[1];
|
||||
} else {
|
||||
$version = false;
|
||||
}
|
||||
$description = preg_replace($regex, '', $description, 1);
|
||||
return array($description, $version);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user