Project

General

Profile

Download (9.41 KB) Statistics
| Branch: | Tag: | Revision:
1 26433cb8 Scott Ullrich
<?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
/*
34
	pfSense_MODULE:	utils
35
*/
36
37
/* The following items will be treated as arrays in config.xml */
38
function listtags() {
39 72a8c829 Ermal
	/*
40
         * Please keep this list alpha sorted and no longer than 80 characters
41
         * I know it's a pain, but it's a pain to find stuff too if it's not
42
         */
43 b79ea46a Ermal
	$ret = array(
44 72a8c829 Ermal
		'acls', 'alias', 'aliasurl', 'allowedip', 'allowedhostname', 'authserver',
45
		'bridged', 'build_port_path',
46
		'ca', 'cacert', 'cert', 'crl', 'clone', 'config', 'container', 'columnitem',
47
		'depends_on_package', 'disk', 'dnsserver', 'dnsupdate', 'domainoverrides', 'dyndns',
48
		'earlyshellcmd', 'element', 'encryption-algorithm-option',
49
		'field', 'fieldname',
50
		'gateway_item', 'gateway_group', 'gif', 'gre', 'group',
51
		'hash-algorithm-option', 'hosts', 'member', 'ifgroupentry', 'igmpentry', 'interface_array', 'item', 'key',
52
		'lagg', 'lbaction', 'lbpool', 'l7rules', 'lbprotocol',
53
		'member', 'menu', 'tab', 'mobilekey', 'monitor_type', 'mount',
54
		'npt', 'ntpserver',
55
		'onetoone', 'openvpn-server', 'openvpn-client', 'openvpn-csc', 'option',
56
		'package', 'passthrumac', 'phase1', 'phase2', 'ppp', 'pppoe', 'priv', 'proxyarpnet', 'pool',
57
		'qinqentry', 'queue',
58
		'pages', 'pipe', 'radnsserver', 'roll', 'route', 'row', 'rrddatafile', 'rule',
59
		'schedule', 'service', 'servernat', 'servers',
60
		'serversdisabled', 'shellcmd', 'staticmap', 'subqueue',
61
		'timerange', 'tunnel', 'user', 'vip', 'virtual_server', 'vlan',
62
		'winsserver', 'wolentry', 'widget'
63
	);
64 26433cb8 Scott Ullrich
	return array_flip($ret);
65
}
66
67
/* Package XML tags that should be treat as a list not as a traditional array */
68
function listtags_pkg() {
69 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');
70 26433cb8 Scott Ullrich
71
	return array_flip($ret);
72
}
73
74
function add_elements(&$cfgarray, &$parser) {
75
        global $listtags;
76 b79ea46a Ermal
77 26433cb8 Scott Ullrich
        while ($parser->read()) {
78
                switch ($parser->nodeType) {
79
                case XMLReader::WHITESPACE:
80
                case XMLReader::SIGNIFICANT_WHITESPACE:
81
                        break;
82
                case XMLReader::ELEMENT:
83 ab83fce0 Ermal
                        if (isset($listtags[strtolower($parser->name)])) {
84 ddfe5e43 Ermal
				$cfgref =& $cfgarray[$parser->name][count($cfgarray[$parser->name])];
85
                        	if (!$parser->isEmptyElement) {
86
                                        add_elements($cfgref, $parser);
87
				} else
88
					$cfgref = array();
89
					
90 ab83fce0 Ermal
                        } else {
91 ddfe5e43 Ermal
				if (isset($cfgarray[$parser->name]) && (!is_array($cfgarray[$parser->name]) || !isset($cfgarray[$parser->name][0]))) {
92
					$nodebkp = $cfgarray[$parser->name];
93
					$cfgarray[$parser->name] = array();
94
					$cfgarray[$parser->name][] = $nodebkp;
95
					$cfgref =& $cfgarray[$parser->name][0];
96
					unset($nodebkp);
97
				} else
98
					$cfgref =& $cfgarray[$parser->name];
99
100
                        	if ($parser->isEmptyElement) {
101
					if (is_array($cfgref))
102
						$cfgref[] = array();
103
					else
104
						$cfgref = "";
105
				} else {
106
					if (is_array($cfgref)) {
107
						$cfgref =& $cfgarray[$parser->name][count($cfgarray[$parser->name])];
108
						add_elements($cfgref, $parser);
109
					} else
110
						add_elements($cfgref, $parser);
111
				}
112 ab83fce0 Ermal
			}
113 8ffb5ccd Ermal
114
			$i = 0;
115
			while ($parser->moveToAttributeNo($i)) {
116 ddfe5e43 Ermal
				$cfgref[$parser->name] = $parser->value;
117 8ffb5ccd Ermal
				$i++;
118
			}
119 26433cb8 Scott Ullrich
                        break;
120
                case XMLReader::TEXT:
121
		case XMLReader::CDATA:
122
                        $cfgarray = $parser->value;
123
                        break;
124
                case XMLReader::END_ELEMENT:
125 ab83fce0 Ermal
			return;
126
			break;
127 26433cb8 Scott Ullrich
                default:
128
                        break;
129
                }
130 ab83fce0 Ermal
	}
131 26433cb8 Scott Ullrich
}
132
133
function parse_xml_config($cffile, $rootobj, $isstring = "false") {
134
	global $listtags;
135 b79ea46a Ermal
136 26433cb8 Scott Ullrich
	$listtags = listtags();
137
        if (isset($GLOBALS['custom_listtags'])) {
138
          foreach($GLOBALS['custom_listtags'] as $tag) {
139
            $listtags[$tag] = $tag;
140
          }
141
        }
142 ab83fce0 Ermal
143 26433cb8 Scott Ullrich
	return parse_xml_config_raw($cffile, $rootobj);
144
}
145
146
function parse_xml_config_pkg($cffile, $rootobj, $isstring = "false") {
147
	global $listtags;
148 b79ea46a Ermal
149 26433cb8 Scott Ullrich
	$listtags = listtags_pkg();
150
        if (isset($GLOBALS['custom_listtags_pkg'])) {
151
          foreach($GLOBALS['custom_listtags_pkg'] as $tag) {
152
            $listtags[$tag] = $tag;
153
          }
154
        }
155
	return parse_xml_config_raw($cffile, $rootobj, $isstring);
156
}
157
158
function parse_xml_config_raw($cffile, $rootobj, $isstring = "false") {
159 b79ea46a Ermal
	global $listtags;
160 26433cb8 Scott Ullrich
161
	$parsedcfg = array();
162
163
	$par = new XMLReader();
164 ab83fce0 Ermal
	if ($par->open($cffile, "UTF-8", LIBXML_NOERROR | LIBXML_NOWARNING)) {
165 26433cb8 Scott Ullrich
		add_elements($parsedcfg, $par);
166
		$par->close();
167
	} else
168 5cfa35df Renato Botelho
		log_error(sprintf(gettext("Error returned while trying to parse %s"), $cffile));
169 26433cb8 Scott Ullrich
170 428c289f Darren Embry
	if ($rootobj) {
171
		if (!is_array($rootobj))
172
			$rootobj = array($rootobj);
173
		foreach ($rootobj as $rootobj_name)
174
			if ($parsedcfg[$rootobj_name])
175
				break;
176
		
177
		return $parsedcfg[$rootobj_name];
178
	} else {
179
		return $parsedcfg;
180
	}
181 26433cb8 Scott Ullrich
}
182
183 ab83fce0 Ermal
function dump_xml_config_sub(& $writer, $arr) {
184
        global $listtags;
185 26433cb8 Scott Ullrich
186 ab83fce0 Ermal
        foreach ($arr as $ent => $val) {
187
                if (is_array($val)) {
188
                        /* is it just a list of multiple values? */
189
                        if (isset($listtags[strtolower($ent)])) {
190
                                foreach ($val as $cval) {
191
                                        if (is_array($cval)) {
192
                                                if (empty($cval))
193
                                                        $writer->writeElement($ent);
194
                                                else {
195
                                                        $writer->startElement($ent);
196
                                                        dump_xml_config_sub($writer, $cval);
197
                                                        $writer->endElement();
198
                                                }
199
                                        } else {
200
                                                if($cval === false) continue;
201
                                                if ((is_bool($val) && ($val == true)) || ($val === ""))
202
                                                        $writer->writeElement($ent);
203
                                                else if (!is_bool($val))
204
							$writer->writeElement($ent, $cval);
205
                                        }
206
                                }
207
                        } else if (empty($val)) {
208
                                $writer->writeElement($ent);
209
                        } else {
210
                                /* it's an array */
211
                                $writer->startElement($ent);
212
                                dump_xml_config_sub($writer, $val);
213
                                $writer->endElement();
214
                        }
215
                } else {
216
			if ((is_bool($val) && ($val == true)) || ($val === ""))
217
                                $writer->writeElement($ent);
218
                        else if (!is_bool($val))
219
                                $writer->writeElement($ent, $val);
220
                }
221
        }
222 26433cb8 Scott Ullrich
}
223
224
function dump_xml_config($arr, $rootobj) {
225
	global $listtags;
226 b79ea46a Ermal
227 26433cb8 Scott Ullrich
	$listtags = listtags();
228
        if (isset($GLOBALS['custom_listtags'])) {
229
          foreach($GLOBALS['custom_listtags'] as $tag) {
230
            $listtags[$tag] = $tag;
231
          }
232
        }
233
	return dump_xml_config_raw($arr, $rootobj);
234
}
235
236
function dump_xml_config_pkg($arr, $rootobj) {
237
	global $listtags;
238 b79ea46a Ermal
239 26433cb8 Scott Ullrich
	$listtags = listtags_pkg();
240
        if (isset($GLOBALS['custom_listtags_pkg'])) {
241
          foreach($GLOBALS['custom_listtags_pkg'] as $tag) {
242
            $listtags[$tag] = $tag;
243
          }
244
        }
245
	return dump_xml_config_raw($arr, $rootobj);
246
}
247
248
function dump_xml_config_raw($arr, $rootobj) {
249
250 ab83fce0 Ermal
        $writer = new XMLWriter();
251
        $writer->openMemory();
252
        $writer->setIndent(true);
253
        $writer->setIndentString("\t");
254
        $writer->startDocument("1.0", "UTF-8");
255
        $writer->startElement($rootobj);
256 26433cb8 Scott Ullrich
257 ab83fce0 Ermal
        dump_xml_config_sub($writer, $arr);
258 26433cb8 Scott Ullrich
259 ab83fce0 Ermal
        $writer->endElement();
260
        $writer->endDocument();
261
        $xmlconfig = $writer->outputMemory(true);
262 b79ea46a Ermal
263 ab83fce0 Ermal
        return $xmlconfig;
264 26433cb8 Scott Ullrich
}
265
266
?>