Project

General

Profile

Download (9.03 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * xmlreader.inc
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2016 Electric Sheep Fencing, LLC
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
 * Redistribution and use in source and binary forms, with or without
14
 * modification, are permitted provided that the following conditions are met:
15
 *
16
 * 1. Redistributions of source code must retain the above copyright notice,
17
 *    this list of conditions and the following disclaimer.
18
 *
19
 * 2. Redistributions in binary form must reproduce the above copyright
20
 *    notice, this list of conditions and the following disclaimer in
21
 *    the documentation and/or other materials provided with the
22
 *    distribution.
23
 *
24
 * 3. All advertising materials mentioning features or use of this software
25
 *    must display the following acknowledgment:
26
 *    "This product includes software developed by the pfSense Project
27
 *    for use in the pfSense® software distribution. (http://www.pfsense.org/).
28
 *
29
 * 4. The names "pfSense" and "pfSense Project" must not be used to
30
 *    endorse or promote products derived from this software without
31
 *    prior written permission. For written permission, please contact
32
 *    coreteam@pfsense.org.
33
 *
34
 * 5. Products derived from this software may not be called "pfSense"
35
 *    nor may "pfSense" appear in their names without prior written
36
 *    permission of the Electric Sheep Fencing, LLC.
37
 *
38
 * 6. Redistributions of any form whatsoever must retain the following
39
 *    acknowledgment:
40
 *
41
 * "This product includes software developed by the pfSense Project
42
 * for use in the pfSense software distribution (http://www.pfsense.org/).
43
 *
44
 * THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
45
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
47
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
48
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
49
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
50
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
51
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
53
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
54
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
55
 * OF THE POSSIBILITY OF SUCH DAMAGE.
56
 */
57

    
58
/* The following items will be treated as arrays in config.xml */
59
function listtags() {
60
	/*
61
	 * Please keep this list alpha sorted and no longer than 80 characters
62
	 * I know it's a pain, but it's a pain to find stuff too if it's not
63
	 */
64
	$ret = array(
65
		'acls', 'alias', 'aliasurl', 'allowedip', 'allowedhostname', 'authserver',
66
		'bridged', 'build_port_path',
67
		'ca', 'cacert', 'cert', 'crl', 'clone', 'config', 'container', 'columnitem',
68
		'depends_on_package', 'disk', 'dnsserver', 'dnsupdate', 'domainoverrides', 'dyndns',
69
		'earlyshellcmd', 'element', 'encryption-algorithm-option',
70
		'field', 'fieldname',
71
		'gateway_item', 'gateway_group', 'gif', 'gre', 'group',
72
		'hash-algorithm-option', 'hosts', 'ifgroupentry', 'igmpentry', 'interface_array', 'item', 'key',
73
		'lagg', 'lbaction', 'lbpool', 'l7rules', 'lbprotocol',
74
		'member', 'menu', 'tab', 'mobilekey', 'monitor_type', 'mount',
75
		'npt', 'ntpserver',
76
		'onetoone', 'openvpn-server', 'openvpn-client', 'openvpn-csc', 'option',
77
		'package', 'passthrumac', 'phase1', 'phase2', 'ppp', 'pppoe', 'priv', 'proxyarpnet', 'pool',
78
		'qinqentry', 'queue',
79
		'pages', 'pipe', 'radnsserver', 'roll', 'route', 'row', 'rrddatafile', 'rule',
80
		'schedule', 'service', 'servernat', 'servers',
81
		'serversdisabled', 'shellcmd', 'staticmap', 'subqueue',
82
		'timerange', 'tunnel', 'user', 'vip', 'virtual_server', 'vlan',
83
		'winsserver', 'wolentry', 'widget'
84
	);
85
	return array_flip($ret);
86
}
87

    
88
/* Package XML tags that should be treat as a list not as a traditional array */
89
function listtags_pkg() {
90
	$ret = array('depends_on_package', 'onetoone', 'queue', 'rule', 'servernat', 'alias', 'additional_files_needed', 'tab', 'template', 'menu', 'rowhelperfield', 'service', 'step', 'package', 'columnitem', 'option', 'item', 'field', 'package', 'file');
91

    
92
	return array_flip($ret);
93
}
94

    
95
function add_elements(&$cfgarray, &$parser) {
96
	global $listtags;
97

    
98
	while ($parser->read()) {
99
		switch ($parser->nodeType) {
100
			case XMLReader::WHITESPACE:
101
			case XMLReader::SIGNIFICANT_WHITESPACE:
102
				break;
103
			case XMLReader::ELEMENT:
104
				if (isset($listtags[strtolower($parser->name)])) {
105
					$cfgref =& $cfgarray[$parser->name][count($cfgarray[$parser->name])];
106
					if (!$parser->isEmptyElement) {
107
						add_elements($cfgref, $parser);
108
					} else {
109
						$cfgref = array();
110
					}
111
				} else {
112
					if (isset($cfgarray[$parser->name]) && (!is_array($cfgarray[$parser->name]) || !isset($cfgarray[$parser->name][0]))) {
113
						$nodebkp = $cfgarray[$parser->name];
114
						$cfgarray[$parser->name] = array();
115
						$cfgarray[$parser->name][] = $nodebkp;
116
						$cfgref =& $cfgarray[$parser->name][0];
117
						unset($nodebkp);
118
					} else {
119
						$cfgref =& $cfgarray[$parser->name];
120
					}
121

    
122
					if ($parser->isEmptyElement) {
123
						if (is_array($cfgref)) {
124
							$cfgref[] = array();
125
						} else {
126
							$cfgref = "";
127
						}
128
					} else {
129
						if (is_array($cfgref)) {
130
							$cfgref =& $cfgarray[$parser->name][count($cfgarray[$parser->name])];
131
							add_elements($cfgref, $parser);
132
						} else {
133
							add_elements($cfgref, $parser);
134
						}
135
					}
136
				}
137

    
138
				$i = 0;
139
				while ($parser->moveToAttributeNo($i)) {
140
					$cfgref[$parser->name] = $parser->value;
141
					$i++;
142
				}
143
				break;
144
			case XMLReader::TEXT:
145
			case XMLReader::CDATA:
146
				$cfgarray = $parser->value;
147
				break;
148
			case XMLReader::END_ELEMENT:
149
				return;
150
				break;
151
			default:
152
				break;
153
		}
154
	}
155
}
156

    
157
function parse_xml_config($cffile, $rootobj, $isstring = "false") {
158
	global $listtags;
159

    
160
	$listtags = listtags();
161
	if (isset($GLOBALS['custom_listtags'])) {
162
		foreach ($GLOBALS['custom_listtags'] as $tag) {
163
			$listtags[$tag] = $tag;
164
		}
165
	}
166

    
167
	return parse_xml_config_raw($cffile, $rootobj);
168
}
169

    
170
function parse_xml_config_pkg($cffile, $rootobj, $isstring = "false") {
171
	global $listtags;
172

    
173
	$listtags = listtags_pkg();
174
	if (isset($GLOBALS['custom_listtags_pkg'])) {
175
		foreach ($GLOBALS['custom_listtags_pkg'] as $tag) {
176
			$listtags[$tag] = $tag;
177
		}
178
	}
179
	return parse_xml_config_raw($cffile, $rootobj, $isstring);
180
}
181

    
182
function parse_xml_config_raw($cffile, $rootobj, $isstring = "false") {
183
	global $listtags;
184

    
185
	$parsedcfg = array();
186

    
187
	$par = new XMLReader();
188
	if ($par->open($cffile, "UTF-8", LIBXML_NOERROR | LIBXML_NOWARNING)) {
189
		add_elements($parsedcfg, $par);
190
		$par->close();
191
	} else {
192
		log_error(sprintf(gettext("Error returned while trying to parse %s"), $cffile));
193
	}
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
		return $parsedcfg[$rootobj_name];
206
	} else {
207
		return $parsedcfg;
208
	}
209
}
210

    
211
function dump_xml_config_sub(& $writer, $arr) {
212
	global $listtags;
213

    
214
	foreach ($arr as $ent => $val) {
215
		if (is_array($val)) {
216
			/* is it just a list of multiple values? */
217
			if (isset($listtags[strtolower($ent)])) {
218
				foreach ($val as $cval) {
219
					if (is_array($cval)) {
220
						if (empty($cval)) {
221
							$writer->writeElement($ent);
222
						} else {
223
							$writer->startElement($ent);
224
							dump_xml_config_sub($writer, $cval);
225
							$writer->endElement();
226
						}
227
					} else {
228
						if ($cval === false) {
229
							continue;
230
						}
231
						if ((is_bool($val) && ($val == true)) || ($val === "")) {
232
							$writer->writeElement($ent);
233
						} else if (!is_bool($val)) {
234
							$writer->writeElement($ent, $cval);
235
						}
236
					}
237
				}
238
			} else if (empty($val)) {
239
				$writer->writeElement($ent);
240
			} else {
241
				/* it's an array */
242
				$writer->startElement($ent);
243
				dump_xml_config_sub($writer, $val);
244
				$writer->endElement();
245
			}
246
		} else {
247
			if ((is_bool($val) && ($val == true)) || ($val === "")) {
248
				$writer->writeElement($ent);
249
			} else if (!is_bool($val)) {
250
				$writer->writeElement($ent, $val);
251
			}
252
		}
253
	}
254
}
255

    
256
function dump_xml_config($arr, $rootobj) {
257
	global $listtags;
258

    
259
	$listtags = listtags();
260
	if (isset($GLOBALS['custom_listtags'])) {
261
		foreach ($GLOBALS['custom_listtags'] as $tag) {
262
			$listtags[$tag] = $tag;
263
		}
264
	}
265
	return dump_xml_config_raw($arr, $rootobj);
266
}
267

    
268
function dump_xml_config_pkg($arr, $rootobj) {
269
	global $listtags;
270

    
271
	$listtags = listtags_pkg();
272
	if (isset($GLOBALS['custom_listtags_pkg'])) {
273
		foreach ($GLOBALS['custom_listtags_pkg'] as $tag) {
274
			$listtags[$tag] = $tag;
275
		}
276
	}
277
	return dump_xml_config_raw($arr, $rootobj);
278
}
279

    
280
function dump_xml_config_raw($arr, $rootobj) {
281

    
282
	$writer = new XMLWriter();
283
	$writer->openMemory();
284
	$writer->setIndent(true);
285
	$writer->setIndentString("\t");
286
	$writer->startDocument("1.0", "UTF-8");
287
	$writer->startElement($rootobj);
288

    
289
	dump_xml_config_sub($writer, $arr);
290

    
291
	$writer->endElement();
292
	$writer->endDocument();
293
	$xmlconfig = $writer->outputMemory(true);
294

    
295
	return $xmlconfig;
296
}
297

    
298
?>
(62-62/65)