Project

General

Profile

Download (9.4 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * xmlparse.inc
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2016 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',
51
		'timerange', 'tunnel', 'user', 'vip', 'virtual_server', 'vlan',
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
		die(sprintf(gettext('XML error: %1$s at line %2$d cannot occur more than once') . "\n",
88
		    $name,
89
		    xml_get_current_line_number($parser)));
90
	}
91

    
92
	$depth++;
93
	$havedata = $depth;
94
}
95

    
96
function endElement($parser, $name) {
97
	global $depth, $curpath, $parsedcfg, $havedata, $listtags;
98

    
99
	if ($havedata == $depth) {
100
		$ptr =& $parsedcfg;
101
		foreach ($curpath as $path) {
102
			$ptr =& $ptr[$path];
103
		}
104
		$ptr = "";
105
	}
106

    
107
	array_pop($curpath);
108

    
109
	if (isset($listtags[strtolower($name)])) {
110
		array_pop($curpath);
111
	}
112

    
113
	$depth--;
114
}
115

    
116
function cData($parser, $data) {
117
	global $depth, $curpath, $parsedcfg, $havedata;
118

    
119
	$data = trim($data, "\t\n\r");
120

    
121
	if ($data != "") {
122
		$ptr =& $parsedcfg;
123
		foreach ($curpath as $path) {
124
			$ptr =& $ptr[$path];
125
		}
126

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

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

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

    
162
	return $cfg;
163
}
164

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

    
167
	global $depth, $curpath, $parsedcfg, $havedata, $listtags;
168
	$parsedcfg = array();
169
	$curpath = array();
170
	$depth = 0;
171
	$havedata = 0;
172

    
173
	$xml_parser = xml_parser_create();
174

    
175
	xml_set_element_handler($xml_parser, "startElement", "endElement");
176
	xml_set_character_data_handler($xml_parser, "cdata");
177
	xml_parser_set_option($xml_parser, XML_OPTION_SKIP_WHITE, 1);
178

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

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

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

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

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

    
217
	global $listtags;
218

    
219
	$xmlconfig = "";
220

    
221
	foreach ($arr as $ent => $val) {
222
		if (is_array($val)) {
223
			/* is it just a list of multiple values? */
224
			if (isset($listtags[strtolower($ent)])) {
225
				foreach ($val as $cval) {
226
					if (is_array($cval)) {
227
						if (empty($cval)) {
228
							$xmlconfig .= str_repeat("\t", $indent);
229
							$xmlconfig .= "<$ent></$ent>\n";
230
						} else {
231
							$xmlconfig .= str_repeat("\t", $indent);
232
							$xmlconfig .= "<$ent>\n";
233
							$xmlconfig .= dump_xml_config_sub($cval, $indent + 1);
234
							$xmlconfig .= str_repeat("\t", $indent);
235
							$xmlconfig .= "</$ent>\n";
236
						}
237
					} else {
238
						if ($cval === false) {
239
							continue;
240
						}
241
						$xmlconfig .= str_repeat("\t", $indent);
242
						if ((is_bool($cval) && $cval == true) || ($cval === "")) {
243
							$xmlconfig .= "<$ent></$ent>\n";
244
						} else if ((substr($ent, 0, 5) == "descr") ||
245
						    (substr($ent, 0, 6) == "detail") ||
246
						    (substr($ent, 0, 12) == "login_banner") ||
247
						    (substr($ent, 0, 9) == "ldap_attr") ||
248
						    (substr($ent, 0, 9) == "ldap_bind") ||
249
						    (substr($ent, 0, 11) == "ldap_basedn") ||
250
						    (substr($ent, 0, 18) == "ldap_authcn") ||
251
						    (substr($ent, 0, 19) == "ldap_extended_query")) {
252
							$xmlconfig .= "<$ent><![CDATA[" . htmlentities($cval) . "]]></$ent>\n";
253
						} else {
254
							$xmlconfig .= "<$ent>" . htmlentities($cval) . "</$ent>\n";
255
						}
256
					}
257
				}
258
			} else if (empty($val)) {
259
				$xmlconfig .= str_repeat("\t", $indent);
260
				$xmlconfig .= "<$ent></$ent>\n";
261
			} else {
262
				/* it's an array */
263
				$xmlconfig .= str_repeat("\t", $indent);
264
				$xmlconfig .= "<$ent>\n";
265
				$xmlconfig .= dump_xml_config_sub($val, $indent + 1);
266
				$xmlconfig .= str_repeat("\t", $indent);
267
				$xmlconfig .= "</$ent>\n";
268
			}
269
		} else {
270
			if ((is_bool($val) && ($val == true)) || ($val === "")) {
271
				$xmlconfig .= str_repeat("\t", $indent);
272
				$xmlconfig .= "<$ent></$ent>\n";
273
			} else if (!is_bool($val)) {
274
				$xmlconfig .= str_repeat("\t", $indent);
275
				if ((substr($ent, 0, 5) == "descr") ||
276
				    (substr($ent, 0, 6) == "detail") ||
277
				    (substr($ent, 0, 12) == "login_banner") ||
278
				    (substr($ent, 0, 9) == "ldap_attr") ||
279
				    (substr($ent, 0, 9) == "ldap_bind") ||
280
				    (substr($ent, 0, 11) == "ldap_basedn") ||
281
				    (substr($ent, 0, 18) == "ldap_authcn") ||
282
				    (substr($ent, 0, 19) == "ldap_extended_query") ||
283
				    (substr($ent, 0, 5) == "text")) {
284
					$xmlconfig .= "<$ent><![CDATA[" . htmlentities($val) . "]]></$ent>\n";
285
				} else {
286
					$xmlconfig .= "<$ent>" . htmlentities($val) . "</$ent>\n";
287
				}
288
			}
289
		}
290
	}
291

    
292
	return $xmlconfig;
293
}
294

    
295
function dump_xml_config($arr, $rootobj) {
296
	global $listtags;
297
	$listtags = listtags();
298
	if (isset($GLOBALS['custom_listtags'])) {
299
		foreach ($GLOBALS['custom_listtags'] as $tag) {
300
			$listtags[$tag] = $tag;
301
		}
302
	}
303
	return dump_xml_config_raw($arr, $rootobj);
304
}
305

    
306
function dump_xml_config_pkg($arr, $rootobj) {
307
	global $listtags;
308
	$listtags = listtags_pkg();
309
	if (isset($GLOBALS['custom_listtags_pkg'])) {
310
		foreach ($GLOBALS['custom_listtags_pkg'] as $tag) {
311
			$listtags[$tag] = $tag;
312
		}
313
	}
314
	return dump_xml_config_raw($arr, $rootobj);
315
}
316

    
317
function dump_xml_config_raw($arr, $rootobj) {
318

    
319
	$xmlconfig = "<?xml version=\"1.0\"?" . ">\n";
320
	$xmlconfig .= "<$rootobj>\n";
321

    
322
	$xmlconfig .= dump_xml_config_sub($arr, 1);
323

    
324
	$xmlconfig .= "</$rootobj>\n";
325

    
326
	return $xmlconfig;
327
}
328

    
329
?>
(48-48/51)