Project

General

Profile

Download (8.99 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 79e99eb4 Scott Ullrich
		"alias aliasurl allowedip allowedhostname authserver bridged ca cacert cert crl ".
40
		"clone config container columnitem build_port_path depends_on_package disk dnsserver  ".
41
		"dnsupdate domainoverrides dyndns earlyshellcmd element encryption-algorithm-option ".
42 9f428275 Erik Fonnesbeck
		"field fieldname hash-algorithm-option gateway_item gateway_group gif gre ".
43 0ec2fdf0 Ermal Lu?i
		"group hosts member ifgroupentry igmpentry interface_array item key lagg " .
44
		"lbaction lbpool l7rules lbprotocol ".
45
		"member menu tab mobilekey monitor_type mount ntpserver onetoone ".
46 b6617403 Matthew Grooms
		"openvpn-server openvpn-client openvpn-csc " .
47 0e642c78 Ermal
		"option package passthrumac phase1 phase2 ppp pppoe priv proxyarpnet qinqentry queue ".
48 336e3c1c Charlie
		"pages pipe roll route row rrddatafile rule schedule service servernat servers ".
49 0919224f Bill Marquette
		"serversdisabled earlyshellcmd shellcmd staticmap subqueue timerange ".
50 71f88d75 smos
		"tunnel user vip virtual_server vlan winsserver wolentry widget npt"
51 0919224f Bill Marquette
		);
52 26433cb8 Scott Ullrich
	return $ret;
53 eeb6c16e Bill Marquette
}
54
55 2fe045e5 Bill Marquette
/* Package XML tags that should be treat as a list not as a traditional array */
56 eeb6c16e Bill Marquette
function listtags_pkg() {
57 d9a6bd8f Scott Ullrich
	$ret = array("build_port_path", "depends_on_package", "onetoone", "queue", "rule", "servernat", "alias", "additional_files_needed", "tab", "template", "menu", "rowhelperfield", "service", "step", "package", "columnitem", "option", "item", "field", "package", "file");
58 eeb6c16e Bill Marquette
59 26433cb8 Scott Ullrich
	return $ret;
60 eeb6c16e Bill Marquette
}
61
62 26433cb8 Scott Ullrich
function startElement($parser, $name, $attrs) {
63 1fb064e8 Erik Fonnesbeck
	global $parsedcfg, $depth, $curpath, $havedata, $listtags;
64 528ae96e Scott Ullrich
65 26433cb8 Scott Ullrich
	array_push($curpath, strtolower($name));
66
67
	$ptr =& $parsedcfg;
68
	foreach ($curpath as $path) {
69
		$ptr =& $ptr[$path];
70
	}
71
72
	/* is it an element that belongs to a list? */
73
	if (in_array(strtolower($name), $listtags)) {
74
75
		/* is there an array already? */
76
		if (!is_array($ptr)) {
77
			/* make an array */
78
			$ptr = array();
79
		}
80
81
		array_push($curpath, count($ptr));
82
83
	} else if (isset($ptr)) {
84
		/* multiple entries not allowed for this element, bail out */
85 addc0439 Renato Botelho
		die(sprintf(gettext('XML error: %1$s at line %2$d cannot occur more than once') . "\n",
86 26433cb8 Scott Ullrich
				$name,
87
				xml_get_current_line_number($parser)));
88
	}
89
90
	$depth++;
91
	$havedata = $depth;
92
}
93
94
function endElement($parser, $name) {
95
	global $depth, $curpath, $parsedcfg, $havedata, $listtags;
96
97
	if ($havedata == $depth) {
98
		$ptr =& $parsedcfg;
99
		foreach ($curpath as $path) {
100
			$ptr =& $ptr[$path];
101
		}
102
		$ptr = "";
103
	}
104
105
	array_pop($curpath);
106
107
	if (in_array(strtolower($name), $listtags))
108
		array_pop($curpath);
109
110
	$depth--;
111
}
112
113
function cData($parser, $data) {
114
	global $depth, $curpath, $parsedcfg, $havedata;
115
116
	$data = trim($data, "\t\n\r");
117
118
	if ($data != "") {
119
		$ptr =& $parsedcfg;
120
		foreach ($curpath as $path) {
121
			$ptr =& $ptr[$path];
122
		}
123
124
		if (is_string($ptr)) {
125 2e6a43a1 Ermal
			$ptr .= html_entity_decode($data);
126 26433cb8 Scott Ullrich
		} else {
127
			if (trim($data, " ") != "") {
128 2e6a43a1 Ermal
				$ptr = html_entity_decode($data);
129 26433cb8 Scott Ullrich
				$havedata++;
130
			}
131
		}
132
	}
133 5b237745 Scott Ullrich
}
134
135 48ca2dc9 Colin Smith
function parse_xml_config($cffile, $rootobj, $isstring = "false") {
136 cb980a2f Colin Smith
	global $listtags;
137 eeb6c16e Bill Marquette
	$listtags = listtags();
138 eb72ceda Scott Ullrich
        if (isset($GLOBALS['custom_listtags'])) {
139
          foreach($GLOBALS['custom_listtags'] as $tag) {
140 26433cb8 Scott Ullrich
            $listtags[] = $tag;
141 eb72ceda Scott Ullrich
          }
142
        }
143 26433cb8 Scott Ullrich
	return parse_xml_config_raw($cffile, $rootobj, $isstring);
144 cb980a2f Colin Smith
}
145
146 48ca2dc9 Colin Smith
function parse_xml_config_pkg($cffile, $rootobj, $isstring = "false") {
147 cb980a2f Colin Smith
	global $listtags;
148 eeb6c16e Bill Marquette
	$listtags = listtags_pkg();
149 eb72ceda Scott Ullrich
        if (isset($GLOBALS['custom_listtags_pkg'])) {
150
          foreach($GLOBALS['custom_listtags_pkg'] as $tag) {
151 26433cb8 Scott Ullrich
            $listtags[] = $tag;
152 eb72ceda Scott Ullrich
          }
153
        }
154 bef6cb99 Ermal
	$cfg =parse_xml_config_raw($cffile, $rootobj, $isstring);
155
	if ($cfg == -1)
156 187ce62b Ermal
		return array();
157 bef6cb99 Ermal
	
158
	return $cfg;
159 cb980a2f Colin Smith
}
160
161 48ca2dc9 Colin Smith
function parse_xml_config_raw($cffile, $rootobj, $isstring = "false") {
162 5b237745 Scott Ullrich
163 26433cb8 Scott Ullrich
	global $depth, $curpath, $parsedcfg, $havedata, $listtags;
164 08c3872d Colin Smith
	$parsedcfg = array();
165 26433cb8 Scott Ullrich
	$curpath = array();
166
	$depth = 0;
167
	$havedata = 0;
168
169
	$xml_parser = xml_parser_create();
170
171
	xml_set_element_handler($xml_parser, "startElement", "endElement");
172
	xml_set_character_data_handler($xml_parser, "cdata");
173 9f695b0f Ermal Lu?i
	xml_parser_set_option($xml_parser,XML_OPTION_SKIP_WHITE, 1); 
174 26433cb8 Scott Ullrich
175
	if (!($fp = fopen($cffile, "r"))) {
176 ada992bd Renato Botelho
		log_error(gettext("Error: could not open XML input") . "\n");
177 541989d5 Ermal
		return -1;
178 26433cb8 Scott Ullrich
	}
179 d7b6c842 Ermal Lu?i
180 26433cb8 Scott Ullrich
	while ($data = fread($fp, 4096)) {
181
		if (!xml_parse($xml_parser, $data, feof($fp))) {
182 c92ccac7 Vinicius Coque
			log_error(sprintf(gettext('XML error: %1$s at line %2$d in %3$s') . "\n",
183 26433cb8 Scott Ullrich
						xml_error_string(xml_get_error_code($xml_parser)),
184 7af33a75 jim-p
						xml_get_current_line_number($xml_parser),
185
						$cffile));
186 26433cb8 Scott Ullrich
			return -1;
187
		}
188
	}
189
	xml_parser_free($xml_parser);
190
191 990d7c03 Erik Fonnesbeck
	if (!is_array($rootobj))
192
		$rootobj = array($rootobj);
193
	foreach ($rootobj as $rootobj_name)
194
		if ($parsedcfg[$rootobj_name])
195
			break;
196
197
	if (!$parsedcfg[$rootobj_name]) {
198 ada992bd Renato Botelho
		log_error(sprintf(gettext("XML error: no %s object found!") . "\n", $rootobj));
199 541989d5 Ermal
		return -1;
200 26433cb8 Scott Ullrich
	}
201 528ae96e Scott Ullrich
202 990d7c03 Erik Fonnesbeck
	return $parsedcfg[$rootobj_name];
203 5b237745 Scott Ullrich
}
204
205
function dump_xml_config_sub($arr, $indent) {
206
207
	global $listtags;
208 528ae96e Scott Ullrich
209 5b237745 Scott Ullrich
	$xmlconfig = "";
210
211
	foreach ($arr as $ent => $val) {
212
		if (is_array($val)) {
213
			/* is it just a list of multiple values? */
214 26433cb8 Scott Ullrich
			if (in_array(strtolower($ent), $listtags)) {
215 5b237745 Scott Ullrich
				foreach ($val as $cval) {
216
					if (is_array($cval)) {
217 70d6b5c4 Ermal
						if (empty($cval)) {
218
							$xmlconfig .= str_repeat("\t", $indent);
219
							$xmlconfig .= "<$ent/>\n";
220
						} else {
221
							$xmlconfig .= str_repeat("\t", $indent);
222
							$xmlconfig .= "<$ent>\n";
223
							$xmlconfig .= dump_xml_config_sub($cval, $indent + 1);
224
							$xmlconfig .= str_repeat("\t", $indent);
225
							$xmlconfig .= "</$ent>\n";
226
						}
227 5b237745 Scott Ullrich
					} else {
228 0d57f5f3 Scott Ullrich
						if($cval === false) continue;
229 70d6b5c4 Ermal
						$xmlconfig .= str_repeat("\t", $indent);
230
						if((is_bool($cval) && $cval == true) || ($cval === "")) {
231 5b237745 Scott Ullrich
							$xmlconfig .= "<$ent/>\n";
232 72b7aa4a jim-p
						} else if ((substr($ent, 0, 5) == "descr") || (substr($ent, 0, 6) == "detail")) {
233 02611466 Ermal
							$xmlconfig .= "<$ent><![CDATA[" . htmlentities($cval) . "]]></$ent>\n";
234 0d57f5f3 Scott Ullrich
						} else {
235 2e6a43a1 Ermal
							$xmlconfig .= "<$ent>" . htmlentities($cval) . "</$ent>\n";
236 70d6b5c4 Ermal
						}
237 5b237745 Scott Ullrich
					}
238
				}
239 78dd5020 Ermal Lu?i
			} else if (empty($val)) {
240
				$xmlconfig .= str_repeat("\t", $indent);
241
				$xmlconfig .= "<$ent/>\n";
242 5b237745 Scott Ullrich
			} else {
243
				/* it's an array */
244
				$xmlconfig .= str_repeat("\t", $indent);
245
				$xmlconfig .= "<$ent>\n";
246
				$xmlconfig .= dump_xml_config_sub($val, $indent + 1);
247
				$xmlconfig .= str_repeat("\t", $indent);
248
				$xmlconfig .= "</$ent>\n";
249
			}
250
		} else {
251
			if ((is_bool($val) && ($val == true)) || ($val === "")) {
252
				$xmlconfig .= str_repeat("\t", $indent);
253
				$xmlconfig .= "<$ent/>\n";
254
			} else if (!is_bool($val)) {
255
				$xmlconfig .= str_repeat("\t", $indent);
256 72b7aa4a jim-p
				if ((substr($ent, 0, 5) == "descr") || (substr($ent, 0, 6) == "detail"))
257 02611466 Ermal
					$xmlconfig .= "<$ent><![CDATA[" . htmlentities($val) . "]]></$ent>\n";
258 2e6a43a1 Ermal
				else
259
					$xmlconfig .= "<$ent>" . htmlentities($val) . "</$ent>\n";
260 5b237745 Scott Ullrich
			}
261
		}
262
	}
263 528ae96e Scott Ullrich
264 5b237745 Scott Ullrich
	return $xmlconfig;
265
}
266
267
function dump_xml_config($arr, $rootobj) {
268 ba6882bf Colin Smith
	global $listtags;
269 eeb6c16e Bill Marquette
	$listtags = listtags();
270 4928848f Scott Ullrich
        if (isset($GLOBALS['custom_listtags'])) {
271
          foreach($GLOBALS['custom_listtags'] as $tag) {
272 26433cb8 Scott Ullrich
            $listtags[] = $tag;
273 4928848f Scott Ullrich
          }
274
        }
275 ba6882bf Colin Smith
	return dump_xml_config_raw($arr, $rootobj);
276
}
277
278
function dump_xml_config_pkg($arr, $rootobj) {
279
	global $listtags;
280 eeb6c16e Bill Marquette
	$listtags = listtags_pkg();
281 4928848f Scott Ullrich
        if (isset($GLOBALS['custom_listtags_pkg'])) {
282
          foreach($GLOBALS['custom_listtags_pkg'] as $tag) {
283 26433cb8 Scott Ullrich
            $listtags[] = $tag;
284 4928848f Scott Ullrich
          }
285
        }
286 ba6882bf Colin Smith
	return dump_xml_config_raw($arr, $rootobj);
287
}
288
289
function dump_xml_config_raw($arr, $rootobj) {
290 5b237745 Scott Ullrich
291
	$xmlconfig = "<?xml version=\"1.0\"?" . ">\n";
292
	$xmlconfig .= "<$rootobj>\n";
293 528ae96e Scott Ullrich
294 5b237745 Scott Ullrich
	$xmlconfig .= dump_xml_config_sub($arr, 1);
295 528ae96e Scott Ullrich
296 5b237745 Scott Ullrich
	$xmlconfig .= "</$rootobj>\n";
297 528ae96e Scott Ullrich
298 5b237745 Scott Ullrich
	return $xmlconfig;
299
}
300
301 187ce62b Ermal
?>