Project

General

Profile

Download (9.43 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-2020 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', 'container', 'columnitem',
38
		'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',
52
		'serversdisabled', 'shellcmd', 'staticmap', 'subqueue', 'switch', 'swport',
53
		'timerange', 'tunnel', 'user', 'vip', 'virtual_server', 'vlan', 'vlangroup',
54
		'winsserver', 'wolentry', 'widget'
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
	if (isset($listtags[strtolower($name)])) {
113
		array_pop($curpath);
114
	}
115

    
116
	$depth--;
117
}
118

    
119
function cData($parser, $data) {
120
	global $depth, $curpath, $parsedcfg, $havedata;
121

    
122
	$data = trim($data, "\t\n\r");
123

    
124
	if ($data != "") {
125
		$ptr =& $parsedcfg;
126
		foreach ($curpath as $path) {
127
			$ptr =& $ptr[$path];
128
		}
129

    
130
		if (is_string($ptr)) {
131
			$ptr .= html_entity_decode($data);
132
		} else {
133
			if (trim($data, " ") != "") {
134
				$ptr = html_entity_decode($data);
135
				$havedata++;
136
			}
137
		}
138
	}
139
}
140

    
141
function parse_xml_config($cffile, $rootobj, $isstring = "false") {
142
	global $listtags;
143
	$listtags = listtags();
144
	if (isset($GLOBALS['custom_listtags'])) {
145
		foreach ($GLOBALS['custom_listtags'] as $tag) {
146
			$listtags[$tag] = $tag;
147
		}
148
	}
149
	return parse_xml_config_raw($cffile, $rootobj, $isstring);
150
}
151

    
152
function parse_xml_config_pkg($cffile, $rootobj, $isstring = "false") {
153
	global $listtags;
154
	$listtags = listtags_pkg();
155
	if (isset($GLOBALS['custom_listtags_pkg'])) {
156
		foreach ($GLOBALS['custom_listtags_pkg'] as $tag) {
157
			$listtags[$tag] = $tag;
158
		}
159
	}
160
	$cfg =parse_xml_config_raw($cffile, $rootobj, $isstring);
161
	if ($cfg == -1) {
162
		return array();
163
	}
164

    
165
	return $cfg;
166
}
167

    
168
function parse_xml_config_raw($cffile, $rootobj, $isstring = "false") {
169

    
170
	global $depth, $curpath, $parsedcfg, $havedata, $listtags;
171
	$parsedcfg = array();
172
	$curpath = array();
173
	$depth = 0;
174
	$havedata = 0;
175

    
176
	$xml_parser = xml_parser_create();
177

    
178
	xml_set_element_handler($xml_parser, "startElement", "endElement");
179
	xml_set_character_data_handler($xml_parser, "cdata");
180
	xml_parser_set_option($xml_parser, XML_OPTION_SKIP_WHITE, 1);
181

    
182
	if (!($fp = fopen($cffile, "r"))) {
183
		log_error(gettext("Error: could not open XML input") . "\n");
184
		return -1;
185
	}
186

    
187
	while ($data = fread($fp, 4096)) {
188
		if (!xml_parse($xml_parser, $data, feof($fp))) {
189
			log_error(sprintf(gettext('XML error: %1$s at line %2$d in %3$s') . "\n",
190
				xml_error_string(xml_get_error_code($xml_parser)),
191
				xml_get_current_line_number($xml_parser),
192
				$cffile));
193
			return -1;
194
		}
195
	}
196
	xml_parser_free($xml_parser);
197

    
198
	if ($rootobj) {
199
		if (!is_array($rootobj)) {
200
			$rootobj = array($rootobj);
201
		}
202
		foreach ($rootobj as $rootobj_name) {
203
			if ($parsedcfg[$rootobj_name]) {
204
				break;
205
			}
206
		}
207

    
208
		if (!$parsedcfg[$rootobj_name]) {
209
			log_error(sprintf(gettext("XML error: no %s object found!") . "\n", implode(" or ", $rootobj)));
210
			return -1;
211
		}
212
		return $parsedcfg[$rootobj_name];
213
	} else {
214
		return $parsedcfg;
215
	}
216
}
217

    
218
/* Return true if a field should be cdata encoded */
219
function is_cdata_entity($ent) {
220
	$cdata_fields = array(
221
		'auth_pass', 'auth_prompt', 'auth_user', 'certca', 'certname', 'city', 'common_name',
222
		'descr', 'detail', 'email', 'encryption_password', 'ldap_attr',
223
		'ldap_authcn', 'ldap_basedn', 'ldap_bind',
224
		'ldap_extended_query', 'login_banner', 'organization', 'password', 'rangedescr',
225
		'state', 'text', 'username', 'varusersusername', 'varuserspassword'
226
	);
227

    
228
	/* Check if the entity name starts with any of the strings above */
229
	return preg_match('/(^' . implode('|^', $cdata_fields) . ')/', $ent) === 1;
230
}
231

    
232
function dump_xml_config_sub($arr, $indent) {
233

    
234
	global $listtags;
235

    
236
	if (!is_array($arr)) {
237
		return null;
238
	}
239

    
240
	$xmlconfig = "";
241

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

    
298
	return $xmlconfig;
299
}
300

    
301
function dump_xml_config($arr, $rootobj) {
302
	global $listtags;
303
	$listtags = listtags();
304
	if (isset($GLOBALS['custom_listtags'])) {
305
		foreach ($GLOBALS['custom_listtags'] as $tag) {
306
			$listtags[$tag] = $tag;
307
		}
308
	}
309
	return dump_xml_config_raw($arr, $rootobj);
310
}
311

    
312
function dump_xml_config_pkg($arr, $rootobj) {
313
	global $listtags;
314
	$listtags = listtags_pkg();
315
	if (isset($GLOBALS['custom_listtags_pkg'])) {
316
		foreach ($GLOBALS['custom_listtags_pkg'] as $tag) {
317
			$listtags[$tag] = $tag;
318
		}
319
	}
320
	return dump_xml_config_raw($arr, $rootobj);
321
}
322

    
323
function dump_xml_config_raw($arr, $rootobj) {
324

    
325
	$xmlconfig = "<?xml version=\"1.0\"?" . ">\n";
326
	$xmlconfig .= "<$rootobj>\n";
327

    
328
	$xmlconfig .= dump_xml_config_sub($arr, 1);
329

    
330
	$xmlconfig .= "</$rootobj>\n";
331

    
332
	return $xmlconfig;
333
}
334

    
335
?>
(57-57/60)