<?php
#authentificate.php stolen in large from testXML.php from authentify.
#included by index.php when authentification is needed.
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;
	}
}

if (!function_exists("parse_authentify_response"))
{
function parse_authentify_response($statuscode)
{
	$a="";
	switch ($statuscode)
	{
		case 0:
			break;
		case 2142:
			$a.="Invalid tsoid for Authentify with tsoid=".TSOID." and application=".APPLICATION.".";
			break;
		case 2510:
			$a.="The XML for Authentify was inproperly formatted.";
			break;
		default:
			$a.="Authentify returned $statuscode.";
	}
	return $a;
}
}

buggywuggy("We are in ".__FILE__." for $page.");
switch ($page)
{
	case "startcall":
		buggywuggy("We are doing the startcall page.");
		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->type = MSGTYPE;
		#$msg->data["phoneNumber"] = new DataNode("7732430225");
		#$msg->data["confirmNumber"] = new DataNode("1234");
		#$msg->asid=$subject["subjectid"];
		if ($testcall ==1)
		{
			buggywuggy("Because skipcall is $skipcall, we are calling Peter Hadley's cellular telephone.");
			$msg->data["phoneNumber"] = new DataNode("+1-617-413-0706");
			$msg->data["confirmNumber"] = new DataNode("1234");
		}
		else
		{
			buggywuggy("Because skipcall is $skipcall, we are calling the designated phone number for transaction "."fred"." which is ".$phone["number"].".");
			$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);
		#$msgs.= htmlize($msg->toXML());
		#echo "<br>";
		#echo "We are about to send our authentification request.";
		$newmsg = $comms->sendMessage($msg);
		#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."-".parse_authentify_response($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>";
		buggywuggy ("Iteration $iter got statuscode=$statuscode for transaction ".$transaction["transactionid"].".");
		if($statuscode != 0) buggywuggy("This means ".parse_authentify_response($statuscode).".");
		break;
	default:
		admin_notify("We got to the bottom of authentificate.php with page=$page.");
		break;
}
?>
