1
|
<?php
|
2
|
/*
|
3
|
xmlparse.inc
|
4
|
functions to parse/dump configuration files in XML format
|
5
|
part of m0n0wall (http://m0n0.ch/wall)
|
6
|
|
7
|
Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
|
8
|
All rights reserved.
|
9
|
|
10
|
Redistribution and use in source and binary forms, with or without
|
11
|
modification, are permitted provided that the following conditions are met:
|
12
|
|
13
|
1. Redistributions of source code must retain the above copyright notice,
|
14
|
this list of conditions and the following disclaimer.
|
15
|
|
16
|
2. Redistributions in binary form must reproduce the above copyright
|
17
|
notice, this list of conditions and the following disclaimer in the
|
18
|
documentation and/or other materials provided with the distribution.
|
19
|
|
20
|
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
21
|
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
22
|
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
23
|
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
24
|
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
25
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
26
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
27
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
28
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
29
|
POSSIBILITY OF SUCH DAMAGE.
|
30
|
*/
|
31
|
|
32
|
/* tags that are always to be handled as lists */
|
33
|
$listtags = explode(" ", "row config package columnitem option item fieldname field rule user key subqueue " .
|
34
|
"dnsserver winsserver encryption-algorithm-option hash-algorithm-option hosts tunnel " .
|
35
|
"onetoone staticmap route alias pipe queue shellcmd earlyshellcmd mobilekey " .
|
36
|
"servernat proxyarpnet passthrumac allowedip wolentry vlan");
|
37
|
|
38
|
function startElement($parser, $name, $attrs) {
|
39
|
global $depth, $curpath, $config, $havedata, $listtags;
|
40
|
|
41
|
array_push($curpath, strtolower($name));
|
42
|
|
43
|
$ptr =& $config;
|
44
|
foreach ($curpath as $path) {
|
45
|
$ptr =& $ptr[$path];
|
46
|
}
|
47
|
|
48
|
/* is it an element that belongs to a list? */
|
49
|
if (in_array(strtolower($name), $listtags)) {
|
50
|
|
51
|
/* is there an array already? */
|
52
|
if (!is_array($ptr)) {
|
53
|
/* make an array */
|
54
|
$ptr = array();
|
55
|
}
|
56
|
|
57
|
array_push($curpath, count($ptr));
|
58
|
|
59
|
} else if (isset($ptr)) {
|
60
|
/* multiple entries not allowed for this element, bail out */
|
61
|
die(sprintf("XML error: %s at line %d cannot occur more than once\n",
|
62
|
$name,
|
63
|
xml_get_current_line_number($parser)));
|
64
|
}
|
65
|
|
66
|
$depth++;
|
67
|
$havedata = $depth;
|
68
|
}
|
69
|
|
70
|
function endElement($parser, $name) {
|
71
|
global $depth, $curpath, $config, $havedata, $listtags;
|
72
|
|
73
|
if ($havedata == $depth) {
|
74
|
$ptr =& $config;
|
75
|
foreach ($curpath as $path) {
|
76
|
$ptr =& $ptr[$path];
|
77
|
}
|
78
|
$ptr = "";
|
79
|
}
|
80
|
|
81
|
array_pop($curpath);
|
82
|
|
83
|
if (in_array(strtolower($name), $listtags))
|
84
|
array_pop($curpath);
|
85
|
|
86
|
$depth--;
|
87
|
}
|
88
|
|
89
|
function cData($parser, $data) {
|
90
|
global $depth, $curpath, $config, $havedata;
|
91
|
|
92
|
$data = trim($data, "\t\n\r");
|
93
|
|
94
|
if ($data != "") {
|
95
|
$ptr =& $config;
|
96
|
foreach ($curpath as $path) {
|
97
|
$ptr =& $ptr[$path];
|
98
|
}
|
99
|
|
100
|
if (is_string($ptr)) {
|
101
|
$ptr .= $data;
|
102
|
} else {
|
103
|
if (trim($data, " ") != "") {
|
104
|
$ptr = $data;
|
105
|
$havedata++;
|
106
|
}
|
107
|
}
|
108
|
}
|
109
|
}
|
110
|
|
111
|
function parse_xml_config($cffile, $rootobj) {
|
112
|
|
113
|
global $depth, $curpath, $config, $havedata, $listtags;
|
114
|
|
115
|
$config = array();
|
116
|
$curpath = array();
|
117
|
$depth = 0;
|
118
|
$havedata = 0;
|
119
|
|
120
|
$xml_parser = xml_parser_create();
|
121
|
|
122
|
xml_set_element_handler($xml_parser, "startElement", "endElement");
|
123
|
xml_set_character_data_handler($xml_parser, "cdata");
|
124
|
|
125
|
if (!($fp = fopen($cffile, "r"))) {
|
126
|
die("Error: could not open XML input\n");
|
127
|
}
|
128
|
|
129
|
while ($data = fread($fp, 4096)) {
|
130
|
if (!xml_parse($xml_parser, $data, feof($fp))) {
|
131
|
die(sprintf("XML error: %s at line %d\n",
|
132
|
xml_error_string(xml_get_error_code($xml_parser)),
|
133
|
xml_get_current_line_number($xml_parser)));
|
134
|
}
|
135
|
}
|
136
|
xml_parser_free($xml_parser);
|
137
|
|
138
|
if (!$config[$rootobj]) {
|
139
|
die("XML error: no $rootobj object found!\n");
|
140
|
}
|
141
|
|
142
|
return $config[$rootobj];
|
143
|
}
|
144
|
|
145
|
function dump_xml_config_sub($arr, $indent) {
|
146
|
|
147
|
global $listtags;
|
148
|
|
149
|
$xmlconfig = "";
|
150
|
|
151
|
foreach ($arr as $ent => $val) {
|
152
|
if (is_array($val)) {
|
153
|
/* is it just a list of multiple values? */
|
154
|
if (in_array(strtolower($ent), $listtags)) {
|
155
|
foreach ($val as $cval) {
|
156
|
if (is_array($cval)) {
|
157
|
$xmlconfig .= str_repeat("\t", $indent);
|
158
|
$xmlconfig .= "<$ent>\n";
|
159
|
$xmlconfig .= dump_xml_config_sub($cval, $indent + 1);
|
160
|
$xmlconfig .= str_repeat("\t", $indent);
|
161
|
$xmlconfig .= "</$ent>\n";
|
162
|
} else {
|
163
|
$xmlconfig .= str_repeat("\t", $indent);
|
164
|
if ((is_bool($cval) && ($cval == true)) ||
|
165
|
($cval === ""))
|
166
|
$xmlconfig .= "<$ent/>\n";
|
167
|
else if (!is_bool($cval))
|
168
|
$xmlconfig .= "<$ent>" . htmlspecialchars($cval) . "</$ent>\n";
|
169
|
}
|
170
|
}
|
171
|
} else {
|
172
|
/* it's an array */
|
173
|
$xmlconfig .= str_repeat("\t", $indent);
|
174
|
$xmlconfig .= "<$ent>\n";
|
175
|
$xmlconfig .= dump_xml_config_sub($val, $indent + 1);
|
176
|
$xmlconfig .= str_repeat("\t", $indent);
|
177
|
$xmlconfig .= "</$ent>\n";
|
178
|
}
|
179
|
} else {
|
180
|
if ((is_bool($val) && ($val == true)) || ($val === "")) {
|
181
|
$xmlconfig .= str_repeat("\t", $indent);
|
182
|
$xmlconfig .= "<$ent/>\n";
|
183
|
} else if (!is_bool($val)) {
|
184
|
$xmlconfig .= str_repeat("\t", $indent);
|
185
|
$xmlconfig .= "<$ent>" . htmlspecialchars($val) . "</$ent>\n";
|
186
|
}
|
187
|
}
|
188
|
}
|
189
|
|
190
|
return $xmlconfig;
|
191
|
}
|
192
|
|
193
|
function dump_xml_config($arr, $rootobj) {
|
194
|
|
195
|
$xmlconfig = "<?xml version=\"1.0\"?" . ">\n";
|
196
|
$xmlconfig .= "<$rootobj>\n";
|
197
|
|
198
|
$xmlconfig .= dump_xml_config_sub($arr, 1);
|
199
|
|
200
|
$xmlconfig .= "</$rootobj>\n";
|
201
|
|
202
|
return $xmlconfig;
|
203
|
}
|
204
|
|
205
|
?>
|