<?php
setlocale(LC_TIME, "de_DE");
global $location; #keep track of location/redirects
global $cookiearr; #store cookies here
global $ch;
global $Betrag;
$inputUsername = 'XXXXXXXXXX'; # place here the Direct banking number
$inputPassword = 'XXXXXX'; # place here your pass
$outputCSV = '/tmp/Umsatzliste.csv';
require_once 'func/hypo_functions.php';
$step = 0;
logF("fetch start");
define ('RND_LOW', 2);
define ('RND_HIGH', 4);
#define ('RND_LOW', 5);
#define ('RND_HIGH', 10);
$headers[] = 'Connection: Keep-Alive';
$headers[] = 'Host: my.hypovereinsbank.de';
$headers[] = 'Content-type: application/x-www-form-urlencoded';
$headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
$headers[] = 'Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3';
$headers[] = 'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7';
$headers[] = 'Keep-Alive: 300';
###################################################################
logF($step."th sleeping for ". $secs = rand(RND_LOW, RND_HIGH)); sleep($secs);
$Url='https://my.hypovereinsbank.de/login?view=/privatkunden/login.jsp';
$cookieFilenameLogin="/tmp/hypo_login.cookie";
$cookieFilenameAuth="/tmp/hypo_auth.cookie";
# first HTTP session : retrieve tr_sid, setcookie etc
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$Url);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFilenameLogin);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFilenameLogin);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt($ch, CURLOPT_HEADER,true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
$step++;
logF($step."th $Url");
$Html = curl_exec ($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
curl_close ($ch);
unset($ch);
logF($step."th ". strlen($Html));
fWriteTo("$step.html", $Html);
$linesHtml = split ("
", $Html);
foreach ($linesHtml as $lineHtml) {
if (strpos($lineHtml, '<a href="https://my.hypovereinsbank.de/login?view=/privatkunden/login.jsp&tr_sid=')!== false) {
$tr_sid = substr($lineHtml, strpos($lineHtml, 'tr_sid=')+7, strlen('200806270805574786894478605040495919'));
}
if (strpos($lineHtml, 'id="javax.faces.ViewState" value="')!== false) {
$lineHtml = substr($lineHtml, strpos($lineHtml, 'id="javax.faces.ViewState" value="')+34);
$javax = substr($lineHtml, 0, strpos($lineHtml, '"'));
# $javax = substr($lineHtml, strpos($lineHtml, 'id="javax.faces.ViewState" value="')+34, 120);
}
}
$tr_sid=str_replace('"', '', $tr_sid);
if (!$tr_sid) {
die (logF('no tr_sid'));
}
if (!$javax) {
die (logF('no javax'));
}
logF("retrieved tr_sid(". strlen($tr_sid) .")=$tr_sid");
logF("retrieved javax(". strlen($javax) .")=$javax");
###################################################################
$Url .= '&tr_sid='. $tr_sid;
$postFields = array (
# 'directBankingLoginForm:viewInitialized' => 'true',
'username' => $inputUsername,
'px2' => $inputPassword,
'secP' => 'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF',
'directBankingLoginForm:loginPanel:loginCommand' => 'Anmelden',
'directBankingLoginForm:_idcl' => '',
'directBankingLoginForm:_link_hidden_' => '',
'directBankingLoginForm_SUBMIT' => '1',
'javax.faces.ViewState' => ($javax),
);
$postUrl = http_build_query_wrong($postFields);
# second HTTP session : effective login
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$Url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFilenameAuth);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFilenameLogin);
#curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt($ch, CURLOPT_HEADER,true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
#curl_setopt($ch, CURLOPT_VERBOSE, 2);
curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'read_header');
$step++;
logF($step."th $Url");
$Html = curl_exec ($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
curl_close ($ch);
unset($ch);
logF($step."th ". strlen($Html));
fWriteTo("$step.html", $Html);
logF($step."th sleeping for ". $secs = rand(RND_LOW, RND_HIGH)); sleep($secs);
###################################################################
###################################################################
logF($step."th sleeping for ". $secs = rand(RND_LOW, RND_HIGH)); sleep($secs);
$Url='https://my.hypovereinsbank.de/login?view=/privatkunden/login.jsp';
$cookieFilenameLogin="/tmp/hypo_login.cookie";
$cookieFilenameAuth="/tmp/hypo_auth.cookie";
# first HTTP session : retrieve tr_sid, setcookie etc
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$Url);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFilenameLogin);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFilenameLogin);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt($ch, CURLOPT_HEADER,true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
$step++;
logF($step."th $Url");
$Html = curl_exec ($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
curl_close ($ch);
unset($ch);
logF($step."th ". strlen($Html));
fWriteTo("$step.html", $Html);
$linesHtml = split ("
", $Html);
foreach ($linesHtml as $lineHtml) {
if (strpos($lineHtml, '<a href="https://my.hypovereinsbank.de/login?view=/privatkunden/login.jsp&tr_sid=')!== false) {
$tr_sid = substr($lineHtml, strpos($lineHtml, 'tr_sid=')+7, strlen('200806270805574786894478605040495919'));
}
if (strpos($lineHtml, 'id="javax.faces.ViewState" value="')!== false) {
$lineHtml = substr($lineHtml, strpos($lineHtml, 'id="javax.faces.ViewState" value="')+34);
$javax = substr($lineHtml, 0, strpos($lineHtml, '"'));
# $javax = substr($lineHtml, strpos($lineHtml, 'id="javax.faces.ViewState" value="')+34, 120);
}
}
$tr_sid=str_replace('"', '', $tr_sid);
if (!$tr_sid) {
die (logF('no tr_sid'));
}
if (!$javax) {
die (logF('no javax'));
}
logF("retrieved tr_sid(". strlen($tr_sid) .")=$tr_sid");
logF("retrieved javax(". strlen($javax) .")=$javax");
###################################################################
$Url .= '&tr_sid='. $tr_sid;
$postFields = array (
# 'directBankingLoginForm:viewInitialized' => 'true',
'username' => $inputUsername,
'px2' => $inputPassword,
'secP' => 'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF',
'directBankingLoginForm:loginPanel:loginCommand' => 'Anmelden',
'directBankingLoginForm:_idcl' => '',
'directBankingLoginForm:_link_hidden_' => '',
'directBankingLoginForm_SUBMIT' => '1',
'javax.faces.ViewState' => ($javax),
);
$postUrl = http_build_query_urlencode($postFields);
# second HTTP session : effective login
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$Url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFilenameAuth);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFilenameLogin);
#curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt($ch, CURLOPT_HEADER,true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
#curl_setopt($ch, CURLOPT_VERBOSE, 2);
curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'read_header');
$step++;
logF($step."th $Url");
$Html = curl_exec ($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
curl_close ($ch);
unset($ch);
logF($step."th ". strlen($Html));
fWriteTo("$step.html", $Html);
logF($step."th sleeping for ". $secs = rand(RND_LOW, RND_HIGH)); sleep($secs);
$lines = split ("
", $Html);
foreach ($lines as $line) {
if (strpos($line, 'mystartpage_finanzstatus')!== false) {
$pieces = explode(''', $line);
$Var_idcl = $pieces[3];
}
}
if (!$Var_idcl) {
print "ERROR: Cannot fetch idcl";
# exit;
}
logF($step ."th idcl: $Var_idcl");
$cookieStr = '';
print_r($cookiearr);
foreach ($cookiearr as $cookieName => $cookieVal) {
$cookieStr.=$cookieName.'='.$cookieVal.'; ';
}
$cookieStr = substr($cookieStr, 0, -2);
###################################################################
###################################################################
# go to account
$Url='https://my.hypovereinsbank.de/portal?view=/banking/accountManagement.jsp';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$Url);
curl_setopt($ch, CURLOPT_COOKIE, $cookieStr);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt($ch, CURLOPT_HEADER,true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
#curl_setopt($ch, CURLOPT_VERBOSE, 2);
$step++;
logF($step."th $Url");
$Html = curl_exec ($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
curl_close ($ch);
unset($ch);
logF($step."th ". strlen($Html));
fWriteTo("$step.html", $Html);
###################################################################
$lines = split ("
", $Html);
$liveStart = false;
foreach ($lines as $line) {
if (strpos($line, 'Aktueller Kontosaldo')!== false) {
$liveStart = true;
}
if (strpos($line, '</fieldset>')!== false) {
$liveStart = false;
}
if ($liveStart && trim($line) == trim(strip_tags($line)) && strpos($line, 'EUR')!== false) {
$Betrag[0] = trim(str_replace('EUR', '', $line));
}
if ($liveStart && strpos($line, 'negbal')!==false && strpos($line, 'EUR')!== false) {
$Betrag[0] = trim(str_replace('EUR', '', strip_tags($line)));
}
if (strpos($line, 'Kontostand am')!== false) {
$linePart = strip_Tags(substr($line, strpos($line, 'Kontostand am')));
$lineParts = explode(date('Y'), $linePart);
$lineParts2 = explode(' ', trim(str_replace('EUR', '', $lineParts[1])));
if ($lineParts2[1]) {
$Betrag[1] = $lineParts2[1];
} else {
$Betrag[1] = trim(str_replace('EUR', '', $lineParts[1]));
}
}
}
logF("Kontostand live ... " . $Betrag[0] ." EUR");
logF("Kontostand old ... " . $Betrag[1] ." EUR");
if (!$Betrag) {
die (print 'no Betrag in step: '. $step);
}
$linesHtml = split ("
", $Html);
foreach ($linesHtml as $lineHtml) {
if (strpos($lineHtml, '<a href="https://my.hypovereinsbank.de/login?view=/privatkunden/login.jsp&tr_sid=')!== false) {
$tr_sid = substr($lineHtml, strpos($lineHtml, 'tr_sid=')+7, strlen('200806270805574786894478605040495919'));
}
if (strpos($lineHtml, 'id="javax.faces.ViewState" value="')!== false) {
$lineHtml = substr($lineHtml, strpos($lineHtml, 'id="javax.faces.ViewState" value="')+34);
$javax = substr($lineHtml, 0, strpos($lineHtml, '"'));
# $javax = substr($lineHtml, strpos($lineHtml, 'id="javax.faces.ViewState" value="')+34, 120);
}
}
$tr_sid=str_replace('"', '', $tr_sid);
if (!$tr_sid) {
die (logF('no tr_sid'));
}
if (!$javax) {
die (logF('no javax'));
}
logF("retrieved tr_sid(". strlen($tr_sid) .")=$tr_sid");
logF("retrieved javax(". strlen($javax) .")=$javax");
#var_dump($Betrag);
###################################################################
$Url = 'https://my.hypovereinsbank.de/portal?view=/banking/accountManagement.jsp';
$postFields = array (
'accountManagement:dayFrom' => '1',
'accountManagement:monthFrom' => strftime("%B %Y", time() - 31*24*3600),
'accountManagement:dayTo' => date('d'),
'accountManagement:monthTo' => strftime("%B %Y"),
'accountManagement:numberOfTurnovers' => '9999',
'accountManagement:refresh' => 'Anzeigen',
# 'accountManagement:buttonNavigation:j_id_id142' => 'Download Kontoums�¤tze ',
'accountManagement:_link_hidden_' => '',
'accountManagement:_idcl' => '',
'accountManagement_SUBMIT' => '1',
'javax.faces.ViewState' => ($javax),
);
$postUrl = http_build_query_urlencode($postFields);
# post-login steps
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$Url);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_COOKIE, $cookieStr);
#curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt($ch, CURLOPT_HEADER,true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_REFERER, 'https://my.hypovereinsbank.de/portal?view=/banking/startpage.jsp');
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
#curl_setopt($ch, CURLOPT_VERBOSE, 2);
$step++;
logF($step."th $Url");
$Html = curl_exec ($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
curl_close ($ch);
unset($ch);
logF($step."th ". strlen($Html));
fWriteTo("$step.html", $Html);
$linesHtml = split ("
", $Html);
foreach ($linesHtml as $lineHtml) {
if (strpos($lineHtml, '<a href="https://my.hypovereinsbank.de/login?view=/privatkunden/login.jsp&tr_sid=')!== false) {
$tr_sid = substr($lineHtml, strpos($lineHtml, 'tr_sid=')+7, strlen('200806270805574786894478605040495919'));
}
if (strpos($lineHtml, 'id="javax.faces.ViewState" value="')!== false) {
$lineHtml = substr($lineHtml, strpos($lineHtml, 'id="javax.faces.ViewState" value="')+34);
$javax = substr($lineHtml, 0, strpos($lineHtml, '"'));
# $javax = substr($lineHtml, strpos($lineHtml, 'id="javax.faces.ViewState" value="')+34, 120);
}
}
$tr_sid=str_replace('"', '', $tr_sid);
if (!$tr_sid) {
die (logF('no tr_sid'));
}
if (!$javax) {
die (logF('no javax'));
}
logF("retrieved tr_sid(". strlen($tr_sid) .")=$tr_sid");
logF("retrieved javax(". strlen($javax) .")=$javax");
logF($step."th sleeping for ". $secs = rand(RND_LOW, RND_HIGH)); sleep($secs);
###################################################################
###################################################################
$Url = 'https://my.hypovereinsbank.de/portal?view=/banking/accountManagement.jsp';
$postFields = array (
'accountManagement:dayFrom' => max(date('d')-7, 1),
'accountManagement:monthFrom' => strftime("%B %Y", time() - 31*24*3600),
## 'accountManagement:monthFrom' => strftime("%B %Y"),
'accountManagement:dayTo' => date('d'),
'accountManagement:monthTo' => strftime("%B %Y"),
'accountManagement:numberOfTurnovers' => '9999',
## 'accountManagement:numberOfTurnovers' => '20',
# 'accountManagement:refresh' => 'Anzeigen',
'accountManagement:buttonNavigation:j_id_id147' => 'Download Kontoums'. substr(strftime("%B", strtotime('2001-03-01 00:00:00')), 1, 1) .'tze ',
'accountManagement:_link_hidden_' => '',
'accountManagement:_idcl' => '',
'accountManagement_SUBMIT' => '1',
'javax.faces.ViewState' => ($javax),
);
print_R($postFields);
$postUrl = http_build_query_urlencode($postFields);
print_R($postUrl);
# post-login steps
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$Url);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_COOKIE, $cookieStr);
#curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
#curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729)");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
#curl_setopt($ch, CURLOPT_HEADER,true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
#curl_setopt($ch, CURLOPT_REFERER, 'https://my.hypovereinsbank.de/portal?view=/banking/startpage.jsp');
curl_setopt($ch, CURLOPT_REFERER, $Url);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
#curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 2);
$step++;
logF($step."th $Url");
$Html = curl_exec ($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
curl_close ($ch);
unset($ch);
logF($step."th ". strlen($Html));
fWriteTo("$step.html", $Html);
logF($step."th sleeping for ". $secs = rand(RND_LOW, RND_HIGH)); sleep($secs);
###################################################################
unlink($outputCSV);
copy("$step.html", $outputCSV);
###################################################################
###################################################################
# party over, logout
$Url='https://my.hypovereinsbank.de/login?view=/privatkunden/logout.jsp';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$Url);
curl_setopt($ch, CURLOPT_COOKIE, $cookieStr);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt($ch, CURLOPT_HEADER,true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
#curl_setopt($ch, CURLOPT_VERBOSE, 2);
$step++;
logF($step."th $Url");
$Html = curl_exec ($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
curl_close ($ch);
unset($ch);
logF($step."th ". strlen($Html));
fWriteTo("$step.html", $Html);
###################################################################
###################################################################
function http_build_query_urlencode($postFields)
{
$postUrl = "";
foreach ($postFields as $fName => $fValue) {
$postUrl.=urlencode($fName).'='.urlencode($fValue)."&";
# $postUrl.=($fName).'='.rawurlencode($fValue)."&";
}
return substr($postUrl, 0, -1);
}
function http_build_query_wrong($postFields)
{
$postUrl = "";
foreach ($postFields as $fName => $fValue) {
$postUrl.=urlencode($fName).'='.utf8_encode($fValue)."&";
# $postUrl.=($fName).'='.rawurlencode($fValue)."&";
}
return substr($postUrl, 0, -1)."
";
}
function read_header($ch, $string)
{
global $location; #keep track of location/redirects
global $cookiearr; #store cookies here
global $ch;
# ^overrides the function param $ch
# this is okay because we need to
# update the global $ch with
# new cookies
$length = strlen($string);
if(!strncmp($string, "Location:", 9))
{ #keep track of last redirect
$location = trim(substr($string, 9, -1));
}
if(!strncmp($string, "Set-Cookie:", 11))
{ #get the cookie
$cookiestr = trim(substr($string, 11, -1));
$cookie = explode(';', $cookiestr);
$cookie = explode('=', $cookie[0]);
$cookiename = trim(array_shift($cookie));
$cookiearr[$cookiename] = trim(implode('=', $cookie));
}
$cookie = "";
if(trim($string) == "")
{ #execute only at end of header
foreach ($cookiearr as $key=>$value)
{
$cookie .= "$key=$value; ";
}
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
}
return $length;
}
?>
source