Commit 2f951805 authored by Ad Schellevis's avatar Ad Schellevis Committed by Franco Fichtner

(Config) add toArrayFromFile() for https://github.com/opnsense/core/issues/1097

(cherry picked from commit f96a3fc0)
(cherry picked from commit ab5cb92d)
parent 1ae99ed2
......@@ -71,6 +71,14 @@ function alias_make_table($config)
}
}
/**
* parse config into array and return
*/
function load_config_from_file($filename)
{
return OPNsense\Core\Config::getInstance()->toArrayFromFile($filename, listtags());
}
/****f* config/parse_config
* NAME
* parse_config - Read in config.xml if needed and return $config array
......
......@@ -155,6 +155,18 @@ class Config extends Singleton
return $result;
}
/**
* @param $filename
* @param null $forceList
* @return array|string
*/
public function toArrayFromFile($filename, $forceList = null)
{
$xml = $this->load_from_file($filename);
return $this->toArray($forceList, $xml);
}
/**
* update (reset) config with array structure (backwards compatibility mode)
* @param $source source array structure
......@@ -292,16 +304,18 @@ class Config extends Singleton
}
/**
* Load config file
* load xml config from file
* @param $filename
* @return \SimpleXMLElement
* @throws ConfigException
*/
private function load()
private function load_from_file($filename)
{
// exception handling
if (!file_exists($this->config_file)) {
if (!file_exists($filename)) {
throw new ConfigException('file not found');
}
$xml = file_get_contents($this->config_file);
$xml = file_get_contents($filename);
if (trim($xml) == '') {
throw new ConfigException('empty file');
}
......@@ -309,18 +323,29 @@ class Config extends Singleton
set_error_handler(
function () {
// reset simplexml pointer on parse error.
$this->simplexml = null;
$result = null;
}
);
$this->simplexml = simplexml_load_string($xml);
$result = simplexml_load_string($xml);
if ($this->simplexml == null) {
restore_error_handler();
restore_error_handler();
if ($result == null) {
throw new ConfigException("invalid config xml");
} else {
return $result;
}
}
restore_error_handler();
/**
* Load config file
* @throws ConfigException
*/
private function load()
{
$this->simplexml = null;
$this->statusIsValid = false;
$this->simplexml = $this->load_from_file($this->config_file);
$this->statusIsValid = true;
}
......
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