Project

General

Profile

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