Project

General

Profile

Download (5.92 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-2013 BSD Perimeter
7
 * Copyright (c) 2013-2016 Electric Sheep Fencing
8
 * Copyright (c) 2014-2025 Rubicon Communications, LLC (Netgate)
9
 * Copyright (c) 2010 Erik Fonnesbeck
10
 * All rights reserved.
11
 *
12
 * originally part of m0n0wall (http://m0n0.ch/wall)
13
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
14
 * All rights reserved.
15
 *
16
 * 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
 *
20
 * http://www.apache.org/licenses/LICENSE-2.0
21
 *
22
 * 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
 */
28

    
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
		if (isset($writeattrs)) {
50
			$attrptr =& $attrptr[$path];
51
		}
52
	}
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
			if (!is_array($attrptr)) {
67
				$attrptr = array();
68
			}
69
			$attrptr[count($ptr)] = $attrs;
70
		}
71

    
72
	} else if (isset($ptr)) {
73
		/* multiple entries not allowed for this element, bail out */
74
		die(sprintf(gettext('XML error: %1$s at line %2$d cannot occur more than once') . "\n",
75
			$name,
76
			xml_get_current_line_number($parser)));
77
	} 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
	if (in_array(strtolower($name), $listtags)) {
99
		array_pop($curpath);
100
	}
101

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

    
130
	if (empty($rdfile)) {
131
		$rdfile = g_get('etc_path') . '/regdomain.xml';
132
	}
133
	$listtags = listtags_rd();
134
	$parsed_xml = array();
135

    
136
	if (file_exists(g_get('tmp_path') . '/regdomain.cache')) {
137
		$parsed_xml = unserialize(file_get_contents(g_get('tmp_path') . '/regdomain.cache'));
138
		if (!empty($parsed_xml)) {
139
			$rdmain = $parsed_xml['main'];
140
			$rdattributes = $parsed_xml['attributes'];
141
		}
142
	}
143
	if (empty($parsed_xml) && file_exists(g_get('etc_path') . '/regdomain.xml')) {
144
		$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
			if (isset($rdmain['regulatory-domains']['rd'][$rdkey]['netband'])) {
149
				unset($rdmain['regulatory-domains']['rd'][$rdkey]['netband']);
150
			}
151
			if (isset($rdattributes['regulatory-domains']['rd'][$rdkey]['netband'])) {
152
				unset($rdattributes['regulatory-domains']['rd'][$rdkey]['netband']);
153
			}
154
		}
155
		if (isset($rdmain['shared-frequency-bands'])) {
156
			unset($rdmain['shared-frequency-bands']);
157
		}
158
		if (isset($rdattributes['shared-frequency-bands'])) {
159
			unset($rdattributes['shared-frequency-bands']);
160
		}
161

    
162
		$parsed_xml = array('main' => $rdmain, 'attributes' => $rdattributes);
163
		$rdcache = fopen(g_get('tmp_path') . '/regdomain.cache', "w");
164
		fwrite($rdcache, serialize($parsed_xml));
165
		fclose($rdcache);
166
	}
167

    
168
	return $rdmain;
169
}
170

    
171
function parse_xml_config_raw_attr($cffile, $rootobj, &$parsed_attributes) {
172

    
173
	global $depth, $curpath, $parsedcfg, $havedata, $listtags, $parsedattrs;
174
	$parsedcfg = array();
175
	$curpath = array();
176
	$depth = 0;
177
	$havedata = 0;
178

    
179
	if (isset($parsed_attributes)) {
180
		$parsedattrs = array();
181
	}
182

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

    
189
	if (!($fp = fopen($cffile, "r"))) {
190
		log_error(gettext("Error: could not open XML input") . "\n");
191
		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
			log_error(sprintf(gettext('XML error: %1$s at line %2$d') . "\n",
201
				xml_error_string(xml_get_error_code($xml_parser)),
202
				xml_get_current_line_number($xml_parser)));
203
			if (isset($parsed_attributes)) {
204
				$parsed_attributes = array();
205
				unset($parsedattrs);
206
			}
207
			return -1;
208
		}
209
	}
210

    
211
	if (!$parsedcfg[$rootobj]) {
212
		log_error(sprintf(gettext("XML error: no %s object found!") . "\n", $rootobj));
213
		if (isset($parsed_attributes)) {
214
			$parsed_attributes = array();
215
			unset($parsedattrs);
216
		}
217
		return -1;
218
	}
219

    
220
	if (isset($parsed_attributes)) {
221
		if ($parsedattrs[$rootobj]) {
222
			$parsed_attributes = $parsedattrs[$rootobj];
223
		}
224
		unset($parsedattrs);
225
	}
226

    
227
	return $parsedcfg[$rootobj];
228
}
229

    
230
?>
(60-60/61)