Project

General

Profile

Download (8.52 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 5cfa35df Renato Botelho
		log_error(sprintf(gettext("Error returned while trying to parse %s"), $cffile));
130 26433cb8 Scott Ullrich
131 428c289f Darren Embry
	if ($rootobj) {
132
		if (!is_array($rootobj))
133
			$rootobj = array($rootobj);
134
		foreach ($rootobj as $rootobj_name)
135
			if ($parsedcfg[$rootobj_name])
136
				break;
137
		
138
		return $parsedcfg[$rootobj_name];
139
	} else {
140
		return $parsedcfg;
141
	}
142 26433cb8 Scott Ullrich
}
143
144 ab83fce0 Ermal
function dump_xml_config_sub(& $writer, $arr) {
145
        global $listtags;
146 26433cb8 Scott Ullrich
147 ab83fce0 Ermal
        foreach ($arr as $ent => $val) {
148
                if (is_array($val)) {
149
                        /* is it just a list of multiple values? */
150
                        if (isset($listtags[strtolower($ent)])) {
151
                                foreach ($val as $cval) {
152
                                        if (is_array($cval)) {
153
                                                if (empty($cval))
154
                                                        $writer->writeElement($ent);
155
                                                else {
156
                                                        $writer->startElement($ent);
157
                                                        dump_xml_config_sub($writer, $cval);
158
                                                        $writer->endElement();
159
                                                }
160
                                        } else {
161
                                                if($cval === false) continue;
162
                                                if ((is_bool($val) && ($val == true)) || ($val === ""))
163
                                                        $writer->writeElement($ent);
164
                                                else if (!is_bool($val))
165
							$writer->writeElement($ent, $cval);
166
                                        }
167
                                }
168
                        } else if (empty($val)) {
169
                                $writer->writeElement($ent);
170
                        } else {
171
                                /* it's an array */
172
                                $writer->startElement($ent);
173
                                dump_xml_config_sub($writer, $val);
174
                                $writer->endElement();
175
                        }
176
                } else {
177
			if ((is_bool($val) && ($val == true)) || ($val === ""))
178
                                $writer->writeElement($ent);
179
                        else if (!is_bool($val))
180
                                $writer->writeElement($ent, $val);
181
                }
182
        }
183 26433cb8 Scott Ullrich
}
184
185
function dump_xml_config($arr, $rootobj) {
186
	global $listtags;
187
	$listtags = listtags();
188
        if (isset($GLOBALS['custom_listtags'])) {
189
          foreach($GLOBALS['custom_listtags'] as $tag) {
190
            $listtags[$tag] = $tag;
191
          }
192
        }
193
	return dump_xml_config_raw($arr, $rootobj);
194
}
195
196
function dump_xml_config_pkg($arr, $rootobj) {
197
	global $listtags;
198
	$listtags = listtags_pkg();
199
        if (isset($GLOBALS['custom_listtags_pkg'])) {
200
          foreach($GLOBALS['custom_listtags_pkg'] as $tag) {
201
            $listtags[$tag] = $tag;
202
          }
203
        }
204
	return dump_xml_config_raw($arr, $rootobj);
205
}
206
207
function dump_xml_config_raw($arr, $rootobj) {
208
209 ab83fce0 Ermal
        $writer = new XMLWriter();
210
        $writer->openMemory();
211
        $writer->setIndent(true);
212
        $writer->setIndentString("\t");
213
        $writer->startDocument("1.0", "UTF-8");
214
        $writer->startElement($rootobj);
215 26433cb8 Scott Ullrich
216 ab83fce0 Ermal
        dump_xml_config_sub($writer, $arr);
217 26433cb8 Scott Ullrich
218 ab83fce0 Ermal
        $writer->endElement();
219
        $writer->endDocument();
220
        $xmlconfig = $writer->outputMemory(true);
221
        return $xmlconfig;
222 26433cb8 Scott Ullrich
}
223
224
?>