class GrabYahoo
{
/*-------------------------------------------------------
Public Variables
-------------------------------------------------------*/
/**
* Service name (1. addressbook, 2. messenger)
* @public
* @var string
*/
var $service = "";
/**
* Abosolute path to save the cookie
* Default value is DOCUMENT_ROOT
* @public
* @var string
*/
var $cookieJarPath = "";
/**
* Turns the localhost mode On or Off
* Default value is false
* @public
* @var boolean
*/
var $isLocalhost = false;
/**
* Abosulte path to the CA Bundle file
* SSL Certificate required to verify CA cert
* Usually required when script ran on Localhost
* Remote servers may not require
* Default value is false
* @public
* @var string
*/
var $caBundleFile = "";
/**
* Specifies if Proxy server required as Gateaway
* Default value is false
* @public
* @var boolean
*/
var $isUsingProxy = false;
/**
* Proxy host name
* @public
* @var string
*/
var $proxyHost = "";
/**
* Proxy port number
* @public
* @var int
*/
var $proxyPort = 0;
/*-------------------------------------------------------
Private Variables
-------------------------------------------------------*/
/**
* URL to Authenticate user on Yahoo!
* @private
* @var string
*/
var $authUrl = "https://login.yahoo.com/config/login?";
/**
* URL for the desired Service
* @private
* @var string
*/
var $serviceUrl = "";
/**
* URL to be used by cURL
* @private
* @var string
*/
var $url = "";
/**
* User agent (used to trick Yahoo!)
* @private
* @var string
*/
var $userAgent = "YahooSeeker-Testing/v3.9 (compatible; Mozilla 4.0; MSIE 5.5; http://search.yahoo.com/)";
/**
* Referer URL (used to trick Yahoo!)
* @private
* @var string
*/
var $referer = "http://my.yahoo.com";
/**
* Specifies whether output includes the header
* @private
* @var int
*/
var $showHeader = 0;
/**
* Specifies number of post fields to pass
* @private
* @var int
*/
var $numPostField = 0;
/**
* Specify fields to send via POST method as key=value
* @private
* @var string
*/
var $postFields = "";
/**
* File where output is temporarily saved during authentication
* @private
* @var string
*/
var $authOutputFile = "";
/**
* File where service output is temporarily saved
* @private
* @var string
*/
var $outputFile = "";
/**
* File where Cookie is temporarily saved
* @private
* @var string
*/
var $cookieFileJar = "";
/**
* Cookie File that is read by service process
* This carries same value as $cookieFileJar
* @private
* @var string
*/
var $cookieFile = "";
/**
* Specifies if Cookie is to be in header
* @private
* @var int
*/
var $cookie = 0;
/**
* Proxy address as proxy.host:port
* @private
* @var string
*/
var $proxy = "";
/**
* Sets Service URL
* @return void
*/
function setServiceUrl()
{
if (empty($this->service))
{
$this->setError("provide_service");
return false;
}
// Sets the URL depending on the choosen service
switch ($this->service)
{
case 'addressbook' : $this->serviceUrl = "http://address.yahoo.com/yab/us/Yahoo_ab.csv?loc=us&.rand=1671497644&A=H&Yahoo_ab.csv"; break;
case 'messenger' : $this->serviceUrl = "http://messenger.yahoo.com/edit/"; break;
}
}
/**
* Sets the Cookie Jar File where Cookie is temporarily saved
* @return void
*/
function setCookieJar()
{
// Sets the encrypted cookie filename using Yahoo! account username
$this->cookieFilename = MD5($this->login);
// Sets the Cookie Jar filename with an absolute path
$this->cookieFileJar = (!empty($this->cookieJarPath)) ? $this->cookieJarPath . "/" . $this->cookieFilename : $_SERVER['DOCUMENT_ROOT'] . "/" . $this->cookieFilename;
}
/**
* Sets cURL options
* @return boolean
*/
function setCurlOption()
{
// Sets the User Agent
curl_setopt($this->curlSession, CURLOPT_USERAGENT, $this->userAgent);
// Sets the HTTP Referer
curl_setopt($this->curlSession, CURLOPT_REFERER, $this->referer);
// Sets the URL that PHP will fetch using cURL
curl_setopt($this->curlSession, CURLOPT_URL, $this->url);
// Sets the number of fields to be passed via HTTP POST
curl_setopt($this->curlSession, CURLOPT_POST, $this->numPostField);
// Sets the fields to be sent via HTTP POST as key=value
curl_setopt($this->curlSession, CURLOPT_POSTFIELDS, $this->postFields);
// Sets the filename where cookie information will be saved
curl_setopt($this->curlSession, CURLOPT_COOKIEJAR, $this->cookieFileJar);
// Sets the filename where cookie information will be looked up
curl_setopt($this->curlSession, CURLOPT_COOKIEFILE, $this->cookieFile);
// Sets the option to set Cookie into HTTP header
curl_setopt($this->curlSession, CURLOPT_COOKIE, $this->cookie);
// Checking if the script is ran on Localhost or Remote server
if ($this->isLocalhost)
{
if (empty($this->caBundleFile))
{
$this->setError("provide_ca");
return false;
}
// Sets the CA Bundle file for SSL Certificate to verify
// This file is mainly used when the script is ran on Localhost
curl_setopt($this->curlSession, CURLOPT_CAINFO, $this->caBundleFile);
}
// Sets the proxy server as proxy.host:port
curl_setopt($this->curlSession, CURLOPT_PROXY, $this->proxy);
// Sets the filename where output will be temporarily saved
curl_setopt($this->curlSession, CURLOPT_FILE, $this->fileHandler);
// Sets the option to include HTTP header into the output
curl_setopt($this->curlSession, CURLOPT_HEADER, $this->showHeader);
return true;
}
/**
* Executes the Service
* @param string $login Username of user's Yahoo! Account
* @param string $password Password of the user's Yahoo! Account
* @return array|false
*/
function execService($login, $password)
{
$login = trim($login);
$password = trim($password);
if (empty($login))
{
$this->setError("provide_login");
return false;
}
if (empty($password))
{
$this->setError("provide_pass");
return false;
}
// Instructs to authenticate user on Yahoo!
$this->auth = $this->doAuthentication();
if ($this->auth)
{
// Instructs to fetch output if Authenticated
$this->getServiceOutput();
return $this->serviceOutput;
}
}
/**
* Authenticates user on Yahoo!
* @return boolean
*/
function doAuthentication()
{
// Instructs to initialize cURL session
$this->initCurl();
// Sets the URL for authentication purpose
$this->url = $this->authUrl;
// Sets the number of fields to send via HTTP POST
$this->numPostField = 22;
// Sets the fields to be sent via HTTP POST as key=value
$this->postFields = "login=$this->login&passwd=$this->password&.src=&.tries=5&.bypass=&.partner=&.md5=&.hash=&.intl=us&.tries=1&.challenge=ydKtXwwZarNeRMeAufKa56.oJqaO&.u=dmvmk8p231bpr&.yplus=&.emailCode=&pkg=&stepid=&.ev=&hasMsgr=0&.v=0&.chkP=N&.last=&.done=" . $this->serviceUrl;
// Instructs to set Cookie Jar
$this->setCookieJar();
// Sets the filename for output generated during authentication
$this->authOutputFile = "auth.output." . MD5($this->login) . ".txt";
// Instructs to open a file for writing the output
$this->fileHandler = fopen($this->authOutputFile, "w");
// Checks if the cURL options are all set properly
if ($this->setCurlOption())
{
// Instructs to execute cURL session
$this->execCurl();
// Checks if any cURL error is generated
if ($this->getCurlError())
{
$this->setError("curl_error");
return false;
}
fclose($this->fileHandler);
// Sets the content of output as a string
$authContent = file_get_contents($this->authOutputFile);
// Checks if the authentication failed, either invalid login or username is not registered
if ((preg_match("/invalid/i", $authContent)) || (preg_match("/not yet taken/i", $authContent)))
{
// Instructs to close cURL session
$this->closeCurl();
// Unlinks the output file
$this->unlinkFile($this->authOutputFile);
// Unlinks the cookie file
$this->unlinkFile($this->cookieFileJar);
/**
* Sets the Service Output
* @return void
*/
function getServiceOutput()
{
// Checks if the user needs proxy (to be set by user)
if ($this->isUsingProxy)
{
// Checks if the proxy host and port is specified
if ((empty($this->proxyHost)) || (empty($this->proxyPort)))
{
$this->setError("proxy_required");
$this->unlinkFile($this->cookieFileJar);
return false;
}
// Sets the proxy address as proxy.host:port
$this->proxy = $this->proxyHost . ":" . $this->proxyPort;
}
// Instructs to process the choosen service
switch ($this->service)
{
case 'addressbook' : $this->showHeader = 0;
$this->serviceOutput = $this->processAddressBook();
break;
if ($this->setCurlOption())
{
$this->execCurl();
$this->closeCurl();
fclose($this->fileHandler);
// Sets the service output as an array
$fileContentArr = file($this->outputFile);
// Sets the address book column headings
$abColumnHeadLine = trim($fileContentArr[0]);
$abColumnHeadLine = str_replace("\"", "", $abColumnHeadLine);
// Sets the address book column headings into an array
$abColumnHeadArr = explode(",", $abColumnHeadLine);
// Unsets the heading line from the file content array
unset($fileContentArr[0]);
foreach ($fileContentArr as $key => $value)
{
// Sets the address book list individually
$listColumnLine = trim($value);
$listColumnLine = str_replace("\"", "", $listColumnLine);
// Sets the individual list into an array
$listColumnArr = explode(",", $listColumnLine);
// Iterates through each item of individual address in the list
foreach ($listColumnArr as $listColumnKey => $listColumnValue)
{
// Sets the column heading as key
$listKey = $abColumnHeadArr[$listColumnKey];
// Sets the value for the key respectively
$list_[$listKey] = $listColumnValue;
}
// Sets the address book list in an array
$list[] = $list_;
}
// Sets the array seperating the Messenger Contact Groups
$listGroupArr = explode("<b>", $value);
}
foreach ($listGroupArr as $key => $value)
{
if (!empty($value))
{
$value = str_replace("</b>", "", $value);
// Sets the array with contacts seperated in different array index
$listArrForGroup[] = explode("·", $value);
}
}
foreach ($listArrForGroup as $key => $value)
{
foreach ($value as $subKey => $subValue)
{
if ($subKey === 0)
{
// Sets the Contact Group name as the array key
$arrKey = trim($subValue);
}
if ($subKey !== 0)
{
// Sets the array of contacts by the Contact Group
// preg_replace is used to replace any non alphanumeric character or an underscore
$subValue = trim($subValue);
$subValue = preg_replace("/\Wn/", "", $subValue);
$list[$arrKey][] = $subValue;
}
}
}
/*
Setting the desired Service
1. addressbook => Used to grab Yahoo! Address Book
2. messenger => Used to grab Yahoo! Messenger List
*/
$yahoo->service = "addressbook";
// The CA Bundle file, used to run the script on Localhost
$yahoo->caBundleFile = $_SERVER['DOCUMENT_ROOT'] . "/path/ca-bundle.crt";
/*
Set to true if running on Localhost
- Setting it to true will require a CA Bundle file
*/
$yahoo->isLocalhost = true;
/*
Set to true if HTTP proxy is required to browse net
- Setting it to true will require to provide Proxy Host Name and Port number
*/
$yahoo->isUsingProxy = true;
// Set the Proxy Host Name
$yahoo->proxyHost = "proxy.host";
// Set the Proxy Port Number
$yahoo->proxyPort = "port";
// Set the location to save the Cookie Jar File
$yahoo->cookieJarPath = $_SERVER['DOCUMENT_ROOT'] . "/path/";
/*
Execute the Service
- Require to pass Yahoo! Account Username and Password
*/
$yahooList = $yahoo->execService($login, $password);
// Printing the array generated by the Class
dump($yahooList);
// Use this line to figure out any error generated during the process
echo $yahoo->errorInfo;
function dump($var)
{
echo "<pre>";
print_r($var);
echo "</pre>";
}