Commit 51c3f922 authored by Ad Schellevis's avatar Ad Schellevis

(legacy) move xml2array() to upgrade_config.inc, included by config.lib.inc.

parent 8d0e40e2
...@@ -27,114 +27,6 @@ ...@@ -27,114 +27,6 @@
*/ */
/* This xml 2 array function is courtesy of the php.net comment section on xml_parse.
* it is roughly 4 times faster then our existing pfSense parser but due to the large
* size of the RRD xml dumps this is required.
* The reason we do not use it for pfSense is that it does not know about array fields
* which causes it to fail on array fields with single items. Possible Todo?
*/
function xml2array($contents, $get_attributes = 1, $priority = 'tag')
{
if (!function_exists('xml_parser_create'))
{
return array ();
}
$parser = xml_parser_create('');
xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8");
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($parser, trim($contents), $xml_values);
xml_parser_free($parser);
if (!$xml_values) {
return; //Hmm...
}
$xml_array = array ();
$parents = array ();
$opened_tags = array ();
$arr = array ();
$current = & $xml_array;
$repeated_tag_index = array ();
foreach ($xml_values as $data) {
unset ($attributes, $value);
extract($data);
$result = array ();
$attributes_data = array ();
if (isset ($value))
{
if ($priority == 'tag') {
$result = $value;
} else {
$result['value'] = $value;
}
}
if (isset ($attributes) and $get_attributes) {
foreach ($attributes as $attr => $val) {
if ($priority == 'tag') {
$attributes_data[$attr] = $val;
} else {
$result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
}
}
}
if ($type == "open") {
$parent[$level -1] = & $current;
if (!is_array($current) || (!in_array($tag, array_keys($current)))) {
$current[$tag] = $result;
if ($attributes_data) {
$current[$tag . '_attr'] = $attributes_data;
}
$repeated_tag_index[$tag . '_' . $level] = 1;
$current = & $current[$tag];
} else {
if (isset ($current[$tag][0])) {
$current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;
$repeated_tag_index[$tag . '_' . $level]++;
} else {
$current[$tag] = array ($current[$tag], $result );
$repeated_tag_index[$tag . '_' . $level] = 2;
if (isset ($current[$tag . '_attr'])) {
$current[$tag]['0_attr'] = $current[$tag . '_attr'];
unset ($current[$tag . '_attr']);
}
}
$last_item_index = $repeated_tag_index[$tag . '_' . $level] - 1;
$current = & $current[$tag][$last_item_index];
}
} elseif ($type == "complete") {
if (!isset ($current[$tag])) {
$current[$tag] = $result;
$repeated_tag_index[$tag . '_' . $level] = 1;
if ($priority == 'tag' and $attributes_data) {
$current[$tag . '_attr'] = $attributes_data;
}
} else {
if (isset ($current[$tag][0]) and is_array($current[$tag])) {
$current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;
if ($priority == 'tag' and $get_attributes and $attributes_data) {
$current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;
}
$repeated_tag_index[$tag . '_' . $level]++;
} else {
$current[$tag] = array ($current[$tag], $result );
$repeated_tag_index[$tag . '_' . $level] = 1;
if ($priority == 'tag' and $get_attributes) {
if (isset ($current[$tag . '_attr'])) {
$current[$tag]['0_attr'] = $current[$tag . '_attr'];
unset ($current[$tag . '_attr']);
}
if ($attributes_data) {
$current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;
}
}
$repeated_tag_index[$tag . '_' . $level]++; //0 and 1 index is already taken
}
}
} elseif ($type == 'close') {
$current = & $parent[$level -1];
}
}
return ($xml_array);
}
/* Returns the calculated bit length of the prefix delegation from the WAN interface */ /* Returns the calculated bit length of the prefix delegation from the WAN interface */
/* DHCP-PD is variable, calculate from the prefix-len on the WAN interface */ /* DHCP-PD is variable, calculate from the prefix-len on the WAN interface */
......
...@@ -42,6 +42,115 @@ function dump_rrd_to_xml($rrddatabase, $xmldumpfile) ...@@ -42,6 +42,115 @@ function dump_rrd_to_xml($rrddatabase, $xmldumpfile)
return($dumpret); return($dumpret);
} }
/* This xml 2 array function is courtesy of the php.net comment section on xml_parse.
* it is roughly 4 times faster then our existing pfSense parser but due to the large
* size of the RRD xml dumps this is required.
* The reason we do not use it for pfSense is that it does not know about array fields
* which causes it to fail on array fields with single items. Possible Todo?
*/
function xml2array($contents, $get_attributes = 1, $priority = 'tag')
{
if (!function_exists('xml_parser_create'))
{
return array ();
}
$parser = xml_parser_create('');
xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8");
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($parser, trim($contents), $xml_values);
xml_parser_free($parser);
if (!$xml_values) {
return; //Hmm...
}
$xml_array = array ();
$parents = array ();
$opened_tags = array ();
$arr = array ();
$current = & $xml_array;
$repeated_tag_index = array ();
foreach ($xml_values as $data) {
unset ($attributes, $value);
extract($data);
$result = array ();
$attributes_data = array ();
if (isset ($value))
{
if ($priority == 'tag') {
$result = $value;
} else {
$result['value'] = $value;
}
}
if (isset ($attributes) and $get_attributes) {
foreach ($attributes as $attr => $val) {
if ($priority == 'tag') {
$attributes_data[$attr] = $val;
} else {
$result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
}
}
}
if ($type == "open") {
$parent[$level -1] = & $current;
if (!is_array($current) || (!in_array($tag, array_keys($current)))) {
$current[$tag] = $result;
if ($attributes_data) {
$current[$tag . '_attr'] = $attributes_data;
}
$repeated_tag_index[$tag . '_' . $level] = 1;
$current = & $current[$tag];
} else {
if (isset ($current[$tag][0])) {
$current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;
$repeated_tag_index[$tag . '_' . $level]++;
} else {
$current[$tag] = array ($current[$tag], $result );
$repeated_tag_index[$tag . '_' . $level] = 2;
if (isset ($current[$tag . '_attr'])) {
$current[$tag]['0_attr'] = $current[$tag . '_attr'];
unset ($current[$tag . '_attr']);
}
}
$last_item_index = $repeated_tag_index[$tag . '_' . $level] - 1;
$current = & $current[$tag][$last_item_index];
}
} elseif ($type == "complete") {
if (!isset ($current[$tag])) {
$current[$tag] = $result;
$repeated_tag_index[$tag . '_' . $level] = 1;
if ($priority == 'tag' and $attributes_data) {
$current[$tag . '_attr'] = $attributes_data;
}
} else {
if (isset ($current[$tag][0]) and is_array($current[$tag])) {
$current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;
if ($priority == 'tag' and $get_attributes and $attributes_data) {
$current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;
}
$repeated_tag_index[$tag . '_' . $level]++;
} else {
$current[$tag] = array ($current[$tag], $result );
$repeated_tag_index[$tag . '_' . $level] = 1;
if ($priority == 'tag' and $get_attributes) {
if (isset ($current[$tag . '_attr'])) {
$current[$tag]['0_attr'] = $current[$tag . '_attr'];
unset ($current[$tag . '_attr']);
}
if ($attributes_data) {
$current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;
}
}
$repeated_tag_index[$tag . '_' . $level]++; //0 and 1 index is already taken
}
}
} elseif ($type == 'close') {
$current = & $parent[$level -1];
}
}
return ($xml_array);
}
function migrate_rrd_format($rrdoldxml, $rrdnewxml) { function migrate_rrd_format($rrdoldxml, $rrdnewxml) {
if(!file_exists("/tmp/rrd_notice_sent.txt")) { if(!file_exists("/tmp/rrd_notice_sent.txt")) {
$_gb = exec("echo 'Converting RRD configuration to new format. This might take a bit...' | wall"); $_gb = exec("echo 'Converting RRD configuration to new format. This might take a bit...' | wall");
......
...@@ -29,13 +29,11 @@ ...@@ -29,13 +29,11 @@
require_once("guiconfig.inc"); require_once("guiconfig.inc");
require_once("services.inc"); require_once("services.inc");
require_once("pfsense-utils.inc");
require_once("system.inc"); require_once("system.inc");
$serviceproviders_xml = "/usr/local/opnsense/contrib/mobile-broadband-provider-info/serviceproviders.xml"; $serviceproviders_xml = "/usr/local/opnsense/contrib/mobile-broadband-provider-info/serviceproviders.xml";
$serviceproviders_contents = file_get_contents($serviceproviders_xml); $serviceproviders_contents = file_get_contents($serviceproviders_xml);
$serviceproviders_attr = xml2array($serviceproviders_contents,1,"attr"); $serviceproviders_attr = xml2array($serviceproviders_contents,1,"attr");
$serviceproviders = &$serviceproviders_attr['serviceproviders']['country']; $serviceproviders = &$serviceproviders_attr['serviceproviders']['country'];
function get_country_providers($country) function get_country_providers($country)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment