2008-01-27 18:50:36 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses string hash files. File format is as such:
|
2008-12-06 07:28:20 +00:00
|
|
|
*
|
2008-01-27 18:50:36 +00:00
|
|
|
* 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
|
2008-03-22 19:30:37 +00:00
|
|
|
* files, but the class itself is usage agnostic.
|
2008-03-23 00:02:37 +00:00
|
|
|
*
|
|
|
|
* You can use ---- to forcibly terminate parsing of a single string-hash;
|
|
|
|
* this marker is used in multi string-hashes to delimit boundaries.
|
2008-01-27 18:50:36 +00:00
|
|
|
*/
|
2008-03-22 19:30:37 +00:00
|
|
|
class HTMLPurifier_StringHashParser
|
2008-01-27 18:50:36 +00:00
|
|
|
{
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-01-27 18:50:36 +00:00
|
|
|
public $default = 'ID';
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-03-23 00:02:37 +00:00
|
|
|
/**
|
|
|
|
* Parses a file that contains a single string-hash.
|
|
|
|
*/
|
2008-01-27 18:50:36 +00:00
|
|
|
public function parseFile($file) {
|
2008-03-22 19:30:37 +00:00
|
|
|
if (!file_exists($file)) return false;
|
2008-02-24 06:19:28 +00:00
|
|
|
$fh = fopen($file, 'r');
|
2008-03-23 00:02:37 +00:00
|
|
|
if (!$fh) return false;
|
|
|
|
$ret = $this->parseHandle($fh);
|
|
|
|
fclose($fh);
|
|
|
|
return $ret;
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-03-23 00:02:37 +00:00
|
|
|
/**
|
|
|
|
* Parses a file that contains multiple string-hashes delimited by '----'
|
|
|
|
*/
|
|
|
|
public function parseMultiFile($file) {
|
|
|
|
if (!file_exists($file)) return false;
|
|
|
|
$ret = array();
|
|
|
|
$fh = fopen($file, 'r');
|
|
|
|
if (!$fh) return false;
|
|
|
|
while (!feof($fh)) {
|
|
|
|
$ret[] = $this->parseHandle($fh);
|
|
|
|
}
|
|
|
|
fclose($fh);
|
|
|
|
return $ret;
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-03-23 00:02:37 +00:00
|
|
|
/**
|
|
|
|
* Internal parser that acepts a file handle.
|
|
|
|
* @note While it's possible to simulate in-memory parsing by using
|
|
|
|
* custom stream wrappers, if such a use-case arises we should
|
|
|
|
* factor out the file handle into its own class.
|
|
|
|
* @param $fh File handle with pointer at start of valid string-hash
|
|
|
|
* block.
|
|
|
|
*/
|
|
|
|
protected function parseHandle($fh) {
|
2008-01-27 18:50:36 +00:00
|
|
|
$state = false;
|
|
|
|
$single = false;
|
|
|
|
$ret = array();
|
2008-03-23 00:02:37 +00:00
|
|
|
do {
|
|
|
|
$line = fgets($fh);
|
|
|
|
if ($line === false) break;
|
2008-01-27 18:50:36 +00:00
|
|
|
$line = rtrim($line, "\n\r");
|
|
|
|
if (!$state && $line === '') continue;
|
2008-03-23 00:02:37 +00:00
|
|
|
if ($line === '----') break;
|
2008-12-06 09:24:59 +00:00
|
|
|
if (strncmp('--#', $line, 3) === 0) {
|
|
|
|
// Comment
|
|
|
|
continue;
|
|
|
|
} elseif (strncmp('--', $line, 2) === 0) {
|
2008-01-27 18:50:36 +00:00
|
|
|
// Multiline declaration
|
|
|
|
$state = trim($line, '- ');
|
2008-07-05 01:26:26 +00:00
|
|
|
if (!isset($ret[$state])) $ret[$state] = '';
|
2008-01-27 18:50:36 +00:00
|
|
|
continue;
|
|
|
|
} elseif (!$state) {
|
|
|
|
$single = true;
|
|
|
|
if (strpos($line, ':') !== false) {
|
|
|
|
// Single-line declaration
|
2008-12-06 07:28:20 +00:00
|
|
|
list($state, $line) = explode(':', $line, 2);
|
|
|
|
$line = trim($line);
|
2008-01-27 18:50:36 +00:00
|
|
|
} else {
|
|
|
|
// Use default declaration
|
|
|
|
$state = $this->default;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($single) {
|
|
|
|
$ret[$state] = $line;
|
|
|
|
$single = false;
|
|
|
|
$state = false;
|
|
|
|
} else {
|
|
|
|
$ret[$state] .= "$line\n";
|
|
|
|
}
|
2008-03-23 00:02:37 +00:00
|
|
|
} while (!feof($fh));
|
2008-01-27 18:50:36 +00:00
|
|
|
return $ret;
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-01-27 18:50:36 +00:00
|
|
|
}
|
2008-12-06 09:24:59 +00:00
|
|
|
|
|
|
|
// vim: et sw=4 sts=4
|