From 4438ef1df40ebfb2ee692cecf6a6ec52c6cbf8a0 Mon Sep 17 00:00:00 2001 From: Ad Schellevis Date: Mon, 17 Aug 2015 13:13:15 +0000 Subject: [PATCH] (legacy) refactor firewall_nat.php --- src/www/firewall_nat.php | 704 ++++++++++++++++++++------------------- 1 file changed, 361 insertions(+), 343 deletions(-) diff --git a/src/www/firewall_nat.php b/src/www/firewall_nat.php index 92a5a62fb..7335f05b0 100644 --- a/src/www/firewall_nat.php +++ b/src/www/firewall_nat.php @@ -33,20 +33,6 @@ require_once("guiconfig.inc"); require_once("interfaces.inc"); require_once("filter.inc"); -/****f* legacy/have_natpfruleint_access - * NAME - * have_natpfruleint_access - * INPUTS - * none - * RESULT - * returns true if user has access to edit a specific firewall nat port forward interface - ******/ -function have_natpfruleint_access($if) { - $security_url = "firewall_nat_edit.php?if=". strtolower($if); - if(isAllowedPage($security_url)) - return true; - return false; -} /****f* itemid/delete_id (duplicate to remove itemid.inc) * NAME @@ -83,362 +69,394 @@ function delete_id($id, &$array){ } - - -if (!is_array($config['nat']['rule'])) - $config['nat']['rule'] = array(); +if (!is_array($config['nat']['rule'])) { + $config['nat']['rule'] = array(); +} $a_nat = &$config['nat']['rule']; +if ($_SERVER['REQUEST_METHOD'] === 'POST') { + $pconfig = $_POST; + if (isset($pconfig['id']) && isset($a_nat[$pconfig['id']])) { + // id found and valid + $id = $pconfig['id']; + } + if (isset($pconfig['apply'])) { + write_config(); + filter_configure(); + $savemsg = get_std_save_message(); + clear_subsystem_dirty('natconf'); + clear_subsystem_dirty('filter'); + } elseif (isset($pconfig['act']) && $pconfig['act'] == 'del' && isset($id)) { + // delete nat rule and associated rule if it exists + if (isset($a_nat[$id]['associated-rule-id'])) { + delete_id($a_nat[$id]['associated-rule-id'], $config['filter']['rule']); + mark_subsystem_dirty('filter'); + } + unset($a_nat[$id]); -/* if a custom message has been passed along, lets process it */ -if ($_GET['savemsg']) - $savemsg = $_GET['savemsg']; - -if ($_POST) { - - $pconfig = $_POST; - - if ($_POST['apply']) { - - write_config(); - - $retval = 0; - - $retval |= filter_configure(); - $savemsg = get_std_save_message($retval); - - if ($retval == 0) { - clear_subsystem_dirty('natconf'); - clear_subsystem_dirty('filter'); - } - - } -} - -if ($_GET['act'] == "del") { - if ($a_nat[$_GET['id']]) { - - if (isset($a_nat[$_GET['id']]['associated-rule-id'])) { - delete_id($a_nat[$_GET['id']]['associated-rule-id'], $config['filter']['rule']); - $want_dirty_filter = true; - } - unset($a_nat[$_GET['id']]); - - if (write_config()) { - mark_subsystem_dirty('natconf'); - if ($want_dirty_filter) - mark_subsystem_dirty('filter'); - } - header("Location: firewall_nat.php"); - exit; - } -} - -if (isset($_POST['del_x'])) { - /* delete selected rules */ - if (is_array($_POST['rule']) && count($_POST['rule'])) { - foreach ($_POST['rule'] as $rulei) { - $target = $rule['target']; - // Check for filter rule associations - if (isset($a_nat[$rulei]['associated-rule-id'])){ - delete_id($a_nat[$rulei]['associated-rule-id'], $config['filter']['rule']); - - mark_subsystem_dirty('filter'); - } - unset($a_nat[$rulei]); - } - if (write_config()) - mark_subsystem_dirty('natconf'); - header("Location: firewall_nat.php"); - exit; - } - -} else { - /* yuck - IE won't send value attributes for image buttons, while Mozilla does - so we use .x/.y to find move button clicks instead... */ - unset($movebtn); - foreach ($_POST as $pn => $pd) { - if (preg_match("/move_(\d+)_x/", $pn, $matches)) { - $movebtn = $matches[1]; - break; - } + if (write_config()) { + mark_subsystem_dirty('natconf'); + } + header("Location: firewall_nat.php"); + exit; + } elseif (isset($pconfig['act']) && $pconfig['act'] == 'del_x' && isset($pconfig['rule']) && count($pconfig['rule']) > 0) { + /* delete selected rules */ + foreach ($pconfig['rule'] as $rulei) { + if (isset($a_nat[$rulei])) { + $target = $rule['target']; + // Check for filter rule associations + if (isset($a_nat[$rulei]['associated-rule-id'])){ + delete_id($a_nat[$rulei]['associated-rule-id'], $config['filter']['rule']); + mark_subsystem_dirty('filter'); + } + unset($a_nat[$rulei]); + } + } + if (write_config()) { + mark_subsystem_dirty('natconf'); } - /* move selected rules before this rule */ - if (isset($movebtn) && is_array($_POST['rule']) && count($_POST['rule'])) { - $a_nat_new = array(); + header("Location: firewall_nat.php"); + exit; + } elseif ( isset($pconfig['act']) && $pconfig['act'] == 'move') { + // move records + if (isset($pconfig['rule']) && count($pconfig['rule']) > 0) { + // if rule not set/found, move to end + if (!isset($id)) { + $id = count($a_nat); + } + $a_nat_new = array(); - /* copy all rules < $movebtn and not selected */ - for ($i = 0; $i < $movebtn; $i++) { - if (!in_array($i, $_POST['rule'])) - $a_nat_new[] = $a_nat[$i]; - } + /* copy all rules < $id and not selected */ + for ($i = 0; $i < $id; $i++) { + if (!in_array($i, $pconfig['rule'])) { + $a_nat_new[] = $a_nat[$i]; + } + } - /* copy all selected rules */ - for ($i = 0; $i < count($a_nat); $i++) { - if ($i == $movebtn) - continue; - if (in_array($i, $_POST['rule'])) - $a_nat_new[] = $a_nat[$i]; - } + /* copy all selected rules */ + for ($i = 0; $i < count($a_nat); $i++) { + if ($i == $id) + continue; + if (in_array($i, $pconfig['rule'])) { + $a_nat_new[] = $a_nat[$i]; + } + } - /* copy $movebtn rule */ - if ($movebtn < count($a_nat)) - $a_nat_new[] = $a_nat[$movebtn]; + /* copy $id rule */ + if ($id < count($a_nat)) { + $a_nat_new[] = $a_nat[$id]; + } - /* copy all rules > $movebtn and not selected */ - for ($i = $movebtn+1; $i < count($a_nat); $i++) { - if (!in_array($i, $_POST['rule'])) - $a_nat_new[] = $a_nat[$i]; - } - $a_nat = $a_nat_new; - if (write_config()) - mark_subsystem_dirty('natconf'); - header("Location: firewall_nat.php"); - exit; + /* copy all rules > $id and not selected */ + for ($i = $id+1; $i < count($a_nat); $i++) { + if (!in_array($i, $pconfig['rule'])) { + $a_nat_new[] = $a_nat[$i]; + } + } + $a_nat = $a_nat_new; } + if (write_config()) { + mark_subsystem_dirty('natconf'); + } + header("Location: firewall_nat.php"); + exit; + } } $closehead = false; $pgtitle = array(gettext("Firewall"),gettext("NAT"),gettext("Port Forward")); include("head.inc"); +legacy_html_escape_form_data($a_nat); $main_buttons = array( array('label'=>'Add', 'href'=>'firewall_nat_edit.php?after=-1'), ); - - ?> + - - + // link move buttons + $(".act_move").click(function(){ + var id = $(this).attr("id").split('_').pop(-1); + $("#id").val(id); + $("#action").val("move"); + $("#iform").submit(); + }); +}); + -
+ + +" . gettext("You must apply the changes in order for them to take effect."));?>
+ +
+ +
+
+ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + - - " . gettext("You must apply the changes in order for them to take effect."));?>
- - -
- - - - -
- - - - -
    
- - - - - - - - - - - - - - - - - - - - "; - - if (isset($natent['disabled'])) - $textss = ""; - else - $textss = ""; - - $textse = ""; - - /* if user does not have access to edit an interface skip on to the next record */ - if(!have_natpfruleint_access($natent['interface'])) - continue; - ?> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -   - - - - - " class="btn btn-default btn-xs"> - " onclick="return confirm('')" class="btn btn-default btn-xs"> - - ">
    - - - - - - "> - - - - "> -
 
- -
-" onclick="return confirm('')" class="btn btn-primary" value="Delete selected rules"/> -
- - -
-
-
-
-
- - - + // show anti-lockout when enabled + if ($lockout_intf_name !== null && !isset($config['system']['webgui']['noantilockout'])): +?> + + + + + + TCP + * + * + + ', filter_get_antilockout_ports(true));?> + * + * + + + + + + ondblclick="document.location='firewall_nat_edit.php?id=';"> + + + + + + "> + + + + + + + + + + + + + + + + + + + + + + + + +   + + + + + +   + + + + + +   + + + + + +   + + + + + +   + + + + + + +   + + + + + + + " class="act_move btn btn-default btn-xs"> + + + " class="btn btn-default btn-xs"> + + + " data-toggle="tooltip" class="act_delete btn btn-default btn-xs"> + + + "> + + + + + + + + + + + + + + + " class="act_move btn btn-default btn-xs"> + + + + + "> + + " data-toggle="tooltip" class="act_delete btn btn-default btn-xs"> + + + + "> + + + + + + + +   + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +