Project

General

Profile

Download (7.95 KB) Statistics
| Branch: | Tag: | Revision:
1 26433cb8 Scott Ullrich
<?php
2
/* $Id$ */
3
/*
4 ce77a9c4 Phil Davis
	xmlreader.inc
5 26433cb8 Scott Ullrich
	functions to parse/dump configuration files in XML format
6
	part of m0n0wall (http://m0n0.ch/wall)
7
8
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
9
	All rights reserved.
10
11
	Redistribution and use in source and binary forms, with or without
12
	modification, are permitted provided that the following conditions are met:
13
14
	1. Redistributions of source code must retain the above copyright notice,
15
	   this list of conditions and the following disclaimer.
16
17
	2. Redistributions in binary form must reproduce the above copyright
18
	   notice, this list of conditions and the following disclaimer in the
19
	   documentation and/or other materials provided with the distribution.
20
21
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
22
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
23
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
25
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
	POSSIBILITY OF SUCH DAMAGE.
31
*/
32
33
/*
34
	pfSense_MODULE:	utils
35
*/
36
37
/* The following items will be treated as arrays in config.xml */
38
function listtags() {
39 72a8c829 Ermal
	/*
40 79262830 Phil Davis
	 * Please keep this list alpha sorted and no longer than 80 characters
41
	 * I know it's a pain, but it's a pain to find stuff too if it's not
42
	 */
43 b79ea46a Ermal
	$ret = array(
44 72a8c829 Ermal
		'acls', 'alias', 'aliasurl', 'allowedip', 'allowedhostname', 'authserver',
45
		'bridged', 'build_port_path',
46
		'ca', 'cacert', 'cert', 'crl', 'clone', 'config', 'container', 'columnitem',
47
		'depends_on_package', 'disk', 'dnsserver', 'dnsupdate', 'domainoverrides', 'dyndns',
48
		'earlyshellcmd', 'element', 'encryption-algorithm-option',
49
		'field', 'fieldname',
50
		'gateway_item', 'gateway_group', 'gif', 'gre', 'group',
51
		'hash-algorithm-option', 'hosts', 'member', 'ifgroupentry', 'igmpentry', 'interface_array', 'item', 'key',
52
		'lagg', 'lbaction', 'lbpool', 'l7rules', 'lbprotocol',
53
		'member', 'menu', 'tab', 'mobilekey', 'monitor_type', 'mount',
54
		'npt', 'ntpserver',
55
		'onetoone', 'openvpn-server', 'openvpn-client', 'openvpn-csc', 'option',
56
		'package', 'passthrumac', 'phase1', 'phase2', 'ppp', 'pppoe', 'priv', 'proxyarpnet', 'pool',
57
		'qinqentry', 'queue',
58
		'pages', 'pipe', 'radnsserver', 'roll', 'route', 'row', 'rrddatafile', 'rule',
59
		'schedule', 'service', 'servernat', 'servers',
60
		'serversdisabled', 'shellcmd', 'staticmap', 'subqueue',
61
		'timerange', 'tunnel', 'user', 'vip', 'virtual_server', 'vlan',
62
		'winsserver', 'wolentry', 'widget'
63
	);
64 26433cb8 Scott Ullrich
	return array_flip($ret);
65
}
66
67
/* Package XML tags that should be treat as a list not as a traditional array */
68
function listtags_pkg() {
69 b79ea46a Ermal
	$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');
70 26433cb8 Scott Ullrich
71
	return array_flip($ret);
72
}
73
74
function add_elements(&$cfgarray, &$parser) {
75 79262830 Phil Davis
	global $listtags;
76
77
	while ($parser->read()) {
78
		switch ($parser->nodeType) {
79
			case XMLReader::WHITESPACE:
80
			case XMLReader::SIGNIFICANT_WHITESPACE:
81
				break;
82
			case XMLReader::ELEMENT:
83
				if (isset($listtags[strtolower($parser->name)])) {
84
					$cfgref =& $cfgarray[$parser->name][count($cfgarray[$parser->name])];
85
					if (!$parser->isEmptyElement) {
86 ddfe5e43 Ermal
						add_elements($cfgref, $parser);
87 79262830 Phil Davis
					} else {
88
						$cfgref = array();
89
					}
90
				} else {
91
					if (isset($cfgarray[$parser->name]) && (!is_array($cfgarray[$parser->name]) || !isset($cfgarray[$parser->name][0]))) {
92
						$nodebkp = $cfgarray[$parser->name];
93
						$cfgarray[$parser->name] = array();
94
						$cfgarray[$parser->name][] = $nodebkp;
95
						$cfgref =& $cfgarray[$parser->name][0];
96
						unset($nodebkp);
97
					} else {
98
						$cfgref =& $cfgarray[$parser->name];
99
					}
100
101
					if ($parser->isEmptyElement) {
102
						if (is_array($cfgref)) {
103
							$cfgref[] = array();
104
						} else {
105
							$cfgref = "";
106
						}
107
					} else {
108
						if (is_array($cfgref)) {
109
							$cfgref =& $cfgarray[$parser->name][count($cfgarray[$parser->name])];
110
							add_elements($cfgref, $parser);
111
						} else {
112
							add_elements($cfgref, $parser);
113
						}
114
					}
115 ddfe5e43 Ermal
				}
116 8ffb5ccd Ermal
117 79262830 Phil Davis
				$i = 0;
118
				while ($parser->moveToAttributeNo($i)) {
119
					$cfgref[$parser->name] = $parser->value;
120
					$i++;
121
				}
122
				break;
123
			case XMLReader::TEXT:
124
			case XMLReader::CDATA:
125
				$cfgarray = $parser->value;
126
				break;
127
			case XMLReader::END_ELEMENT:
128
				return;
129
				break;
130
			default:
131
				break;
132
		}
133 ab83fce0 Ermal
	}
134 26433cb8 Scott Ullrich
}
135
136
function parse_xml_config($cffile, $rootobj, $isstring = "false") {
137
	global $listtags;
138 b79ea46a Ermal
139 26433cb8 Scott Ullrich
	$listtags = listtags();
140 79262830 Phil Davis
	if (isset($GLOBALS['custom_listtags'])) {
141
		foreach ($GLOBALS['custom_listtags'] as $tag) {
142
			$listtags[$tag] = $tag;
143
		}
144
	}
145 ab83fce0 Ermal
146 26433cb8 Scott Ullrich
	return parse_xml_config_raw($cffile, $rootobj);
147
}
148
149
function parse_xml_config_pkg($cffile, $rootobj, $isstring = "false") {
150
	global $listtags;
151 b79ea46a Ermal
152 26433cb8 Scott Ullrich
	$listtags = listtags_pkg();
153 79262830 Phil Davis
	if (isset($GLOBALS['custom_listtags_pkg'])) {
154
		foreach ($GLOBALS['custom_listtags_pkg'] as $tag) {
155
			$listtags[$tag] = $tag;
156
		}
157
	}
158 26433cb8 Scott Ullrich
	return parse_xml_config_raw($cffile, $rootobj, $isstring);
159
}
160
161
function parse_xml_config_raw($cffile, $rootobj, $isstring = "false") {
162 b79ea46a Ermal
	global $listtags;
163 26433cb8 Scott Ullrich
164
	$parsedcfg = array();
165
166
	$par = new XMLReader();
167 ab83fce0 Ermal
	if ($par->open($cffile, "UTF-8", LIBXML_NOERROR | LIBXML_NOWARNING)) {
168 26433cb8 Scott Ullrich
		add_elements($parsedcfg, $par);
169
		$par->close();
170 79262830 Phil Davis
	} else {
171 5cfa35df Renato Botelho
		log_error(sprintf(gettext("Error returned while trying to parse %s"), $cffile));
172 79262830 Phil Davis
	}
173 26433cb8 Scott Ullrich
174 428c289f Darren Embry
	if ($rootobj) {
175 79262830 Phil Davis
		if (!is_array($rootobj)) {
176 428c289f Darren Embry
			$rootobj = array($rootobj);
177 79262830 Phil Davis
		}
178
		foreach ($rootobj as $rootobj_name) {
179
			if ($parsedcfg[$rootobj_name]) {
180 428c289f Darren Embry
				break;
181 79262830 Phil Davis
			}
182
		}
183
184 428c289f Darren Embry
		return $parsedcfg[$rootobj_name];
185
	} else {
186
		return $parsedcfg;
187
	}
188 26433cb8 Scott Ullrich
}
189
190 ab83fce0 Ermal
function dump_xml_config_sub(& $writer, $arr) {
191 79262830 Phil Davis
	global $listtags;
192
193
	foreach ($arr as $ent => $val) {
194
		if (is_array($val)) {
195
			/* is it just a list of multiple values? */
196
			if (isset($listtags[strtolower($ent)])) {
197
				foreach ($val as $cval) {
198
					if (is_array($cval)) {
199
						if (empty($cval)) {
200
							$writer->writeElement($ent);
201
						} else {
202
							$writer->startElement($ent);
203
							dump_xml_config_sub($writer, $cval);
204
							$writer->endElement();
205
						}
206
					} else {
207
						if ($cval === false) {
208
							continue;
209
						}
210
						if ((is_bool($val) && ($val == true)) || ($val === "")) {
211
							$writer->writeElement($ent);
212
						} else if (!is_bool($val)) {
213 ab83fce0 Ermal
							$writer->writeElement($ent, $cval);
214 79262830 Phil Davis
						}
215
					}
216
				}
217
			} else if (empty($val)) {
218
				$writer->writeElement($ent);
219
			} else {
220
				/* it's an array */
221
				$writer->startElement($ent);
222
				dump_xml_config_sub($writer, $val);
223
				$writer->endElement();
224
			}
225
		} else {
226
			if ((is_bool($val) && ($val == true)) || ($val === "")) {
227
				$writer->writeElement($ent);
228
			} else if (!is_bool($val)) {
229
				$writer->writeElement($ent, $val);
230
			}
231
		}
232
	}
233 26433cb8 Scott Ullrich
}
234
235
function dump_xml_config($arr, $rootobj) {
236
	global $listtags;
237 b79ea46a Ermal
238 26433cb8 Scott Ullrich
	$listtags = listtags();
239 79262830 Phil Davis
	if (isset($GLOBALS['custom_listtags'])) {
240
		foreach ($GLOBALS['custom_listtags'] as $tag) {
241
			$listtags[$tag] = $tag;
242
		}
243
	}
244 26433cb8 Scott Ullrich
	return dump_xml_config_raw($arr, $rootobj);
245
}
246
247
function dump_xml_config_pkg($arr, $rootobj) {
248
	global $listtags;
249 b79ea46a Ermal
250 26433cb8 Scott Ullrich
	$listtags = listtags_pkg();
251 79262830 Phil Davis
	if (isset($GLOBALS['custom_listtags_pkg'])) {
252
		foreach ($GLOBALS['custom_listtags_pkg'] as $tag) {
253
			$listtags[$tag] = $tag;
254
		}
255
	}
256 26433cb8 Scott Ullrich
	return dump_xml_config_raw($arr, $rootobj);
257
}
258
259
function dump_xml_config_raw($arr, $rootobj) {
260
261 79262830 Phil Davis
	$writer = new XMLWriter();
262
	$writer->openMemory();
263
	$writer->setIndent(true);
264
	$writer->setIndentString("\t");
265
	$writer->startDocument("1.0", "UTF-8");
266
	$writer->startElement($rootobj);
267 26433cb8 Scott Ullrich
268 79262830 Phil Davis
	dump_xml_config_sub($writer, $arr);
269 26433cb8 Scott Ullrich
270 79262830 Phil Davis
	$writer->endElement();
271
	$writer->endDocument();
272
	$xmlconfig = $writer->outputMemory(true);
273 b79ea46a Ermal
274 79262830 Phil Davis
	return $xmlconfig;
275 26433cb8 Scott Ullrich
}
276
277
?>