<?php

////
//
// Read through the comments in this script for configuration guides
// Disclaimer:
// This software is distributed as-is. If it causes your network to catch fire, it's not my fault.
//
// ASAP v0.0.1 - index.php
//
// 06.01.07 Paul Venezia (pvenezia@pvenezia.net)
//


$GLOBALS['pagetitle'] = "Network Access/ASAP Tool";

// Subnets not to allow changes to/from unless admin
$GLOBALS['denysubnets'] = Array ( '172.16.1.0/24',
				  '172.16.96.0/24'	
						 );

// List of routers to query when looking for MAC addresses, since they aren't site-specific
$GLOBALS['routers'] = Array (  	'172.16.1.1',
		    		'172.16.96.1',
		    		'172.16.64.1' );

main();

//
// Functions start
//

function main() {
session_start(); // starting session
require_once('db.inc');

  switch($_REQUEST['action']) {
    case 'chvlan':
	if ($_REQUEST['ip'] != $_SERVER['REMOTE_ADDR']) {
		if (!defined($_SESSION['admin'])) {
			$GLOBALS['error'] = "Unprivileged action. Aborting";
			$GLOBALS['disabled'] = 1;
		}
	}

// Check to see if the request is coming from the target. If it is, then we need to sprinkle
// some special juju around to get the client kicked to another page while the VLAN changed 
// and the port is reset. If not, they get a timeout and a browser error. Probably broken on IE.

	if ($_REQUEST['ip'] == $_SERVER['REMOTE_ADDR']) {
		ob_end_clean();
		header("Connection: close");
		ob_start();

      		header("Location: /jumppage.html");
		#include('jumppage.html');

		$size=ob_get_length();
		header("Content-Length: $size");

		ob_end_flush();
		flush();
	}

	if (!defined($GLOBALS['error'])) {
      initSNMP();
      doCheck();
      getVLANS();
      setVLAN();
	}

	if ($_REQUEST['ip'] != $_SERVER['REMOTE_ADDR']) {
      		drawPage();
	}

      break;

    default:
      initSNMP();
      doCheck();

	if (!defined($GLOBALS['error'])) {
 	     getSNMP();
	     getVLANS();
	}

      drawPage();   
      break;
  }
}

//
// Init the SNMP sessions, set vars, global communities
//

function initSNMP() {

	$GLOBALS['rocomm'] = 'public';
	$GLOBALS['rwcomm'] = 'private';

	// Return back the numeric OIDs, instead of text strings.
	snmp_set_oid_numeric_print(TRUE);
	
	// Get just the values.
	snmp_set_quick_print(TRUE);
	
	// For sequence types, return just the numbers, not the string and numbers.
	snmp_set_enum_print(TRUE);
	
	// Don't let the SNMP library get cute with value interpretation.  This makes
	// MAC addresses return the 6 binary bytes, timeticks to return just the integer
	// value, and some other things.
	snmp_set_valueretrieval(SNMP_VALUE_PLAIN);  

}

//
// See what search we're doing, get relvant info
//

function doCheck() {

	#if ($_REQUEST['port']) {
	#	$port = preg_replace("/-/", "/", $_REQUEST['port']);
	#} elseif ($_REQUEST['ip']) {

	if ($_REQUEST['hostname']) {
		$_REQUEST['ip'] = gethostbyname($_REQUEST['hostname']);
		
		if ((ip2long($_REQUEST['ip'])) == -1) {
			$GLOBALS['error'] = "Hostname lookup failed";
		}
	}
	
	if ($_REQUEST['ip'] && !$GLOBALS['error']) {
		if ((ip2long($_REQUEST['ip'])) == -1) {
			 $GLOBALS['error'] = "Invalid IP Address: " . $_REQUEST['ip'];
		} else {
			$GLOBALS['ip'] = $_REQUEST['ip'];
		}
	} elseif ($_REQUEST['mac']) {
		if (preg_match("/^([0-9a-f]{2}([:-]|$)){6}$/i", $_REQUEST['mac'])) {
			$GLOBALS['mac'] = $_REQUEST['mac'];
			locateMAC();
		} else {
			$GLOBALS['error'] = "Invalid MAC address " . $_REQUEST['mac'];
		}
	} else {
		$GLOBALS['ip'] = $_SERVER['REMOTE_ADDR'];
	}
	$GLOBALS['hostname'] = gethostbyaddr($GLOBALS['ip']);
	$GLOBALS['net'] = explode(".", $GLOBALS['ip']);

// Site-specific info, detemined by third octet of the focused IP addr.
// Multiple switches per-site can be added with $GLOBALS['sw'][0-9] vars.
// If you have multiple routers per site, you're on your own. For 99%
// of all installs, the router should be set to the defroute or HSRP IP

	if (substr($GLOBALS['ip'], 0, 7) == "172.16") {

	  if (!isset($_SESSION['admin'])) {
	    foreach ($GLOBALS['denysubnets'] as $denysub) {
		list($net,$mask) = split("/", $denysub);
		if (isIPIn($GLOBALS['ip'], $net, $mask)) {
			$GLOBALS['error'] = "Permission Denied. No changes can be made from this system.";
			$GLOBALS['disabled'] = 1;
			continue;
		}
	    }
	  }

	// This is matching on the third octet, so this matches subnets between 0 and 64
	  if ($GLOBALS['net'][2] < 64 ) {
		$GLOBALS['sw'][0] = '172.16.1.2';
		$GLOBALS['sw'][1] = '172.16.1.3';
		$GLOBALS['router'] = '172.16.1.1';
		$GLOBALS['site'] = "Los Angeles";

	// These are the VLAN indices, manually populated for now
	// To find the values, `snmpwalk -v1 -c <community> <router> .1.3.6.1.2.1.4.22.1.2`
	// Note the index number before each IP in the OID and map it to a VLAN here
	// This could be simpler, but doing it this way eliminates the need to walk the ARP 
	// tables every time.

		#$GLOBALS['intMap'] = Array (	1 => 4,
		#				12 => 7,
		#				16 => 8,
		#				20 => 9,
		#				24 => 11 );
		$GLOBALS['intMap'] = Array (	1 => 341,
						12 => 352,
						16 => 353,
						20 => 354,
						22 => 355,
						24 => 356,
						29 => 368 );
	  } elseif ($GLOBALS['net'][2] == 64) {
		$GLOBALS['sw'][0] = '172.16.64.3';
		$GLOBALS['router'] = '172.16.64.1';
		$GLOBALS['rocomm'] = 'public';
		$GLOBALS['site'] = "Massachusetts";
		$GLOBALS['intMap'] = Array (	64 => 2 );

	  } elseif ($GLOBALS['net'][2] > 95 && $GLOBALS['net'][2] < 128) {
		$GLOBALS['sw'][0] = '172.16.96.2';
		$GLOBALS['sw'][1] = '172.16.96.3';
		$GLOBALS['router'] = '172.16.96.1';
		$GLOBALS['site'] = "New York";
		$GLOBALS['intMap'] = Array (	96 => 346,
						100 => 347,
						104 => 348,
						108 => 349,
						119 => 351 );
	  } elseif (!isset($GLOBALS['error'])) {
		$GLOBALS['error'] = "Site not found for IP " . $GLOBALS['ip'];
		$GLOBALS['disabled'] = 1;
	  }
	} else {
		$GLOBALS['error'] .= "Site not found for IP " . $GLOBALS['ip'];
		$GLOBALS['disabled'] = 1;
	}

}

//
// Locate the MAC address by polling every router. Expensive, but necessary.
// Stops at the first match. This may result in invalid data if the tables contain
// multiple entries per MAC. TODO: Pull these into an array
//

function locateMAC() {

	$GLOBALS['mac'] = strtoupper($GLOBALS['mac']);
	$_REQUEST['mac'] = strtoupper($_REQUEST['mac']);
	foreach ($GLOBALS['routers'] as $router) {
	if (!$found) {
	$comm = $GLOBALS['rocomm'];

// Exception for vendor-controlled MPLS router at small site.
	if ($router == '172.16.64.1') {
		$comm = 'public';
	}
	snmp_set_valueretrieval(0);
	$arptab = snmprealwalk("$router", "$comm", ".1.3.6.1.2.1.4.22.1.2");
	$found = 0;

// Walk the ARP tables, looking for the MAC.
	foreach ($arptab as $toid => $tmac) {
		if (!$found) {
		  $newip = preg_replace("/\.1\.3\.6\.1\.2\.1\.4\.22\.1\.2\.\d+\./", "", $toid);	
#			print $toid. "<br>";
			$macpat[0] = '/^0:/';
			$macpat[1] = '/:0$/';
			$macpat[2] = '/:(\w):/';
			$macpat[3] = '/:(\w)$/';
			$macrep[0] = '00:';
			$macrep[1] = ':00';
			$macrep[2] = ':0$1:';
			$macrep[3] = ':0$1';
			$mac = preg_replace($macpat, $macrep, $tmac);
			$mac = strtoupper($mac);
#			print "$mac -- $newip <br>";
		  if ($mac == $_REQUEST['mac']) {
			$_REQUEST['ip'] = $newip;
			$GLOBALS['ip'] = $newip;
			$found++;
			#$GLOBALS['mac'] = strtoupper($GLOBALS['mac']);
		  }
		}
	}
	}
	}
#	print $GLOBALS['ip'];
}

function updateDB() {
	$tvlan = $GLOBALS['tvlan'];
	$query = "INSERT INTO reqs (timestamp, mac, ip, hostname, swport, swname, site, origvlan, vlanreq, reqip, requname, proc, method) VALUES (NOW(), '".$_REQUEST['mac']."', INET_ATON('".$GLOBALS['ip']."'),'" . $GLOBALS['hostname'] . "','". preg_replace("/-/", "/\//", $_REQUEST['port'])."','" . $_REQUEST['swname'] . "','". $_REQUEST['site'] . "','". $_REQUEST['vlan'] ."','". $_REQUEST['newvlan'] ."',INET_ATON('". $_SERVER['REMOTE_ADDR'] ."'),'". $_SESSION['uname'] . "', 1, 'web')";
	#print ($query);
	mysql_query($query);
	mysql_close();
#$logf = "/tmp/asap.log";
#$fh = fopen($logf, 'w');
#fwrite($fh, $query);
#fclose($fh);
#print "$query\n";
}
//
// Let's get some info, shall we?
//
function getSNMP() {
	$router = $GLOBALS['router'];
	$comm = $GLOBALS['rocomm'];
	if ($GLOBALS['site'] == 'Massachusetts') {
		$vlan = '@1';
	} else {
		$vlan = '@' . $GLOBALS['net'][2];
	}
	snmp_set_valueretrieval(0);
			#print $toid. "<br>";
		$net = $GLOBALS['net'][2];
		$vlmap = $GLOBALS['intMap']["$net"];
#		print "$net, $vlmap";
	// Get the MAC from the IP
		$mapmacoid = ".1.3.6.1.2.1.4.22.1.2." . $vlmap . "." . $GLOBALS['ip'];
#		print $mapmacoid . "<br>";
		#$mac = snmprealwalk("$router", "$comm", ".1.3.6.1.2.1.4.22.1.2");
		$mac = snmpget("$router", "$comm", "$mapmacoid");
#		print "$mac";
		  #if ($toid == $GLOBALS['ip']) {
	/*
			$macpat[0] = '/^0:/';
			$macpat[1] = '/:0$/';
			$macpat[2] = '/:(\w):/';
			$macpat[3] = '/:(\w)$/';
			$macrep[0] = '00:';
			$macrep[1] = ':00';
			$macrep[2] = ':0$1:';
			$macrep[3] = ':0$1';
			$GLOBALS['mac'] = preg_replace($macpat, $macrep, $mac);
	*/	
			$GLOBALS['mac'] = vsprintf("%02s:%02s:%02s:%02s:%02s:%02s", explode(":", strtoupper($mac)));
	#		$GLOBALS['mac'] = strtoupper($GLOBALS['mac']);
		  #}
#	$GLOBALS['brport'] = snmprealwalk("$sw", "$comm$vlan", ".1.3.6.1.2.1.17.4.3.1.2");
	$GLOBALS['mactbl'] = Array();
#	print "$comm$vlan";
	foreach ($GLOBALS['sw'] as $sw) {
		#print "$sw";
				if ($GLOBALS['realport'] != 'Po1') {
					$found++;
				}
		$GLOBALS['mactbl'] = snmprealwalk("$sw", "$comm$vlan", ".1.3.6.1.2.1.17.4.3.1.1");
		$found = 0;
		foreach ($GLOBALS['mactbl'] as $oid => $dmac) {
			if (!$found) {
			  $dmac = rtrim(preg_replace("/\"/", "", $dmac));	
			  $dmac = preg_replace("/ /",":",$dmac);
			  if ($dmac == $GLOBALS['mac']) {
				$oid = preg_replace("/4.3.1.1.0/", "4.3.1.2.0", $oid);
			#	print "$oid";
				$tbp = snmpget("$sw", "$comm$vlan", "$oid");
			#	print "$tbp";
			// Get ifIndex
				$GLOBALS['tifidx'] = snmpget("$sw", "$comm$vlan", ".1.3.6.1.2.1.17.1.4.1.2.$tbp");
				$tifidx = $GLOBALS['tifidx'];
			
			// Get this port's real port ID
				$GLOBALS['realport'] = snmpget("$sw", "$comm$vlan", ".1.3.6.1.2.1.31.1.1.1.1.$tifidx");
				if ($GLOBALS['realport'] != 'Po1') {
					$found++;
				}

			// Get this port's VLAN
				$GLOBALS['tvlan'] = snmpget("$sw", "$comm$vlan", ".1.3.6.1.4.1.9.9.68.1.2.2.1.2.$tifidx");

			// Get this port's speed
				$GLOBALS['speed'] = snmpget("$sw", "$comm", ".1.3.6.1.2.1.31.1.1.1.15.$tifidx");
				$GLOBALS['duplex'] = snmpget("$sw", "$comm", ".1.3.6.1.2.1.10.7.2.1.19.$tifidx");
				#print $GLOBALS['duplex'];

				$GLOBALS['thissw'] = $sw;

			// Get the switch's name
				$GLOBALS['swname'] = snmpget("$sw", "$comm", "1.3.6.1.2.1.1.5.0");
			  }
			}
		
		}

	// Stop processing if we've found something.
		if ($found) { 
			break; 
		}
	}
	//
	// Don't allow any changes if we can't find a port.
	//
	if (!isset($GLOBALS['realport'])) {
		$GLOBALS['disabled'] = 1;
	}
	
	snmp_set_valueretrieval(1);

} // getSNMP

//
// Not working right for some reason. Low priority. Cribbed from Remo Rickli/NeDi 
//

function nbtStat($ip) {

        global $timeout;

        $nbts   = pack('C50',129,98,00,00,00,01,00,00,00,00,00,00,32,67,75,65,65,65,65,65,65,65,65,65,65,65,65,65,65,65,65,65,65,65,65,65,65,65,65,65,65,65,65,65,65,00,00,33,00,01);
        $fp             = fsockopen("udp://$ip", 137, $errno, $errstr);
        if (!$fp) {
                return "ERROR! $errno $errstr";
        }else {
                fwrite($fp, "$nbts");
                socket_set_timeout($fp, ($timeout/2) );
                $data =  fread($fp, 400);
                fclose($fp);

                if (preg_match("/AAAAAAAAAA/",$data) ){
                        $nna = unpack('cnam',substr($data,56,1));                                                       # Get number of names
                        $out = substr($data,57);                                                                        # get rid of WINS header

                        for ($i = 0; $i < $nna[nam];$i++){
                                $nam = preg_replace("/ +/","",substr($out,18*$i,15));
                                $id = unpack('cid',substr($out,18*$i+15,1));
                                $fl = unpack('cfl',substr($out,18*$i+16,1));
                                if ($fl[fl] > 0){
                                        if ($id[id] == "3"){
                                                if ($na == ""){
                                                        $na = $nam;
                                                }else{
                                                        $co = $nam;
                                                }
                                        }
                                }else{
                                        if ($na == ""){
                                                $gr = $nam;
                                        }
                                }
                        }
                        return "$na $gr $co";
                }else{
                        return "No response";
                }
        }
}

//
// Future direction, not implemented
//

function checkTCP ($ip, $p){

        global $timeout;

        $fp = fsockopen($ip, $p, $errno, $errstr, ($timeout/3) );

        if (!$fp) {
                return "<img src=img/16/bstp.png hspace=20> $errstr";
        } else {
                stream_set_timeout($fp, $timeout );
                fwrite($fp, " \n");
                $str = fread($fp, 400);
                fclose($fp);

                $str = preg_replace("/[^\x20-\x7e]/",'',$str);

                if( preg_match("/<address>/i",$str) ){
                        $str = preg_replace("/.*<address>(.*)/i"," $1",$str);
                }elseif( preg_match("/server/i",$str) ){
                        $str = strstr($str,'Server:');
                }
                return "<img src=img/16/bchk.png hspace=20> " . $str;
        }
}

//
// Returns VLAN name/ID mappings. This was originally done via SNMP, but
// it's far more likely that admins will want to control this list
// to remove certain VLANs that should never be modified.
//
// TODO: re-implement the SNMP call for VLANs per switch and provide
// an exclude filter.
//

function getVLANS() {

	if ($GLOBALS['site'] == "New York") {
	  if (isset($_SESSION['admin'])) {
	    $GLOBALS['vlans'] = Array (	96 => 'Servers',
					100 => 'Desktops',
					104 => 'Phones',
					108 => 'Linux',
					119 => 'Deadzone' );
	  } else {
	    $GLOBALS['vlans'] = Array (	100 => 'Desktops',
					104 => 'Phones',
					108 => 'Linux' );
	  }
	} elseif ($GLOBALS['site'] == "Los Angeles") {	
	  if (isset($_SESSION['admin'])) {
	    $GLOBALS['vlans'] = Array (	1 => 'Servers',
					12 => 'Desktops',
					16 => 'Lab',
					20 => 'Linux',
					29 => 'Deadzone' );
	  } else {
	    $GLOBALS['vlans'] = Array ( 12 => 'Desktops',
					16 => 'Phones',
					20 => 'Linux' );
	  }

	} elseif ($GLOBALS['site'] == "Massachusetts") {	
	  $GLOBALS['vlans'] = Array (	1 => 'Desktop');
	}
}

//
// Sets the port to another VLAN, then calls resetPort to trigger a client release/renew
//

function setVLAN() {
	$sw = $_REQUEST['sw'];
	$rwcomm = $GLOBALS['rwcomm'];
	if ($_REQUEST['tifidx']) {
#		print "Setting ($sw, $rwcomm, .1.3.6.1.4.1.9.9.68.1.2.2.1.2." . $_REQUEST['tifidx'] . " integer " . $_REQUEST['newvlan'] . ")";
		snmpset("$sw", "$rwcomm", ".1.3.6.1.4.1.9.9.68.1.2.2.1.2." . $_REQUEST['tifidx'], "i", $_REQUEST['newvlan']);
		$check = snmpget("$sw", "$rwcomm", ".1.3.6.1.4.1.9.9.68.1.2.2.1.2." . $_REQUEST['tifidx']);
#		print "Checking " . $check . " -  ". $_REQUEST['newvlan'];
		if ($_REQUEST['newvlan'] == $check) {
			$_REQUEST['setcomplete'] = 1;
#		print "Resetting";
			resetPort($_REQUEST['tifidx']);
			updateDB();
			$GLOBALS['error'] = "VLAN has been modified.";
			if (isset($_SESSION['admin'])) { $GLOBALS['error'] .= " To find host again, wait for DHCP then  search for it by MAC Address"; }
		}
	} else {
		$GLOBALS['error'] = "Error in ifIndex";
	}
}

//
// Reset the port via an up/down
//

function resetPort($tifidx) {
	$sw = $_REQUEST['sw'];
	$router = $GLOBALS['router'];
	$rwcomm = $GLOBALS['rwcomm'];
		snmpset("$sw", "$rwcomm", ".1.3.6.1.2.1.2.2.1.7." . $tifidx, "i", "2");
		sleep(5);
		snmpset("$sw", "$rwcomm", ".1.3.6.1.2.1.2.2.1.7." . $tifidx, "i", "1");

	// Clear the old entry from the ARP table

		$net = $GLOBALS['net'][2];
	        $vlmap = $GLOBALS['intMap']["$net"];
                $delarpoid = ".1.3.6.1.2.1.4.22.1.4." . $vlmap . "." . $GLOBALS['ip'];
#		print "$router, $rwcomm, $delarpoid\n";
		snmpset("$router", "$rwcomm", "$delarpoid", "i", "2");

}

function isIPIn($ip,$net,$mask) {

	$lnet=ip2long($net);
	$lip=ip2long($ip);
	$binnet=str_pad( decbin($lnet),32,"0","STR_PAD_LEFT" );
	$firstpart=substr($binnet,0,$mask);
	$binip=str_pad( decbin($lip),32,"0","STR_PAD_LEFT" );
	$firstip=substr($binip,0,$mask);
	return(strcmp($firstpart,$firstip)==0);

}

//
// Main page output
//

function drawPage() {

$tvlan = $GLOBALS['tvlan'];
$duplexes = Array ( 1 => 'unknown',
		    2 => 'Half',
		    3 => 'Full' );

?>

<HTML>
<HEAD>
<META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
<LINK href="/style.css" type=text/css rel=StyleSheet>
<TITLE> <? print $GLOBALS['pagetitle']; ?></TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<TABLE CLASS="tblgroup" CELLPADDING="8" CELLSPACING="4"><TR>
  <TH COLSPAN="2" CLASS="title"><? print $GLOBALS['pagetitle']; ?></TH><TR><TD VALIGN="top">
  <TABLE CELLPADDING="7" BGCOLOR="#666688" CELLSPACING="1" WIDTH="400">
    <TH COLSPAN="2">LAN Info</TH>
    <TR BGCOLOR="#F5F5F5">
    <TD>IP address </TD><TD><? print $GLOBALS['ip']; ?></TD>
    <TR BGCOLOR="#FFFFFF"><TD>Hostname </TD><TD><? print $GLOBALS['hostname']; ?></TD>
    <TR BGCOLOR="#F5F5F5"><TD>MAC address</TD><TD> <? print $GLOBALS['mac']; ?></TD>
    <TR BGCOLOR="#FFFFFF"><TD>Switch</TD><TD> <? if ($GLOBALS['swname']) { print $GLOBALS['swname'] . " (" . $GLOBALS['site'] . ")"; } ?></TD> 
    <TR BGCOLOR="#F5F5F5"><TD>Port</TD><TD> <? print $GLOBALS['realport']; ?></TD> 
    <TR BGCOLOR="#FFFFFF"><TD>Speed</TD><TD> <? if ($GLOBALS['speed']) { print $GLOBALS['speed'] . " Mbit";}?></TD> 
    <TR BGCOLOR="#F5F5F5"><TD>Duplex</TD><TD> <? if ($GLOBALS['duplex']) { print $duplexes[$GLOBALS['duplex']]; }?></TD> 
    <TR BGCOLOR="#FFFFFF"><TD>VLAN</TD><TD> <? if ($tvlan) { print $GLOBALS['vlans']["$tvlan"]; print " (" . $GLOBALS['tvlan'] . ")"; } ?></TD> 
    <TR BGCOLOR="#F5F5F5"><TD>NetBIOS name</TD><TD><? if (!$GLOBALS['error']) { print nbtStat($GLOBALS['ip']); } ?></TD>
  </TABLE>
  </TD>
  <TD VALIGN="top">
  <FORM NAME="mod_port" action="<? print $_SERVER['PHP_SELF']; ?>">
  <INPUT type="hidden" name="action" value="chvlan">
  <INPUT type="hidden" name="site" value="<? print $GLOBALS['site']; ?>">
  <INPUT type="hidden" name="port" value="<? print $GLOBALS['realport']; ?>">
  <!-- <INPUT type="hidden" name="port" value="<? print preg_replace("/\//", "-", $GLOBALS['realport']); ?>"> -->
  <INPUT type="hidden" name="ip" value="<? print $GLOBALS['ip']; ?>">
  <INPUT type="hidden" name="mac" value="<? print $GLOBALS['mac']; ?>">
  <INPUT type="hidden" name="hostname" value="<? print $GLOBALS['hostname']; ?>">
  <INPUT type="hidden" name="swname" value="<? print $GLOBALS['swname']; ?>">
  <INPUT type="hidden" name="vlan" value="<? print $GLOBALS['tvlan']; ?>">
  <INPUT type="hidden" name="tifidx" value="<? print $GLOBALS['tifidx']; ?>">
  <INPUT type="hidden" name="sw" value="<? print $GLOBALS['thissw']; ?>">
  <TABLE CELLPADDING="5" BGCOLOR="#666688" CELLSPACING="1" WIDTH="290">
    <TR>
    <TH>Change VLAN</TH><TH ALIGN="right">
    <SELECT name="newvlan">
<?
foreach ($GLOBALS['vlans'] as $id => $name) {
	?>
	<OPTION value="<? print $id; ?>" <? if ($GLOBALS['tvlan'] == $id) { print "selected=\"selected\""; } if ($GLOBALS['disabled']) { print " disabled"; } ?>><? print $name . " (" . $id . ")"; ?></OPTION>
	<?
}

?>
    </TH>
    <TR BGCOLOR="#F5F5F5"><TD CLASS="cli">WARNING: This change will be immediate</TD><TD ALIGN="right"><INPUT type="submit" value="Modify"<? if ($GLOBALS['disabled']) { print " disabled"; } ?>></TD>
  </TABLE>
  </FORM>

<? if ($_SESSION['admin']) {
?>
  <TABLE CELLPADDING="2" BGCOLOR="#666688" CELLSPACING="1" WIDTH="290">
    <TR> <TH>Host Search Functions</TH>
  </TABLE><p>
  <FORM NAME="sel_ip" action="<? print $_SERVER['PHP_SELF']; ?>">
  <TABLE CELLPADDING="2" BGCOLOR="#666688" CELLSPACING="1" WIDTH="290">
    <TR>
    <TH>By Name</TH><TH ALIGN="right"><INPUT TYPE="text" SIZE="30" NAME="hostname" VALUE="<? print $_REQUEST['hostname']; ?>"></TH>
    <TR BGCOLOR="#F5F5F5"><TD CLASS="cli">e.g. mdpagehp</TD><TD ALIGN="right"><INPUT type="submit" value="Search"></TD>
  </TABLE>
  </FORM>
  <FORM NAME="sel_name" action="<? print $_SERVER['PHP_SELF']; ?>">
  <TABLE CELLPADDING="2" BGCOLOR="#666688" CELLSPACING="1" WIDTH="290">
    <TR>
    <TH>By IP</TH><TH ALIGN="right"><INPUT TYPE="text" SIZE="20" NAME="ip" VALUE="<? print $_REQUEST['ip']; ?>"></TH>
    <TR BGCOLOR="#F5F5F5"><TD CLASS="cli">e.g. 172.16.100.55</TD><TD ALIGN="right"><INPUT type="submit" value="Search"></TD>
  </TABLE>
  </FORM>
  <FORM NAME="sel_mac" action="<? print $_SERVER['PHP_SELF']; ?>">
  <TABLE CELLPADDING="2" BGCOLOR="#666688" CELLSPACING="1" WIDTH="290">
    <TR>
    <TH>By MAC</TH><TH ALIGN="right"><INPUT TYPE="text" SIZE="20" NAME="mac" VALUE="<? print $_REQUEST['mac']; ?>"></TH>
    <TR BGCOLOR="#F5F5F5"><TD CLASS="cli">e.g. 00:00:DE:AD:BE:EF</TD><TD ALIGN="right"><INPUT type="submit" value="Search"></TD>
  </TABLE>
  </FORM>
  </TD>
<?
}
?>
  <TR>
<? if ($GLOBALS['error']) { 
?>
  <TD COLSPAN="4" CLASS="error">NOTICE:  <? print $GLOBALS['error']; ?></TD></TD><TR>
<?
  }
?>
  <TD colspan="2">
  <TABLE CLASS="tblgroup" CELLPADDING="8" CELLSPACING="4" width="700"><TR>
    <TD><? include('help.html'); ?></TD>
  </TABLE></TD>
  <TR>
  <TD>
  <A HREF="/">Reset</A> | <? if (isset($_SESSION['admin'])) { echo "<A HREF=\"/logout.php\">Logout ".$_SESSION['fn'] . " " . $_SESSION['sn'] . "</A> | <A HREF=\"reports.php\">Reports</A>"; } else { echo "<A HREF=\"/login.php\">Admin Login</A>"; } ?></TD><TD align="right" CLASS="tag">2oo7 Paul Venezia</TD>
</TABLE>
<?
?>
</BODY>
</HTML>
<?
}
?>
