<?php
    global $CTABNAME;
    global $FILTERCONDITION;
    global $FILTERSCOPE;
    global $GIMMEDATES;

    require_once 'dbtools.php';
    
    include 'header.php';
    
    if (isset($_SERVER['PHP_AUTH_USER'])) {
	
	$CTABNAME = "hist";
    
	include 'topmenu.php';
	
	include 'histsubmenu.php';
	
	if (isset($_POST['relaypresip'])) {
	    if (isset($FILTERCONDITION)) {
		$FILTERCONDITION = $FILTERCONDITION . "and relaypresip='".$_POST['relaypresip']."' ";
	    } else {
		$FILTERCONDITION = " where relaypresip='".$_POST['relaypresip']."' ";
	    }
	    if (isset($FILTERSCOPE)) {
		$FILTERSCOPE = $FILTERSCOPE . "<div class=\"filterscope\">Relay IP :".$_POST['relaypresip']."</div>";
	    } else {
		$FILTERSCOPE = "<div class=\"filterscope\">Relay IP :".$_POST['relaypresip']."</div>";
	    }
	}
	if (isset($_POST['connectdate'])) {
	    if (isset($FILTERCONDITION)) {
		$FILTERCONDITION = $FILTERCONDITION . "and connectdate>='".$_POST['connectdate']." 00:00:00' and connectdate<='".$_POST['connectdate']." 23:59:59' ";
	    } else {
		$FILTERCONDITION = "where connectdate>='".$_POST['connectdate']." 00:00:00' and connectdate<='".$_POST['connectdate']." 23:59:59' ";
	    }
	    if (isset($FILTERSCOPE)) {
		$FILTERSCOPE = $FILTERSCOPE . "<div class=\"filterscope\">Connection Date: ".$_POST['connectdate']."</div>";
	    } else {
		$FILTERSCOPE = "<div class=\"filterscope\">Connection Date: ".$_POST['connectdate']."</div>";
	    }
	}
	if (isset($_POST['startdate']) && isset($_POST['enddate']) && $_POST['enddate'] != "" && $_POST['startdate'] != "") {
	    if (isset($FILTERCONDITION)) {
		$FILTERCONDITION = $FILTERCONDITION . "and connectdate>='".$_POST['startdate']." 00:00:00' and connectdate<='".$_POST['enddate']." 23:59:59' ";
	    } else {
		$FILTERCONDITION = "where connectdate>='".$_POST['startdate']." 00:00:00' and connectdate<='".$_POST['enddate']." 23:59:59' ";
	    }
	    if (isset($FILTERSCOPE)) {
		$FILTERSCOPE = $FILTERSCOPE . "<div class=\"filterscope\">Connection date between: ".$_POST['startdate']." and ".$_POST['enddate']."</div>";
	    } else {
		$FILTERSCOPE = "<div class=\"filterscope\">Connection date between: ".$_POST['startdate']." and ".$_POST['enddate']."</div>";
	    }
	}
	if (isset($_POST['receivinghost'])) {
	    if (isset($FILTERCONDITION)) {
		$FILTERCONDITION = $FILTERCONDITION . "and receivinghost='".$_POST['receivinghost']."' ";
	    } else {
		$FILTERCONDITION = "where receivinghost='".$_POST['receivinghost']."' ";
	    }
	    if (isset($FILTERSCOPE)) {
		$FILTERSCOPE = $FILTERSCOPE . "<div class=\"filterscope\">Receiving Host: ".$_POST['receivinghost']."</div>";
	    } else {
		$FILTERSCOPE = "<div class=\"filterscope\">Receiving Host: ".$_POST['receivinghost']."</div>";
	    }
	}
	if (isset($_POST['action'])) {
	    if (isset($FILTERCONDITION)) {
		$FILTERCONDITION = $FILTERCONDITION . "and action='".$_POST['action']."' ";
	    } else {
		$FILTERCONDITION = "where action='".$_POST['action']."' ";
	    }
	    if (isset($FILTERSCOPE)) {
		$FILTERSCOPE = $FILTERSCOPE . "<div class=\"filterscope\">Only msgs with action: ".$_POST['action']."</div>";
	    } else {
		$FILTERSCOPE = "<div class=\"filterscope\">Only msgs with action: ".$_POST['action']."</div>";
	    }
	}
	if (isset($_POST['envfrom']) && $_POST['envfrom'] != "") {
	    if (isset($FILTERCONDITION)) {
		$FILTERCONDITION = $FILTERCONDITION . "and envfrom ilike '%".$_POST['envfrom']."%' ";
	    } else {
		$FILTERCONDITION = "where envfrom ilike '%".$_POST['envfrom']."%' ";
	    }
	    if (isset($FILTERSCOPE)) {
		$FILTERSCOPE = $FILTERSCOPE . "<div class=\"filterscope\">Only msgs From: ".$_POST['envfrom']."</div>";
	    } else {
		$FILTERSCOPE = "<div class=\"filterscope\">Only msgs From: ".$_POST['envfrom']."</div>";
	    }
	}
	if (isset($_POST['gimmedates']) && $_POST['gimmedates'] == "1") {
	    $GIMMEDATES = 1;
	}
	
	include 'histheader.php';
    
	readconfig();
	if (db_connect() == 0) {
	    include 'histbody.php';
	};
	
    } else {
	include 'error.php';
    };
    
    include 'footer.php';
?>
