Project

General

Profile

Download (9.92 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-2022 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', 'lbaction', 'lbpool', 'l7rules', 'lbprotocol',
45
		'member', 'menu', 'tab', 'mobilekey', '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('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

    
63
	return array_flip($ret);
64
}
65

    
66
function startElement($parser, $name, $attrs) {
67
	global $parsedcfg, $depth, $curpath, $havedata, $listtags;
68

    
69
	array_push($curpath, strtolower($name));
70

    
71
	$ptr =& $parsedcfg;
72
	foreach ($curpath as $path) {
73
		$ptr =& $ptr[$path];
74
	}
75

    
76
	/* is it an element that belongs to a list? */
77
	if (isset($listtags[strtolower($name)])) {
78

    
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
		throw new Exception(
90
		    sprintf(gettext('XML error: %1$s at line %2$d cannot occur more than once') . "\n",
91
		    $name,
92
		    xml_get_current_line_number($parser)));
93
	}
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
		$ptr =& $parsedcfg;
104
		foreach ($curpath as $path) {
105
			$ptr =& $ptr[$path];
106
		}
107
		$ptr = "";
108
	}
109

    
110
	array_pop($curpath);
111

    
112
	/* Special case handling for erroneous empty <config> elements. Pop the most
113
	 * recent element off if it is not an array. */
114
	if (strtolower($name) == 'config') {
115
		$ptr =& $parsedcfg;
116
		foreach ($curpath as $path) {
117
			$ptr = &$ptr[$path];
118
		}
119
		if (!is_array(end($ptr))) {
120
			array_pop($ptr);
121
		}
122
	}
123

    
124
	if (isset($listtags[strtolower($name)])) {
125
		array_pop($curpath);
126
	}
127
	
128
	$depth--;
129
}
130

    
131
function cData($parser, $data) {
132
	global $depth, $curpath, $parsedcfg, $havedata;
133

    
134
	$data = trim($data, "\t\n\r");
135

    
136
	if ($data != "") {
137
		$ptr =& $parsedcfg;
138
		foreach ($curpath as $path) {
139
			$ptr =& $ptr[$path];
140
		}
141

    
142
		if (is_string($ptr)) {
143
			$ptr .= html_entity_decode($data);
144
		} else {
145
			if (trim($data, " ") != "") {
146
				$ptr = html_entity_decode($data);
147
				$havedata++;
148
			}
149
		}
150
	}
151
}
152

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

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

    
177
	return $cfg;
178
}
179

    
180
function parse_xml_config_raw($cffile, $rootobj, $isstring = "false") {
181

    
182
	global $depth, $curpath, $parsedcfg, $havedata, $listtags;
183
	$parsedcfg = array();
184
	$curpath = array();
185
	$depth = 0;
186
	$havedata = 0;
187

    
188
	$xml_parser = xml_parser_create();
189

    
190
	xml_set_element_handler($xml_parser, "startElement", "endElement");
191
	xml_set_character_data_handler($xml_parser, "cdata");
192
	xml_parser_set_option($xml_parser, XML_OPTION_SKIP_WHITE, 1);
193

    
194
	if (!($fp = fopen($cffile, "r"))) {
195
		log_error(gettext("Error: could not open XML input") . "\n");
196
		return -1;
197
	}
198

    
199
	while ($data = fread($fp, 4096)) {
200
		if (!xml_parse($xml_parser, $data, feof($fp))) {
201
			log_error(sprintf(gettext('XML error: %1$s at line %2$d in %3$s') . "\n",
202
				xml_error_string(xml_get_error_code($xml_parser)),
203
				xml_get_current_line_number($xml_parser),
204
				$cffile));
205
			return -1;
206
		}
207
	}
208
	xml_parser_free($xml_parser);
209

    
210
	if ($rootobj) {
211
		if (!is_array($rootobj)) {
212
			$rootobj = array($rootobj);
213
		}
214
		foreach ($rootobj as $rootobj_name) {
215
			if ($parsedcfg[$rootobj_name]) {
216
				break;
217
			}
218
		}
219

    
220
		if (!$parsedcfg[$rootobj_name]) {
221
			log_error(sprintf(gettext("XML error: no %s object found!") . "\n", implode(" or ", $rootobj)));
222
			return -1;
223
		}
224
		return $parsedcfg[$rootobj_name];
225
	} else {
226
		return $parsedcfg;
227
	}
228
}
229

    
230
/* Return true if a field should be cdata encoded */
231
function is_cdata_entity($ent) {
232
	$cdata_fields = array(
233
		'aclname', 'auth_pass', 'auth_prompt', 'auth_user', 'certca', 'certname', 'city', 'common_name',
234
		'descr', 'detail', 'email', 'encryption_password', 'hint', 'ldap_attr',
235
		'ldap_authcn', 'ldap_basedn', 'ldap_basedomain', 'ldap_bind', 'ldapbinddn',
236
		'ldapbindpass', 'ldap_extended_query', 'ldap_filter', 'ldap_pam_groupdn', 'ldap_pass', 'ldap_user',
237
		'login_banner', 'organization', 'password', 'rangedescr', 'state', 'text',
238
		'username', 'varusersusername', 'varuserspassword'
239
	);
240

    
241
	/* Check if the entity name starts with any of the strings above */
242
	return preg_match('/(^' . implode('|^', $cdata_fields) . ')/', $ent) === 1;
243
}
244

    
245
function dump_xml_config_sub($arr, $indent) {
246

    
247
	global $listtags;
248

    
249
	if (!is_array($arr)) {
250
		return null;
251
	}
252

    
253
	$xmlconfig = "";
254

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

    
311
	return $xmlconfig;
312
}
313

    
314
function dump_xml_config($arr, $rootobj) {
315
	global $listtags;
316
	$listtags = listtags();
317
	if (isset($GLOBALS['custom_listtags'])) {
318
		foreach ($GLOBALS['custom_listtags'] as $tag) {
319
			$listtags[$tag] = $tag;
320
		}
321
	}
322
	return dump_xml_config_raw($arr, $rootobj);
323
}
324

    
325
function dump_xml_config_pkg($arr, $rootobj) {
326
	global $listtags;
327
	$listtags = listtags_pkg();
328
	if (isset($GLOBALS['custom_listtags_pkg'])) {
329
		foreach ($GLOBALS['custom_listtags_pkg'] as $tag) {
330
			$listtags[$tag] = $tag;
331
		}
332
	}
333
	return dump_xml_config_raw($arr, $rootobj);
334
}
335

    
336
function dump_xml_config_raw($arr, $rootobj) {
337

    
338
	$xmlconfig = "<?xml version=\"1.0\"?" . ">\n";
339
	$xmlconfig .= "<$rootobj>\n";
340

    
341
	$xmlconfig .= dump_xml_config_sub($arr, 1);
342

    
343
	$xmlconfig .= "</$rootobj>\n";
344

    
345
	return $xmlconfig;
346
}
347

    
348
?>
(59-59/61)