1 |
26433cb8
|
Scott Ullrich
|
<?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 |
|
|
/*
|
34 |
|
|
pfSense_MODULE: utils
|
35 |
|
|
*/
|
36 |
|
|
|
37 |
|
|
/* The following items will be treated as arrays in config.xml */
|
38 |
|
|
function listtags() {
|
39 |
|
|
/* 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 |
|
|
$ret = explode(" ",
|
43 |
e08e4ebc
|
Ermal
|
"alias aliasurl allowedip authserver bridged ca cacert cert clone config ".
|
44 |
|
|
"container columnitem depends_on_package disk dnsserver dnsupdate ".
|
45 |
|
|
"domainoverrides dyndns earlyshellcmd element encryption-algorithm-option ".
|
46 |
|
|
"field fieldname hash-algorithm-option gateway_item gateway_group gif gre ".
|
47 |
|
|
"group hosts member ifgroupentry igmpentry interface_array item key lagg " .
|
48 |
|
|
"lbaction lbpool l7rules lbprotocol ".
|
49 |
|
|
"member menu tab mobilekey monitor_type mount ntpserver onetoone ".
|
50 |
|
|
"openvpn-server openvpn-client openvpn-csc " .
|
51 |
|
|
"option ppp package passthrumac phase1 phase2 priv proxyarpnet qinqentry queue ".
|
52 |
|
|
"pages pipe roll route row rrddatafile rule schedule service servernat servers ".
|
53 |
|
|
"serversdisabled earlyshellcmd shellcmd staticmap subqueue timerange ".
|
54 |
|
|
"tunnel user vip virtual_server vlan winsserver wolentry widget"
|
55 |
|
|
);
|
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 |
|
|
$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 |
|
|
|
63 |
|
|
return array_flip($ret);
|
64 |
|
|
}
|
65 |
|
|
|
66 |
|
|
function add_elements(&$cfgarray, &$parser) {
|
67 |
|
|
global $listtags;
|
68 |
|
|
while ($parser->read()) {
|
69 |
|
|
switch ($parser->nodeType) {
|
70 |
|
|
case XMLReader::WHITESPACE:
|
71 |
|
|
case XMLReader::SIGNIFICANT_WHITESPACE:
|
72 |
|
|
break;
|
73 |
|
|
case XMLReader::ELEMENT:
|
74 |
ab83fce0
|
Ermal
|
if (isset($listtags[strtolower($parser->name)])) {
|
75 |
|
|
if (!$parser->isEmptyElement)
|
76 |
26433cb8
|
Scott Ullrich
|
add_elements($cfgarray[$parser->name][], $parser);
|
77 |
ab83fce0
|
Ermal
|
} else {
|
78 |
|
|
if ($parser->isEmptyElement)
|
79 |
|
|
$cfgarray[$parser->name] = "";
|
80 |
|
|
else
|
81 |
|
|
add_elements($cfgarray[$parser->name], $parser);
|
82 |
|
|
}
|
83 |
26433cb8
|
Scott Ullrich
|
break;
|
84 |
|
|
case XMLReader::TEXT:
|
85 |
|
|
case XMLReader::CDATA:
|
86 |
|
|
$cfgarray = $parser->value;
|
87 |
|
|
break;
|
88 |
|
|
case XMLReader::END_ELEMENT:
|
89 |
ab83fce0
|
Ermal
|
return;
|
90 |
|
|
break;
|
91 |
26433cb8
|
Scott Ullrich
|
default:
|
92 |
|
|
break;
|
93 |
|
|
}
|
94 |
ab83fce0
|
Ermal
|
}
|
95 |
26433cb8
|
Scott Ullrich
|
}
|
96 |
|
|
|
97 |
|
|
function parse_xml_config($cffile, $rootobj, $isstring = "false") {
|
98 |
|
|
global $listtags;
|
99 |
|
|
$listtags = listtags();
|
100 |
|
|
if (isset($GLOBALS['custom_listtags'])) {
|
101 |
|
|
foreach($GLOBALS['custom_listtags'] as $tag) {
|
102 |
|
|
$listtags[$tag] = $tag;
|
103 |
|
|
}
|
104 |
|
|
}
|
105 |
ab83fce0
|
Ermal
|
|
106 |
26433cb8
|
Scott Ullrich
|
return parse_xml_config_raw($cffile, $rootobj);
|
107 |
|
|
}
|
108 |
|
|
|
109 |
|
|
function parse_xml_config_pkg($cffile, $rootobj, $isstring = "false") {
|
110 |
|
|
global $listtags;
|
111 |
|
|
$listtags = listtags_pkg();
|
112 |
|
|
if (isset($GLOBALS['custom_listtags_pkg'])) {
|
113 |
|
|
foreach($GLOBALS['custom_listtags_pkg'] as $tag) {
|
114 |
|
|
$listtags[$tag] = $tag;
|
115 |
|
|
}
|
116 |
|
|
}
|
117 |
|
|
return parse_xml_config_raw($cffile, $rootobj, $isstring);
|
118 |
|
|
}
|
119 |
|
|
|
120 |
|
|
function parse_xml_config_raw($cffile, $rootobj, $isstring = "false") {
|
121 |
|
|
|
122 |
|
|
$parsedcfg = array();
|
123 |
|
|
|
124 |
|
|
$par = new XMLReader();
|
125 |
ab83fce0
|
Ermal
|
if ($par->open($cffile, "UTF-8", LIBXML_NOERROR | LIBXML_NOWARNING)) {
|
126 |
26433cb8
|
Scott Ullrich
|
add_elements($parsedcfg, $par);
|
127 |
|
|
$par->close();
|
128 |
|
|
} else
|
129 |
|
|
log_error("Error returned while trying to parse {$cffile}");
|
130 |
|
|
|
131 |
|
|
return $parsedcfg[$rootobj];
|
132 |
|
|
}
|
133 |
|
|
|
134 |
ab83fce0
|
Ermal
|
function dump_xml_config_sub(& $writer, $arr) {
|
135 |
|
|
global $listtags;
|
136 |
26433cb8
|
Scott Ullrich
|
|
137 |
ab83fce0
|
Ermal
|
foreach ($arr as $ent => $val) {
|
138 |
|
|
if (is_array($val)) {
|
139 |
|
|
/* is it just a list of multiple values? */
|
140 |
|
|
if (isset($listtags[strtolower($ent)])) {
|
141 |
|
|
foreach ($val as $cval) {
|
142 |
|
|
if (is_array($cval)) {
|
143 |
|
|
if (empty($cval))
|
144 |
|
|
$writer->writeElement($ent);
|
145 |
|
|
else {
|
146 |
|
|
$writer->startElement($ent);
|
147 |
|
|
dump_xml_config_sub($writer, $cval);
|
148 |
|
|
$writer->endElement();
|
149 |
|
|
}
|
150 |
|
|
} else {
|
151 |
|
|
if($cval === false) continue;
|
152 |
|
|
if ((is_bool($val) && ($val == true)) || ($val === ""))
|
153 |
|
|
$writer->writeElement($ent);
|
154 |
|
|
else if (!is_bool($val))
|
155 |
|
|
$writer->writeElement($ent, $cval);
|
156 |
|
|
}
|
157 |
|
|
}
|
158 |
|
|
} else if (empty($val)) {
|
159 |
|
|
$writer->writeElement($ent);
|
160 |
|
|
} else {
|
161 |
|
|
/* it's an array */
|
162 |
|
|
$writer->startElement($ent);
|
163 |
|
|
dump_xml_config_sub($writer, $val);
|
164 |
|
|
$writer->endElement();
|
165 |
|
|
}
|
166 |
|
|
} else {
|
167 |
|
|
if ((is_bool($val) && ($val == true)) || ($val === ""))
|
168 |
|
|
$writer->writeElement($ent);
|
169 |
|
|
else if (!is_bool($val))
|
170 |
|
|
$writer->writeElement($ent, $val);
|
171 |
|
|
}
|
172 |
|
|
}
|
173 |
26433cb8
|
Scott Ullrich
|
}
|
174 |
|
|
|
175 |
|
|
function dump_xml_config($arr, $rootobj) {
|
176 |
|
|
global $listtags;
|
177 |
|
|
$listtags = listtags();
|
178 |
|
|
if (isset($GLOBALS['custom_listtags'])) {
|
179 |
|
|
foreach($GLOBALS['custom_listtags'] as $tag) {
|
180 |
|
|
$listtags[$tag] = $tag;
|
181 |
|
|
}
|
182 |
|
|
}
|
183 |
|
|
return dump_xml_config_raw($arr, $rootobj);
|
184 |
|
|
}
|
185 |
|
|
|
186 |
|
|
function dump_xml_config_pkg($arr, $rootobj) {
|
187 |
|
|
global $listtags;
|
188 |
|
|
$listtags = listtags_pkg();
|
189 |
|
|
if (isset($GLOBALS['custom_listtags_pkg'])) {
|
190 |
|
|
foreach($GLOBALS['custom_listtags_pkg'] as $tag) {
|
191 |
|
|
$listtags[$tag] = $tag;
|
192 |
|
|
}
|
193 |
|
|
}
|
194 |
|
|
return dump_xml_config_raw($arr, $rootobj);
|
195 |
|
|
}
|
196 |
|
|
|
197 |
|
|
function dump_xml_config_raw($arr, $rootobj) {
|
198 |
|
|
|
199 |
ab83fce0
|
Ermal
|
$writer = new XMLWriter();
|
200 |
|
|
$writer->openMemory();
|
201 |
|
|
$writer->setIndent(true);
|
202 |
|
|
$writer->setIndentString("\t");
|
203 |
|
|
$writer->startDocument("1.0", "UTF-8");
|
204 |
|
|
$writer->startElement($rootobj);
|
205 |
26433cb8
|
Scott Ullrich
|
|
206 |
ab83fce0
|
Ermal
|
dump_xml_config_sub($writer, $arr);
|
207 |
26433cb8
|
Scott Ullrich
|
|
208 |
ab83fce0
|
Ermal
|
$writer->endElement();
|
209 |
|
|
$writer->endDocument();
|
210 |
|
|
$xmlconfig = $writer->outputMemory(true);
|
211 |
|
|
return $xmlconfig;
|
212 |
26433cb8
|
Scott Ullrich
|
}
|
213 |
|
|
|
214 |
|
|
?>
|