1
|
<?php
|
2
|
/*
|
3
|
* xmlparse_attr.inc
|
4
|
*
|
5
|
* part of pfSense (https://www.pfsense.org)
|
6
|
* Copyright (c) 2010-2018 Rubicon Communications, LLC (Netgate)
|
7
|
* Copyright (c) 2010 Erik Fonnesbeck
|
8
|
* All rights reserved.
|
9
|
*
|
10
|
* originally part of m0n0wall (http://m0n0.ch/wall)
|
11
|
* Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
|
12
|
* All rights reserved.
|
13
|
*
|
14
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
15
|
* you may not use this file except in compliance with the License.
|
16
|
* You may obtain a copy of the License at
|
17
|
*
|
18
|
* http://www.apache.org/licenses/LICENSE-2.0
|
19
|
*
|
20
|
* Unless required by applicable law or agreed to in writing, software
|
21
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
22
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
23
|
* See the License for the specific language governing permissions and
|
24
|
* limitations under the License.
|
25
|
*/
|
26
|
|
27
|
/* The following items will be treated as arrays in regdomain.xml */
|
28
|
function listtags_rd() {
|
29
|
$ret = explode(" ",
|
30
|
"band country flags freqband netband rd"
|
31
|
);
|
32
|
return $ret;
|
33
|
}
|
34
|
|
35
|
function startElement_attr($parser, $name, $attrs) {
|
36
|
global $parsedcfg, $depth, $curpath, $havedata, $listtags, $parsedattrs;
|
37
|
|
38
|
array_push($curpath, strtolower($name));
|
39
|
|
40
|
$ptr =& $parsedcfg;
|
41
|
if (!empty($attrs)) {
|
42
|
$attrptr =& $parsedattrs;
|
43
|
$writeattrs = true;
|
44
|
}
|
45
|
foreach ($curpath as $path) {
|
46
|
$ptr =& $ptr[$path];
|
47
|
if (isset($writeattrs)) {
|
48
|
$attrptr =& $attrptr[$path];
|
49
|
}
|
50
|
}
|
51
|
|
52
|
/* is it an element that belongs to a list? */
|
53
|
if (in_array(strtolower($name), $listtags)) {
|
54
|
|
55
|
/* is there an array already? */
|
56
|
if (!is_array($ptr)) {
|
57
|
/* make an array */
|
58
|
$ptr = array();
|
59
|
}
|
60
|
|
61
|
array_push($curpath, count($ptr));
|
62
|
|
63
|
if (isset($writeattrs)) {
|
64
|
if (!is_array($attrptr)) {
|
65
|
$attrptr = array();
|
66
|
}
|
67
|
$attrptr[count($ptr)] = $attrs;
|
68
|
}
|
69
|
|
70
|
} else if (isset($ptr)) {
|
71
|
/* multiple entries not allowed for this element, bail out */
|
72
|
die(sprintf(gettext('XML error: %1$s at line %2$d cannot occur more than once') . "\n",
|
73
|
$name,
|
74
|
xml_get_current_line_number($parser)));
|
75
|
} else if (isset($writeattrs)) {
|
76
|
$attrptr = $attrs;
|
77
|
}
|
78
|
|
79
|
$depth++;
|
80
|
$havedata = $depth;
|
81
|
}
|
82
|
|
83
|
function endElement_attr($parser, $name) {
|
84
|
global $depth, $curpath, $parsedcfg, $havedata, $listtags;
|
85
|
|
86
|
if ($havedata == $depth) {
|
87
|
$ptr =& $parsedcfg;
|
88
|
foreach ($curpath as $path) {
|
89
|
$ptr =& $ptr[$path];
|
90
|
}
|
91
|
$ptr = "";
|
92
|
}
|
93
|
|
94
|
array_pop($curpath);
|
95
|
|
96
|
if (in_array(strtolower($name), $listtags)) {
|
97
|
array_pop($curpath);
|
98
|
}
|
99
|
|
100
|
$depth--;
|
101
|
}
|
102
|
|
103
|
function cData_attr($parser, $data) {
|
104
|
global $depth, $curpath, $parsedcfg, $havedata;
|
105
|
|
106
|
$data = trim($data, "\t\n\r");
|
107
|
|
108
|
if ($data != "") {
|
109
|
$ptr =& $parsedcfg;
|
110
|
foreach ($curpath as $path) {
|
111
|
$ptr =& $ptr[$path];
|
112
|
}
|
113
|
|
114
|
if (is_string($ptr)) {
|
115
|
$ptr .= html_entity_decode($data);
|
116
|
} else {
|
117
|
if (trim($data, " ") != "") {
|
118
|
$ptr = html_entity_decode($data);
|
119
|
$havedata++;
|
120
|
}
|
121
|
}
|
122
|
}
|
123
|
}
|
124
|
|
125
|
function parse_xml_regdomain(&$rdattributes, $rdfile = '', $rootobj = 'regulatory-data') {
|
126
|
global $g, $listtags;
|
127
|
|
128
|
if (empty($rdfile)) {
|
129
|
$rdfile = $g['etc_path'] . '/regdomain.xml';
|
130
|
}
|
131
|
$listtags = listtags_rd();
|
132
|
$parsed_xml = array();
|
133
|
|
134
|
if (file_exists($g['tmp_path'] . '/regdomain.cache')) {
|
135
|
$parsed_xml = unserialize(file_get_contents($g['tmp_path'] . '/regdomain.cache'));
|
136
|
if (!empty($parsed_xml)) {
|
137
|
$rdmain = $parsed_xml['main'];
|
138
|
$rdattributes = $parsed_xml['attributes'];
|
139
|
}
|
140
|
}
|
141
|
if (empty($parsed_xml) && file_exists($g['etc_path'] . '/regdomain.xml')) {
|
142
|
$rdmain = parse_xml_config_raw_attr($rdfile, $rootobj, $rdattributes);
|
143
|
|
144
|
// unset parts that aren't used before making cache
|
145
|
foreach ($rdmain['regulatory-domains']['rd'] as $rdkey => $rdentry) {
|
146
|
if (isset($rdmain['regulatory-domains']['rd'][$rdkey]['netband'])) {
|
147
|
unset($rdmain['regulatory-domains']['rd'][$rdkey]['netband']);
|
148
|
}
|
149
|
if (isset($rdattributes['regulatory-domains']['rd'][$rdkey]['netband'])) {
|
150
|
unset($rdattributes['regulatory-domains']['rd'][$rdkey]['netband']);
|
151
|
}
|
152
|
}
|
153
|
if (isset($rdmain['shared-frequency-bands'])) {
|
154
|
unset($rdmain['shared-frequency-bands']);
|
155
|
}
|
156
|
if (isset($rdattributes['shared-frequency-bands'])) {
|
157
|
unset($rdattributes['shared-frequency-bands']);
|
158
|
}
|
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
|
}
|
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
|
|
181
|
$xml_parser = xml_parser_create();
|
182
|
|
183
|
xml_set_element_handler($xml_parser, "startElement_attr", "endElement_attr");
|
184
|
xml_set_character_data_handler($xml_parser, "cData_attr");
|
185
|
xml_parser_set_option($xml_parser, XML_OPTION_SKIP_WHITE, 1);
|
186
|
|
187
|
if (!($fp = fopen($cffile, "r"))) {
|
188
|
log_error(gettext("Error: could not open XML input") . "\n");
|
189
|
if (isset($parsed_attributes)) {
|
190
|
$parsed_attributes = array();
|
191
|
unset($parsedattrs);
|
192
|
}
|
193
|
return -1;
|
194
|
}
|
195
|
|
196
|
while ($data = fread($fp, 4096)) {
|
197
|
if (!xml_parse($xml_parser, $data, feof($fp))) {
|
198
|
log_error(sprintf(gettext('XML error: %1$s at line %2$d') . "\n",
|
199
|
xml_error_string(xml_get_error_code($xml_parser)),
|
200
|
xml_get_current_line_number($xml_parser)));
|
201
|
if (isset($parsed_attributes)) {
|
202
|
$parsed_attributes = array();
|
203
|
unset($parsedattrs);
|
204
|
}
|
205
|
return -1;
|
206
|
}
|
207
|
}
|
208
|
xml_parser_free($xml_parser);
|
209
|
|
210
|
if (!$parsedcfg[$rootobj]) {
|
211
|
log_error(sprintf(gettext("XML error: no %s object found!") . "\n", $rootobj));
|
212
|
if (isset($parsed_attributes)) {
|
213
|
$parsed_attributes = array();
|
214
|
unset($parsedattrs);
|
215
|
}
|
216
|
return -1;
|
217
|
}
|
218
|
|
219
|
if (isset($parsed_attributes)) {
|
220
|
if ($parsedattrs[$rootobj]) {
|
221
|
$parsed_attributes = $parsedattrs[$rootobj];
|
222
|
}
|
223
|
unset($parsedattrs);
|
224
|
}
|
225
|
|
226
|
return $parsedcfg[$rootobj];
|
227
|
}
|
228
|
|
229
|
?>
|