<?
/********************************************************/
/* returns true if $str is a valid email address format
/* else returns false
/********************************************************/
function isEmail($str){
	// note that an email address can be between <> chars
  // e.g. <joe@world.com> is valid
	$pattern = "^[<a-zA-Z0-9_\.-]+@[a-zA-Z0-9\.-]+\.[a-zA-Z>]{2,5}$";
          if(ereg($pattern, $str, $reg)){
          	return true;
          }	
          else
          	return false;
}


require "/a/infofa/h/users/mgonzale/private/pg_open_db_infection.php";

$interval = $_GET['interval'];
$new_infection = $_GET['new_infection'];
$old_infection = $_GET['old_infection'];

// assign default values
if($interval == ""){
	$interval = 3;
}

if($new_infection == ""){
	$new_infection = 2;
}

if($old_infection == ""){
	$old_infection = 7;
}
?>
<html>
<title>PC Infection</title>
<head><basefont face="Arial"></head>
<body>
<form action="pc_infected_list.php">
	<table border='1'><tr><td>
  <table>
  	<tr>
    	<td><br></td>
      <td><center><small>in day(s)</small></center></td>
      <td><br></td>
    </tr>
  	<tr>
			<td>Interval:</td>
      <td><input size="3" name=interval type=text value=<?echo $interval?>></td>
      <td>
      	Only Machines in the interval will be listed (Interval = now - last_scanned date)
      </td>
    </tr>
    <tr>
      <td><font color='blue'>New Infections Treshold:</font></td>
      <td><input size="3" name=new_infection type=text value=<?echo $new_infection?>></td>
      <td>
      	Records of machines that have been infected for less or equal than this value are in blue text.
      </td>
    </tr>
    <tr>
      <td><font color='red'>Old Infections Treshold:</font></td>
      <td><input size="3" name=old_infection type=text value=<?echo $old_infection?>></td>
      <td>
      	Records of machines that have been infected for more or equal than this value are in red text.
      </td>
    </tr>
    <tr>
    	<td colspan="3"><center><input name=submit type=submit value="GO"></center></td>
    </tr>
  </table>
  </td></tr></table>
</form>
<br>
<?
// generate and execute a query

$query = "
					select s.allocated_to, s.contact, i.ip, i.last_scanned, s.description, s.building_code, i.list,
min(h.ts) as initial_infection_date, date_trunc('day',now() - min(h.ts)) as inter
from infected_summary i, subnets s, infected_hosts h
where i.ip between start_s
and end_s and i.ip=h.ip
and now() - i.last_scanned <= interval '$interval day'
group by s.allocated_to, s.contact, i.ip, i.last_scanned, s.description, s.building_code, i.list
UNION
SELECT ' N/A', 'N/A', ih.ip, last_scanned, 'N/A', 'N/A', list, min(ts), date_trunc('day',now() - min(h.ts))
FROM infected_summary ih, infected_hosts h
WHERE now() - last_scanned <= interval '$interval day'
AND NOT EXISTS (
  SELECT *
  FROM subnets w
  WHERE ih.ip between w.start_s and w.end_s)
  group by ih.ip, last_scanned, list
order by allocated_to, ip
				";         
$result = pg_exec($connection, $query) or die("Error in query: $query." . pg_last_error($connection));

// get the number of rows in the resultset
$rows = pg_num_rows($result);
echo "Query result returned $rows records.<br>";

// if records present
// iterate through resultset and print in a table
$num_of_field = pg_numfields($result);
//echo "number of fields = $num_of_field<br>";
?>
<table border=1>
<?
$n=1;
$temp = "";

while ($row = pg_fetch_row($result)){

		echo "<tr>";
	
		while(list($key, $value) = each($row)){
    		// check the initial_infection_date and set the flags for color format
        $color_flag = "black";
          
        $num_of_days_infected = intval($row[$num_of_field-1]);
        if($num_of_days_infected >= $old_infection){
          	$color_flag = "red";
        }
        
        if($num_of_days_infected <= $new_infection){
          	$color_flag = "blue";
        }
         
        // fisrt 2 fields (allocated_to and contact) will appear in a separate row  
	      if($key == 0 && $temp != $value){
        	$contact = $row[1];
          //debug
          if($contact == ""){
          	$contact = "N/A";
          }
          
          // verify if $contact is an email address format
          $contact_temp = "";
          $email = "";
          $contact_arr = explode(" ", $contact);
          
          while(list($index, $val) = each($contact_arr)){
          	if(isEmail($val)){
            	$email = trim($val, "\<\>");
              $email = "<a href='mailto:$email'>$email<a>";
            }
            else{
            	$contact_temp .= $val. " ";
            }
          }
          
          $contact_temp .= " ".$email;
        	echo "<tr bgcolor='pink'><th colspan='$num_of_field-3'><b>Allocated To: $value <small>(Contact: $contact_temp)</small></th></tr>";
          echo "<tr bgcolor=#c0c0c0>";
          
          
          
          for($i=2; $i<$num_of_field-1; $i++){
	          $field = pg_fieldname($result, $i);
						echo "<td><b><center><small>$field</small></center></b></td>";
          }
          echo "</tr>";
          $temp = $value;
        }
        else{
        	if($key > 1 && $key!=$num_of_field-1){
          	// get only the date from the last_scanned and initial_infection_date fields, not the time
            if($key == pg_fieldnum($result, 'last_scanned') || $key == pg_fieldnum($result, 'initial_infection_date')){
              $date = substr($value, 0, 11);
  						echo "<td><font color='$color_flag'>$date<br></font></td>";
            }
            else{      
	        		echo "<td><font color='$color_flag'>$value<br></font></td>";
            }
          }
        }
		}
		echo "</tr>";
		$n++;		
}
?>
</table>
<?
// close database connection
pg_close($connection);
?>
</body>
</html>

