From 0bc35548aa37d1ec20bc27212507f49bee1a93e1 Mon Sep 17 00:00:00 2001 From: Ad Schellevis Date: Thu, 31 Dec 2015 15:40:56 +0100 Subject: [PATCH] (legacy) refactor interfaces_bridge_edit.php --- src/www/interfaces_bridge_edit.php | 1215 ++++++++++++++-------------- 1 file changed, 609 insertions(+), 606 deletions(-) diff --git a/src/www/interfaces_bridge_edit.php b/src/www/interfaces_bridge_edit.php index 746c7324c..ccf348d1c 100644 --- a/src/www/interfaces_bridge_edit.php +++ b/src/www/interfaces_bridge_edit.php @@ -1,30 +1,30 @@ $bdescr) { - if (substr(get_real_interface($bif), 0, 3) == "gre") - unset($ifacelist[$bif]); +// interface list +$ifacelist = array(); +foreach (get_configured_interface_with_descr() as $bif => $bdescr) { + if (substr(get_real_interface($bif), 0, 3) != "gre") { + $ifacelist[$bif] = $bdescr; + } } -if (is_numericint($_GET['id'])) - $id = $_GET['id']; -if (isset($_POST['id']) && is_numericint($_POST['id'])) - $id = $_POST['id']; -if (isset($id) && $a_bridges[$id]) { - $pconfig['enablestp'] = isset($a_bridges[$id]['enablestp']); - $pconfig['descr'] = $a_bridges[$id]['descr']; - $pconfig['bridgeif'] = $a_bridges[$id]['bridgeif']; - $pconfig['members'] = $a_bridges[$id]['members']; - $pconfig['maxaddr'] = $a_bridges[$id]['maxaddr']; - $pconfig['timeout'] = $a_bridges[$id]['timeout']; - if ($a_bridges[$id]['static']) - $pconfig['static'] = $a_bridges[$id]['static']; - if ($a_bridges[$id]['private']) - $pconfig['private'] = $a_bridges[$id]['private']; - if (isset($a_bridges[$id]['stp'])) - $pconfig['stp'] = $a_bridges[$id]['stp']; - $pconfig['maxage'] = $a_bridges[$id]['maxage']; - $pconfig['fwdelay'] = $a_bridges[$id]['fwdelay']; - $pconfig['hellotime'] = $a_bridges[$id]['hellotime']; - $pconfig['priority'] = $a_bridges[$id]['priority']; - $pconfig['proto'] = $a_bridges[$id]['proto']; - $pconfig['holdcnt'] = $a_bridges[$id]['holdcnt']; - if (!empty($a_bridges[$id]['ifpriority'])) { - $pconfig['ifpriority'] = explode(",", $a_bridges[$id]['ifpriority']); - $ifpriority = array(); - foreach ($pconfig['ifpriority'] as $cfg) { - list ($key, $value) = explode(":", $cfg); - $embprioritycfg[$key] = $value; - foreach ($embprioritycfg as $key => $value) { - $ifpriority[$key] = $value; - } - } - $pconfig['ifpriority'] = $ifpriority; - } - if (!empty($a_bridges[$id]['ifpathcost'])) { - $pconfig['ifpathcost'] = explode(",", $a_bridges[$id]['ifpathcost']); - $ifpathcost = array(); - foreach ($pconfig['ifpathcost'] as $cfg) { - list ($key, $value) = explode(":", $cfg); - $embpathcfg[$key] = $value; - foreach ($embpathcfg as $key => $value) { - $ifpathcost[$key] = $value; - } - } - $pconfig['ifpathcost'] = $ifpathcost; - } - $pconfig['span'] = $a_bridges[$id]['span']; - if (isset($a_bridges[$id]['edge'])) - $pconfig['edge'] = $a_bridges[$id]['edge']; - if (isset($a_bridges[$id]['autoedge'])) - $pconfig['autoedge'] = $a_bridges[$id]['autoedge']; - if (isset($a_bridges[$id]['ptp'])) - $pconfig['ptp'] = $a_bridges[$id]['ptp']; - if (isset($a_bridges[$id]['autoptp'])) - $pconfig['autoptp'] = $a_bridges[$id]['autoptp']; -} - -if ($_POST) { - - unset($input_errors); - $pconfig = $_POST; - - /* input validation */ - $reqdfields = explode(" ", "members"); - $reqdfieldsn = array(gettext("Member Interfaces")); - - do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors); - - if ($_POST['maxage'] && !is_numeric($_POST['maxage'])) - $input_errors[] = gettext("Maxage needs to be an integer between 6 and 40."); - if ($_POST['maxaddr'] && !is_numeric($_POST['maxaddr'])) - $input_errors[] = gettext("Maxaddr needs to be an integer."); - if ($_POST['timeout'] && !is_numeric($_POST['timeout'])) - $input_errors[] = gettext("Timeout needs to be an integer."); - if ($_POST['fwdelay'] && !is_numeric($_POST['fwdelay'])) - $input_errors[] = gettext("Forward Delay needs to be an integer between 4 and 30."); - if ($_POST['hellotime'] && !is_numeric($_POST['hellotime'])) - $input_errors[] = gettext("Hello time for STP needs to be an integer between 1 and 2."); - if ($_POST['priority'] && !is_numeric($_POST['priority'])) - $input_errors[] = gettext("Priority for STP needs to be an integer between 0 and 61440."); - if ($_POST['holdcnt'] && !is_numeric($_POST['holdcnt'])) - $input_errors[] = gettext("Transmit Hold Count for STP needs to be an integer between 1 and 10."); - foreach ($ifacelist as $ifn => $ifdescr) { - if ($_POST[$ifn] <> "" && !is_numeric($_POST[$ifn])) { - $input_errors[] = sprintf(gettext("%s interface priority for STP needs to be an integer between 0 and 240."), $ifdescr); - } - } - $i = 0; - foreach ($ifacelist as $ifn => $ifdescr) { - if ($_POST["{$ifn}{$i}"] <> "" && !is_numeric($_POST["{$ifn}{$i}"])) { - $input_errors[] = sprintf(gettext("%s interface path cost for STP needs to be an integer between 1 and 200000000."), $ifdescr); - } - $i++; - } - - if (!is_array($_POST['members']) || count($_POST['members']) < 2) - $input_errors[] = gettext("You must select at least 2 member interfaces for a bridge."); - - if (is_array($_POST['members'])) { - foreach($_POST['members'] as $ifmembers) { - if (empty($config['interfaces'][$ifmembers])) - $input_errors[] = gettext("A member interface passed does not exist in configuration"); - if (is_array($config['interfaces'][$ifmembers]['wireless']) && - $config['interfaces'][$ifmembers]['wireless']['mode'] != "hostap") - $input_errors[] = gettext("Bridging a wireless interface is only possible in hostap mode."); - if ($_POST['span'] != "none" && $_POST['span'] == $ifmembers) - $input_errors[] = gettext("Span interface cannot be part of the bridge. Remove the span interface from bridge members to continue."); - } - } - - if (!$input_errors) { - $bridge = array(); - $bridge['members'] = implode(',', $_POST['members']); - $bridge['enablestp'] = $_POST['enablestp'] ? true : false; - $bridge['descr'] = $_POST['descr']; - $bridge['maxaddr'] = $_POST['maxaddr']; - $bridge['timeout'] = $_POST['timeout']; - if ($_POST['static']) - $bridge['static'] = implode(',', $_POST['static']); - if ($_POST['private']) - $bridge['private'] = implode(',', $_POST['private']); - if (isset($_POST['stp'])) - $bridge['stp'] = implode(',', $_POST['stp']); - $bridge['maxage'] = $_POST['maxage']; - $bridge['fwdelay'] = $_POST['fwdelay']; - $bridge['hellotime'] = $_POST['hellotime']; - $bridge['priority'] = $_POST['priority']; - $bridge['proto'] = $_POST['proto']; - $bridge['holdcnt'] = $_POST['holdcnt']; - $i = 0; - $ifpriority = ""; - $ifpathcost = ""; - foreach ($ifacelist as $ifn => $ifdescr) { - if ($_POST[$ifn] <> "") { - if ($i > 0) - $ifpriority .= ","; - $ifpriority .= $ifn.":".$_POST[$ifn]; - } - if ($_POST["{$ifn}0"] <> "") { - if ($i > 0) - $ifpathcost .= ","; - $ifpathcost .= $ifn.":".$_POST["{$ifn}0"]; - } - $i++; - } - $bridge['ifpriority'] = $ifpriority; - $bridge['ifpathcost'] = $ifpathcost; - - if ($_POST['span'] != "none") - $bridge['span'] = $_POST['span']; - else - unset($bridge['span']); - if (isset($_POST['edge'])) - $bridge['edge'] = implode(',', $_POST['edge']); - if (isset($_POST['autoedge'])) - $bridge['autoedge'] = implode(',', $_POST['autoedge']); - if (isset($_POST['ptp'])) - $bridge['ptp'] = implode(',', $_POST['ptp']); - if (isset($_POST['autoptp'])) - $bridge['autoptp'] = implode(',', $_POST['autoptp']); - - $bridge['bridgeif'] = $_POST['bridgeif']; - interface_bridge_configure($bridge); - if ($bridge['bridgeif'] == "" || !stristr($bridge['bridgeif'], "bridge")) - $input_errors[] = gettext("Error occurred creating interface, please retry."); - else { - if (isset($id) && $a_bridges[$id]) - $a_bridges[$id] = $bridge; - else - $a_bridges[] = $bridge; - - write_config(); - - $confif = convert_real_interface_to_friendly_interface_name($bridge['bridgeif']); - if ($confif <> "") - interface_configure($confif); - - header("Location: interfaces_bridge.php"); - exit; - } - } +if ($_SERVER['REQUEST_METHOD'] === 'GET') { + // read form data + if (!empty($a_bridges[$_GET['id']])) { + $id = $_GET['id']; + } + // copy fields 1-on-1 + $copy_fields = array('descr', 'bridgeif', 'maxaddr', 'timeout', 'maxage','fwdelay', 'hellotime', 'priority', 'proto', 'holdcnt', 'span'); + foreach ($copy_fields as $fieldname) { + if (isset($a_bridges[$id][$fieldname])) { + $pconfig[$fieldname] = $a_bridges[$id][$fieldname]; + } else { + $pconfig[$fieldname] = null; + } + } + // bool fields + $pconfig['enablestp'] = isset($a_bridges[$id]['enablestp']); + + // simple array fields + $array_fields = array('members', 'stp', 'edge', 'autoedge', 'ptp', 'autoptp', 'static', 'private'); + foreach ($array_fields as $fieldname) { + if (!empty($a_bridges[$id][$fieldname])) { + $pconfig[$fieldname] = explode(',', $a_bridges[$id][$fieldname]); + } else { + $pconfig[$fieldname] = array(); + } + } + + // array key/value sets + if (!empty($a_bridges[$id]['ifpriority'])) { + foreach (explode(",", $a_bridges[$id]['ifpriority']) as $cfg) { + list ($key, $value) = explode(":", $cfg); + $pconfig['ifpriority_'.$key] = $value; + } + } + if (!empty($a_bridges[$id]['ifpathcost'])) { + foreach (explode(",", $a_bridges[$id]['ifpathcost']) as $cfg) { + list ($key, $value) = explode(":", $cfg); + $pconfig['ifpathcost_'.$key] = $value; + } + } +} elseif ($_SERVER['REQUEST_METHOD'] === 'POST') { + // save / validate formdata + if (!empty($a_bridges[$_POST['id']])) { + $id = $_POST['id']; + } + + $input_errors = array(); + $pconfig = $_POST; + + /* input validation */ + $reqdfields = explode(" ", "members"); + $reqdfieldsn = array(gettext("Member Interfaces")); + + do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors); + + if (!empty($pconfig['maxage']) && !is_numeric($pconfig['maxage'])) { + $input_errors[] = gettext("Maxage needs to be an integer between 6 and 40."); + } + if (!empty($pconfig['maxaddr']) && !is_numeric($pconfig['maxaddr'])) { + $input_errors[] = gettext("Maxaddr needs to be an integer."); + } + if (!empty($pconfig['timeout']) && !is_numeric($pconfig['timeout'])) { + $input_errors[] = gettext("Timeout needs to be an integer."); + } + if (!empty($pconfig['fwdelay']) && !is_numeric($pconfig['fwdelay'])) { + $input_errors[] = gettext("Forward Delay needs to be an integer between 4 and 30."); + } + if (!empty($pconfig['hellotime']) && !is_numeric($pconfig['hellotime'])) { + $input_errors[] = gettext("Hello time for STP needs to be an integer between 1 and 2."); + } + if (!empty($pconfig['priority']) && !is_numeric($pconfig['priority'])) { + $input_errors[] = gettext("Priority for STP needs to be an integer between 0 and 61440."); + } + if (!empty($pconfig['holdcnt']) && !is_numeric($pconfig['holdcnt'])) { + $input_errors[] = gettext("Transmit Hold Count for STP needs to be an integer between 1 and 10."); + } + foreach ($ifacelist as $ifn => $ifdescr) { + if (!empty($pconfig['ifpriority_'.$ifn]) && !is_numeric($pconfig['ifpriority_'.$ifn])) { + $input_errors[] = sprintf(gettext("%s interface priority for STP needs to be an integer between 0 and 240."), $ifdescr); + } + if (!empty($pconfig['ifpathcost_'.$ifn]) && !is_numeric($pconfig['ifpathcost_'.$ifn])) { + $input_errors[] = sprintf(gettext("%s interface path cost for STP needs to be an integer between 1 and 200000000."), $ifdescr); + } + } + + if (!is_array($pconfig['members']) || count($_POST['members']) < 2) { + $input_errors[] = gettext("You must select at least 2 member interfaces for a bridge."); + } + + if (is_array($pconfig['members'])) { + foreach($pconfig['members'] as $ifmembers) { + if (empty($config['interfaces'][$ifmembers])) { + $input_errors[] = gettext("A member interface passed does not exist in configuration"); + } + if (!empty($config['interfaces'][$ifmembers]['wireless']['mode']) && $config['interfaces'][$ifmembers]['wireless']['mode'] != "hostap") { + $input_errors[] = gettext("Bridging a wireless interface is only possible in hostap mode."); + } + if ($pconfig['span'] != "none" && $pconfig['span'] == $ifmembers) { + $input_errors[] = gettext("Span interface cannot be part of the bridge. Remove the span interface from bridge members to continue."); + } + } + } + + if (count($input_errors) == 0) { + $bridge = array(); + $bridge['enablestp'] = !empty($pconfig['enablestp']); + // 1 on 1 copy + $copy_fields = array('descr', 'maxaddr', 'timeout', 'bridgeif', 'maxage','fwdelay', 'hellotime', 'priority', 'proto', 'holdcnt'); + foreach ($copy_fields as $fieldname) { + if (isset($pconfig[$fieldname]) && $pconfig[$fieldname] != "") { + $bridge[$fieldname] = $pconfig[$fieldname]; + } else { + $bridge[$fieldname] = null; + } + } + if ($pconfig['span'] != "none") { + $bridge['span'] = $pconfig['span']; + } + // simple array fields + $array_fields = array('members', 'stp', 'edge', 'autoedge', 'ptp', 'autoptp', 'static', 'private'); + foreach ($array_fields as $fieldname) { + if(!empty($pconfig[$fieldname])) { + $bridge[$fieldname] = implode(',', $pconfig[$fieldname]); + } + } + // array key/value sets + $bridge['ifpriority'] = ""; + $bridge['ifpathcost'] = ""; + foreach ($ifacelist as $ifn => $ifdescr) { + if (isset($pconfig['ifpriority_'.$ifn]) && $pconfig['ifpriority_'.$ifn] != "") { + if (!empty($bridge['ifpriority'])) { + $bridge['ifpriority'] .= ','; + } + $bridge['ifpriority'] .= $ifn.":".$pconfig['ifpriority_'.$ifn]; + } + if (isset($pconfig['ifpathcost_'.$ifn]) && $pconfig['ifpathcost_'.$ifn] != "") { + if (!empty($bridge['ifpathcost'])) { + $bridge['ifpathcost'] .= ','; + } + $bridge['ifpathcost'] .= $ifn.":".$pconfig['ifpathcost_'.$ifn]; + } + } + + interface_bridge_configure($bridge); + if ($bridge['bridgeif'] == "" || !stristr($bridge['bridgeif'], "bridge")) { + $input_errors[] = gettext("Error occurred creating interface, please retry."); + } else { + if (isset($id)) { + $a_bridges[$id] = $bridge; + } else { + $a_bridges[] = $bridge; + } + write_config(); + $confif = convert_real_interface_to_friendly_interface_name($bridge['bridgeif']); + if ($confif <> "") { + interface_configure($confif); + } + header("Location: interfaces_bridge.php"); + exit; + } + } } +legacy_html_escape_form_data($pconfig); include("head.inc"); - ?> - -
-
-
- - 0) print_input_errors($input_errors); ?> -
- - -
- -
- -
-

-
- -
- -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- -
- -
- -
-

" />

-
  - - " /> - " onclick="window.location.href=''" /> - - - -
-
-
-
-
-
-
-
- +
+
+
+ 0) print_input_errors($input_errors); ?> +
+
+
+
+ + + + + + + + + + + + + + + + + + + + + +
+ + +   +
+ + +
+ + +
+ " /> +
+
+
+ + + + + +
+
+ + + + + + + +
  + + " /> + " onclick="window.location.href=''" /> + + + +
+
+
+
+
+
+
+