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