Project

General

Profile

Download (7.84 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 0919224f Bill Marquette
  /* Please keep this list alpha sorted and no longer than 80 characters
36
   * I know it's a pain, but it's a pain to find stuff too if it's not
37
   */
38 64cc39d3 Matthew Grooms
	$ret = explode(" ",
39 0919224f Bill Marquette
		"alias aliasurl allowedip authserver bridged ca cacert cert config ".
40
		"columnitem depends_on_package disk dnsserver dnsupdate domainoverrides ".
41
		"dyndns earlyshellcmd element encryption-algorithm-option field ".
42
		"fieldname hash-algorithm-option gateway_item gateway_group gif gre ".
43
		"group hosts member interface_array item key lagg lbaction lbpool ".
44
		"lbprotocol member menu mobilekey monitor_type mount ntpserver onetoone ".
45 b6617403 Matthew Grooms
		"openvpn-server openvpn-client openvpn-csc " .
46 0919224f Bill Marquette
		"option ppp package passthrumac phase1 phase2 priv proxyarpnet queue ".
47
		"pages pipe route row rule schedule service servernat servers ".
48
		"serversdisabled earlyshellcmd shellcmd staticmap subqueue timerange ".
49
		"tunnel user vip virtual_server vlan winsserver wolentry widget  "
50
		);
51 eeb6c16e Bill Marquette
	return $ret;
52
}
53
54 2fe045e5 Bill Marquette
/* Package XML tags that should be treat as a list not as a traditional array */
55 eeb6c16e Bill Marquette
function listtags_pkg() {
56 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");
57 eeb6c16e Bill Marquette
58
	return $ret;
59
}
60
61 5b237745 Scott Ullrich
function startElement($parser, $name, $attrs) {
62 08c3872d Colin Smith
	global $parsedcfg, $depth, $curpath, $havedata, $listtags;
63 528ae96e Scott Ullrich
64 5b237745 Scott Ullrich
	array_push($curpath, strtolower($name));
65 528ae96e Scott Ullrich
66 eba0b1d8 Colin Smith
	$ptr =& $parsedcfg;
67 5b237745 Scott Ullrich
	foreach ($curpath as $path) {
68
		$ptr =& $ptr[$path];
69
	}
70 528ae96e Scott Ullrich
71 5b237745 Scott Ullrich
	/* is it an element that belongs to a list? */
72
	if (in_array(strtolower($name), $listtags)) {
73 528ae96e Scott Ullrich
74 5b237745 Scott Ullrich
		/* is there an array already? */
75
		if (!is_array($ptr)) {
76
			/* make an array */
77
			$ptr = array();
78
		}
79 528ae96e Scott Ullrich
80 5b237745 Scott Ullrich
		array_push($curpath, count($ptr));
81 528ae96e Scott Ullrich
82 5b237745 Scott Ullrich
	} else if (isset($ptr)) {
83
		/* multiple entries not allowed for this element, bail out */
84
		die(sprintf("XML error: %s at line %d cannot occur more than once\n",
85
				$name,
86
				xml_get_current_line_number($parser)));
87
	}
88 528ae96e Scott Ullrich
89 5b237745 Scott Ullrich
	$depth++;
90
	$havedata = $depth;
91
}
92
93
function endElement($parser, $name) {
94 08c3872d Colin Smith
	global $depth, $curpath, $parsedcfg, $havedata, $listtags;
95 528ae96e Scott Ullrich
96 5b237745 Scott Ullrich
	if ($havedata == $depth) {
97 08c3872d Colin Smith
		$ptr =& $parsedcfg;
98 5b237745 Scott Ullrich
		foreach ($curpath as $path) {
99
			$ptr =& $ptr[$path];
100
		}
101
		$ptr = "";
102
	}
103 528ae96e Scott Ullrich
104 5b237745 Scott Ullrich
	array_pop($curpath);
105
106
	if (in_array(strtolower($name), $listtags))
107
		array_pop($curpath);
108 528ae96e Scott Ullrich
109 5b237745 Scott Ullrich
	$depth--;
110
}
111
112
function cData($parser, $data) {
113 08c3872d Colin Smith
	global $depth, $curpath, $parsedcfg, $havedata;
114 528ae96e Scott Ullrich
115 5b237745 Scott Ullrich
	$data = trim($data, "\t\n\r");
116 528ae96e Scott Ullrich
117 5b237745 Scott Ullrich
	if ($data != "") {
118 08c3872d Colin Smith
		$ptr =& $parsedcfg;
119 5b237745 Scott Ullrich
		foreach ($curpath as $path) {
120
			$ptr =& $ptr[$path];
121
		}
122
123
		if (is_string($ptr)) {
124
			$ptr .= $data;
125
		} else {
126
			if (trim($data, " ") != "") {
127
				$ptr = $data;
128
				$havedata++;
129
			}
130
		}
131
	}
132
}
133
134 48ca2dc9 Colin Smith
function parse_xml_config($cffile, $rootobj, $isstring = "false") {
135 cb980a2f Colin Smith
	global $listtags;
136 eeb6c16e Bill Marquette
	$listtags = listtags();
137 eb72ceda Scott Ullrich
        if (isset($GLOBALS['custom_listtags'])) {
138
          foreach($GLOBALS['custom_listtags'] 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_pkg($cffile, $rootobj, $isstring = "false") {
146 cb980a2f Colin Smith
	global $listtags;
147 eeb6c16e Bill Marquette
	$listtags = listtags_pkg();
148 eb72ceda Scott Ullrich
        if (isset($GLOBALS['custom_listtags_pkg'])) {
149
          foreach($GLOBALS['custom_listtags_pkg'] as $tag) {
150
            $listtags[] = $tag;
151
          }
152
        }
153 48ca2dc9 Colin Smith
	return parse_xml_config_raw($cffile, $rootobj, $isstring);
154 cb980a2f Colin Smith
}
155
156 48ca2dc9 Colin Smith
function parse_xml_config_raw($cffile, $rootobj, $isstring = "false") {
157 5b237745 Scott Ullrich
158 08c3872d Colin Smith
	global $depth, $curpath, $parsedcfg, $havedata, $listtags;
159
	$parsedcfg = array();
160 5b237745 Scott Ullrich
	$curpath = array();
161
	$depth = 0;
162
	$havedata = 0;
163 528ae96e Scott Ullrich
164 5b237745 Scott Ullrich
	$xml_parser = xml_parser_create();
165 528ae96e Scott Ullrich
166 5b237745 Scott Ullrich
	xml_set_element_handler($xml_parser, "startElement", "endElement");
167
	xml_set_character_data_handler($xml_parser, "cdata");
168 528ae96e Scott Ullrich
169 5b237745 Scott Ullrich
	if (!($fp = fopen($cffile, "r"))) {
170
		die("Error: could not open XML input\n");
171
	}
172 528ae96e Scott Ullrich
173 5b237745 Scott Ullrich
	while ($data = fread($fp, 4096)) {
174
		if (!xml_parse($xml_parser, $data, feof($fp))) {
175 2c1689fd Scott Ullrich
			log_error(sprintf("XML error: %s at line %d\n",
176 5b237745 Scott Ullrich
						xml_error_string(xml_get_error_code($xml_parser)),
177
						xml_get_current_line_number($xml_parser)));
178 2c1689fd Scott Ullrich
			return -1;
179 5b237745 Scott Ullrich
		}
180
	}
181
	xml_parser_free($xml_parser);
182 528ae96e Scott Ullrich
183 08c3872d Colin Smith
	if (!$parsedcfg[$rootobj]) {
184 5b237745 Scott Ullrich
		die("XML error: no $rootobj object found!\n");
185
	}
186 528ae96e Scott Ullrich
187 08c3872d Colin Smith
	return $parsedcfg[$rootobj];
188 5b237745 Scott Ullrich
}
189
190
function dump_xml_config_sub($arr, $indent) {
191
192
	global $listtags;
193 528ae96e Scott Ullrich
194 5b237745 Scott Ullrich
	$xmlconfig = "";
195
196
	foreach ($arr as $ent => $val) {
197
		if (is_array($val)) {
198
			/* is it just a list of multiple values? */
199
			if (in_array(strtolower($ent), $listtags)) {
200
				foreach ($val as $cval) {
201
					if (is_array($cval)) {
202
						$xmlconfig .= str_repeat("\t", $indent);
203
						$xmlconfig .= "<$ent>\n";
204
						$xmlconfig .= dump_xml_config_sub($cval, $indent + 1);
205
						$xmlconfig .= str_repeat("\t", $indent);
206
						$xmlconfig .= "</$ent>\n";
207
					} else {
208
						$xmlconfig .= str_repeat("\t", $indent);
209 0d57f5f3 Scott Ullrich
						if($cval === false) continue;
210
						if(($cval === true) || ($cval === "")) {
211 5b237745 Scott Ullrich
							$xmlconfig .= "<$ent/>\n";
212 0d57f5f3 Scott Ullrich
						} else {
213 5b237745 Scott Ullrich
							$xmlconfig .= "<$ent>" . htmlspecialchars($cval) . "</$ent>\n";
214
					}
215
				}
216 0d57f5f3 Scott Ullrich
				}
217 5b237745 Scott Ullrich
			} else {
218
				/* it's an array */
219
				$xmlconfig .= str_repeat("\t", $indent);
220
				$xmlconfig .= "<$ent>\n";
221
				$xmlconfig .= dump_xml_config_sub($val, $indent + 1);
222
				$xmlconfig .= str_repeat("\t", $indent);
223
				$xmlconfig .= "</$ent>\n";
224
			}
225
		} else {
226
			if ((is_bool($val) && ($val == true)) || ($val === "")) {
227
				$xmlconfig .= str_repeat("\t", $indent);
228
				$xmlconfig .= "<$ent/>\n";
229
			} else if (!is_bool($val)) {
230
				$xmlconfig .= str_repeat("\t", $indent);
231
				$xmlconfig .= "<$ent>" . htmlspecialchars($val) . "</$ent>\n";
232
			}
233
		}
234
	}
235 528ae96e Scott Ullrich
236 5b237745 Scott Ullrich
	return $xmlconfig;
237
}
238
239
function dump_xml_config($arr, $rootobj) {
240 ba6882bf Colin Smith
	global $listtags;
241 eeb6c16e Bill Marquette
	$listtags = listtags();
242 4928848f Scott Ullrich
        if (isset($GLOBALS['custom_listtags'])) {
243
          foreach($GLOBALS['custom_listtags'] as $tag) {
244
            $listtags[] = $tag;
245
          }
246
        }
247 ba6882bf Colin Smith
	return dump_xml_config_raw($arr, $rootobj);
248
}
249
250
function dump_xml_config_pkg($arr, $rootobj) {
251
	global $listtags;
252 eeb6c16e Bill Marquette
	$listtags = listtags_pkg();
253 4928848f Scott Ullrich
        if (isset($GLOBALS['custom_listtags_pkg'])) {
254
          foreach($GLOBALS['custom_listtags_pkg'] as $tag) {
255
            $listtags[] = $tag;
256
          }
257
        }
258 ba6882bf Colin Smith
	return dump_xml_config_raw($arr, $rootobj);
259
}
260
261
function dump_xml_config_raw($arr, $rootobj) {
262 5b237745 Scott Ullrich
263
	$xmlconfig = "<?xml version=\"1.0\"?" . ">\n";
264
	$xmlconfig .= "<$rootobj>\n";
265 528ae96e Scott Ullrich
266 5b237745 Scott Ullrich
	$xmlconfig .= dump_xml_config_sub($arr, 1);
267 528ae96e Scott Ullrich
268 5b237745 Scott Ullrich
	$xmlconfig .= "</$rootobj>\n";
269 528ae96e Scott Ullrich
270 5b237745 Scott Ullrich
	return $xmlconfig;
271
}
272
273 d173230c Seth Mos
?>