Project

General

Profile

Download (5.54 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
/* tags that are always to be handled as lists */
34
$listtags = explode(" ", "row config package columnitem option item fieldname field rule user key subqueue " .
35
	"dnsserver winsserver encryption-algorithm-option hash-algorithm-option hosts tunnel " .
36
	"onetoone staticmap route alias pipe queue shellcmd earlyshellcmd mobilekey " .
37
	"servernat proxyarpnet passthrumac allowedip wolentry vlan menu domainoverrides");
38

    
39
function startElement($parser, $name, $attrs) {
40
	global $parsedcfg, $depth, $curpath, $havedata, $listtags;
41

    
42
	array_push($curpath, strtolower($name));
43

    
44
	$ptr =& $parsedcfg;
45
	foreach ($curpath as $path) {
46
		$ptr =& $ptr[$path];
47
	}
48

    
49
	/* is it an element that belongs to a list? */
50
	if (in_array(strtolower($name), $listtags)) {
51

    
52
		/* is there an array already? */
53
		if (!is_array($ptr)) {
54
			/* make an array */
55
			$ptr = array();
56
		}
57

    
58
		array_push($curpath, count($ptr));
59

    
60
	} else if (isset($ptr)) {
61
		/* multiple entries not allowed for this element, bail out */
62
		die(sprintf("XML error: %s at line %d cannot occur more than once\n",
63
				$name,
64
				xml_get_current_line_number($parser)));
65
	}
66

    
67
	$depth++;
68
	$havedata = $depth;
69
}
70

    
71
function endElement($parser, $name) {
72
	global $depth, $curpath, $parsedcfg, $havedata, $listtags;
73

    
74
	if ($havedata == $depth) {
75
		$ptr =& $parsedcfg;
76
		foreach ($curpath as $path) {
77
			$ptr =& $ptr[$path];
78
		}
79
		$ptr = "";
80
	}
81

    
82
	array_pop($curpath);
83

    
84
	if (in_array(strtolower($name), $listtags))
85
		array_pop($curpath);
86

    
87
	$depth--;
88
}
89

    
90
function cData($parser, $data) {
91
	global $depth, $curpath, $parsedcfg, $havedata;
92

    
93
	$data = trim($data, "\t\n\r");
94

    
95
	if ($data != "") {
96
		$ptr =& $parsedcfg;
97
		foreach ($curpath as $path) {
98
			$ptr =& $ptr[$path];
99
		}
100

    
101
		if (is_string($ptr)) {
102
			$ptr .= $data;
103
		} else {
104
			if (trim($data, " ") != "") {
105
				$ptr = $data;
106
				$havedata++;
107
			}
108
		}
109
	}
110
}
111

    
112
function parse_xml_config($cffile, $rootobj) {
113

    
114
	global $depth, $curpath, $parsedcfg, $havedata, $listtags;
115

    
116
	$parsedcfg = array();
117
	$curpath = array();
118
	$depth = 0;
119
	$havedata = 0;
120

    
121
	$xml_parser = xml_parser_create();
122

    
123
	xml_set_element_handler($xml_parser, "startElement", "endElement");
124
	xml_set_character_data_handler($xml_parser, "cdata");
125

    
126
	if (!($fp = fopen($cffile, "r"))) {
127
		die("Error: could not open XML input\n");
128
	}
129

    
130
	while ($data = fread($fp, 4096)) {
131
		if (!xml_parse($xml_parser, $data, feof($fp))) {
132
			die(sprintf("XML error: %s at line %d\n",
133
						xml_error_string(xml_get_error_code($xml_parser)),
134
						xml_get_current_line_number($xml_parser)));
135
		}
136
	}
137
	xml_parser_free($xml_parser);
138

    
139
	if (!$parsedcfg[$rootobj]) {
140
		die("XML error: no $rootobj object found!\n");
141
	}
142

    
143
	return $parsedcfg[$rootobj];
144
}
145

    
146
function dump_xml_config_sub($arr, $indent) {
147

    
148
	global $listtags;
149

    
150
	$xmlconfig = "";
151

    
152
	foreach ($arr as $ent => $val) {
153
		if (is_array($val)) {
154
			/* is it just a list of multiple values? */
155
			if (in_array(strtolower($ent), $listtags)) {
156
				foreach ($val as $cval) {
157
					if (is_array($cval)) {
158
						$xmlconfig .= str_repeat("\t", $indent);
159
						$xmlconfig .= "<$ent>\n";
160
						$xmlconfig .= dump_xml_config_sub($cval, $indent + 1);
161
						$xmlconfig .= str_repeat("\t", $indent);
162
						$xmlconfig .= "</$ent>\n";
163
					} else {
164
						$xmlconfig .= str_repeat("\t", $indent);
165
						if ((is_bool($cval) && ($cval == true)) ||
166
							($cval === ""))
167
							$xmlconfig .= "<$ent/>\n";
168
						else if (!is_bool($cval))
169
							$xmlconfig .= "<$ent>" . htmlspecialchars($cval) . "</$ent>\n";
170
					}
171
				}
172
			} else {
173
				/* it's an array */
174
				$xmlconfig .= str_repeat("\t", $indent);
175
				$xmlconfig .= "<$ent>\n";
176
				$xmlconfig .= dump_xml_config_sub($val, $indent + 1);
177
				$xmlconfig .= str_repeat("\t", $indent);
178
				$xmlconfig .= "</$ent>\n";
179
			}
180
		} else {
181
			if ((is_bool($val) && ($val == true)) || ($val === "")) {
182
				$xmlconfig .= str_repeat("\t", $indent);
183
				$xmlconfig .= "<$ent/>\n";
184
			} else if (!is_bool($val)) {
185
				$xmlconfig .= str_repeat("\t", $indent);
186
				$xmlconfig .= "<$ent>" . htmlspecialchars($val) . "</$ent>\n";
187
			}
188
		}
189
	}
190

    
191
	return $xmlconfig;
192
}
193

    
194
function dump_xml_config($arr, $rootobj) {
195

    
196
	$xmlconfig = "<?xml version=\"1.0\"?" . ">\n";
197
	$xmlconfig .= "<$rootobj>\n";
198

    
199
	$xmlconfig .= dump_xml_config_sub($arr, 1);
200

    
201
	$xmlconfig .= "</$rootobj>\n";
202

    
203
	return $xmlconfig;
204
}
205

    
206
?>
(16-16/19)