<?php
    require_once 'dbtools.php';
    
    print("<div class=\"sumtabcontainer\">\n");

    if (!isset($FILTERCONDITION)) {
	if (isset($GIMMEDATES) && $GIMMEDATES == 1) {
	    $result = pg_query("select receivinghost, envfrom, count(*) as tries, helofrom, 
		relaypresip, gracefuldns, whitelisted, spf, resolvedrelayip, action, connectdate
		from history 
		group by receivinghost, envfrom, helofrom, 
		relaypresip, gracefuldns, whitelisted, spf, resolvedrelayip, action, connectdate
		order by connectdate desc, tries desc, receivinghost;");
	} else  {
	    $result = pg_query("select receivinghost, envfrom, count(*) as tries, helofrom, 
		relaypresip, gracefuldns, whitelisted, spf, resolvedrelayip, action 
		from history 
		group by receivinghost, envfrom, helofrom, 
		relaypresip, gracefuldns, whitelisted, spf, resolvedrelayip, action 
		order by tries desc, receivinghost;");
	}
    } else {
	if (isset($GIMMEDATES) && $GIMMEDATES == 1) {
	    $result = pg_query("select receivinghost, envfrom, count(*) as tries, helofrom, 
		relaypresip, gracefuldns, whitelisted, spf, resolvedrelayip, action, connectdate 
		from history $FILTERCONDITION
		group by receivinghost, envfrom, helofrom, 
		relaypresip, gracefuldns, whitelisted, spf, resolvedrelayip, action, connectdate 
		order by connectdate desc, tries desc, receivinghost;");
	} else {
	    $result = pg_query("select receivinghost, envfrom, count(*) as tries, helofrom, 
		relaypresip, gracefuldns, whitelisted, spf, resolvedrelayip, action 
		from history $FILTERCONDITION
		group by receivinghost, envfrom, helofrom, 
		relaypresip, gracefuldns, whitelisted, spf, resolvedrelayip, action 
		order by tries desc, receivinghost;");
	}
    }

    print("<table class=\"histtable\">\n");
    if (isset($GIMMEDATES) && $GIMMEDATES == 1) {
	print("<tr class=\"histtabhead\"><th>Date</th><th>Host</th><th>Env-From</th><th>Msgs</th><th>Helo</th>
	        <th>IP</th><th>GDNS</th><th>WL</th><th>SPF</th><th>Resolved IP</th><th>Action</th>
		</tr>");
    } else {
	print("<tr class=\"histtabhead\"><th>Host</th><th>Env-From</th><th>Msgs</th><th>Helo</th>
	        <th>IP</th><th>GDNS</th><th>WL</th><th>SPF</th><th>Resolved IP</th><th>Action</th>
		</tr>");
    }
    $rowcount = 0;
    $maxlen = 32;
    while ($row = pg_fetch_array($result)) {
	if (strlen($row['envfrom']) > $maxlen) {
	    $row['envfrom'] = "[...]" . substr($row['envfrom'], strlen($row['envfrom']) - $maxlen, $maxlen);
	}
	if (strlen($row['resolvedrelayip']) > $maxlen) {
	    $row['resolvedrelayip'] = "[...]" . substr($row['resolvedrelayip'], strlen($row['resolvedrelayip']) - $maxlen, $maxlen);
	}
	if (strlen($row['helofrom']) > $maxlen) {
	    $row['helofrom'] = "[...]" . substr($row['helofrom'], strlen($row['helofrom']) - $maxlen, $maxlen);
	}
	print("<tr class=\"".getrowclass($rowcount,"sumtabrowlite", "sumtabrowdark")."\">");
	if (isset($GIMMEDATES) && $GIMMEDATES == 1) {
	    print("
	    <td class=\"histtabcellpassive\">"
		.$row['connectdate'].
	    "</td>");
	}
	print("
	    <td class=\"histtabcell\" onclick=\"javascript:document.getElementById('host-".$rowcount."').submit()\">
		<form action=\"history.php\" method=\"post\" id=\"host-$rowcount\">
	            <input type=\"hidden\" name=\"receivinghost\" value=\"".$row['receivinghost']."\"/>
		    <input type=\"hidden\" name=\"envfrom\" value=\"".$row['envfrom']."\"/>
		    <input type=\"hidden\" name=\"relaypresip\" value=\"".$row['relaypresip']."\"/>
		    <input type=\"hidden\" name=\"gimmedates\" value=\"1\"/>
		</form>".$row['receivinghost']."
	    </td>
	    <td class=\"histtabcell\" onclick=\"javascript:document.getElementById('envfrom-".$rowcount."').submit()\">
		<form action=\"history.php\" method=\"post\" id=\"envfrom-$rowcount\">
		    <input type=\"hidden\" name=\"envfrom\" value=\"".$row['envfrom']."\"/>
		    <input type=\"hidden\" name=\"gimmedates\" value=\"1\"/>
		</form>".$row['envfrom'].
	    "</td>
	    <td class=\"histtabcellpassive\">"
		.$row['tries'].
	    "</td>
	    <td class=\"histtabcellpassive\">"
		.$row['helofrom'].
	    "</td>
	    <td class=\"histtabcell\" onclick=\"javascript:document.getElementById('relay-".$rowcount."').submit()\">
		<form action=\"relay.php\" method=\"post\" id=\"relay-$rowcount\">
		    <input type=\"hidden\" name=\"relaypresip\" value=\"".$row['relaypresip']."\"/>
		</form>".$row['relaypresip']."</td>");
	    if ($row['gracefuldns'] == "t") {
		print("<td class=\"histtabcellpassivecenter\">
		    <object data=\"images/yeah.png\"
		            type=\"image/png\">
		</td>");
	    } else {
		print("<td class=\"histtabcellpassivecenter\">
		    <object data=\"images/nope.png\"
		            type=\"image/png\">
		</td>");
	    }
	    if ($row['whitelisted'] == "t") {
		print("<td class=\"histtabcellpassivecenter\">
		    <object data=\"images/yeah.png\"
		            type=\"image/png\">
		</td>");
	    } else {
		print("<td class=\"histtabcellpassivecenter\">
		    <object data=\"images/nope.png\"
		            type=\"image/png\">
		</td>");
	    }
	    if ($row['spf'] == "2") {
		print("<td class=\"histtabcellpassivecenter\">
		    <object data=\"images/yeah.png\"
		            type=\"image/png\">
		</td>");
	    } else {
		if ($row['spf'] == "3") {
		    print("<td class=\"histtabcellpassivecenter\">
			    <object data=\"images/nope.png\"
		        	type=\"image/png\">
			</td>");
		} else {
		    print("<td class=\"histtabcellpassivecenter\">
			    <object data=\"images/empty.png\"
		        	type=\"image/png\">
			</td>");
		}
	    }
	    print("<td class=\"histtabcellpassive\">"
		.$row['resolvedrelayip'].
	    "</td>
	    <td class=\"histtabcellpassive\">"
		.$row['action'].
	    "</td>
	    </tr>\n");
	$rowcount++;
    }
    print("</table>\n");
    print("</div>\n");
?>