<?php

require_once 'dbtools.php';

function getxmlhead(){
    header("Content-type: text/xml");
    header("Cache-Control: no-cache");
    print("<?xml version=\"1.0\"?>\n");
    print("<response>\n");
}

function getxmlfoot(){
    print("</response>\n");
}

if (isset($_SERVER['PHP_AUTH_USER'])) {
    if (!isset($DBCONN)) {
	readconfig();
        db_connect();
    }

    $errfieldid = "none";

    if (isset($_POST['comment']) && $_POST['comment'] != "") {
        $comment = $_POST['comment'];
    } else {
        $errfieldid = "comment";
    }
    if (isset($_POST['relaypresip']) && $_POST['relaypresip'] != "") {
        $relaypresip = $_POST['relaypresip'];
    } else {
        $errfieldid = "relaypresip";
    }
	
    // general check for correct data
    if ($errfieldid == "none") {	
    	$insertresult = pg_query($DBCONN, "insert into whitelist
	    (whitelisteddate, whitelistedby, address, comment) 
	    values('".gettoday()."','".$_SERVER['PHP_AUTH_USER']."','".$relaypresip."','".$comment."');");
	if ($insertresult) {
	    getxmlhead();
	    print("\t<status>200</status>\n");
	    getxmlfoot();
	} else {
    	    getxmlhead();
	    print("\t<status>500</status>\n");
	    print("\t<errormessage>".pg_last_error($DBCONN)."</errormessage>\n");
	    getxmlfoot();
	}
    } else {
        getxmlhead();
        print("\t<status>400</status>\n");
        print("\t<errorfield>".$errfieldid."</errorfield>\n");
        getxmlfoot();
    }
} else {
    getxmlhead();
    print("\t<status>401</status>\n");
    getxmlfoot();
}
?>
