Project

General

Profile

Download (8.52 KB) Statistics
| Branch: | Tag: | Revision:
1
<?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
                "alias aliasurl allowedip authserver bridged ca cacert cert crl clone config ".
44
                "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
                "option package passthrumac phase1 phase2 ppp pppoe priv proxyarpnet qinqentry queue ".
52
                "pages pipe roll route row rrddatafile rule schedule service servernat servers ".
53
                "serversdisabled earlyshellcmd shellcmd staticmap subqueue timerange ".
54
                "tunnel user vip virtual_server vlan winsserver wolentry widget npt pool"
55
                );
56
	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
                        if (isset($listtags[strtolower($parser->name)])) {
75
                        	if (!$parser->isEmptyElement)
76
                                        add_elements($cfgarray[$parser->name][], $parser);
77
                        } else {
78
                        	if ($parser->isEmptyElement)
79
					$cfgarray[$parser->name] = "";
80
				else
81
                        		add_elements($cfgarray[$parser->name], $parser);
82
			}
83
                        break;
84
                case XMLReader::TEXT:
85
		case XMLReader::CDATA:
86
                        $cfgarray = $parser->value;
87
                        break;
88
                case XMLReader::END_ELEMENT:
89
			return;
90
			break;
91
                default:
92
                        break;
93
                }
94
	}
95
}
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

    
106
	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
	if ($par->open($cffile, "UTF-8", LIBXML_NOERROR | LIBXML_NOWARNING)) {
126
		add_elements($parsedcfg, $par);
127
		$par->close();
128
	} else
129
		log_error(sprintf(gettext("Error returned while trying to parse %s"), $cffile));
130

    
131
	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
}
143

    
144
function dump_xml_config_sub(& $writer, $arr) {
145
        global $listtags;
146

    
147
        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
}
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
        $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

    
216
        dump_xml_config_sub($writer, $arr);
217

    
218
        $writer->endElement();
219
        $writer->endDocument();
220
        $xmlconfig = $writer->outputMemory(true);
221
        return $xmlconfig;
222
}
223

    
224
?>
(62-62/66)