Project

General

Profile

Download (9.08 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
  /* 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
	$ret = explode(" ",
39
		"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
		"field fieldname hash-algorithm-option gateway_item gateway_group gif gre ".
43
		"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
		"openvpn-server openvpn-client openvpn-csc " .
47
		"option package passthrumac phase1 phase2 ppp pppoe priv proxyarpnet qinqentry queue ".
48
		"pages pipe radnsserver roll route row rrddatafile rule schedule service servernat servers ".
49
		"serversdisabled earlyshellcmd shellcmd staticmap subqueue timerange ".
50
		"tunnel user vip virtual_server vlan winsserver wolentry widget npt pool"
51
		);
52
	return $ret;
53
}
54

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

    
59
	return $ret;
60
}
61

    
62
function startElement($parser, $name, $attrs) {
63
	global $parsedcfg, $depth, $curpath, $havedata, $listtags;
64

    
65
	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
		die(sprintf(gettext('XML error: %1$s at line %2$d cannot occur more than once') . "\n",
86
				$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
			$ptr .= html_entity_decode($data);
126
		} else {
127
			if (trim($data, " ") != "") {
128
				$ptr = html_entity_decode($data);
129
				$havedata++;
130
			}
131
		}
132
	}
133
}
134

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

    
146
function parse_xml_config_pkg($cffile, $rootobj, $isstring = "false") {
147
	global $listtags;
148
	$listtags = listtags_pkg();
149
        if (isset($GLOBALS['custom_listtags_pkg'])) {
150
          foreach($GLOBALS['custom_listtags_pkg'] as $tag) {
151
            $listtags[] = $tag;
152
          }
153
        }
154
	$cfg =parse_xml_config_raw($cffile, $rootobj, $isstring);
155
	if ($cfg == -1)
156
		return array();
157
	
158
	return $cfg;
159
}
160

    
161
function parse_xml_config_raw($cffile, $rootobj, $isstring = "false") {
162

    
163
	global $depth, $curpath, $parsedcfg, $havedata, $listtags;
164
	$parsedcfg = array();
165
	$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
	xml_parser_set_option($xml_parser,XML_OPTION_SKIP_WHITE, 1); 
174

    
175
	if (!($fp = fopen($cffile, "r"))) {
176
		log_error(gettext("Error: could not open XML input") . "\n");
177
		return -1;
178
	}
179

    
180
	while ($data = fread($fp, 4096)) {
181
		if (!xml_parse($xml_parser, $data, feof($fp))) {
182
			log_error(sprintf(gettext('XML error: %1$s at line %2$d in %3$s') . "\n",
183
						xml_error_string(xml_get_error_code($xml_parser)),
184
						xml_get_current_line_number($xml_parser),
185
						$cffile));
186
			return -1;
187
		}
188
	}
189
	xml_parser_free($xml_parser);
190

    
191
	if ($rootobj) {
192
		if (!is_array($rootobj))
193
			$rootobj = array($rootobj);
194
		foreach ($rootobj as $rootobj_name)
195
			if ($parsedcfg[$rootobj_name])
196
				break;
197
		
198
		if (!$parsedcfg[$rootobj_name]) {
199
			log_error(sprintf(gettext("XML error: no %s object found!") . "\n", implode(" or ", $rootobj)));
200
			return -1;
201
		}
202
		return $parsedcfg[$rootobj_name];
203
	} else {
204
		return $parsedcfg;
205
	}
206
}
207

    
208
function dump_xml_config_sub($arr, $indent) {
209

    
210
	global $listtags;
211

    
212
	$xmlconfig = "";
213

    
214
	foreach ($arr as $ent => $val) {
215
		if (is_array($val)) {
216
			/* is it just a list of multiple values? */
217
			if (in_array(strtolower($ent), $listtags)) {
218
				foreach ($val as $cval) {
219
					if (is_array($cval)) {
220
						if (empty($cval)) {
221
							$xmlconfig .= str_repeat("\t", $indent);
222
							$xmlconfig .= "<$ent/>\n";
223
						} else {
224
							$xmlconfig .= str_repeat("\t", $indent);
225
							$xmlconfig .= "<$ent>\n";
226
							$xmlconfig .= dump_xml_config_sub($cval, $indent + 1);
227
							$xmlconfig .= str_repeat("\t", $indent);
228
							$xmlconfig .= "</$ent>\n";
229
						}
230
					} else {
231
						if($cval === false) continue;
232
						$xmlconfig .= str_repeat("\t", $indent);
233
						if((is_bool($cval) && $cval == true) || ($cval === "")) {
234
							$xmlconfig .= "<$ent/>\n";
235
						} else if ((substr($ent, 0, 5) == "descr") || (substr($ent, 0, 6) == "detail")) {
236
							$xmlconfig .= "<$ent><![CDATA[" . htmlentities($cval) . "]]></$ent>\n";
237
						} else {
238
							$xmlconfig .= "<$ent>" . htmlentities($cval) . "</$ent>\n";
239
						}
240
					}
241
				}
242
			} else if (empty($val)) {
243
				$xmlconfig .= str_repeat("\t", $indent);
244
				$xmlconfig .= "<$ent/>\n";
245
			} else {
246
				/* it's an array */
247
				$xmlconfig .= str_repeat("\t", $indent);
248
				$xmlconfig .= "<$ent>\n";
249
				$xmlconfig .= dump_xml_config_sub($val, $indent + 1);
250
				$xmlconfig .= str_repeat("\t", $indent);
251
				$xmlconfig .= "</$ent>\n";
252
			}
253
		} else {
254
			if ((is_bool($val) && ($val == true)) || ($val === "")) {
255
				$xmlconfig .= str_repeat("\t", $indent);
256
				$xmlconfig .= "<$ent/>\n";
257
			} else if (!is_bool($val)) {
258
				$xmlconfig .= str_repeat("\t", $indent);
259
				if ((substr($ent, 0, 5) == "descr") || (substr($ent, 0, 6) == "detail"))
260
					$xmlconfig .= "<$ent><![CDATA[" . htmlentities($val) . "]]></$ent>\n";
261
				else
262
					$xmlconfig .= "<$ent>" . htmlentities($val) . "</$ent>\n";
263
			}
264
		}
265
	}
266

    
267
	return $xmlconfig;
268
}
269

    
270
function dump_xml_config($arr, $rootobj) {
271
	global $listtags;
272
	$listtags = listtags();
273
        if (isset($GLOBALS['custom_listtags'])) {
274
          foreach($GLOBALS['custom_listtags'] as $tag) {
275
            $listtags[] = $tag;
276
          }
277
        }
278
	return dump_xml_config_raw($arr, $rootobj);
279
}
280

    
281
function dump_xml_config_pkg($arr, $rootobj) {
282
	global $listtags;
283
	$listtags = listtags_pkg();
284
        if (isset($GLOBALS['custom_listtags_pkg'])) {
285
          foreach($GLOBALS['custom_listtags_pkg'] as $tag) {
286
            $listtags[] = $tag;
287
          }
288
        }
289
	return dump_xml_config_raw($arr, $rootobj);
290
}
291

    
292
function dump_xml_config_raw($arr, $rootobj) {
293

    
294
	$xmlconfig = "<?xml version=\"1.0\"?" . ">\n";
295
	$xmlconfig .= "<$rootobj>\n";
296

    
297
	$xmlconfig .= dump_xml_config_sub($arr, 1);
298

    
299
	$xmlconfig .= "</$rootobj>\n";
300

    
301
	return $xmlconfig;
302
}
303

    
304
?>
(62-62/68)