Project

General

Profile

Download (9.99 KB) Statistics
| Branch: | Tag: | Revision:
1 5b237745 Scott Ullrich
<?php
2
/*
3 ac24dc24 Renato Botelho
 * xmlparse.inc
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6 b8f91b7c Luiz Souza
 * Copyright (c) 2004-2018 Rubicon Communications, LLC (Netgate)
7 ac24dc24 Renato Botelho
 * All rights reserved.
8
 *
9
 * originally part of m0n0wall (http://m0n0.ch/wall)
10 c5d81585 Renato Botelho
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
11 ac24dc24 Renato Botelho
 * All rights reserved.
12
 *
13 b12ea3fb Renato Botelho
 * Licensed under the Apache License, Version 2.0 (the "License");
14
 * you may not use this file except in compliance with the License.
15
 * You may obtain a copy of the License at
16 ac24dc24 Renato Botelho
 *
17 b12ea3fb Renato Botelho
 * http://www.apache.org/licenses/LICENSE-2.0
18 ac24dc24 Renato Botelho
 *
19 b12ea3fb Renato Botelho
 * Unless required by applicable law or agreed to in writing, software
20
 * distributed under the License is distributed on an "AS IS" BASIS,
21
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22
 * See the License for the specific language governing permissions and
23
 * limitations under the License.
24 ac24dc24 Renato Botelho
 */
25 5b237745 Scott Ullrich
26 2e5ebf5d Scott Ullrich
/* The following items will be treated as arrays in config.xml */
27 eeb6c16e Bill Marquette
function listtags() {
28 72a8c829 Ermal
	/*
29
	 * Please keep this list alpha sorted and no longer than 80 characters
30
	 * I know it's a pain, but it's a pain to find stuff too if it's not
31
	 */
32
	$ret = array(
33 79262830 Phil Davis
		'acls', 'alias', 'aliasurl', 'allowedip', 'allowedhostname', 'authserver',
34
		'bridged', 'build_port_path',
35
		'ca', 'cacert', 'cert', 'crl', 'clone', 'config', 'container', 'columnitem',
36 39d2f39d NOYB
		'checkipservice',
37 79262830 Phil Davis
		'depends_on_package', 'disk', 'dnsserver', 'dnsupdate', 'domainoverrides', 'dyndns',
38
		'earlyshellcmd', 'element', 'encryption-algorithm-option',
39
		'field', 'fieldname',
40
		'gateway_item', 'gateway_group', 'gif', 'gre', 'group',
41 40d7e4be NOYB
		'hash-algorithm-option', 'hosts', 'ifgroupentry', 'igmpentry', 'interface_array', 'item', 'key',
42 79262830 Phil Davis
		'lagg', 'lbaction', 'lbpool', 'l7rules', 'lbprotocol',
43
		'member', 'menu', 'tab', 'mobilekey', 'monitor_type', 'mount',
44 72a8c829 Ermal
		'npt', 'ntpserver',
45 79262830 Phil Davis
		'onetoone', 'openvpn-server', 'openvpn-client', 'openvpn-csc', 'option',
46
		'package', 'passthrumac', 'phase1', 'phase2', 'ppp', 'pppoe', 'priv', 'proxyarpnet', 'pool',
47 72a8c829 Ermal
		'qinqentry', 'queue',
48 79262830 Phil Davis
		'pages', 'pipe', 'radnsserver', 'roll', 'route', 'row', 'rrddatafile', 'rule',
49 72a8c829 Ermal
		'schedule', 'service', 'servernat', 'servers',
50 1328b154 Luiz Souza
		'serversdisabled', 'shellcmd', 'staticmap', 'subqueue', 'switch', 'swport',
51
		'timerange', 'tunnel', 'user', 'vip', 'virtual_server', 'vlan', 'vlangroup',
52 72a8c829 Ermal
		'winsserver', 'wolentry', 'widget'
53
	);
54 6d84c956 Ermal
	return array_flip($ret);
55 eeb6c16e Bill Marquette
}
56
57 79262830 Phil Davis
/* Package XML tags that should be treated as a list not as a traditional array */
58 eeb6c16e Bill Marquette
function listtags_pkg() {
59 72a8c829 Ermal
	$ret = array('build_port_path', 'depends_on_package', 'onetoone', 'queue', 'rule', 'servernat', 'alias', 'additional_files_needed', 'tab', 'template', 'menu', 'rowhelperfield', 'service', 'step', 'package', 'columnitem', 'option', 'item', 'field', 'package', 'file');
60 eeb6c16e Bill Marquette
61 1c62178a Ermal
	return array_flip($ret);
62 eeb6c16e Bill Marquette
}
63
64 26433cb8 Scott Ullrich
function startElement($parser, $name, $attrs) {
65 1fb064e8 Erik Fonnesbeck
	global $parsedcfg, $depth, $curpath, $havedata, $listtags;
66 528ae96e Scott Ullrich
67 26433cb8 Scott Ullrich
	array_push($curpath, strtolower($name));
68
69
	$ptr =& $parsedcfg;
70
	foreach ($curpath as $path) {
71
		$ptr =& $ptr[$path];
72
	}
73
74
	/* is it an element that belongs to a list? */
75 6d84c956 Ermal
	if (isset($listtags[strtolower($name)])) {
76 26433cb8 Scott Ullrich
77
		/* is there an array already? */
78
		if (!is_array($ptr)) {
79
			/* make an array */
80
			$ptr = array();
81
		}
82
83
		array_push($curpath, count($ptr));
84
85
	} else if (isset($ptr)) {
86
		/* multiple entries not allowed for this element, bail out */
87 6153d668 PiBa-NL
		throw new Exception(
88
		    sprintf(gettext('XML error: %1$s at line %2$d cannot occur more than once') . "\n",
89 79262830 Phil Davis
		    $name,
90
		    xml_get_current_line_number($parser)));
91 26433cb8 Scott Ullrich
	}
92
93
	$depth++;
94
	$havedata = $depth;
95
}
96
97
function endElement($parser, $name) {
98
	global $depth, $curpath, $parsedcfg, $havedata, $listtags;
99
100
	if ($havedata == $depth) {
101
		$ptr =& $parsedcfg;
102
		foreach ($curpath as $path) {
103
			$ptr =& $ptr[$path];
104
		}
105
		$ptr = "";
106
	}
107
108
	array_pop($curpath);
109
110 79262830 Phil Davis
	if (isset($listtags[strtolower($name)])) {
111 26433cb8 Scott Ullrich
		array_pop($curpath);
112 79262830 Phil Davis
	}
113 26433cb8 Scott Ullrich
114
	$depth--;
115
}
116
117
function cData($parser, $data) {
118
	global $depth, $curpath, $parsedcfg, $havedata;
119
120
	$data = trim($data, "\t\n\r");
121
122
	if ($data != "") {
123
		$ptr =& $parsedcfg;
124
		foreach ($curpath as $path) {
125
			$ptr =& $ptr[$path];
126
		}
127
128
		if (is_string($ptr)) {
129 2e6a43a1 Ermal
			$ptr .= html_entity_decode($data);
130 26433cb8 Scott Ullrich
		} else {
131
			if (trim($data, " ") != "") {
132 2e6a43a1 Ermal
				$ptr = html_entity_decode($data);
133 26433cb8 Scott Ullrich
				$havedata++;
134
			}
135
		}
136
	}
137 5b237745 Scott Ullrich
}
138
139 48ca2dc9 Colin Smith
function parse_xml_config($cffile, $rootobj, $isstring = "false") {
140 cb980a2f Colin Smith
	global $listtags;
141 eeb6c16e Bill Marquette
	$listtags = listtags();
142 79262830 Phil Davis
	if (isset($GLOBALS['custom_listtags'])) {
143
		foreach ($GLOBALS['custom_listtags'] as $tag) {
144
			$listtags[$tag] = $tag;
145
		}
146
	}
147 26433cb8 Scott Ullrich
	return parse_xml_config_raw($cffile, $rootobj, $isstring);
148 cb980a2f Colin Smith
}
149
150 48ca2dc9 Colin Smith
function parse_xml_config_pkg($cffile, $rootobj, $isstring = "false") {
151 cb980a2f Colin Smith
	global $listtags;
152 eeb6c16e Bill Marquette
	$listtags = listtags_pkg();
153 79262830 Phil Davis
	if (isset($GLOBALS['custom_listtags_pkg'])) {
154
		foreach ($GLOBALS['custom_listtags_pkg'] as $tag) {
155
			$listtags[$tag] = $tag;
156
		}
157
	}
158 bef6cb99 Ermal
	$cfg =parse_xml_config_raw($cffile, $rootobj, $isstring);
159 79262830 Phil Davis
	if ($cfg == -1) {
160 187ce62b Ermal
		return array();
161 79262830 Phil Davis
	}
162
163 bef6cb99 Ermal
	return $cfg;
164 cb980a2f Colin Smith
}
165
166 48ca2dc9 Colin Smith
function parse_xml_config_raw($cffile, $rootobj, $isstring = "false") {
167 5b237745 Scott Ullrich
168 26433cb8 Scott Ullrich
	global $depth, $curpath, $parsedcfg, $havedata, $listtags;
169 08c3872d Colin Smith
	$parsedcfg = array();
170 26433cb8 Scott Ullrich
	$curpath = array();
171
	$depth = 0;
172
	$havedata = 0;
173
174
	$xml_parser = xml_parser_create();
175
176
	xml_set_element_handler($xml_parser, "startElement", "endElement");
177
	xml_set_character_data_handler($xml_parser, "cdata");
178 086cf944 Phil Davis
	xml_parser_set_option($xml_parser, XML_OPTION_SKIP_WHITE, 1);
179 26433cb8 Scott Ullrich
180
	if (!($fp = fopen($cffile, "r"))) {
181 ada992bd Renato Botelho
		log_error(gettext("Error: could not open XML input") . "\n");
182 541989d5 Ermal
		return -1;
183 26433cb8 Scott Ullrich
	}
184 d7b6c842 Ermal Lu?i
185 26433cb8 Scott Ullrich
	while ($data = fread($fp, 4096)) {
186
		if (!xml_parse($xml_parser, $data, feof($fp))) {
187 c92ccac7 Vinicius Coque
			log_error(sprintf(gettext('XML error: %1$s at line %2$d in %3$s') . "\n",
188 79262830 Phil Davis
				xml_error_string(xml_get_error_code($xml_parser)),
189
				xml_get_current_line_number($xml_parser),
190
				$cffile));
191 26433cb8 Scott Ullrich
			return -1;
192
		}
193
	}
194
	xml_parser_free($xml_parser);
195
196 428c289f Darren Embry
	if ($rootobj) {
197 79262830 Phil Davis
		if (!is_array($rootobj)) {
198 428c289f Darren Embry
			$rootobj = array($rootobj);
199 79262830 Phil Davis
		}
200
		foreach ($rootobj as $rootobj_name) {
201
			if ($parsedcfg[$rootobj_name]) {
202 428c289f Darren Embry
				break;
203 79262830 Phil Davis
			}
204
		}
205
206 428c289f Darren Embry
		if (!$parsedcfg[$rootobj_name]) {
207
			log_error(sprintf(gettext("XML error: no %s object found!") . "\n", implode(" or ", $rootobj)));
208
			return -1;
209
		}
210
		return $parsedcfg[$rootobj_name];
211
	} else {
212
		return $parsedcfg;
213 26433cb8 Scott Ullrich
	}
214 5b237745 Scott Ullrich
}
215
216
function dump_xml_config_sub($arr, $indent) {
217
218
	global $listtags;
219 528ae96e Scott Ullrich
220 5b237745 Scott Ullrich
	$xmlconfig = "";
221
222
	foreach ($arr as $ent => $val) {
223
		if (is_array($val)) {
224
			/* is it just a list of multiple values? */
225 6d84c956 Ermal
			if (isset($listtags[strtolower($ent)])) {
226 5b237745 Scott Ullrich
				foreach ($val as $cval) {
227
					if (is_array($cval)) {
228 70d6b5c4 Ermal
						if (empty($cval)) {
229
							$xmlconfig .= str_repeat("\t", $indent);
230 da7054b7 Steve Beaver
							$xmlconfig .= "<$ent></$ent>\n";
231 70d6b5c4 Ermal
						} else {
232
							$xmlconfig .= str_repeat("\t", $indent);
233
							$xmlconfig .= "<$ent>\n";
234
							$xmlconfig .= dump_xml_config_sub($cval, $indent + 1);
235
							$xmlconfig .= str_repeat("\t", $indent);
236
							$xmlconfig .= "</$ent>\n";
237
						}
238 5b237745 Scott Ullrich
					} else {
239 79262830 Phil Davis
						if ($cval === false) {
240
							continue;
241
						}
242 70d6b5c4 Ermal
						$xmlconfig .= str_repeat("\t", $indent);
243 79262830 Phil Davis
						if ((is_bool($cval) && $cval == true) || ($cval === "")) {
244 da7054b7 Steve Beaver
							$xmlconfig .= "<$ent></$ent>\n";
245 79262830 Phil Davis
						} else if ((substr($ent, 0, 5) == "descr") ||
246
						    (substr($ent, 0, 6) == "detail") ||
247
						    (substr($ent, 0, 12) == "login_banner") ||
248 00d5594c jim-p
						    (substr($ent, 0, 5) == "state") ||
249
						    (substr($ent, 0, 4) == "city") ||
250
						    (substr($ent, 0, 12) == "organization") ||
251
						    (substr($ent, 0, 5) == "email") ||
252
						    (substr($ent, 0, 6) == "certca") ||
253
						    (substr($ent, 0, 8) == "certname") ||
254 79262830 Phil Davis
						    (substr($ent, 0, 9) == "ldap_attr") ||
255
						    (substr($ent, 0, 9) == "ldap_bind") ||
256
						    (substr($ent, 0, 11) == "ldap_basedn") ||
257 856514f5 Phil Davis
						    (substr($ent, 0, 11) == "ldap_authcn") ||
258 79262830 Phil Davis
						    (substr($ent, 0, 19) == "ldap_extended_query")) {
259 02611466 Ermal
							$xmlconfig .= "<$ent><![CDATA[" . htmlentities($cval) . "]]></$ent>\n";
260 0d57f5f3 Scott Ullrich
						} else {
261 2e6a43a1 Ermal
							$xmlconfig .= "<$ent>" . htmlentities($cval) . "</$ent>\n";
262 70d6b5c4 Ermal
						}
263 5b237745 Scott Ullrich
					}
264
				}
265 78dd5020 Ermal Lu?i
			} else if (empty($val)) {
266
				$xmlconfig .= str_repeat("\t", $indent);
267 da7054b7 Steve Beaver
				$xmlconfig .= "<$ent></$ent>\n";
268 5b237745 Scott Ullrich
			} else {
269
				/* it's an array */
270
				$xmlconfig .= str_repeat("\t", $indent);
271
				$xmlconfig .= "<$ent>\n";
272
				$xmlconfig .= dump_xml_config_sub($val, $indent + 1);
273
				$xmlconfig .= str_repeat("\t", $indent);
274
				$xmlconfig .= "</$ent>\n";
275
			}
276
		} else {
277
			if ((is_bool($val) && ($val == true)) || ($val === "")) {
278
				$xmlconfig .= str_repeat("\t", $indent);
279 da7054b7 Steve Beaver
				$xmlconfig .= "<$ent></$ent>\n";
280 5b237745 Scott Ullrich
			} else if (!is_bool($val)) {
281
				$xmlconfig .= str_repeat("\t", $indent);
282 79262830 Phil Davis
				if ((substr($ent, 0, 5) == "descr") ||
283
				    (substr($ent, 0, 6) == "detail") ||
284
				    (substr($ent, 0, 12) == "login_banner") ||
285 00d5594c jim-p
				    (substr($ent, 0, 5) == "state") ||
286
				    (substr($ent, 0, 4) == "city") ||
287
				    (substr($ent, 0, 12) == "organization") ||
288
				    (substr($ent, 0, 5) == "email") ||
289
				    (substr($ent, 0, 6) == "certca") ||
290
				    (substr($ent, 0, 8) == "certname") ||
291 79262830 Phil Davis
				    (substr($ent, 0, 9) == "ldap_attr") ||
292
				    (substr($ent, 0, 9) == "ldap_bind") ||
293
				    (substr($ent, 0, 11) == "ldap_basedn") ||
294 856514f5 Phil Davis
				    (substr($ent, 0, 11) == "ldap_authcn") ||
295 d1db3f36 Stephen Beaver
				    (substr($ent, 0, 19) == "ldap_extended_query") ||
296 856514f5 Phil Davis
				    (substr($ent, 0, 4) == "text")) {
297 02611466 Ermal
					$xmlconfig .= "<$ent><![CDATA[" . htmlentities($val) . "]]></$ent>\n";
298 086cf944 Phil Davis
				} else {
299 2e6a43a1 Ermal
					$xmlconfig .= "<$ent>" . htmlentities($val) . "</$ent>\n";
300 79262830 Phil Davis
				}
301 5b237745 Scott Ullrich
			}
302
		}
303
	}
304 528ae96e Scott Ullrich
305 5b237745 Scott Ullrich
	return $xmlconfig;
306
}
307
308
function dump_xml_config($arr, $rootobj) {
309 ba6882bf Colin Smith
	global $listtags;
310 eeb6c16e Bill Marquette
	$listtags = listtags();
311 79262830 Phil Davis
	if (isset($GLOBALS['custom_listtags'])) {
312
		foreach ($GLOBALS['custom_listtags'] as $tag) {
313
			$listtags[$tag] = $tag;
314
		}
315
	}
316 ba6882bf Colin Smith
	return dump_xml_config_raw($arr, $rootobj);
317
}
318
319
function dump_xml_config_pkg($arr, $rootobj) {
320
	global $listtags;
321 eeb6c16e Bill Marquette
	$listtags = listtags_pkg();
322 79262830 Phil Davis
	if (isset($GLOBALS['custom_listtags_pkg'])) {
323
		foreach ($GLOBALS['custom_listtags_pkg'] as $tag) {
324
			$listtags[$tag] = $tag;
325
		}
326
	}
327 ba6882bf Colin Smith
	return dump_xml_config_raw($arr, $rootobj);
328
}
329
330
function dump_xml_config_raw($arr, $rootobj) {
331 5b237745 Scott Ullrich
332
	$xmlconfig = "<?xml version=\"1.0\"?" . ">\n";
333
	$xmlconfig .= "<$rootobj>\n";
334 528ae96e Scott Ullrich
335 5b237745 Scott Ullrich
	$xmlconfig .= dump_xml_config_sub($arr, 1);
336 528ae96e Scott Ullrich
337 5b237745 Scott Ullrich
	$xmlconfig .= "</$rootobj>\n";
338 528ae96e Scott Ullrich
339 5b237745 Scott Ullrich
	return $xmlconfig;
340
}
341
342 187ce62b Ermal
?>