Projet

Général

Profil

Télécharger (10,1 ko) Statistiques
| Branche: | Tag: | Révision:

univnautes / etc / inc / xmlparse.inc @ c650b2f7

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
	/*
36
	 * Please keep this list alpha sorted and no longer than 80 characters
37
	 * I know it's a pain, but it's a pain to find stuff too if it's not
38
	 */
39
	$ret = array(
40
                'acls', 'alias', 'aliasurl', 'allowedip', 'allowedhostname', 'authserver',
41
                'bridged', 'build_port_path',
42
                'ca', 'cacert', 'cert', 'crl', 'clone', 'config', 'container', 'columnitem',
43
                'depends_on_package', 'disk', 'dnsserver', 'dnsupdate', 'domainoverrides', 'dyndns',
44
                'earlyshellcmd', 'element', 'encryption-algorithm-option',
45
                'field', 'fieldname',
46
                'gateway_item', 'gateway_group', 'gif', 'gre', 'group',
47
                'hash-algorithm-option', 'hosts', 'member', 'ifgroupentry', 'igmpentry', 'interface_array', 'item', 'key',        
48
                'lagg', 'lbaction', 'lbpool', 'l7rules', 'lbprotocol',
49
                'member', 'menu', 'tab', 'mobilekey', 'monitor_type', 'mount',
50
		'npt', 'ntpserver',
51
                'onetoone', 'openvpn-server', 'openvpn-client', 'openvpn-csc', 'option',
52
                'package', 'passthrumac', 'phase1', 'phase2', 'ppp', 'pppoe', 'priv', 'proxyarpnet', 'pool',
53
		'qinqentry', 'queue',
54
                'pages', 'pipe', 'radnsserver', 'roll', 'route', 'row', 'rrddatafile', 'rule',
55
		'schedule', 'service', 'servernat', 'servers',
56
                'serversdisabled', 'shellcmd', 'staticmap', 'subqueue',
57
		'timerange', 'tunnel', 'user', 'vip', 'virtual_server', 'vlan',
58
		'winsserver', 'wolentry', 'widget'
59
	);
60
	return array_flip($ret);
61
}
62

    
63
/* Package XML tags that should be treat as a list not as a traditional array */
64
function listtags_pkg() {
65
	$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');
66

    
67
	return array_flip($ret);
68
}
69

    
70
function startElement($parser, $name, $attrs) {
71
	global $parsedcfg, $depth, $curpath, $havedata, $listtags;
72

    
73
	array_push($curpath, strtolower($name));
74

    
75
	$ptr =& $parsedcfg;
76
	foreach ($curpath as $path) {
77
		$ptr =& $ptr[$path];
78
	}
79

    
80
	/* is it an element that belongs to a list? */
81
	if (isset($listtags[strtolower($name)])) {
82

    
83
		/* is there an array already? */
84
		if (!is_array($ptr)) {
85
			/* make an array */
86
			$ptr = array();
87
		}
88

    
89
		array_push($curpath, count($ptr));
90

    
91
	} else if (isset($ptr)) {
92
		/* multiple entries not allowed for this element, bail out */
93
		die(sprintf(gettext('XML error: %1$s at line %2$d cannot occur more than once') . "\n",
94
				$name,
95
				xml_get_current_line_number($parser)));
96
	}
97

    
98
	$depth++;
99
	$havedata = $depth;
100
}
101

    
102
function endElement($parser, $name) {
103
	global $depth, $curpath, $parsedcfg, $havedata, $listtags;
104

    
105
	if ($havedata == $depth) {
106
		$ptr =& $parsedcfg;
107
		foreach ($curpath as $path) {
108
			$ptr =& $ptr[$path];
109
		}
110
		$ptr = "";
111
	}
112

    
113
	array_pop($curpath);
114

    
115
	if (isset($listtags[strtolower($name)]))
116
		array_pop($curpath);
117

    
118
	$depth--;
119
}
120

    
121
function cData($parser, $data) {
122
	global $depth, $curpath, $parsedcfg, $havedata;
123

    
124
	$data = trim($data, "\t\n\r");
125

    
126
	if ($data != "") {
127
		$ptr =& $parsedcfg;
128
		foreach ($curpath as $path) {
129
			$ptr =& $ptr[$path];
130
		}
131

    
132
		if (is_string($ptr)) {
133
			$ptr .= html_entity_decode($data);
134
		} else {
135
			if (trim($data, " ") != "") {
136
				$ptr = html_entity_decode($data);
137
				$havedata++;
138
			}
139
		}
140
	}
141
}
142

    
143
function parse_xml_config($cffile, $rootobj, $isstring = "false") {
144
	global $listtags;
145
	$listtags = listtags();
146
        if (isset($GLOBALS['custom_listtags'])) {
147
          foreach($GLOBALS['custom_listtags'] as $tag) {
148
            $listtags[$tag] = $tag;
149
          }
150
        }
151
	return parse_xml_config_raw($cffile, $rootobj, $isstring);
152
}
153

    
154
function parse_xml_config_pkg($cffile, $rootobj, $isstring = "false") {
155
	global $listtags;
156
	$listtags = listtags_pkg();
157
        if (isset($GLOBALS['custom_listtags_pkg'])) {
158
          foreach($GLOBALS['custom_listtags_pkg'] as $tag) {
159
            $listtags[$tag] = $tag;
160
          }
161
        }
162
	$cfg =parse_xml_config_raw($cffile, $rootobj, $isstring);
163
	if ($cfg == -1)
164
		return array();
165
	
166
	return $cfg;
167
}
168

    
169
function parse_xml_config_raw($cffile, $rootobj, $isstring = "false") {
170

    
171
	global $depth, $curpath, $parsedcfg, $havedata, $listtags;
172
	$parsedcfg = array();
173
	$curpath = array();
174
	$depth = 0;
175
	$havedata = 0;
176

    
177
	$xml_parser = xml_parser_create();
178

    
179
	xml_set_element_handler($xml_parser, "startElement", "endElement");
180
	xml_set_character_data_handler($xml_parser, "cdata");
181
	xml_parser_set_option($xml_parser,XML_OPTION_SKIP_WHITE, 1); 
182

    
183
	if (!($fp = fopen($cffile, "r"))) {
184
		log_error(gettext("Error: could not open XML input") . "\n");
185
		return -1;
186
	}
187

    
188
	while ($data = fread($fp, 4096)) {
189
		if (!xml_parse($xml_parser, $data, feof($fp))) {
190
			log_error(sprintf(gettext('XML error: %1$s at line %2$d in %3$s') . "\n",
191
						xml_error_string(xml_get_error_code($xml_parser)),
192
						xml_get_current_line_number($xml_parser),
193
						$cffile));
194
			return -1;
195
		}
196
	}
197
	xml_parser_free($xml_parser);
198

    
199
	if ($rootobj) {
200
		if (!is_array($rootobj))
201
			$rootobj = array($rootobj);
202
		foreach ($rootobj as $rootobj_name)
203
			if ($parsedcfg[$rootobj_name])
204
				break;
205
		
206
		if (!$parsedcfg[$rootobj_name]) {
207
			log_error(sprintf(gettext("XML error: no %s object found!") . "\n", implode(" or ", $rootobj)));
208
			return -1;
209
		}
210
		return $parsedcfg[$rootobj_name];
211
	} else {
212
		return $parsedcfg;
213
	}
214
}
215

    
216
function dump_xml_config_sub($arr, $indent) {
217

    
218
	global $listtags;
219

    
220
	$xmlconfig = "";
221

    
222
	foreach ($arr as $ent => $val) {
223
		if (is_array($val)) {
224
			/* is it just a list of multiple values? */
225
			if (isset($listtags[strtolower($ent)])) {
226
				foreach ($val as $cval) {
227
					if (is_array($cval)) {
228
						if (empty($cval)) {
229
							$xmlconfig .= str_repeat("\t", $indent);
230
							$xmlconfig .= "<$ent/>\n";
231
						} else {
232
							$xmlconfig .= str_repeat("\t", $indent);
233
							$xmlconfig .= "<$ent>\n";
234
							$xmlconfig .= dump_xml_config_sub($cval, $indent + 1);
235
							$xmlconfig .= str_repeat("\t", $indent);
236
							$xmlconfig .= "</$ent>\n";
237
						}
238
					} else {
239
						if($cval === false) continue;
240
						$xmlconfig .= str_repeat("\t", $indent);
241
						if((is_bool($cval) && $cval == true) || ($cval === "")) {
242
							$xmlconfig .= "<$ent/>\n";
243
						} else if ((substr($ent, 0, 5) == "descr")
244
							|| (substr($ent, 0, 6) == "detail")
245
							|| (substr($ent, 0, 12) == "login_banner")
246
							|| (substr($ent, 0, 9) == "ldap_attr")
247
							|| (substr($ent, 0, 9) == "ldap_bind")
248
							|| (substr($ent, 0, 11) == "ldap_basedn")
249
							|| (substr($ent, 0, 18) == "ldap_authcn")
250
							|| (substr($ent, 0, 19) == "ldap_extended_query")) {
251
							$xmlconfig .= "<$ent><![CDATA[" . htmlentities($cval) . "]]></$ent>\n";
252
						} else {
253
							$xmlconfig .= "<$ent>" . htmlentities($cval) . "</$ent>\n";
254
						}
255
					}
256
				}
257
			} else if (empty($val)) {
258
				$xmlconfig .= str_repeat("\t", $indent);
259
				$xmlconfig .= "<$ent/>\n";
260
			} else {
261
				/* it's an array */
262
				$xmlconfig .= str_repeat("\t", $indent);
263
				$xmlconfig .= "<$ent>\n";
264
				$xmlconfig .= dump_xml_config_sub($val, $indent + 1);
265
				$xmlconfig .= str_repeat("\t", $indent);
266
				$xmlconfig .= "</$ent>\n";
267
			}
268
		} else {
269
			if ((is_bool($val) && ($val == true)) || ($val === "")) {
270
				$xmlconfig .= str_repeat("\t", $indent);
271
				$xmlconfig .= "<$ent/>\n";
272
			} else if (!is_bool($val)) {
273
				$xmlconfig .= str_repeat("\t", $indent);
274
				if ((substr($ent, 0, 5) == "descr")
275
				|| (substr($ent, 0, 6) == "detail")
276
				|| (substr($ent, 0, 12) == "login_banner")
277
				|| (substr($ent, 0, 9) == "ldap_attr")
278
				|| (substr($ent, 0, 9) == "ldap_bind")
279
				|| (substr($ent, 0, 11) == "ldap_basedn")
280
				|| (substr($ent, 0, 18) == "ldap_authcn")
281
				|| (substr($ent, 0, 19) == "ldap_extended_query"))
282
					$xmlconfig .= "<$ent><![CDATA[" . htmlentities($val) . "]]></$ent>\n";
283
				else
284
					$xmlconfig .= "<$ent>" . htmlentities($val) . "</$ent>\n";
285
			}
286
		}
287
	}
288

    
289
	return $xmlconfig;
290
}
291

    
292
function dump_xml_config($arr, $rootobj) {
293
	global $listtags;
294
	$listtags = listtags();
295
        if (isset($GLOBALS['custom_listtags'])) {
296
          foreach($GLOBALS['custom_listtags'] as $tag) {
297
            $listtags[$tag] = $tag;
298
          }
299
        }
300
	return dump_xml_config_raw($arr, $rootobj);
301
}
302

    
303
function dump_xml_config_pkg($arr, $rootobj) {
304
	global $listtags;
305
	$listtags = listtags_pkg();
306
        if (isset($GLOBALS['custom_listtags_pkg'])) {
307
          foreach($GLOBALS['custom_listtags_pkg'] as $tag) {
308
            $listtags[$tag] = $tag;
309
          }
310
        }
311
	return dump_xml_config_raw($arr, $rootobj);
312
}
313

    
314
function dump_xml_config_raw($arr, $rootobj) {
315

    
316
	$xmlconfig = "<?xml version=\"1.0\"?" . ">\n";
317
	$xmlconfig .= "<$rootobj>\n";
318

    
319
	$xmlconfig .= dump_xml_config_sub($arr, 1);
320

    
321
	$xmlconfig .= "</$rootobj>\n";
322

    
323
	return $xmlconfig;
324
}
325

    
326
?>
(62-62/68)