Project

General

Profile

Download (6.69 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
	xmlparse.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
/* The following items will be treated as arrays in config.xml */
34
function listtags() {
35
	$ret = explode(" ", "alias allowedip cacert config columnitem dnsserver domainoverrides " .
36
		"earlyshellcmd encryption-algorithm-option field fieldname hash-algorithm-option " .
37
		"hosts interface_array item key lbpool menu mobilekey onetoone option package passthrumac proxyarpnet " .
38
		"queue pipe route row rule service servernat servers earlyshellcmd shellcmd staticmap subqueue " .
39
                "tunnel user vip virtual_server vlan winsserver wolentry");
40
	return $ret;
41
}
42

    
43
/* Package XML tags that should be treat as a list not as a traditional array */
44
function listtags_pkg() {
45
	$ret = array("onetoone", "queue", "rule", "servernat", "alias", "additional_files_needed", "tab", "template", "menu", "rowhelperfield", "service", "step", "package", "columnitem", "option", "item", "field", "package", "file");
46

    
47
	return $ret;
48
}
49

    
50
function startElement($parser, $name, $attrs) {
51
	global $parsedcfg, $depth, $curpath, $havedata, $listtags;
52

    
53
	array_push($curpath, strtolower($name));
54

    
55
	$ptr =& $parsedcfg;
56
	foreach ($curpath as $path) {
57
		$ptr =& $ptr[$path];
58
	}
59

    
60
	/* is it an element that belongs to a list? */
61
	if (in_array(strtolower($name), $listtags)) {
62

    
63
		/* is there an array already? */
64
		if (!is_array($ptr)) {
65
			/* make an array */
66
			$ptr = array();
67
		}
68

    
69
		array_push($curpath, count($ptr));
70

    
71
	} 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

    
78
	$depth++;
79
	$havedata = $depth;
80
}
81

    
82
function endElement($parser, $name) {
83
	global $depth, $curpath, $parsedcfg, $havedata, $listtags;
84

    
85
	if ($havedata == $depth) {
86
		$ptr =& $parsedcfg;
87
		foreach ($curpath as $path) {
88
			$ptr =& $ptr[$path];
89
		}
90
		$ptr = "";
91
	}
92

    
93
	array_pop($curpath);
94

    
95
	if (in_array(strtolower($name), $listtags))
96
		array_pop($curpath);
97

    
98
	$depth--;
99
}
100

    
101
function cData($parser, $data) {
102
	global $depth, $curpath, $parsedcfg, $havedata;
103

    
104
	$data = trim($data, "\t\n\r");
105

    
106
	if ($data != "") {
107
		$ptr =& $parsedcfg;
108
		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
function parse_xml_config($cffile, $rootobj, $isstring = "false") {
124
	global $listtags;
125
	$listtags = listtags();
126
	return parse_xml_config_raw($cffile, $rootobj, $isstring);
127
}
128

    
129
function parse_xml_config_pkg($cffile, $rootobj, $isstring = "false") {
130
	global $listtags;
131
	$listtags = listtags_pkg();
132
	return parse_xml_config_raw($cffile, $rootobj, $isstring);
133
}
134

    
135
function parse_xml_config_raw($cffile, $rootobj, $isstring = "false") {
136

    
137
	global $depth, $curpath, $parsedcfg, $havedata, $listtags;
138
	$parsedcfg = array();
139
	$curpath = array();
140
	$depth = 0;
141
	$havedata = 0;
142

    
143
	$xml_parser = xml_parser_create();
144

    
145
	xml_set_element_handler($xml_parser, "startElement", "endElement");
146
	xml_set_character_data_handler($xml_parser, "cdata");
147

    
148
	if (!($fp = fopen($cffile, "r"))) {
149
		die("Error: could not open XML input\n");
150
	}
151

    
152
	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

    
161
	if (!$parsedcfg[$rootobj]) {
162
		die("XML error: no $rootobj object found!\n");
163
	}
164

    
165
	return $parsedcfg[$rootobj];
166
}
167

    
168
function dump_xml_config_sub($arr, $indent) {
169

    
170
	global $listtags;
171

    
172
	$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($cval === false) continue;
188
						if(($cval === true) || ($cval === "")) {
189
							$xmlconfig .= "<$ent/>\n";
190
						} else {
191
							$xmlconfig .= "<$ent>" . htmlspecialchars($cval) . "</$ent>\n";
192
					}
193
				}
194
				}
195
			} else {
196
				/* it's an array */
197
				$xmlconfig .= str_repeat("\t", $indent);
198
				$xmlconfig .= "<$ent>\n";
199
				$xmlconfig .= dump_xml_config_sub($val, $indent + 1);
200
				$xmlconfig .= str_repeat("\t", $indent);
201
				$xmlconfig .= "</$ent>\n";
202
			}
203
		} else {
204
			if ((is_bool($val) && ($val == true)) || ($val === "")) {
205
				$xmlconfig .= str_repeat("\t", $indent);
206
				$xmlconfig .= "<$ent/>\n";
207
			} else if (!is_bool($val)) {
208
				$xmlconfig .= str_repeat("\t", $indent);
209
				$xmlconfig .= "<$ent>" . htmlspecialchars($val) . "</$ent>\n";
210
			}
211
		}
212
	}
213

    
214
	return $xmlconfig;
215
}
216

    
217
function dump_xml_config($arr, $rootobj) {
218
	global $listtags;
219
	$listtags = listtags();
220
	return dump_xml_config_raw($arr, $rootobj);
221
}
222

    
223
function dump_xml_config_pkg($arr, $rootobj) {
224
	global $listtags;
225
	$listtags = listtags_pkg();
226
	return dump_xml_config_raw($arr, $rootobj);
227
}
228

    
229
function dump_xml_config_raw($arr, $rootobj) {
230

    
231
	$xmlconfig = "<?xml version=\"1.0\"?" . ">\n";
232
	$xmlconfig .= "<$rootobj>\n";
233

    
234
	$xmlconfig .= dump_xml_config_sub($arr, 1);
235

    
236
	$xmlconfig .= "</$rootobj>\n";
237

    
238
	return $xmlconfig;
239
}
240

    
241
?>
(22-22/25)