From 26433cb8424871912ef94213976205cd16d7831c Mon Sep 17 00:00:00 2001 From: Scott Ullrich Date: Fri, 6 Nov 2009 22:28:00 -0500 Subject: Adding newer xmlreader code to it's own file so that it can be turned and off until remaining bugs are fixed --- etc/inc/xmlparse.inc | 156 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 104 insertions(+), 52 deletions(-) (limited to 'etc/inc/xmlparse.inc') diff --git a/etc/inc/xmlparse.inc b/etc/inc/xmlparse.inc index 7627c4b..cb55117 100644 --- a/etc/inc/xmlparse.inc +++ b/etc/inc/xmlparse.inc @@ -30,10 +30,6 @@ POSSIBILITY OF SUCH DAMAGE. */ -/* - pfSense_MODULE: utils -*/ - /* The following items will be treated as arrays in config.xml */ function listtags() { /* Please keep this list alpha sorted and no longer than 80 characters @@ -53,51 +49,87 @@ function listtags() { "serversdisabled earlyshellcmd shellcmd staticmap subqueue timerange ". "tunnel user vip virtual_server vlan winsserver wolentry widget " ); - return array_flip($ret); + return $ret; } /* Package XML tags that should be treat as a list not as a traditional array */ function listtags_pkg() { $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"); - return array_flip($ret); + return $ret; } -function add_elements(&$cfgarray, &$parser) { - global $listtags; - while ($parser->read()) { - switch ($parser->nodeType) { - case XMLReader::WHITESPACE: - //$type = "WHITESPACE"; - break; - case XMLReader::SIGNIFICANT_WHITESPACE: - //$type = "SIGNIFICANT_WHITESPACE"; - break; - case XMLReader::ELEMENT: - if ($parser->isEmptyElement) { - $cfgarray[$parser->name] = ""; - } else { - if (isset($listtags[$parser->name])) - add_elements($cfgarray[$parser->name][], $parser); - else { - add_elements($cfgarray[$parser->name], $parser); - if (!isset($cfgarray[$parser->name])) - $cfgarray[$parser->name] = array(); - } - } - break; - case XMLReader::TEXT: - case XMLReader::CDATA: - $cfgarray = $parser->value; - break; - case XMLReader::END_ELEMENT: - return; - break; - default: - break; - } +function startElement($parser, $name, $attrs) { + global $parsedcfg, $depth, $curpath, $havedata, $listtags; - } + array_push($curpath, strtolower($name)); + + $ptr =& $parsedcfg; + foreach ($curpath as $path) { + $ptr =& $ptr[$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)); + + } else if (isset($ptr)) { + /* multiple entries not allowed for this element, bail out */ + die(sprintf("XML error: %s at line %d cannot occur more than once\n", + $name, + xml_get_current_line_number($parser))); + } + + $depth++; + $havedata = $depth; +} + +function endElement($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($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 .= $data; + } else { + if (trim($data, " ") != "") { + $ptr = $data; + $havedata++; + } + } + } } function parse_xml_config($cffile, $rootobj, $isstring = "false") { @@ -105,10 +137,10 @@ function parse_xml_config($cffile, $rootobj, $isstring = "false") { $listtags = listtags(); if (isset($GLOBALS['custom_listtags'])) { foreach($GLOBALS['custom_listtags'] as $tag) { - $listtags[$tag] = $tag; + $listtags[] = $tag; } } - return parse_xml_config_raw($cffile, $rootobj); + return parse_xml_config_raw($cffile, $rootobj, $isstring); } function parse_xml_config_pkg($cffile, $rootobj, $isstring = "false") { @@ -116,7 +148,7 @@ function parse_xml_config_pkg($cffile, $rootobj, $isstring = "false") { $listtags = listtags_pkg(); if (isset($GLOBALS['custom_listtags_pkg'])) { foreach($GLOBALS['custom_listtags_pkg'] as $tag) { - $listtags[$tag] = $tag; + $listtags[] = $tag; } } return parse_xml_config_raw($cffile, $rootobj, $isstring); @@ -124,14 +156,34 @@ function parse_xml_config_pkg($cffile, $rootobj, $isstring = "false") { function parse_xml_config_raw($cffile, $rootobj, $isstring = "false") { + global $depth, $curpath, $parsedcfg, $havedata, $listtags; $parsedcfg = array(); + $curpath = array(); + $depth = 0; + $havedata = 0; + + $xml_parser = xml_parser_create(); + + xml_set_element_handler($xml_parser, "startElement", "endElement"); + xml_set_character_data_handler($xml_parser, "cdata"); + + if (!($fp = fopen($cffile, "r"))) { + die("Error: could not open XML input\n"); + } - $par = new XMLReader(); - if ($par->open($cffile)) { - add_elements($parsedcfg, $par); - $par->close(); - } else - log_error("Error returned while trying to parse {$cffile}"); + while ($data = fread($fp, 4096)) { + if (!xml_parse($xml_parser, $data, feof($fp))) { + log_error(sprintf("XML error: %s at line %d\n", + xml_error_string(xml_get_error_code($xml_parser)), + xml_get_current_line_number($xml_parser))); + return -1; + } + } + xml_parser_free($xml_parser); + + if (!$parsedcfg[$rootobj]) { + die("XML error: no $rootobj object found!\n"); + } return $parsedcfg[$rootobj]; } @@ -145,7 +197,7 @@ function dump_xml_config_sub($arr, $indent) { foreach ($arr as $ent => $val) { if (is_array($val)) { /* is it just a list of multiple values? */ - if (isset($listtags[strtolower($ent)])) { + if (in_array(strtolower($ent), $listtags)) { foreach ($val as $cval) { if (is_array($cval)) { $xmlconfig .= str_repeat("\t", $indent); @@ -190,7 +242,7 @@ function dump_xml_config($arr, $rootobj) { $listtags = listtags(); if (isset($GLOBALS['custom_listtags'])) { foreach($GLOBALS['custom_listtags'] as $tag) { - $listtags[$tag] = $tag; + $listtags[] = $tag; } } return dump_xml_config_raw($arr, $rootobj); @@ -201,7 +253,7 @@ function dump_xml_config_pkg($arr, $rootobj) { $listtags = listtags_pkg(); if (isset($GLOBALS['custom_listtags_pkg'])) { foreach($GLOBALS['custom_listtags_pkg'] as $tag) { - $listtags[$tag] = $tag; + $listtags[] = $tag; } } return dump_xml_config_raw($arr, $rootobj); -- cgit v1.1