Project

General

Profile

Download (6.67 KB) Statistics
| Branch: | Tag: | Revision:
1 5b237745 Scott Ullrich
<?php
2 307cd525 Bill Marquette
/* $Id$ */
3 5b237745 Scott Ullrich
/*
4
	xmlparse.inc
5
	functions to parse/dump configuration files in XML format
6
	part of m0n0wall (http://m0n0.ch/wall)
7 528ae96e Scott Ullrich
8 5b237745 Scott Ullrich
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
9
	All rights reserved.
10 528ae96e Scott Ullrich
11 5b237745 Scott Ullrich
	Redistribution and use in source and binary forms, with or without
12
	modification, are permitted provided that the following conditions are met:
13 528ae96e Scott Ullrich
14 5b237745 Scott Ullrich
	1. Redistributions of source code must retain the above copyright notice,
15
	   this list of conditions and the following disclaimer.
16 528ae96e Scott Ullrich
17 5b237745 Scott Ullrich
	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 528ae96e Scott Ullrich
21 5b237745 Scott Ullrich
	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 2e5ebf5d Scott Ullrich
/* The following items will be treated as arrays in config.xml */
34 eeb6c16e Bill Marquette
function listtags() {
35 f60e6259 Bill Marquette
	$ret = explode(" ", "alias allowedip cacert config columnitem dnsserver domainoverrides " .
36
		"earlyshellcmd encryption-algorithm-option field fieldname hash-algorithm-option " .
37 2e5ebf5d Scott Ullrich
		"hosts interface_array item key lbpool menu mobilekey onetoone option package passthrumac proxyarpnet " .
38 9f09f99a Bill Marquette
		"queue route row rule service servernat servers shellcmd staticmap subqueue " .
39 b6d1659d Bill Marquette
                "tunnel user vip virtual_server vlan winsserver wolentry");
40 eeb6c16e Bill Marquette
	return $ret;
41
}
42
43 2fe045e5 Bill Marquette
/* Package XML tags that should be treat as a list not as a traditional array */
44 eeb6c16e Bill Marquette
function listtags_pkg() {
45 48ca2dc9 Colin Smith
	$ret = array("onetoone", "queue", "rule", "servernat", "alias", "additional_files_needed", "tab", "template", "menu", "rowhelperfield", "service", "step", "package", "columnitem", "option", "item", "field", "package", "file");
46 eeb6c16e Bill Marquette
47
	return $ret;
48
}
49
50 5b237745 Scott Ullrich
function startElement($parser, $name, $attrs) {
51 08c3872d Colin Smith
	global $parsedcfg, $depth, $curpath, $havedata, $listtags;
52 528ae96e Scott Ullrich
53 5b237745 Scott Ullrich
	array_push($curpath, strtolower($name));
54 528ae96e Scott Ullrich
55 eba0b1d8 Colin Smith
	$ptr =& $parsedcfg;
56 5b237745 Scott Ullrich
	foreach ($curpath as $path) {
57
		$ptr =& $ptr[$path];
58
	}
59 528ae96e Scott Ullrich
60 5b237745 Scott Ullrich
	/* is it an element that belongs to a list? */
61
	if (in_array(strtolower($name), $listtags)) {
62 528ae96e Scott Ullrich
63 5b237745 Scott Ullrich
		/* is there an array already? */
64
		if (!is_array($ptr)) {
65
			/* make an array */
66
			$ptr = array();
67
		}
68 528ae96e Scott Ullrich
69 5b237745 Scott Ullrich
		array_push($curpath, count($ptr));
70 528ae96e Scott Ullrich
71 5b237745 Scott Ullrich
	} else if (isset($ptr)) {
72
		/* multiple entries not allowed for this element, bail out */
73
		die(sprintf("XML error: %s at line %d cannot occur more than once\n",
74
				$name,
75
				xml_get_current_line_number($parser)));
76
	}
77 528ae96e Scott Ullrich
78 5b237745 Scott Ullrich
	$depth++;
79
	$havedata = $depth;
80
}
81
82
function endElement($parser, $name) {
83 08c3872d Colin Smith
	global $depth, $curpath, $parsedcfg, $havedata, $listtags;
84 528ae96e Scott Ullrich
85 5b237745 Scott Ullrich
	if ($havedata == $depth) {
86 08c3872d Colin Smith
		$ptr =& $parsedcfg;
87 5b237745 Scott Ullrich
		foreach ($curpath as $path) {
88
			$ptr =& $ptr[$path];
89
		}
90
		$ptr = "";
91
	}
92 528ae96e Scott Ullrich
93 5b237745 Scott Ullrich
	array_pop($curpath);
94
95
	if (in_array(strtolower($name), $listtags))
96
		array_pop($curpath);
97 528ae96e Scott Ullrich
98 5b237745 Scott Ullrich
	$depth--;
99
}
100
101
function cData($parser, $data) {
102 08c3872d Colin Smith
	global $depth, $curpath, $parsedcfg, $havedata;
103 528ae96e Scott Ullrich
104 5b237745 Scott Ullrich
	$data = trim($data, "\t\n\r");
105 528ae96e Scott Ullrich
106 5b237745 Scott Ullrich
	if ($data != "") {
107 08c3872d Colin Smith
		$ptr =& $parsedcfg;
108 5b237745 Scott Ullrich
		foreach ($curpath as $path) {
109
			$ptr =& $ptr[$path];
110
		}
111
112
		if (is_string($ptr)) {
113
			$ptr .= $data;
114
		} else {
115
			if (trim($data, " ") != "") {
116
				$ptr = $data;
117
				$havedata++;
118
			}
119
		}
120
	}
121
}
122
123 48ca2dc9 Colin Smith
function parse_xml_config($cffile, $rootobj, $isstring = "false") {
124 cb980a2f Colin Smith
	global $listtags;
125 eeb6c16e Bill Marquette
	$listtags = listtags();
126 48ca2dc9 Colin Smith
	return parse_xml_config_raw($cffile, $rootobj, $isstring);
127 cb980a2f Colin Smith
}
128
129 48ca2dc9 Colin Smith
function parse_xml_config_pkg($cffile, $rootobj, $isstring = "false") {
130 cb980a2f Colin Smith
	global $listtags;
131 eeb6c16e Bill Marquette
	$listtags = listtags_pkg();
132 48ca2dc9 Colin Smith
	return parse_xml_config_raw($cffile, $rootobj, $isstring);
133 cb980a2f Colin Smith
}
134
135 48ca2dc9 Colin Smith
function parse_xml_config_raw($cffile, $rootobj, $isstring = "false") {
136 5b237745 Scott Ullrich
137 08c3872d Colin Smith
	global $depth, $curpath, $parsedcfg, $havedata, $listtags;
138
	$parsedcfg = array();
139 5b237745 Scott Ullrich
	$curpath = array();
140
	$depth = 0;
141
	$havedata = 0;
142 528ae96e Scott Ullrich
143 5b237745 Scott Ullrich
	$xml_parser = xml_parser_create();
144 528ae96e Scott Ullrich
145 5b237745 Scott Ullrich
	xml_set_element_handler($xml_parser, "startElement", "endElement");
146
	xml_set_character_data_handler($xml_parser, "cdata");
147 528ae96e Scott Ullrich
148 5b237745 Scott Ullrich
	if (!($fp = fopen($cffile, "r"))) {
149
		die("Error: could not open XML input\n");
150
	}
151 528ae96e Scott Ullrich
152 5b237745 Scott Ullrich
	while ($data = fread($fp, 4096)) {
153
		if (!xml_parse($xml_parser, $data, feof($fp))) {
154
			die(sprintf("XML error: %s at line %d\n",
155
						xml_error_string(xml_get_error_code($xml_parser)),
156
						xml_get_current_line_number($xml_parser)));
157
		}
158
	}
159
	xml_parser_free($xml_parser);
160 528ae96e Scott Ullrich
161 08c3872d Colin Smith
	if (!$parsedcfg[$rootobj]) {
162 5b237745 Scott Ullrich
		die("XML error: no $rootobj object found!\n");
163
	}
164 528ae96e Scott Ullrich
165 08c3872d Colin Smith
	return $parsedcfg[$rootobj];
166 5b237745 Scott Ullrich
}
167
168
function dump_xml_config_sub($arr, $indent) {
169
170
	global $listtags;
171 528ae96e Scott Ullrich
172 5b237745 Scott Ullrich
	$xmlconfig = "";
173
174
	foreach ($arr as $ent => $val) {
175
		if (is_array($val)) {
176
			/* is it just a list of multiple values? */
177
			if (in_array(strtolower($ent), $listtags)) {
178
				foreach ($val as $cval) {
179
					if (is_array($cval)) {
180
						$xmlconfig .= str_repeat("\t", $indent);
181
						$xmlconfig .= "<$ent>\n";
182
						$xmlconfig .= dump_xml_config_sub($cval, $indent + 1);
183
						$xmlconfig .= str_repeat("\t", $indent);
184
						$xmlconfig .= "</$ent>\n";
185
					} else {
186
						$xmlconfig .= str_repeat("\t", $indent);
187
						if ((is_bool($cval) && ($cval == true)) ||
188
							($cval === ""))
189
							$xmlconfig .= "<$ent/>\n";
190
						else if (!is_bool($cval))
191
							$xmlconfig .= "<$ent>" . htmlspecialchars($cval) . "</$ent>\n";
192
					}
193
				}
194
			} else {
195
				/* it's an array */
196
				$xmlconfig .= str_repeat("\t", $indent);
197
				$xmlconfig .= "<$ent>\n";
198
				$xmlconfig .= dump_xml_config_sub($val, $indent + 1);
199
				$xmlconfig .= str_repeat("\t", $indent);
200
				$xmlconfig .= "</$ent>\n";
201
			}
202
		} else {
203
			if ((is_bool($val) && ($val == true)) || ($val === "")) {
204
				$xmlconfig .= str_repeat("\t", $indent);
205
				$xmlconfig .= "<$ent/>\n";
206
			} else if (!is_bool($val)) {
207
				$xmlconfig .= str_repeat("\t", $indent);
208
				$xmlconfig .= "<$ent>" . htmlspecialchars($val) . "</$ent>\n";
209
			}
210
		}
211
	}
212 528ae96e Scott Ullrich
213 5b237745 Scott Ullrich
	return $xmlconfig;
214
}
215
216
function dump_xml_config($arr, $rootobj) {
217 ba6882bf Colin Smith
	global $listtags;
218 eeb6c16e Bill Marquette
	$listtags = listtags();
219 ba6882bf Colin Smith
	return dump_xml_config_raw($arr, $rootobj);
220
}
221
222
function dump_xml_config_pkg($arr, $rootobj) {
223
	global $listtags;
224 eeb6c16e Bill Marquette
	$listtags = listtags_pkg();
225 ba6882bf Colin Smith
	return dump_xml_config_raw($arr, $rootobj);
226
}
227
228
function dump_xml_config_raw($arr, $rootobj) {
229 5b237745 Scott Ullrich
230
	$xmlconfig = "<?xml version=\"1.0\"?" . ">\n";
231
	$xmlconfig .= "<$rootobj>\n";
232 528ae96e Scott Ullrich
233 5b237745 Scott Ullrich
	$xmlconfig .= dump_xml_config_sub($arr, 1);
234 528ae96e Scott Ullrich
235 5b237745 Scott Ullrich
	$xmlconfig .= "</$rootobj>\n";
236 528ae96e Scott Ullrich
237 5b237745 Scott Ullrich
	return $xmlconfig;
238
}
239
240 2e5ebf5d Scott Ullrich
?>