<?php
	#authentificate.php stolen in large from testXML.php from Authentify.
	#included by index.php when phone authentification is needed.
	buggywuggy("authentificate.php with skipcall=$skipcall and testcall=$testcall.");
	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 ($page)
	{
		case "startcall":
			include_once(".htauthentify.inc.php");
			include("AuthentComms.php");
			if ($skipcall == 1) break;
			$msg = new Message();
			#$msg->tsoid = "Authentrus";
			#$msg->application = "PhoneConfirmRec";
			#$msg->password = "1mpl#NV!";
			#$msg->asid = "ExampleASID_USER0001";
			#$msg->action = "PhoneSession";
			$msg->tsoid = TSOID;
			$msg->application = APPLICATION;
			$msg->password = PASSWORD;
			$msg->action = ACTION;
			$msg->type = "request";
			#$msg->data["phoneNumber"] = new DataNode("7732430225");
			#$msg->data["confirmNumber"] = new DataNode("1234");
			#$msg->asid=$subject["subjectid"];
			$msg->tsoid="Auth_Sandbox";
			$msg->application="PinDelivery";
			$msg->account="AuthenticityInstitute";
			$msg->licenseKey="dc391483-473c-4c3a-80d8-61ce0d0f60a9";
	/*
			<tsoid>Auth_Sandbox</tsoid>
			<application>PinDelivery</application>
			<account>AuthenticityInstitute</account>
			<licenseKey>dc391483-473c-4c3a-80d8-61ce0d0f60a9</licenseKey>
	 */
			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);
			$comms = new AuthentComms("https://imp.authentify.com/s2s/default.asp");
			#$msgs.= htmlize($msg->toXML());
			#echo "<br>";
			#echo "We are about to send our authentification request.";
			$newmsg = $comms->sendMessage($msg);
			if (is_null($newmsg))
			{
				echo "<br/>We did not communicate.<br/>".$comms->lastError;
				authentify_error($msg,$comms,__LINE__,__FILE__);
				die();
			}
			if ($newmsg->statusCode != 0)
			{
				echo "<br/>We failed to communicate.<br/>".$comms->lastError;
				authentify_error($msg,$comms,__LINE__,__FILE__);
				die();
			}

			#send the above message and retrieve a response.
			$msgs.="<BR>We just sent the request and got back<BR>".htmlize($newmsg->toXML())."<BR>";
			if( !isset($newmsg) )
			{
				loggywoggy("Our attempt to contact authentify for transaction ".$transaction["transactionid"]." at number "
						   .$phone["number"]." failed because of\n".$comms->lastError.".");
				
				echo $comms->lastError;
				echo "<BR>I guess this means that you won't get your certificate just yet.";
			}
			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":
			if ($skipcall == 1) break;
			$iter=0;
			while(isset($newmsg) && $newmsg->type != "result")
			{
				loggywoggy("Iteration $iter for transaction ".$transaction["transactionid"]." no result yet.");
				echo "<BR>Iteration ".$iter++;
				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>";
				ob_flush();
				flush();
			}
			$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 page=$page.");
			break;
	}
	?>