Project

General

Profile

Download (7.95 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
	xmlreader.inc
5
	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
	/*
40
	 * 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
	$ret = array(
44
		'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
	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
	$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

    
71
	return array_flip($ret);
72
}
73

    
74
function add_elements(&$cfgarray, &$parser) {
75
	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
						add_elements($cfgref, $parser);
87
					} 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
				}
116

    
117
				$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
	}
134
}
135

    
136
function parse_xml_config($cffile, $rootobj, $isstring = "false") {
137
	global $listtags;
138

    
139
	$listtags = listtags();
140
	if (isset($GLOBALS['custom_listtags'])) {
141
		foreach ($GLOBALS['custom_listtags'] as $tag) {
142
			$listtags[$tag] = $tag;
143
		}
144
	}
145

    
146
	return parse_xml_config_raw($cffile, $rootobj);
147
}
148

    
149
function parse_xml_config_pkg($cffile, $rootobj, $isstring = "false") {
150
	global $listtags;
151

    
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
	return parse_xml_config_raw($cffile, $rootobj, $isstring);
159
}
160

    
161
function parse_xml_config_raw($cffile, $rootobj, $isstring = "false") {
162
	global $listtags;
163

    
164
	$parsedcfg = array();
165

    
166
	$par = new XMLReader();
167
	if ($par->open($cffile, "UTF-8", LIBXML_NOERROR | LIBXML_NOWARNING)) {
168
		add_elements($parsedcfg, $par);
169
		$par->close();
170
	} else {
171
		log_error(sprintf(gettext("Error returned while trying to parse %s"), $cffile));
172
	}
173

    
174
	if ($rootobj) {
175
		if (!is_array($rootobj)) {
176
			$rootobj = array($rootobj);
177
		}
178
		foreach ($rootobj as $rootobj_name) {
179
			if ($parsedcfg[$rootobj_name]) {
180
				break;
181
			}
182
		}
183

    
184
		return $parsedcfg[$rootobj_name];
185
	} else {
186
		return $parsedcfg;
187
	}
188
}
189

    
190
function dump_xml_config_sub(& $writer, $arr) {
191
	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
							$writer->writeElement($ent, $cval);
214
						}
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
}
234

    
235
function dump_xml_config($arr, $rootobj) {
236
	global $listtags;
237

    
238
	$listtags = listtags();
239
	if (isset($GLOBALS['custom_listtags'])) {
240
		foreach ($GLOBALS['custom_listtags'] as $tag) {
241
			$listtags[$tag] = $tag;
242
		}
243
	}
244
	return dump_xml_config_raw($arr, $rootobj);
245
}
246

    
247
function dump_xml_config_pkg($arr, $rootobj) {
248
	global $listtags;
249

    
250
	$listtags = listtags_pkg();
251
	if (isset($GLOBALS['custom_listtags_pkg'])) {
252
		foreach ($GLOBALS['custom_listtags_pkg'] as $tag) {
253
			$listtags[$tag] = $tag;
254
		}
255
	}
256
	return dump_xml_config_raw($arr, $rootobj);
257
}
258

    
259
function dump_xml_config_raw($arr, $rootobj) {
260

    
261
	$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

    
268
	dump_xml_config_sub($writer, $arr);
269

    
270
	$writer->endElement();
271
	$writer->endDocument();
272
	$xmlconfig = $writer->outputMemory(true);
273

    
274
	return $xmlconfig;
275
}
276

    
277
?>
(63-63/67)