<?php
session_start();

$path='http://192.168.0.11/cybermatch/cybermatch/tmpfiles/';
//$path='http://www.homewidget.com/clients/dcdollardate/tmpfiles/';

function get_sub_string($from="", $to="", $string="")
	{
		$pos	= strpos($string, $from);
		if ($pos === false) {
			$return	= "";
		} else {
			$pos1	= strpos($string, $to);
			if ($pos1 === false) {
				$return	= "";
			} else {
				$return	= substr($string, $pos+strlen($from), $pos1-($pos+strlen($to)-1));
			}
		}
		return $return;
	}
	
if(isset($_POST['btnGo']) && $_POST['btnGo']=='Check')
{

		$string		= "";
		$passed		= "NO";
		$msg		= "Invalid input parameters";
		$qtyFound	= 0;
		$qtyReturn	= 0;
		
		$bgc_url	 = $_POST['vURL'];
		//$bgc_url	 ='https://model.backgroundchecks.com/integration/bgcdirectpost.aspx';
//		$bgc_url	 ='https://online.backgroundchecks.com/integration/bgcdirectpost.aspx';

		
		$user		= $_POST['vUserName'];
		$pass		= $_POST['vPassword'];
		$account	= $_POST['vAccount'];
		$firstName	= addslashes($_POST['vFirstName']);
		$middleName	= addslashes($_POST['vMiddleName']);
		$lastName	= addslashes($_POST['vLastName']);
		$suffixCode	= "";
		$ssn		= addslashes($_POST['vSSN']);
		$month		= addslashes($_POST['vMM']);
		$day		= addslashes($_POST['vDD']);
		$year		= addslashes($_POST['vYYYY']);
		$street		= addslashes($_POST['vStreet']);
		$city		= addslashes($_POST['vCity']);
		$state		= addslashes($_POST['vState']);
		$postalCode			= addslashes($_POST['vPostCode']);
		$telephoneNumber	= addslashes($_POST['vPhone']);

		$xmlrequest = '<?xml version="1.0" encoding="us-ascii" standalone="yes"?>
		<BGC>
		<login>
			<user>'.$user.'</user>
			<password>'.$pass.'</password>
			<account>'.$account.'</account>
		</login>
		<product>
			<USOneTrace>
				<order>
					<ssn>'.$ssn.'</ssn>
					<firstName>'.$firstName.'</firstName>
					<lastName>'.$lastName.'</lastName>
					<DOB>
						<month>'.$month.'</month>
						<day>'.$day.'</day>
						<year>'.$year.'</year>
					</DOB>
				</order>
			</USOneTrace>
		</product>
		</BGC>';
		
			//for testing
				//$_SESSION['test_before_file']='USOneTrace Request to Server'."\n\n".$xmlrequest;
				$_SESSION['test_before_file']=$xmlrequest;
			//teting end
		
		//separate the request URL and parameters into separate variables
		//initialize curl session $session = curl_init($request);
		$ch = curl_init();
		//set curl options to set to use https post point to url
		curl_setopt($ch, CURLOPT_URL, $bgc_url);
		curl_setopt($ch, CURLOPT_POST, TRUE);
		//specify which fields to post
		
		//for goddady server
		/*curl_setopt ($ch, CURLOPT_HTTPPROXYTUNNEL, TRUE);
		curl_setopt ($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
		curl_setopt ($ch, CURLOPT_PROXY,"http://proxy.shr.secureserver.net:3128");
		//for goddady server*/
		
		curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlrequest);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
		//set authentication and security
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
		curl_setopt($ch, CURLOPT_TIMEOUT, CURL_TIMEOUT);
		//run the curl_exec to execute the post $response = curl_exec($session);
		$xml_report = curl_exec ($ch);
		//close curl resource and free up resources
		curl_close ($ch);


		$xml_report = preg_replace("/<form[^~]*<\/form>/","", $xml_report,1);
		$xml_report = preg_replace("/<script>[^~]*<\/script>/","", $xml_report,1);

		$xml	= get_sub_string("<response>", "</response>", $xml_report);
		if ($xml <> "") {
				$xml_report	= $xml;
		
			//for testing
				//$_SESSION['test_before_file_response']='USOneTrace Response from Server'."\n\n".$xml_report;
				$_SESSION['test_before_file_response']=$xml_report;
			//teting end

	    } else {
			$msg		= strip_tags(get_sub_string("<warnings>", "</warnings>", $xml_report));
			$msg		.= strip_tags(get_sub_string("<errors>", "</errors>", $xml_report));
			
			//for testing
				//$_SESSION['test_before_file_response'].='USOneTrace Error Response from Server'."\n\n".$msg;
				$_SESSION['test_before_file_response'].=$msg;
			//teting end
		}


		$array	= array();
		$arrRes	= array();
		$myarray= array();
		$array	= explode("<record>", $xml_report);

		foreach ($array as $key => $value) {
			$arrRes[]['fname']	= strtoupper(trim(get_sub_string("<firstName>", "</firstName>", $value)));
			$arrRes[]['mname']	= strtoupper(trim(get_sub_string("<middleName>", "</middleName>", $value)));
			$arrRes[]['lname']	= strtoupper(trim(get_sub_string("<lastName>", "</lastName>", $value)));
			$arrRes[]['age']	= strtoupper(trim(get_sub_string("<age>", "</age>", $value)));
		}

		if (!empty($arrRes)) {
			foreach ($arrRes as $key => $value) {
				if (($value['fname'] == strtoupper($firstName))
					AND ($value['mname'] == strtoupper($middleName))
					AND ($value['lname'] == strtoupper($lastName))
					AND ($value['age'] == get_age_from_date($year.$month.$day))) {
					$myarray	= $value;
				}
			}
		}

		if (!empty($myarray)) {
			$xmlrequest = '<?xml version="1.0" encoding="us-ascii" standalone="yes"?>
			<BGC>
			<login>
				<user>'.$user.'</user>
				<password>'.$pass.'</password>
				<account>'.$account.'</account>
			</login>
			<product>
				<USOneSearch>
					<order>
						<firstName>'.$firstName.'</firstName>
						<middleName>'.$middleName.'</middleName>
						<lastName>'.$lastName.'</lastName>
						<DOB>
						  <month>'.$month.'</month>
						  <day>'.$day.'</day>
						  <year>'.$year.'</year>
						</DOB>
					</order>
				</USOneSearch>
			</product>
			</BGC>';
			
			//for testing
				//$_SESSION['test_before_file2']='USOneSearch Request to Server'."\n\n".$xmlrequest;
				$_SESSION['test_before_file2']=$xmlrequest;
			//teting end
				
			//separate the request URL and parameters into separate variables
			//initialize curl session $session = curl_init($request);
			$ch = curl_init();
			//set curl options to set to use https post point to url
			curl_setopt($ch, CURLOPT_URL, $bgc_url);
			curl_setopt($ch, CURLOPT_POST, TRUE);
			//specify which fields to post
			
			curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlrequest);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
			
			//for goddady server
		/*curl_setopt ($ch, CURLOPT_HTTPPROXYTUNNEL, TRUE);
		curl_setopt ($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
		curl_setopt ($ch, CURLOPT_PROXY,"http://proxy.shr.secureserver.net:3128");
		//for goddady server*/
		
			//set authentication and security
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
			curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
			curl_setopt($ch, CURLOPT_TIMEOUT, CURL_TIMEOUT);
			//run the curl_exec to execute the post $response = curl_exec($session);
			$xml_report = curl_exec ($ch);
			//close curl resource and free up resources
			curl_close ($ch);

			$xml_report = preg_replace("/<form[^~]*<\/form>/","", $xml_report,1);
			$xml_report = preg_replace("/<script>[^~]*<\/script>/","", $xml_report,1);

			$xml	= get_sub_string("<response>", "</response>", $xml_report);
			if ($xml <> "") {
				$xml_report	= $xml;
				//for testing
					//$_SESSION['test_before_file_response2']='USOneSearch Response from Server'."\n\n".$xml_report;
					$_SESSION['test_before_file_response2']=$xml_report;
				//teting end
			
			} else {
				$msg		= strip_tags(get_sub_string("<warnings>", "</warnings>", $xml_report));
				$msg		.= strip_tags(get_sub_string("<errors>", "</errors>", $xml_report));
				
				//for testing
					//$_SESSION['test_before_file_response2']='USOneSearch Response from Server'."\n\n".$xml_report;
					$_SESSION['test_before_file_response2']=$xml_report;
				//teting end
			
			}

			$qtyFound	= get_sub_string("qtyFound=\"", "\"", $xml_report);
			$qtyReturn	= get_sub_string("qtyReturned=\"", "\"", $xml_report);

			if ($qtyFound > 0 OR $qtyReturn > 0)
				$passed	= "NO";
			else
				$passed	= "YES";

		}
	$msg='yes';

}//end if

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Testing  Background Check</title>
</head>
<body>
<form action="" method="post">
<table width="100%"  border="0" cellspacing="1" cellpadding="5">
<?php
	if(isset($msg) && $msg=='yes')
	{
		$n1=rand(100,111).'_'.time().'trace_req.xml';
		$fp=@fopen('./tmpfiles/'.$n1,'w+');
		@fwrite($fp,$_SESSION['test_before_file']);
		@rewind($fp);
		@fclose($fp);
		
		$n2=rand(100,111).'_'.time().'trace_resp.xml';
		$fp=@fopen('./tmpfiles/'.$n2,'w+');
		@fwrite($fp,$_SESSION['test_before_file_response']);
		@rewind($fp);
		@fclose($fp);
		
		$n3=rand(100,111).'_'.time().'search_req.xml';
		$fp=@fopen('./tmpfiles/'.$n3,'w+');
		@fwrite($fp,$_SESSION['test_before_file2']);
		@rewind($fp);
		@fclose($fp);
		
		$n4=rand(100,111).'_'.time().'search_resp.xml';
		$fp=@fopen('./tmpfiles/'.$n4,'w+');
		@fwrite($fp,$_SESSION['test_before_file_response2']);
		@rewind($fp);
		@fclose($fp);
		
		$_SESSION['test_before_file']='';
		$_SESSION['test_before_file_response']='';
		$_SESSION['test_before_file2']='';
		$_SESSION['test_before_file_response2']='';
?>
<tr align="left">
    <td width="34%" colspan="2">Request XML <a href="<?php echo $path.$n1;?>" target="_blank">USOneTrace Request to Server</a><br>
      <br>
	Response XML <a href="<?php echo $path.$n2;?>" target="_blank">USOneTrace Response from Server</a><br><br>
	Request XML <a href="<?php echo $path.$n3;?>" target="_blank">USOneSearch Request to Server</a>
	Response XML <a href="<?php echo $path.$n4;?>" target="_blank">USOneSearch Response from Server</a></td>
  </tr>
<?php }//end if?>  
 <tr align="left">
    <td width="34%" colspan="2">
		<strong>Backgroung check URL</strong><br>
		Test mode : https://model.backgroundchecks.com/integration/bgcdirectpost.aspx<br>
		Live mode : https://online.backgroundchecks.com/integration/bgcdirectpost.aspx<br>	
	</td>
  </tr>
  <tr>
    <td align="right">Backgroung check URL</td>
    <td align="left"><input name="vURL" type="text" class="inputbox" value="https://online.backgroundchecks.com/integration/bgcdirectpost.aspx" size="80" maxlength="200"></td>
  </tr>
 <tr align="left">
    <td width="34%" colspan="2"><strong>Account Information</strong></td>
  </tr>
  <tr>
    <td align="right">User Name</td>
    <td align="left"><input name="vUserName" type="text" class="inputbox" value="mdickey" size="30" maxlength="100"></td>
  </tr>
  <tr>
    <td align="right">Password</td>
    <td align="left"><input name="vPassword" type="text" class="inputbox" value="dcdate@123" size="30" maxlength="100"></td>
  </tr>
  <tr>
    <td align="right">Account</td>
    <td align="left"><input name="vAccount" type="text" class="inputbox" value="10013649" size="30" maxlength="100"></td>
  </tr>
  <tr align="left">
    <td colspan="2"><strong>User Information </strong></td>
    </tr>
  <tr>
    <td width="34%" align="right">First Name *</td>
    <td width="66%" align="left"><input name="vFirstName" type="text" class="inputbox" value="gertrude" size="30" maxlength="200"></td>
  </tr>
  <tr>
    <td align="right">Middle Name</td>
    <td align="left"><input name="vMiddleName" type="text" class="inputbox" value="x" size="30" maxlength="100"></td>
  </tr>
  <tr>
    <td align="right">Last Name</td>
    <td align="left"><input name="vLastName" type="text" class="inputbox"  value="harkenreadeo" size="30" maxlength="200"></td>
  </tr>
  <tr>
    <td align="right">SSN *</td>
    <td align="left"><input name="vSSN" type="text" class="inputbox"  value="255066112" size="30" maxlength="200"></td>
  </tr>
  <tr>
    <td align="right">DOB *</td>
    <td align="left">Day:
                                    <input name="vDD" type="text" class="inputbox" value="8" size="2" maxlength="2">
Month:
<input name="vMM" type="text" class="inputbox" value="12" size="2" maxlength="2">
Year:
<input name="vYYYY" type="text" class="inputbox" value="1953" size="6" maxlength="4"></td>
  </tr>
  <tr>
    <td align="right">Street *</td>
    <td align="left"><input name="vStreet" type="text" class="inputbox" value="305 linden bv" size="30" maxlength="100"></td>
  </tr>
  <tr>
    <td align="right">City</td>
    <td align="left"><input name="vCity" type="text" class="inputbox" value="atlanta" size="30" maxlength="100"></td>
  </tr>
  <tr>
    <td align="right">State *</td>
    <td align="left"><input name="vState" type="text" class="inputbox" value="GA" size="30" maxlength="100"></td>
  </tr>
  <tr>
    <td align="right">Postal Code *</td>
    <td align="left"><input name="vPostCode" type="text" class="inputbox" value="30316" size="30" maxlength="100"></td>
  </tr>
  <tr>
    <td align="right">Telephone Number *</td>
    <td align="left"><input name="vPhone" type="text" class="inputbox" value="255066112" size="30" maxlength="100"></td>
  </tr>
  <tr>
    <td align="right">&nbsp;</td>
    <td align="left"><input type="submit" name="btnGo" value="Check"></td>
  </tr>
</table>

</form>
<?php include_once('track_code.inc.php');?>
</body>
</html>
