Project

General

Profile

Download (7.4 KB) Statistics
| Branch: | Tag: | Revision:
1 5b237745 Scott Ullrich
<?php
2 307cd525 Bill Marquette
/* $Id$ */
3 5b237745 Scott Ullrich
/*
4
	xmlparse.inc
5
	functions to parse/dump configuration files in XML format
6
	part of m0n0wall (http://m0n0.ch/wall)
7 528ae96e Scott Ullrich
8 5b237745 Scott Ullrich
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
9
	All rights reserved.
10 528ae96e Scott Ullrich
11 5b237745 Scott Ullrich
	Redistribution and use in source and binary forms, with or without
12
	modification, are permitted provided that the following conditions are met:
13 528ae96e Scott Ullrich
14 5b237745 Scott Ullrich
	1. Redistributions of source code must retain the above copyright notice,
15
	   this list of conditions and the following disclaimer.
16 528ae96e Scott Ullrich
17 5b237745 Scott Ullrich
	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 528ae96e Scott Ullrich
21 5b237745 Scott Ullrich
	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 523855b0 Scott Ullrich
/*
34
	pfSense_MODULE:	utils
35
*/
36
37 2e5ebf5d Scott Ullrich
/* The following items will be treated as arrays in config.xml */
38 eeb6c16e Bill Marquette
function listtags() {
39 0919224f Bill Marquette
  /* 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 64cc39d3 Matthew Grooms
	$ret = explode(" ",
43 f63d5b66 Helder Pereira
		"alias aliasurl allowedip authserver bridged ca cacert cert config container ".
44 0919224f Bill Marquette
		"columnitem depends_on_package disk dnsserver dnsupdate domainoverrides ".
45
		"dyndns earlyshellcmd element encryption-algorithm-option field ".
46
		"fieldname hash-algorithm-option gateway_item gateway_group gif gre ".
47 0ec2fdf0 Ermal Lu?i
		"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 b6617403 Matthew Grooms
		"openvpn-server openvpn-client openvpn-csc " .
51 5f1e1d26 Ermal Lu?i
		"option ppp package passthrumac phase1 phase2 priv proxyarpnet qinqentry queue ".
52 336e3c1c Charlie
		"pages pipe roll route row rrddatafile rule schedule service servernat servers ".
53 0919224f Bill Marquette
		"serversdisabled earlyshellcmd shellcmd staticmap subqueue timerange ".
54 5e7d127a Scott Ullrich
		"tunnel user vip virtual_server vlan winsserver wolentry widget  "
55 0919224f Bill Marquette
		);
56 d1bd66b4 Ermal Lu?i
	return array_flip($ret);
57 eeb6c16e Bill Marquette
}
58
59 2fe045e5 Bill Marquette
/* Package XML tags that should be treat as a list not as a traditional array */
60 eeb6c16e Bill Marquette
function listtags_pkg() {
61 e1b5fccb Scott Ullrich
	$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 eeb6c16e Bill Marquette
63 d1bd66b4 Ermal Lu?i
	return array_flip($ret);
64 eeb6c16e Bill Marquette
}
65
66 d7b6c842 Ermal Lu?i
function add_elements(&$cfgarray, &$parser) {
67
        global $listtags;
68
        while ($parser->read()) {
69
                switch ($parser->nodeType) {
70
                case XMLReader::WHITESPACE:
71
                        //$type = "WHITESPACE";
72
                        break;
73
                case XMLReader::SIGNIFICANT_WHITESPACE:
74
                        //$type = "SIGNIFICANT_WHITESPACE";
75
                        break;
76
                case XMLReader::ELEMENT:
77
                        if ($parser->isEmptyElement) {
78
                                $cfgarray[$parser->name] = "";
79
                        } else {
80 d1bd66b4 Ermal Lu?i
                                if (isset($listtags[$parser->name]))
81
                                        add_elements($cfgarray[$parser->name][], $parser);
82 0eb230cb Ermal Lu?i
                                else {
83 d7b6c842 Ermal Lu?i
                                        add_elements($cfgarray[$parser->name], $parser);
84 0eb230cb Ermal Lu?i
					if (!isset($cfgarray[$parser->name]))
85
						$cfgarray[$parser->name] = array();
86
				}
87 d7b6c842 Ermal Lu?i
                        }
88
                        break;
89
                case XMLReader::TEXT:
90 5e4071be Ermal Lu?i
		case XMLReader::CDATA:
91 d7b6c842 Ermal Lu?i
                        $cfgarray = $parser->value;
92
                        break;
93
                case XMLReader::END_ELEMENT:
94
                        return;
95
                        break;
96
                default:
97
                        break;
98
                }
99 528ae96e Scott Ullrich
100 d7b6c842 Ermal Lu?i
        }
101 5b237745 Scott Ullrich
}
102
103 48ca2dc9 Colin Smith
function parse_xml_config($cffile, $rootobj, $isstring = "false") {
104 cb980a2f Colin Smith
	global $listtags;
105 eeb6c16e Bill Marquette
	$listtags = listtags();
106 eb72ceda Scott Ullrich
        if (isset($GLOBALS['custom_listtags'])) {
107
          foreach($GLOBALS['custom_listtags'] as $tag) {
108 d1bd66b4 Ermal Lu?i
            $listtags[$tag] = $tag;
109 eb72ceda Scott Ullrich
          }
110
        }
111 d7b6c842 Ermal Lu?i
	return parse_xml_config_raw($cffile, $rootobj);
112 cb980a2f Colin Smith
}
113
114 48ca2dc9 Colin Smith
function parse_xml_config_pkg($cffile, $rootobj, $isstring = "false") {
115 cb980a2f Colin Smith
	global $listtags;
116 eeb6c16e Bill Marquette
	$listtags = listtags_pkg();
117 eb72ceda Scott Ullrich
        if (isset($GLOBALS['custom_listtags_pkg'])) {
118
          foreach($GLOBALS['custom_listtags_pkg'] as $tag) {
119 d1bd66b4 Ermal Lu?i
            $listtags[$tag] = $tag;
120 eb72ceda Scott Ullrich
          }
121
        }
122 48ca2dc9 Colin Smith
	return parse_xml_config_raw($cffile, $rootobj, $isstring);
123 cb980a2f Colin Smith
}
124
125 48ca2dc9 Colin Smith
function parse_xml_config_raw($cffile, $rootobj, $isstring = "false") {
126 5b237745 Scott Ullrich
127 08c3872d Colin Smith
	$parsedcfg = array();
128 d7b6c842 Ermal Lu?i
129
	$par = new XMLReader();
130 8f192f4e Ermal Lu?i
	if ($par->open($cffile)) {
131
		add_elements($parsedcfg, $par);
132
		$par->close();
133
	} else
134
		log_error("Error returned while trying to parse {$cffile}");
135 528ae96e Scott Ullrich
136 08c3872d Colin Smith
	return $parsedcfg[$rootobj];
137 5b237745 Scott Ullrich
}
138
139
function dump_xml_config_sub($arr, $indent) {
140
141
	global $listtags;
142 528ae96e Scott Ullrich
143 5b237745 Scott Ullrich
	$xmlconfig = "";
144
145
	foreach ($arr as $ent => $val) {
146
		if (is_array($val)) {
147
			/* is it just a list of multiple values? */
148 d1bd66b4 Ermal Lu?i
			if (isset($listtags[strtolower($ent)])) {
149 5b237745 Scott Ullrich
				foreach ($val as $cval) {
150
					if (is_array($cval)) {
151
						$xmlconfig .= str_repeat("\t", $indent);
152
						$xmlconfig .= "<$ent>\n";
153
						$xmlconfig .= dump_xml_config_sub($cval, $indent + 1);
154
						$xmlconfig .= str_repeat("\t", $indent);
155
						$xmlconfig .= "</$ent>\n";
156
					} else {
157
						$xmlconfig .= str_repeat("\t", $indent);
158 0d57f5f3 Scott Ullrich
						if($cval === false) continue;
159
						if(($cval === true) || ($cval === "")) {
160 5b237745 Scott Ullrich
							$xmlconfig .= "<$ent/>\n";
161 0d57f5f3 Scott Ullrich
						} else {
162 5b237745 Scott Ullrich
							$xmlconfig .= "<$ent>" . htmlspecialchars($cval) . "</$ent>\n";
163
					}
164
				}
165 0d57f5f3 Scott Ullrich
				}
166 5b237745 Scott Ullrich
			} else {
167
				/* it's an array */
168
				$xmlconfig .= str_repeat("\t", $indent);
169
				$xmlconfig .= "<$ent>\n";
170
				$xmlconfig .= dump_xml_config_sub($val, $indent + 1);
171
				$xmlconfig .= str_repeat("\t", $indent);
172
				$xmlconfig .= "</$ent>\n";
173
			}
174
		} else {
175
			if ((is_bool($val) && ($val == true)) || ($val === "")) {
176
				$xmlconfig .= str_repeat("\t", $indent);
177
				$xmlconfig .= "<$ent/>\n";
178
			} else if (!is_bool($val)) {
179
				$xmlconfig .= str_repeat("\t", $indent);
180
				$xmlconfig .= "<$ent>" . htmlspecialchars($val) . "</$ent>\n";
181
			}
182
		}
183
	}
184 528ae96e Scott Ullrich
185 5b237745 Scott Ullrich
	return $xmlconfig;
186
}
187
188
function dump_xml_config($arr, $rootobj) {
189 ba6882bf Colin Smith
	global $listtags;
190 eeb6c16e Bill Marquette
	$listtags = listtags();
191 4928848f Scott Ullrich
        if (isset($GLOBALS['custom_listtags'])) {
192
          foreach($GLOBALS['custom_listtags'] as $tag) {
193 d1bd66b4 Ermal Lu?i
            $listtags[$tag] = $tag;
194 4928848f Scott Ullrich
          }
195
        }
196 ba6882bf Colin Smith
	return dump_xml_config_raw($arr, $rootobj);
197
}
198
199
function dump_xml_config_pkg($arr, $rootobj) {
200
	global $listtags;
201 eeb6c16e Bill Marquette
	$listtags = listtags_pkg();
202 4928848f Scott Ullrich
        if (isset($GLOBALS['custom_listtags_pkg'])) {
203
          foreach($GLOBALS['custom_listtags_pkg'] as $tag) {
204 d1bd66b4 Ermal Lu?i
            $listtags[$tag] = $tag;
205 4928848f Scott Ullrich
          }
206
        }
207 ba6882bf Colin Smith
	return dump_xml_config_raw($arr, $rootobj);
208
}
209
210
function dump_xml_config_raw($arr, $rootobj) {
211 5b237745 Scott Ullrich
212
	$xmlconfig = "<?xml version=\"1.0\"?" . ">\n";
213
	$xmlconfig .= "<$rootobj>\n";
214 528ae96e Scott Ullrich
215 5b237745 Scott Ullrich
	$xmlconfig .= dump_xml_config_sub($arr, 1);
216 528ae96e Scott Ullrich
217 5b237745 Scott Ullrich
	$xmlconfig .= "</$rootobj>\n";
218 528ae96e Scott Ullrich
219 5b237745 Scott Ullrich
	return $xmlconfig;
220
}
221
222 d173230c Seth Mos
?>