<?php
require_once('../libs/cons.php');
require_once('../libs/functions.php');
function klsfcurl($url,$post=0,$referer=0,$cookie=0,$header=0,$ua=0,$nobaody=0,$iscookie=0,$isopenc=0){
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL,$url);
		$klsf[] = "Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"; 
		$klsf[] = "Accept-Encoding:gzip,deflate,sdch"; 
		$klsf[] = "Accept-Language:zh-CN,zh;q=0.8"; 
		$klsf[] = "Connection:keep-alive"; 
		$cookie_jar = dirname(__FILE__)."/qq.cookie";
		curl_setopt($ch, CURLOPT_HTTPHEADER, $klsf);
		if($post){
			curl_setopt($ch, CURLOPT_POST, 1);
			curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
		}
		if($header){
			curl_setopt($ch, CURLOPT_HEADER, TRUE);
		}
		if($cookie){
			curl_setopt($ch, CURLOPT_COOKIE, $cookie);
		}
		if($referer){
			if($referer==1){
				curl_setopt($ch, CURLOPT_REFERER, "http://m.qzone.com/infocenter?g_f=");
			}else{
				curl_setopt($ch, CURLOPT_REFERER, $referer);
			}
		}
		if($ua){
			curl_setopt($ch, CURLOPT_USERAGENT,$ua);
		}else{
			curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36');
		}
		if($nobaody){
			curl_setopt($ch, CURLOPT_NOBODY,1);//主要头部
			curl_setopt($ch, CURLOPT_FOLLOWLOCATION,0);
		}
		if($iscookie){
			curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);
			}
		if($isopenc){
			curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);
			}
		curl_setopt($ch, CURLOPT_ENCODING, "gzip");
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
		$ret = curl_exec($ch);
		curl_close($ch);
		return $ret;
	}
	$login_sig = get_login_sig();
function get_login_sig(){//获取登录sig
		$url="http://xui.ptlogin2.qq.com/cgi-bin/xlogin?proxy_url=http%3A//qzs.qq.com/qzone/v6/portal/proxy.html&daid=5&pt_qzone_sig=1&hide_title_bar=1&low_login=0&qlogin_auto_login=1&no_verifyimg=1&link_target=blank&appid=549000912&style=22&target=self&s_url=http%3A//qzs.qq.com/qzone/v5/loginsucc.html?para=izone&pt_qr_app=手机QQ空间&pt_qr_link=http%3A//z.qzone.com/download.html&self_regurl=http%3A//qzs.qq.com/qzone/v6/reg/index.html&pt_qr_help_link=http%3A//z.qzone.com/download.html";
		$ret = klsfcurl($url,0,1,0,1);
		preg_match('/pt_login_sig=(.*?);/',$ret,$skey);
		return $skey[1];
	}
function checkvc($uin,$login_sig)
{
	$url='http://check.ptlogin2.qq.com/check?regmaster=&pt_tea=1&pt_vcode=1&uin='.$uin.'&appid=549000912&js_ver=10132&js_type=1&login_sig='.$login_sig.'&u1=http%3A%2F%2Fqzs.qq.com%2Fqzone%2Fv5%2Floginsucc.html%3Fpara%3Dizone&r=0.3827968'.time();
	$data = klsfcurl($url);

	if (preg_match("/ptui_checkVC\('(.*?)'\);/", $data, $arr)) {
		$r = explode("','", $arr[1]);

		if ($r[0] == 0) {
			return array("0", $r[1], $r[3]);
		}
		else {
			return array("1");
		}
	}
	else {
		return array("2");
	}
}

function getsid($url,$do=0){
		$do++;
		if($ret = klsfcurl($url)){
			$ret = preg_replace('/([\x80-\xff]*)/i','',$ret);
			if(preg_match('/sid=(.{24})&/iU', $ret,$sid)){
				return $sid[1];
			}else{
				if($do<5){
					return getsid3($url,$do);
				}else{
					$this->sidcode=2;
					return false;
				}
			}
		}else{
			$this->sidcode=1;
			return false;
		}
	}


function get_pskey($uin,$ptsig,$cookie){
		$url = "http://user.qzone.qq.com/".$uin."?ptsig=".$ptsig;
		$ret = klsfcurl($url,0,0,$cookie,1);
		if(preg_match("/p_skey=(.*?);/", $ret, $arr)){
			$p_skey = $arr[1];
			return $p_skey;
		}else return false;
	}
	
function qqlogin($uin, $p, $vcode, $pt_verifysession,$login_sig)
{
	$v1 = 0;
	$url = "http://ptlogin2.qq.com/login?u=".$uin."&verifycode=".$vcode."&pt_vcode_v1=".$v1."&pt_verifysession_v1=".$pt_verifysession."&p=".$p."&pt_randsalt=0&u1=http%3A%2F%2Fqzs.qq.com%2Fqzone%2Fv5%2Floginsucc.html%3Fpara%3Dizone&ptredirect=0&h=1&t=1&g=1&from_ui=1&ptlang=2052&action=4-6-1441545995792&js_ver=10132&js_type=1&login_sig=".$login_sig."&pt_uistyle=32&aid=549000912&daid=5&pt_qzone_sig=1&";
	$sidurl = 'http://ptlogin2.qzone.com/login?verifycode='.$vcode.'&u='.$uin.'&p='.$p.'&pt_randsalt=0&ptlang=2052&low_login_enable=0&u1=http%3A%2F%2Fsqq2.3g.qq.com%2Fhtml5%2Fsqq2vip%2Findex.jsp&from_ui=1&fp=loginerroralert&device=2&aid=549000912&pt_ttype=1&daid=147&pt_3rd_aid=0&ptredirect=1&h=1&g=1&pt_uistyle=9&pt_vcode_v1='.$v1.'&pt_verifysession_v1='.$pt_verifysession.'&';
	$ret = klsfcurl($url,0,0,0,1,0);
	$sidret = klsfcurl($sidurl,0,0,0,1,"'Mozilla/5.0 (Linux; U; Android 4.0.4; es-mx; HTC_One_X Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0");
	if (preg_match("/ptuiCB\('(.*?)'\);/", $ret, $arr) && preg_match("/ptuiCB\('(.*?)'\);/", $sidret, $sidarr)) {
		$r = explode("','", $arr[1]);
		$sr=explode("','",$sidarr[1]);
		if($r[0]==0){
				$cookie='';
				preg_match_all('/Set-Cookie: (.*);/iU',$ret,$matchs);
				foreach ($matchs[1] as $val) {
					$cookie.=$val.'; ';
				}
				preg_match('/skey=@(.{9});/',$ret,$skey);
				preg_match('/ptsig=(.{44})/',$r[2],$ptsig);
				$p_skey=get_pskey($uin,$ptsig[1],$cookie);
				$data=klsfcurl($sr[2],0,0,0,1,"'Mozilla/5.0 (Linux; U; Android 4.0.4; es-mx; HTC_One_X Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0");
				if($data) {
					preg_match("/p_skey=(.*?);/", $data, $matchs);
					$p_skey2 = $matchs[1];
					preg_match("/Location: (.*?)\r\n/iU", $data, $matchs);
					$sid=getsid($matchs[1]);
				}
			$array["uin"] = $uin;
			$array["skey"] = $skey[1];
			$array['p_skey']=$p_skey;
			$array['sid'] = $sid;
			$array['ptsig']=$ptsig[1];
			$array['p_skey2'] =$p_skey2;

			return $array;
		}
		else if ($r[0] == 4) {
			return 0;
		}
		else if ($r[0] == 3) {
			return 0;
		}
		else if ($r[0] == 19) {
			return 0;
		}
		else {
			return 0;
		}
	}
	else {
		return 0;
	}
}

$uin = $row["qq"];
$pwd = decrypt($row["pwd"],$syskey);
@mysql_query("UPDATE {$tableqz}qqs SET lastauto='$now' WHERE qid='{$qid}'");
$check = checkvc($uin,$login_sig);

if ($check[0] == 0) {
	$vcode = $check[1];
	$p = klsfcurl('http://qqapp.aliapp.com/?uin='.$uin.'&pwd='.strtoupper($pwd).'&vcode='.strtoupper($vcode));
	$now=date("Y-m-d-H:i:s");
	if ($arr = qqlogin($uin, $p, $vcode, $check[2],$login_sig)) {
		
		
		if ($p_skey = $arr["p_skey"]) {
			$sql = "p_skey='$p_skey',";
		
		}

		@mysql_query("UPDATE {$tableqz}qqs SET {$sql}skey='@{$arr["skey"]}',sid='{$arr["sid"]}',p_skey2='{$arr["p_skey2"]}',ptsig='{$arr["ptsig"]}',sidzt='0',skeyzt='0' WHERE qid='{$qid}'");
		exit($uin."更新成功!");

	}
	else {
		echo $uin."更新失败!";
	}
}
else {
	echo $uin."需要验证码，无法自动更新!";
}

?>
