<?php
require("functions.php");
/*

$url = "https://onlinebanking.tdbank.com/";
$request = curl_init($url);
curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($request, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($request, CURLOPT_HEADER, 1);
//curl_setopt($request, CURLOPT_COOKIE, "PHPSESSID=d07738ade570aa9128396d9167e8a5af");
$response = curl_exec($request);
curl_close($request);

?><?php

$response = substr($response, strpos($response, "<frame SRC=\"")+strlen("<frame SRC=\""), strpos($response, "\"", strpos($response, "<frame SRC=\"")+strlen("<frame SRC=\"")) - (strpos($response, "<frame SRC=\"")+strlen("<frame SRC=\"")));

?><?php

$url = $response;
$request = curl_init($url);
curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($request, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($request, CURLOPT_HEADER, 1);
$response = curl_exec($request);

?><?php

$posturl = substr($response, strpos($response, '<form name="login" action="')+strlen('<form name="login" action="'), strpos($response, "\"", strpos($response, '<form name="login" action="')+strlen('<form name="login" action="')) - (strpos($response, '<form name="login" action="')+strlen('<form name="login" 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: ')));

$response = substr($response, strpos($response, 'et-Cookie'));

$cookie[1] = 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: ')));


?>
<?php

$url = "https://onlinebanking.tdbank.com/".$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_HEADER, 1);
curl_setopt($request, CURLOPT_POST, 1);
//curl_setopt($request, CURLOPT_COOKIE, $cookie[0]);
curl_setopt($request, CURLOPT_COOKIE, $cookies);
curl_setopt($request, CURLOPT_POSTFIELDS, "user=088827162&pin=2684");
$response = curl_exec($request);
curl_close($request);


if(strpos($response, "Object moved")){

	$redir = substr($response, strpos($response, 'Location: ')+strlen('Location: '), strpos($response, "\r\n", strpos($response, 'Location: ')+strlen('Location: ')) - (strpos($response, 'Location: ')+strlen('Location: ')));
	
	$cookie[2] = 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: ')));
	
	$response = substr($response, strpos($response, 'et-Cookie'));
	
	$cookie[3] = 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: ')));
	
	
	
	
	
	
	$url = "https://onlinebanking.tdbank.com/".$redir;
	$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_HEADER, 1);
	curl_setopt($request, CURLOPT_COOKIE, "$cookie[1]; $cookie[2]; $cookie[3]");
	$response = curl_exec($request);
	curl_close($request);

?><?php

	if(strpos($redir, "challenge")){
		$posturl = substr($redir, 0, strpos($redir, "/"))."/".strfrmto($response, 'action = "', '"');
		
		print_r($posturl);
		
		
		$url = "https://onlinebanking.tdbank.com/".$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_POST, 1);
		curl_setopt($request, CURLOPT_POSTFIELDS, "isPostBack=True&QuestionAnswer=".urlencode("Brooklyn")."&QuestionID=10");
		curl_setopt($request, CURLOPT_HEADER, 1);
		curl_setopt($request, CURLOPT_COOKIE, "$cookie[1]; $cookie[2]; $cookie[3]");
		$response = curl_exec($request);
		curl_close($request);
	}

}*/

/* if(strpos($response, "Object moved")){

	$redir = substr($response, strpos($response, 'Location: ')+strlen('Location: '), strpos($response, "\r\n", strpos($response, 'Location: ')+strlen('Location: ')) - (strpos($response, 'Location: ')+strlen('Location: ')));
	
	
	$url = "https://online.tdbank.com/".$redir;
	$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_HEADER, 1);
	curl_setopt($request, CURLOPT_COOKIE, "$cookie[1]; $cookie[2]; $cookie[3]");
	$response = curl_exec($request);
	curl_close($request);
} */
/*


$head = substr($response, 0, strpos($response, "<"));
$spit = substr($response, strpos($response, "<"));

echo $spit;
*/
?><?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);


?><?php



$postfieldsarray = array("__VIEWSTATE" => strfrmto($response, '__VIEWSTATE" value="', '"'), "__EVENTVALIDATION" => strfrmto($response, '__EVENTVALIDATION" value="', '"'), "txtLogin" => "OmarButt3", "txtPassword" => "atm0987", "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);


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

$termID = "DAT02230";

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




$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.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_POST, 1);
curl_setopt($request, CURLOPT_COOKIE, $cookies);
curl_setopt($request, CURLOPT_POSTFIELDS, $postfields);
$response = curl_exec($request);
curl_close($request);



?><textarea rows="20" cols="100"><?php echo $response."\r\n\r\n\r\n$postfields\r\n\r\n\r\n$cookies;"/*."\r\n\r\n\r\n$cookie[1]; $cookie[2]; $cookie[3]; \r\n\r\n\r\n$cookies"*/;?></textarea>

<?php /*
<textarea rows="200" cols="100"><?php echo "\r\n\r\n\r\n$terminalresponse\r\n\r\n\r\n";//."\r\n\r\n\r\n$cookie[1]; $cookie[2]; $cookie[3]; \r\n\r\n\r\n$cookies"
print_r($terminalRowsArray);?></textarea>
*/?>