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