<?php
class SET_MQQ
{

    function __construct($uin, $pwd)
    {
        $this->uin = $uin;
        $this->pwd = $pwd;
        $this->host= '119.147.14.253';
        $this->port= '14000';
    }



    /*(1).登录QQ*/
    function Login($code = 10)
    {
        $pack = 'VER=1.4&CON=1&CMD=Login&SEQ=' . rand(1,9000) . '&UIN='.$this->uin.'&PS=' . $this->pwd . '&M5=1&LG=0&ST='.$code.'&LC=812822641C978097&GD=5MWX2PF3FOVGTP6B&CKE=';
        $return =$this->request_by_curl($pack);
        $result = $return;
        if(strpos($return,"CMD=VERIFYCODE")){
        $vc=explode("&VC=",$return);
        $return=json_encode(array('status'=>'vc','res'=>$vc[1]));
        }elseif(strpos($return,"Password error")){
        $return=json_encode(array('status'=>'invaid password'));
        }elseif(strpos($return,"&RS=0")){
            $return=json_encode(array("status"=>"ok"));
        }else{
            $return=json_encode(array("status"=>"error",'qq'=>$this->uin,'result'=>$result));
        }
    //setlog('QQ:'.$this->uin.'|'.json_encode($return));
    return $return;
    }
    /*验证码处理*/
    function Verifycode($code)
    {
        $pack = 'VER=1.4&CON=1&CMD=VERIFYCODE&SEQ=' . rand(1,9000) . '&UIN='.$this->uin.'&SID=&XP=C4CA4238A0B92382&SC=2&VC='.$code;
        $return =$this->request_by_curl($pack);
        if(strpos($return,"&VC=")){
        $vc=explode("&VC=",$return);
        $return=json_encode(array('status'=>'vc','res'=>$vc[1]));
        }else{$return=json_encode(array('status'=>'ok'));}
    //setlog('QQ:'.$this->uin.'|'.json_encode($return));
    return $return;
    }
    /*更改在线状态*/
    function ChangeStat($code)
    {
        //$this->Login($code);
        $pack = 'VER=1.4&CMD=Change_Stat&SEQ=' . rand(1,9000) . '&UIN='.$this->uin.'&ST='.$code;
        $return =$this->request_by_curl($pack);
        setlog('QQ:'.$this->uin.'|'.$return);
        if(strpos($return,"&RES=20")){
        $return=json_encode(array('status'=>'offline'));
        }else{$return=json_encode(array('status'=>'ok'));}
    return $return;
    }
    function ChangeStat_BUG($code)    //一个小小的BUG，发挥了无限潜力
    {
        //$this->Login($code);
        $pack = 'VER=1.4&CMD=Change_Stat2&SEQ=' . rand(1,9000) . '&UIN='.$this->uin.'&ST='.$code;
        $return =$this->request_by_curl($pack);
        setlog('QQ:'.$this->uin.'|'.$return);
        if(strpos($return,"&RES=20")){
        $return=json_encode(array('status'=>'offline'));
        }else{$return=json_encode(array('status'=>'ok'));}
    return $return;
    }
    /*(2).发送聊天消息*/
    function SendMsg($toperson, $msg)
    {
        $this->Login();
        $msg=str_replace("#br#","%0D%0A",$msg);
        $msg2= str_replace("+"," ",$msg). '%0D%0A'.$empty.'http://'.$_SERVER['HTTP_HOST'].'%0D%0A                               Powered by %E5%BF%83%E6%8C%82Q%28iQQol%29';
        $pack = 'VER=1.4&CON=0&CMD=CLTMSG&SEQ=' . rand(1,9000) . '&UIN='.$this->uin.'&SID=&XP=B58304217ADD0489&UN='.$toperson.'&MG='.$msg2;
        $return =$this->request_by_curl($pack);
        //setlog('QQ:'.$this->uin.'|'.json_encode($return));
    }

    /*(3).获得即时聊天消息*/
    function GetMsg($_reply)
    {
        $this->Login();
        $pack = 'VER=1.4&CON=0&CMD=GetMsgEx&SEQ='. rand(1,9000).'&UIN=' . $this -> uin . '&SID=&XP=29E41F6186ED43F8';
        $return =$this->request_by_curl($pack);
        $message=explode('&UN=',$return);
        $unandmsg=$message[1];
        $need=explode('&MG=',$unandmsg);
        $need_MTstr=explode('&MT=',$message[0]);
        $need_mt=$need_MTstr[1];
        $need_un=$need[0];
        $need_mg=$need[1];
        $un=explode(',',$need_un);
        $mg=explode(',',$need_mg);
        $mt=explode(',',$need_mt);
            for($i=0; $i<count($un); $i++){
                if($un[$i]!='' and $mt[$i]!='99'){
                    $n .= urlencode($this->GetInfo($un[$i],'NK','1',rand(1,9000)));//QQ昵称
                    $n .= '('.$un[$i];$n .= ')%0D%0A';//收到消息来自的QQ号码
                    $n .= urlencode($mg[$i]).'%0D%0A';//收到的消息的文本内容
                        $this->SendMsg($un[$i],$_reply.SERVICE_REPLY);
                }
                else {
                    continue;
                }
            }
        return $n;
    }

    /*(4).查看好友信息*/
    function GetInfo($toperson, $set, $lv)
    {
        $this->Login();
        $pack = 'VER=1.4&CMD=GetInfo&SEQ='.rand(1,9000).'&UIN=' . $this -> uin . '&LV='.$lv.'&UN='.$toperson;//配置信息详细程度LV
        $return =$this->request_by_curl($pack);
        $message=explode('&'.$set.'=',$return);
        $need_arr=explode('&',$message[1]);
        $need=$need_arr[0];
        //print_r($message);
        //print_r($message["$set"]);
        return $need;
    }

    /*(5).查看好友列表*/
    function GetList()
    {
        $this->Login();
        $pack = 'VER=1.4&CMD=List&SEQ='.rand(1,9000).'&UIN='.$this->uin;//配置信息详细程度LV
        $return =$this->request_by_curl($pack);
        $need = explode('&UN=',$return);
        return $need[1];
    }

    function request_by_curl($fields)
    {
  $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "http://{$this->host}:{$this->port}");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
        curl_setopt($ch, CURLOPT_PROXY, PROXYIP);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_USERAGENT, "iQQol Client");
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }
/*function request_by_curl($pack)
{
   /*$url = "http://{$this->host}:{$this->port}";
   $info = parse_url($url);
   $fp = pfsockopen("http://{$this->host}", $this->port, $errno, $errstr, 3);
   $head = "POST ".$info['path']."?".$info["query"]." HTTP/1.0rn";
   $head .= "Host: ".$info['host']."rn";
   $head .= "Referer: http://".$info['host'].$info['path']."rn";
   $head .= "Content-type: application/x-www-form-urlencodedrn";
   $head .= "Content-Length: ".strlen(trim($query))."rn";
   $head .= "rn";
   $head .= trim($query);
   $write = fputs($fp, $head);
   while (!feof($fp))
   {
    $line = fread($fp,4096);
    return $line;
   }
   return false;*//*
   $fp = pfsockopen("58.60.12.177", 14000, $errno, $errstr, 10);
        if (!$fp)
            return "Socket Denied.";
        fputs($fp, sprintf("POST %s%s%s HTTP/1.0\r\n", $url['path'], $url['query'] ? "?" : "", $pack));
        fputs($fp, "Host:mconn.tencent.com\r\n");
        fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
        fputs($fp, "Content-length: " . strlen($pack) . "\r\n");
        fputs($fp, "Connection: close\r\n\r\n");
        fputs($fp, $pack);
        $info=fread($fp,1024);
        return $info;
}*/
    function HexToText($hex)
    {
        $str = "";
        for($i = 0; $i < strlen($hex); $i += 2)
        {
            $str .=  chr(hexdec($hex[$i].$hex[$i+1]));
        }
        return $str;
    }
}
class SET_3GQQ
{
    function __construct($qq)
    {
        global $con,$table;
        $this->qq = $qq;
        $qqinfo_str = mysql_query("SELECT * FROM {$table}online WHERE qq=".$qq,$con);
        //mysql_query("INSERT INTO {$table}log (qq,log,time) VALUES ('{$qq}','SELECT * FROM {$table}online WHERE qq={$qq}','".date('Y-m-d')."')");
        $qqinfo = mysql_fetch_array($qqinfo_str);
        $this->info=$qqinfo;
    }
    function Login()
    {
        $str = $this->info;
        $pack = "http://q16.3g.qq.com/g/s?sid=".$str['sid']."&aid=chgStatus&s=".$str['login_status'];
        $return = $this->request_by_curl($pack);
        $return_str1 = explode('ontimer="',$return);
        $return_str2 = explode('">',$return_str1[1]);
        $return_url = $return_str2[0];
        $return = $this->request_by_curl($return_url);
        return $return;
    }
    function Logout()
    {
        $str = $this->info;
        $pack = "http://q16.3g.qq.com/g/s?sid=".$str['sid']."&aid=chgStatus&s=20";
        $return = $this->request_by_curl($pack);
        $return_str1 = explode('ontimer="',$return);
        $return_str2 = explode('">',$return_str1[1]);
        $return_url = $return_str2[0];
        $return = $this->request_by_curl($return_url);
        return $return;
    }
    function request_by_curl($fields)
    {
  $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $fields);
        curl_setopt($ch, CURLOPT_PROXY, PROXYIP);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_USERAGENT, "iQQol Client");
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }
}

function qq_login($qqno, $qqpw)
{
    $cookie = dirname(__FILE__) . '/cookie.txt';
    $post   = array(
        'login_url' => 'http://pt.3g.qq.com/s?sid=ATAll43N7ZULRQ5V8zdfojol&amp;aid=nLogin',
        'q_from' => '',
        'loginTitle' => 'login',
        'bid' => '0',
        'qq' => $qqno,
        'pwd' => $qqpw,
        'loginType' => '1',
        'loginsubmit' => 'login'
    );
    $curl   = curl_init('http://pt.3g.qq.com/handleLogin?aid=nLoginHandle&amp;sid=ATAll43N7ZULRQ5V8zdfojol');
    curl_setopt($curl, CURLOPT_HEADER, 0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie); // ?Cookie
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));
    global $result;
    $result = curl_exec($curl);
    curl_close($curl);
    return $result;
}
function add_qq($qqno, $sid, $qpassword, $login_status, $reply, $noticeqq, $runtime) {
    global $con, $User_username, $User_uid, $style, $table;
    if (mysql_query("INSERT INTO {$table}online (qid,uid,username,qq,pass,sid,status,login_status,reply,noticeqq,runtime) VALUES (NULL,'$User_uid','$User_username','$qqno','".md5($qpassword)."','$sid','1','$login_status','$reply','$noticeqq','$runtime')")) {
        $qqresult = mysql_query("SELECT * FROM {$table}online
WHERE qq='$qqno'");
        while ($r = mysql_fetch_array($qqresult, MYSQL_ASSOC)) {
            $msg = "您的QQ号码{$r['qq']}添加成功，欢迎加入iQQol！#br##br#在开始享受您的挂机服务之前，请务必详细阅读以下内容：#br#1.您的QQ在挂机期间可能会遇到腾讯检测出现验证码而导致掉线，我们会通过邮件形式和大号提醒方式向您推送免费提醒，请及时注意并回到网站管理#br#2.如果您在挂机期间修改密码，您的记录将会从本站永久删除，我们会通过邮件形式以及大号提醒方式推送提醒#br#3.您可以在本站查看您的个人资料页面或用户广场#br#4.如果我们的站点附有论坛，那么您的账号可以在论坛同步登陆#br#5.在个人主页发布您的消息时，请注意您的言行！";
            $head = "[{$r['qq']}]添加成功";
            $mi=uc_get_user($User_username);
            mysql_query("INSERT INTO {$table}query (id,user,message,qq,email,noticeqq,head) VALUES (NULL,'$User_username','$msg','{$r['qq']}','{$mi['2']}','$noticeqq','$head ')");
        }
        $state = "挂Q记录已经成功添加。QQ:$qqno SID:$sid <br>自动回复语句:{$reply} <br>接收提醒QQ：$noticeqq";
        $style->assign("state", $state);
        $style->display('main/qqinsert.html');
    }
}
?>