Commit 07685b68 authored by Ad Schellevis's avatar Ad Schellevis

(fw, alias) refactor update_alias_names_upon_change to support nested alias...

(fw, alias) refactor update_alias_names_upon_change to support nested alias renaming, closes https://github.com/opnsense/core/issues/1028
parent 42ae8081
......@@ -131,36 +131,48 @@ function download_file($url, $destination, $verify_ssl = false, $connect_timeout
return ($http_code == 200);
}
function update_alias_names_upon_change($section, $field, $new_alias_name, $origname)
function update_alias_names_upon_change($section, $field, $new_alias_name, $origname, $field_separator=null)
{
global $config;
if (!$origname) {
return;
}
$sectionref = &$config;
foreach($section as $sectionname) {
if (is_array($sectionref) && isset($sectionref[$sectionname])) {
$sectionref = &$sectionref[$sectionname];
} else {
return;
if (!empty($origname) && !empty($new_alias_name)) {
// find section, return if not found
$sectionref = &$config;
foreach ($section as $sectionname) {
if (!empty($sectionref[$sectionname]) && is_array($sectionref[$sectionname])) {
$sectionref = &$sectionref[$sectionname];
} else {
return;
}
}
}
if (is_array($sectionref)) {
// traverse all found sections
foreach($sectionref as $itemkey => $item) {
$fieldfound = true;
// locate field within structure
$fieldref = &$sectionref[$itemkey];
foreach($field as $fieldname) {
if (is_array($fieldref) && isset($fieldref[$fieldname])) {
if (!empty($fieldref[$fieldname])) {
$fieldref = &$fieldref[$fieldname];
} else {
$fieldfound = false;
unset($fieldref);
break;
}
}
if ($fieldfound && $fieldref == $origname) {
$fieldref = $new_alias_name;
// if field is found, check and replace
if (isset($fieldref) && !is_array($fieldref)) {
if ($fieldref == $origname) {
$fieldref = $new_alias_name;
} elseif ($field_separator != null) {
// field contains more then one value
$parts = explode($field_separator, $fieldref);
foreach ($parts as &$part) {
if ($part == $origname) {
$part = $new_alias_name;
}
}
$new_field_value = implode($field_separator, $parts);
if ($new_field_value != $fieldref) {
$fieldref = $new_field_value;
}
}
}
}
}
......
......@@ -238,7 +238,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'GET') {
update_alias_names_upon_change(array('nat', 'advancedoutbound', 'rule'), array('dstport'), $pconfig['name'], $origname);
update_alias_names_upon_change(array('nat', 'advancedoutbound', 'rule'), array('target'), $pconfig['name'], $origname);
// Alias in an alias
update_alias_names_upon_change(array('aliases', 'alias'), array('address'), $pconfig['name'], $origname);
update_alias_names_upon_change(array('aliases', 'alias'), array('address'), $pconfig['name'], $origname, ' ');
}
......
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