<?
////
//
// This code needs some logic work.
//
// Disclaimer:
// This software is distributed as-is. If it causes your network to catch fire, it's not my fault.
//
// ASAP v0.0.1 - reports.php
//
// 06.01.07 Paul Venezia (pvenezia@pvenezia.net)
//


require_once('db.inc');

session_start();

#print_r($_SESSION);
if (!isset($_SESSION['admin'])) {
	header("Location: login.php");
	exit;
}

$tblnames = Array ( 	'id' => 'ID',
			'timestamp' => 'Timestamp',
			'mac' => 'MAC Address',
			'ip' => 'IP Address',
			'hostname' => 'Hostname',
			'site' => 'Site',
			'swport' => 'Switch Port',
			'swname' => 'Switch Name',
			'origvlan' => 'Original VLAN',
			'vlanreq' => 'Requested VLAN',
			'reqip' => 'Requestor IP',
			'method' => 'Method',
			'requname' => 'Requestor Username' );

#$nrows = mysql_num_rows(mysql_query("SELECT * FROM reqs"));

switch ($_REQUEST['action']) {
	case 'search' :
		if (preg_match("/ip/", $_REQUEST['field'])) {
			$field = "INET_NTOA(" . $_REQUEST['field'] . ")";
			#$mstr = " = '". $_REQUEST['str'] . "'";
		} else {
			$field = $_REQUEST['field'];
		}
		switch ($_REQUEST['op']) {
			case 'eq' :
				$mstr = " = '". $_REQUEST['str'] . "'";
				break;
			case 'neq' :
				$mstr = " != '". $_REQUEST['str'] . "'";
				break;
			case '>' :
				$mstr = " > '". $_REQUEST['str'] . "'";
				break;
			case '<' :
				$mstr = " < '". $_REQUEST['str'] . "'";
				break;
			case 'like' :
				$mstr = " LIKE '%". $_REQUEST['str'] . "%'";
				break;
			case 'nlike' :
				$mstr = " NOT LIKE '%". $_REQUEST['str'] . "%'";
				break;
		}

	if ($_REQUEST['str']) {
			$query = "SELECT id, timestamp, mac, INET_NTOA(ip) as ip, hostname, site, swport, swname, origvlan, vlanreq, INET_NTOA(reqip) as reqip, method, requname from reqs WHERE $field $mstr";
	}
}

if (!isset($query)) {
		$query = "SELECT id, timestamp, mac, INET_NTOA(ip) as ip, hostname, site, swport, swname, origvlan, vlanreq, INET_NTOA(reqip) as reqip, method, requname from reqs";
}

if (!isset($_REQUEST['action']) && !isset($_REQUEST['str'])) {
$newdir = Array ();
	$sort = $_REQUEST['sort'];
	$dir = $_REQUEST['dir'];
	if (!isset($sort)) {
		$sort = 'id';
	}
	if (!isset($dir)) {
		$dir = 'ASC';
	}
	$query .= " ORDER BY " . $sort . " " . $dir;
	if ($_REQUEST['dir'] == 'desc') {
		$newdir[$sort] = 'asc';
	}
		
}
#print $query;
$nrows = mysql_num_rows($query);
#print $nrows. "---";

if (isset($_REQUEST['len']) && isset($nrows)  && $nrows > 50) {
        $rowstart = ($nrows - $_REQUEST['len']);
	$query .= " LIMIT " . $rowstart . ", " . $_REQUEST['len'];
} else {
        $rowstart = ($nrows - 50);
        $query .= " LIMIT " . $rowstart . ", 50";
	$_REQUEST['len'] = '100';
}
	


#print $query;
$tblquery = "DESCRIBE reqs";

$tblres = mysql_query($tblquery);

$res = mysql_query($query);

mysql_close();
  $rslt = Array();
  while($row = mysql_fetch_assoc($res)) {
    array_push($rslt, $row);
  }

$tblrslt = Array();
while($row = mysql_fetch_assoc($tblres)) {
	array_push($tblrslt, $row);
}
#print_r($tblrslt);

?>
<HTML>
<HEAD>
<TITLE>ASAP Reporting</TITLE>
<META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
<LINK href="/style.css" type=text/css rel=StyleSheet>
</HEAD>
<BODY>
<TABLE BGCOLOR="#000000"></TD>
<TABLE BORDER=0 CELLSPACING=1 CELLPADDING=5 CLASS="tblgroup">
<TR><TH ALIGN="center" COLSPAN="<? print (sizeof($tblrslt) - 9); ?>">ASAP Reporting</A></TH>
<TH COLSPAN="7" ALIGN="left"><FORM name="search" action="<? print $_SERVER['PHP_SELF']; ?>"><INPUT TYPE="hidden" NAME="action" VALUE="search">Search: <SELECT NAME="field">
<? 
foreach ($tblnames as $fld => $name) {
?>
	<OPTION VALUE="<? print $fld; ?>" <? $_REQUEST['field'] == $fld ? print "selected" : print ""; ?>><? print $name; ?></OPTION>
<?
}
?>
</SELECT><SELECT NAME="op">
<?
$ops = Array (	'eq' => '=',
		'neq' => '!=',
		'>' => '>',
		'<' => '<',
		'like' => 'LIKE',
		'nlike' => 'NOT LIKE'
	     );

foreach ($ops as $op => $opname) {
?>
<OPTION VALUE="<? print $op; ?>" <? $_REQUEST['op'] == $op ? print "selected" : print ""; ?>><? print $opname; ?></OPTION>
<?
}
?>
</SELECT>
<INPUT TYPE="text" SIZE="25" NAME="str" VALUE="<? print $_REQUEST['str']; ?>"> <INPUT TYPE="submit" VALUE="Search"> (<A HREF="<? print $_SERVER['PHP_SELF']; ?>">Clear</A> | <A HREF="/">Return</A>)<p>
Display <SELECT NAME="len">

<? 
$numlen = Array ('50', '100', '200', '250', '300', '400', '500');

foreach ($numlen as $tlen) {
?>
  <OPTION VALUE="<? print $tlen; ?>" <? $_REQUEST['len'] == $tlen ? print "selected" : print ""; ?>><? print $tlen; ?></OPTION>
<?
}
?>

</SELECT> rows
</FORM></TH><TR><TD COLSPAN="<? print sizeof($tblrslt); ?>"></TD>

<TR>
<?


foreach ($tblrslt as $col) { 
	if (preg_match("/^(os|proc)$/", $col['Field'])) {
		continue;
	}
?>
  <TH CLASS="thtag"><A HREF="?sort=<? print $col['Field'];?>&dir=<? $_REQUEST['sort'] = $newdir[$col[Field]] ? print $newdir[$col[Field]] : print 'desc';   if (isset($_REQUEST['field'])) { print "&action=search&field=" . $_REQUEST['field'] . "&str=" . $_REQUEST['str'] . "&op=" . $_REQUEST['op']; }  if (isset($_REQUEST['len'])) { print "&len=" . $_REQUEST['len']; }?>"><? print $tblnames[$col[Field]];?></A></TH>
<? 
}
?>
</TR>

<?php 
  
if($rslt) {
  $even=1;
  foreach(array_reverse($rslt) as $result) {
    if($even == 1) {
      $even = 0;
    } else {
      $even = 1;
    }
?>
<TR CLASS="tag<? print $even; ?>">
<TD CLASS="tag"><? print $result['id']; ?></TD>
<TD CLASS="tag"><? print $result['timestamp']; ?></TD>
<!-- <TD CLASS="tag"><? print date('m/d/Y, H:m:s', strtotime($result['timestamp'])); ?></TD> -->
<TD CLASS="tag"><? print $result['mac']; ?></TD>
<TD CLASS="tag"><? print $result['ip']; ?></TD>
<TD CLASS="tag"><? print $result['hostname']; ?></TD>
<TD CLASS="tag"><? print $result['site']; ?></TD>
<TD CLASS="tag"><? print $result['swname']; ?></TD>
<TD CLASS="tag"><? print $result['swport']; ?></TD>
<TD CLASS="tag"><? print $result['origvlan']; ?></TD>
<TD CLASS="tag"><? print $result['vlanreq']; ?></TD>
<TD CLASS="tag"><? print $result['reqip']; ?></TD>
<TD CLASS="tag"><? print $result['method']; ?></TD>
<TD CLASS="tag"><? print $result['requname']; ?></TD>
</TR>
<?
}
}
?>
</TABLE>
</TD></TABLE>
</BODY>
</HTML>
