1 |
5b237745
|
Scott Ullrich
|
<?php
|
2 |
307cd525
|
Bill Marquette
|
/* $Id$ */
|
3 |
5b237745
|
Scott Ullrich
|
/*
|
4 |
|
|
xmlparse.inc
|
5 |
|
|
functions to parse/dump configuration files in XML format
|
6 |
|
|
part of m0n0wall (http://m0n0.ch/wall)
|
7 |
528ae96e
|
Scott Ullrich
|
|
8 |
5b237745
|
Scott Ullrich
|
Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
|
9 |
|
|
All rights reserved.
|
10 |
528ae96e
|
Scott Ullrich
|
|
11 |
5b237745
|
Scott Ullrich
|
Redistribution and use in source and binary forms, with or without
|
12 |
|
|
modification, are permitted provided that the following conditions are met:
|
13 |
528ae96e
|
Scott Ullrich
|
|
14 |
5b237745
|
Scott Ullrich
|
1. Redistributions of source code must retain the above copyright notice,
|
15 |
|
|
this list of conditions and the following disclaimer.
|
16 |
528ae96e
|
Scott Ullrich
|
|
17 |
5b237745
|
Scott Ullrich
|
2. Redistributions in binary form must reproduce the above copyright
|
18 |
|
|
notice, this list of conditions and the following disclaimer in the
|
19 |
|
|
documentation and/or other materials provided with the distribution.
|
20 |
528ae96e
|
Scott Ullrich
|
|
21 |
5b237745
|
Scott Ullrich
|
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
22 |
|
|
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
23 |
|
|
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
24 |
|
|
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
25 |
|
|
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
26 |
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
27 |
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
28 |
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
29 |
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
30 |
|
|
POSSIBILITY OF SUCH DAMAGE.
|
31 |
|
|
*/
|
32 |
|
|
|
33 |
2e5ebf5d
|
Scott Ullrich
|
/* The following items will be treated as arrays in config.xml */
|
34 |
eeb6c16e
|
Bill Marquette
|
function listtags() {
|
35 |
0919224f
|
Bill Marquette
|
/* Please keep this list alpha sorted and no longer than 80 characters
|
36 |
|
|
* I know it's a pain, but it's a pain to find stuff too if it's not
|
37 |
|
|
*/
|
38 |
64cc39d3
|
Matthew Grooms
|
$ret = explode(" ",
|
39 |
f63d5b66
|
Helder Pereira
|
"alias aliasurl allowedip authserver bridged ca cacert cert config container ".
|
40 |
0919224f
|
Bill Marquette
|
"columnitem depends_on_package disk dnsserver dnsupdate domainoverrides ".
|
41 |
|
|
"dyndns earlyshellcmd element encryption-algorithm-option field ".
|
42 |
|
|
"fieldname hash-algorithm-option gateway_item gateway_group gif gre ".
|
43 |
0ec2fdf0
|
Ermal Lu?i
|
"group hosts member ifgroupentry igmpentry interface_array item key lagg " .
|
44 |
|
|
"lbaction lbpool l7rules lbprotocol ".
|
45 |
|
|
"member menu tab mobilekey monitor_type mount ntpserver onetoone ".
|
46 |
b6617403
|
Matthew Grooms
|
"openvpn-server openvpn-client openvpn-csc " .
|
47 |
5f1e1d26
|
Ermal Lu?i
|
"option ppp package passthrumac phase1 phase2 priv proxyarpnet qinqentry queue ".
|
48 |
336e3c1c
|
Charlie
|
"pages pipe roll route row rrddatafile rule schedule service servernat servers ".
|
49 |
0919224f
|
Bill Marquette
|
"serversdisabled earlyshellcmd shellcmd staticmap subqueue timerange ".
|
50 |
5e7d127a
|
Scott Ullrich
|
"tunnel user vip virtual_server vlan winsserver wolentry widget "
|
51 |
0919224f
|
Bill Marquette
|
);
|
52 |
eeb6c16e
|
Bill Marquette
|
return $ret;
|
53 |
|
|
}
|
54 |
|
|
|
55 |
2fe045e5
|
Bill Marquette
|
/* Package XML tags that should be treat as a list not as a traditional array */
|
56 |
eeb6c16e
|
Bill Marquette
|
function listtags_pkg() {
|
57 |
e1b5fccb
|
Scott Ullrich
|
$ret = array("depends_on_package", "onetoone", "queue", "rule", "servernat", "alias", "additional_files_needed", "tab", "template", "menu", "rowhelperfield", "service", "step", "package", "columnitem", "option", "item", "field", "package", "file");
|
58 |
eeb6c16e
|
Bill Marquette
|
|
59 |
|
|
return $ret;
|
60 |
|
|
}
|
61 |
|
|
|
62 |
5b237745
|
Scott Ullrich
|
function startElement($parser, $name, $attrs) {
|
63 |
08c3872d
|
Colin Smith
|
global $parsedcfg, $depth, $curpath, $havedata, $listtags;
|
64 |
528ae96e
|
Scott Ullrich
|
|
65 |
5b237745
|
Scott Ullrich
|
array_push($curpath, strtolower($name));
|
66 |
528ae96e
|
Scott Ullrich
|
|
67 |
eba0b1d8
|
Colin Smith
|
$ptr =& $parsedcfg;
|
68 |
5b237745
|
Scott Ullrich
|
foreach ($curpath as $path) {
|
69 |
|
|
$ptr =& $ptr[$path];
|
70 |
|
|
}
|
71 |
528ae96e
|
Scott Ullrich
|
|
72 |
5b237745
|
Scott Ullrich
|
/* is it an element that belongs to a list? */
|
73 |
|
|
if (in_array(strtolower($name), $listtags)) {
|
74 |
528ae96e
|
Scott Ullrich
|
|
75 |
5b237745
|
Scott Ullrich
|
/* is there an array already? */
|
76 |
|
|
if (!is_array($ptr)) {
|
77 |
|
|
/* make an array */
|
78 |
|
|
$ptr = array();
|
79 |
|
|
}
|
80 |
528ae96e
|
Scott Ullrich
|
|
81 |
5b237745
|
Scott Ullrich
|
array_push($curpath, count($ptr));
|
82 |
528ae96e
|
Scott Ullrich
|
|
83 |
5b237745
|
Scott Ullrich
|
} else if (isset($ptr)) {
|
84 |
|
|
/* multiple entries not allowed for this element, bail out */
|
85 |
|
|
die(sprintf("XML error: %s at line %d cannot occur more than once\n",
|
86 |
|
|
$name,
|
87 |
|
|
xml_get_current_line_number($parser)));
|
88 |
|
|
}
|
89 |
528ae96e
|
Scott Ullrich
|
|
90 |
5b237745
|
Scott Ullrich
|
$depth++;
|
91 |
|
|
$havedata = $depth;
|
92 |
|
|
}
|
93 |
|
|
|
94 |
|
|
function endElement($parser, $name) {
|
95 |
08c3872d
|
Colin Smith
|
global $depth, $curpath, $parsedcfg, $havedata, $listtags;
|
96 |
528ae96e
|
Scott Ullrich
|
|
97 |
5b237745
|
Scott Ullrich
|
if ($havedata == $depth) {
|
98 |
08c3872d
|
Colin Smith
|
$ptr =& $parsedcfg;
|
99 |
5b237745
|
Scott Ullrich
|
foreach ($curpath as $path) {
|
100 |
|
|
$ptr =& $ptr[$path];
|
101 |
|
|
}
|
102 |
|
|
$ptr = "";
|
103 |
|
|
}
|
104 |
528ae96e
|
Scott Ullrich
|
|
105 |
5b237745
|
Scott Ullrich
|
array_pop($curpath);
|
106 |
|
|
|
107 |
|
|
if (in_array(strtolower($name), $listtags))
|
108 |
|
|
array_pop($curpath);
|
109 |
528ae96e
|
Scott Ullrich
|
|
110 |
5b237745
|
Scott Ullrich
|
$depth--;
|
111 |
|
|
}
|
112 |
|
|
|
113 |
|
|
function cData($parser, $data) {
|
114 |
08c3872d
|
Colin Smith
|
global $depth, $curpath, $parsedcfg, $havedata;
|
115 |
528ae96e
|
Scott Ullrich
|
|
116 |
5b237745
|
Scott Ullrich
|
$data = trim($data, "\t\n\r");
|
117 |
528ae96e
|
Scott Ullrich
|
|
118 |
5b237745
|
Scott Ullrich
|
if ($data != "") {
|
119 |
08c3872d
|
Colin Smith
|
$ptr =& $parsedcfg;
|
120 |
5b237745
|
Scott Ullrich
|
foreach ($curpath as $path) {
|
121 |
|
|
$ptr =& $ptr[$path];
|
122 |
|
|
}
|
123 |
|
|
|
124 |
|
|
if (is_string($ptr)) {
|
125 |
|
|
$ptr .= $data;
|
126 |
|
|
} else {
|
127 |
|
|
if (trim($data, " ") != "") {
|
128 |
|
|
$ptr = $data;
|
129 |
|
|
$havedata++;
|
130 |
|
|
}
|
131 |
|
|
}
|
132 |
|
|
}
|
133 |
|
|
}
|
134 |
|
|
|
135 |
48ca2dc9
|
Colin Smith
|
function parse_xml_config($cffile, $rootobj, $isstring = "false") {
|
136 |
cb980a2f
|
Colin Smith
|
global $listtags;
|
137 |
eeb6c16e
|
Bill Marquette
|
$listtags = listtags();
|
138 |
eb72ceda
|
Scott Ullrich
|
if (isset($GLOBALS['custom_listtags'])) {
|
139 |
|
|
foreach($GLOBALS['custom_listtags'] as $tag) {
|
140 |
|
|
$listtags[] = $tag;
|
141 |
|
|
}
|
142 |
|
|
}
|
143 |
48ca2dc9
|
Colin Smith
|
return parse_xml_config_raw($cffile, $rootobj, $isstring);
|
144 |
cb980a2f
|
Colin Smith
|
}
|
145 |
|
|
|
146 |
48ca2dc9
|
Colin Smith
|
function parse_xml_config_pkg($cffile, $rootobj, $isstring = "false") {
|
147 |
cb980a2f
|
Colin Smith
|
global $listtags;
|
148 |
eeb6c16e
|
Bill Marquette
|
$listtags = listtags_pkg();
|
149 |
eb72ceda
|
Scott Ullrich
|
if (isset($GLOBALS['custom_listtags_pkg'])) {
|
150 |
|
|
foreach($GLOBALS['custom_listtags_pkg'] as $tag) {
|
151 |
|
|
$listtags[] = $tag;
|
152 |
|
|
}
|
153 |
|
|
}
|
154 |
48ca2dc9
|
Colin Smith
|
return parse_xml_config_raw($cffile, $rootobj, $isstring);
|
155 |
cb980a2f
|
Colin Smith
|
}
|
156 |
|
|
|
157 |
48ca2dc9
|
Colin Smith
|
function parse_xml_config_raw($cffile, $rootobj, $isstring = "false") {
|
158 |
5b237745
|
Scott Ullrich
|
|
159 |
08c3872d
|
Colin Smith
|
global $depth, $curpath, $parsedcfg, $havedata, $listtags;
|
160 |
|
|
$parsedcfg = array();
|
161 |
5b237745
|
Scott Ullrich
|
$curpath = array();
|
162 |
|
|
$depth = 0;
|
163 |
|
|
$havedata = 0;
|
164 |
528ae96e
|
Scott Ullrich
|
|
165 |
5b237745
|
Scott Ullrich
|
$xml_parser = xml_parser_create();
|
166 |
528ae96e
|
Scott Ullrich
|
|
167 |
5b237745
|
Scott Ullrich
|
xml_set_element_handler($xml_parser, "startElement", "endElement");
|
168 |
|
|
xml_set_character_data_handler($xml_parser, "cdata");
|
169 |
528ae96e
|
Scott Ullrich
|
|
170 |
5b237745
|
Scott Ullrich
|
if (!($fp = fopen($cffile, "r"))) {
|
171 |
|
|
die("Error: could not open XML input\n");
|
172 |
|
|
}
|
173 |
528ae96e
|
Scott Ullrich
|
|
174 |
5b237745
|
Scott Ullrich
|
while ($data = fread($fp, 4096)) {
|
175 |
|
|
if (!xml_parse($xml_parser, $data, feof($fp))) {
|
176 |
2c1689fd
|
Scott Ullrich
|
log_error(sprintf("XML error: %s at line %d\n",
|
177 |
5b237745
|
Scott Ullrich
|
xml_error_string(xml_get_error_code($xml_parser)),
|
178 |
|
|
xml_get_current_line_number($xml_parser)));
|
179 |
2c1689fd
|
Scott Ullrich
|
return -1;
|
180 |
5b237745
|
Scott Ullrich
|
}
|
181 |
|
|
}
|
182 |
|
|
xml_parser_free($xml_parser);
|
183 |
528ae96e
|
Scott Ullrich
|
|
184 |
08c3872d
|
Colin Smith
|
if (!$parsedcfg[$rootobj]) {
|
185 |
5b237745
|
Scott Ullrich
|
die("XML error: no $rootobj object found!\n");
|
186 |
|
|
}
|
187 |
528ae96e
|
Scott Ullrich
|
|
188 |
08c3872d
|
Colin Smith
|
return $parsedcfg[$rootobj];
|
189 |
5b237745
|
Scott Ullrich
|
}
|
190 |
|
|
|
191 |
|
|
function dump_xml_config_sub($arr, $indent) {
|
192 |
|
|
|
193 |
|
|
global $listtags;
|
194 |
528ae96e
|
Scott Ullrich
|
|
195 |
5b237745
|
Scott Ullrich
|
$xmlconfig = "";
|
196 |
|
|
|
197 |
|
|
foreach ($arr as $ent => $val) {
|
198 |
|
|
if (is_array($val)) {
|
199 |
|
|
/* is it just a list of multiple values? */
|
200 |
|
|
if (in_array(strtolower($ent), $listtags)) {
|
201 |
|
|
foreach ($val as $cval) {
|
202 |
|
|
if (is_array($cval)) {
|
203 |
|
|
$xmlconfig .= str_repeat("\t", $indent);
|
204 |
|
|
$xmlconfig .= "<$ent>\n";
|
205 |
|
|
$xmlconfig .= dump_xml_config_sub($cval, $indent + 1);
|
206 |
|
|
$xmlconfig .= str_repeat("\t", $indent);
|
207 |
|
|
$xmlconfig .= "</$ent>\n";
|
208 |
|
|
} else {
|
209 |
|
|
$xmlconfig .= str_repeat("\t", $indent);
|
210 |
0d57f5f3
|
Scott Ullrich
|
if($cval === false) continue;
|
211 |
|
|
if(($cval === true) || ($cval === "")) {
|
212 |
5b237745
|
Scott Ullrich
|
$xmlconfig .= "<$ent/>\n";
|
213 |
0d57f5f3
|
Scott Ullrich
|
} else {
|
214 |
5b237745
|
Scott Ullrich
|
$xmlconfig .= "<$ent>" . htmlspecialchars($cval) . "</$ent>\n";
|
215 |
|
|
}
|
216 |
|
|
}
|
217 |
0d57f5f3
|
Scott Ullrich
|
}
|
218 |
5b237745
|
Scott Ullrich
|
} else {
|
219 |
|
|
/* it's an array */
|
220 |
|
|
$xmlconfig .= str_repeat("\t", $indent);
|
221 |
|
|
$xmlconfig .= "<$ent>\n";
|
222 |
|
|
$xmlconfig .= dump_xml_config_sub($val, $indent + 1);
|
223 |
|
|
$xmlconfig .= str_repeat("\t", $indent);
|
224 |
|
|
$xmlconfig .= "</$ent>\n";
|
225 |
|
|
}
|
226 |
|
|
} else {
|
227 |
|
|
if ((is_bool($val) && ($val == true)) || ($val === "")) {
|
228 |
|
|
$xmlconfig .= str_repeat("\t", $indent);
|
229 |
|
|
$xmlconfig .= "<$ent/>\n";
|
230 |
|
|
} else if (!is_bool($val)) {
|
231 |
|
|
$xmlconfig .= str_repeat("\t", $indent);
|
232 |
|
|
$xmlconfig .= "<$ent>" . htmlspecialchars($val) . "</$ent>\n";
|
233 |
|
|
}
|
234 |
|
|
}
|
235 |
|
|
}
|
236 |
528ae96e
|
Scott Ullrich
|
|
237 |
5b237745
|
Scott Ullrich
|
return $xmlconfig;
|
238 |
|
|
}
|
239 |
|
|
|
240 |
|
|
function dump_xml_config($arr, $rootobj) {
|
241 |
ba6882bf
|
Colin Smith
|
global $listtags;
|
242 |
eeb6c16e
|
Bill Marquette
|
$listtags = listtags();
|
243 |
4928848f
|
Scott Ullrich
|
if (isset($GLOBALS['custom_listtags'])) {
|
244 |
|
|
foreach($GLOBALS['custom_listtags'] as $tag) {
|
245 |
|
|
$listtags[] = $tag;
|
246 |
|
|
}
|
247 |
|
|
}
|
248 |
ba6882bf
|
Colin Smith
|
return dump_xml_config_raw($arr, $rootobj);
|
249 |
|
|
}
|
250 |
|
|
|
251 |
|
|
function dump_xml_config_pkg($arr, $rootobj) {
|
252 |
|
|
global $listtags;
|
253 |
eeb6c16e
|
Bill Marquette
|
$listtags = listtags_pkg();
|
254 |
4928848f
|
Scott Ullrich
|
if (isset($GLOBALS['custom_listtags_pkg'])) {
|
255 |
|
|
foreach($GLOBALS['custom_listtags_pkg'] as $tag) {
|
256 |
|
|
$listtags[] = $tag;
|
257 |
|
|
}
|
258 |
|
|
}
|
259 |
ba6882bf
|
Colin Smith
|
return dump_xml_config_raw($arr, $rootobj);
|
260 |
|
|
}
|
261 |
|
|
|
262 |
|
|
function dump_xml_config_raw($arr, $rootobj) {
|
263 |
5b237745
|
Scott Ullrich
|
|
264 |
|
|
$xmlconfig = "<?xml version=\"1.0\"?" . ">\n";
|
265 |
|
|
$xmlconfig .= "<$rootobj>\n";
|
266 |
528ae96e
|
Scott Ullrich
|
|
267 |
5b237745
|
Scott Ullrich
|
$xmlconfig .= dump_xml_config_sub($arr, 1);
|
268 |
528ae96e
|
Scott Ullrich
|
|
269 |
5b237745
|
Scott Ullrich
|
$xmlconfig .= "</$rootobj>\n";
|
270 |
528ae96e
|
Scott Ullrich
|
|
271 |
5b237745
|
Scott Ullrich
|
return $xmlconfig;
|
272 |
|
|
}
|
273 |
|
|
|
274 |
d173230c
|
Seth Mos
|
?>
|