Project

General

Profile

Download (5.48 KB) Statistics
| Branch: | Tag: | Revision:
1 5b237745 Scott Ullrich
<?php
2
/*
3
	xmlparse.inc
4
	functions to parse/dump configuration files in XML format
5
	part of m0n0wall (http://m0n0.ch/wall)
6 528ae96e Scott Ullrich
7 5b237745 Scott Ullrich
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
8
	All rights reserved.
9 528ae96e Scott Ullrich
10 5b237745 Scott Ullrich
	Redistribution and use in source and binary forms, with or without
11
	modification, are permitted provided that the following conditions are met:
12 528ae96e Scott Ullrich
13 5b237745 Scott Ullrich
	1. Redistributions of source code must retain the above copyright notice,
14
	   this list of conditions and the following disclaimer.
15 528ae96e Scott Ullrich
16 5b237745 Scott Ullrich
	2. Redistributions in binary form must reproduce the above copyright
17
	   notice, this list of conditions and the following disclaimer in the
18
	   documentation and/or other materials provided with the distribution.
19 528ae96e Scott Ullrich
20 5b237745 Scott Ullrich
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
21
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
24
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
	POSSIBILITY OF SUCH DAMAGE.
30
*/
31
32
/* tags that are always to be handled as lists */
33 5412cd45 Scott Ullrich
$listtags = explode(" ", "row config package columnitem option item fieldname field rule user key subqueue " .
34 a7f908db Scott Ullrich
	"dnsserver winsserver encryption-algorithm-option hash-algorithm-option hosts tunnel " .
35
	"onetoone staticmap route alias pipe queue shellcmd earlyshellcmd mobilekey " .
36 5b237745 Scott Ullrich
	"servernat proxyarpnet passthrumac allowedip wolentry vlan");
37
38
function startElement($parser, $name, $attrs) {
39
	global $depth, $curpath, $config, $havedata, $listtags;
40 528ae96e Scott Ullrich
41 5b237745 Scott Ullrich
	array_push($curpath, strtolower($name));
42 528ae96e Scott Ullrich
43 5b237745 Scott Ullrich
	$ptr =& $config;
44
	foreach ($curpath as $path) {
45
		$ptr =& $ptr[$path];
46
	}
47 528ae96e Scott Ullrich
48 5b237745 Scott Ullrich
	/* is it an element that belongs to a list? */
49
	if (in_array(strtolower($name), $listtags)) {
50 528ae96e Scott Ullrich
51 5b237745 Scott Ullrich
		/* is there an array already? */
52
		if (!is_array($ptr)) {
53
			/* make an array */
54
			$ptr = array();
55
		}
56 528ae96e Scott Ullrich
57 5b237745 Scott Ullrich
		array_push($curpath, count($ptr));
58 528ae96e Scott Ullrich
59 5b237745 Scott Ullrich
	} else if (isset($ptr)) {
60
		/* multiple entries not allowed for this element, bail out */
61
		die(sprintf("XML error: %s at line %d cannot occur more than once\n",
62
				$name,
63
				xml_get_current_line_number($parser)));
64
	}
65 528ae96e Scott Ullrich
66 5b237745 Scott Ullrich
	$depth++;
67
	$havedata = $depth;
68
}
69
70
function endElement($parser, $name) {
71
	global $depth, $curpath, $config, $havedata, $listtags;
72 528ae96e Scott Ullrich
73 5b237745 Scott Ullrich
	if ($havedata == $depth) {
74
		$ptr =& $config;
75
		foreach ($curpath as $path) {
76
			$ptr =& $ptr[$path];
77
		}
78
		$ptr = "";
79
	}
80 528ae96e Scott Ullrich
81 5b237745 Scott Ullrich
	array_pop($curpath);
82
83
	if (in_array(strtolower($name), $listtags))
84
		array_pop($curpath);
85 528ae96e Scott Ullrich
86 5b237745 Scott Ullrich
	$depth--;
87
}
88
89
function cData($parser, $data) {
90
	global $depth, $curpath, $config, $havedata;
91 528ae96e Scott Ullrich
92 5b237745 Scott Ullrich
	$data = trim($data, "\t\n\r");
93 528ae96e Scott Ullrich
94 5b237745 Scott Ullrich
	if ($data != "") {
95
		$ptr =& $config;
96
		foreach ($curpath as $path) {
97
			$ptr =& $ptr[$path];
98
		}
99
100
		if (is_string($ptr)) {
101
			$ptr .= $data;
102
		} else {
103
			if (trim($data, " ") != "") {
104
				$ptr = $data;
105
				$havedata++;
106
			}
107
		}
108
	}
109
}
110
111
function parse_xml_config($cffile, $rootobj) {
112
113
	global $depth, $curpath, $config, $havedata, $listtags;
114
115
	$config = array();
116
	$curpath = array();
117
	$depth = 0;
118
	$havedata = 0;
119 528ae96e Scott Ullrich
120 5b237745 Scott Ullrich
	$xml_parser = xml_parser_create();
121 528ae96e Scott Ullrich
122 5b237745 Scott Ullrich
	xml_set_element_handler($xml_parser, "startElement", "endElement");
123
	xml_set_character_data_handler($xml_parser, "cdata");
124 528ae96e Scott Ullrich
125 5b237745 Scott Ullrich
	if (!($fp = fopen($cffile, "r"))) {
126
		die("Error: could not open XML input\n");
127
	}
128 528ae96e Scott Ullrich
129 5b237745 Scott Ullrich
	while ($data = fread($fp, 4096)) {
130
		if (!xml_parse($xml_parser, $data, feof($fp))) {
131
			die(sprintf("XML error: %s at line %d\n",
132
						xml_error_string(xml_get_error_code($xml_parser)),
133
						xml_get_current_line_number($xml_parser)));
134
		}
135
	}
136
	xml_parser_free($xml_parser);
137 528ae96e Scott Ullrich
138 5b237745 Scott Ullrich
	if (!$config[$rootobj]) {
139
		die("XML error: no $rootobj object found!\n");
140
	}
141 528ae96e Scott Ullrich
142 5b237745 Scott Ullrich
	return $config[$rootobj];
143
}
144
145
function dump_xml_config_sub($arr, $indent) {
146
147
	global $listtags;
148 528ae96e Scott Ullrich
149 5b237745 Scott Ullrich
	$xmlconfig = "";
150
151
	foreach ($arr as $ent => $val) {
152
		if (is_array($val)) {
153
			/* is it just a list of multiple values? */
154
			if (in_array(strtolower($ent), $listtags)) {
155
				foreach ($val as $cval) {
156
					if (is_array($cval)) {
157
						$xmlconfig .= str_repeat("\t", $indent);
158
						$xmlconfig .= "<$ent>\n";
159
						$xmlconfig .= dump_xml_config_sub($cval, $indent + 1);
160
						$xmlconfig .= str_repeat("\t", $indent);
161
						$xmlconfig .= "</$ent>\n";
162
					} else {
163
						$xmlconfig .= str_repeat("\t", $indent);
164
						if ((is_bool($cval) && ($cval == true)) ||
165
							($cval === ""))
166
							$xmlconfig .= "<$ent/>\n";
167
						else if (!is_bool($cval))
168
							$xmlconfig .= "<$ent>" . htmlspecialchars($cval) . "</$ent>\n";
169
					}
170
				}
171
			} else {
172
				/* it's an array */
173
				$xmlconfig .= str_repeat("\t", $indent);
174
				$xmlconfig .= "<$ent>\n";
175
				$xmlconfig .= dump_xml_config_sub($val, $indent + 1);
176
				$xmlconfig .= str_repeat("\t", $indent);
177
				$xmlconfig .= "</$ent>\n";
178
			}
179
		} else {
180
			if ((is_bool($val) && ($val == true)) || ($val === "")) {
181
				$xmlconfig .= str_repeat("\t", $indent);
182
				$xmlconfig .= "<$ent/>\n";
183
			} else if (!is_bool($val)) {
184
				$xmlconfig .= str_repeat("\t", $indent);
185
				$xmlconfig .= "<$ent>" . htmlspecialchars($val) . "</$ent>\n";
186
			}
187
		}
188
	}
189 528ae96e Scott Ullrich
190 5b237745 Scott Ullrich
	return $xmlconfig;
191
}
192
193
function dump_xml_config($arr, $rootobj) {
194
195
	$xmlconfig = "<?xml version=\"1.0\"?" . ">\n";
196
	$xmlconfig .= "<$rootobj>\n";
197 528ae96e Scott Ullrich
198 5b237745 Scott Ullrich
	$xmlconfig .= dump_xml_config_sub($arr, 1);
199 528ae96e Scott Ullrich
200 5b237745 Scott Ullrich
	$xmlconfig .= "</$rootobj>\n";
201 528ae96e Scott Ullrich
202 5b237745 Scott Ullrich
	return $xmlconfig;
203
}
204
205
?>