Project

General

Profile

Download (7.42 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
/* The following items will be treated as arrays in config.xml */
34
function listtags() {
35
	$ret = explode(" ", "element alias aliasurl allowedip cacert config columnitem disk dnsserver domainoverrides " .
36
		"earlyshellcmd encryption-algorithm-option field fieldname hash-algorithm-option " .
37
		"hosts group interface_array item key lbpool menu mobilekey mount onetoone option package passthrumac priv proxyarpnet " .
38
		"queue pages pipe route row rule schedule service servernat servers earlyshellcmd shellcmd staticmap subqueue " .
39
		"timepart tunnel user vip virtual_server vlan winsserver wolentry depends_on_package");
40
	return $ret;
41
}
42

    
43
/* Package XML tags that should be treat as a list not as a traditional array */
44
function listtags_pkg() {
45
	$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

    
47
	return $ret;
48
}
49

    
50
function startElement($parser, $name, $attrs) {
51
	global $parsedcfg, $depth, $curpath, $havedata, $listtags;
52

    
53
	array_push($curpath, strtolower($name));
54

    
55
	$ptr =& $parsedcfg;
56
	foreach ($curpath as $path) {
57
		$ptr =& $ptr[$path];
58
	}
59

    
60
	/* is it an element that belongs to a list? */
61
	if (in_array(strtolower($name), $listtags)) {
62

    
63
		/* is there an array already? */
64
		if (!is_array($ptr)) {
65
			/* make an array */
66
			$ptr = array();
67
		}
68

    
69
		array_push($curpath, count($ptr));
70

    
71
	} 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

    
78
	$depth++;
79
	$havedata = $depth;
80
}
81

    
82
function endElement($parser, $name) {
83
	global $depth, $curpath, $parsedcfg, $havedata, $listtags;
84

    
85
	if ($havedata == $depth) {
86
		$ptr =& $parsedcfg;
87
		foreach ($curpath as $path) {
88
			$ptr =& $ptr[$path];
89
		}
90
		$ptr = "";
91
	}
92

    
93
	array_pop($curpath);
94

    
95
	if (in_array(strtolower($name), $listtags))
96
		array_pop($curpath);
97

    
98
	$depth--;
99
}
100

    
101
function cData($parser, $data) {
102
	global $depth, $curpath, $parsedcfg, $havedata;
103

    
104
	$data = trim($data, "\t\n\r");
105

    
106
	if ($data != "") {
107
		$ptr =& $parsedcfg;
108
		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
function parse_xml_config($cffile, $rootobj, $isstring = "false") {
124
	global $listtags;
125
	$listtags = listtags();
126
        if (isset($GLOBALS['custom_listtags'])) {
127
          foreach($GLOBALS['custom_listtags'] as $tag) {
128
            $listtags[] = $tag;
129
          }
130
        }
131
	return parse_xml_config_raw($cffile, $rootobj, $isstring);
132
}
133

    
134
function parse_xml_config_pkg($cffile, $rootobj, $isstring = "false") {
135
	global $listtags;
136
	$listtags = listtags_pkg();
137
        if (isset($GLOBALS['custom_listtags_pkg'])) {
138
          foreach($GLOBALS['custom_listtags_pkg'] as $tag) {
139
            $listtags[] = $tag;
140
          }
141
        }
142
	return parse_xml_config_raw($cffile, $rootobj, $isstring);
143
}
144

    
145
function parse_xml_config_raw($cffile, $rootobj, $isstring = "false") {
146

    
147
	global $depth, $curpath, $parsedcfg, $havedata, $listtags;
148
	$parsedcfg = array();
149
	$curpath = array();
150
	$depth = 0;
151
	$havedata = 0;
152

    
153
	$xml_parser = xml_parser_create();
154

    
155
	xml_set_element_handler($xml_parser, "startElement", "endElement");
156
	xml_set_character_data_handler($xml_parser, "cdata");
157

    
158
	if (!($fp = fopen($cffile, "r"))) {
159
		die("Error: could not open XML input\n");
160
	}
161

    
162
	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

    
171
	if (!$parsedcfg[$rootobj]) {
172
		die("XML error: no $rootobj object found!\n");
173
	}
174

    
175
	return $parsedcfg[$rootobj];
176
}
177

    
178
function dump_xml_config_sub($arr, $indent) {
179

    
180
	global $listtags;
181

    
182
	$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
						if($cval === false) continue;
198
						if(($cval === true) || ($cval === "")) {
199
							$xmlconfig .= "<$ent/>\n";
200
						} else {
201
							$xmlconfig .= "<$ent>" . htmlspecialchars($cval) . "</$ent>\n";
202
					}
203
				}
204
				}
205
			} 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

    
224
	return $xmlconfig;
225
}
226

    
227
function dump_xml_config($arr, $rootobj) {
228
	global $listtags;
229
	$listtags = listtags();
230
        if (isset($GLOBALS['custom_listtags'])) {
231
          foreach($GLOBALS['custom_listtags'] as $tag) {
232
            $listtags[] = $tag;
233
          }
234
        }
235
	return dump_xml_config_raw($arr, $rootobj);
236
}
237

    
238
function dump_xml_config_pkg($arr, $rootobj) {
239
	global $listtags;
240
	$listtags = listtags_pkg();
241
        if (isset($GLOBALS['custom_listtags_pkg'])) {
242
          foreach($GLOBALS['custom_listtags_pkg'] as $tag) {
243
            $listtags[] = $tag;
244
          }
245
        }
246
	return dump_xml_config_raw($arr, $rootobj);
247
}
248

    
249
function dump_xml_config_raw($arr, $rootobj) {
250

    
251
	$xmlconfig = "<?xml version=\"1.0\"?" . ">\n";
252
	$xmlconfig .= "<$rootobj>\n";
253

    
254
	$xmlconfig .= dump_xml_config_sub($arr, 1);
255

    
256
	$xmlconfig .= "</$rootobj>\n";
257

    
258
	return $xmlconfig;
259
}
260

    
261
?>
(24-24/27)