1 |
1fb064e8
|
Erik Fonnesbeck
|
<?php
|
2 |
|
|
/* $Id$ */
|
3 |
|
|
/*
|
4 |
|
|
xmlparse_attr.inc
|
5 |
|
|
functions to parse configuration files in XML format with attributes
|
6 |
|
|
Copyright (C) 2010 Erik Fonnesbeck
|
7 |
|
|
All rights reserved.
|
8 |
|
|
|
9 |
|
|
Based on xmlparse.inc, originally part of m0n0wall (http://m0n0.ch/wall)
|
10 |
|
|
Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
|
11 |
|
|
All rights reserved.
|
12 |
|
|
|
13 |
|
|
Redistribution and use in source and binary forms, with or without
|
14 |
|
|
modification, are permitted provided that the following conditions are met:
|
15 |
|
|
|
16 |
|
|
1. Redistributions of source code must retain the above copyright notice,
|
17 |
|
|
this list of conditions and the following disclaimer.
|
18 |
|
|
|
19 |
|
|
2. Redistributions in binary form must reproduce the above copyright
|
20 |
|
|
notice, this list of conditions and the following disclaimer in the
|
21 |
|
|
documentation and/or other materials provided with the distribution.
|
22 |
|
|
|
23 |
|
|
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
24 |
|
|
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
25 |
|
|
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
26 |
|
|
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
27 |
|
|
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
28 |
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
29 |
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
30 |
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
31 |
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
32 |
|
|
POSSIBILITY OF SUCH DAMAGE.
|
33 |
|
|
*/
|
34 |
|
|
|
35 |
|
|
/* The following items will be treated as arrays in regdomain.xml */
|
36 |
|
|
function listtags_rd() {
|
37 |
|
|
$ret = explode(" ",
|
38 |
|
|
"band country flags freqband netband rd"
|
39 |
|
|
);
|
40 |
|
|
return $ret;
|
41 |
|
|
}
|
42 |
|
|
|
43 |
|
|
function startElement_attr($parser, $name, $attrs) {
|
44 |
|
|
global $parsedcfg, $depth, $curpath, $havedata, $listtags, $parsedattrs;
|
45 |
|
|
|
46 |
|
|
array_push($curpath, strtolower($name));
|
47 |
|
|
|
48 |
|
|
$ptr =& $parsedcfg;
|
49 |
|
|
if (!empty($attrs)) {
|
50 |
|
|
$attrptr =& $parsedattrs;
|
51 |
|
|
$writeattrs = true;
|
52 |
|
|
}
|
53 |
|
|
foreach ($curpath as $path) {
|
54 |
|
|
$ptr =& $ptr[$path];
|
55 |
|
|
if (isset($writeattrs))
|
56 |
|
|
$attrptr =& $attrptr[$path];
|
57 |
|
|
}
|
58 |
|
|
|
59 |
|
|
/* is it an element that belongs to a list? */
|
60 |
|
|
if (in_array(strtolower($name), $listtags)) {
|
61 |
|
|
|
62 |
|
|
/* is there an array already? */
|
63 |
|
|
if (!is_array($ptr)) {
|
64 |
|
|
/* make an array */
|
65 |
|
|
$ptr = array();
|
66 |
|
|
}
|
67 |
|
|
|
68 |
|
|
array_push($curpath, count($ptr));
|
69 |
|
|
|
70 |
|
|
if (isset($writeattrs)) {
|
71 |
|
|
if (!is_array($attrptr))
|
72 |
|
|
$attrptr = array();
|
73 |
|
|
$attrptr[count($ptr)] = $attrs;
|
74 |
|
|
}
|
75 |
|
|
|
76 |
|
|
} else if (isset($ptr)) {
|
77 |
|
|
/* multiple entries not allowed for this element, bail out */
|
78 |
|
|
die(sprintf("XML error: %s at line %d cannot occur more than once\n",
|
79 |
|
|
$name,
|
80 |
|
|
xml_get_current_line_number($parser)));
|
81 |
|
|
} else if (isset($writeattrs)) {
|
82 |
|
|
$attrptr = $attrs;
|
83 |
|
|
}
|
84 |
|
|
|
85 |
|
|
$depth++;
|
86 |
|
|
$havedata = $depth;
|
87 |
|
|
}
|
88 |
|
|
|
89 |
|
|
function endElement_attr($parser, $name) {
|
90 |
|
|
global $depth, $curpath, $parsedcfg, $havedata, $listtags;
|
91 |
|
|
|
92 |
|
|
if ($havedata == $depth) {
|
93 |
|
|
$ptr =& $parsedcfg;
|
94 |
|
|
foreach ($curpath as $path) {
|
95 |
|
|
$ptr =& $ptr[$path];
|
96 |
|
|
}
|
97 |
|
|
$ptr = "";
|
98 |
|
|
}
|
99 |
|
|
|
100 |
|
|
array_pop($curpath);
|
101 |
|
|
|
102 |
|
|
if (in_array(strtolower($name), $listtags))
|
103 |
|
|
array_pop($curpath);
|
104 |
|
|
|
105 |
|
|
$depth--;
|
106 |
|
|
}
|
107 |
|
|
|
108 |
|
|
function cData_attr($parser, $data) {
|
109 |
|
|
global $depth, $curpath, $parsedcfg, $havedata;
|
110 |
|
|
|
111 |
|
|
$data = trim($data, "\t\n\r");
|
112 |
|
|
|
113 |
|
|
if ($data != "") {
|
114 |
|
|
$ptr =& $parsedcfg;
|
115 |
|
|
foreach ($curpath as $path) {
|
116 |
|
|
$ptr =& $ptr[$path];
|
117 |
|
|
}
|
118 |
|
|
|
119 |
|
|
if (is_string($ptr)) {
|
120 |
|
|
$ptr .= html_entity_decode($data);
|
121 |
|
|
} else {
|
122 |
|
|
if (trim($data, " ") != "") {
|
123 |
|
|
$ptr = html_entity_decode($data);
|
124 |
|
|
$havedata++;
|
125 |
|
|
}
|
126 |
|
|
}
|
127 |
|
|
}
|
128 |
|
|
}
|
129 |
|
|
|
130 |
7017b54e
|
Erik Fonnesbeck
|
function parse_xml_regdomain(&$rdattributes, $rdfile = '', $rootobj = 'regulatory-data') {
|
131 |
|
|
global $g, $listtags;
|
132 |
|
|
|
133 |
|
|
if (empty($rdfile))
|
134 |
|
|
$rdfile = $g['etc_path'] . '/regdomain.xml';
|
135 |
1fb064e8
|
Erik Fonnesbeck
|
$listtags = listtags_rd();
|
136 |
7017b54e
|
Erik Fonnesbeck
|
$parsed_xml = array();
|
137 |
|
|
|
138 |
|
|
if (file_exists($g['tmp_path'] . '/regdomain.cache')) {
|
139 |
|
|
$parsed_xml = unserialize(file_get_contents($g['tmp_path'] . '/regdomain.cache'));
|
140 |
|
|
if (!empty($parsed_xml)) {
|
141 |
|
|
$rdmain = $parsed_xml['main'];
|
142 |
|
|
$rdattributes = $parsed_xml['attributes'];
|
143 |
|
|
}
|
144 |
|
|
}
|
145 |
|
|
if (empty($parsed_xml) && file_exists($g['etc_path'] . '/regdomain.xml')) {
|
146 |
|
|
$rdmain = parse_xml_config_raw_attr($rdfile, $rootobj, $rdattributes);
|
147 |
|
|
|
148 |
|
|
// unset parts that aren't used before making cache
|
149 |
|
|
foreach ($rdmain['regulatory-domains']['rd'] as $rdkey => $rdentry) {
|
150 |
|
|
if (isset($rdmain['regulatory-domains']['rd'][$rdkey]['netband']))
|
151 |
|
|
unset($rdmain['regulatory-domains']['rd'][$rdkey]['netband']);
|
152 |
|
|
if (isset($rdattributes['regulatory-domains']['rd'][$rdkey]['netband']))
|
153 |
|
|
unset($rdattributes['regulatory-domains']['rd'][$rdkey]['netband']);
|
154 |
|
|
}
|
155 |
|
|
if (isset($rdmain['shared-frequency-bands']))
|
156 |
|
|
unset($rdmain['shared-frequency-bands']);
|
157 |
|
|
if (isset($rdattributes['shared-frequency-bands']))
|
158 |
|
|
unset($rdattributes['shared-frequency-bands']);
|
159 |
|
|
|
160 |
|
|
$parsed_xml = array('main' => $rdmain, 'attributes' => $rdattributes);
|
161 |
|
|
$rdcache = fopen($g['tmp_path'] . '/regdomain.cache', "w");
|
162 |
|
|
fwrite($rdcache, serialize($parsed_xml));
|
163 |
|
|
fclose($rdcache);
|
164 |
|
|
}
|
165 |
|
|
|
166 |
|
|
return $rdmain;
|
167 |
1fb064e8
|
Erik Fonnesbeck
|
}
|
168 |
|
|
|
169 |
|
|
function parse_xml_config_raw_attr($cffile, $rootobj, &$parsed_attributes, $isstring = "false") {
|
170 |
|
|
|
171 |
|
|
global $depth, $curpath, $parsedcfg, $havedata, $listtags, $parsedattrs;
|
172 |
|
|
$parsedcfg = array();
|
173 |
|
|
$curpath = array();
|
174 |
|
|
$depth = 0;
|
175 |
|
|
$havedata = 0;
|
176 |
|
|
|
177 |
|
|
if (isset($parsed_attributes))
|
178 |
|
|
$parsedattrs = array();
|
179 |
|
|
|
180 |
|
|
$xml_parser = xml_parser_create();
|
181 |
|
|
|
182 |
|
|
xml_set_element_handler($xml_parser, "startElement_attr", "endElement_attr");
|
183 |
|
|
xml_set_character_data_handler($xml_parser, "cData_attr");
|
184 |
|
|
xml_parser_set_option($xml_parser,XML_OPTION_SKIP_WHITE, 1);
|
185 |
|
|
|
186 |
|
|
if (!($fp = fopen($cffile, "r"))) {
|
187 |
|
|
log_error("Error: could not open XML input\n");
|
188 |
|
|
if (isset($parsed_attributes)) {
|
189 |
|
|
$parsed_attributes = array();
|
190 |
|
|
unset($parsedattrs);
|
191 |
|
|
}
|
192 |
|
|
return -1;
|
193 |
|
|
}
|
194 |
|
|
|
195 |
|
|
while ($data = fread($fp, 4096)) {
|
196 |
|
|
if (!xml_parse($xml_parser, $data, feof($fp))) {
|
197 |
|
|
log_error(sprintf("XML error: %s at line %d\n",
|
198 |
|
|
xml_error_string(xml_get_error_code($xml_parser)),
|
199 |
|
|
xml_get_current_line_number($xml_parser)));
|
200 |
|
|
if (isset($parsed_attributes)) {
|
201 |
|
|
$parsed_attributes = array();
|
202 |
|
|
unset($parsedattrs);
|
203 |
|
|
}
|
204 |
|
|
return -1;
|
205 |
|
|
}
|
206 |
|
|
}
|
207 |
|
|
xml_parser_free($xml_parser);
|
208 |
|
|
|
209 |
|
|
if (!$parsedcfg[$rootobj]) {
|
210 |
|
|
log_error("XML error: no $rootobj object found!\n");
|
211 |
|
|
if (isset($parsed_attributes)) {
|
212 |
|
|
$parsed_attributes = array();
|
213 |
|
|
unset($parsedattrs);
|
214 |
|
|
}
|
215 |
|
|
return -1;
|
216 |
|
|
}
|
217 |
|
|
|
218 |
|
|
if (isset($parsed_attributes)) {
|
219 |
|
|
if ($parsedattrs[$rootobj])
|
220 |
|
|
$parsed_attributes = $parsedattrs[$rootobj];
|
221 |
|
|
unset($parsedattrs);
|
222 |
|
|
}
|
223 |
|
|
|
224 |
|
|
return $parsedcfg[$rootobj];
|
225 |
|
|
}
|
226 |
|
|
|
227 |
9734b054
|
Scott Ullrich
|
?>
|