1
|
<?php
|
2
|
/* $Id$ */
|
3
|
/*
|
4
|
xmlparse.inc
|
5
|
functions to parse/dump configuration files in XML format
|
6
|
part of m0n0wall (http://m0n0.ch/wall)
|
7
|
|
8
|
Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
|
9
|
All rights reserved.
|
10
|
|
11
|
Redistribution and use in source and binary forms, with or without
|
12
|
modification, are permitted provided that the following conditions are met:
|
13
|
|
14
|
1. Redistributions of source code must retain the above copyright notice,
|
15
|
this list of conditions and the following disclaimer.
|
16
|
|
17
|
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
|
|
21
|
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
|
/* The following items will be treated as arrays in config.xml */
|
34
|
function listtags() {
|
35
|
$ret = explode(" ", "element alias aliasurl allowedip cacert config columnitem disk dnsserver domainoverrides " .
|
36
|
"earlyshellcmd encryption-algorithm-option field fieldname hash-algorithm-option " .
|
37
|
"hosts group interface_array item key lbpool menu mobilekey mount onetoone option package passthrumac priv proxyarpnet " .
|
38
|
"queue pages pipe route row rule schedule service servernat servers earlyshellcmd shellcmd staticmap subqueue " .
|
39
|
"timerange tunnel user vip virtual_server vlan winsserver ntpserver wolentry widget depends_on_package");
|
40
|
return $ret;
|
41
|
}
|
42
|
|
43
|
/* Package XML tags that should be treat as a list not as a traditional array */
|
44
|
function listtags_pkg() {
|
45
|
$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");
|
46
|
|
47
|
return $ret;
|
48
|
}
|
49
|
|
50
|
function startElement($parser, $name, $attrs) {
|
51
|
global $parsedcfg, $depth, $curpath, $havedata, $listtags;
|
52
|
|
53
|
array_push($curpath, strtolower($name));
|
54
|
|
55
|
$ptr =& $parsedcfg;
|
56
|
foreach ($curpath as $path) {
|
57
|
$ptr =& $ptr[$path];
|
58
|
}
|
59
|
|
60
|
/* is it an element that belongs to a list? */
|
61
|
if (in_array(strtolower($name), $listtags)) {
|
62
|
|
63
|
/* is there an array already? */
|
64
|
if (!is_array($ptr)) {
|
65
|
/* make an array */
|
66
|
$ptr = array();
|
67
|
}
|
68
|
|
69
|
array_push($curpath, count($ptr));
|
70
|
|
71
|
} else if (isset($ptr)) {
|
72
|
/* multiple entries not allowed for this element, bail out */
|
73
|
die(sprintf("XML error: %s at line %d cannot occur more than once\n",
|
74
|
$name,
|
75
|
xml_get_current_line_number($parser)));
|
76
|
}
|
77
|
|
78
|
$depth++;
|
79
|
$havedata = $depth;
|
80
|
}
|
81
|
|
82
|
function endElement($parser, $name) {
|
83
|
global $depth, $curpath, $parsedcfg, $havedata, $listtags;
|
84
|
|
85
|
if ($havedata == $depth) {
|
86
|
$ptr =& $parsedcfg;
|
87
|
foreach ($curpath as $path) {
|
88
|
$ptr =& $ptr[$path];
|
89
|
}
|
90
|
$ptr = "";
|
91
|
}
|
92
|
|
93
|
array_pop($curpath);
|
94
|
|
95
|
if (in_array(strtolower($name), $listtags))
|
96
|
array_pop($curpath);
|
97
|
|
98
|
$depth--;
|
99
|
}
|
100
|
|
101
|
function cData($parser, $data) {
|
102
|
global $depth, $curpath, $parsedcfg, $havedata;
|
103
|
|
104
|
$data = trim($data, "\t\n\r");
|
105
|
|
106
|
if ($data != "") {
|
107
|
$ptr =& $parsedcfg;
|
108
|
foreach ($curpath as $path) {
|
109
|
$ptr =& $ptr[$path];
|
110
|
}
|
111
|
|
112
|
if (is_string($ptr)) {
|
113
|
$ptr .= $data;
|
114
|
} else {
|
115
|
if (trim($data, " ") != "") {
|
116
|
$ptr = $data;
|
117
|
$havedata++;
|
118
|
}
|
119
|
}
|
120
|
}
|
121
|
}
|
122
|
|
123
|
function parse_xml_config($cffile, $rootobj, $isstring = "false") {
|
124
|
global $listtags;
|
125
|
$listtags = listtags();
|
126
|
if (isset($GLOBALS['custom_listtags'])) {
|
127
|
foreach($GLOBALS['custom_listtags'] as $tag) {
|
128
|
$listtags[] = $tag;
|
129
|
}
|
130
|
}
|
131
|
return parse_xml_config_raw($cffile, $rootobj, $isstring);
|
132
|
}
|
133
|
|
134
|
function parse_xml_config_pkg($cffile, $rootobj, $isstring = "false") {
|
135
|
global $listtags;
|
136
|
$listtags = listtags_pkg();
|
137
|
if (isset($GLOBALS['custom_listtags_pkg'])) {
|
138
|
foreach($GLOBALS['custom_listtags_pkg'] as $tag) {
|
139
|
$listtags[] = $tag;
|
140
|
}
|
141
|
}
|
142
|
return parse_xml_config_raw($cffile, $rootobj, $isstring);
|
143
|
}
|
144
|
|
145
|
function parse_xml_config_raw($cffile, $rootobj, $isstring = "false") {
|
146
|
|
147
|
global $depth, $curpath, $parsedcfg, $havedata, $listtags;
|
148
|
$parsedcfg = array();
|
149
|
$curpath = array();
|
150
|
$depth = 0;
|
151
|
$havedata = 0;
|
152
|
|
153
|
$xml_parser = xml_parser_create();
|
154
|
|
155
|
xml_set_element_handler($xml_parser, "startElement", "endElement");
|
156
|
xml_set_character_data_handler($xml_parser, "cdata");
|
157
|
|
158
|
if (!($fp = fopen($cffile, "r"))) {
|
159
|
die("Error: could not open XML input\n");
|
160
|
}
|
161
|
|
162
|
while ($data = fread($fp, 4096)) {
|
163
|
if (!xml_parse($xml_parser, $data, feof($fp))) {
|
164
|
log_error(sprintf("XML error: %s at line %d\n",
|
165
|
xml_error_string(xml_get_error_code($xml_parser)),
|
166
|
xml_get_current_line_number($xml_parser)));
|
167
|
return -1;
|
168
|
}
|
169
|
}
|
170
|
xml_parser_free($xml_parser);
|
171
|
|
172
|
if (!$parsedcfg[$rootobj]) {
|
173
|
die("XML error: no $rootobj object found!\n");
|
174
|
}
|
175
|
|
176
|
return $parsedcfg[$rootobj];
|
177
|
}
|
178
|
|
179
|
function dump_xml_config_sub($arr, $indent) {
|
180
|
|
181
|
global $listtags;
|
182
|
|
183
|
$xmlconfig = "";
|
184
|
|
185
|
foreach ($arr as $ent => $val) {
|
186
|
if (is_array($val)) {
|
187
|
/* is it just a list of multiple values? */
|
188
|
if (in_array(strtolower($ent), $listtags)) {
|
189
|
foreach ($val as $cval) {
|
190
|
if (is_array($cval)) {
|
191
|
$xmlconfig .= str_repeat("\t", $indent);
|
192
|
$xmlconfig .= "<$ent>\n";
|
193
|
$xmlconfig .= dump_xml_config_sub($cval, $indent + 1);
|
194
|
$xmlconfig .= str_repeat("\t", $indent);
|
195
|
$xmlconfig .= "</$ent>\n";
|
196
|
} else {
|
197
|
$xmlconfig .= str_repeat("\t", $indent);
|
198
|
if($cval === false) continue;
|
199
|
if(($cval === true) || ($cval === "")) {
|
200
|
$xmlconfig .= "<$ent/>\n";
|
201
|
} else {
|
202
|
$xmlconfig .= "<$ent>" . htmlspecialchars($cval) . "</$ent>\n";
|
203
|
}
|
204
|
}
|
205
|
}
|
206
|
} else {
|
207
|
/* it's an array */
|
208
|
$xmlconfig .= str_repeat("\t", $indent);
|
209
|
$xmlconfig .= "<$ent>\n";
|
210
|
$xmlconfig .= dump_xml_config_sub($val, $indent + 1);
|
211
|
$xmlconfig .= str_repeat("\t", $indent);
|
212
|
$xmlconfig .= "</$ent>\n";
|
213
|
}
|
214
|
} else {
|
215
|
if ((is_bool($val) && ($val == true)) || ($val === "")) {
|
216
|
$xmlconfig .= str_repeat("\t", $indent);
|
217
|
$xmlconfig .= "<$ent/>\n";
|
218
|
} else if (!is_bool($val)) {
|
219
|
$xmlconfig .= str_repeat("\t", $indent);
|
220
|
$xmlconfig .= "<$ent>" . htmlspecialchars($val) . "</$ent>\n";
|
221
|
}
|
222
|
}
|
223
|
}
|
224
|
|
225
|
return $xmlconfig;
|
226
|
}
|
227
|
|
228
|
function dump_xml_config($arr, $rootobj) {
|
229
|
global $listtags;
|
230
|
$listtags = listtags();
|
231
|
if (isset($GLOBALS['custom_listtags'])) {
|
232
|
foreach($GLOBALS['custom_listtags'] as $tag) {
|
233
|
$listtags[] = $tag;
|
234
|
}
|
235
|
}
|
236
|
return dump_xml_config_raw($arr, $rootobj);
|
237
|
}
|
238
|
|
239
|
function dump_xml_config_pkg($arr, $rootobj) {
|
240
|
global $listtags;
|
241
|
$listtags = listtags_pkg();
|
242
|
if (isset($GLOBALS['custom_listtags_pkg'])) {
|
243
|
foreach($GLOBALS['custom_listtags_pkg'] as $tag) {
|
244
|
$listtags[] = $tag;
|
245
|
}
|
246
|
}
|
247
|
return dump_xml_config_raw($arr, $rootobj);
|
248
|
}
|
249
|
|
250
|
function dump_xml_config_raw($arr, $rootobj) {
|
251
|
|
252
|
$xmlconfig = "<?xml version=\"1.0\"?" . ">\n";
|
253
|
$xmlconfig .= "<$rootobj>\n";
|
254
|
|
255
|
$xmlconfig .= dump_xml_config_sub($arr, 1);
|
256
|
|
257
|
$xmlconfig .= "</$rootobj>\n";
|
258
|
|
259
|
return $xmlconfig;
|
260
|
}
|
261
|
|
262
|
?>
|