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

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

// require_once("checkuser.php");

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



/////////////////////////
/*
?><textfield>hello</textfield><?php
*/
$postfieldsarray = array("__VIEWSTATE" => strfrmto($response, '__VIEWSTATE" value="', '"'), "__EVENTVALIDATION" => strfrmto($response, '__EVENTVALIDATION" value="', '"'), "txtLogin" => $userID, "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;
}

$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/Views.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/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);


$ctlresponse = substr($response, 0, strpos($response, "[Global] Heidi", strpos($response, "[Global] Heidi") + 1));
	
$ctlresponse = substr($ctlresponse, strrpos($ctlresponse, "value=\"") + strlen("value=\""));
$ctlresponse = substr($ctlresponse, 0, strpos($ctlresponse, "\""));
$ctlnumber = $ctlresponse;

$postfieldsnames = array("__EVENTTARGET", "__EVENTARGUMENT", "__VIEWSTATE", "__EVENTVALIDATION", "__LASTFOCUS", "txtTermID", "drpLstView");
$postfieldsvals = array("drpLstView", "", 0, 0, "", "", $ctlnumber);
$postfields = makePostFields($postfieldsnames, $postfieldsvals, $response);

//echo "$ctlnumber\r\n\r\n$postfields";exit;

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







$terminalresponse = $response;

$terminalresponse = strfrmto($terminalresponse, '<table class="DataListTable', '</table>');
$terminalresponse = substr($terminalresponse, strpos($terminalresponse, '<tr>') + strlen('<tr>'));

$terminalRowsArray = explode('<tr', $terminalresponse);

$outputString = "";

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

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

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

echo $outputString;

?>