Project

General

Profile

Download (7.51 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	xmlparse_attr.inc
4
	functions to parse configuration files in XML format with attributes
5

    
6
	part of pfSense (https://www.pfsense.org)
7
	Copyright (C) 2010 Erik Fonnesbeck
8
	Copyright (c) 2010-2016 Electric Sheep Fencing, LLC.
9
	All rights reserved.
10

    
11
	Based on xmlparse.inc, originally part of m0n0wall (http://m0n0.ch/wall)
12
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
13
	All rights reserved.
14

    
15
	Redistribution and use in source and binary forms, with or without
16
	modification, are permitted provided that the following conditions are met:
17

    
18
	1. Redistributions of source code must retain the above copyright notice,
19
	   this list of conditions and the following disclaimer.
20

    
21
	2. Redistributions in binary form must reproduce the above copyright
22
	   notice, this list of conditions and the following disclaimer in
23
	   the documentation and/or other materials provided with the
24
	   distribution.
25

    
26
	3. All advertising materials mentioning features or use of this software
27
	   must display the following acknowledgment:
28
	   "This product includes software developed by the pfSense Project
29
	   for use in the pfSense® software distribution. (http://www.pfsense.org/).
30

    
31
	4. The names "pfSense" and "pfSense Project" must not be used to
32
	   endorse or promote products derived from this software without
33
	   prior written permission. For written permission, please contact
34
	   coreteam@pfsense.org.
35

    
36
	5. Products derived from this software may not be called "pfSense"
37
	   nor may "pfSense" appear in their names without prior written
38
	   permission of the Electric Sheep Fencing, LLC.
39

    
40
	6. Redistributions of any form whatsoever must retain the following
41
	   acknowledgment:
42

    
43
	"This product includes software developed by the pfSense Project
44
	for use in the pfSense software distribution (http://www.pfsense.org/).
45

    
46
	THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
47
	EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48
	IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
49
	PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
50
	ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
51
	SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
52
	NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
53
	LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54
	HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
55
	STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
56
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
57
	OF THE POSSIBILITY OF SUCH DAMAGE.
58
*/
59

    
60
/* The following items will be treated as arrays in regdomain.xml */
61
function listtags_rd() {
62
	$ret = explode(" ",
63
		"band country flags freqband netband rd"
64
		);
65
	return $ret;
66
}
67

    
68
function startElement_attr($parser, $name, $attrs) {
69
	global $parsedcfg, $depth, $curpath, $havedata, $listtags, $parsedattrs;
70

    
71
	array_push($curpath, strtolower($name));
72

    
73
	$ptr =& $parsedcfg;
74
	if (!empty($attrs)) {
75
		$attrptr =& $parsedattrs;
76
		$writeattrs = true;
77
	}
78
	foreach ($curpath as $path) {
79
		$ptr =& $ptr[$path];
80
		if (isset($writeattrs)) {
81
			$attrptr =& $attrptr[$path];
82
		}
83
	}
84

    
85
	/* is it an element that belongs to a list? */
86
	if (in_array(strtolower($name), $listtags)) {
87

    
88
		/* is there an array already? */
89
		if (!is_array($ptr)) {
90
			/* make an array */
91
			$ptr = array();
92
		}
93

    
94
		array_push($curpath, count($ptr));
95

    
96
		if (isset($writeattrs)) {
97
			if (!is_array($attrptr)) {
98
				$attrptr = array();
99
			}
100
			$attrptr[count($ptr)] = $attrs;
101
		}
102

    
103
	} else if (isset($ptr)) {
104
		/* multiple entries not allowed for this element, bail out */
105
		die(sprintf(gettext('XML error: %1$s at line %2$d cannot occur more than once') . "\n",
106
			$name,
107
			xml_get_current_line_number($parser)));
108
	} else if (isset($writeattrs)) {
109
		$attrptr = $attrs;
110
	}
111

    
112
	$depth++;
113
	$havedata = $depth;
114
}
115

    
116
function endElement_attr($parser, $name) {
117
	global $depth, $curpath, $parsedcfg, $havedata, $listtags;
118

    
119
	if ($havedata == $depth) {
120
		$ptr =& $parsedcfg;
121
		foreach ($curpath as $path) {
122
			$ptr =& $ptr[$path];
123
		}
124
		$ptr = "";
125
	}
126

    
127
	array_pop($curpath);
128

    
129
	if (in_array(strtolower($name), $listtags)) {
130
		array_pop($curpath);
131
	}
132

    
133
	$depth--;
134
}
135

    
136
function cData_attr($parser, $data) {
137
	global $depth, $curpath, $parsedcfg, $havedata;
138

    
139
	$data = trim($data, "\t\n\r");
140

    
141
	if ($data != "") {
142
		$ptr =& $parsedcfg;
143
		foreach ($curpath as $path) {
144
			$ptr =& $ptr[$path];
145
		}
146

    
147
		if (is_string($ptr)) {
148
			$ptr .= html_entity_decode($data);
149
		} else {
150
			if (trim($data, " ") != "") {
151
				$ptr = html_entity_decode($data);
152
				$havedata++;
153
			}
154
		}
155
	}
156
}
157

    
158
function parse_xml_regdomain(&$rdattributes, $rdfile = '', $rootobj = 'regulatory-data') {
159
	global $g, $listtags;
160

    
161
	if (empty($rdfile)) {
162
		$rdfile = $g['etc_path'] . '/regdomain.xml';
163
	}
164
	$listtags = listtags_rd();
165
	$parsed_xml = array();
166

    
167
	if (file_exists($g['tmp_path'] . '/regdomain.cache')) {
168
		$parsed_xml = unserialize(file_get_contents($g['tmp_path'] . '/regdomain.cache'));
169
		if (!empty($parsed_xml)) {
170
			$rdmain = $parsed_xml['main'];
171
			$rdattributes = $parsed_xml['attributes'];
172
		}
173
	}
174
	if (empty($parsed_xml) && file_exists($g['etc_path'] . '/regdomain.xml')) {
175
		$rdmain = parse_xml_config_raw_attr($rdfile, $rootobj, $rdattributes);
176

    
177
		// unset parts that aren't used before making cache
178
		foreach ($rdmain['regulatory-domains']['rd'] as $rdkey => $rdentry) {
179
			if (isset($rdmain['regulatory-domains']['rd'][$rdkey]['netband'])) {
180
				unset($rdmain['regulatory-domains']['rd'][$rdkey]['netband']);
181
			}
182
			if (isset($rdattributes['regulatory-domains']['rd'][$rdkey]['netband'])) {
183
				unset($rdattributes['regulatory-domains']['rd'][$rdkey]['netband']);
184
			}
185
		}
186
		if (isset($rdmain['shared-frequency-bands'])) {
187
			unset($rdmain['shared-frequency-bands']);
188
		}
189
		if (isset($rdattributes['shared-frequency-bands'])) {
190
			unset($rdattributes['shared-frequency-bands']);
191
		}
192

    
193
		$parsed_xml = array('main' => $rdmain, 'attributes' => $rdattributes);
194
		$rdcache = fopen($g['tmp_path'] . '/regdomain.cache', "w");
195
		fwrite($rdcache, serialize($parsed_xml));
196
		fclose($rdcache);
197
	}
198

    
199
	return $rdmain;
200
}
201

    
202
function parse_xml_config_raw_attr($cffile, $rootobj, &$parsed_attributes, $isstring = "false") {
203

    
204
	global $depth, $curpath, $parsedcfg, $havedata, $listtags, $parsedattrs;
205
	$parsedcfg = array();
206
	$curpath = array();
207
	$depth = 0;
208
	$havedata = 0;
209

    
210
	if (isset($parsed_attributes)) {
211
		$parsedattrs = array();
212
	}
213

    
214
	$xml_parser = xml_parser_create();
215

    
216
	xml_set_element_handler($xml_parser, "startElement_attr", "endElement_attr");
217
	xml_set_character_data_handler($xml_parser, "cData_attr");
218
	xml_parser_set_option($xml_parser, XML_OPTION_SKIP_WHITE, 1);
219

    
220
	if (!($fp = fopen($cffile, "r"))) {
221
		log_error(gettext("Error: could not open XML input") . "\n");
222
		if (isset($parsed_attributes)) {
223
			$parsed_attributes = array();
224
			unset($parsedattrs);
225
		}
226
		return -1;
227
	}
228

    
229
	while ($data = fread($fp, 4096)) {
230
		if (!xml_parse($xml_parser, $data, feof($fp))) {
231
			log_error(sprintf(gettext('XML error: %1$s at line %2$d') . "\n",
232
				xml_error_string(xml_get_error_code($xml_parser)),
233
				xml_get_current_line_number($xml_parser)));
234
			if (isset($parsed_attributes)) {
235
				$parsed_attributes = array();
236
				unset($parsedattrs);
237
			}
238
			return -1;
239
		}
240
	}
241
	xml_parser_free($xml_parser);
242

    
243
	if (!$parsedcfg[$rootobj]) {
244
		log_error(sprintf(gettext("XML error: no %s object found!") . "\n", $rootobj));
245
		if (isset($parsed_attributes)) {
246
			$parsed_attributes = array();
247
			unset($parsedattrs);
248
		}
249
		return -1;
250
	}
251

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

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

    
262
?>
(61-61/65)