Project

General

Profile

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