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