<?php
    require_once 'dbtools.php';
    
    function parseFieldRes ($fieldRes) {
	switch ($fieldRes) {
	    case -1:
		print("<td class=\"histtabcellpassive\">
			unknown
		    </td>");
	        break;
	    case 0:
		print("<td class=\"histtabcellpassive\">
			passed
		    </td>");
	        break;
	    case 1:
		print("<td class=\"histtabcellpassive\">
			rejected
		    </td>");
	        break;
	    case 2:
		print("<td class=\"histtabcellpassive\">
			tempfailed
		    </td>");
        	break;
	    case 3:
		print("<td class=\"histtabcellpassive\">
			skipped
		    </td>");
        	break;
	    case 4:
		print("<td class=\"histtabcellpassive\">
			graceful
		    </td>");
        	break;
        }
    }
    
    print("<div class=\"sumtabcontainer\">\n");

    $result = pg_query("select count(*) as tries, envfrom, action, 
	mxesthatreject, mxesthataccept, mxesoutoforder, 
	mxesthattempfail, mxnum, gracefuldns, whitelisted, spf, reverserelay, fakerelay 
	from history 
	where relaypresip='".$RELAYPRESIP."' 
	group by envfrom, action, mxesthatreject, mxesthataccept, 
	mxesoutoforder, mxesthattempfail, mxnum, gracefuldns, 
	whitelisted, action, spf, reverserelay, fakerelay 
	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>Reverse</th><th>Fake</th><th>SPF</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>");
	
	parseFieldRes($row['reverserelay']);
	parseFieldRes($row['fakerelay']);
	
	/* if ($row['reverserelay'] == "0") {
	    print("<td class=\"histtabcellpassivecenter\">
	        <object data=\"images/yeah.png\" type=\"image/png\">
	    </td>");
	} else {
	    if ($row['reverserelay'] == "1") {
    		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>");
	    }
        }
	
	if ($row['fakerelay'] == "0") {
	    print("<td class=\"histtabcellpassivecenter\">
	        <object data=\"images/yeah.png\" type=\"image/png\">
	    </td>");
	} else {
	    if ($row['fakerelay'] == "1") {
    		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>");
	    }
        } */
	
	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>");
	    }
        }
	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");
?>