<?php	#valID/authentificate.php
#authentificate.php stolen in large from testXML.php from authentify.
#included by index.php in two different places:
#	the first time is with $display="startcall" which intitiates an Authentify call
#	the second time is with $display="callwaiting" which iterates until the 
#		phone call is resolved
if (!function_exists("htmlize"))
{
	function htmlize($str)
	{
		$out = str_replace("<", "&lt;", $str);
		$out = str_replace(">", "&gt;", $out);
		$out = str_replace("\t", " ", $out);
		$out = str_replace(" ", "&nbsp;", $out);
		$out = str_replace("\n", "<br>\n", $out); return $out;
	}
}
switch ($display)
{
	case "startcall":
		include_once(".htauthentify.inc.php");
		include("AuthentComms.php");
		buggywuggy("We are setting up the call but skipcall=$skipcall.");
		if ($skipcall == 1) break;
		$msg = new Message();
		$msg->tsoid = TSOID;
		$msg->application = APPLICATION;
		$msg->password = PASSWORD;
		$msg->action = ACTION;
		$msg->type = "request";
		if ($testcall ==1)
		{
			$msg->data["phoneNumber"] = new DataNode("+1-617-413-0706");
			$msg->data["confirmNumber"] = new DataNode("1234");
		}
		else
		{
			$msg->data["phoneNumber"] =new DataNode($phone["number"]);
			$msg->data["confirmNumber"] = new DataNode($transaction["confirmnumber"]);
		}
		$msg->data["language"] = new DataNode("en-us");
		$msg->asid=ASID;
		$comms = new AuthentComms(AUTHENTIFY_URL);
		$newmsg = $comms->sendMessage($msg);
		#send the above message and retrieve a response.
		if( !isset($newmsg) )
		{
			loggywoggy("Our attempt to contact authentify for transaction ".$transaction["transactionid"]." at number "
				.$phone["number"]." failed because of\n".$comms->lastError.".");

			echo "<BR>We have encountered an error which will prevent your getting a certificate.  Please contact us.";
			echo "<BR>".$comms->lastError."<BR>";
		}
		else
		{
			loggywoggy("We are asking for authentication for transaction "
				.$transaction["transactionid"]." at ".$phone["number"]
				." with ".$transaction["confirmnumber"].".");
			$msgs.="<BR>We have asked to have you authenticated at "
				.$phone["number"]." with ".$transaction["confirmnumber"].".";
		}
		break;
	case "callwaiting":
		$iter=0;
		if ($skipcall == 1)
		{
			for ($i=1;$i<2;$i++)
			{
				echo "<BR>Not calling--Iteration $i.";
				ob_flush();
				flush();
				sleep(WAITTIME);
			}
			break;
		}
		while(isset($newmsg) && $newmsg->type != "result")
		{
			loggywoggy("Iteration $iter for transaction ".$transaction["transactionid"]." no result yet.");
			echo "<BR>Iteration ".++$iter;
			ob_flush();
			flush();
			sleep(WAITTIME);
			$newmsg = $comms->sendPoll();
			if( !isset($newmsg) )
			{
				loggywoggy("Iteration $iter of transaction ".$transaction["transactionid"]." failed with this error:\n".$comms->lastError);
				echo $comms->lastError;
				break;
			}
			#echo htmlize($newmsg->toXML());
			#echo "<br>";
		}
		$statuscode=$newmsg->statusCode;
		#echo "<BR>With a status code of $statuscode, at ".$newmsg->timestamp." we have this tree:<BR>".htmlize($newmsg->toXML())."<BR>";
		loggywoggy ("Iteration $iter got statuscode=$statuscode for transaction ".$transaction["transactionid"].".");
		break;
	default:
		PKH_error("We got to the bottom of authentificate.php with display=$display.");
		break;
}
?>