MVC & VLan interfaces - properly select the root node for /vlans, // select "any" node within the sructure, which can point to items deeper in the structure than intended.

To limit the impact, make sure to only alter vlans now, eventually we should fix the other models as well as they are using // too (but are less likely to overlap)

(take 2)
This commit is contained in:
Ad Schellevis 2022-03-24 19:36:20 +01:00
parent ad4715a9fc
commit 364ec4782d

View File

@ -321,10 +321,11 @@ abstract class BaseModel
* mountpoints starting with a single /
*/
if (strpos($model_xml->mount, "//") === 0) {
$this->internal_mountpoint = $model_xml->mount;
$src_mountpoint = $model_xml->mount;
} else {
$this->internal_mountpoint = "/opnsense{$model_xml->mount}";
$src_mountpoint = "/opnsense{$model_xml->mount}";
}
$this->internal_mountpoint = $model_xml->mount;
if (!empty($model_xml->version)) {
$this->internal_model_version = (string)$model_xml->version;
@ -336,7 +337,7 @@ abstract class BaseModel
// use an xpath expression to find the root of our model in the config.xml file
// if found, convert the data to a simple structure (or create an empty array)
$tmp_config_data = $internalConfigHandle->xpath($this->internal_mountpoint);
$tmp_config_data = $internalConfigHandle->xpath($src_mountpoint);
if ($tmp_config_data->length > 0) {
$config_array = simplexml_import_dom($tmp_config_data->item(0));
} else {
@ -490,17 +491,11 @@ abstract class BaseModel
{
// calculate root node from mountpoint
$xml_root_node = "";
$str_parts = explode("/", str_replace("//", "/", $this->internal_mountpoint));
for ($i = 0; $i < count($str_parts); $i++) {
if ($str_parts[$i] != "") {
$xml_root_node .= "<" . $str_parts[$i] . ">";
}
foreach (explode("/", ltrim($this->internal_mountpoint, "/")) as $part) {
$xml_root_node .= "<" . $part . ">";
}
for ($i = count($str_parts) - 1; $i >= 0; $i--) {
if ($str_parts[$i] != "") {
$xml_root_node .= "</" . $str_parts[$i] . ">";
}
foreach (array_reverse(explode("/", ltrim($this->internal_mountpoint, "/"))) as $part) {
$xml_root_node .= "</" . $part . ">";
}
$xml = new SimpleXMLElement($xml_root_node);
@ -532,14 +527,11 @@ abstract class BaseModel
// find parent of mountpoint (create if it doesn't exists)
$target_node = $config_xml;
$str_parts = explode("/", str_replace("//", "/", $this->internal_mountpoint));
for ($i = 0; $i < count($str_parts); $i++) {
if ($str_parts[$i] != "") {
if (count($target_node->xpath($str_parts[$i])) == 0) {
$target_node = $target_node->addChild($str_parts[$i]);
} else {
$target_node = $target_node->xpath($str_parts[$i])[0];
}
foreach (explode("/", ltrim($this->internal_mountpoint, "/")) as $part) {
if (count($target_node->xpath($part)) == 0) {
$target_node = $target_node->addChild($part);
} else {
$target_node = $target_node->xpath($part)[0];
}
}