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

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 = "userid";
	$pw = "password";
	$termID = "DAT02230";
}

$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=$pw&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;
}


if(strpos($response, "unexpected error occurred in the application.")){
	echo "Server Error";
	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);

if(strpos($response, "unexpected error occurred in the application.")){
	echo "Server Error";
	exit;
}

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

if(strpos($response, "unexpected error occurred in the application.")){
	echo "Server Error";
	exit;
}

$response1 = $response;

$url = "https://www.datastreamatm.com/addins".strfrmto($response, "Today's Transactions\" href=\"..", '"');
$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);

if(strpos($response, "unexpected error occurred in the application.")){
	echo "Server Error";
	exit;
}

$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])) ? "Empty" : $outputString;

?>