<?php
require_once("functions.php");

/*
$termID = "DAT02230";
$userID = "OmarButt3";
$pw = "atm0987";
*/

if(array_key_exists('userid', $_POST) && array_key_exists('pw', $_POST) && array_key_exists('termid', $_POST)){
	$userID = $_POST['userid'];
	$pw = $_POST['pw'];
	$termID = $_POST['termid'];
}
else{
	$userID = "OmarButt";
	$pw = "Atm0987";
	$termID = "EF001607";
}

$url = "https://www.efxatm.com/login/login.aspx?ReturnUrl=%2fDefault.aspx";
$request = curl_init($url);
curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($request, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($request, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt($request, CURLOPT_HEADER, 1);
$response = curl_exec($request);
curl_close($request);

$posturl = substr($response, strpos($response, 'action="')+strlen('action="'), strpos($response, "\"", strpos($response, 'action="')+strlen('action="')) - (strpos($response, 'action="')+strlen('action="')));

$cookie[0] = substr($response, strpos($response, 'Set-Cookie: ')+strlen('Set-Cookie: '), strpos($response, ";", strpos($response, 'Set-Cookie: ')+strlen('Set-Cookie: ')) - (strpos($response, 'Set-Cookie: ')+strlen('Set-Cookie: ')));

$cookies = implode("; ", $cookie);


//////////



$postfieldsarray = array("__VIEWSTATE" => strfrmto($response, '__VIEWSTATE" value="', '"'), "__EVENTVALIDATION" => strfrmto($response, '__EVENTVALIDATION" value="', '"'), "txtLogin" => "$userID", "txtPassword" => "$pw", "CmdGo" => "Login");

//$postfields = "__VIEWSTATE=%2FwEPDwULLTExMTMwNzQ0ODIPZBYCAgEPZBYCAgcPFgIeB1Zpc2libGVoZGS66vGzkl1LlIdbZuTcwWuCGLJOuQ%3D%3D&__EVENTVALIDATION=%2FwEWBAKukrKGAgKG87HkBgK1qbSRCwLEioisCL8WlHlukdEmOD7XT%2FzQ%2Bi51RAds&txtLogin=OmarButt&txtPassword=Atm0987&cmdGo=Login";

$postfields = "";

foreach($postfieldsarray as $field => $val){
	
	$valweb = urlencode($val);
	$fieldweb = urlencode($field);
	$postfields = $postfields."&$fieldweb=$valweb";
	
}
$postfields = substr($postfields, 1);




$url = "https://www.efxatm.com/login/".$posturl;
$request = curl_init($url);
curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($request, CURLOPT_CRLF, 1);
curl_setopt($request, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($request, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt($request, CURLOPT_HEADER, 1);
curl_setopt($request, CURLOPT_POST, 1);
curl_setopt($request, CURLOPT_COOKIE, $cookies);
curl_setopt($request, CURLOPT_POSTFIELDS, $postfields);
$response = curl_exec($request);
curl_close($request);

$url = "https://www.efxatm.com/login/".$posturl;
$request = curl_init($url);
curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($request, CURLOPT_CRLF, 1);
curl_setopt($request, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($request, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt($request, CURLOPT_HEADER, 1);
curl_setopt($request, CURLOPT_POST, 1);
curl_setopt($request, CURLOPT_COOKIE, $cookies);
curl_setopt($request, CURLOPT_POSTFIELDS, $postfields);
$response = curl_exec($request);
curl_close($request);

if(!strpos($response, "Location: ")){
	echo "Wrong Password";
	exit;
}

$posturl = strfrmto($response, "Location: ", "aspx");
$posturl = $posturl."aspx";

array_push($cookie, substr($response, strpos($response, 'Set-Cookie: ')+strlen('Set-Cookie: '), strpos($response, ";", strpos($response, 'Set-Cookie: ')+strlen('Set-Cookie: ')) - (strpos($response, 'Set-Cookie: ')+strlen('Set-Cookie: '))));

$cookies = implode("; ", $cookie);


$url = $posturl;
$request = curl_init($url);
curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($request, CURLOPT_CRLF, 1);
curl_setopt($request, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($request, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt($request, CURLOPT_HEADER, 1);
curl_setopt($request, CURLOPT_COOKIE, $cookies);
$response = curl_exec($request);
curl_close($request);


$url = "https://www.efxatm.com/addins/TerminalManager/TerminalProperties.aspx?terminalID=$termID";
$request = curl_init($url);
curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($request, CURLOPT_CRLF, 1);
curl_setopt($request, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($request, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt($request, CURLOPT_HEADER, 1);
curl_setopt($request, CURLOPT_COOKIE, $cookies);
$response = curl_exec($request);
curl_close($request);




$postfieldsarray = array("__EVENTTARGET" => 'btnGo', "__VIEWSTATE" => strfrmto($response, '__VIEWSTATE" value="', '"'), "__EVENTVALIDATION" => strfrmto($response, '__EVENTVALIDATION" value="', '"'), "txtTermID" => "" , "Tab_Devices" => strfrmto($response, 'Tab_Devices" value="', '"'));


$postfields = "";

foreach($postfieldsarray as $field => $val){
	
	$valweb = urlencode($val);
	$fieldweb = urlencode($field);
	$postfields = $postfields."&$fieldweb=$valweb";
	
}
$postfields = substr($postfields, 1);


$url = "https://www.efxatm.com/addins/TerminalManager/TerminalProperties.aspx?terminalID=$termID";
$request = curl_init($url);
curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($request, CURLOPT_CRLF, 1);
curl_setopt($request, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($request, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt($request, CURLOPT_HEADER, 1);
curl_setopt($request, CURLOPT_POST, 1);
curl_setopt($request, CURLOPT_COOKIE, $cookies);
curl_setopt($request, CURLOPT_POSTFIELDS, $postfields);
$response = curl_exec($request);
curl_close($request);

$errorResponse = $response;
$errorResponse = strfrmto($errorResponse, '<table class="DataTable', '</table>');
$errorResponse = substr($errorResponse, strpos($errorResponse, '<tr>') + strlen('<tr>'));

$errorRowsArray = explode('<tr', $errorResponse);
array_pop($errorRowsArray);

$outputString = "";

foreach($errorRowsArray as $key => $val){
	
	$errorRowsArray[$key] = explode('<td>', substr($val, strpos($val, '<')));
	array_shift($errorRowsArray[$key]);
	foreach($errorRowsArray[$key] as $colKey => $colVal){
		
		$colVal = substr($colVal, 0, strpos($colVal, '</td>'));
		$errorRowsArray[$key][$colKey] = $colVal;
		
	}

	foreach($errorRowsArray[$key] as $colVal){
		
		$outputString .= $colVal."\t";
		
	}
	
	$outputString = substr($outputString, 0, -1);
	
	$outputString .= "\r\n";

}
$outputString = substr($outputString, 0, -2);

$outputString = str_replace("&nbsp;", " ", $outputString);

echo $outputString;//."\r\n\r\n\r\n\r\n<textarea rows=\"200\" cols=\"100\">$postfields\r\n\r\n\r\n$transHistUrl\r\n\r\n\r\n$tranHistResponse\r\n\r\n\r\n$response</textarea>";
?>