From cb45585d92144a65ff25bc1109cde4cbf60913de Mon Sep 17 00:00:00 2001 From: paulr Date: Thu, 6 Oct 2011 02:47:28 +1100 Subject: [PATCH] moved interfaces ui into a seperate php file. implemented the messages interface (for displaying stuff to users) --- libcbfwr/comms.php | 2 + libcbfwr/config.php | 7 + libcbfwr/fwui.php | 199 -------------------------------- libcbfwr/interfaceui.php | 288 ++++++++++++++++++++++++++++++++++++++++++++++ libcbfwr/lib.php | 2 +- libcbfwr/web.php | 39 ++++++- 6 files changed, 336 insertions(+), 201 deletions(-) create mode 100644 libcbfwr/interfaceui.php diff --git a/libcbfwr/comms.php b/libcbfwr/comms.php index 8e7ed32..7486150 100644 --- a/libcbfwr/comms.php +++ b/libcbfwr/comms.php @@ -52,6 +52,8 @@ class Comms { // type = 4 for merged running config cache // type = 5 for merged web config cache + + // type = 6 for "messages" that appear on the pages function putConfig($config, $type=0) { global $MESSAGE_KEY, $LOCKING_KEY, $STORE_KEY, $CONF_STORE_SIZE; diff --git a/libcbfwr/config.php b/libcbfwr/config.php index 2f6ce0b..70878f5 100644 --- a/libcbfwr/config.php +++ b/libcbfwr/config.php @@ -163,6 +163,7 @@ class Config { } echo "process command ".$expl[0]."\n"; + // TODO: when dealing with descriptions, we need to deal with a null description (i.e. delete the description) // find a description $description = null; $hasdescription = false; @@ -186,6 +187,12 @@ class Config { $expl = $expl_r; } + // this is a special case for debugging + if($expl[0] == "hold") { + error_log("command is $line"); + return 0; + } + $delete = false; // check for delete on the line if($expl[0] == "delete") { diff --git a/libcbfwr/fwui.php b/libcbfwr/fwui.php index adc9541..e19e9a4 100644 --- a/libcbfwr/fwui.php +++ b/libcbfwr/fwui.php @@ -217,205 +217,6 @@ function CBFWRoutingPageDisplay() echo "

Routing

"; } -function CBFWInterfacesPage($urls) -{ - if(isset($urls[1])) { - switch($urls[1]) { - case "edit": - CBFWpageBuilder(null, "CBFWInterfacesPageEdit", null, null, $urls[2]); - break; - case "change": - CBFWInterfacesChange(); - break; - default: - CBFWpageBuilder(null, "CBFWInterfacesPageDisplay"); - } - } else { - error_log("main interface page"); - CBFWpageBuilder(null, "CBFWInterfacesPageDisplay"); - } -} - -function CBFWInterfacesChange() -{ - global $BASE_URL, $MENU_ITEMS; - $comms = new Comms(); - - header("Location: $BASE_URL/interfaces"); -} - -function CBFWInterfacesPageEdit($urls) -{ - global $BASE_URL, $MENU_ITEMS; - $interface = $urls; - - $comms = new Comms(); - - $config = getWebUserConfig(); - $boot_config = $comms->getConfig(1); - - if(isset($config["interface"]["dev"]["$interface"]["name"])) $name = $config["interface"]["dev"]["$interface"]["name"]; - else $name = ""; - - $extra2 = ""; - if(file_exists("/sys/class/net/$interface/address")) { - $extra2 = trim(file_get_contents("/sys/class/net/$interface/address")); - } - - $extra = ""; - if(isset($boot_config["hardware"]["netdev"]["$interface"]["hwaddress"])) { - $hwaddr = $boot_config["hardware"]["netdev"]["$interface"]["hwaddress"]; - $extra = " Original address: $hwaddr"; - if($hwaddr != $extra2 && $extra2 != "") { - $extra .= " Current Address: $hwaddr"; - } - } - if(isset($config["interface"]["dev"]["$interface"]["hwaddress"])) $hwaddr = $config["interface"]["dev"]["$interface"]["hwaddress"]; - - if(isset($config["interface"]["dev"]["$interface"]["zone"])) $zone = $config["interface"]["dev"]["$interface"]["zone"]; - - if(isset($config["interface"]["dev"]["$interface"]["address4"])) $ip4 = $config["interface"]["dev"]["$interface"]["address4"]; - - if(isset($config["interface"]["dev"]["$interface"]["address6"])) $ip6 = $config["interface"]["dev"]["$interface"]["address6"]; - - if(isset($boot_config["hardware"]["netdev"]["$interface"]["speed"])) $speed = $boot_config["hardware"]["netdev"]["$interface"]["speed"]; - if(isset($boot_config["hardware"]["netdev"]["$interface"]["duplex"])) $duplex = $boot_config["hardware"]["netdev"]["$interface"]["duplex"]; - if(isset($config["interface"]["dev"]["$interface"]["speed"])) $speed = $config["interface"]["dev"]["$interface"]["speed"]; - if(isset($config["interface"]["dev"]["$interface"]["duplex"])) $duplex = $config["interface"]["dev"]["$interface"]["duplex"]; - - echo "

Edit Interface $interface


"; - echo "
"; - echo ""; - echo ""; - echo ""; - echo ""; - echo ""; - echo ""; - echo ""; - echo ""; - echo "
Name
HW Address$extra
Zone
IPv4 Address
IPv6 Address
Speed
Duplex
"; - echo "
"; - //echo "Back"; - echo "
"; -} - -function CBFWInterfacesPageDisplay() -{ - global $BASE_URL, $MENU_ITEMS; - $comms = new Comms(); - - $config = getWebUserConfig(); - $boot_config = $comms->getConfig(1); - - // at the top, we put in the creation bits - echo "

Create Interface

"; - echo "
  • Link Aggregation Group (bonded/teamed interfaces)
    "; - echo "
  • VLAN Tagged Sub Interfaces
    "; - echo "
  • Bridged Interfaces
    "; - echo "
    "; - - // now build an interface table - $table = array(); - foreach($boot_config["hardware"]["netdev"] as $key=>$val) { - $ename = $key; - $table[$key]["realname"] = $val["name"]; - $table[$key]["name"] = $val["name"]; - - - // driver - if(isset($val["driver"])) $table[$key]["type"] = $val["driver"]; - else $table[$key]["type"] = "Unknown"; - - // hw address - if(isset($val["hwaddress"])) $table[$key]["hwaddress"] = $val["hwaddress"]; - - // current mtu - if(isset($val["mtu"])) $table[$key]["mtu"] = $val["mtu"]; - - // speed - if(isset($val["speed"])) $table[$key]["speed"] = $val["speed"]; - else $table[$key]["speed"] = "default"; - - // duplex - if(isset($val["duplex"])) $table[$key]["duplex"] = $val["duplex"]; - else $table[$key]["duplex"] = "default"; - } - - foreach($config["interface"]["dev"] as $key => $val) { - // TODO construction zone - error_log("doing $key"); - if(isset($val["name"])) $table[$key]["name"] = $val["name"]; - if(isset($val["address4"])) $table[$key]["address4"] = $val["address4"]; - if(isset($val["address6"])) $table[$key]["address6"] = $val["address6"]; - if(isset($val["description"])) $table[$key]["description"] = $val["description"]; - if(isset($val["zone"])) $table[$key]["zone"] = $val["zone"]; - } - - - echo "

    Interfaces

    "; - foreach($table as $key =>$val) { - if($val["realname"] == $val["name"]) $name = $val["name"]; - else $name = $val["name"]." (".$val["realname"].")"; - - // zone - if(isset($val["zone"])) { - if($val["zone"] == null) $zone = "-"; - else $zone = $val["zone"]; - } else $zone = "-"; - - // mtu - if(isset($val["mtu"])) { - $mtu = $val["mtu"]; - } else $mtu = "-"; - - // driver type - if(isset($val["type"])) { - $type = $val["type"]; - } else $type = "-"; - - // hardware address - if(isset($val["hwaddress"])) { - $mac = $val["hwaddress"]; - } else $mac = "-"; - - // speed and duplex - if(isset($val["duplex"])) { - if($val["duplex"] == null) $dup = "default"; - else $dup = $val["duplex"]; - } //else $dup = "default"; // TODO: needs to go away in fav of hardware setting - - if(isset($val["speed"])) { - if($val["speed"] == null) $spd = "default"; - else $spd = $val["speed"]; - } //else $spd = "default"; // TODO: needs to go away inf av of hardware setting - - $spanddu = "$spd/$dup"; - - // network address - if(isset($val["address4"]) && isset($val["address6"])) { - $address = $val["address4"]."
    ".$val["address6"]; - } else if(isset($val["address4"])) { - $address = $val["address4"]; - } else if(isset($val["address6"])) { - $address = $val["address6"]; - } else { - $address = "-"; - } - - if(isset($val["description"])) $desc = $val["description"]; - else $desc = "-"; - - echo ""; - } - echo "
    DeviceZoneTypeMAC AddressAddressMTUSpeed/DuplexDescriptionControl
    $name$zone$type$mac$address$mtu$spanddu$descEdit
    "; - - // to test - echo "config:
    ";
    -	print_r($config);
    -	echo "
    "; -} - function CBFWObjectsPage($urls) { $comms = new Comms(); diff --git a/libcbfwr/interfaceui.php b/libcbfwr/interfaceui.php new file mode 100644 index 0000000..379314d --- /dev/null +++ b/libcbfwr/interfaceui.php @@ -0,0 +1,288 @@ +getConfig(1); + + // set all to null + $hwaddr = null; + $zone = null; + $ip4 = null; + $ip6 = null; + $desc = null; + $speed = null; + $duplex = null; + + if(isset($config["interface"]["dev"]["$interface"]["name"])) $name = $config["interface"]["dev"]["$interface"]["name"]; + else $name = null; + + + $extra2 = ""; + if(file_exists("/sys/class/net/$interface/address")) { + $extra2 = trim(file_get_contents("/sys/class/net/$interface/address")); + } + + $extra = ""; + if(isset($boot_config["hardware"]["netdev"]["$interface"]["hwaddress"])) { + $hwaddr = $boot_config["hardware"]["netdev"]["$interface"]["hwaddress"]; + $extra = " Original address: $hwaddr"; + if($hwaddr != $extra2 && $extra2 != "") { + $extra .= " Current Address: $hwaddr"; + } + } + + if(isset($config["interface"]["dev"]["$interface"]["hwaddress"])) $hwaddr = $config["interface"]["dev"]["$interface"]["hwaddress"]; + if(isset($config["interface"]["dev"]["$interface"]["zone"])) $zone = $config["interface"]["dev"]["$interface"]["zone"]; + if(isset($config["interface"]["dev"]["$interface"]["address4"])) $ip4 = $config["interface"]["dev"]["$interface"]["address4"]; + if(isset($config["interface"]["dev"]["$interface"]["address6"])) $ip6 = $config["interface"]["dev"]["$interface"]["address6"]; + if(isset($config["interface"]["dev"]["$interface"]["description"])) $desc = $config["interface"]["dev"]["$interface"]["description"]; + if(isset($config["interface"]["dev"]["$interface"]["speed"])) $speed = $config["interface"]["dev"]["$interface"]["speed"]; + if(isset($config["interface"]["dev"]["$interface"]["duplex"])) $duplex = $config["interface"]["dev"]["$interface"]["duplex"]; + + $newzone = $_REQUEST["zone"]; + $newhwaddr = $_REQUEST["hwaddr"]; + $newaddr4 = $_REQUEST["ipv4addr"]; + $newaddr6 = $_REQUEST["ipv6addr"]; + $newname = $_REQUEST["name"]; + $newspeed = $_REQUEST["speed"]; + $newduplex = $_REQUEST["duplex"]; + $newdesc = $_REQUEST["description"]; + + // first, check the zone + error_log("comparing zone, $zone to $newzone for $interface"); + if($zone == null && $newzone != "-") { + // we changed from no zone to some zone + error_log("modify interface zone to $newzone for $interface"); + addWebUserChange("hold modify interface $interface zone $newzone"); + } else if($zone != null && $newzone != "-" && $zone != $newzone) { + error_log("modify interface zone from $zone to $newzone for $interface"); + addWebUserChange("hold modify interface $interface zone $newzone"); + } else if($newzone == "-" && $zone != null) { + error_log("remove interface zone from $zone for $interface"); + addWebUserChange("hold delete interface $interface zone $zone"); + } + + // next, check the description + if($newdesc != "" && $desc == null) { + addWebUserChange("hold modify interface $interface # $newdesc"); + } else if($desc != null && $newdesc != "" && $desc != $newdesc) { + addWebUserChange("hold modify interface $interface # $newdesc"); + } else if($desc != null && $newdesc == "") { + addWebUserChange("hold modify interface $interface #"); + } + + header("Location: $BASE_URL/interfaces"); +} + +function CBFWInterfacesPageEdit($urls) +{ + global $BASE_URL, $MENU_ITEMS; + $interface = $urls; + + $comms = new Comms(); + + $config = getWebUserConfig(); + $boot_config = $comms->getConfig(1); + + $hwaddr = null; + $zone = null; + $ip4 = null; + $ip6 = null; + $speed = null; + $duplex = null; + $desc = null; + + if(isset($config["interface"]["dev"]["$interface"]["name"])) $name = $config["interface"]["dev"]["$interface"]["name"]; + else $name = ""; + + $extra2 = ""; + if(file_exists("/sys/class/net/$interface/address")) { + $extra2 = trim(file_get_contents("/sys/class/net/$interface/address")); + } + + $extra = ""; + if(isset($boot_config["hardware"]["netdev"]["$interface"]["hwaddress"])) { + $hwaddr = $boot_config["hardware"]["netdev"]["$interface"]["hwaddress"]; + $extra = " Original address: $hwaddr"; + if($hwaddr != $extra2 && $extra2 != "") { + $extra .= " Current Address: $hwaddr"; + } + } + + if(isset($config["interface"]["dev"]["$interface"]["hwaddress"])) $hwaddr = $config["interface"]["dev"]["$interface"]["hwaddress"]; + if(isset($config["interface"]["dev"]["$interface"]["zone"])) $zone = $config["interface"]["dev"]["$interface"]["zone"]; + if(isset($config["interface"]["dev"]["$interface"]["address4"])) $ip4 = $config["interface"]["dev"]["$interface"]["address4"]; + if(isset($config["interface"]["dev"]["$interface"]["address6"])) $ip6 = $config["interface"]["dev"]["$interface"]["address6"]; + if(isset($config["interface"]["dev"]["$interface"]["description"])) $desc = $config["interface"]["dev"]["$interface"]["description"]; + if(isset($boot_config["hardware"]["netdev"]["$interface"]["speed"])) $speed = $boot_config["hardware"]["netdev"]["$interface"]["speed"]; + if(isset($boot_config["hardware"]["netdev"]["$interface"]["duplex"])) $duplex = $boot_config["hardware"]["netdev"]["$interface"]["duplex"]; + if(isset($config["interface"]["dev"]["$interface"]["speed"])) $speed = $config["interface"]["dev"]["$interface"]["speed"]; + if(isset($config["interface"]["dev"]["$interface"]["duplex"])) $duplex = $config["interface"]["dev"]["$interface"]["duplex"]; + + echo "

    Edit Interface $interface


    "; + echo "
    "; + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + echo "
    Name
    Description
    HW Address$extra
    Zone"; + echo ""; + echo "
    IPv4 Address
    IPv6 Address
    Speed
    Duplex
    "; + echo "
    "; + //echo "Back"; + echo "
    "; +} + +function CBFWInterfacesPageDisplay() +{ + global $BASE_URL, $MENU_ITEMS; + $comms = new Comms(); + + $config = getWebUserConfig(); + $boot_config = $comms->getConfig(1); + + // at the top, we put in the creation bits + echo "

    Create Interface

    "; + echo "
  • Link Aggregation Group (bonded/teamed interfaces)
    "; + echo "
  • VLAN Tagged Sub Interfaces
    "; + echo "
  • Bridged Interfaces
    "; + echo "
    "; + + // now build an interface table + $table = array(); + foreach($boot_config["hardware"]["netdev"] as $key=>$val) { + $ename = $key; + $table[$key]["realname"] = $val["name"]; + $table[$key]["name"] = $val["name"]; + + + // driver + if(isset($val["driver"])) $table[$key]["type"] = $val["driver"]; + else $table[$key]["type"] = "Unknown"; + + // hw address + if(isset($val["hwaddress"])) $table[$key]["hwaddress"] = $val["hwaddress"]; + + // current mtu + if(isset($val["mtu"])) $table[$key]["mtu"] = $val["mtu"]; + + // speed + if(isset($val["speed"])) $table[$key]["speed"] = $val["speed"]; + else $table[$key]["speed"] = "default"; + + // duplex + if(isset($val["duplex"])) $table[$key]["duplex"] = $val["duplex"]; + else $table[$key]["duplex"] = "default"; + } + + foreach($config["interface"]["dev"] as $key => $val) { + // TODO construction zone + error_log("doing $key"); + if(isset($val["name"])) $table[$key]["name"] = $val["name"]; + if(isset($val["address4"])) $table[$key]["address4"] = $val["address4"]; + if(isset($val["address6"])) $table[$key]["address6"] = $val["address6"]; + if(isset($val["description"])) $table[$key]["description"] = $val["description"]; + if(isset($val["zone"])) $table[$key]["zone"] = $val["zone"]; + } + + + echo "

    Interfaces

    "; + foreach($table as $key =>$val) { + if($val["realname"] == $val["name"]) $name = $val["name"]; + else $name = $val["name"]." (".$val["realname"].")"; + + // zone + if(isset($val["zone"])) { + if($val["zone"] == null) $zone = "-"; + else $zone = $val["zone"]; + } else $zone = "-"; + + // mtu + if(isset($val["mtu"])) { + $mtu = $val["mtu"]; + } else $mtu = "-"; + + // driver type + if(isset($val["type"])) { + $type = $val["type"]; + } else $type = "-"; + + // hardware address + if(isset($val["hwaddress"])) { + $mac = $val["hwaddress"]; + } else $mac = "-"; + + // speed and duplex + if(isset($val["duplex"])) { + if($val["duplex"] == null) $dup = "default"; + else $dup = $val["duplex"]; + } //else $dup = "default"; // TODO: needs to go away in fav of hardware setting + + if(isset($val["speed"])) { + if($val["speed"] == null) $spd = "default"; + else $spd = $val["speed"]; + } //else $spd = "default"; // TODO: needs to go away inf av of hardware setting + + $spanddu = "$spd/$dup"; + + // network address + if(isset($val["address4"]) && isset($val["address6"])) { + $address = $val["address4"]."
    ".$val["address6"]; + } else if(isset($val["address4"])) { + $address = $val["address4"]; + } else if(isset($val["address6"])) { + $address = $val["address6"]; + } else { + $address = "-"; + } + + if(isset($val["description"])) $desc = $val["description"]; + else $desc = "-"; + + echo ""; + } + echo "
    DeviceZoneTypeMAC AddressAddressMTUSpeed/DuplexDescriptionControl
    $name$zone$type$mac$address$mtu$spanddu$descEdit
    "; + + // to test + echo "config:
    ";
    +	print_r($config);
    +	echo "
    "; +} + +?> \ No newline at end of file diff --git a/libcbfwr/lib.php b/libcbfwr/lib.php index 67bf0e3..55a25a9 100644 --- a/libcbfwr/lib.php +++ b/libcbfwr/lib.php @@ -26,7 +26,7 @@ require_once("libcbfwr/config.php"); require_once("libcbfwr/comms.php"); require_once("libcbfwr/web.php"); require_once("libcbfwr/fwui.php"); - +require_once("libcbfwr/interfaceui.php"); function glcas_pluginLoader($path="") { diff --git a/libcbfwr/web.php b/libcbfwr/web.php index 6f97675..289fe19 100644 --- a/libcbfwr/web.php +++ b/libcbfwr/web.php @@ -89,6 +89,8 @@ function CBFBuildHomePage($urls) $comms = new Comms(); + + CBFWAddMessage("1", "i am a message"); echo "
    ";
     	print_r($comms->getConfig(0));
     	echo "\n\n\n";
    @@ -132,12 +134,47 @@ function gcam($m, $s)
     
     function CBFWAddMessage($messagelev, $message)
     {
    +	$comms = new Comms();
    +	$msgs = $comms->getConfig(6);
    +	$cnum = 0;
    +	if($msgs != false) {
    +		foreach($msgs as $key=>$val) {
    +			if($key >= $cnum) $cnum = $key+1;
    +		}
    +	} else {
    +		$msgs = array();
    +	}
    +	$msgs[$cnum]["timestamp"] = time();
    +	$msgs[$cnum]["level"] = $messagelev;
    +	$msgs[$cnum]["message"] = $message;
     	
    +	$comms->putConfig($msgs, 6);
     }
     
     function CBFWMessageBuilder()
     {
    -	echo "message";
    +	// show only 10 messages on this page
    +	$maxshow = 6;
    +	
    +	$comms = new Comms();
    +	
    +	$msgs = $comms->getConfig(6);
    +	if($msgs != false) {
    +		krsort($msgs);
    +	}
    +	$show = 0;
    +	if(count($msgs) > 0) {
    +		echo "";
    +		foreach($msgs as $key => $val) {
    +			$datetime = $val["timestamp"];
    +			$sev = $val["level"];
    +			$message = $val["message"];
    +			echo "";
    +			$show++;
    +			if($show >= $maxshow) break;
    +		}
    +		echo "
    $datetime$sev$message
    "; + } } function CBFWpageBuilder($bodyClass, $bodyFunction, $bodycontent=null, $title="CBFW", $bodyfunctiondata=null) -- 1.7.0.4