Project

General

Profile

Download (6.47 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
	xmlparse_attr.inc
5
	functions to parse configuration files in XML format with attributes
6
	Copyright (C) 2010 Erik Fonnesbeck
7
	All rights reserved.
8

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

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

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

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

    
23
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
24
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
25
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
27
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32
	POSSIBILITY OF SUCH DAMAGE.
33
*/
34

    
35
/* The following items will be treated as arrays in regdomain.xml */
36
function listtags_rd() {
37
	$ret = explode(" ",
38
		"band country flags freqband netband rd"
39
		);
40
	return $ret;
41
}
42

    
43
function startElement_attr($parser, $name, $attrs) {
44
	global $parsedcfg, $depth, $curpath, $havedata, $listtags, $parsedattrs;
45

    
46
	array_push($curpath, strtolower($name));
47

    
48
	$ptr =& $parsedcfg;
49
	if (!empty($attrs)) {
50
		$attrptr =& $parsedattrs;
51
		$writeattrs = true;
52
	}
53
	foreach ($curpath as $path) {
54
		$ptr =& $ptr[$path];
55
		if (isset($writeattrs)) {
56
			$attrptr =& $attrptr[$path];
57
		}
58
	}
59

    
60
	/* is it an element that belongs to a list? */
61
	if (in_array(strtolower($name), $listtags)) {
62

    
63
		/* is there an array already? */
64
		if (!is_array($ptr)) {
65
			/* make an array */
66
			$ptr = array();
67
		}
68

    
69
		array_push($curpath, count($ptr));
70

    
71
		if (isset($writeattrs)) {
72
			if (!is_array($attrptr)) {
73
				$attrptr = array();
74
			}
75
			$attrptr[count($ptr)] = $attrs;
76
		}
77

    
78
	} else if (isset($ptr)) {
79
		/* multiple entries not allowed for this element, bail out */
80
		die(sprintf(gettext('XML error: %1$s at line %2$d cannot occur more than once') . "\n",
81
			$name,
82
			xml_get_current_line_number($parser)));
83
	} else if (isset($writeattrs)) {
84
		$attrptr = $attrs;
85
	}
86

    
87
	$depth++;
88
	$havedata = $depth;
89
}
90

    
91
function endElement_attr($parser, $name) {
92
	global $depth, $curpath, $parsedcfg, $havedata, $listtags;
93

    
94
	if ($havedata == $depth) {
95
		$ptr =& $parsedcfg;
96
		foreach ($curpath as $path) {
97
			$ptr =& $ptr[$path];
98
		}
99
		$ptr = "";
100
	}
101

    
102
	array_pop($curpath);
103

    
104
	if (in_array(strtolower($name), $listtags)) {
105
		array_pop($curpath);
106
	}
107

    
108
	$depth--;
109
}
110

    
111
function cData_attr($parser, $data) {
112
	global $depth, $curpath, $parsedcfg, $havedata;
113

    
114
	$data = trim($data, "\t\n\r");
115

    
116
	if ($data != "") {
117
		$ptr =& $parsedcfg;
118
		foreach ($curpath as $path) {
119
			$ptr =& $ptr[$path];
120
		}
121

    
122
		if (is_string($ptr)) {
123
			$ptr .= html_entity_decode($data);
124
		} else {
125
			if (trim($data, " ") != "") {
126
				$ptr = html_entity_decode($data);
127
				$havedata++;
128
			}
129
		}
130
	}
131
}
132

    
133
function parse_xml_regdomain(&$rdattributes, $rdfile = '', $rootobj = 'regulatory-data') {
134
	global $g, $listtags;
135

    
136
	if (empty($rdfile)) {
137
		$rdfile = $g['etc_path'] . '/regdomain.xml';
138
	}
139
	$listtags = listtags_rd();
140
	$parsed_xml = array();
141

    
142
	if (file_exists($g['tmp_path'] . '/regdomain.cache')) {
143
		$parsed_xml = unserialize(file_get_contents($g['tmp_path'] . '/regdomain.cache'));
144
		if (!empty($parsed_xml)) {
145
			$rdmain = $parsed_xml['main'];
146
			$rdattributes = $parsed_xml['attributes'];
147
		}
148
	}
149
	if (empty($parsed_xml) && file_exists($g['etc_path'] . '/regdomain.xml')) {
150
		$rdmain = parse_xml_config_raw_attr($rdfile, $rootobj, $rdattributes);
151

    
152
		// unset parts that aren't used before making cache
153
		foreach ($rdmain['regulatory-domains']['rd'] as $rdkey => $rdentry) {
154
			if (isset($rdmain['regulatory-domains']['rd'][$rdkey]['netband'])) {
155
				unset($rdmain['regulatory-domains']['rd'][$rdkey]['netband']);
156
			}
157
			if (isset($rdattributes['regulatory-domains']['rd'][$rdkey]['netband'])) {
158
				unset($rdattributes['regulatory-domains']['rd'][$rdkey]['netband']);
159
			}
160
		}
161
		if (isset($rdmain['shared-frequency-bands'])) {
162
			unset($rdmain['shared-frequency-bands']);
163
		}
164
		if (isset($rdattributes['shared-frequency-bands'])) {
165
			unset($rdattributes['shared-frequency-bands']);
166
		}
167

    
168
		$parsed_xml = array('main' => $rdmain, 'attributes' => $rdattributes);
169
		$rdcache = fopen($g['tmp_path'] . '/regdomain.cache', "w");
170
		fwrite($rdcache, serialize($parsed_xml));
171
		fclose($rdcache);
172
	}
173

    
174
	return $rdmain;
175
}
176

    
177
function parse_xml_config_raw_attr($cffile, $rootobj, &$parsed_attributes, $isstring = "false") {
178

    
179
	global $depth, $curpath, $parsedcfg, $havedata, $listtags, $parsedattrs;
180
	$parsedcfg = array();
181
	$curpath = array();
182
	$depth = 0;
183
	$havedata = 0;
184

    
185
	if (isset($parsed_attributes)) {
186
		$parsedattrs = array();
187
	}
188

    
189
	$xml_parser = xml_parser_create();
190

    
191
	xml_set_element_handler($xml_parser, "startElement_attr", "endElement_attr");
192
	xml_set_character_data_handler($xml_parser, "cData_attr");
193
	xml_parser_set_option($xml_parser, XML_OPTION_SKIP_WHITE, 1);
194

    
195
	if (!($fp = fopen($cffile, "r"))) {
196
		log_error(gettext("Error: could not open XML input") . "\n");
197
		if (isset($parsed_attributes)) {
198
			$parsed_attributes = array();
199
			unset($parsedattrs);
200
		}
201
		return -1;
202
	}
203

    
204
	while ($data = fread($fp, 4096)) {
205
		if (!xml_parse($xml_parser, $data, feof($fp))) {
206
			log_error(sprintf(gettext('XML error: %1$s at line %2$d') . "\n",
207
				xml_error_string(xml_get_error_code($xml_parser)),
208
				xml_get_current_line_number($xml_parser)));
209
			if (isset($parsed_attributes)) {
210
				$parsed_attributes = array();
211
				unset($parsedattrs);
212
			}
213
			return -1;
214
		}
215
	}
216
	xml_parser_free($xml_parser);
217

    
218
	if (!$parsedcfg[$rootobj]) {
219
		log_error(sprintf(gettext("XML error: no %s object found!") . "\n", $rootobj));
220
		if (isset($parsed_attributes)) {
221
			$parsed_attributes = array();
222
			unset($parsedattrs);
223
		}
224
		return -1;
225
	}
226

    
227
	if (isset($parsed_attributes)) {
228
		if ($parsedattrs[$rootobj]) {
229
			$parsed_attributes = $parsedattrs[$rootobj];
230
		}
231
		unset($parsedattrs);
232
	}
233

    
234
	return $parsedcfg[$rootobj];
235
}
236

    
237
?>
(63-63/67)