<?php

/* 
*--------------------------------------------------------------- 
* WEBQQ协议 
*--------------------------------------------------------------- 
* @copyright (c) 2014 
*快乐是福> 
* @version 1.0 
* @仅供研究WEBQQ协议所用。 
*/ 
class webqq{ 
public $db; 
public $qq; 
public $qqrow; 
public $pwd; 
public $code; 
public $error; 
public $msg; 
public $cookie; 
public $cookiearr; 
public $ptwebqq; 
public $psessionid; 
public $clientid; 
public $msgid; 

function set($qq,$pwd=0,$row=0){ 
global $db; 
$this->db = $db; 
$this->qq=$qq; 
$this->clientid='5'.rand(100000,999999); 
$this->msgid=3750000; 
if($pwd){ 
$this->pwd=$pwd; 
} 
if($row){ 
$this->qqrow=$row; 
} 
} 

function runs(){ 
if($this->qqrow){ 
$rows=$this->qqrow; 
$this->cookie=$rows['cookie']; 
$this->ptwebqq=$rows['ptwebqq']; 
$this->psessionid=$rows['psessionid']; 
$this->clientid=$rows['clientid']; 
$this->msgid=$rows['msgid']; 
$runurl="http://d.web2.qq.com/channel/poll2"; 
$post='r=%7B%22clientid%22%3A%22'.$this->clientid.'%22%2C%22psessionid%22%3A%22'.$this->psessionid.'%22%2C%22key%22%3A0%2C%22ids%22%3A%5B%5D%7D&clientid='.$this->clientid.'&psessionid='.$this->psessionid; 
$get=$this->getcurl($runurl,$this->cookie,0,$post,1); 
$arr=json_decode($get,true); 
if(@array_key_exists('retcode',$arr) && $arr['retcode']==0){ 
$this->msg[]='刷新成功！'; 
foreach($arr['result'] as $news){ 
if($news['poll_type']=='message' and $news['value']['msg_type']==9){ 
//$nike=$this->getnike($news['value']['to_uin']); 
$this->msg[]='收到'.$news['value']['to_uin'].'的消息'; 
$mg=trim($news['value']['content'][1]); 
$reply=$this->robot($mg); 
$rerow=$this->sendmg($news['value']['from_uin'],$reply); 
if(array_key_exists('retcode',$rerow) && $rerow['retcode']==0){ 
$this->msg[]="回复 (".$news['value']['to_uin'].") 成功！"; 
}else{ 
$this->error[]="回复 (".$news['value']['to_uin'].") 失败！"; 
} 
}elseif($news['poll_type']=='group_message'){ 
$this->msg[]='收到群'.$news['value']['info_seq'].'的消息'; 
$mg=trim($news['value']['content'][1]); 
$reply=$this->robot($mg); 
$rerow=$this->sendmg($news['value']['from_uin'],$reply,1); 
print_r($rerow); 
if(array_key_exists('retcode',$rerow) && $rerow['retcode']==0){ 
$this->msg[]="回复群(".$news['value']['info_seq'].") 成功！"; 
}else{ 
$this->error[]="回复群(".$news['value']['info_seq'].") 失败！"; 
} 
} 
} 
}elseif(@array_key_exists('retcode',$arr)){ 
$this->error[]='刷新出现异常！错误码:'.$arr['retcode'].$arr['errmsg']; 
$this->checkvc(); 
}else{ 
$this->msg[]='刷新时间过长，说明暂时无消息！'; 
} 
}else{ 
$this->checkvc(); 
} 
} 
function checkvc(){ 
$vcurl="https://ssl.ptlogin2.qq.com/check?uin=".$this->qq."&appid=1003903"; 
$get=$this->getcurl($vcurl,0,1); 
$arr=$this->pregs($get,'checkvc'); 
if(array_key_exists(1,$arr) && $arr[1]==0){ 
$carr=$this->pregs($get,'cookie'); 
$this->cookie=$this->cookies($carr); 
$this->login($arr[2],1); 
$this->msg[]='不需要验证码！'; 
}else{ 
$this->getvc(); 
$this->msg[]='需要验证码！'; 
} 
} 
function getvc(){ 
$codeurl='https://ssl.captcha.qq.com/getimage?aid=1003903&uin='.$this->qq; 
$get=$this->getcurl($codeurl,0,1); 
$arr=$this->pregs($get,'cookie'); 
$this->cookie=$arr[0]; 
$this->code=$this->pregs($get,'code'); 
$array['qq']=$this->qq; 
$array['pwd']=$this->pwd; 
$array['cookie']=$this->cookie; 
$array['code']=$this->code; 
$_SESSION['webcode']=$array; 
} 
function login($code=0,$do=0){ 
if($do){ 
$cookie=$this->cookie; 
$mm=$this->getmm($code); 
}else{ 
$cookie=$_SESSION['webcode']['cookie']; 
unset($_SESSION['webcode']); 
$mm=$this->getmm($code); 
} 
$login1url="https://ssl.ptlogin2.qq.com/login?u=". 
$this->qq."&p=".$mm."&verifycode=".$code."&webqq_type=10&remember_uin=1&login2qq=1&aid=1003903&u1=http%3A%2F%2Fweb2.qq.com%2Floginproxy.html%3Flogin2qq%3D1%26webqq_type%3D10&h=1&ptredirect=0&ptlang=2052&daid=164&from_ui=1&pttype=1&dumy=&fp=loginerroralert&action=13-53-2524995&mibao_css=m_webqq&t=4&g=1&js_type=0&js_ver=10087&login_sig=*5fN14OiNAlez7vQbzLYkY9E7pV5BJfm5QxSMPXCZn1wWKmx5hGse0tzTC5XGzVp&pt_uistyle=5"; 
$get=$this->getcurl($login1url,$cookie,1); 
$arr=$this->pregs($get,'cookie'); 
$this->cookie=$this->cookies($arr); 
$this->cookiearr=$this->cookies($arr,1); 
$this->ptwebqq=$this->cookiearr['ptwebqq']; 
$arr=$this->pregs($get,'login1'); 
if(array_key_exists(1,$arr) && $arr[1]==0){ 
$this->msg[]='握手登录成功！'; 
$this->check($arr[3]); 
$this->login2(); 
}elseif(array_key_exists(1,$arr)){ 
$this->error[]='握手登录失败！原因:'.$arr[5]; 
}else{ 
$this->error[]='握手登录链接服务器失败！'; 
} 
} 
function check($url){ 
$get=$this->getcurl($url,$this->cookie); 
$arr=$this->pregs($get,'cookie'); 
$this->cookie=$this->cookies($arr); 
$this->cookiearr=$this->cookies($arr,1,$this->cookiearr); 
} 
function login2(){ 
$login2url="http://d.web2.qq.com/channel/login2"; 
$post='r=%7B%22status%22%3A%22online%22%2C%22ptwebqq%22%3A%22'.$this->ptwebqq.'%22%2C%22passwd_sig%22%3A%22%22%2C%22clientid%22%3A%22'.$this->clientid.'%22%2C%22psessionid%22%3Anull%7D&clientid='.$this->clientid.'&psessionid=null'; 
$get=$this->getcurl($login2url,$this->cookie,0,$post,1); 
$arr=json_decode($get,true); 
if(array_key_exists('retcode',$arr) && $arr['retcode']==0){ 
$this->msg[]='WEBQQ登录成功！'; 
$this->psessionid=$arr['result']['psessionid']; 
$this->cookie=$this->arrcookie($this->cookiearr); 
$this->putsql(); 
}elseif(array_key_exists('retcode',$arr)){ 
$this->error[]='WEBQQ登录失败！错误码:'.$arr['retcode'].$arr['errmsg']; 
}else{ 
$this->error[]='WEBQQ登录链接服务器失败！'; 
} 
} 
function poll2(){ 
$poll2url='http://d.web2.qq.com/channel/poll2'; 

} 
function getnike($un){ 
if($data=file_get_contents("http://r.qzone.qq.com/cgi-bin/user/cgi_personal_card?uin=".$un)){ 
$data=str_replace(array('_Callback(',');'),array('',''),$data); 
$row=json_decode($data, true); 
return $row['nickname']; 
}else{ 
return $un; 
} 

} 
function logs(){ 
$msg=$this->encode_json($this->msg); 
$error=$this->encode_json($this->error); 
$time=time(); 
$this->db->query("insert into kl_logs (qq,msg,error,addtime) values ('".$this->qq."','$msg','$error','$time')"); 
if($row=$this->db->get_row("select logid from kl_logs where qq='".$this->uin."' order by logid desc limit 499,1")){ 
$this->db->query("delete from kl_logs where qq='".$this->qq."' and logid<".$row['logid']); 
} 
} 
function encode_json($str) { 
return urldecode(json_encode($this->url_encode($str))); 
} 

function url_encode($str) { 
if(is_array($str)) { 
foreach($str as $key=>$value) { 
$str[urlencode($key)] = $this->url_encode($value); 
} 
} else { 
$str = urlencode($str); 
} 
return $str; 
} 
function sendmg($uin,$mg,$do=0){ 
$msgid=$this->msgid; 
if($do){ 
$post = "r=%7B%22group_uin%22%3A{$uin}%2C%22content%22%3A%22%5B%5C%22{$mg}%5C%5Cn%5C%22%2C%5B%5C%22font%5C%22%2C%7B%5C%22name%5C%22%3A%5C%22%E5%AE%8B%E4%BD%93%5C%22%2C%5C%22size%5C%22%3A%5C%2210%5C%22%2C%5C%22style%5C%22%3A%5B0%2C0%2C0%5D%2C%5C%22color%5C%22%3A%5C%22000000%5C%22%7D%5D%5D%22%2C%22msg_id%22%3A". $msgid."%2C%22clientid%22%3A%22".$this->clientid."%22%2C%22psessionid%22%3A%22".$this->psessionid."%22%7D&clientid=".$this->clientid."&psessionid=".$this->psessionid; 
$sendurl="http://d.web2.qq.com/channel/send_qun_msg2"; 
$this->msgid=++$msgid; 
//$this->db->query("update kl_webqqs set msgid='".$msgid."' where webid='".$this->qqrow['webid']."'"); 
}else{ 
$post = "r=%7B%22to%22%3A{$uin}%2C%22face%22%3A606%2C%22content%22%3A%22%5B%5C%22{$mg}%5C%5Cn%5C%22%2C%5B%5C%22font%5C%22%2C%7B%5C%22name%5C%22%3A%5C%22%E5%AE%8B%E4%BD%93%5C%22%2C%5C%22size%5C%22%3A%5C%2210%5C%22%2C%5C%22style%5C%22%3A%5B0%2C0%2C0%5D%2C%5C%22color%5C%22%3A%5C%22000000%5C%22%7D%5D%5D%22%2C%22msg_id%22%3A". $msgid."%2C%22clientid%22%3A%22".$this->clientid."%22%2C%22psessionid%22%3A%22".$this->psessionid."%22%7D&clientid=".$this->clientid."&psessionid=".$this->psessionid; 
$sendurl="http://d.web2.qq.com/channel/send_buddy_msg2"; 
} 
$get=$this->getcurl($sendurl,$this->cookie,0,$post,1); 
$arr=json_decode($get,true); 
return $arr; 
} 
function robot($mg){ 
$apikey="a82987edf37e207410fd7e0cf039422a"; 
$apiurl="http://www.tuling123.com/openapi/api?key=".$apikey."&info="; 
$url=$apiurl.$mg; 
$json=file_get_contents($url); 
$row=json_decode($json,true); 
return $row['text']; 
} 
function putsql(){ 
if($this->qqrow['webid']){ 
$this->db->query("update kl_webqqs set zt=0,cookie='".$this->cookie."',ptwebqq='".$this->ptwebqq."',psessionid='".$this->psessionid."',clientid='".$this->clientid."' where webid='".$this->qqrow['webid']."'"); 
}else{ 
$this->db->query("insert into kl_webqqs (qq,zt,cookie,ptwebqq,psessionid,clientid) values ('".$this->qq."','0','".$this->cookie."','".$this->ptwebqq."','".$this->psessionid."','".$this->clientid."')"); 
} 
} 
function arrcookie($arr){ 
$keys=array_keys($arr); 
foreach($keys as $key){ 
$cookie.=$key.'='.$arr[$key].';'; 
} 
return $cookie; 
} 
function getcurl($url,$sendcookie=0,$getcookie=1,$post=0,$lailu=0){ 
$ch=curl_init($url); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
if($sendcookie){ 
curl_setopt($ch, CURLOPT_COOKIE, $sendcookie); 
} 
if($getcookie){ 
curl_setopt($ch, CURLOPT_HEADER, TRUE); 
}else{ 
curl_setopt($ch, CURLOPT_HEADER, false); 
} 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
if($lailu){ 
curl_setopt($ch, CURLOPT_REFERER, "http://d.web2.qq.com/proxy.html?v=20110331002&callback=1&id=2"); 
} 
if($post){ 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
} 
$content = curl_exec($ch); 
curl_close($ch); 
return $content; 
} 
function pregs($data,$do=0){ 
if($do=='login1'){ 
preg_match("/ptuiCB\('(.*)','(.*)','(.*)','(.*)','(.*)',\s'(.*)'\);/U", $data, $array); 
return $array; 
}elseif($do=='login2'){ 
preg_match("/ptuiCB\('(.*)','(.*)','(.*)','(.*)','(.*)',\s'(.*)'\);/U", $data, $array); 
return $array; 
}elseif($do=='cookie'){ 
preg_match_all('/Set-Cookie:(.*);/iU',$data,$array); 
return $array[1]; 
}elseif($do=='code'){ 
$start=stripos($data,'DOMAIN=qq.com;'); 
$code=substr($data,$start); 
$code=str_replace('DOMAIN=qq.com;','',$code); 
return trim($code); 
}elseif($do=='checkvc'){ 
preg_match("/ptui_checkVC\('(.*)','(.*)','(.*)'\);/",$data,$arr); 
return $arr; 
} 
} 
function cookies($arr,$do=0,$cookies=array()){ 
if($do){ 
foreach($arr as $str){ 
if($x=explode('=',$str) and trim($x[1])!=null){ 
$cookies[trim($x[0])]=trim($x[1]); 
} 
} 
return $cookies; 
}else{ 
foreach($arr as $str){ 
$cookie.=$str.';'; 
} 
return $cookie; 
} 
} 
function getmm($code,$do=0){ 
function hex2asc($str) { 
$str = join('',explode('\x',$str)); 
$len = strlen($str); 
for ($i=0;$i<$len;$i+=2) $data.=chr(hexdec(substr($str,$i,2))); 
return $data; 
} 
function hexchar2bin($str){ 
for ($i=0;$i
$arr.='\x'.substr($str,$i,2); 
} 
return hex2asc($arr); 
} 
function uin2hex($num) { 
$maxLength = 16; 
//$str=dechex(2799314776); 
$str=dechex($num); 
$hex=str_pad($str,$maxLength,0,STR_PAD_LEFT); 
return hexchar2bin($hex); 
} 
function dmd5($str){ 
return strtoupper(md5($str)); 
} 
if($do){
$pm=dmd5(hexchar2bin(dmd5($this->pwd)).uin2hex($this->qq)); 
}else{ 
//$pm='71CB84F9793F08F957D923095AB844EE'; 
$pm=dmd5(hexchar2bin(strtoupper($this->pwd)).uin2hex($this->qq)); 
} 
return dmd5($pm.strtoupper($code)); 
} 

}