Project

General

Profile

Download (7.42 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 72a15785 Scott Ullrich
	$ret = explode(" ", "element alias aliasurl allowedip cacert config columnitem disk dnsserver domainoverrides " .
36 f60e6259 Bill Marquette
		"earlyshellcmd encryption-algorithm-option field fieldname hash-algorithm-option " .
37 c5a2bfdb Scott Ullrich
		"hosts group interface_array item key lbpool menu mobilekey mount onetoone option package passthrumac priv proxyarpnet " .
38 615b27bc Scott Dale
		"queue pages pipe route row rule schedule service servernat servers earlyshellcmd shellcmd staticmap subqueue " .
39 d244855a Scott Ullrich
		"timerange tunnel user vip virtual_server vlan winsserver wolentry depends_on_package");
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 e1b5fccb Scott Ullrich
	$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");
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 eb72ceda Scott Ullrich
        if (isset($GLOBALS['custom_listtags'])) {
127
          foreach($GLOBALS['custom_listtags'] as $tag) {
128
            $listtags[] = $tag;
129
          }
130
        }
131 48ca2dc9 Colin Smith
	return parse_xml_config_raw($cffile, $rootobj, $isstring);
132 cb980a2f Colin Smith
}
133
134 48ca2dc9 Colin Smith
function parse_xml_config_pkg($cffile, $rootobj, $isstring = "false") {
135 cb980a2f Colin Smith
	global $listtags;
136 eeb6c16e Bill Marquette
	$listtags = listtags_pkg();
137 eb72ceda Scott Ullrich
        if (isset($GLOBALS['custom_listtags_pkg'])) {
138
          foreach($GLOBALS['custom_listtags_pkg'] as $tag) {
139
            $listtags[] = $tag;
140
          }
141
        }
142 48ca2dc9 Colin Smith
	return parse_xml_config_raw($cffile, $rootobj, $isstring);
143 cb980a2f Colin Smith
}
144
145 48ca2dc9 Colin Smith
function parse_xml_config_raw($cffile, $rootobj, $isstring = "false") {
146 5b237745 Scott Ullrich
147 08c3872d Colin Smith
	global $depth, $curpath, $parsedcfg, $havedata, $listtags;
148
	$parsedcfg = array();
149 5b237745 Scott Ullrich
	$curpath = array();
150
	$depth = 0;
151
	$havedata = 0;
152 528ae96e Scott Ullrich
153 5b237745 Scott Ullrich
	$xml_parser = xml_parser_create();
154 528ae96e Scott Ullrich
155 5b237745 Scott Ullrich
	xml_set_element_handler($xml_parser, "startElement", "endElement");
156
	xml_set_character_data_handler($xml_parser, "cdata");
157 528ae96e Scott Ullrich
158 5b237745 Scott Ullrich
	if (!($fp = fopen($cffile, "r"))) {
159
		die("Error: could not open XML input\n");
160
	}
161 528ae96e Scott Ullrich
162 5b237745 Scott Ullrich
	while ($data = fread($fp, 4096)) {
163
		if (!xml_parse($xml_parser, $data, feof($fp))) {
164
			die(sprintf("XML error: %s at line %d\n",
165
						xml_error_string(xml_get_error_code($xml_parser)),
166
						xml_get_current_line_number($xml_parser)));
167
		}
168
	}
169
	xml_parser_free($xml_parser);
170 528ae96e Scott Ullrich
171 08c3872d Colin Smith
	if (!$parsedcfg[$rootobj]) {
172 5b237745 Scott Ullrich
		die("XML error: no $rootobj object found!\n");
173
	}
174 528ae96e Scott Ullrich
175 08c3872d Colin Smith
	return $parsedcfg[$rootobj];
176 5b237745 Scott Ullrich
}
177
178
function dump_xml_config_sub($arr, $indent) {
179
180
	global $listtags;
181 528ae96e Scott Ullrich
182 5b237745 Scott Ullrich
	$xmlconfig = "";
183
184
	foreach ($arr as $ent => $val) {
185
		if (is_array($val)) {
186
			/* is it just a list of multiple values? */
187
			if (in_array(strtolower($ent), $listtags)) {
188
				foreach ($val as $cval) {
189
					if (is_array($cval)) {
190
						$xmlconfig .= str_repeat("\t", $indent);
191
						$xmlconfig .= "<$ent>\n";
192
						$xmlconfig .= dump_xml_config_sub($cval, $indent + 1);
193
						$xmlconfig .= str_repeat("\t", $indent);
194
						$xmlconfig .= "</$ent>\n";
195
					} else {
196
						$xmlconfig .= str_repeat("\t", $indent);
197 0d57f5f3 Scott Ullrich
						if($cval === false) continue;
198
						if(($cval === true) || ($cval === "")) {
199 5b237745 Scott Ullrich
							$xmlconfig .= "<$ent/>\n";
200 0d57f5f3 Scott Ullrich
						} else {
201 5b237745 Scott Ullrich
							$xmlconfig .= "<$ent>" . htmlspecialchars($cval) . "</$ent>\n";
202
					}
203
				}
204 0d57f5f3 Scott Ullrich
				}
205 5b237745 Scott Ullrich
			} else {
206
				/* it's an array */
207
				$xmlconfig .= str_repeat("\t", $indent);
208
				$xmlconfig .= "<$ent>\n";
209
				$xmlconfig .= dump_xml_config_sub($val, $indent + 1);
210
				$xmlconfig .= str_repeat("\t", $indent);
211
				$xmlconfig .= "</$ent>\n";
212
			}
213
		} else {
214
			if ((is_bool($val) && ($val == true)) || ($val === "")) {
215
				$xmlconfig .= str_repeat("\t", $indent);
216
				$xmlconfig .= "<$ent/>\n";
217
			} else if (!is_bool($val)) {
218
				$xmlconfig .= str_repeat("\t", $indent);
219
				$xmlconfig .= "<$ent>" . htmlspecialchars($val) . "</$ent>\n";
220
			}
221
		}
222
	}
223 528ae96e Scott Ullrich
224 5b237745 Scott Ullrich
	return $xmlconfig;
225
}
226
227
function dump_xml_config($arr, $rootobj) {
228 ba6882bf Colin Smith
	global $listtags;
229 eeb6c16e Bill Marquette
	$listtags = listtags();
230 4928848f Scott Ullrich
        if (isset($GLOBALS['custom_listtags'])) {
231
          foreach($GLOBALS['custom_listtags'] as $tag) {
232
            $listtags[] = $tag;
233
          }
234
        }
235 ba6882bf Colin Smith
	return dump_xml_config_raw($arr, $rootobj);
236
}
237
238
function dump_xml_config_pkg($arr, $rootobj) {
239
	global $listtags;
240 eeb6c16e Bill Marquette
	$listtags = listtags_pkg();
241 4928848f Scott Ullrich
        if (isset($GLOBALS['custom_listtags_pkg'])) {
242
          foreach($GLOBALS['custom_listtags_pkg'] as $tag) {
243
            $listtags[] = $tag;
244
          }
245
        }
246 ba6882bf Colin Smith
	return dump_xml_config_raw($arr, $rootobj);
247
}
248
249
function dump_xml_config_raw($arr, $rootobj) {
250 5b237745 Scott Ullrich
251
	$xmlconfig = "<?xml version=\"1.0\"?" . ">\n";
252
	$xmlconfig .= "<$rootobj>\n";
253 528ae96e Scott Ullrich
254 5b237745 Scott Ullrich
	$xmlconfig .= dump_xml_config_sub($arr, 1);
255 528ae96e Scott Ullrich
256 5b237745 Scott Ullrich
	$xmlconfig .= "</$rootobj>\n";
257 528ae96e Scott Ullrich
258 5b237745 Scott Ullrich
	return $xmlconfig;
259
}
260
261 f479b407 Scott Ullrich
?>