<?PHP
#This file constructs a query to send to NCMS for snoopy questions.
#The page returned is stuffed into $snoopyfile as determined by snoopy.php
#enrollee and address are extracted from the SESSION in .htconfig.php.
progress(__FILE__,__LINE__,"We are constructing a query for $subjectid.");
$license=$_SESSION['license'];
$phone=$_SESSION['landPhone'];
$ssn=$_SESSION['ssn'];

$args["strSocialSecurity"]=$ssn;
$args["strOwnNameLast"]=$enrollee["last_name"];
$args["strCurAddrCity"]=$address['city'];
$addst=$address['state'];
if (strlen($addst) == 2)
  $args["strCurAddrState"]=$addst;
else
{
  if (!isset($link)) $link=databaseconnect();
  $ast=mysql_getOne("select state_code from state where state_name = '$addst'");
  if (is_null($ast)) $ast=mysql_getOne("select state_code from state where state_name like '%$addst%'");
  if (is_null($ast)) $ast=substr($addst,0,2);
  $args["strCurAddrState"]=$ast;
}
$args["strCurAddrZip"]=$address["zipCode"];
$args["strDrivingLicense"]=$license["number"];
$args["strTelephone"]=$phone;
$licst=$license["issuer"];
progress(__FILE__,__LINE__,"The license is from $licst.");
if (strlen($licst) == 2)
  $args["strDrivingLicenseState"]=$licst;
else
{
  if (!isset($link)) $link=databaseconnect();
  $dlst=mysql_getOne("select state_code from state where state_name = '$licst'");
  if (is_null($dlst))
    $dlst=mysql_getOne("select state_code from state where state_name like '%$licst%'");
  if (is_null($dlst))
    $dlst=substr($licst,0,2);
  $args["strDrivingLicenseState"]=$dlst;
}
$args["strCustRefNumber"]=NCMSUSER;
$args["strCustPassword"]=NCMSPASSWORD;
$args["strOwnNameFirst"]=$enrollee["first_name"];
$args["strAddress"]=$address["street_line_01"];
$args["strEmail"]=$enrollee['email'];
$args["strIPAddress"]=$_SERVER["REMOTE_ADDR"];
$args["strLanguage"]="en";
buggywuggy(print_r($args,TRUE));

buggywuggy("Going to NCMS.");
$url=NCMSURL."?".http_build_query($args);
buggywuggy("Going to NCMS with $url.");
loggywoggy("Going to NCMS with $url.");
#disabled with url_fopen, replaced with readurl in .htconfig.php
#$res=file_get_contents($url);
$res=readurl($url);
if (strlen($res) == 0)
{
  reporterror("Could not get the PII from NCMS with $url");
}
else
{
  file_put_contents($snoopyfile,$res);
  #echo "<BR>We just stored<BR>$res<BR>in $snoopyfile.";
}
?>