<?php


/*
$userID = "OmarButt3";
$pw = "atm0987";
$lookupMonth = "January";
$lookupYear = "2011";
*/

require_once("functions.php");

if(array_key_exists('userid', $_POST) && array_key_exists('pw', $_POST) && array_key_exists('lookupmonth', $_POST) && array_key_exists('lookupyear', $_POST)){
	$userID = $_POST['userid'];
	$pw = $_POST['pw'];
	$lookupMonth = $_POST['lookupmonth'];
	$lookupYear = $_POST['lookupyear'];
}
else{
	$userID = "OmarButt";
	$pw = "Atm0987";
	$lookupMonth = "February";
	$lookupYear = "2011";
}


$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/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.15) Gecko/20110303 Firefox/3.6.15");
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/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.15) Gecko/20110303 Firefox/3.6.15");
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);

$response1 = $response;

if(strpos($response, 'Set-Cookie: ')!=0){
	$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/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.15) Gecko/20110303 Firefox/3.6.15");
	curl_setopt($request, CURLOPT_HEADER, 1);
	curl_setopt($request, CURLOPT_COOKIE, $cookies);
	$response = curl_exec($request);
	curl_close($request);
	
	$response2 = $response;
	
		
	
	
	$url = "https://www.efxatm.com/addins/Reports/rptIndex.aspx";
	$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/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.15) Gecko/20110303 Firefox/3.6.15");
	curl_setopt($request, CURLOPT_HEADER, 1);
	curl_setopt($request, CURLOPT_COOKIE, $cookies);
	$response = curl_exec($request);
	curl_close($request);
	
	
	$postfieldsnames = array("__EVENTTARGET", "__EVENTARGUMENT", "__VIEWSTATE", "__EVENTVALIDATION");
	$postfieldsvals = array("ctl61", "", 0, 0);
	$postfields = makePostFields($postfieldsnames, $postfieldsvals, $response);
	
	$url = "https://www.efxatm.com/addins/Reports/rptIndex.aspx";
	$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/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.15) Gecko/20110303 Firefox/3.6.15");
	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/addins/Reports/rptOptions.aspx?BuildSelect=yes";
	$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/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.15) Gecko/20110303 Firefox/3.6.15");
	curl_setopt($request, CURLOPT_HEADER, 1);
	curl_setopt($request, CURLOPT_COOKIE, $cookies);
	$response = curl_exec($request);
	curl_close($request);
	
	
	$tranHistResponse = $response;
	
	$postfieldsnames = array("__VIEWSTATE", "__EVENTVALIDATION", "UserID", "Month", "Year", "ctl00", "rblOptions", "bGetReport");
	
	$postfieldsvals = array(0, 0, $userID, $lookupMonth, $lookupYear, "2", "0", "Get Report");
	
	$postfields = makePostFields($postfieldsnames, $postfieldsvals, $response);
	
	$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/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.15) Gecko/20110303 Firefox/3.6.15");
	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);
	
	
	
	
	
	$transResponse = $response;
	
	$transResponse = strfrmto($transResponse, '<table class="DatalistTable"', '</table>');
	$transResponse = substr($transResponse, strpos($transResponse, '<tr') + strlen('<tr'));
	
	$transRowsArray = explode('<tr', $transResponse);
	array_shift($transRowsArray);
	array_pop($transRowsArray);
	//array_shift($transRowsArray);
	//array_pop($transRowsArray);
	array_pop($transRowsArray);
	
	$outputString = "";
	foreach($transRowsArray as $key => $val){
		
		$val = strfrmto($val, '>', '</tr');
		$transRowsArray[$key] = explode("<td", $val);
		array_shift($transRowsArray[$key]);
		foreach($transRowsArray[$key]  as $colKey => $colVal){
			
			$transRowsArray[$key][$colKey] = trim(strfrmto($colVal, '>', '<'));
			
		}
		
		unset($transRowsArray[$key][3-1]);
		unset($transRowsArray[$key][4-1]);
		unset($transRowsArray[$key][5-1]);
		unset($transRowsArray[$key][6-1]);
		unset($transRowsArray[$key][8-1]);
		unset($transRowsArray[$key][11-1]);
		unset($transRowsArray[$key][12-1]);
		unset($transRowsArray[$key][13-1]);
		
		foreach($transRowsArray[$key] as $colVal)
			$outputString .= $colVal."\t";
		
		$outputString = substr($outputString, 0, -1);
		
		$outputString .= "\r\n";
		
	}
	
	$outputString = substr($outputString, 0, -2);
	/*
	?><textarea rows="200" cols="100"><?php echo "\r\n"; print_r($transRowsArray); echo "\r\n\r\n$url\r\n\r\n$response\r\n\r\n\r\n\r\n$response1";?></textarea>
	*/
	
	echo (empty($transRowsArray) || empty($transRowsArray[0])) ? "Server Error1" : $outputString;//."\r\n\r\n\r\n\r\n<textarea rows=\"200\" cols=\"100\">$postfields\r\n\r\n\r\n$transResponse\r\n\r\n\r\n$tranHistResponse\r\n\</textarea>";
}
else{
	echo "Wrong Password";
}

?>