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

if(array_key_exists('userid', $_POST) && array_key_exists('pw', $_POST) && array_key_exists('termid', $_POST) && array_key_exists('frmdate', $_POST) && array_key_exists('todate', $_POST)){
	$userID = $_POST['userid'];
	$pw = $_POST['pw'];
	$termID = $_POST['termid'];
	$fromDate = $_POST['frmdate'];
	$toDate = $_POST['todate'];
}
else{
	$userID = "userid";
	$pw = "atm0987";
	$termID = "DAT00653";
	$fromDate = "2011.02.01";
	$toDate = "2011.03.08";
}

/*
	$userID = "OmarButt3";
	$pw = "atm0987";
	$termID = "DAT00653";
	$fromDate = "2011.02.01";
	$toDate = "2011.03.08";
*/	
	

$url = "https://www.datastreamatm.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.datastreamatm.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.datastreamatm.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);

$response1 = $response;

$transHistUrl = "https://www.datastreamatm.com/addins".strfrmto($response, 'Transaction History" href="..', '"');

$url = $transHistUrl;
$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);

$tranHistResponse = $response;

$postfieldsnames = array("__EVENTTARGET", "__EVENTARGUMENT", "__VIEWSTATE", "__EVENTVALIDATION", "TerminalID_txt", "ckSingle", 'StartDate$dtp', 'EndDate$dtp', "BatchNumber", "SortOrder", "UserID", "ctl00", "rblOptions", "bGetReport");

$postfieldsvals = array(0, 0, 0, 0, $termID, "on", $fromDate, $toDate, "", "Ascending By Time", $userID, "2", "0", "Get Report");

$postfields = makePostFields($postfieldsnames, $postfieldsvals, $response);


$url = $transHistUrl;
$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);





$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);

$outputString = "";
foreach($transRowsArray as $key => $val){
	
	$val = strfrmto($val, '>', '</tr');
	$transRowsArray[$key] = explode("<td", $val);
	
	foreach($transRowsArray[$key]  as $colKey => $colVal){
		
		$transRowsArray[$key][$colKey] = trim(strfrmto($colVal, '>', '<'));
		
	}
	unset($transRowsArray[$key][4]);
	unset($transRowsArray[$key][5]);
	unset($transRowsArray[$key][9]);
	unset($transRowsArray[$key][11]);
	unset($transRowsArray[$key][12]);
	unset($transRowsArray[$key][13]);
	unset($transRowsArray[$key][14]);
	unset($transRowsArray[$key][15]);
	unset($transRowsArray[$key][16]);
	array_shift($transRowsArray[$key]);
	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 Error" : $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>";

?>