Project

General

Profile

Download (7.67 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * xmlreader.inc
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2013 BSD Perimeter
7
 * Copyright (c) 2013-2016 Electric Sheep Fencing
8
 * Copyright (c) 2014-2022 Rubicon Communications, LLC (Netgate)
9
 * All rights reserved.
10
 *
11
 * originally part of m0n0wall (http://m0n0.ch/wall)
12
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
13
 * All rights reserved.
14
 *
15
 * Licensed under the Apache License, Version 2.0 (the "License");
16
 * you may not use this file except in compliance with the License.
17
 * You may obtain a copy of the License at
18
 *
19
 * http://www.apache.org/licenses/LICENSE-2.0
20
 *
21
 * Unless required by applicable law or agreed to in writing, software
22
 * distributed under the License is distributed on an "AS IS" BASIS,
23
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
 * See the License for the specific language governing permissions and
25
 * limitations under the License.
26
 */
27

    
28
/* The following items will be treated as arrays in config.xml */
29
function listtags() {
30
	/*
31
	 * Please keep this list alpha sorted and no longer than 80 characters
32
	 * I know it's a pain, but it's a pain to find stuff too if it's not
33
	 */
34
	$ret = array(
35
		'acls', 'alias', 'aliasurl', 'allowedip', 'allowedhostname', 'authserver',
36
		'bridged', 'build_port_path',
37
		'ca', 'cacert', 'cert', 'crl', 'clone', 'config',
38
	       	'container', 'columnitem', 'checkipservice',
39
		'depends_on_package', 'disk', 'dnsserver', 'dnsupdate', 'domainoverrides', 'dyndns',
40
		'earlyshellcmd', 'element', 'encryption-algorithm-option',
41
		'field', 'fieldname',
42
		'gateway_item', 'gateway_group', 'gif', 'gre', 'group',
43
		'hash-algorithm-option', 'hosts', 'ifgroupentry', 'igmpentry', 'interface_array', 'item', 'key',
44
		'lagg', 'lbaction', 'lbpool', 'l7rules', 'lbprotocol',
45
		'member', 'menu', 'tab', 'mobilekey', 'monitor_type', 'mount',
46
		'npt', 'ntpserver',
47
		'onetoone', 'openvpn-server', 'openvpn-client', 'openvpn-csc', 'option',
48
		'package', 'passthrumac', 'phase1', 'phase2', 'ppp', 'pppoe', 'priv', 'proxyarpnet', 'pool',
49
		'qinqentry', 'queue',
50
		'pages', 'pipe', 'radnsserver', 'roll', 'route', 'row', 'rrddatafile', 'rule',
51
		'schedule', 'service', 'servernat', 'servers',
52
		'serversdisabled', 'shellcmd', 'staticmap', 'subqueue', 'switch', 'swport',
53
		'timerange', 'tunnel', 'user', 'vip', 'virtual_server', 'vlan', 'vlangroup', 'voucherdbfile',
54
		'vxlan', 'winsserver', 'wolentry', 'widget', 'xmldatafile'
55
	);
56
	return array_flip($ret);
57
}
58

    
59
/* Package XML tags that should be treat as a list not as a traditional array */
60
function listtags_pkg() {
61
	$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');
62

    
63
	return array_flip($ret);
64
}
65

    
66
function add_elements(&$cfgarray, &$parser) {
67
	global $listtags;
68

    
69
	while ($parser->read()) {
70
		switch ($parser->nodeType) {
71
			case XMLReader::WHITESPACE:
72
			case XMLReader::SIGNIFICANT_WHITESPACE:
73
				break;
74
			case XMLReader::ELEMENT:
75
				if (isset($listtags[strtolower($parser->name)])) {
76
					$cfgref =& $cfgarray[$parser->name][count($cfgarray[$parser->name])];
77
					if (!$parser->isEmptyElement) {
78
						add_elements($cfgref, $parser);
79
					} else {
80
						$cfgref = array();
81
					}
82
				} else {
83
					if (isset($cfgarray[$parser->name]) && (!is_array($cfgarray[$parser->name]) || !isset($cfgarray[$parser->name][0]))) {
84
						$nodebkp = $cfgarray[$parser->name];
85
						$cfgarray[$parser->name] = array();
86
						$cfgarray[$parser->name][] = $nodebkp;
87
						$cfgref =& $cfgarray[$parser->name][0];
88
						unset($nodebkp);
89
					} else {
90
						$cfgref =& $cfgarray[$parser->name];
91
					}
92

    
93
					if ($parser->isEmptyElement) {
94
						if (is_array($cfgref)) {
95
							$cfgref[] = array();
96
						} else {
97
							$cfgref = "";
98
						}
99
					} else {
100
						if (is_array($cfgref)) {
101
							$cfgref =& $cfgarray[$parser->name][count($cfgarray[$parser->name])];
102
							add_elements($cfgref, $parser);
103
						} else {
104
							add_elements($cfgref, $parser);
105
						}
106
					}
107
				}
108

    
109
				$i = 0;
110
				while ($parser->moveToAttributeNo($i)) {
111
					$cfgref[$parser->name] = $parser->value;
112
					$i++;
113
				}
114
				break;
115
			case XMLReader::TEXT:
116
			case XMLReader::CDATA:
117
				$cfgarray = $parser->value;
118
				break;
119
			case XMLReader::END_ELEMENT:
120
				return;
121
				break;
122
			default:
123
				break;
124
		}
125
	}
126
}
127

    
128
function parse_xml_config($cffile, $rootobj, $isstring = "false") {
129
	global $listtags;
130

    
131
	$listtags = listtags();
132
	if (isset($GLOBALS['custom_listtags'])) {
133
		foreach ($GLOBALS['custom_listtags'] as $tag) {
134
			$listtags[$tag] = $tag;
135
		}
136
	}
137

    
138
	return parse_xml_config_raw($cffile, $rootobj);
139
}
140

    
141
function parse_xml_config_pkg($cffile, $rootobj, $isstring = "false") {
142
	global $listtags;
143

    
144
	$listtags = listtags_pkg();
145
	if (isset($GLOBALS['custom_listtags_pkg'])) {
146
		foreach ($GLOBALS['custom_listtags_pkg'] as $tag) {
147
			$listtags[$tag] = $tag;
148
		}
149
	}
150
	return parse_xml_config_raw($cffile, $rootobj, $isstring);
151
}
152

    
153
function parse_xml_config_raw($cffile, $rootobj, $isstring = "false") {
154
	global $listtags;
155

    
156
	$parsedcfg = array();
157

    
158
	$par = new XMLReader();
159
	if ($par->open($cffile, "UTF-8", LIBXML_NOERROR | LIBXML_NOWARNING)) {
160
		add_elements($parsedcfg, $par);
161
		$par->close();
162
	} else {
163
		log_error(sprintf(gettext("Error returned while trying to parse %s"), $cffile));
164
	}
165

    
166
	if ($rootobj) {
167
		if (!is_array($rootobj)) {
168
			$rootobj = array($rootobj);
169
		}
170
		foreach ($rootobj as $rootobj_name) {
171
			if ($parsedcfg[$rootobj_name]) {
172
				break;
173
			}
174
		}
175

    
176
		return $parsedcfg[$rootobj_name];
177
	} else {
178
		return $parsedcfg;
179
	}
180
}
181

    
182
function dump_xml_config_sub(& $writer, $arr) {
183
	global $listtags;
184

    
185
	foreach ($arr as $ent => $val) {
186
		if (is_array($val)) {
187
			/* is it just a list of multiple values? */
188
			if (isset($listtags[strtolower($ent)])) {
189
				foreach ($val as $cval) {
190
					if (is_array($cval)) {
191
						if (empty($cval)) {
192
							$writer->startElement($ent);
193
							$writer->endElement();
194
						} else {
195
							$writer->startElement($ent);
196
							dump_xml_config_sub($writer, $cval);
197
							$writer->endElement();
198
						}
199
					} else {
200
						if ($cval === false) {
201
							continue;
202
						}
203
						if ((is_bool($val) && ($val == true)) || ($val === "")) {
204
							$writer->startElement($ent);
205
							$writer->endElement();
206
						} else if (!is_bool($val)) {
207
							$writer->writeElement($ent, $cval);
208
						}
209
					}
210
				}
211
			} else if (empty($val)) {
212
				$writer->startElement($ent);
213
				$writer->endElement();
214
			} else {
215
				/* it's an array */
216
				$writer->startElement($ent);
217
				dump_xml_config_sub($writer, $val);
218
				$writer->endElement();
219
			}
220
		} else {
221
			if ((is_bool($val) && ($val == true)) || ($val === "")) {
222
				$writer->startElement($ent);
223
				$writer->endElement();
224
			} else if (!is_bool($val)) {
225
				$writer->writeElement($ent, $val);
226
			}
227
		}
228
	}
229
}
230

    
231
function dump_xml_config($arr, $rootobj) {
232
	global $listtags;
233

    
234
	$listtags = listtags();
235
	if (isset($GLOBALS['custom_listtags'])) {
236
		foreach ($GLOBALS['custom_listtags'] as $tag) {
237
			$listtags[$tag] = $tag;
238
		}
239
	}
240

    
241

    
242
	return dump_xml_config_raw($arr, $rootobj);
243
}
244

    
245
function dump_xml_config_pkg($arr, $rootobj) {
246
	global $listtags;
247

    
248
	$listtags = listtags_pkg();
249
	if (isset($GLOBALS['custom_listtags_pkg'])) {
250
		foreach ($GLOBALS['custom_listtags_pkg'] as $tag) {
251
			$listtags[$tag] = $tag;
252
		}
253
	}
254
	return dump_xml_config_raw($arr, $rootobj);
255
}
256

    
257
function dump_xml_config_raw($arr, $rootobj) {
258

    
259
	$writer = new XMLWriter();
260
	$writer->openMemory();
261
	$writer->setIndent(true);
262
	$writer->setIndentString("\t");
263
	$writer->startDocument("1.0", "UTF-8");
264
	$writer->startElement($rootobj);
265

    
266
	dump_xml_config_sub($writer, $arr);
267

    
268
	$writer->endElement();
269
	$writer->endDocument();
270
	$xmlconfig = $writer->outputMemory(true);
271

    
272
	return $xmlconfig;
273
}
274

    
275
?>
(60-60/61)