xmlparse_attr.inc 6.42 KB
Newer Older
Ad Schellevis's avatar
Ad Schellevis committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
<?php
/* $Id$ */
/*
	xmlparse_attr.inc
	functions to parse configuration files in XML format with attributes
	Copyright (C) 2010 Erik Fonnesbeck
	All rights reserved.

	Based on xmlparse.inc, originally part of m0n0wall (http://m0n0.ch/wall)
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
	All rights reserved.

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions are met:

	1. Redistributions of source code must retain the above copyright notice,
	   this list of conditions and the following disclaimer.

	2. Redistributions in binary form must reproduce the above copyright
	   notice, this list of conditions and the following disclaimer in the
	   documentation and/or other materials provided with the distribution.

	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
	POSSIBILITY OF SUCH DAMAGE.
*/

/* The following items will be treated as arrays in regdomain.xml */
function listtags_rd() {
	$ret = explode(" ",
		"band country flags freqband netband rd"
		);
	return $ret;
}

function startElement_attr($parser, $name, $attrs) {
	global $parsedcfg, $depth, $curpath, $havedata, $listtags, $parsedattrs;

	array_push($curpath, strtolower($name));

	$ptr =& $parsedcfg;
	if (!empty($attrs)) {
		$attrptr =& $parsedattrs;
		$writeattrs = true;
	}
	foreach ($curpath as $path) {
		$ptr =& $ptr[$path];
		if (isset($writeattrs))
			$attrptr =& $attrptr[$path];
	}

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

		/* is there an array already? */
		if (!is_array($ptr)) {
			/* make an array */
			$ptr = array();
		}

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

		if (isset($writeattrs)) {
			if (!is_array($attrptr))
				$attrptr = array();
			$attrptr[count($ptr)] = $attrs;
		}

	} else if (isset($ptr)) {
		/* multiple entries not allowed for this element, bail out */
		die(sprintf(gettext('XML error: %1$s at line %2$d cannot occur more than once') . "\n",
				$name,
				xml_get_current_line_number($parser)));
	} else if (isset($writeattrs)) {
		$attrptr = $attrs;
	}

	$depth++;
	$havedata = $depth;
}

function endElement_attr($parser, $name) {
	global $depth, $curpath, $parsedcfg, $havedata, $listtags;

	if ($havedata == $depth) {
		$ptr =& $parsedcfg;
		foreach ($curpath as $path) {
			$ptr =& $ptr[$path];
		}
		$ptr = "";
	}

	array_pop($curpath);

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

	$depth--;
}

function cData_attr($parser, $data) {
	global $depth, $curpath, $parsedcfg, $havedata;

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

	if ($data != "") {
		$ptr =& $parsedcfg;
		foreach ($curpath as $path) {
			$ptr =& $ptr[$path];
		}

		if (is_string($ptr)) {
			$ptr .= html_entity_decode($data);
		} else {
			if (trim($data, " ") != "") {
				$ptr = html_entity_decode($data);
				$havedata++;
			}
		}
	}
}

function parse_xml_regdomain(&$rdattributes, $rdfile = '', $rootobj = 'regulatory-data') {
	global $g, $listtags;

	if (empty($rdfile))
		$rdfile = $g['etc_path'] . '/regdomain.xml';
	$listtags = listtags_rd();
	$parsed_xml = array();

	if (file_exists($g['tmp_path'] . '/regdomain.cache')) {
		$parsed_xml = unserialize(file_get_contents($g['tmp_path'] . '/regdomain.cache'));
		if (!empty($parsed_xml)) {
			$rdmain = $parsed_xml['main'];
			$rdattributes = $parsed_xml['attributes'];
		}
	}
	if (empty($parsed_xml) && file_exists($g['etc_path'] . '/regdomain.xml')) {
		$rdmain = parse_xml_config_raw_attr($rdfile, $rootobj, $rdattributes);

		// unset parts that aren't used before making cache
		foreach ($rdmain['regulatory-domains']['rd'] as $rdkey => $rdentry) {
			if (isset($rdmain['regulatory-domains']['rd'][$rdkey]['netband']))
				unset($rdmain['regulatory-domains']['rd'][$rdkey]['netband']);
			if (isset($rdattributes['regulatory-domains']['rd'][$rdkey]['netband']))
				unset($rdattributes['regulatory-domains']['rd'][$rdkey]['netband']);
		}
		if (isset($rdmain['shared-frequency-bands']))
			unset($rdmain['shared-frequency-bands']);
		if (isset($rdattributes['shared-frequency-bands']))
			unset($rdattributes['shared-frequency-bands']);

		$parsed_xml = array('main' => $rdmain, 'attributes' => $rdattributes);
		$rdcache = fopen($g['tmp_path'] . '/regdomain.cache', "w");
		fwrite($rdcache, serialize($parsed_xml));
		fclose($rdcache);
	}

	return $rdmain;
}

function parse_xml_config_raw_attr($cffile, $rootobj, &$parsed_attributes, $isstring = "false") {

	global $depth, $curpath, $parsedcfg, $havedata, $listtags, $parsedattrs;
	$parsedcfg = array();
	$curpath = array();
	$depth = 0;
	$havedata = 0;

	if (isset($parsed_attributes))
		$parsedattrs = array();

	$xml_parser = xml_parser_create();

	xml_set_element_handler($xml_parser, "startElement_attr", "endElement_attr");
	xml_set_character_data_handler($xml_parser, "cData_attr");
184
	xml_parser_set_option($xml_parser,XML_OPTION_SKIP_WHITE, 1);
Ad Schellevis's avatar
Ad Schellevis committed
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227

	if (!($fp = fopen($cffile, "r"))) {
		log_error(gettext("Error: could not open XML input") . "\n");
		if (isset($parsed_attributes)) {
			$parsed_attributes = array();
			unset($parsedattrs);
		}
		return -1;
	}

	while ($data = fread($fp, 4096)) {
		if (!xml_parse($xml_parser, $data, feof($fp))) {
			log_error(sprintf(gettext('XML error: %1$s at line %2$d') . "\n",
						xml_error_string(xml_get_error_code($xml_parser)),
						xml_get_current_line_number($xml_parser)));
			if (isset($parsed_attributes)) {
				$parsed_attributes = array();
				unset($parsedattrs);
			}
			return -1;
		}
	}
	xml_parser_free($xml_parser);

	if (!$parsedcfg[$rootobj]) {
		log_error(sprintf(gettext("XML error: no %s object found!") . "\n", $rootobj));
		if (isset($parsed_attributes)) {
			$parsed_attributes = array();
			unset($parsedattrs);
		}
		return -1;
	}

	if (isset($parsed_attributes)) {
		if ($parsedattrs[$rootobj])
			$parsed_attributes = $parsedattrs[$rootobj];
		unset($parsedattrs);
	}

	return $parsedcfg[$rootobj];
}

?>