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

    $result = pg_query("select count(*) as tries, envfrom, action, 
	mxesthatreject, mxesthataccept, mxesoutoforder, 
	mxesthattempfail, mxnum, gracefuldns, whitelisted 
	from history 
	where relaypresip='".$RELAYPRESIP."' 
	group by envfrom, action, mxesthatreject, mxesthataccept, 
	mxesoutoforder, mxesthattempfail, mxnum, gracefuldns, 
	whitelisted, action 
	order by tries desc, envfrom;");
    
    $wlres = pg_query("select address from whitelist where address='".$RELAYPRESIP."'");
    $wlrow = pg_fetch_array($wlres);
    
    if ($wlrow[0] == $RELAYPRESIP) {
    	$relayiswled = 1;
    } else {
	$relayiswled = 0;
    }
    
    if ($relayiswled == 0) {
	print("<div class=\"wlc\">");
	print("<div class=\"whitelistbtn\" id=\"whitelistbtn\">");
	print("add this IP to whitelist");
	print("</div>");
    
	print("<div class=\"commentcontainer\">");
	print("<form action=\"\" method=\"post\" name=\"hiddencommentform\">\n");
	print("<label for=\"relaypresip\">comment:</label>\n");
	print("<input type=\"text\" id=\"comment\" name=\"comment\" value=\"\">\n");
	print("<input type=\"hidden\" name=\"relaypresip\" id=\"relaypresip\" value=\"".$RELAYPRESIP."\">\n");
	print("</form>\n");
	print("</div>");
    
	print("<div class=\"whitelistresok\" id=\"whitelistres\">");
	print("you weren't suppose to be able to see nor this");
	print("<p id=\"errormessage\">neither this</p>");
        print("</div>\n");
        print("</div>\n");
    } else {
	print("<div class=\"whitelisted\">");
	print("this relay is whitelisted already.");
	print("</div>");
    }

    print("<table class=\"histtable\">\n");
    print("<tr class=\"histtabhead\"><th>Tries</th><th>Envfrom</th><th>Action</th><th>MX Reject</th><th>MX Accept</th>
    	    <th>MX Out</th><th>MX TF</th><th>MX Total</th><th>GDNS</th><th>WL</th>
	</tr>");
    $rowcount = 0;
    $maxlen = 48;
    while ($row = pg_fetch_array($result)) {
	if (strlen($row['envfrom']) > $maxlen) {
	    $row['envfrom'] = "[...]" . substr($row['envfrom'], strlen($row['envfrom']) - $maxlen, $maxlen);
	}
	print("<tr class=\"".getrowclass($rowcount,"sumtabrowlite", "sumtabrowdark")."\">");
        print("
	    <td class=\"histtabcellpassive\">"
		.$row['tries'].
	    "</td>");
	print("
	    <td class=\"histtabcellpassive\">"
		.$row['envfrom'].
	    "</td>");
	print("
	    <td class=\"histtabcellpassive\">"
		.$row['action'].
	    "</td>");
	print("
	    <td class=\"histtabcellpassive\">"
		.$row['mxesthatreject'].
	    "</td>");
	print("
	    <td class=\"histtabcellpassive\">"
		.$row['mxesthataccept'].
	    "</td>");
	print("
	    <td class=\"histtabcellpassive\">"
		.$row['mxesoutoforder'].
	    "</td>");
	print("
	    <td class=\"histtabcellpassive\">"
		.$row['mxesthattempfail'].
	    "</td>");
	print("
	    <td class=\"histtabcellpassive\">"
		.$row['mxnum'].
	    "</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>");
        }
	print("</tr>\n");
	$rowcount++;
    }
    print("</table>\n");
    
    $resolvedres = pg_query("select distinct resolvedrelayip from history where relaypresip='".$RELAYPRESIP."';");
    
    print("<table class=\"relaydetails\">\n");
    print("<tr>\n");
    print("<td>\n");
    print("<table class=\"histtable\">\n");
    print("<tr class=\"histtabhead\"><th>Resolved IP</th></tr>");
    $rowcount = 0;
    while ($row = pg_fetch_array($resolvedres)) {
	print("<tr class=\"".getrowclass($rowcount,"sumtabrowlite", "sumtabrowdark")."\">");
        print("
	    <td class=\"histtabcellpassive\">"
		.$row['resolvedrelayip'].
	    "</td>");
	print("</tr>\n");
	$rowcount++;
    }
    print("</table>\n");
    print("</td>\n");
    
    $helores = pg_query("select distinct helofrom from history where relaypresip='".$RELAYPRESIP."';");
    print("<td>\n");
    print("<table class=\"histtable\">\n");
    print("<tr class=\"histtabhead\"><th>Helo</th></tr>");
    $rowcount = 0;
    while ($row = pg_fetch_array($helores)) {
	print("<tr class=\"".getrowclass($rowcount,"sumtabrowlite", "sumtabrowdark")."\">");
        print("
	    <td class=\"histtabcellpassive\">"
		.$row['helofrom'].
	    "</td>");
	print("</tr>\n");
	$rowcount++;
    }
    print("</table>\n");
    print("</td>\n");
    
    $connectfromres = pg_query("select distinct connectfrom from history where relaypresip='".$RELAYPRESIP."';");
    print("<td>\n");
    print("<table class=\"histtable\">\n");
    print("<tr class=\"histtabhead\"><th>Connect From</th></tr>");
    $rowcount = 0;
    while ($row = pg_fetch_array($connectfromres)) {
	print("<tr class=\"".getrowclass($rowcount,"sumtabrowlite", "sumtabrowdark")."\">");
        print("
	    <td class=\"histtabcellpassive\">"
		.$row['connectfrom'].
	    "</td>");
	print("</tr>\n");
	$rowcount++;
    }
    print("</table>\n");
    print("</td>\n");
    print("</tr>\n");
    print("</table>\n");
    
    print("</div>\n");
?>