Project

General

Profile

Download (7.53 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * xmlparse_attr.inc
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2010-2016 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
 * 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

    
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
		if (isset($writeattrs)) {
80
			$attrptr =& $attrptr[$path];
81
		}
82
	}
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
			if (!is_array($attrptr)) {
97
				$attrptr = array();
98
			}
99
			$attrptr[count($ptr)] = $attrs;
100
		}
101

    
102
	} else if (isset($ptr)) {
103
		/* multiple entries not allowed for this element, bail out */
104
		die(sprintf(gettext('XML error: %1$s at line %2$d cannot occur more than once') . "\n",
105
			$name,
106
			xml_get_current_line_number($parser)));
107
	} 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
	if (in_array(strtolower($name), $listtags)) {
129
		array_pop($curpath);
130
	}
131

    
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
function parse_xml_regdomain(&$rdattributes, $rdfile = '', $rootobj = 'regulatory-data') {
158
	global $g, $listtags;
159

    
160
	if (empty($rdfile)) {
161
		$rdfile = $g['etc_path'] . '/regdomain.xml';
162
	}
163
	$listtags = listtags_rd();
164
	$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
			if (isset($rdmain['regulatory-domains']['rd'][$rdkey]['netband'])) {
179
				unset($rdmain['regulatory-domains']['rd'][$rdkey]['netband']);
180
			}
181
			if (isset($rdattributes['regulatory-domains']['rd'][$rdkey]['netband'])) {
182
				unset($rdattributes['regulatory-domains']['rd'][$rdkey]['netband']);
183
			}
184
		}
185
		if (isset($rdmain['shared-frequency-bands'])) {
186
			unset($rdmain['shared-frequency-bands']);
187
		}
188
		if (isset($rdattributes['shared-frequency-bands'])) {
189
			unset($rdattributes['shared-frequency-bands']);
190
		}
191

    
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
}
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
	if (isset($parsed_attributes)) {
210
		$parsedattrs = array();
211
	}
212

    
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
	xml_parser_set_option($xml_parser, XML_OPTION_SKIP_WHITE, 1);
218

    
219
	if (!($fp = fopen($cffile, "r"))) {
220
		log_error(gettext("Error: could not open XML input") . "\n");
221
		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
			log_error(sprintf(gettext('XML error: %1$s at line %2$d') . "\n",
231
				xml_error_string(xml_get_error_code($xml_parser)),
232
				xml_get_current_line_number($xml_parser)));
233
			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
		log_error(sprintf(gettext("XML error: no %s object found!") . "\n", $rootobj));
244
		if (isset($parsed_attributes)) {
245
			$parsed_attributes = array();
246
			unset($parsedattrs);
247
		}
248
		return -1;
249
	}
250

    
251
	if (isset($parsed_attributes)) {
252
		if ($parsedattrs[$rootobj]) {
253
			$parsed_attributes = $parsedattrs[$rootobj];
254
		}
255
		unset($parsedattrs);
256
	}
257

    
258
	return $parsedcfg[$rootobj];
259
}
260

    
261
?>
(61-61/65)