Project

General

Profile

Download (8.44 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
  /* Please keep this list alpha sorted and no longer than 80 characters
40
   * I know it's a pain, but it's a pain to find stuff too if it's not
41
   */
42
	$ret = explode(" ",
43 6733f0f9 jim-p
                "alias aliasurl allowedip authserver bridged ca cacert cert crl clone config ".
44 e08e4ebc Ermal
                "container columnitem depends_on_package disk dnsserver dnsupdate ".
45
                "domainoverrides dyndns earlyshellcmd element encryption-algorithm-option ".
46
                "field fieldname hash-algorithm-option gateway_item gateway_group gif gre ".
47
                "group hosts member ifgroupentry igmpentry interface_array item key lagg " .
48
                "lbaction lbpool l7rules lbprotocol ".
49
                "member menu tab mobilekey monitor_type mount ntpserver onetoone ".
50
                "openvpn-server openvpn-client openvpn-csc " .
51 0e642c78 Ermal
                "option package passthrumac phase1 phase2 ppp pppoe priv proxyarpnet qinqentry queue ".
52 e08e4ebc Ermal
                "pages pipe roll route row rrddatafile rule schedule service servernat servers ".
53
                "serversdisabled earlyshellcmd shellcmd staticmap subqueue timerange ".
54 71f88d75 smos
                "tunnel user vip virtual_server vlan winsserver wolentry widget npt"
55 e08e4ebc Ermal
                );
56 26433cb8 Scott Ullrich
	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
        while ($parser->read()) {
69
                switch ($parser->nodeType) {
70
                case XMLReader::WHITESPACE:
71
                case XMLReader::SIGNIFICANT_WHITESPACE:
72
                        break;
73
                case XMLReader::ELEMENT:
74 ab83fce0 Ermal
                        if (isset($listtags[strtolower($parser->name)])) {
75
                        	if (!$parser->isEmptyElement)
76 26433cb8 Scott Ullrich
                                        add_elements($cfgarray[$parser->name][], $parser);
77 ab83fce0 Ermal
                        } else {
78
                        	if ($parser->isEmptyElement)
79
					$cfgarray[$parser->name] = "";
80
				else
81
                        		add_elements($cfgarray[$parser->name], $parser);
82
			}
83 26433cb8 Scott Ullrich
                        break;
84
                case XMLReader::TEXT:
85
		case XMLReader::CDATA:
86
                        $cfgarray = $parser->value;
87
                        break;
88
                case XMLReader::END_ELEMENT:
89 ab83fce0 Ermal
			return;
90
			break;
91 26433cb8 Scott Ullrich
                default:
92
                        break;
93
                }
94 ab83fce0 Ermal
	}
95 26433cb8 Scott Ullrich
}
96
97
function parse_xml_config($cffile, $rootobj, $isstring = "false") {
98
	global $listtags;
99
	$listtags = listtags();
100
        if (isset($GLOBALS['custom_listtags'])) {
101
          foreach($GLOBALS['custom_listtags'] as $tag) {
102
            $listtags[$tag] = $tag;
103
          }
104
        }
105 ab83fce0 Ermal
106 26433cb8 Scott Ullrich
	return parse_xml_config_raw($cffile, $rootobj);
107
}
108
109
function parse_xml_config_pkg($cffile, $rootobj, $isstring = "false") {
110
	global $listtags;
111
	$listtags = listtags_pkg();
112
        if (isset($GLOBALS['custom_listtags_pkg'])) {
113
          foreach($GLOBALS['custom_listtags_pkg'] as $tag) {
114
            $listtags[$tag] = $tag;
115
          }
116
        }
117
	return parse_xml_config_raw($cffile, $rootobj, $isstring);
118
}
119
120
function parse_xml_config_raw($cffile, $rootobj, $isstring = "false") {
121
122
	$parsedcfg = array();
123
124
	$par = new XMLReader();
125 ab83fce0 Ermal
	if ($par->open($cffile, "UTF-8", LIBXML_NOERROR | LIBXML_NOWARNING)) {
126 26433cb8 Scott Ullrich
		add_elements($parsedcfg, $par);
127
		$par->close();
128
	} else
129
		log_error("Error returned while trying to parse {$cffile}");
130
131 990d7c03 Erik Fonnesbeck
	if (!is_array($rootobj))
132
		$rootobj = array($rootobj);
133
	foreach ($rootobj as $rootobj_name)
134
		if ($parsedcfg[$rootobj_name])
135
			break;
136
137
	return $parsedcfg[$rootobj_name];
138 26433cb8 Scott Ullrich
}
139
140 ab83fce0 Ermal
function dump_xml_config_sub(& $writer, $arr) {
141
        global $listtags;
142 26433cb8 Scott Ullrich
143 ab83fce0 Ermal
        foreach ($arr as $ent => $val) {
144
                if (is_array($val)) {
145
                        /* is it just a list of multiple values? */
146
                        if (isset($listtags[strtolower($ent)])) {
147
                                foreach ($val as $cval) {
148
                                        if (is_array($cval)) {
149
                                                if (empty($cval))
150
                                                        $writer->writeElement($ent);
151
                                                else {
152
                                                        $writer->startElement($ent);
153
                                                        dump_xml_config_sub($writer, $cval);
154
                                                        $writer->endElement();
155
                                                }
156
                                        } else {
157
                                                if($cval === false) continue;
158
                                                if ((is_bool($val) && ($val == true)) || ($val === ""))
159
                                                        $writer->writeElement($ent);
160
                                                else if (!is_bool($val))
161
							$writer->writeElement($ent, $cval);
162
                                        }
163
                                }
164
                        } else if (empty($val)) {
165
                                $writer->writeElement($ent);
166
                        } else {
167
                                /* it's an array */
168
                                $writer->startElement($ent);
169
                                dump_xml_config_sub($writer, $val);
170
                                $writer->endElement();
171
                        }
172
                } else {
173
			if ((is_bool($val) && ($val == true)) || ($val === ""))
174
                                $writer->writeElement($ent);
175
                        else if (!is_bool($val))
176
                                $writer->writeElement($ent, $val);
177
                }
178
        }
179 26433cb8 Scott Ullrich
}
180
181
function dump_xml_config($arr, $rootobj) {
182
	global $listtags;
183
	$listtags = listtags();
184
        if (isset($GLOBALS['custom_listtags'])) {
185
          foreach($GLOBALS['custom_listtags'] as $tag) {
186
            $listtags[$tag] = $tag;
187
          }
188
        }
189
	return dump_xml_config_raw($arr, $rootobj);
190
}
191
192
function dump_xml_config_pkg($arr, $rootobj) {
193
	global $listtags;
194
	$listtags = listtags_pkg();
195
        if (isset($GLOBALS['custom_listtags_pkg'])) {
196
          foreach($GLOBALS['custom_listtags_pkg'] as $tag) {
197
            $listtags[$tag] = $tag;
198
          }
199
        }
200
	return dump_xml_config_raw($arr, $rootobj);
201
}
202
203
function dump_xml_config_raw($arr, $rootobj) {
204
205 ab83fce0 Ermal
        $writer = new XMLWriter();
206
        $writer->openMemory();
207
        $writer->setIndent(true);
208
        $writer->setIndentString("\t");
209
        $writer->startDocument("1.0", "UTF-8");
210
        $writer->startElement($rootobj);
211 26433cb8 Scott Ullrich
212 ab83fce0 Ermal
        dump_xml_config_sub($writer, $arr);
213 26433cb8 Scott Ullrich
214 ab83fce0 Ermal
        $writer->endElement();
215
        $writer->endDocument();
216
        $xmlconfig = $writer->outputMemory(true);
217
        return $xmlconfig;
218 26433cb8 Scott Ullrich
}
219
220 71f88d75 smos
?>