]> git.immae.eu Git - github/wallabag/wallabag.git/blame - inc/3rdparty/htmlpurifier/HTMLPurifier/StringHashParser.php
remove autoload section in composer.json
[github/wallabag/wallabag.git] / inc / 3rdparty / htmlpurifier / HTMLPurifier / StringHashParser.php
CommitLineData
d4949327
NL
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
28class HTMLPurifier_StringHashParser\r
29{\r
30\r
31 /**\r
32 * @type string\r
33 */\r
34 public $default = 'ID';\r
35\r
36 /**\r
37 * Parses a file that contains a single string-hash.\r
38 * @param string $file\r
39 * @return array\r
40 */\r
41 public function parseFile($file)\r
42 {\r
43 if (!file_exists($file)) {\r
44 return false;\r
45 }\r
46 $fh = fopen($file, 'r');\r
47 if (!$fh) {\r
48 return false;\r
49 }\r
50 $ret = $this->parseHandle($fh);\r
51 fclose($fh);\r
52 return $ret;\r
53 }\r
54\r
55 /**\r
56 * Parses a file that contains multiple string-hashes delimited by '----'\r
57 * @param string $file\r
58 * @return array\r
59 */\r
60 public function parseMultiFile($file)\r
61 {\r
62 if (!file_exists($file)) {\r
63 return false;\r
64 }\r
65 $ret = array();\r
66 $fh = fopen($file, 'r');\r
67 if (!$fh) {\r
68 return false;\r
69 }\r
70 while (!feof($fh)) {\r
71 $ret[] = $this->parseHandle($fh);\r
72 }\r
73 fclose($fh);\r
74 return $ret;\r
75 }\r
76\r
77 /**\r
78 * Internal parser that acepts a file handle.\r
79 * @note While it's possible to simulate in-memory parsing by using\r
80 * custom stream wrappers, if such a use-case arises we should\r
81 * factor out the file handle into its own class.\r
82 * @param resource $fh File handle with pointer at start of valid string-hash\r
83 * block.\r
84 * @return array\r
85 */\r
86 protected function parseHandle($fh)\r
87 {\r
88 $state = false;\r
89 $single = false;\r
90 $ret = array();\r
91 do {\r
92 $line = fgets($fh);\r
93 if ($line === false) {\r
94 break;\r
95 }\r
96 $line = rtrim($line, "\n\r");\r
97 if (!$state && $line === '') {\r
98 continue;\r
99 }\r
100 if ($line === '----') {\r
101 break;\r
102 }\r
103 if (strncmp('--#', $line, 3) === 0) {\r
104 // Comment\r
105 continue;\r
106 } elseif (strncmp('--', $line, 2) === 0) {\r
107 // Multiline declaration\r
108 $state = trim($line, '- ');\r
109 if (!isset($ret[$state])) {\r
110 $ret[$state] = '';\r
111 }\r
112 continue;\r
113 } elseif (!$state) {\r
114 $single = true;\r
115 if (strpos($line, ':') !== false) {\r
116 // Single-line declaration\r
117 list($state, $line) = explode(':', $line, 2);\r
118 $line = trim($line);\r
119 } else {\r
120 // Use default declaration\r
121 $state = $this->default;\r
122 }\r
123 }\r
124 if ($single) {\r
125 $ret[$state] = $line;\r
126 $single = false;\r
127 $state = false;\r
128 } else {\r
129 $ret[$state] .= "$line\n";\r
130 }\r
131 } while (!feof($fh));\r
132 return $ret;\r
133 }\r
134}\r
135\r
136// vim: et sw=4 sts=4\r