Project

General

Profile

Download (5.92 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 37d60e23 Luiz Souza
 * Copyright (c) 2014-2025 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 2568e151 Christian McDonald
		$rdfile = g_get('etc_path') . '/regdomain.xml';
132 79262830 Phil Davis
	}
133 1fb064e8 Erik Fonnesbeck
	$listtags = listtags_rd();
134 7017b54e Erik Fonnesbeck
	$parsed_xml = array();
135
136 2568e151 Christian McDonald
	if (file_exists(g_get('tmp_path') . '/regdomain.cache')) {
137
		$parsed_xml = unserialize(file_get_contents(g_get('tmp_path') . '/regdomain.cache'));
138 7017b54e Erik Fonnesbeck
		if (!empty($parsed_xml)) {
139
			$rdmain = $parsed_xml['main'];
140
			$rdattributes = $parsed_xml['attributes'];
141
		}
142
	}
143 2568e151 Christian McDonald
	if (empty($parsed_xml) && file_exists(g_get('etc_path') . '/regdomain.xml')) {
144 7017b54e Erik Fonnesbeck
		$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 2568e151 Christian McDonald
		$rdcache = fopen(g_get('tmp_path') . '/regdomain.cache', "w");
164 7017b54e Erik Fonnesbeck
		fwrite($rdcache, serialize($parsed_xml));
165
		fclose($rdcache);
166
	}
167
168
	return $rdmain;
169 1fb064e8 Erik Fonnesbeck
}
170
171 b1360be3 Marcos Mendoza
function parse_xml_config_raw_attr($cffile, $rootobj, &$parsed_attributes) {
172 1fb064e8 Erik Fonnesbeck
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
211
	if (!$parsedcfg[$rootobj]) {
212 f55943aa Renato Botelho
		log_error(sprintf(gettext("XML error: no %s object found!") . "\n", $rootobj));
213 1fb064e8 Erik Fonnesbeck
		if (isset($parsed_attributes)) {
214
			$parsed_attributes = array();
215
			unset($parsedattrs);
216
		}
217
		return -1;
218
	}
219
220
	if (isset($parsed_attributes)) {
221 79262830 Phil Davis
		if ($parsedattrs[$rootobj]) {
222 1fb064e8 Erik Fonnesbeck
			$parsed_attributes = $parsedattrs[$rootobj];
223 79262830 Phil Davis
		}
224 1fb064e8 Erik Fonnesbeck
		unset($parsedattrs);
225
	}
226
227
	return $parsedcfg[$rootobj];
228
}
229
230
?>