<?php
/** 
* @file class.sohuHttp.php
* 获得sohu邮箱通讯录列表
* @author jvones<jvones@gmail.com> http://www.jvones.com/blog
* @date 2009-10-10
**/
define("COOKIEJAR1", tempnam("./tmp", "s1_"));
define("COOKIEJAR2", tempnam("./tmp", "s2_"));
define("COOKIEJAR3", tempnam("./tmp", "s3_"));

class netsEmailContact_soho extends netsEmailContact{
	private function login($username, $password){		
		//第一步：初步登陆
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		
		curl_setopt($ch, CURLOPT_URL, "https://passport.sohu.com/sso/login.jsp?userid=".$username."@sohu.com&password=".md5($password)."&appid=1000&persistentcookie=0&s=".time()."&b=2&w=1440&pwdtype=1");
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIEJAR1);
		curl_setopt($ch, CURLOPT_TIMEOUT, TIMEOUT);
		$contents = curl_exec( $ch );
		
		//file_put_contents('./sohuresult.txt', $contents);	
		curl_close( $ch );
		
		if ( strpos( $contents, "success" ) === false )
		{
			return 0;
		}
		return 1;
	}
	/**
	 * 获取邮箱通讯录-地址
	 * @param $user
	 * @param $password
	 * @param $result
	 * @return array
	 */
	public function getAddressList($username, $password){		
		if (!$this->login($username, $password))
		{
			return 0;
		}

		$cookies = array( );
		$bRet = $this->readcookies( COOKIEJAR1, $cookies );
		file_put_contents('./JSESSIONID.txt', $cookies['JSESSIONID']);	
		if ( !$bRet && !$cookies['JSESSIONID'] )
		{
			return 100;
		}

		//第二步：再次跳转到到上面$_url1
//		$ch = curl_init("http://login.mail.sohu.com/servlet/LoginServlet?appid=1000");		
//		
//		curl_setopt($ch, CURLOPT_TIMEOUT, TIMEOUT);
//		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//		curl_setopt($ch,CURLOPT_COOKIEFILE,COOKIEJAR1);
//		curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIEJAR2);		
//		curl_setopt($ch,CURLOPT_HEADER,1);	
//		$str2 = curl_exec($ch);
//		file_put_contents('./sohuresult2222.txt', $str2);
//		curl_close($ch);		

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIEJAR1);
		curl_setopt($ch, CURLOPT_TIMEOUT, TIMEOUT);
		curl_setopt($ch, CURLOPT_URL, "http://mail.sohu.com/bapp/95/main");
		$content = curl_exec( $ch );

		file_put_contents('./sohuresult_end.txt', $contents);	
		curl_close( $ch );
		$bRet = $this->_parsedata( $content, $result );
		if ( !$bRet )
		{
			return -1;
		}
		return 1;
	}
	function _parsedata( $content, &$ar ){
		$ar = array( );
		if ( !$content )
		{
			return 0;
		}
		$data = json_decode( $content );
		unset( $content );
		foreach ( $data->listString as $value )
		{
			if ( preg_match_all( "/[a-z0-9_\\.\\-]+@[a-z0-9\\-]+\\.[a-z]{2,6}/i", $value->email, $matches ) )
			{
				$emails = array_unique( $matches[0] );
				unset( $matches );
				foreach ( $emails as $email )
				{
					$ar[$email] = $value->name;
				}
			}
		}
		return 1;
	}	
	function readcookies( $file, &$result ){
		$fp = fopen( $file, "r" );
		while ( !feof( $fp ) )
		{
			$buffer = fgets( $fp, 4096 );
			$tmp = split( "\t", $buffer );
			$result[trim( $tmp[5] )] = trim( $tmp[6] );
		}
		return 1;
	}
}

?>