Project

General

Profile

« Previous | Next » 

Revision 528ae96e

Added by Scott Ullrich over 20 years ago

Add tem field as array xml markers

View differences:

etc/inc/xmlparse.inc
3 3
	xmlparse.inc
4 4
	functions to parse/dump configuration files in XML format
5 5
	part of m0n0wall (http://m0n0.ch/wall)
6
	
6

  
7 7
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
8 8
	All rights reserved.
9
	
9

  
10 10
	Redistribution and use in source and binary forms, with or without
11 11
	modification, are permitted provided that the following conditions are met:
12
	
12

  
13 13
	1. Redistributions of source code must retain the above copyright notice,
14 14
	   this list of conditions and the following disclaimer.
15
	
15

  
16 16
	2. Redistributions in binary form must reproduce the above copyright
17 17
	   notice, this list of conditions and the following disclaimer in the
18 18
	   documentation and/or other materials provided with the distribution.
19
	
19

  
20 20
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
21 21
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22 22
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
......
30 30
*/
31 31

  
32 32
/* tags that are always to be handled as lists */
33
$listtags = explode(" ", "rule user key subqueue dnsserver winsserver " .
33
$listtags = explode(" ", "item field rule user key subqueue dnsserver winsserver " .
34 34
	"encryption-algorithm-option hash-algorithm-option hosts tunnel onetoone " .
35 35
	"staticmap route alias pipe queue shellcmd earlyshellcmd mobilekey " .
36 36
	"servernat proxyarpnet passthrumac allowedip wolentry vlan");
37 37

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

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

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

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

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

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

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

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

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

  
73 73
	if ($havedata == $depth) {
74 74
		$ptr =& $config;
75 75
		foreach ($curpath as $path) {
......
77 77
		}
78 78
		$ptr = "";
79 79
	}
80
	
80

  
81 81
	array_pop($curpath);
82 82

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

  
86 86
	$depth--;
87 87
}
88 88

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

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

  
94 94
	if ($data != "") {
95 95
		$ptr =& $config;
96 96
		foreach ($curpath as $path) {
......
116 116
	$curpath = array();
117 117
	$depth = 0;
118 118
	$havedata = 0;
119
	
119

  
120 120
	$xml_parser = xml_parser_create();
121
	
121

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

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

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

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

  
142 142
	return $config[$rootobj];
143 143
}
144 144

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

  
147 147
	global $listtags;
148
	
148

  
149 149
	$xmlconfig = "";
150 150

  
151 151
	foreach ($arr as $ent => $val) {
......
186 186
			}
187 187
		}
188 188
	}
189
	
189

  
190 190
	return $xmlconfig;
191 191
}
192 192

  
......
194 194

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

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

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

  
202 202
	return $xmlconfig;
203 203
}
204 204

  

Also available in: Unified diff