本来想模拟登录 google docs,但一直未成功,郁闷中...
}
function gzBody($gzData){
if(substr($gzData,0,3)=="/x1f/x8b/x08"){
$i=10;
$flg=ord(substr($gzData,3,1));
if($flg>0){
if($flg&4){
list($xlen)=unpack('v',substr($gzData,$i,2));
$i=$i+2+$xlen;
}
if($flg&8) $i=strpos($gzData,"/0",$i)+1;
if($flg&16) $i=strpos($gzData,"/0",$i)+1;
if($flg&2) $i=$i+2;
}
return gzinflate(substr($gzData,$i,-8));
}
else return false;
}
function gzDocOut($contents) {
header("Content-Encoding: gzip");
$output = "/x1f/x8b/x08/x00/x00/x00/x00/x00";
$output .= substr(gzcompress($contents, 2), 0, -4);
echo $output;
exit();
}
function postDoc()
{
$res='';
$fp = fsockopen("docs.google.com", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />/n";
} else {
$out = "GET / HTTP/1.1/r/n";//Doc?docid=0AZEa4CrZhKjLZG5yNWg3Y18yNzJkZGZxY3NkaA&hl=en
$out .= "Host: docs.google.com/r/n";
$out .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.7) Gecko/20100713 Firefox/3.6.7/r/n";
$out .= "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8/r/n";
$out .= "Keep-Alive: 115/r/n";
$out .= "Connection: keep-alive/r/n/r/n";
if($flag=fwrite($fp, $out))
{
while (!feof($fp)) {
$res .= fgets($fp, 128);
if(strpos($res,"/r/n/r/n")!==false){ //only fetch header
break;
}
}
}
fclose($fp);
//var_dump($flag);var_dump($res);exit;
preg_match("/Location:([^/n]+)/i", $res, $matches);
return array("Location"=>trim($matches[1]));
}
}
function getLogin($url)
{
$res='';
$arr_cookie=array();
$arr_url=parse_url($url);
$fp = fsockopen('ssl://www.google.com', 443, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />/n";
} else {
$out = "GET {$arr_url['path']}?{$arr_url['query']} HTTP/1.1/r/n";
$out .= "Host: {$arr_url['host']}/r/n";
$out .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.7) Gecko/20100713 Firefox/3.6.7/r/n";
$out .= "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8/r/n";
$out .= "Keep-Alive: 115/r/n";
$out .= "Connection: close/r/n/r/n";
//echo '<pre>';var_dump($out);exit;
fwrite($fp, $out);
while (!feof($fp)) {
$res.= @fgets($fp, 4096);
if(preg_match('//<input/stype="hidden"/sname="dsh"/sid="dsh"/s+value="([^"]+)"/s//>/',$res,$matches_dsh))
{
$break;
}
}
fclose($fp);
//var_dump($url); var_dump($res);
preg_match_all("/^Set/-Cookie:/s(.+)$/mi", $res, $matches_cookie);
$cookie=$matches_cookie[1];
foreach((array)($matches_cookie[1]) as $v)
{
if(preg_match("/GALX=(/w+)/i",$v,$match_galx))
{
$galx=$match_galx[1];
}
$arr_cookie+=parseCookie($v);
}
//preg_match('//<input/stype="hidden"/sname="dsh"/sid="dsh"/s+value="([^"]+)"/s//>/',$res,$matches_dsh);
$dsh=$matches_dsh[1];
// var_dump($galx);exit;
return array("galx"=>$galx,"cookie"=>$arr_cookie,"dsh"=>$dsh);
}
}
function parseCookie($cookies)
{
$arr=explode(";",$cookies);
if(in_array("Secure",$arr))
{
$secure=true;
}else{
$secure=false;
}
if(in_array("HttpOnly",$arr))
{
$httponly =true;
}else{
$httponly =false;
}
foreach((array)$arr as $cookie){
$arr_cookie=explode("=",$cookie);
if(count($arr_cookie)==2)
{
switch(trim($arr_cookie[0]))
{
case 'Domain':
$domain=$arr_cookie[1];
break;
case 'Path':
$path=$arr_cookie[1];
break;
case 'Expires':
$expire=strtotime($arr_cookie[1]);
break;
default:
$name=$arr_cookie[0];
$value=$arr_cookie[1];
break;
}
}
}
$domain=$domain?$domain:'.google.com';
setcookie($name,$value,$expire ,$path ,$domain ,$secure,$httponly );
return array($name=>$value);
}
//include url,galx,dsh
function postAuth($params)
{
$post=array();
$post['GALX']=$params['galx'];
$post['dsh']=$params['dsh'];
$post['Email']='google用户名@gmail.com';
$post['Passwd']='google密码';
$post['PersistentCookie']='yes';
$post['asts']='';
$post['continue']='http://docs.google.com/';
$post['followup']='http://docs.google.com/';
$post['hl']='en';
$post['ltmpl']='homepage';
$post['rmShown']='1';
$post['secTok']='';
$post['service']='writely';
$post['signIn']='Sign in';
$post['timeStmp']='';
//$str_post=http_build_query($post);
foreach($post as $key=>$value)
$post2[]="$key=".urlencode($value);
$str_post=implode("&",$post2);
$length=strlen($str_post);
$fp = fsockopen('ssl://www.google.com', 443, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />/n";
} else {
$out = "POST /accounts/ServiceLoginAuth HTTP/1.1/r/n";
$out .= "Host: www.google.com/r/n";
$out .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.7) Gecko/20100713 Firefox/3.6.7/r/n";
$out .= "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8/r/n";
$out .= "Keep-Alive: 115/r/n";
$out .= "Connection: keep-alive/r/n";
$out .= "Referer: {$params['referer']}/r/n";
$out .= "Cookie: GoogleAccountsLocale_session=en; __utma=173272373.847323090.1280126480.1280126480.1280126480.1; __utmb=173272373.1.10.1280126480; __utmc=173272373; __utmz=173272373.1280126480.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); GALX={$params['galx']}/r/n";
$out .= "Content-Type: application/x-www-form-urlencoded/r/n";
$out .= "Content-Length: {$length}/r/n/r/n";
$out .= $str_post;
fwrite($fp, $out);
while (!feof($fp)) {
$res.= @fgets($fp, 128);
if(strpos($res,"/r/n/r/n")!==false){ //only fetch header
break;
}
}
fclose($fp);
}
$arr_cookie=array();
preg_match_all("/^Set/-Cookie:/s(.+)$/mi", $res, $matches_cookie);
$cookie=$matches_cookie[1];
foreach((array)($matches_cookie[1]) as $v)
{
/*if(preg_match("/LSID=(/w+)/i",$v,$match_lsid))
{
$lsid=$match_lsid[1];
}
if(preg_match("/SID=(/w+)/i",$v,$match_sid))
{
$sid=$match_sid[1];
}
if(preg_match("/HSID=(/w+)/i",$v,$match_hsid))
{
$hsid=$match_hsid[1];
}
if(preg_match("/SSID=(/w+)/i",$v,$match_ssid))
{
$ssid=$match_ssid[1];
}*/
$arr_cookie=array_merge($arr_cookie,parseCookie($v));
}
preg_match("/Location:([^/n]+)/i", $res, $matches_location);
return array_merge(array("url"=>$matches_location[1]), $arr_cookie);
}
//include LSID,SID,HSID,SSID,galx,url,refer
function checkCookie($params)
{
$res='';
$arr_cookie=array();
$arr_url=parse_url(trim($params['url']));
$fp = fsockopen('ssl://www.google.com', 443, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />/n";
} else {
$out = "GET {$arr_url['path']}?{$arr_url['query']} HTTP/1.1/r/n";
$out .= "Host: {$arr_url['host']}/r/n";
$out .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.7) Gecko/20100713 Firefox/3.6.7/r/n";
$out .= "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8/r/n";
$out .= "Keep-Alive: 115/r/n";
$out .= "Referer: {$params['referer']}/r/n";
$out .= "Cookie: GoogleAccountsLocale_session=en; __utma=173272373.847323090.1280126480.1280126480.1280126480.1; __utmb=173272373.1.10.1280126480; __utmc=173272373; __utmz=173272373.1280126480.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); GALX={$params['galx']}; GAUSR=google用户名@gmail.com; LSID={$params['LSID']}; SID={$params['SID']}; HSID={$params['HSID']}; SSID={$params['SSID']}/r/n";
$out .= "Connection: close/r/n/r/n";
//echo '<pre>';var_dump($params['url']);var_dump($out);fclose($fp);exit;
fwrite($fp, $out);
while (!feof($fp)) {
$res.= @fgets($fp, 4096);
if(preg_match("//r/n/r/n/", $res, $matches_page)==2){ //only fetch header
break;
}
}
fclose($fp);
// var_dump($res);exit;
preg_match_all("/^Set/-Cookie:/s(.+)$/mi", $res, $matches_cookie);
$cookie=$matches_cookie[1];
foreach((array)($matches_cookie[1]) as $v)
{
$arr_cookie+=parseCookie($v);
}
preg_match("/url='(.*?)'/i",$res,$matches_location);
//$res=str_replace("<",">",$res);
//var_dump($res);var_dump($matches_location);exit;
return array("url"=>$matches_location[1]);
}
}
function enterDoc($params=array())
{
$res='';
$arr_cookie=array();
$arr_url=parse_url(trim($params['url']));
$fp = fsockopen('docs.google.com', 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />/n";
} else {
$out = "GET {$arr_url['path']}?{$arr_url['query']} HTTP/1.1/r/n";
$out .= "Host: {$arr_url['host']}/r/n";
$out .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8/r/n";
$out .= "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8/r/n";
$out .= "Accept-Language: en-us,zh-cn;q=0.7,zh;q=0.3/r/n";
$out .= "Accept-Encoding: gzip,deflate/r/n";
$out .= "Accept-Charset: GB2312,utf-8;q=0.7,*;q=0.7/r/n";
$out .= "Keep-Alive: 115/r/n";
$out .= "Connection: close/r/n";
$out .= "Cookie: SID={$params['SID']}; HSID={$params['HSID']}/r/n/r/n";
//echo '<pre>';var_dump($params['url']);var_dump($out);fclose($fp);exit;
fwrite($fp, $out);
while (!feof($fp)) {
$res.= @fgets($fp, 4096);
if(preg_match("//r/n/r/n/", $res, $matches_page)==2){ //only fetch header
break;
}
}
fclose($fp);
// var_dump($res);exit;
preg_match_all("/^Set/-Cookie:/s(.+)$/mi", $res, $matches_cookie);
$cookie=$matches_cookie[1];
foreach((array)($matches_cookie[1]) as $v)
{
$arr_cookie+=parseCookie($v);
}
preg_match("/Location:([^/n]+)/i", $res, $matches_location);
return array_merge(array("url"=>$matches_location[1]), $arr_cookie);
}
}
function outputDoc($params=array())
{
$res='';
$arr_cookie=array();
$params['url']=html_entity_decode(trim($params['url']));
$arr_url=parse_url($params['url']);
$fp = fsockopen('docs.google.com', 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />/n";
} else {
$out = "GET {$arr_url['path']}?{$arr_url['query']} HTTP/1.1/r/n";
$out .= "Host: {$arr_url['host']}/r/n";
$out .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8/r/n";
$out .= "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8/r/n";
$out .= "Accept-Language: en-us,zh-cn;q=0.7,zh;q=0.3/r/n";
$out .= "Accept-Encoding: gzip,deflate/r/n";
$out .= "Accept-Charset: GB2312,utf-8;q=0.7,*;q=0.7/r/n";
$out .= "Keep-Alive: 115/r/n";
$out .= "Connection: close/r/n";
$out .= "Cookie: SID={$params['SID']}; HSID={$params['HSID']}; WRITELY_SID={$params['WRITELY_SID']}/r/n/r/n";
//echo '<pre>';var_dump($params['url']);var_dump($out);fclose($fp);exit;
fwrite($fp, $out);
while (!feof($fp)) {
$res.= @fgets($fp, 4096);
// if(preg_match("//r/n/r/n/", $res, $matches_page)==2){ //only fetch header
//break;
//}
}
fclose($fp);
preg_match_all("/^Set/-Cookie:/s(.+)$/mi", $res, $matches_cookie);
$cookie=$matches_cookie[1];
foreach((array)($matches_cookie[1]) as $v)
{
$arr_cookie+=parseCookie($v);
}
$arr_res=preg_split("//r/n/r/n/",$res);
echo $arr_res[1];exit;
gzuncompress((substr($arr_res[1],6)));
file_put_contents("response.gzip",(substr($arr_res[1],6)));
$content= gzopen("response.gzip",'r');
echo $content;
// header("Location: {$params['url']}");
//header("Location: docs.google.com");
}
}
$ret_doc=postDoc();
$ret_login=getLogin($ret_doc['Location']);
$ret_login['referer']=$ret_doc['Location'];
$ret_auth=postAuth($ret_login);
$ret_auth['referer']=$ret_doc['Location'];
$ret_auth['galx']=$ret_login['galx'];
//echo '<pre>';print_r($ret_auth);exit;
$ret_checkCookie=checkCookie($ret_auth);
$ret_checkCookie=array_merge($ret_auth,$ret_checkCookie);
$ret_enterDoc=enterDoc($ret_checkCookie);
$ret_enterDoc=array_merge($ret_auth,$ret_enterDoc);
outputDoc($ret_enterDoc)
//var_dump($ret_doc);var_dump($ret_login);var_dump($ret_auth);var_dump($ret_checkCookie);
?>