Wednesday, April 21, 2010

Import Email contacts

Import Yahoo Contacts:
<?php
#Copyright 2006 Svetlozar Petrov
#All Rights Reserved
#svetlozar@svetlozar.net
#http://svetlozar.net

#Script to import the names and emails from yahoo contact list

#Globals Section, $location and $cookiearr should be used in any script that uses
# get_contacts function
$location = "";
$cookiearr = array();

#$addopt specifies whether you want to assume that yahoo id's without email addresses
# have @yahoo email. Most do. Leave true if you want to have those emails returned.
# Change to false if you don't want @yahoo.com to be added to yahoo id's
# In that case you'll only get the id's instead of email address for those.
$addopt = true;


#function get_contacts, accepts as arguments $login (the username) and $password
#returns array of: array of the names and array of the emails if login successful
#otherwise returns 1 if login is invalid and 2 if username or password was not specified
function get_contacts($login, $password)
{
#the globals will be updated/used in the read_header function
#read the notes above about $addopt
global $location;
global $cookiearr;
global $ch;
global $addopt;

#check if username and password was given:
if ((isset($login) && trim($login)=="") || (isset($password) && trim($password)==""))
{
#return error code if they weren't
return 2;
}

#initialize the curl session
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://address.yahoo.com");
curl_setopt($ch, CURLOPT_REFERER, "");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

$html = curl_exec($ch);
$matches = array();

#parse the hidden fields from the login form:
preg_match_all('/<input type\="hidden" name\="([^"]+)" value\="([^"]*)">/', $html, $matches);
$values = $matches[2];
$params = "";

$i=0;
foreach ($matches[1] as $name)
{
$params .= "$name=$values[$i]&";
++$i;
}

$login = urlencode($login);
$password = urlencode($password);
#attempt login:
curl_setopt($ch, CURLOPT_URL,"http://login.yahoo.com/config/login?");
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params . "login=$login&passwd=$password");
curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'read_header');


$html = curl_exec($ch);

#$loginarr = explode("?", $location);

#test if login was successful:
if (!is_array($cookiearr) || !isset($cookiearr['F']))
{
return 1;
}

preg_match('/\.rand=([^"]*)"/si', $html, $matches);
$value = $matches[1];

#go to the address book page
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_URL,"http://address.mail.yahoo.com/?1&VPC=import_export&A=B&.rand=$value");
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);

$html = curl_exec($ch);

#parse the yahoo.csv form and submit it:
$matches = array();
preg_match('/name="\.crumb".*?id="crumb2".*?value="([^"]*)"/si', $html, $matches);
$crumb = $matches[1];

preg_match('/\.rand=([^"]*)"/si', $html, $matches);
$rand = $matches[1];


$action = "http://address.yahoo.com/?1&VPC=import_export&A=B&.rand=$rand";
curl_setopt($ch, CURLOPT_URL, $action);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, ".crumb=$crumb&VPC=import_export&A=B&submit%5Baction_export_yahoo%5D=Export+Now");
$html = curl_exec($ch);

curl_close ($ch);

#parse the csv file:
$table = explode("\n\"", $html);

array_shift($table);
$maxi = count($table);

$names = array();
$emails = array();
$phones = array(); //cosmin, phones

#parse emails and names:
for($i=0; $i<$maxi; ++$i)
{
$table[$i]=explode(',"',$table[$i]);
$table[$i]=array_map("trimvals", $table[$i]);
#echo '<pre>'; print_r($table[$i]); echo '</pre>'; // cosmin, debug

$names[$i]=trim("{$table[$i][0]} {$table[$i][1]} {$table[$i][2]}");

if($names[$i]=="")
{
$names[$i]=trim($table[$i][3]); //if no name set to nickname
}
if($names[$i]=="")
{
$names[$i]=trim($table[$i][7]); //if no name set to yahooid
}

$emails[$i]=trim($table[$i][4]);
if($emails[$i]=="" && trim($table[$i][7])!="")
{

if ($addopt && !eregi("@", $table[$i][7]) && (trim($table[$i][7])!=""))
$emails[$i]=trim($table[$i][7]) . "@yahoo.com"; //if no email set to yahooid + @yahoo.com
else
$emails[$i]=trim($table[$i][7]);
}

// cosmin, phones
//$phones[$i]=trim($table[$i][4]);
if($phones[$i]=="" && trim($table[$i][12])!="")
{
$phones[$i]=trim($table[$i][12]);
}
}

#return the result:
return array($names, $emails, $phones);
}

#function to trim the whitespace around names and email addresses
#used by get_contacts when parsing the csv file
function trimvals($val)
{
return trim ($val, '" ');
}

#read_header is essential as it processes all cookies and keeps track of the current location url
#leave unchanged, include it with get_contacts
function read_header($ch, $string)
{
global $location;
global $cookiearr;
global $ch;

$length = strlen($string);
if(!strncmp($string, "Location:", 9))
{
$location = trim(substr($string, 9, -1));
}
if(!strncmp($string, "Set-Cookie:", 11))
{
$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) == "")
{
foreach ($cookiearr as $key=>$value)
{
$cookie .= "$key=$value; ";
}
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
}

return $length;
}
?>

Gmail Contacts:
<?php
#Copyright 2006 Svetlozar Petrov
#All Rights Reserved
#svetlozar@svetlozar.net
#http://svetlozar.net

#Script to import the names and emails from gmail contact list

#Globals Section, $location and $cookiearr should be used in any script that uses
# get_contacts function
$location = "";
$cookiearr = array();

#function get_contacts, accepts as arguments $login (the username) and $password
#returns array of: array of the names and array of the emails if login successful
#otherwise returns 1 if login is invalid and 2 if username or password was not specified
function get_contacts($login, $password)
{
#the globals will be updated/used in the read_header function
global $location;
global $cookiearr;
global $ch;

#check if username and password was given:
if ((isset($login) && trim($login)=="") || (isset($password) && trim($password)==""))
{
#return error code if they weren't
return 2;
}

#initialize the curl session
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://mail.google.com/mail/");
curl_setopt($ch, CURLOPT_REFERER, "");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'read_header');

#get the html from gmail.com
$html = curl_exec($ch);

$matches = array();
$actionarr = array();

$action = "https://www.google.com/accounts/ServiceLoginAuth?service=mail";

#parse the login form:
#parse all the hidden elements of the form
preg_match_all('/<input type\="hidden" name\="([^"]+)".*?value\="([^"]*)"[^>]*>/si', $html, $matches);
$values = $matches[2];
$params = "";

$i=0;
foreach ($matches[1] as $name)
{
$params .= "$name=" . urlencode($values[$i]) . "&";
++$i;
}

$login = urlencode($login);
$password = urlencode($password);

#submit the login form:
curl_setopt($ch, CURLOPT_URL,$action);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params ."Email=$login&Passwd=$password&PersistentCookie=");

$html = curl_exec($ch);

#test if login was successful:
if (!isset($cookiearr['GX']) && (!isset($cookiearr['LSID']) || $cookiearr['LSID'] == "EXPIRED"))
{
return 1;
}

#this is the new csv url:
curl_setopt($ch, CURLOPT_URL, "http://mail.google.com/mail/contacts/data/export?exportType=ALL&groupToExport=&out=OUTLOOK_CSV");
curl_setopt($ch, CURLOPT_POST, 0);// cosmin, vad ca altfel nu merge...
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);

$html = curl_exec($ch);

$csvrows = explode("\n", $html);
array_shift($csvrows);
//echo'<pre>';var_dump($csvrows);echo'</pre>';
$names = array();
$emails = array();
$phones = array();// cosmin, phones
foreach ($csvrows as $row)
{
$values = explode(",", $row);
if (eregi("@", $values[1]))
{
$names[] = ( trim($values[0])=="" ) ? $values[1] : $values [0];
$emails[] = $values[1];
$phones[] = $values[9]; // cosmin, phones
}
}


return array($names, $emails, $phones);
}

#read_header is essential as it processes all cookies and keeps track of the current location url
#leave unchanged, include it with get_contacts
function read_header($ch, $string)
{
global $location;
global $cookiearr;
global $ch;

$length = strlen($string);
if(!strncmp($string, "Location:", 9))
{
$location = trim(substr($string, 9, -1));
}
if(!strncmp($string, "Set-Cookie:", 11))
{
$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) == "")
{
foreach ($cookiearr as $key=>$value)
{
$cookie .= "$key=$value; ";
}
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
}

return $length;
}

#function to trim the whitespace around names and email addresses
#used by get_contacts when parsing the csv file
function trimvals($val)
{
return trim ($val, "\" \n");
}
?>

Hotmail Contact
<?php
#Copyright 2006 Svetlozar Petrov
#All Rights Reserved
#svetlozar@svetlozar.net
#http://svetlozar.net

#Script to import the names and emails from hotmail contact list

#Globals Section, $location and $cookiearr should be used in any script that uses
# get_contacts function
$location = "";
$cookiearr = array();
$chget = null;
$chpost = null;
$mspauth = "";
$mspprof = "";

#function get_contacts, accepts as arguments $login (the username) and $password
#returns array of: array of the names and array of the emails if login successful
#otherwise returns 1 if login is invalid and 2 if username or password was not specified
function get_contacts($login, $passwd)
{
global $location;
global $cookiearr;
global $chget;
global $chpost;
$names = array();
$emails = array();

$cookiearr['CkTst']= "G" . time() . "000";

#check if username and password was given:
if ((isset($login) && trim($login)=="") || (isset($passwd) && trim($passwd)==""))
{
#return error code if they weren't
return 2;
}

#hotmail requires to add @hotmail.com when you sign in:
if (!eregi("@", $login))
$login .= "@" . "hotmail.com";

#initialize the curl session
$chget = curl_init();
$chpost = curl_init();

#get the login form:
curl_setopt($chget, CURLOPT_URL,"http://login.live.com/login.srf?id=2");
curl_setopt($chget, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($chget, CURLOPT_REFERER, "");
curl_setopt($chget, CURLOPT_RETURNTRANSFER,1);
curl_setopt($chget, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($chget, CURLOPT_HEADERFUNCTION, 'read_header');
$html = curl_exec($chget);

$matches = array();

#parse the hidden fields (logon form):
preg_match_all('/<input type\="hidden"[^>]*name\="([^"]+)"[^>]*value\="([^"]*)">/', $html, $matches);
$values = $matches[2];
$params = "";

$i=0;
foreach ($matches[1] as $name)
{
$params .= "$name=" . urlencode($values[$i]);
++$i;
if(isset($matches[$i]))
{
$params .= "&";
}
}

$params = trim ($params, "&");

curl_setopt($chpost, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($chpost, CURLOPT_RETURNTRANSFER,1);
curl_setopt($chpost, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($chpost, CURLOPT_POST, 1);
curl_setopt($chpost, CURLOPT_HEADERFUNCTION, 'read_header');


#parse the login form:
$matches = array();
preg_match('/<form [^>]+action\="([^"]+)"[^>]*>/', $html, $matches);
$opturl = $matches[1];


#parse the hidden fields:
preg_match_all('/<input type="hidden"[^>]*name\="([^"]+)"[^>]*value\="([^"]*)"[^>]*>/', $html, $matches);
$values = $matches[2];
$params = "";


$i=0;
foreach ($matches[1] as $name)
{
$paramsin[$name]=$values[$i];
++$i;
}

#some form specific javascript stuff before submission, this takes care of that:
$sPad="IfYouAreReadingThisYouHaveTooMuchFreeTime";
$lPad=strlen($sPad)-strlen($passwd);
$PwPad=substr($sPad, 0,($lPad<0)?0:$lPad);

$paramsin['PwdPad']=urlencode($PwPad);
foreach ($paramsin as $key=>$value)
{
$params .= "$key=" . urlencode($value) . "&";
}

curl_setopt($chpost, CURLOPT_URL, $opturl);
curl_setopt($chpost, CURLOPT_RETURNTRANSFER,1);
curl_setopt($chpost, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($chpost, CURLOPT_POST, 1);
curl_setopt($chpost, CURLOPT_POSTFIELDS, $params . "login=" . urlencode($login) . "&passwd=" . urlencode($passwd) . "&LoginOptions=2");
$html = curl_exec($chpost);


#test for valid login:
if((preg_match('/replace[^"]*"([^"]*)"/', $html, $matches)==0) && (preg_match("/url=([^\"]*)\"/si", $html, $matches)==0 || eregi("password is incorrect", $html)))
{
return 1;
}



#curl_setopt($chget, CURLOPT_URL, $location);
curl_setopt($chget, CURLOPT_URL,$matches[1]);
curl_setopt($chget, CURLOPT_RETURNTRANSFER,1);
curl_setopt($chget, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($chget, CURLOPT_HEADERFUNCTION, 'read_header');
$html = curl_exec($chget);


if (eregi("hotmail.msn.com/", $location))
{
#process the non-live interface
#passed the login, you need to load this page to get some more cookies to complete the login
curl_setopt($chget, CURLOPT_URL,"http://cb1.msn.com/hm/header.armx?lid=1033&cbpage=login&lc=1033&x=3.200.4104.0");
curl_setopt($chget, CURLOPT_RETURNTRANSFER,1);
curl_setopt($chget, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($chget, CURLOPT_HEADERFUNCTION, 'read_header');
$html = curl_exec($chget);

#follow the javascript redirection url:
curl_setopt($chget, CURLOPT_POST, 0);
curl_setopt($chget, CURLOPT_URL,$matches[1]);
curl_setopt($chget, CURLOPT_RETURNTRANSFER,1);
curl_setopt($chget, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($chget, CURLOPT_HEADERFUNCTION, 'read_header');

$html = curl_exec($chget);

#get the base url and build the url for the page with contacts:
preg_match("/(http:\/\/[^\/]*\/cgi-bin\/).*?curmbox=([^\& ]*).*?a=([^\& ]*)/i", $location, $baseurl);
$url = $baseurl[1] . "AddressPicker?a=$baseurl[3]&curmbox=$baseurl[2]&Context=InsertAddress&_HMaction=Edit&qF=to";

curl_setopt($chget, CURLOPT_URL,$url);
curl_setopt($chget, CURLOPT_USERAGENT, 0);
curl_setopt($chget, CURLOPT_RETURNTRANSFER,1);
curl_setopt($chget, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($chget, CURLOPT_HEADERFUNCTION, 'read_header');

$html = curl_exec($chget);

#parse the emails and names:
preg_match_all('/<option.*?value="([^"]*)"[^>]*>(.*?)\</i', $html, $emailarr);


#get rid of duplicates:
$emailsunique = array_unique($emailarr[1]);

$i = 0;
foreach ($emailsunique as $key => $value)
{
$emails[$i] = $emailarr[1][$key];
$names[$i++] = $emailarr[2][$key];
}

#return the result:
return array($names, $emails);
}

global $mspauth;
global $mspprof;
#override cookies for live mail
#this is a quick solution for right now

if (!eregi("@msn", $login))
{
$cookiearr["MSPAuth"] = $mspauth;
$cookiearr["MSPProf"] = $mspprof;

$cookie = "";
foreach ($cookiearr as $key=>$value)
{
$cookie .= "$key=$value; ";
}
$cookie = trim ($cookie, "; ");

curl_setopt($chget, CURLOPT_COOKIE, $cookie);
curl_setopt($chpost, CURLOPT_COOKIE, $cookie);
}


if (preg_match('/location\.replace[^"]*"([^"]*)"/', $html, $matches))
{
curl_setopt($chget, CURLOPT_URL,$matches[1]);
curl_setopt($chget, CURLOPT_RETURNTRANSFER,1);
curl_setopt($chget, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($chget, CURLOPT_HEADERFUNCTION, 'read_header');
$html = curl_exec($chget);
}



#new live mail ####################################################################################
if (strpos($html, "TodayLight") > 0)
{
$url = $location; //$matches[1];
$url = explode("/", $url);
$url[sizeof($url)-1] = "options.aspx?subsection=26";
$url = implode("/", $url);
curl_setopt($chget, CURLOPT_URL,$url);
curl_setopt($chget, CURLOPT_RETURNTRANSFER,1);
curl_setopt($chget, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($chget, CURLOPT_HEADERFUNCTION, 'read_header');

$html = curl_exec($chget);

preg_match_all('/<input type="hidden"[^>]*name\="([^"]+)"[^>]*value\="([^"]*)"[^>]*>/si', $html, $matches);
$values = $matches[2];
$params = "";


$i=0;
foreach ($matches[1] as $name)
{
$paramsin[$name]=$values[$i];
++$i;
}

$paramsin['ctl01$ExportButton'] = "Export contacts";
$paramsin['mt'] = $cookiearr['mt'];

foreach ($paramsin as $key=>$value)
{
$params .= "$key=" . urlencode($value) . "&";
}

curl_setopt($chpost, CURLOPT_URL, $url);
curl_setopt($chpost, CURLOPT_RETURNTRANSFER,1);
curl_setopt($chpost, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($chpost, CURLOPT_POST, 1);
curl_setopt($chpost, CURLOPT_POSTFIELDS, $params);
$html = curl_exec($chpost);


#parse the csv file:
$table = explode("\n", $html);

$separator = ",";
if (count($table)>0)
{
$rowzero = array_shift($table);
if (count(explode(",", $rowzero)) > 10 ) #10 doesn't mean a lot here, could be compared to 1
{
$separator = ",";
}
else
{
$separator = ";";
}
}

$maxi = count($table);

$names = array();
$emails = array();
$phones = array();//cosmin, phones

#parse emails and names:
for($i=0; $i<$maxi; ++$i)
{
$table[$i]=explode($separator,$table[$i]);
#echo '<pre>'; print_r($table[$i]); echo '</pre>'; // cosmin, debug

if (count($table[$i])>46)
{
$names[$i]=trim($table[$i][1],'"') . " " . trim($table[$i][3],'"');
$emails[$i]=trim($table[$i][46],'"');
$phones[$i]=trim($table[$i][25],'"');// cosmin, phones
}
}

#return the result:
return array($names, $emails, $phones);

}
################################################# end new live mail - old live mail below

$url = $matches[1];
$url = explode("/", $url);
$url[count($url)-1] = "ApplicationMainReach.aspx?Control=EditMessage&_ec=1&FolderID=00000000-0000-0000-0000-000000000001";//$matches[1];
$url = implode("/", $url);

curl_setopt($chget, CURLOPT_URL,$url);
curl_setopt($chget, CURLOPT_RETURNTRANSFER,1);
curl_setopt($chget, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($chget, CURLOPT_HEADERFUNCTION, 'read_header');

$html = curl_exec($chget);


preg_match_all('/<input.*?type\="?hidden"?.*?name\="([^"]*)".*?value\="([^"]*)"/si', $html, $matches);
$postarr[$matches[1][0]]=($matches[2][0]);
$postarr["query"]= "Find in Mail";
$postarr["ToContact"]= "To:";
$postarr["fTo"]= "";
$postarr["fCC"]= "";
$postarr["fBcc"]= "";
$postarr["fSubject"]= "";
$postarr["fMessageBody"]= "";

curl_setopt($chpost, CURLOPT_URL, $url);
curl_setopt($chpost, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($chpost, CURLOPT_RETURNTRANSFER,1);
curl_setopt($chpost, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($chpost, CURLOPT_POST, 1);
curl_setopt($chpost, CURLOPT_POSTFIELDS, $postarr);
curl_setopt($chpost, CURLOPT_HEADERFUNCTION, 'read_header');
$html = curl_exec($chpost);

preg_match_all('/<input type="checkbox" name="contactNameEmail" value="([^;]*);([^"]+)"/si', $html, $matches);

$emails = array_map("arrurldecode", $matches[2]);
$names = array_map("arrurldecode", $matches[1]);

return array($names, $emails);
}

function arrurldecode($val)
{
return urldecode ($val);
}

#read_header is essential as it processes all cookies and keeps track of the current location url
#leave unchanged, include it with get_contacts
function read_header($ch, $string)
{
global $location;
global $cookiearr;
global $chget;
global $chpost;
global $mspauth;
global $mspprof;

$length = strlen($string);
if(!strncmp($string, "Location:", 9))
{

$url = trim(substr($string, 9, -1));
if (eregi("http:", $url))
{
$location = $url;
}
else
{
$matches = array();
preg_match("#(https?\:\/\/[^\/]*)\/#si", $location, $matches);
$location = $matches[0] . $url;
}
}
if(!strncmp($string, "Set-Cookie:", 11))
{
$cookiestr = trim(substr($string, 11, -1));
$cookie = explode(';', $cookiestr);
$cookie = explode('=', $cookie[0]);
$cookiename = trim(array_shift($cookie));

if ($cookiename == "MSPAuth")
{
$mspauth = !empty($cookiearr['MSPAuth']) ? $cookiearr['MSPAuth'] : "";
}

if ($cookiename == "MSPProf")
{
$mspprof = !empty($cookiearr['MSPProf']) ? $cookiearr['MSPProf'] : "";
}

if ($cookiename!=="MSPOK" || !$cookiearr['MSPOK'])
{
$cookiearr[$cookiename] = trim(implode('=', $cookie));
}

}
$cookie = "";
if(trim($string) == "")
{
foreach ($cookiearr as $key=>$value)
{
$cookie .= "$key=$value; ";
}
$cookie = trim ($cookie, "; ");

curl_setopt($chget, CURLOPT_COOKIE, $cookie);
curl_setopt($chpost, CURLOPT_COOKIE, $cookie);
}

return $length;
}

?>

AOL:
<?php
#Copyright 2006 Svetlozar Petrov
#All Rights Reserved
#svetlozar@svetlozar.net
#http://svetlozar.net

#Script to import the names and emails from aol contact list

#Globals Section, $location and $cookiearr should be used in any script that uses
# get_contacts function
$location = "";
$cookiearr = array();

#function get_contacts, accepts as arguments $login (the username) and $password
#returns array of: array of the names and array of the emails if login successful
#otherwise returns 1 if login is invalid and 2 if username or password was not specified
function get_contacts($login, $passwd)
{
global $location;
global $cookiearr;
global $ch;

$login = explode("@", $login);
$login = $login[0];

#check if username and password was given:
if ((isset($login) && trim($login)=="") || (isset($passwd) && trim($passwd)==""))
{
#return error code if they weren't
return 2;
}

#initialize the curl session
$ch = curl_init();

#get the login form:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_REFERER, "");
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'read_header');

curl_setopt($ch, CURLOPT_URL, "https://my.screenname.aol.com/_cqr/login/login.psp?mcState=initialized&seamless=novl&sitedomain=sns.webmail.aol.com&lang=en&locale=us&authLev=2&siteState=ver%3a2%7cac%3aWS%7cat%3aSNS%7cld%3awebmail.aol.com%7cuv%3aAOL%7clc%3aen-us");
$html = curl_exec($ch);

#parse the login form:
preg_match('/<form name="AOLLoginForm".*?action="([^"]*).*?<\/form>/si', $html, $matches);
#$opturl = "https://my.screenname.aol.com" .$matches[1];
$opturl = "https://my.screenname.aol.com/_cqr/login/login.psp";

#get the hidden fields:
$hiddens = array();
preg_match_all('/<input type="hidden" name="([^"]*)" value="([^"]*)".*?>/si', $matches[0], $hiddens);
$hiddennames = $hiddens[1];
$hiddenvalues = $hiddens[2];


$hcount = count($hiddennames);
$params = "";
for($i=0; $i<$hcount; $i++)
{
$params .= $hiddennames[$i] . "=" . urlencode($hiddenvalues[$i]) . "&";
}


$login = urlencode($login);
$passwd = urlencode($passwd);

#attempt login:
curl_setopt($ch, CURLOPT_URL, $opturl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params . "loginId=$login&password=$passwd");
$html = curl_exec($ch);



#check if login successful:
if(!preg_match("/'loginForm', 'false', '([^']*)'/si", $html, $matches))
{
#return error if it's not
return 1;
}



$opturl = $matches[1];
curl_close ($ch);
$ch = curl_init();
foreach ($cookiearr as $key=>$value)
{
$cookie .= "$key=$value; ";
}
curl_setopt($ch, CURLOPT_COOKIE, $cookie);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_REFERER, $location);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'read_header');
curl_setopt($ch, CURLOPT_URL, $opturl);
$html = curl_exec($ch);

if (preg_match('/gTargetHost = "([^"]*)".*?gSuccessPath = "([^"]*)"/si', $html, $matches) || preg_match('/gPreferredHost = "([^"]*)".*?gSuccessPath = "([^"]*)"/si', $html, $matches))
{
$opturl = $matches[1];
$opturl .= $matches[2];
$opturl = "http://" . $opturl;
}
else
{
if(preg_match("/'loginForm', 'false', '([^']*)'/si", $html, $matches))
{
$opturl = $matches[1];
curl_setopt($ch, CURLOPT_URL, $opturl);
$html = curl_exec($ch);
$opturl = $location;
}
}


$opturl = explode("/", $opturl);
$opturl[count($opturl)-1]="AB";
$opturl = implode("/", $opturl);

preg_match('/\&uid:([^\&]*)\&/si', $cookiearr['Auth'], $matches);
$usr = $matches[1];

#get the address book:
$opturl .= "/addresslist-print.aspx?command=all&undefined&sort=LastFirstNick&sortDir=Ascending&nameFormat=FirstLastNick&version=$cookiearr[Version]&user=$usr";

curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_URL, $opturl);
$html = curl_exec($ch);
curl_close ($ch);


#parse the emails and names:
preg_match_all('/<span class="fullName">(.*?)<\/span>(.*?)<hr class="contactSeparator">/si', $html, $matches);
$names = $matches[1];
$emails = array_map("parse_emails", $matches[2]);
$phones = array(); //cosmin, phones

#return the result:
return array($names, $emails, $phones);
}

#parse_emails needs to be included to be able to get the emails
function parse_emails($str)
{
$matches = array();
preg_match('/<span>Email 1:<\/span> <span>([^<]*)<\/span>/si', $str, $matches) || preg_match('/<span>Primary Email:<\/span> <span>([^<]*)<\/span>/si', $str, $matches);
return $matches[1];
}


#read_header is essential as it processes all cookies and keeps track of the current location url
#leave unchanged, include it with get_contacts
function read_header($ch, $string)
{
global $location;
global $cookiearr;
global $ch;


$length = strlen($string);
if(!strncmp($string, "Location:", 9))
{
$location = trim(substr($string, 9, -1));
}
if(!strncmp($string, "Set-Cookie:", 11))
{
$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) == "")
{
foreach ($cookiearr as $key=>$value)
{
$cookie .= "$key=$value; ";
}
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
}

return $length;
}

?>

MSN:
<?php
#Copyright 2006 Svetlozar Petrov
#All Rights Reserved
#svetlozar@svetlozar.net
#http://svetlozar.net

#Script to import the names and emails from msn contact list

#Globals Section, $location and $cookiearr should be used in any script that uses
# get_contacts function
$location = "";
$cookiearr = array();
$chget = null;
$chpost = null;
$mspauth = "";
$mspprof = "";

#function get_contacts, accepts as arguments $login (the username) and $password
#returns array of: array of the names and array of the emails if login successful
#otherwise returns 1 if login is invalid and 2 if username or password was not specified
function get_contacts($login, $passwd)
{
global $location;
global $cookiearr;
global $chget;
global $chpost;
$names = array();
$emails = array();

$cookiearr['CkTst']= "G" . time() . "000";

#check if username and password was given:
if ((isset($login) && trim($login)=="") || (isset($passwd) && trim($passwd)==""))
{
#return error code if they weren't
return 2;
}

#msn requires to full email address
if (!eregi("@", $login))
$login .= "@" . "msn.com";

#initialize the curl session
$chget = curl_init();
$chpost = curl_init();

#get the login form:
curl_setopt($chget, CURLOPT_URL,"http://login.live.com/login.srf?id=2");
curl_setopt($chget, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($chget, CURLOPT_REFERER, "");
curl_setopt($chget, CURLOPT_RETURNTRANSFER,1);
curl_setopt($chget, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($chget, CURLOPT_HEADERFUNCTION, 'read_header');
$html = curl_exec($chget);

$matches = array();

#parse the hidden fields (logon form):
preg_match_all('/<input type\="hidden"[^>]*name\="([^"]+)"[^>]*value\="([^"]*)">/', $html, $matches);
$values = $matches[2];
$params = "";

$i=0;
foreach ($matches[1] as $name)
{
$params .= "$name=" . urlencode($values[$i]);
++$i;
if(isset($matches[$i]))
{
$params .= "&";
}
}

$params = trim ($params, "&");

curl_setopt($chpost, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($chpost, CURLOPT_RETURNTRANSFER,1);
curl_setopt($chpost, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($chpost, CURLOPT_POST, 1);
curl_setopt($chpost, CURLOPT_HEADERFUNCTION, 'read_header');


#parse the login form:
$matches = array();
preg_match('/<form [^>]+action\="([^"]+)"[^>]*>/', $html, $matches);
$opturl = $matches[1];


#parse the hidden fields:
preg_match_all('/<input type="hidden"[^>]*name\="([^"]+)"[^>]*value\="([^"]*)"[^>]*>/', $html, $matches);
$values = $matches[2];
$params = "";


$i=0;
foreach ($matches[1] as $name)
{
$paramsin[$name]=$values[$i];
++$i;
}

#some form specific javascript stuff before submission, this takes care of that:
$sPad="IfYouAreReadingThisYouHaveTooMuchFreeTime";
$lPad=strlen($sPad)-strlen($passwd);
$PwPad=substr($sPad, 0,($lPad<0)?0:$lPad);

$paramsin['PwdPad']=urlencode($PwPad);
foreach ($paramsin as $key=>$value)
{
$params .= "$key=" . urlencode($value) . "&";
}

curl_setopt($chpost, CURLOPT_URL, $opturl);
curl_setopt($chpost, CURLOPT_RETURNTRANSFER,1);
curl_setopt($chpost, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($chpost, CURLOPT_POST, 1);
curl_setopt($chpost, CURLOPT_POSTFIELDS, $params . "login=" . urlencode($login) . "&passwd=" . urlencode($passwd) . "&LoginOptions=2");
$html = curl_exec($chpost);


#test for valid login:
if((preg_match('/replace[^"]*"([^"]*)"/', $html, $matches)==0) && (preg_match("/url=([^\"]*)\"/si", $html, $matches)==0 || eregi("password is incorrect", $html)))
{
return 1;
}



#curl_setopt($chget, CURLOPT_URL, $location);
curl_setopt($chget, CURLOPT_URL,$matches[1]);
curl_setopt($chget, CURLOPT_RETURNTRANSFER,1);
curl_setopt($chget, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($chget, CURLOPT_HEADERFUNCTION, 'read_header');

$html = curl_exec($chget);


if (eregi("hotmail.msn.com/", $location))
{
#process the non-live interface
#passed the login, you need to load this page to get some more cookies to complete the login
curl_setopt($chget, CURLOPT_URL,"http://cb1.msn.com/hm/header.armx?lid=1033&cbpage=login&lc=1033&x=3.200.4104.0");
curl_setopt($chget, CURLOPT_RETURNTRANSFER,1);
curl_setopt($chget, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($chget, CURLOPT_HEADERFUNCTION, 'read_header');
$html = curl_exec($chget);

#follow the javascript redirection url:
curl_setopt($chget, CURLOPT_POST, 0);
curl_setopt($chget, CURLOPT_URL,$matches[1]);
curl_setopt($chget, CURLOPT_RETURNTRANSFER,1);
curl_setopt($chget, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($chget, CURLOPT_HEADERFUNCTION, 'read_header');

$html = curl_exec($chget);

#get the base url and build the url for the page with contacts:
preg_match("/(http:\/\/[^\/]*\/cgi-bin\/).*?curmbox=([^\& ]*).*?a=([^\& ]*)/i", $location, $baseurl);
$url = $baseurl[1] . "AddressPicker?a=$baseurl[3]&curmbox=$baseurl[2]&Context=InsertAddress&_HMaction=Edit&qF=to";

curl_setopt($chget, CURLOPT_URL,$url);
curl_setopt($chget, CURLOPT_USERAGENT, 0);
curl_setopt($chget, CURLOPT_RETURNTRANSFER,1);
curl_setopt($chget, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($chget, CURLOPT_HEADERFUNCTION, 'read_header');

$html = curl_exec($chget);

#parse the emails and names:
preg_match_all('/<option.*?value="([^"]*)"[^>]*>(.*?)\</i', $html, $emailarr);


#get rid of duplicates:
$emailsunique = array_unique($emailarr[1]);

$i = 0;
foreach ($emailsunique as $key => $value)
{
$emails[$i] = $emailarr[1][$key];
$names[$i++] = $emailarr[2][$key];
}

#return the result:
return array($names, $emails);
}

global $mspauth;
global $mspprof;
#override cookies for live mail
#this is a quick solution for right now

if (!eregi("@msn", $login))
{
$cookiearr["MSPAuth"] = $mspauth;
$cookiearr["MSPProf"] = $mspprof;

$cookie = "";
foreach ($cookiearr as $key=>$value)
{
$cookie .= "$key=$value; ";
}
$cookie = trim ($cookie, "; ");

curl_setopt($chget, CURLOPT_COOKIE, $cookie);
curl_setopt($chpost, CURLOPT_COOKIE, $cookie);
}

if (preg_match('/location\.replace[^"]*"([^"]*)"/', $html, $matches))
{
curl_setopt($chget, CURLOPT_URL,$matches[1]);
curl_setopt($chget, CURLOPT_RETURNTRANSFER,1);
curl_setopt($chget, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($chget, CURLOPT_HEADERFUNCTION, 'read_header');
$html = curl_exec($chget);
}

#new live mail ####################################################################################
if (strpos($html, "TodayLight") > 0)
{
$url = $location; //$matches[1];
$url = explode("/", $url);
$url[sizeof($url)-1] = "options.aspx?subsection=26";
$url = implode("/", $url);
curl_setopt($chget, CURLOPT_URL,$url);
curl_setopt($chget, CURLOPT_RETURNTRANSFER,1);
curl_setopt($chget, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($chget, CURLOPT_HEADERFUNCTION, 'read_header');

$html = curl_exec($chget);

preg_match_all('/<input type="hidden"[^>]*name\="([^"]+)"[^>]*value\="([^"]*)"[^>]*>/si', $html, $matches);
$values = $matches[2];
$params = "";


$i=0;
foreach ($matches[1] as $name)
{
$paramsin[$name]=$values[$i];
++$i;
}

$paramsin['ctl01$ExportButton'] = "Export contacts";
$paramsin['mt'] = $cookiearr['mt'];

foreach ($paramsin as $key=>$value)
{
$params .= "$key=" . urlencode($value) . "&";
}

curl_setopt($chpost, CURLOPT_URL, $url);
curl_setopt($chpost, CURLOPT_RETURNTRANSFER,1);
curl_setopt($chpost, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($chpost, CURLOPT_POST, 1);
curl_setopt($chpost, CURLOPT_POSTFIELDS, $params);
$html = curl_exec($chpost);


#parse the csv file:
$table = explode("\n", $html);

$separator = ",";
if (count($table)>0)
{
$rowzero = array_shift($table);
if (count(explode(",", $rowzero)) > 10 ) #10 doesn't mean a lot here, could be compared to 1
{
$separator = ",";
}
else
{
$separator = ";";
}
}

$maxi = count($table);

$names = array();
$emails = array();
$phones = array();//cosmin, phones

#parse emails and names:
for($i=0; $i<$maxi; ++$i)
{
$table[$i]=explode($separator,$table[$i]);

if (count($table[$i])>46)
{
$names[$i]=trim($table[$i][1],'"') . " " . trim($table[$i][3],'"');
$emails[$i]=trim($table[$i][46],'"');
$phones[$i]=trim($table[$i][25],'"');// cosmin, phones
}
}

#return the result:
return array($names, $emails, $phones);

}
################################################# end new live mail - old live mail below

$url = $matches[1];
$url = explode("/", $url);
$url[count($url)-1] = "ApplicationMainReach.aspx?Control=EditMessage&_ec=1&FolderID=00000000-0000-0000-0000-000000000001";//$matches[1];
$url = implode("/", $url);

curl_setopt($chget, CURLOPT_URL,$url);
curl_setopt($chget, CURLOPT_RETURNTRANSFER,1);
curl_setopt($chget, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($chget, CURLOPT_HEADERFUNCTION, 'read_header');

$html = curl_exec($chget);


preg_match_all('/<input.*?type\="?hidden"?.*?name\="([^"]*)".*?value\="([^"]*)"/si', $html, $matches);
$postarr[$matches[1][0]]=($matches[2][0]);
$postarr["query"]= "Find in Mail";
$postarr["ToContact"]= "To:";
$postarr["fTo"]= "";
$postarr["fCC"]= "";
$postarr["fBcc"]= "";
$postarr["fSubject"]= "";
$postarr["fMessageBody"]= "";

curl_setopt($chpost, CURLOPT_URL, $url);
curl_setopt($chpost, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($chpost, CURLOPT_RETURNTRANSFER,1);
curl_setopt($chpost, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($chpost, CURLOPT_POST, 1);
curl_setopt($chpost, CURLOPT_POSTFIELDS, $postarr);
curl_setopt($chpost, CURLOPT_HEADERFUNCTION, 'read_header');
$html = curl_exec($chpost);

preg_match_all('/<input type="checkbox" name="contactNameEmail" value="([^;]*);([^"]+)"/si', $html, $matches);

$emails = array_map("arrurldecode", $matches[2]);
$names = array_map("arrurldecode", $matches[1]);

return array($names, $emails);
}

function arrurldecode($val)
{
return urldecode ($val);
}

#read_header is essential as it processes all cookies and keeps track of the current location url
#leave unchanged, include it with get_contacts
function read_header($ch, $string)
{
global $location;
global $cookiearr;
global $chget;
global $chpost;
global $mspauth;
global $mspprof;

$length = strlen($string);
if(!strncmp($string, "Location:", 9))
{

$url = trim(substr($string, 9, -1));
if (eregi("http:", $url))
{
$location = $url;
}
else
{
$matches = array();
preg_match("#(https?\:\/\/[^\/]*)\/#si", $location, $matches);
$location = $matches[0] . $url;
}
}
if(!strncmp($string, "Set-Cookie:", 11))
{
$cookiestr = trim(substr($string, 11, -1));
$cookie = explode(';', $cookiestr);
$cookie = explode('=', $cookie[0]);
$cookiename = trim(array_shift($cookie));

if ($cookiename == "MSPAuth")
{
$mspauth = !empty($cookiearr['MSPAuth']) ? $cookiearr['MSPAuth'] : "";
}

if ($cookiename == "MSPProf")
{
$mspprof = !empty($cookiearr['MSPProf']) ? $cookiearr['MSPProf'] : "";
}

if ($cookiename!=="MSPOK" || !$cookiearr['MSPOK'])
{
$cookiearr[$cookiename] = trim(implode('=', $cookie));
}

}
$cookie = "";
if(trim($string) == "")
{
foreach ($cookiearr as $key=>$value)
{
$cookie .= "$key=$value; ";
}
$cookie = trim ($cookie, "; ");

curl_setopt($chget, CURLOPT_COOKIE, $cookie);
curl_setopt($chpost, CURLOPT_COOKIE, $cookie);
}

return $length;
}

?>

No comments:

Post a Comment