Add a little bit of documentation about contexts for URIFilters.
[htmlpurifier.git] / library / HTMLPurifier / StringHashParser.php
blobf3e70c712f077cc2c3f0325df1075e1985647b15
1 <?php
3 /**
4 * Parses string hash files. File format is as such:
6 * DefaultKeyValue
7 * KEY: Value
8 * KEY2: Value2
9 * --MULTILINE-KEY--
10 * Multiline
11 * value.
13 * Which would output something similar to:
15 * array(
16 * 'ID' => 'DefaultKeyValue',
17 * 'KEY' => 'Value',
18 * 'KEY2' => 'Value2',
19 * 'MULTILINE-KEY' => "Multiline\nvalue.\n",
20 * )
22 * We use this as an easy to use file-format for configuration schema
23 * files, but the class itself is usage agnostic.
25 * You can use ---- to forcibly terminate parsing of a single string-hash;
26 * this marker is used in multi string-hashes to delimit boundaries.
28 class HTMLPurifier_StringHashParser
31 public $default = 'ID';
33 /**
34 * Parses a file that contains a single string-hash.
36 public function parseFile($file) {
37 if (!file_exists($file)) return false;
38 $fh = fopen($file, 'r');
39 if (!$fh) return false;
40 $ret = $this->parseHandle($fh);
41 fclose($fh);
42 return $ret;
45 /**
46 * Parses a file that contains multiple string-hashes delimited by '----'
48 public function parseMultiFile($file) {
49 if (!file_exists($file)) return false;
50 $ret = array();
51 $fh = fopen($file, 'r');
52 if (!$fh) return false;
53 while (!feof($fh)) {
54 $ret[] = $this->parseHandle($fh);
56 fclose($fh);
57 return $ret;
60 /**
61 * Internal parser that acepts a file handle.
62 * @note While it's possible to simulate in-memory parsing by using
63 * custom stream wrappers, if such a use-case arises we should
64 * factor out the file handle into its own class.
65 * @param $fh File handle with pointer at start of valid string-hash
66 * block.
68 protected function parseHandle($fh) {
69 $state = false;
70 $single = false;
71 $ret = array();
72 do {
73 $line = fgets($fh);
74 if ($line === false) break;
75 $line = rtrim($line, "\n\r");
76 if (!$state && $line === '') continue;
77 if ($line === '----') break;
78 if (strncmp('--#', $line, 3) === 0) {
79 // Comment
80 continue;
81 } elseif (strncmp('--', $line, 2) === 0) {
82 // Multiline declaration
83 $state = trim($line, '- ');
84 if (!isset($ret[$state])) $ret[$state] = '';
85 continue;
86 } elseif (!$state) {
87 $single = true;
88 if (strpos($line, ':') !== false) {
89 // Single-line declaration
90 list($state, $line) = explode(':', $line, 2);
91 $line = trim($line);
92 } else {
93 // Use default declaration
94 $state = $this->default;
97 if ($single) {
98 $ret[$state] = $line;
99 $single = false;
100 $state = false;
101 } else {
102 $ret[$state] .= "$line\n";
104 } while (!feof($fh));
105 return $ret;
110 // vim: et sw=4 sts=4