Project

General

Profile

Download (7.54 KB) Statistics
| Branch: | Tag: | Revision:
1 26433cb8 Scott Ullrich
<?php
2
/*
3 ac24dc24 Renato Botelho
 * xmlreader.inc
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6 b8f91b7c Luiz Souza
 * Copyright (c) 2004-2018 Rubicon Communications, LLC (Netgate)
7 ac24dc24 Renato Botelho
 * All rights reserved.
8
 *
9
 * originally part of m0n0wall (http://m0n0.ch/wall)
10 c5d81585 Renato Botelho
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
11 ac24dc24 Renato Botelho
 * All rights reserved.
12
 *
13 b12ea3fb Renato Botelho
 * Licensed under the Apache License, Version 2.0 (the "License");
14
 * you may not use this file except in compliance with the License.
15
 * You may obtain a copy of the License at
16 ac24dc24 Renato Botelho
 *
17 b12ea3fb Renato Botelho
 * http://www.apache.org/licenses/LICENSE-2.0
18 ac24dc24 Renato Botelho
 *
19 b12ea3fb Renato Botelho
 * Unless required by applicable law or agreed to in writing, software
20
 * distributed under the License is distributed on an "AS IS" BASIS,
21
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22
 * See the License for the specific language governing permissions and
23
 * limitations under the License.
24 ac24dc24 Renato Botelho
 */
25 26433cb8 Scott Ullrich
26
/* The following items will be treated as arrays in config.xml */
27
function listtags() {
28 72a8c829 Ermal
	/*
29 79262830 Phil Davis
	 * Please keep this list alpha sorted and no longer than 80 characters
30
	 * I know it's a pain, but it's a pain to find stuff too if it's not
31
	 */
32 b79ea46a Ermal
	$ret = array(
33 72a8c829 Ermal
		'acls', 'alias', 'aliasurl', 'allowedip', 'allowedhostname', 'authserver',
34
		'bridged', 'build_port_path',
35
		'ca', 'cacert', 'cert', 'crl', 'clone', 'config', 'container', 'columnitem',
36 39d2f39d NOYB
		'checkipservice',
37 72a8c829 Ermal
		'depends_on_package', 'disk', 'dnsserver', 'dnsupdate', 'domainoverrides', 'dyndns',
38
		'earlyshellcmd', 'element', 'encryption-algorithm-option',
39
		'field', 'fieldname',
40
		'gateway_item', 'gateway_group', 'gif', 'gre', 'group',
41 40d7e4be NOYB
		'hash-algorithm-option', 'hosts', 'ifgroupentry', 'igmpentry', 'interface_array', 'item', 'key',
42 72a8c829 Ermal
		'lagg', 'lbaction', 'lbpool', 'l7rules', 'lbprotocol',
43
		'member', 'menu', 'tab', 'mobilekey', 'monitor_type', 'mount',
44
		'npt', 'ntpserver',
45
		'onetoone', 'openvpn-server', 'openvpn-client', 'openvpn-csc', 'option',
46
		'package', 'passthrumac', 'phase1', 'phase2', 'ppp', 'pppoe', 'priv', 'proxyarpnet', 'pool',
47
		'qinqentry', 'queue',
48
		'pages', 'pipe', 'radnsserver', 'roll', 'route', 'row', 'rrddatafile', 'rule',
49
		'schedule', 'service', 'servernat', 'servers',
50 1328b154 Luiz Souza
		'serversdisabled', 'shellcmd', 'staticmap', 'subqueue', 'switch', 'swport',
51
		'timerange', 'tunnel', 'user', 'vip', 'virtual_server', 'vlan', 'vlangroup',
52 72a8c829 Ermal
		'winsserver', 'wolentry', 'widget'
53
	);
54 26433cb8 Scott Ullrich
	return array_flip($ret);
55
}
56
57
/* Package XML tags that should be treat as a list not as a traditional array */
58
function listtags_pkg() {
59 b79ea46a Ermal
	$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');
60 26433cb8 Scott Ullrich
61
	return array_flip($ret);
62
}
63
64
function add_elements(&$cfgarray, &$parser) {
65 79262830 Phil Davis
	global $listtags;
66
67
	while ($parser->read()) {
68
		switch ($parser->nodeType) {
69
			case XMLReader::WHITESPACE:
70
			case XMLReader::SIGNIFICANT_WHITESPACE:
71
				break;
72
			case XMLReader::ELEMENT:
73
				if (isset($listtags[strtolower($parser->name)])) {
74
					$cfgref =& $cfgarray[$parser->name][count($cfgarray[$parser->name])];
75
					if (!$parser->isEmptyElement) {
76 ddfe5e43 Ermal
						add_elements($cfgref, $parser);
77 79262830 Phil Davis
					} else {
78
						$cfgref = array();
79
					}
80
				} else {
81
					if (isset($cfgarray[$parser->name]) && (!is_array($cfgarray[$parser->name]) || !isset($cfgarray[$parser->name][0]))) {
82
						$nodebkp = $cfgarray[$parser->name];
83
						$cfgarray[$parser->name] = array();
84
						$cfgarray[$parser->name][] = $nodebkp;
85
						$cfgref =& $cfgarray[$parser->name][0];
86
						unset($nodebkp);
87
					} else {
88
						$cfgref =& $cfgarray[$parser->name];
89
					}
90
91
					if ($parser->isEmptyElement) {
92
						if (is_array($cfgref)) {
93
							$cfgref[] = array();
94
						} else {
95
							$cfgref = "";
96
						}
97
					} else {
98
						if (is_array($cfgref)) {
99
							$cfgref =& $cfgarray[$parser->name][count($cfgarray[$parser->name])];
100
							add_elements($cfgref, $parser);
101
						} else {
102
							add_elements($cfgref, $parser);
103
						}
104
					}
105 ddfe5e43 Ermal
				}
106 8ffb5ccd Ermal
107 79262830 Phil Davis
				$i = 0;
108
				while ($parser->moveToAttributeNo($i)) {
109
					$cfgref[$parser->name] = $parser->value;
110
					$i++;
111
				}
112
				break;
113
			case XMLReader::TEXT:
114
			case XMLReader::CDATA:
115
				$cfgarray = $parser->value;
116
				break;
117
			case XMLReader::END_ELEMENT:
118
				return;
119
				break;
120
			default:
121
				break;
122
		}
123 ab83fce0 Ermal
	}
124 26433cb8 Scott Ullrich
}
125
126
function parse_xml_config($cffile, $rootobj, $isstring = "false") {
127
	global $listtags;
128 b79ea46a Ermal
129 26433cb8 Scott Ullrich
	$listtags = listtags();
130 79262830 Phil Davis
	if (isset($GLOBALS['custom_listtags'])) {
131
		foreach ($GLOBALS['custom_listtags'] as $tag) {
132
			$listtags[$tag] = $tag;
133
		}
134
	}
135 ab83fce0 Ermal
136 26433cb8 Scott Ullrich
	return parse_xml_config_raw($cffile, $rootobj);
137
}
138
139
function parse_xml_config_pkg($cffile, $rootobj, $isstring = "false") {
140
	global $listtags;
141 b79ea46a Ermal
142 26433cb8 Scott Ullrich
	$listtags = listtags_pkg();
143 79262830 Phil Davis
	if (isset($GLOBALS['custom_listtags_pkg'])) {
144
		foreach ($GLOBALS['custom_listtags_pkg'] as $tag) {
145
			$listtags[$tag] = $tag;
146
		}
147
	}
148 26433cb8 Scott Ullrich
	return parse_xml_config_raw($cffile, $rootobj, $isstring);
149
}
150
151
function parse_xml_config_raw($cffile, $rootobj, $isstring = "false") {
152 b79ea46a Ermal
	global $listtags;
153 26433cb8 Scott Ullrich
154
	$parsedcfg = array();
155
156
	$par = new XMLReader();
157 ab83fce0 Ermal
	if ($par->open($cffile, "UTF-8", LIBXML_NOERROR | LIBXML_NOWARNING)) {
158 26433cb8 Scott Ullrich
		add_elements($parsedcfg, $par);
159
		$par->close();
160 79262830 Phil Davis
	} else {
161 5cfa35df Renato Botelho
		log_error(sprintf(gettext("Error returned while trying to parse %s"), $cffile));
162 79262830 Phil Davis
	}
163 26433cb8 Scott Ullrich
164 428c289f Darren Embry
	if ($rootobj) {
165 79262830 Phil Davis
		if (!is_array($rootobj)) {
166 428c289f Darren Embry
			$rootobj = array($rootobj);
167 79262830 Phil Davis
		}
168
		foreach ($rootobj as $rootobj_name) {
169
			if ($parsedcfg[$rootobj_name]) {
170 428c289f Darren Embry
				break;
171 79262830 Phil Davis
			}
172
		}
173
174 428c289f Darren Embry
		return $parsedcfg[$rootobj_name];
175
	} else {
176
		return $parsedcfg;
177
	}
178 26433cb8 Scott Ullrich
}
179
180 ab83fce0 Ermal
function dump_xml_config_sub(& $writer, $arr) {
181 79262830 Phil Davis
	global $listtags;
182
183
	foreach ($arr as $ent => $val) {
184
		if (is_array($val)) {
185
			/* is it just a list of multiple values? */
186
			if (isset($listtags[strtolower($ent)])) {
187
				foreach ($val as $cval) {
188
					if (is_array($cval)) {
189
						if (empty($cval)) {
190 da7054b7 Steve Beaver
							$writer->startElement($ent);
191
							$writer->endElement();
192 79262830 Phil Davis
						} else {
193
							$writer->startElement($ent);
194
							dump_xml_config_sub($writer, $cval);
195
							$writer->endElement();
196
						}
197
					} else {
198
						if ($cval === false) {
199
							continue;
200
						}
201
						if ((is_bool($val) && ($val == true)) || ($val === "")) {
202 da7054b7 Steve Beaver
							$writer->startElement($ent);
203
							$writer->endElement();
204 79262830 Phil Davis
						} else if (!is_bool($val)) {
205 ab83fce0 Ermal
							$writer->writeElement($ent, $cval);
206 79262830 Phil Davis
						}
207
					}
208
				}
209
			} else if (empty($val)) {
210 da7054b7 Steve Beaver
				$writer->startElement($ent);
211
				$writer->endElement();
212 79262830 Phil Davis
			} else {
213
				/* it's an array */
214
				$writer->startElement($ent);
215
				dump_xml_config_sub($writer, $val);
216
				$writer->endElement();
217
			}
218
		} else {
219
			if ((is_bool($val) && ($val == true)) || ($val === "")) {
220 da7054b7 Steve Beaver
				$writer->startElement($ent);
221
				$writer->endElement();
222 79262830 Phil Davis
			} else if (!is_bool($val)) {
223
				$writer->writeElement($ent, $val);
224
			}
225
		}
226
	}
227 26433cb8 Scott Ullrich
}
228
229
function dump_xml_config($arr, $rootobj) {
230
	global $listtags;
231 b79ea46a Ermal
232 26433cb8 Scott Ullrich
	$listtags = listtags();
233 79262830 Phil Davis
	if (isset($GLOBALS['custom_listtags'])) {
234
		foreach ($GLOBALS['custom_listtags'] as $tag) {
235
			$listtags[$tag] = $tag;
236
		}
237
	}
238 da7054b7 Steve Beaver
239
240 26433cb8 Scott Ullrich
	return dump_xml_config_raw($arr, $rootobj);
241
}
242
243
function dump_xml_config_pkg($arr, $rootobj) {
244
	global $listtags;
245 b79ea46a Ermal
246 26433cb8 Scott Ullrich
	$listtags = listtags_pkg();
247 79262830 Phil Davis
	if (isset($GLOBALS['custom_listtags_pkg'])) {
248
		foreach ($GLOBALS['custom_listtags_pkg'] as $tag) {
249
			$listtags[$tag] = $tag;
250
		}
251
	}
252 26433cb8 Scott Ullrich
	return dump_xml_config_raw($arr, $rootobj);
253
}
254
255
function dump_xml_config_raw($arr, $rootobj) {
256
257 79262830 Phil Davis
	$writer = new XMLWriter();
258
	$writer->openMemory();
259
	$writer->setIndent(true);
260
	$writer->setIndentString("\t");
261
	$writer->startDocument("1.0", "UTF-8");
262
	$writer->startElement($rootobj);
263 26433cb8 Scott Ullrich
264 79262830 Phil Davis
	dump_xml_config_sub($writer, $arr);
265 26433cb8 Scott Ullrich
266 79262830 Phil Davis
	$writer->endElement();
267
	$writer->endDocument();
268
	$xmlconfig = $writer->outputMemory(true);
269 b79ea46a Ermal
270 79262830 Phil Davis
	return $xmlconfig;
271 26433cb8 Scott Ullrich
}
272
273
?>