Project

General

Profile

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