<?php
	include_once("AuthentMessage.php");
	
	class AuthentComms{
		
		//URL to send xmls to
		var $authentifyURL;
		var $pollURL;
		//Teid for polling
		var $teid;
		
		//LastError
		var $lastError;
		
		//Constructor,  one may send in the data if you get it from some other source, rather than hardcoding it here.
		function AuthentComms($url="https://achdmp01/s2s/default.asp"){
			$this->authentifyURL = $url;
		}
		
		//returns LastError
		function getLastError(){
			return $this->lastError;
		}
		
		//Takes a message for request params,  returns a message of the response.
		function sendMessage( $message ){
			$xml = $message->toXML();
			$this->lastError = "";
			$returnXML = $this->send($xml);
			if( !isset($returnXML) ){
				return null;
			}
			$msg = new Message($returnXML);
			$this->teid = $msg->teid;
			if(isset($msg->replyTo)){
				$this->pollURL = $msg->replyTo;
			}
			return $msg;
		}
		
		function sendPoll($teid=null){
			if(!isset($teid)){
				$teid = $this->teid;
			}
			$xml="<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>
<AuthentXML xmlns=\"http://xml.authentify.net/MessageSchema.xml\" version=\"1.0\">
<header>
<teid>".$teid."</teid>
</header>
<body>
<poll />
</body>
</AuthentXML>";
$this->lastError = "";
$returnXML = $this->send($xml);
if( !isset($returnXML) ){
	return null;
}
$msg = new Message($returnXML);
return $msg;
}

function send($data, $url=null){
    $ch = null;
    if(isset($url)){
		//      echo "Using URL";
		$ch = curl_init($url);
    }else{
		//      echo "Using authentifyURL";
		$ch = curl_init($this->authentifyURL);
    }
	
    //    echo $url;
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
    if(!empty($data)){
		curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }
    $output = null;
    ob_start();
    if(!curl_exec($ch)){
		$this->lastError = "Error: ".curl_error($ch).".  Error Number:".curl_errno($ch);
    }else{
		$output = ob_get_contents();
    }
    ob_end_clean();
    curl_close($ch);
    return $output;
}

}
?>
