Project

General

Profile

Download (9.99 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * xmlparse.inc
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2018 Rubicon Communications, LLC (Netgate)
7
 * All rights reserved.
8
 *
9
 * originally part of m0n0wall (http://m0n0.ch/wall)
10
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
11
 * All rights reserved.
12
 *
13
 * 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
 *
17
 * http://www.apache.org/licenses/LICENSE-2.0
18
 *
19
 * 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
 */
25

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

    
57
/* Package XML tags that should be treated as a list not as a traditional array */
58
function listtags_pkg() {
59
	$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

    
61
	return array_flip($ret);
62
}
63

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

    
67
	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
	if (isset($listtags[strtolower($name)])) {
76

    
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
		throw new Exception(
88
		    sprintf(gettext('XML error: %1$s at line %2$d cannot occur more than once') . "\n",
89
		    $name,
90
		    xml_get_current_line_number($parser)));
91
	}
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
	if (isset($listtags[strtolower($name)])) {
111
		array_pop($curpath);
112
	}
113

    
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
			$ptr .= html_entity_decode($data);
130
		} else {
131
			if (trim($data, " ") != "") {
132
				$ptr = html_entity_decode($data);
133
				$havedata++;
134
			}
135
		}
136
	}
137
}
138

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

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

    
163
	return $cfg;
164
}
165

    
166
function parse_xml_config_raw($cffile, $rootobj, $isstring = "false") {
167

    
168
	global $depth, $curpath, $parsedcfg, $havedata, $listtags;
169
	$parsedcfg = array();
170
	$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
	xml_parser_set_option($xml_parser, XML_OPTION_SKIP_WHITE, 1);
179

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

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

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

    
206
		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
	}
214
}
215

    
216
function dump_xml_config_sub($arr, $indent) {
217

    
218
	global $listtags;
219

    
220
	$xmlconfig = "";
221

    
222
	foreach ($arr as $ent => $val) {
223
		if (is_array($val)) {
224
			/* is it just a list of multiple values? */
225
			if (isset($listtags[strtolower($ent)])) {
226
				foreach ($val as $cval) {
227
					if (is_array($cval)) {
228
						if (empty($cval)) {
229
							$xmlconfig .= str_repeat("\t", $indent);
230
							$xmlconfig .= "<$ent></$ent>\n";
231
						} 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
					} else {
239
						if ($cval === false) {
240
							continue;
241
						}
242
						$xmlconfig .= str_repeat("\t", $indent);
243
						if ((is_bool($cval) && $cval == true) || ($cval === "")) {
244
							$xmlconfig .= "<$ent></$ent>\n";
245
						} else if ((substr($ent, 0, 5) == "descr") ||
246
						    (substr($ent, 0, 6) == "detail") ||
247
						    (substr($ent, 0, 12) == "login_banner") ||
248
						    (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
						    (substr($ent, 0, 9) == "ldap_attr") ||
255
						    (substr($ent, 0, 9) == "ldap_bind") ||
256
						    (substr($ent, 0, 11) == "ldap_basedn") ||
257
						    (substr($ent, 0, 11) == "ldap_authcn") ||
258
						    (substr($ent, 0, 19) == "ldap_extended_query")) {
259
							$xmlconfig .= "<$ent><![CDATA[" . htmlentities($cval) . "]]></$ent>\n";
260
						} else {
261
							$xmlconfig .= "<$ent>" . htmlentities($cval) . "</$ent>\n";
262
						}
263
					}
264
				}
265
			} else if (empty($val)) {
266
				$xmlconfig .= str_repeat("\t", $indent);
267
				$xmlconfig .= "<$ent></$ent>\n";
268
			} 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
				$xmlconfig .= "<$ent></$ent>\n";
280
			} else if (!is_bool($val)) {
281
				$xmlconfig .= str_repeat("\t", $indent);
282
				if ((substr($ent, 0, 5) == "descr") ||
283
				    (substr($ent, 0, 6) == "detail") ||
284
				    (substr($ent, 0, 12) == "login_banner") ||
285
				    (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
				    (substr($ent, 0, 9) == "ldap_attr") ||
292
				    (substr($ent, 0, 9) == "ldap_bind") ||
293
				    (substr($ent, 0, 11) == "ldap_basedn") ||
294
				    (substr($ent, 0, 11) == "ldap_authcn") ||
295
				    (substr($ent, 0, 19) == "ldap_extended_query") ||
296
				    (substr($ent, 0, 4) == "text")) {
297
					$xmlconfig .= "<$ent><![CDATA[" . htmlentities($val) . "]]></$ent>\n";
298
				} else {
299
					$xmlconfig .= "<$ent>" . htmlentities($val) . "</$ent>\n";
300
				}
301
			}
302
		}
303
	}
304

    
305
	return $xmlconfig;
306
}
307

    
308
function dump_xml_config($arr, $rootobj) {
309
	global $listtags;
310
	$listtags = listtags();
311
	if (isset($GLOBALS['custom_listtags'])) {
312
		foreach ($GLOBALS['custom_listtags'] as $tag) {
313
			$listtags[$tag] = $tag;
314
		}
315
	}
316
	return dump_xml_config_raw($arr, $rootobj);
317
}
318

    
319
function dump_xml_config_pkg($arr, $rootobj) {
320
	global $listtags;
321
	$listtags = listtags_pkg();
322
	if (isset($GLOBALS['custom_listtags_pkg'])) {
323
		foreach ($GLOBALS['custom_listtags_pkg'] as $tag) {
324
			$listtags[$tag] = $tag;
325
		}
326
	}
327
	return dump_xml_config_raw($arr, $rootobj);
328
}
329

    
330
function dump_xml_config_raw($arr, $rootobj) {
331

    
332
	$xmlconfig = "<?xml version=\"1.0\"?" . ">\n";
333
	$xmlconfig .= "<$rootobj>\n";
334

    
335
	$xmlconfig .= dump_xml_config_sub($arr, 1);
336

    
337
	$xmlconfig .= "</$rootobj>\n";
338

    
339
	return $xmlconfig;
340
}
341

    
342
?>
(57-57/60)