Project

General

Profile

« Previous | Next » 

Revision 26433cb8

Added by Scott Ullrich over 15 years ago

Adding newer xmlreader code to it's own file so that it can be turned and off until remaining bugs are fixed

View differences:

etc/inc/xmlparse.inc
30 30
	POSSIBILITY OF SUCH DAMAGE.
31 31
*/
32 32

  
33
/*
34
	pfSense_MODULE:	utils
35
*/
36

  
37 33
/* The following items will be treated as arrays in config.xml */
38 34
function listtags() {
39 35
  /* Please keep this list alpha sorted and no longer than 80 characters
......
53 49
		"serversdisabled earlyshellcmd shellcmd staticmap subqueue timerange ".
54 50
		"tunnel user vip virtual_server vlan winsserver wolentry widget  "
55 51
		);
56
	return array_flip($ret);
52
	return $ret;
57 53
}
58 54

  
59 55
/* Package XML tags that should be treat as a list not as a traditional array */
60 56
function listtags_pkg() {
61 57
	$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 58

  
63
	return array_flip($ret);
59
	return $ret;
64 60
}
65 61

  
66
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
                                if (isset($listtags[$parser->name]))
81
                                        add_elements($cfgarray[$parser->name][], $parser);
82
                                else {
83
                                        add_elements($cfgarray[$parser->name], $parser);
84
					if (!isset($cfgarray[$parser->name]))
85
						$cfgarray[$parser->name] = array();
86
				}
87
                        }
88
                        break;
89
                case XMLReader::TEXT:
90
		case XMLReader::CDATA:
91
                        $cfgarray = $parser->value;
92
                        break;
93
                case XMLReader::END_ELEMENT:
94
                        return;
95
                        break;
96
                default:
97
                        break;
98
                }
62
function startElement($parser, $name, $attrs) {
63
	global $parsedcfg, $depth, $curpath, $havedata, $listtags;
99 64

  
100
        }
65
	array_push($curpath, strtolower($name));
66

  
67
	$ptr =& $parsedcfg;
68
	foreach ($curpath as $path) {
69
		$ptr =& $ptr[$path];
70
	}
71

  
72
	/* is it an element that belongs to a list? */
73
	if (in_array(strtolower($name), $listtags)) {
74

  
75
		/* is there an array already? */
76
		if (!is_array($ptr)) {
77
			/* make an array */
78
			$ptr = array();
79
		}
80

  
81
		array_push($curpath, count($ptr));
82

  
83
	} else if (isset($ptr)) {
84
		/* multiple entries not allowed for this element, bail out */
85
		die(sprintf("XML error: %s at line %d cannot occur more than once\n",
86
				$name,
87
				xml_get_current_line_number($parser)));
88
	}
89

  
90
	$depth++;
91
	$havedata = $depth;
92
}
93

  
94
function endElement($parser, $name) {
95
	global $depth, $curpath, $parsedcfg, $havedata, $listtags;
96

  
97
	if ($havedata == $depth) {
98
		$ptr =& $parsedcfg;
99
		foreach ($curpath as $path) {
100
			$ptr =& $ptr[$path];
101
		}
102
		$ptr = "";
103
	}
104

  
105
	array_pop($curpath);
106

  
107
	if (in_array(strtolower($name), $listtags))
108
		array_pop($curpath);
109

  
110
	$depth--;
111
}
112

  
113
function cData($parser, $data) {
114
	global $depth, $curpath, $parsedcfg, $havedata;
115

  
116
	$data = trim($data, "\t\n\r");
117

  
118
	if ($data != "") {
119
		$ptr =& $parsedcfg;
120
		foreach ($curpath as $path) {
121
			$ptr =& $ptr[$path];
122
		}
123

  
124
		if (is_string($ptr)) {
125
			$ptr .= $data;
126
		} else {
127
			if (trim($data, " ") != "") {
128
				$ptr = $data;
129
				$havedata++;
130
			}
131
		}
132
	}
101 133
}
102 134

  
103 135
function parse_xml_config($cffile, $rootobj, $isstring = "false") {
......
105 137
	$listtags = listtags();
106 138
        if (isset($GLOBALS['custom_listtags'])) {
107 139
          foreach($GLOBALS['custom_listtags'] as $tag) {
108
            $listtags[$tag] = $tag;
140
            $listtags[] = $tag;
109 141
          }
110 142
        }
111
	return parse_xml_config_raw($cffile, $rootobj);
143
	return parse_xml_config_raw($cffile, $rootobj, $isstring);
112 144
}
113 145

  
114 146
function parse_xml_config_pkg($cffile, $rootobj, $isstring = "false") {
......
116 148
	$listtags = listtags_pkg();
117 149
        if (isset($GLOBALS['custom_listtags_pkg'])) {
118 150
          foreach($GLOBALS['custom_listtags_pkg'] as $tag) {
119
            $listtags[$tag] = $tag;
151
            $listtags[] = $tag;
120 152
          }
121 153
        }
122 154
	return parse_xml_config_raw($cffile, $rootobj, $isstring);
......
124 156

  
125 157
function parse_xml_config_raw($cffile, $rootobj, $isstring = "false") {
126 158

  
159
	global $depth, $curpath, $parsedcfg, $havedata, $listtags;
127 160
	$parsedcfg = array();
161
	$curpath = array();
162
	$depth = 0;
163
	$havedata = 0;
164

  
165
	$xml_parser = xml_parser_create();
166

  
167
	xml_set_element_handler($xml_parser, "startElement", "endElement");
168
	xml_set_character_data_handler($xml_parser, "cdata");
169

  
170
	if (!($fp = fopen($cffile, "r"))) {
171
		die("Error: could not open XML input\n");
172
	}
128 173

  
129
	$par = new XMLReader();
130
	if ($par->open($cffile)) {
131
		add_elements($parsedcfg, $par);
132
		$par->close();
133
	} else
134
		log_error("Error returned while trying to parse {$cffile}");
174
	while ($data = fread($fp, 4096)) {
175
		if (!xml_parse($xml_parser, $data, feof($fp))) {
176
			log_error(sprintf("XML error: %s at line %d\n",
177
						xml_error_string(xml_get_error_code($xml_parser)),
178
						xml_get_current_line_number($xml_parser)));
179
			return -1;
180
		}
181
	}
182
	xml_parser_free($xml_parser);
183

  
184
	if (!$parsedcfg[$rootobj]) {
185
		die("XML error: no $rootobj object found!\n");
186
	}
135 187

  
136 188
	return $parsedcfg[$rootobj];
137 189
}
......
145 197
	foreach ($arr as $ent => $val) {
146 198
		if (is_array($val)) {
147 199
			/* is it just a list of multiple values? */
148
			if (isset($listtags[strtolower($ent)])) {
200
			if (in_array(strtolower($ent), $listtags)) {
149 201
				foreach ($val as $cval) {
150 202
					if (is_array($cval)) {
151 203
						$xmlconfig .= str_repeat("\t", $indent);
......
190 242
	$listtags = listtags();
191 243
        if (isset($GLOBALS['custom_listtags'])) {
192 244
          foreach($GLOBALS['custom_listtags'] as $tag) {
193
            $listtags[$tag] = $tag;
245
            $listtags[] = $tag;
194 246
          }
195 247
        }
196 248
	return dump_xml_config_raw($arr, $rootobj);
......
201 253
	$listtags = listtags_pkg();
202 254
        if (isset($GLOBALS['custom_listtags_pkg'])) {
203 255
          foreach($GLOBALS['custom_listtags_pkg'] as $tag) {
204
            $listtags[$tag] = $tag;
256
            $listtags[] = $tag;
205 257
          }
206 258
        }
207 259
	return dump_xml_config_raw($arr, $rootobj);
etc/inc/xmlreader.inc
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 config container ".
44
		"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
		"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 ppp package passthrumac phase1 phase2 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  "
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
                        //$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
                                if (isset($listtags[$parser->name]))
81
                                        add_elements($cfgarray[$parser->name][], $parser);
82
                                else {
83
                                        add_elements($cfgarray[$parser->name], $parser);
84
					if (!isset($cfgarray[$parser->name]))
85
						$cfgarray[$parser->name] = array();
86
				}
87
                        }
88
                        break;
89
                case XMLReader::TEXT:
90
		case XMLReader::CDATA:
91
                        $cfgarray = $parser->value;
92
                        break;
93
                case XMLReader::END_ELEMENT:
94
                        return;
95
                        break;
96
                default:
97
                        break;
98
                }
99

  
100
        }
101
}
102

  
103
function parse_xml_config($cffile, $rootobj, $isstring = "false") {
104
	global $listtags;
105
	$listtags = listtags();
106
        if (isset($GLOBALS['custom_listtags'])) {
107
          foreach($GLOBALS['custom_listtags'] as $tag) {
108
            $listtags[$tag] = $tag;
109
          }
110
        }
111
	return parse_xml_config_raw($cffile, $rootobj);
112
}
113

  
114
function parse_xml_config_pkg($cffile, $rootobj, $isstring = "false") {
115
	global $listtags;
116
	$listtags = listtags_pkg();
117
        if (isset($GLOBALS['custom_listtags_pkg'])) {
118
          foreach($GLOBALS['custom_listtags_pkg'] as $tag) {
119
            $listtags[$tag] = $tag;
120
          }
121
        }
122
	return parse_xml_config_raw($cffile, $rootobj, $isstring);
123
}
124

  
125
function parse_xml_config_raw($cffile, $rootobj, $isstring = "false") {
126

  
127
	$parsedcfg = array();
128

  
129
	$par = new XMLReader();
130
	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

  
136
	return $parsedcfg[$rootobj];
137
}
138

  
139
function dump_xml_config_sub($arr, $indent) {
140

  
141
	global $listtags;
142

  
143
	$xmlconfig = "";
144

  
145
	foreach ($arr as $ent => $val) {
146
		if (is_array($val)) {
147
			/* is it just a list of multiple values? */
148
			if (isset($listtags[strtolower($ent)])) {
149
				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
						if($cval === false) continue;
159
						if(($cval === true) || ($cval === "")) {
160
							$xmlconfig .= "<$ent/>\n";
161
						} else {
162
							$xmlconfig .= "<$ent>" . htmlspecialchars($cval) . "</$ent>\n";
163
					}
164
				}
165
				}
166
			} 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

  
185
	return $xmlconfig;
186
}
187

  
188
function dump_xml_config($arr, $rootobj) {
189
	global $listtags;
190
	$listtags = listtags();
191
        if (isset($GLOBALS['custom_listtags'])) {
192
          foreach($GLOBALS['custom_listtags'] as $tag) {
193
            $listtags[$tag] = $tag;
194
          }
195
        }
196
	return dump_xml_config_raw($arr, $rootobj);
197
}
198

  
199
function dump_xml_config_pkg($arr, $rootobj) {
200
	global $listtags;
201
	$listtags = listtags_pkg();
202
        if (isset($GLOBALS['custom_listtags_pkg'])) {
203
          foreach($GLOBALS['custom_listtags_pkg'] as $tag) {
204
            $listtags[$tag] = $tag;
205
          }
206
        }
207
	return dump_xml_config_raw($arr, $rootobj);
208
}
209

  
210
function dump_xml_config_raw($arr, $rootobj) {
211

  
212
	$xmlconfig = "<?xml version=\"1.0\"?" . ">\n";
213
	$xmlconfig .= "<$rootobj>\n";
214

  
215
	$xmlconfig .= dump_xml_config_sub($arr, 1);
216

  
217
	$xmlconfig .= "</$rootobj>\n";
218

  
219
	return $xmlconfig;
220
}
221

  
222
?>

Also available in: Unified diff