1 |
26433cb8
|
Scott Ullrich
|
<?php
|
2 |
|
|
/*
|
3 |
ac24dc24
|
Renato Botelho
|
* xmlreader.inc
|
4 |
|
|
*
|
5 |
|
|
* part of pfSense (https://www.pfsense.org)
|
6 |
38809d47
|
Renato Botelho do Couto
|
* Copyright (c) 2004-2013 BSD Perimeter
|
7 |
|
|
* Copyright (c) 2013-2016 Electric Sheep Fencing
|
8 |
8f585441
|
Luiz Souza
|
* Copyright (c) 2014-2021 Rubicon Communications, LLC (Netgate)
|
9 |
ac24dc24
|
Renato Botelho
|
* All rights reserved.
|
10 |
|
|
*
|
11 |
|
|
* originally part of m0n0wall (http://m0n0.ch/wall)
|
12 |
c5d81585
|
Renato Botelho
|
* Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
|
13 |
ac24dc24
|
Renato Botelho
|
* All rights reserved.
|
14 |
|
|
*
|
15 |
b12ea3fb
|
Renato Botelho
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
16 |
|
|
* you may not use this file except in compliance with the License.
|
17 |
|
|
* You may obtain a copy of the License at
|
18 |
ac24dc24
|
Renato Botelho
|
*
|
19 |
b12ea3fb
|
Renato Botelho
|
* http://www.apache.org/licenses/LICENSE-2.0
|
20 |
ac24dc24
|
Renato Botelho
|
*
|
21 |
b12ea3fb
|
Renato Botelho
|
* Unless required by applicable law or agreed to in writing, software
|
22 |
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
23 |
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
24 |
|
|
* See the License for the specific language governing permissions and
|
25 |
|
|
* limitations under the License.
|
26 |
ac24dc24
|
Renato Botelho
|
*/
|
27 |
26433cb8
|
Scott Ullrich
|
|
28 |
|
|
/* The following items will be treated as arrays in config.xml */
|
29 |
|
|
function listtags() {
|
30 |
72a8c829
|
Ermal
|
/*
|
31 |
79262830
|
Phil Davis
|
* Please keep this list alpha sorted and no longer than 80 characters
|
32 |
|
|
* I know it's a pain, but it's a pain to find stuff too if it's not
|
33 |
|
|
*/
|
34 |
b79ea46a
|
Ermal
|
$ret = array(
|
35 |
72a8c829
|
Ermal
|
'acls', 'alias', 'aliasurl', 'allowedip', 'allowedhostname', 'authserver',
|
36 |
|
|
'bridged', 'build_port_path',
|
37 |
1af1e47e
|
Viktor G
|
'ca', 'cacert', 'cert', 'crl', 'clone', 'config',
|
38 |
9185bfff
|
Viktor G
|
'container', 'columnitem', 'checkipservice',
|
39 |
72a8c829
|
Ermal
|
'depends_on_package', 'disk', 'dnsserver', 'dnsupdate', 'domainoverrides', 'dyndns',
|
40 |
|
|
'earlyshellcmd', 'element', 'encryption-algorithm-option',
|
41 |
|
|
'field', 'fieldname',
|
42 |
|
|
'gateway_item', 'gateway_group', 'gif', 'gre', 'group',
|
43 |
40d7e4be
|
NOYB
|
'hash-algorithm-option', 'hosts', 'ifgroupentry', 'igmpentry', 'interface_array', 'item', 'key',
|
44 |
72a8c829
|
Ermal
|
'lagg', 'lbaction', 'lbpool', 'l7rules', 'lbprotocol',
|
45 |
|
|
'member', 'menu', 'tab', 'mobilekey', 'monitor_type', 'mount',
|
46 |
|
|
'npt', 'ntpserver',
|
47 |
|
|
'onetoone', 'openvpn-server', 'openvpn-client', 'openvpn-csc', 'option',
|
48 |
|
|
'package', 'passthrumac', 'phase1', 'phase2', 'ppp', 'pppoe', 'priv', 'proxyarpnet', 'pool',
|
49 |
|
|
'qinqentry', 'queue',
|
50 |
|
|
'pages', 'pipe', 'radnsserver', 'roll', 'route', 'row', 'rrddatafile', 'rule',
|
51 |
|
|
'schedule', 'service', 'servernat', 'servers',
|
52 |
1328b154
|
Luiz Souza
|
'serversdisabled', 'shellcmd', 'staticmap', 'subqueue', 'switch', 'swport',
|
53 |
9185bfff
|
Viktor G
|
'timerange', 'tunnel', 'user', 'vip', 'virtual_server', 'vlan', 'vlangroup', 'voucherdbfile',
|
54 |
1af1e47e
|
Viktor G
|
'vxlan', 'winsserver', 'wolentry', 'widget', 'xmldatafile'
|
55 |
72a8c829
|
Ermal
|
);
|
56 |
26433cb8
|
Scott Ullrich
|
return array_flip($ret);
|
57 |
|
|
}
|
58 |
|
|
|
59 |
|
|
/* Package XML tags that should be treat as a list not as a traditional array */
|
60 |
|
|
function listtags_pkg() {
|
61 |
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');
|
62 |
26433cb8
|
Scott Ullrich
|
|
63 |
|
|
return array_flip($ret);
|
64 |
|
|
}
|
65 |
|
|
|
66 |
|
|
function add_elements(&$cfgarray, &$parser) {
|
67 |
79262830
|
Phil Davis
|
global $listtags;
|
68 |
|
|
|
69 |
|
|
while ($parser->read()) {
|
70 |
|
|
switch ($parser->nodeType) {
|
71 |
|
|
case XMLReader::WHITESPACE:
|
72 |
|
|
case XMLReader::SIGNIFICANT_WHITESPACE:
|
73 |
|
|
break;
|
74 |
|
|
case XMLReader::ELEMENT:
|
75 |
|
|
if (isset($listtags[strtolower($parser->name)])) {
|
76 |
|
|
$cfgref =& $cfgarray[$parser->name][count($cfgarray[$parser->name])];
|
77 |
|
|
if (!$parser->isEmptyElement) {
|
78 |
ddfe5e43
|
Ermal
|
add_elements($cfgref, $parser);
|
79 |
79262830
|
Phil Davis
|
} else {
|
80 |
|
|
$cfgref = array();
|
81 |
|
|
}
|
82 |
|
|
} else {
|
83 |
|
|
if (isset($cfgarray[$parser->name]) && (!is_array($cfgarray[$parser->name]) || !isset($cfgarray[$parser->name][0]))) {
|
84 |
|
|
$nodebkp = $cfgarray[$parser->name];
|
85 |
|
|
$cfgarray[$parser->name] = array();
|
86 |
|
|
$cfgarray[$parser->name][] = $nodebkp;
|
87 |
|
|
$cfgref =& $cfgarray[$parser->name][0];
|
88 |
|
|
unset($nodebkp);
|
89 |
|
|
} else {
|
90 |
|
|
$cfgref =& $cfgarray[$parser->name];
|
91 |
|
|
}
|
92 |
|
|
|
93 |
|
|
if ($parser->isEmptyElement) {
|
94 |
|
|
if (is_array($cfgref)) {
|
95 |
|
|
$cfgref[] = array();
|
96 |
|
|
} else {
|
97 |
|
|
$cfgref = "";
|
98 |
|
|
}
|
99 |
|
|
} else {
|
100 |
|
|
if (is_array($cfgref)) {
|
101 |
|
|
$cfgref =& $cfgarray[$parser->name][count($cfgarray[$parser->name])];
|
102 |
|
|
add_elements($cfgref, $parser);
|
103 |
|
|
} else {
|
104 |
|
|
add_elements($cfgref, $parser);
|
105 |
|
|
}
|
106 |
|
|
}
|
107 |
ddfe5e43
|
Ermal
|
}
|
108 |
8ffb5ccd
|
Ermal
|
|
109 |
79262830
|
Phil Davis
|
$i = 0;
|
110 |
|
|
while ($parser->moveToAttributeNo($i)) {
|
111 |
|
|
$cfgref[$parser->name] = $parser->value;
|
112 |
|
|
$i++;
|
113 |
|
|
}
|
114 |
|
|
break;
|
115 |
|
|
case XMLReader::TEXT:
|
116 |
|
|
case XMLReader::CDATA:
|
117 |
|
|
$cfgarray = $parser->value;
|
118 |
|
|
break;
|
119 |
|
|
case XMLReader::END_ELEMENT:
|
120 |
|
|
return;
|
121 |
|
|
break;
|
122 |
|
|
default:
|
123 |
|
|
break;
|
124 |
|
|
}
|
125 |
ab83fce0
|
Ermal
|
}
|
126 |
26433cb8
|
Scott Ullrich
|
}
|
127 |
|
|
|
128 |
|
|
function parse_xml_config($cffile, $rootobj, $isstring = "false") {
|
129 |
|
|
global $listtags;
|
130 |
b79ea46a
|
Ermal
|
|
131 |
26433cb8
|
Scott Ullrich
|
$listtags = listtags();
|
132 |
79262830
|
Phil Davis
|
if (isset($GLOBALS['custom_listtags'])) {
|
133 |
|
|
foreach ($GLOBALS['custom_listtags'] as $tag) {
|
134 |
|
|
$listtags[$tag] = $tag;
|
135 |
|
|
}
|
136 |
|
|
}
|
137 |
ab83fce0
|
Ermal
|
|
138 |
26433cb8
|
Scott Ullrich
|
return parse_xml_config_raw($cffile, $rootobj);
|
139 |
|
|
}
|
140 |
|
|
|
141 |
|
|
function parse_xml_config_pkg($cffile, $rootobj, $isstring = "false") {
|
142 |
|
|
global $listtags;
|
143 |
b79ea46a
|
Ermal
|
|
144 |
26433cb8
|
Scott Ullrich
|
$listtags = listtags_pkg();
|
145 |
79262830
|
Phil Davis
|
if (isset($GLOBALS['custom_listtags_pkg'])) {
|
146 |
|
|
foreach ($GLOBALS['custom_listtags_pkg'] as $tag) {
|
147 |
|
|
$listtags[$tag] = $tag;
|
148 |
|
|
}
|
149 |
|
|
}
|
150 |
26433cb8
|
Scott Ullrich
|
return parse_xml_config_raw($cffile, $rootobj, $isstring);
|
151 |
|
|
}
|
152 |
|
|
|
153 |
|
|
function parse_xml_config_raw($cffile, $rootobj, $isstring = "false") {
|
154 |
b79ea46a
|
Ermal
|
global $listtags;
|
155 |
26433cb8
|
Scott Ullrich
|
|
156 |
|
|
$parsedcfg = array();
|
157 |
|
|
|
158 |
|
|
$par = new XMLReader();
|
159 |
ab83fce0
|
Ermal
|
if ($par->open($cffile, "UTF-8", LIBXML_NOERROR | LIBXML_NOWARNING)) {
|
160 |
26433cb8
|
Scott Ullrich
|
add_elements($parsedcfg, $par);
|
161 |
|
|
$par->close();
|
162 |
79262830
|
Phil Davis
|
} else {
|
163 |
5cfa35df
|
Renato Botelho
|
log_error(sprintf(gettext("Error returned while trying to parse %s"), $cffile));
|
164 |
79262830
|
Phil Davis
|
}
|
165 |
26433cb8
|
Scott Ullrich
|
|
166 |
428c289f
|
Darren Embry
|
if ($rootobj) {
|
167 |
79262830
|
Phil Davis
|
if (!is_array($rootobj)) {
|
168 |
428c289f
|
Darren Embry
|
$rootobj = array($rootobj);
|
169 |
79262830
|
Phil Davis
|
}
|
170 |
|
|
foreach ($rootobj as $rootobj_name) {
|
171 |
|
|
if ($parsedcfg[$rootobj_name]) {
|
172 |
428c289f
|
Darren Embry
|
break;
|
173 |
79262830
|
Phil Davis
|
}
|
174 |
|
|
}
|
175 |
|
|
|
176 |
428c289f
|
Darren Embry
|
return $parsedcfg[$rootobj_name];
|
177 |
|
|
} else {
|
178 |
|
|
return $parsedcfg;
|
179 |
|
|
}
|
180 |
26433cb8
|
Scott Ullrich
|
}
|
181 |
|
|
|
182 |
ab83fce0
|
Ermal
|
function dump_xml_config_sub(& $writer, $arr) {
|
183 |
79262830
|
Phil Davis
|
global $listtags;
|
184 |
|
|
|
185 |
|
|
foreach ($arr as $ent => $val) {
|
186 |
|
|
if (is_array($val)) {
|
187 |
|
|
/* is it just a list of multiple values? */
|
188 |
|
|
if (isset($listtags[strtolower($ent)])) {
|
189 |
|
|
foreach ($val as $cval) {
|
190 |
|
|
if (is_array($cval)) {
|
191 |
|
|
if (empty($cval)) {
|
192 |
da7054b7
|
Steve Beaver
|
$writer->startElement($ent);
|
193 |
|
|
$writer->endElement();
|
194 |
79262830
|
Phil Davis
|
} else {
|
195 |
|
|
$writer->startElement($ent);
|
196 |
|
|
dump_xml_config_sub($writer, $cval);
|
197 |
|
|
$writer->endElement();
|
198 |
|
|
}
|
199 |
|
|
} else {
|
200 |
|
|
if ($cval === false) {
|
201 |
|
|
continue;
|
202 |
|
|
}
|
203 |
|
|
if ((is_bool($val) && ($val == true)) || ($val === "")) {
|
204 |
da7054b7
|
Steve Beaver
|
$writer->startElement($ent);
|
205 |
|
|
$writer->endElement();
|
206 |
79262830
|
Phil Davis
|
} else if (!is_bool($val)) {
|
207 |
ab83fce0
|
Ermal
|
$writer->writeElement($ent, $cval);
|
208 |
79262830
|
Phil Davis
|
}
|
209 |
|
|
}
|
210 |
|
|
}
|
211 |
|
|
} else if (empty($val)) {
|
212 |
da7054b7
|
Steve Beaver
|
$writer->startElement($ent);
|
213 |
|
|
$writer->endElement();
|
214 |
79262830
|
Phil Davis
|
} else {
|
215 |
|
|
/* it's an array */
|
216 |
|
|
$writer->startElement($ent);
|
217 |
|
|
dump_xml_config_sub($writer, $val);
|
218 |
|
|
$writer->endElement();
|
219 |
|
|
}
|
220 |
|
|
} else {
|
221 |
|
|
if ((is_bool($val) && ($val == true)) || ($val === "")) {
|
222 |
da7054b7
|
Steve Beaver
|
$writer->startElement($ent);
|
223 |
|
|
$writer->endElement();
|
224 |
79262830
|
Phil Davis
|
} else if (!is_bool($val)) {
|
225 |
|
|
$writer->writeElement($ent, $val);
|
226 |
|
|
}
|
227 |
|
|
}
|
228 |
|
|
}
|
229 |
26433cb8
|
Scott Ullrich
|
}
|
230 |
|
|
|
231 |
|
|
function dump_xml_config($arr, $rootobj) {
|
232 |
|
|
global $listtags;
|
233 |
b79ea46a
|
Ermal
|
|
234 |
26433cb8
|
Scott Ullrich
|
$listtags = listtags();
|
235 |
79262830
|
Phil Davis
|
if (isset($GLOBALS['custom_listtags'])) {
|
236 |
|
|
foreach ($GLOBALS['custom_listtags'] as $tag) {
|
237 |
|
|
$listtags[$tag] = $tag;
|
238 |
|
|
}
|
239 |
|
|
}
|
240 |
da7054b7
|
Steve Beaver
|
|
241 |
|
|
|
242 |
26433cb8
|
Scott Ullrich
|
return dump_xml_config_raw($arr, $rootobj);
|
243 |
|
|
}
|
244 |
|
|
|
245 |
|
|
function dump_xml_config_pkg($arr, $rootobj) {
|
246 |
|
|
global $listtags;
|
247 |
b79ea46a
|
Ermal
|
|
248 |
26433cb8
|
Scott Ullrich
|
$listtags = listtags_pkg();
|
249 |
79262830
|
Phil Davis
|
if (isset($GLOBALS['custom_listtags_pkg'])) {
|
250 |
|
|
foreach ($GLOBALS['custom_listtags_pkg'] as $tag) {
|
251 |
|
|
$listtags[$tag] = $tag;
|
252 |
|
|
}
|
253 |
|
|
}
|
254 |
26433cb8
|
Scott Ullrich
|
return dump_xml_config_raw($arr, $rootobj);
|
255 |
|
|
}
|
256 |
|
|
|
257 |
|
|
function dump_xml_config_raw($arr, $rootobj) {
|
258 |
|
|
|
259 |
79262830
|
Phil Davis
|
$writer = new XMLWriter();
|
260 |
|
|
$writer->openMemory();
|
261 |
|
|
$writer->setIndent(true);
|
262 |
|
|
$writer->setIndentString("\t");
|
263 |
|
|
$writer->startDocument("1.0", "UTF-8");
|
264 |
|
|
$writer->startElement($rootobj);
|
265 |
26433cb8
|
Scott Ullrich
|
|
266 |
79262830
|
Phil Davis
|
dump_xml_config_sub($writer, $arr);
|
267 |
26433cb8
|
Scott Ullrich
|
|
268 |
79262830
|
Phil Davis
|
$writer->endElement();
|
269 |
|
|
$writer->endDocument();
|
270 |
|
|
$xmlconfig = $writer->outputMemory(true);
|
271 |
b79ea46a
|
Ermal
|
|
272 |
79262830
|
Phil Davis
|
return $xmlconfig;
|
273 |
26433cb8
|
Scott Ullrich
|
}
|
274 |
|
|
|
275 |
|
|
?>
|