Project

General

Profile

Download (10.2 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * xmlparse.inc
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2013 BSD Perimeter
7
 * Copyright (c) 2013-2016 Electric Sheep Fencing
8
 * Copyright (c) 2014-2025 Rubicon Communications, LLC (Netgate)
9
 * All rights reserved.
10
 *
11
 * originally part of m0n0wall (http://m0n0.ch/wall)
12
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
13
 * All rights reserved.
14
 *
15
 * 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
 *
19
 * http://www.apache.org/licenses/LICENSE-2.0
20
 *
21
 * 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
 */
27

    
28
/* The following items will be treated as arrays in config.xml */
29
function listtags() {
30
	/*
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
		'acls', 'alias', 'aliasurl', 'allowedip', 'allowedhostname', 'authserver',
36
		'bridged', 'build_port_path',
37
		'ca', 'cacert', 'cert', 'crl', 'clone', 'config',
38
		'container', 'columnitem', 'checkipservice',
39
		'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
		'hash-algorithm-option', 'hosts', 'ifgroupentry', 'igmpentry', 'interface_array', 'item', 'key',
44
		'lagg', 'laggroup', 'lbaction', 'lbpool', 'l7rules', 'lbprotocol',
45
		'member', 'menu', 'tab', 'mobilekey', 'mobilegroup', 'monitor_type', 'mount',
46
		'npt', 'ntpserver',
47
		'onetoone', 'openvpn-server', 'openvpn-client', 'openvpn-csc', 'option',
48
		'package', 'passthrumac', 'phase1', 'phase2', 'ppp', 'pppoe', 'priv', 'proxyarpnet', 'pool',
49
		'qinqentry', 'queue',
50
		'pages', 'pipe', 'radnsserver', 'roll', 'route', 'row', 'rrddatafile', 'rule',
51
		'schedule', 'service', 'servernat', 'servers', 'sshkeyfile',
52
		'serversdisabled', 'shellcmd', 'staticmap', 'subqueue', 'switch', 'swport',
53
		'timerange', 'tunnel', 'user', 'vip', 'virtual_server', 'vlan', 'vlangroup', 'voucherdbfile',
54
		'vxlan', 'wgpeer', 'winsserver', 'wolentry', 'widget', 'xmldatafile'
55
	);
56
	return array_flip($ret);
57
}
58

    
59
/* Package XML tags that should be treated as a list not as a traditional array */
60
function listtags_pkg() {
61
	$ret = array(
62
		'additional_files_needed', 'alias', 'build_port_path', 'class', 'columnitem', 'depends_on_package',
63
		'field', 'file', 'item', 'menu', 'onetoone', 'option', 'package', 'queue', 'rowhelperfield', 'rule', 'servernat',
64
		'service', 'step', 'tab', 'template'
65
	);
66
	return array_flip($ret);
67
}
68

    
69
function startElement($parser, $name, $attrs) {
70
	global $parsedcfg, $depth, $curpath, $havedata, $listtags;
71

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

    
74
	$ptr = &$parsedcfg;
75
	foreach ($curpath as $path) {
76
		$ptr = &$ptr[$path];
77
	}
78

    
79
	/* is it an element that belongs to a list? */
80
	if (isset($listtags[strtolower($name)])) {
81

    
82
		/* is there an array already? */
83
		if (!is_array($ptr)) {
84
			/* make an array */
85
			$ptr = array();
86
		}
87

    
88
		array_push($curpath, count($ptr));
89

    
90
	} else if (isset($ptr)) {
91
		/* multiple entries not allowed for this element, bail out */
92
		throw new Exception(
93
		    sprintf(gettext('XML error: %1$s at line %2$d cannot occur more than once') . "\n",
94
		    $name,
95
		    xml_get_current_line_number($parser)));
96
	}
97

    
98
	$depth++;
99
	$havedata = $depth;
100
}
101

    
102
function endElement($parser, $name) {
103
	global $depth, $curpath, $parsedcfg, $havedata, $listtags;
104

    
105
	if ($havedata == $depth) {
106
		$ptr = &$parsedcfg;
107
		foreach ($curpath as $path) {
108
			$ptr = &$ptr[$path];
109
		}
110
		$ptr = "";
111
	}
112

    
113
	array_pop($curpath);
114

    
115
	/* Special case handling for erroneous empty $listtag elements. Pop the most
116
	 * recent $listtag element off if it is a string AND is zero length. */
117
	if (isset($listtags[strtolower($name)])) {
118
		$ptr = &$parsedcfg;
119
		foreach ($curpath as $path) {
120
			$ptr = &$ptr[$path];
121
		}
122

    
123
		if (!(is_array(end($ptr)) || strlen(end($ptr)))) {
124
			array_pop($ptr);
125
		}
126
	}
127

    
128
	if (isset($listtags[strtolower($name)])) {
129
		array_pop($curpath);
130
	}
131

    
132
	$depth--;
133
}
134

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

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

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

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

    
157
function parse_xml_config($cffile, $rootobj, $isstring = "false") {
158
	global $listtags;
159
	$listtags = listtags();
160
	if (isset($GLOBALS['custom_listtags'])) {
161
		foreach ($GLOBALS['custom_listtags'] as $tag) {
162
			$listtags[$tag] = $tag;
163
		}
164
	}
165
	return parse_xml_config_raw($cffile, $rootobj, $isstring);
166
}
167

    
168
function parse_xml_config_pkg($cffile, $rootobj, $isstring = "false") {
169
	global $listtags;
170
	$listtags = listtags_pkg();
171
	if (isset($GLOBALS['custom_listtags_pkg'])) {
172
		foreach ($GLOBALS['custom_listtags_pkg'] as $tag) {
173
			$listtags[$tag] = $tag;
174
		}
175
	}
176
	$cfg =parse_xml_config_raw($cffile, $rootobj, $isstring);
177
	if ($cfg == -1) {
178
		return array();
179
	}
180

    
181
	return $cfg;
182
}
183

    
184
function parse_xml_config_raw($cffile, $rootobj, $isstring = "false") {
185

    
186
	global $depth, $curpath, $parsedcfg, $havedata, $listtags;
187
	$parsedcfg = array();
188
	$curpath = array();
189
	$depth = 0;
190
	$havedata = 0;
191

    
192
	$xml_parser = xml_parser_create();
193

    
194
	xml_set_element_handler($xml_parser, "startElement", "endElement");
195
	xml_set_character_data_handler($xml_parser, "cdata");
196
	xml_parser_set_option($xml_parser, XML_OPTION_SKIP_WHITE, 1);
197

    
198
	if (!($fp = fopen($cffile, "r"))) {
199
		log_error(gettext("Error: could not open XML input") . "\n");
200
		return -1;
201
	}
202

    
203
	while ($data = fread($fp, 4096)) {
204
		try {
205
			if (!xml_parse($xml_parser, $data, feof($fp))) {
206
				log_error(sprintf(gettext('XML error: %1$s at line %2$d in %3$s') . "\n",
207
					xml_error_string(xml_get_error_code($xml_parser)),
208
					xml_get_current_line_number($xml_parser),
209
					$cffile));
210
				return -1;
211
			}
212
		} catch (\Throwable | \Error | \Exception $e) {
213
			log_error($e->getMessage());
214
			return -1;
215
		}
216
	}
217

    
218
	if ($rootobj) {
219
		if (!is_array($rootobj)) {
220
			$rootobj = array($rootobj);
221
		}
222
		foreach ($rootobj as $rootobj_name) {
223
			if ($parsedcfg[$rootobj_name]) {
224
				break;
225
			}
226
		}
227

    
228
		if (!$parsedcfg[$rootobj_name]) {
229
			log_error(sprintf(gettext("XML error: no %s object found!") . "\n", implode(" or ", $rootobj)));
230
			return -1;
231
		}
232
		return $parsedcfg[$rootobj_name];
233
	} else {
234
		return $parsedcfg;
235
	}
236
}
237

    
238
/* Return true if a field should be cdata encoded */
239
function is_cdata_entity($ent) {
240
	$cdata_fields = array(
241
		'aclname', 'auth_pass', 'auth_prompt', 'auth_user', 'certca', 'certname', 'city', 'common_name',
242
		'descr', 'detail', 'email', 'encryption_password', 'hint', 'ldap_attr',
243
		'ldap_authcn', 'ldap_basedn', 'ldap_basedomain', 'ldap_bind', 'ldapbinddn',
244
		'ldapbindpass', 'ldap_extended_query', 'ldap_filter', 'ldap_pam_groupdn', 'ldap_pass', 'ldap_user',
245
		'login_banner', 'organization', 'password', 'proxypass', 'proxyuser', 'rangedescr', 'state', 'text',
246
		'username', 'varusersusername', 'varuserspassword'
247
	);
248

    
249
	/* Check if the entity name starts with any of the strings above */
250
	return preg_match('/(^' . implode('|^', $cdata_fields) . ')/', $ent) === 1;
251
}
252

    
253
function dump_xml_config_sub($arr, $indent) {
254

    
255
	global $listtags;
256

    
257
	if (!is_array($arr)) {
258
		return null;
259
	}
260

    
261
	$xmlconfig = "";
262

    
263
	foreach ($arr as $ent => $val) {
264
		if (is_array($val)) {
265
			/* is it just a list of multiple values? */
266
			if (isset($listtags[strtolower($ent)])) {
267
				foreach ($val as $cval) {
268
					if (is_array($cval)) {
269
						if (empty($cval)) {
270
							$xmlconfig .= str_repeat("\t", $indent);
271
							$xmlconfig .= "<{$ent}></{$ent}>\n";
272
						} else {
273
							$xmlconfig .= str_repeat("\t", $indent);
274
							$xmlconfig .= "<{$ent}>\n";
275
							$xmlconfig .= dump_xml_config_sub($cval, $indent + 1);
276
							$xmlconfig .= str_repeat("\t", $indent);
277
							$xmlconfig .= "</{$ent}>\n";
278
						}
279
					} else {
280
						if ($cval === false) {
281
							continue;
282
						}
283
						$xmlconfig .= str_repeat("\t", $indent);
284
						if ((is_bool($cval) && $cval == true) || ($cval === "")) {
285
							$xmlconfig .= "<{$ent}></{$ent}>\n";
286
						} else if (is_cdata_entity($ent)) {
287
							$xmlconfig .= "<{$ent}><![CDATA[" . htmlentities($cval) . "]]></{$ent}>\n";
288
						} else {
289
							$xmlconfig .= "<{$ent}>" . htmlentities($cval) . "</{$ent}>\n";
290
						}
291
					}
292
				}
293
			} else if (empty($val)) {
294
				$xmlconfig .= str_repeat("\t", $indent);
295
				$xmlconfig .= "<{$ent}></{$ent}>\n";
296
			} else {
297
				/* it's an array */
298
				$xmlconfig .= str_repeat("\t", $indent);
299
				$xmlconfig .= "<{$ent}>\n";
300
				$xmlconfig .= dump_xml_config_sub($val, $indent + 1);
301
				$xmlconfig .= str_repeat("\t", $indent);
302
				$xmlconfig .= "</{$ent}>\n";
303
			}
304
		} else {
305
			if ((is_bool($val) && ($val == true)) || ($val === "")) {
306
				$xmlconfig .= str_repeat("\t", $indent);
307
				$xmlconfig .= "<{$ent}></{$ent}>\n";
308
			} else if (!is_bool($val)) {
309
				$xmlconfig .= str_repeat("\t", $indent);
310
				if (is_cdata_entity($ent)) {
311
					$xmlconfig .= "<{$ent}><![CDATA[" . htmlentities($val) . "]]></{$ent}>\n";
312
				} else {
313
					$xmlconfig .= "<{$ent}>" . htmlentities($val) . "</{$ent}>\n";
314
				}
315
			}
316
		}
317
	}
318

    
319
	return $xmlconfig;
320
}
321

    
322
function dump_xml_config($arr, $rootobj) {
323
	global $listtags;
324
	$listtags = listtags();
325
	if (isset($GLOBALS['custom_listtags'])) {
326
		foreach ($GLOBALS['custom_listtags'] as $tag) {
327
			$listtags[$tag] = $tag;
328
		}
329
	}
330
	return dump_xml_config_raw($arr, $rootobj);
331
}
332

    
333
function dump_xml_config_pkg($arr, $rootobj) {
334
	global $listtags;
335
	$listtags = listtags_pkg();
336
	if (isset($GLOBALS['custom_listtags_pkg'])) {
337
		foreach ($GLOBALS['custom_listtags_pkg'] as $tag) {
338
			$listtags[$tag] = $tag;
339
		}
340
	}
341
	return dump_xml_config_raw($arr, $rootobj);
342
}
343

    
344
function dump_xml_config_raw($arr, $rootobj) {
345

    
346
	$xmlconfig = "<?xml version=\"1.0\"?" . ">\n";
347
	$xmlconfig .= "<{$rootobj}>\n";
348

    
349
	$xmlconfig .= dump_xml_config_sub($arr, 1);
350

    
351
	$xmlconfig .= "</{$rootobj}>\n";
352

    
353
	return $xmlconfig;
354
}
(59-59/61)