Projet

Général

Profil

Télécharger (9,41 ko) Statistiques
| Branche: | Tag: | Révision:

univnautes / etc / inc / xmlreader.inc @ c650b2f7

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
	/*
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
	$ret = array(
44
		'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
	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
	$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

    
71
	return array_flip($ret);
72
}
73

    
74
function add_elements(&$cfgarray, &$parser) {
75
        global $listtags;
76

    
77
        while ($parser->read()) {
78
                switch ($parser->nodeType) {
79
                case XMLReader::WHITESPACE:
80
                case XMLReader::SIGNIFICANT_WHITESPACE:
81
                        break;
82
                case XMLReader::ELEMENT:
83
                        if (isset($listtags[strtolower($parser->name)])) {
84
				$cfgref =& $cfgarray[$parser->name][count($cfgarray[$parser->name])];
85
                        	if (!$parser->isEmptyElement) {
86
                                        add_elements($cfgref, $parser);
87
				} else
88
					$cfgref = array();
89
					
90
                        } else {
91
				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
			}
113

    
114
			$i = 0;
115
			while ($parser->moveToAttributeNo($i)) {
116
				$cfgref[$parser->name] = $parser->value;
117
				$i++;
118
			}
119
                        break;
120
                case XMLReader::TEXT:
121
		case XMLReader::CDATA:
122
                        $cfgarray = $parser->value;
123
                        break;
124
                case XMLReader::END_ELEMENT:
125
			return;
126
			break;
127
                default:
128
                        break;
129
                }
130
	}
131
}
132

    
133
function parse_xml_config($cffile, $rootobj, $isstring = "false") {
134
	global $listtags;
135

    
136
	$listtags = listtags();
137
        if (isset($GLOBALS['custom_listtags'])) {
138
          foreach($GLOBALS['custom_listtags'] as $tag) {
139
            $listtags[$tag] = $tag;
140
          }
141
        }
142

    
143
	return parse_xml_config_raw($cffile, $rootobj);
144
}
145

    
146
function parse_xml_config_pkg($cffile, $rootobj, $isstring = "false") {
147
	global $listtags;
148

    
149
	$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
	global $listtags;
160

    
161
	$parsedcfg = array();
162

    
163
	$par = new XMLReader();
164
	if ($par->open($cffile, "UTF-8", LIBXML_NOERROR | LIBXML_NOWARNING)) {
165
		add_elements($parsedcfg, $par);
166
		$par->close();
167
	} else
168
		log_error(sprintf(gettext("Error returned while trying to parse %s"), $cffile));
169

    
170
	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
}
182

    
183
function dump_xml_config_sub(& $writer, $arr) {
184
        global $listtags;
185

    
186
        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
}
223

    
224
function dump_xml_config($arr, $rootobj) {
225
	global $listtags;
226

    
227
	$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

    
239
	$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
        $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

    
257
        dump_xml_config_sub($writer, $arr);
258

    
259
        $writer->endElement();
260
        $writer->endDocument();
261
        $xmlconfig = $writer->outputMemory(true);
262

    
263
        return $xmlconfig;
264
}
265

    
266
?>
(64-64/68)