From 07685b68aa5363f499f2c8bb81eb7a7b8cb7ee0c Mon Sep 17 00:00:00 2001 From: Ad Schellevis Date: Wed, 20 Jul 2016 14:20:19 +0200 Subject: [PATCH] (fw, alias) refactor update_alias_names_upon_change to support nested alias renaming, closes https://github.com/opnsense/core/issues/1028 --- src/etc/inc/filter.inc | 50 +++++++++++++++++++------------ src/www/firewall_aliases_edit.php | 2 +- 2 files changed, 32 insertions(+), 20 deletions(-) diff --git a/src/etc/inc/filter.inc b/src/etc/inc/filter.inc index 8dc3bd400..ef6f3572a 100644 --- a/src/etc/inc/filter.inc +++ b/src/etc/inc/filter.inc @@ -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; + } + } } } } diff --git a/src/www/firewall_aliases_edit.php b/src/www/firewall_aliases_edit.php index 8daa1559c..8b7e1d06e 100644 --- a/src/www/firewall_aliases_edit.php +++ b/src/www/firewall_aliases_edit.php @@ -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, ' '); }