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

[3.1.0] Make StringHash system-agnostic.

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1621 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang
2008-03-22 19:30:37 +00:00
parent ec59062a9d
commit 56cfcba5d1
11 changed files with 12 additions and 63 deletions

View File

@@ -1,46 +0,0 @@
<?php
/**
* Decorator for interchange that performs validations
*/
class HTMLPurifier_ConfigSchema_InterchangeValidator
{
protected $interchange;
public $namespace;
public $directive;
/**
* @param $interchange Instance of HTMLPurifier_ConfigSchema_Interchange
* to save changes to.
*/
public function __construct($interchange) {
$this->interchange = $interchange;
$this->namespace = new HTMLPurifier_ConfigSchema_Validator_Composite();
$this->directive = new HTMLPurifier_ConfigSchema_Validator_Composite();
}
/**
* Registers a HTMLPurifier_ConfigSchema_Validator for both
* directive and namespace
*/
public function addValidator($validator) {
$this->directive->addValidator($validator);
$this->namespace->addValidator($validator);
}
/**
* Validates and adds a namespace hash
*/
public function addNamespace($hash) {
$this->namespace->validate($hash, $this->interchange);
$this->interchange->addNamespace($hash);
}
/**
* Validates and adds a directive hash
*/
public function addDirective($hash) {
$this->directive->validate($hash, $this->interchange);
$this->interchange->addDirective($hash);
}
}

View File

@@ -1,37 +0,0 @@
<?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();
}
}

View File

@@ -1,70 +0,0 @@
<?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 HTMLPurifier_ConfigSchema_Exception('File ' . $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;
}
}