1 |
26433cb8
|
Scott Ullrich
|
<?php
|
2 |
|
|
/*
|
3 |
ce77a9c4
|
Phil Davis
|
xmlreader.inc
|
4 |
26433cb8
|
Scott Ullrich
|
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 |
|
|
/*
|
33 |
|
|
pfSense_MODULE: utils
|
34 |
|
|
*/
|
35 |
|
|
|
36 |
|
|
/* The following items will be treated as arrays in config.xml */
|
37 |
|
|
function listtags() {
|
38 |
72a8c829
|
Ermal
|
/*
|
39 |
79262830
|
Phil Davis
|
* Please keep this list alpha sorted and no longer than 80 characters
|
40 |
|
|
* I know it's a pain, but it's a pain to find stuff too if it's not
|
41 |
|
|
*/
|
42 |
b79ea46a
|
Ermal
|
$ret = array(
|
43 |
72a8c829
|
Ermal
|
'acls', 'alias', 'aliasurl', 'allowedip', 'allowedhostname', 'authserver',
|
44 |
|
|
'bridged', 'build_port_path',
|
45 |
|
|
'ca', 'cacert', 'cert', 'crl', 'clone', 'config', 'container', 'columnitem',
|
46 |
|
|
'depends_on_package', 'disk', 'dnsserver', 'dnsupdate', 'domainoverrides', 'dyndns',
|
47 |
|
|
'earlyshellcmd', 'element', 'encryption-algorithm-option',
|
48 |
|
|
'field', 'fieldname',
|
49 |
|
|
'gateway_item', 'gateway_group', 'gif', 'gre', 'group',
|
50 |
|
|
'hash-algorithm-option', 'hosts', 'member', 'ifgroupentry', 'igmpentry', 'interface_array', 'item', 'key',
|
51 |
|
|
'lagg', 'lbaction', 'lbpool', 'l7rules', 'lbprotocol',
|
52 |
|
|
'member', 'menu', 'tab', 'mobilekey', 'monitor_type', 'mount',
|
53 |
|
|
'npt', 'ntpserver',
|
54 |
|
|
'onetoone', 'openvpn-server', 'openvpn-client', 'openvpn-csc', 'option',
|
55 |
|
|
'package', 'passthrumac', 'phase1', 'phase2', 'ppp', 'pppoe', 'priv', 'proxyarpnet', 'pool',
|
56 |
|
|
'qinqentry', 'queue',
|
57 |
|
|
'pages', 'pipe', 'radnsserver', 'roll', 'route', 'row', 'rrddatafile', 'rule',
|
58 |
|
|
'schedule', 'service', 'servernat', 'servers',
|
59 |
|
|
'serversdisabled', 'shellcmd', 'staticmap', 'subqueue',
|
60 |
|
|
'timerange', 'tunnel', 'user', 'vip', 'virtual_server', 'vlan',
|
61 |
|
|
'winsserver', 'wolentry', 'widget'
|
62 |
|
|
);
|
63 |
26433cb8
|
Scott Ullrich
|
return array_flip($ret);
|
64 |
|
|
}
|
65 |
|
|
|
66 |
|
|
/* Package XML tags that should be treat as a list not as a traditional array */
|
67 |
|
|
function listtags_pkg() {
|
68 |
b79ea46a
|
Ermal
|
$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');
|
69 |
26433cb8
|
Scott Ullrich
|
|
70 |
|
|
return array_flip($ret);
|
71 |
|
|
}
|
72 |
|
|
|
73 |
|
|
function add_elements(&$cfgarray, &$parser) {
|
74 |
79262830
|
Phil Davis
|
global $listtags;
|
75 |
|
|
|
76 |
|
|
while ($parser->read()) {
|
77 |
|
|
switch ($parser->nodeType) {
|
78 |
|
|
case XMLReader::WHITESPACE:
|
79 |
|
|
case XMLReader::SIGNIFICANT_WHITESPACE:
|
80 |
|
|
break;
|
81 |
|
|
case XMLReader::ELEMENT:
|
82 |
|
|
if (isset($listtags[strtolower($parser->name)])) {
|
83 |
|
|
$cfgref =& $cfgarray[$parser->name][count($cfgarray[$parser->name])];
|
84 |
|
|
if (!$parser->isEmptyElement) {
|
85 |
ddfe5e43
|
Ermal
|
add_elements($cfgref, $parser);
|
86 |
79262830
|
Phil Davis
|
} else {
|
87 |
|
|
$cfgref = array();
|
88 |
|
|
}
|
89 |
|
|
} else {
|
90 |
|
|
if (isset($cfgarray[$parser->name]) && (!is_array($cfgarray[$parser->name]) || !isset($cfgarray[$parser->name][0]))) {
|
91 |
|
|
$nodebkp = $cfgarray[$parser->name];
|
92 |
|
|
$cfgarray[$parser->name] = array();
|
93 |
|
|
$cfgarray[$parser->name][] = $nodebkp;
|
94 |
|
|
$cfgref =& $cfgarray[$parser->name][0];
|
95 |
|
|
unset($nodebkp);
|
96 |
|
|
} else {
|
97 |
|
|
$cfgref =& $cfgarray[$parser->name];
|
98 |
|
|
}
|
99 |
|
|
|
100 |
|
|
if ($parser->isEmptyElement) {
|
101 |
|
|
if (is_array($cfgref)) {
|
102 |
|
|
$cfgref[] = array();
|
103 |
|
|
} else {
|
104 |
|
|
$cfgref = "";
|
105 |
|
|
}
|
106 |
|
|
} else {
|
107 |
|
|
if (is_array($cfgref)) {
|
108 |
|
|
$cfgref =& $cfgarray[$parser->name][count($cfgarray[$parser->name])];
|
109 |
|
|
add_elements($cfgref, $parser);
|
110 |
|
|
} else {
|
111 |
|
|
add_elements($cfgref, $parser);
|
112 |
|
|
}
|
113 |
|
|
}
|
114 |
ddfe5e43
|
Ermal
|
}
|
115 |
8ffb5ccd
|
Ermal
|
|
116 |
79262830
|
Phil Davis
|
$i = 0;
|
117 |
|
|
while ($parser->moveToAttributeNo($i)) {
|
118 |
|
|
$cfgref[$parser->name] = $parser->value;
|
119 |
|
|
$i++;
|
120 |
|
|
}
|
121 |
|
|
break;
|
122 |
|
|
case XMLReader::TEXT:
|
123 |
|
|
case XMLReader::CDATA:
|
124 |
|
|
$cfgarray = $parser->value;
|
125 |
|
|
break;
|
126 |
|
|
case XMLReader::END_ELEMENT:
|
127 |
|
|
return;
|
128 |
|
|
break;
|
129 |
|
|
default:
|
130 |
|
|
break;
|
131 |
|
|
}
|
132 |
ab83fce0
|
Ermal
|
}
|
133 |
26433cb8
|
Scott Ullrich
|
}
|
134 |
|
|
|
135 |
|
|
function parse_xml_config($cffile, $rootobj, $isstring = "false") {
|
136 |
|
|
global $listtags;
|
137 |
b79ea46a
|
Ermal
|
|
138 |
26433cb8
|
Scott Ullrich
|
$listtags = listtags();
|
139 |
79262830
|
Phil Davis
|
if (isset($GLOBALS['custom_listtags'])) {
|
140 |
|
|
foreach ($GLOBALS['custom_listtags'] as $tag) {
|
141 |
|
|
$listtags[$tag] = $tag;
|
142 |
|
|
}
|
143 |
|
|
}
|
144 |
ab83fce0
|
Ermal
|
|
145 |
26433cb8
|
Scott Ullrich
|
return parse_xml_config_raw($cffile, $rootobj);
|
146 |
|
|
}
|
147 |
|
|
|
148 |
|
|
function parse_xml_config_pkg($cffile, $rootobj, $isstring = "false") {
|
149 |
|
|
global $listtags;
|
150 |
b79ea46a
|
Ermal
|
|
151 |
26433cb8
|
Scott Ullrich
|
$listtags = listtags_pkg();
|
152 |
79262830
|
Phil Davis
|
if (isset($GLOBALS['custom_listtags_pkg'])) {
|
153 |
|
|
foreach ($GLOBALS['custom_listtags_pkg'] as $tag) {
|
154 |
|
|
$listtags[$tag] = $tag;
|
155 |
|
|
}
|
156 |
|
|
}
|
157 |
26433cb8
|
Scott Ullrich
|
return parse_xml_config_raw($cffile, $rootobj, $isstring);
|
158 |
|
|
}
|
159 |
|
|
|
160 |
|
|
function parse_xml_config_raw($cffile, $rootobj, $isstring = "false") {
|
161 |
b79ea46a
|
Ermal
|
global $listtags;
|
162 |
26433cb8
|
Scott Ullrich
|
|
163 |
|
|
$parsedcfg = array();
|
164 |
|
|
|
165 |
|
|
$par = new XMLReader();
|
166 |
ab83fce0
|
Ermal
|
if ($par->open($cffile, "UTF-8", LIBXML_NOERROR | LIBXML_NOWARNING)) {
|
167 |
26433cb8
|
Scott Ullrich
|
add_elements($parsedcfg, $par);
|
168 |
|
|
$par->close();
|
169 |
79262830
|
Phil Davis
|
} else {
|
170 |
5cfa35df
|
Renato Botelho
|
log_error(sprintf(gettext("Error returned while trying to parse %s"), $cffile));
|
171 |
79262830
|
Phil Davis
|
}
|
172 |
26433cb8
|
Scott Ullrich
|
|
173 |
428c289f
|
Darren Embry
|
if ($rootobj) {
|
174 |
79262830
|
Phil Davis
|
if (!is_array($rootobj)) {
|
175 |
428c289f
|
Darren Embry
|
$rootobj = array($rootobj);
|
176 |
79262830
|
Phil Davis
|
}
|
177 |
|
|
foreach ($rootobj as $rootobj_name) {
|
178 |
|
|
if ($parsedcfg[$rootobj_name]) {
|
179 |
428c289f
|
Darren Embry
|
break;
|
180 |
79262830
|
Phil Davis
|
}
|
181 |
|
|
}
|
182 |
|
|
|
183 |
428c289f
|
Darren Embry
|
return $parsedcfg[$rootobj_name];
|
184 |
|
|
} else {
|
185 |
|
|
return $parsedcfg;
|
186 |
|
|
}
|
187 |
26433cb8
|
Scott Ullrich
|
}
|
188 |
|
|
|
189 |
ab83fce0
|
Ermal
|
function dump_xml_config_sub(& $writer, $arr) {
|
190 |
79262830
|
Phil Davis
|
global $listtags;
|
191 |
|
|
|
192 |
|
|
foreach ($arr as $ent => $val) {
|
193 |
|
|
if (is_array($val)) {
|
194 |
|
|
/* is it just a list of multiple values? */
|
195 |
|
|
if (isset($listtags[strtolower($ent)])) {
|
196 |
|
|
foreach ($val as $cval) {
|
197 |
|
|
if (is_array($cval)) {
|
198 |
|
|
if (empty($cval)) {
|
199 |
|
|
$writer->writeElement($ent);
|
200 |
|
|
} else {
|
201 |
|
|
$writer->startElement($ent);
|
202 |
|
|
dump_xml_config_sub($writer, $cval);
|
203 |
|
|
$writer->endElement();
|
204 |
|
|
}
|
205 |
|
|
} else {
|
206 |
|
|
if ($cval === false) {
|
207 |
|
|
continue;
|
208 |
|
|
}
|
209 |
|
|
if ((is_bool($val) && ($val == true)) || ($val === "")) {
|
210 |
|
|
$writer->writeElement($ent);
|
211 |
|
|
} else if (!is_bool($val)) {
|
212 |
ab83fce0
|
Ermal
|
$writer->writeElement($ent, $cval);
|
213 |
79262830
|
Phil Davis
|
}
|
214 |
|
|
}
|
215 |
|
|
}
|
216 |
|
|
} else if (empty($val)) {
|
217 |
|
|
$writer->writeElement($ent);
|
218 |
|
|
} else {
|
219 |
|
|
/* it's an array */
|
220 |
|
|
$writer->startElement($ent);
|
221 |
|
|
dump_xml_config_sub($writer, $val);
|
222 |
|
|
$writer->endElement();
|
223 |
|
|
}
|
224 |
|
|
} else {
|
225 |
|
|
if ((is_bool($val) && ($val == true)) || ($val === "")) {
|
226 |
|
|
$writer->writeElement($ent);
|
227 |
|
|
} else if (!is_bool($val)) {
|
228 |
|
|
$writer->writeElement($ent, $val);
|
229 |
|
|
}
|
230 |
|
|
}
|
231 |
|
|
}
|
232 |
26433cb8
|
Scott Ullrich
|
}
|
233 |
|
|
|
234 |
|
|
function dump_xml_config($arr, $rootobj) {
|
235 |
|
|
global $listtags;
|
236 |
b79ea46a
|
Ermal
|
|
237 |
26433cb8
|
Scott Ullrich
|
$listtags = listtags();
|
238 |
79262830
|
Phil Davis
|
if (isset($GLOBALS['custom_listtags'])) {
|
239 |
|
|
foreach ($GLOBALS['custom_listtags'] as $tag) {
|
240 |
|
|
$listtags[$tag] = $tag;
|
241 |
|
|
}
|
242 |
|
|
}
|
243 |
26433cb8
|
Scott Ullrich
|
return dump_xml_config_raw($arr, $rootobj);
|
244 |
|
|
}
|
245 |
|
|
|
246 |
|
|
function dump_xml_config_pkg($arr, $rootobj) {
|
247 |
|
|
global $listtags;
|
248 |
b79ea46a
|
Ermal
|
|
249 |
26433cb8
|
Scott Ullrich
|
$listtags = listtags_pkg();
|
250 |
79262830
|
Phil Davis
|
if (isset($GLOBALS['custom_listtags_pkg'])) {
|
251 |
|
|
foreach ($GLOBALS['custom_listtags_pkg'] as $tag) {
|
252 |
|
|
$listtags[$tag] = $tag;
|
253 |
|
|
}
|
254 |
|
|
}
|
255 |
26433cb8
|
Scott Ullrich
|
return dump_xml_config_raw($arr, $rootobj);
|
256 |
|
|
}
|
257 |
|
|
|
258 |
|
|
function dump_xml_config_raw($arr, $rootobj) {
|
259 |
|
|
|
260 |
79262830
|
Phil Davis
|
$writer = new XMLWriter();
|
261 |
|
|
$writer->openMemory();
|
262 |
|
|
$writer->setIndent(true);
|
263 |
|
|
$writer->setIndentString("\t");
|
264 |
|
|
$writer->startDocument("1.0", "UTF-8");
|
265 |
|
|
$writer->startElement($rootobj);
|
266 |
26433cb8
|
Scott Ullrich
|
|
267 |
79262830
|
Phil Davis
|
dump_xml_config_sub($writer, $arr);
|
268 |
26433cb8
|
Scott Ullrich
|
|
269 |
79262830
|
Phil Davis
|
$writer->endElement();
|
270 |
|
|
$writer->endDocument();
|
271 |
|
|
$xmlconfig = $writer->outputMemory(true);
|
272 |
b79ea46a
|
Ermal
|
|
273 |
79262830
|
Phil Davis
|
return $xmlconfig;
|
274 |
26433cb8
|
Scott Ullrich
|
}
|
275 |
|
|
|
276 |
|
|
?>
|