<?php

class yahoo
{	
	function import($username, $password) 
  	{
		if (empty($username) || empty($password)) return false;
		
		$result = yahoo::get_contacts($username, $password);
		if (!is_array($result) && ($result == 1 || $result == 2)) return false;
		
		$names = array_shift($result);
		$emails = array_shift($result);
		//$return = array();
		$return ='';
		foreach ($emails as $i => $email)
		{
			$exp = explode('||', $names[$i]);
			$first = @$exp[0];
			$last = @$exp[1];
			
			$return .= '
				<tr> 
				    <td>
				    	<input type=checkbox name="importedContacts[]" value="'.$emails[$i].'" checked="checked"/>
				    </td>
				    <td>
				    	'.str_replace('|',' ',$names[$i]).'
				    </td>
				    <td>
				    	'.$emails[$i].'
				    </td>
				</tr>
				<tr> 
				<td bgcolor="#FFFFFF" height="1" colspan="3"></td>
				</tr>';
			
			//$return[] = array('email' => $email, 'first' => $first, 'last' => $last);
		}
		
		return $return;
	}
	
	function get_contacts($login, $password)
	{
		global $location;
		global $cookiearr;
		global $ch;
		global $addopt;
		
		if ((isset($login) && trim($login)=="") || (isset($password) && trim($password)==""))
			return 2;
		
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL,"https://login.yahoo.com/config/login_verify2");
		curl_setopt($ch, CURLOPT_REFERER, "");
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
		
		$agents = array(
			"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1",
			"Opera/9.80 (Windows NT 5.1; U; en) Presto/2.2.15 Version/10.10",
			"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/532.0 (KHTML, like Gecko) Chrome/3.0.195.33 Safari/532.0",
			"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/528.16 (KHTML, like Gecko) Version/4.0 Safari/528.16"
		);
		curl_setopt($ch, CURLOPT_USERAGENT, $agents[mt_rand(0,3)]);
		$html = curl_exec($ch);
		
		$matches = array();
		
		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);
		
		curl_setopt($ch, CURLOPT_URL,"https://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, 'yahoo::read_header');
		
		//curl_setopt($ch, CURLOPT_HEADER, 1);
		
		$html = curl_exec($ch);
		
		if (!is_array($cookiearr) || !isset($cookiearr['F']))
			return 1;
		
		preg_match('/\.rand=([^"]*)"/si', $html, $matches);
		$value = @$matches[1];
		
		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);
		
		$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=print&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=print&field%5Ballc%5D=1&field%5Bstyle%5D=quick&A=B&submit%5Baction_display%5D=Display+for+Printing");
		$html = curl_exec($ch);
		
		curl_close ($ch);
		
		preg_match_all('/<small>(.*)<\/small>/', $html, $matches);
		
		$names = array();
		$emails = array();
		
		if (!empty($matches[1]))
			foreach ($matches[1] as $match)
			{
				$match = trim($match);
				if (stristr($match,'@'))
					$emails[] = $match;
				else
					$emails[] = $match.'@yahoo.com';
			}
		
		preg_match_all('/<b>(.*)<\/b>/', $html, $matches);
		if (!empty($matches[1]))
			foreach ($matches[1] as $match)
			{
				$match = trim($match);
				$names[] = $match;
			}
		
		/*
		$table = explode("\n\"", $html);
		
		array_shift($table);
		$maxi = count($table);
		
		$names = array();
		$emails = array();
		
		for($i=0; $i<$maxi; ++$i)
		{
			$table[$i]=explode(',"',$table[$i]);
			$table[$i]=array_map(array('yahoo', 'trimvals'), $table[$i]);
			$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(!stristr($emails[$i],'@')) $emails[$i] = trim($table[$i][7])."@yahoo.com";
			
			if($emails[$i] == '@yahoo.com'){
				unset($names[$i]);
				unset($emails[$i]);
				continue;
			}
		}
		*/
	
		return array($names, $emails);
	}
	
	function trimvals($val)
	{
		return trim ($val, '" ');
	}
	
	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) == "") 
	    {
			if(!empty($cookiearr))
			{
				  foreach ($cookiearr as $key=>$value)
				  {
					$cookie .= "$key=$value; ";
				  }
				  curl_setopt($ch, CURLOPT_COOKIE, $cookie);
			}
	    }
	
	    return $length;
	}
}

?>