Преглед на файлове

fix wechatauth qrcode auth. issue #75

dodgepudding преди 11 години
родител
ревизия
0329e27c62
променени са 2 файла, в които са добавени 68 реда и са изтрити 30 реда
  1. 34 15
      Thinkphp/Wechatauth.class.php
  2. 34 15
      wechatauth.class.php

+ 34 - 15
Thinkphp/Wechatauth.class.php

@@ -127,7 +127,7 @@ class Wechatauth
 		if (!$this->_logincode) return false;
 		$t = time().strval(mt_rand(100,999));
 
-			$url = 'https://login.weixin.qq.com/cgi-bin/mmwebwx-bin/login?uuid='.$this->_logincode.'&tip=1&_='.$t;
+			$url = 'https://login.weixin.qq.com/cgi-bin/mmwebwx-bin/login?uuid='.$this->_logincode.'&tip=0&_='.$t;
 			$send_snoopy = new Snoopy; 
 			$send_snoopy->referer = "https://wx.qq.com/";
 			$send_snoopy->fetch($url);
@@ -140,26 +140,33 @@ class Wechatauth
 					if ($status==201) $_SESSION['login_step'] = 1;
 					if ($status==200) {
 						preg_match("/ticket=([0-9a-z-_]+)&lang=zh_CN&scan=(\d+)/",$result,$matches);
+						preg_match("/window.redirect_uri=\"([^\"]+)\"/",$result,$matcheurl);
 						$this->log('step2:'.print_r($matches,true));
-						if (count($matches)>1) {
+						if (count($matcheurl)>1) {
 							$ticket = $matches[1];
 							$scan = $matches[2];
-							$loginurl = 'https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxnewloginpage?ticket='.$ticket.'&lang=zh_CN&scan='.$scan.'&fun=new';
+							//$loginurl = 'https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxnewloginpage?ticket='.$ticket.'&lang=zh_CN&scan='.$scan.'&fun=new';
+							$loginurl = str_replace("wx.qq.com", "wx2.qq.com", $matcheurl[1]).'&fun=old';
+							$urlpart = parse_url($loginurl);
 							$send_snoopy = new Snoopy; 
-							$send_snoopy->referer = "https://wx.qq.com/";
+							$send_snoopy->referer = "https://{$urlpart['host']}/cgi-bin/mmwebwx-bin/webwxindex?t=chat";
 							$send_snoopy->fetch($loginurl);
-							$this->log('step3:'.print_r($send_snoopy->headers,true));
+							$result = $send_snoopy->results;
+							$xml = simplexml_load_string($result);
+							if ($xml->ret=="0") $this->skey = $xml->skey;
 							foreach ($send_snoopy->headers as $key => $value) {
 								$value = trim($value);
 								if(strpos($value,'Set-Cookie: ') !== false){
 									$tmp = str_replace("Set-Cookie: ","",$value);
-									$tmp = str_replace("Path=/","",$tmp);
-									$tmp = str_replace("Domain=.qq.com; ","",$tmp);
-									$cookie.=$tmp;
+									$tmparray = explode(';', $tmp);
+									$item = trim($tmparray[0]);
+									$cookie.=$item.';';
 								}
 							}
 							$cookie .="Domain=.qq.com;";
 							$this->cookie = $cookie;
+							$this->log('step3:'.$loginurl.';cookie:'.$cookie.';respond:'.$result);
+							
 							$this->saveCookie($this->_cookiename,$this->cookie);
 						}
 					}
@@ -197,13 +204,25 @@ class Wechatauth
 		if (!$this->cookie) return false;
 		$t = time().strval(mt_rand(100,999));
 		$send_snoopy = new Snoopy; 
-		$submit = 'https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxinit?r='.$t;
+		$submit = 'https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxinit?r='.$t.'&skey='.urlencode($this->skey);
 		$send_snoopy->rawheaders['Cookie']= $this->cookie;
-		$send_snoopy->referer = "https://wx.qq.com/";
-		$send_snoopy->submit($submit,array());
+		$send_snoopy->referer = "https://wx2.qq.com/";
+		$citems = $this->get_login_cookie(true);
+		$post = array(
+			"BaseRequest"=>array(
+				array(
+					"Uin"=>$citems['wxuin'],
+					"Sid"=>$citems['wxsid'],
+					"Skey"=>$this->skey,
+					"DeviceID"=>''
+				)
+			)
+		);
+		$send_snoopy->submit($submit,json_encode($post));
 		$this->log('login_info:'.$send_snoopy->results);
 		$result = json_decode($send_snoopy->results,true);
 		if ($result['BaseResponse']['Ret']<0) return false;
+		$this->_login_user = $result['User'];
 		return $result;
 	}
 	
@@ -214,11 +233,11 @@ class Wechatauth
 	public function get_avatar($url) {
 		if (!$this->cookie) return false;
 		if (strpos($url, 'http')===false) {
-			$url = 'http://wx.qq.com'.$url;
+			$url = 'http://wx2.qq.com'.$url;
 		}
 		$send_snoopy = new Snoopy; 
 		$send_snoopy->rawheaders['Cookie']= $this->cookie;
-		$send_snoopy->referer = "https://wx.qq.com/";
+		$send_snoopy->referer = "https://wx2.qq.com/";
 		$send_snoopy->fetch($url);
 		$result = $send_snoopy->results;
 		if ($result) 
@@ -238,9 +257,9 @@ class Wechatauth
 		if (count($matches)>1) $sid = $matches[1];
 		$this->log('logout: uid='.$uid.';sid='.$sid);
 		$send_snoopy = new Snoopy; 
-		$submit = 'https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxlogout?redirect=1&type=1';
+		$submit = 'https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxlogout?redirect=1&type=1';
 		$send_snoopy->rawheaders['Cookie']= $this->cookie;
-		$send_snoopy->referer = "https://wx.qq.com/";
+		$send_snoopy->referer = "https://wx2.qq.com/";
 		$send_snoopy->submit($submit,array('uin'=>$uid,'sid'=>$sid));
 		$this->deleteCookie($this->_cookiename);
 		return true;

+ 34 - 15
wechatauth.class.php

@@ -19,6 +19,7 @@ include "snoopy.class.php";
 class Wechatauth
 {
 	private $cookie;
+	private $skey;
 	private $_cookiename;
 	private $_cookieexpired = 3600;
 	private $_account = 'test';
@@ -131,7 +132,7 @@ class Wechatauth
 		if (!$this->_logincode) return false;
 		$t = time().strval(mt_rand(100,999));
 
-			$url = 'https://login.weixin.qq.com/cgi-bin/mmwebwx-bin/login?uuid='.$this->_logincode.'&tip=1&_='.$t;
+			$url = 'https://login.weixin.qq.com/cgi-bin/mmwebwx-bin/login?uuid='.$this->_logincode.'&tip=0&_='.$t;
 			$send_snoopy = new Snoopy; 
 			$send_snoopy->referer = "https://wx.qq.com/";
 			$send_snoopy->fetch($url);
@@ -144,26 +145,33 @@ class Wechatauth
 					if ($status==201) $_SESSION['login_step'] = 1;
 					if ($status==200) {
 						preg_match("/ticket=([0-9a-z-_]+)&lang=zh_CN&scan=(\d+)/",$result,$matches);
+						preg_match("/window.redirect_uri=\"([^\"]+)\"/",$result,$matcheurl);
 						$this->log('step2:'.print_r($matches,true));
-						if (count($matches)>1) {
+						if (count($matcheurl)>1) {
 							$ticket = $matches[1];
 							$scan = $matches[2];
-							$loginurl = 'https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxnewloginpage?ticket='.$ticket.'&lang=zh_CN&scan='.$scan.'&fun=new';
+							//$loginurl = 'https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxnewloginpage?ticket='.$ticket.'&lang=zh_CN&scan='.$scan.'&fun=new';
+							$loginurl = str_replace("wx.qq.com", "wx2.qq.com", $matcheurl[1]).'&fun=old';
+							$urlpart = parse_url($loginurl);
 							$send_snoopy = new Snoopy; 
-							$send_snoopy->referer = "https://wx.qq.com/";
+							$send_snoopy->referer = "https://{$urlpart['host']}/cgi-bin/mmwebwx-bin/webwxindex?t=chat";
 							$send_snoopy->fetch($loginurl);
-							$this->log('step3:'.print_r($send_snoopy->headers,true));
+							$result = $send_snoopy->results;
+							$xml = simplexml_load_string($result);
+							if ($xml->ret=="0") $this->skey = $xml->skey;
 							foreach ($send_snoopy->headers as $key => $value) {
 								$value = trim($value);
 								if(strpos($value,'Set-Cookie: ') !== false){
 									$tmp = str_replace("Set-Cookie: ","",$value);
-									$tmp = str_replace("Path=/","",$tmp);
-									$tmp = str_replace("Domain=.qq.com; ","",$tmp);
-									$cookie.=$tmp;
+									$tmparray = explode(';', $tmp);
+									$item = trim($tmparray[0]);
+									$cookie.=$item.';';
 								}
 							}
 							$cookie .="Domain=.qq.com;";
 							$this->cookie = $cookie;
+							$this->log('step3:'.$loginurl.';cookie:'.$cookie.';respond:'.$result);
+							
 							$this->saveCookie($this->_cookiename,$this->cookie);
 						}
 					}
@@ -201,10 +209,21 @@ class Wechatauth
 		if (!$this->cookie) return false;
 		$t = time().strval(mt_rand(100,999));
 		$send_snoopy = new Snoopy; 
-		$submit = 'https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxinit?r='.$t;
+		$submit = 'https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxinit?r='.$t.'&skey='.urlencode($this->skey);
 		$send_snoopy->rawheaders['Cookie']= $this->cookie;
-		$send_snoopy->referer = "https://wx.qq.com/";
-		$send_snoopy->submit($submit,array());
+		$send_snoopy->referer = "https://wx2.qq.com/";
+		$citems = $this->get_login_cookie(true);
+		$post = array(
+			"BaseRequest"=>array(
+				array(
+					"Uin"=>$citems['wxuin'],
+					"Sid"=>$citems['wxsid'],
+					"Skey"=>$this->skey,
+					"DeviceID"=>''
+				)
+			)
+		);
+		$send_snoopy->submit($submit,json_encode($post));
 		$this->log('login_info:'.$send_snoopy->results);
 		$result = json_decode($send_snoopy->results,true);
 		if ($result['BaseResponse']['Ret']<0) return false;
@@ -219,11 +238,11 @@ class Wechatauth
 	public function get_avatar($url) {
 		if (!$this->cookie) return false;
 		if (strpos($url, 'http')===false) {
-			$url = 'http://wx.qq.com'.$url;
+			$url = 'http://wx2.qq.com'.$url;
 		}
 		$send_snoopy = new Snoopy; 
 		$send_snoopy->rawheaders['Cookie']= $this->cookie;
-		$send_snoopy->referer = "https://wx.qq.com/";
+		$send_snoopy->referer = "https://wx2.qq.com/";
 		$send_snoopy->fetch($url);
 		$result = $send_snoopy->results;
 		if ($result) 
@@ -243,9 +262,9 @@ class Wechatauth
 		if (count($matches)>1) $sid = $matches[1];
 		$this->log('logout: uid='.$uid.';sid='.$sid);
 		$send_snoopy = new Snoopy; 
-		$submit = 'https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxlogout?redirect=1&type=1';
+		$submit = 'https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxlogout?redirect=1&type=1';
 		$send_snoopy->rawheaders['Cookie']= $this->cookie;
-		$send_snoopy->referer = "https://wx.qq.com/";
+		$send_snoopy->referer = "https://wx2.qq.com/";
 		$send_snoopy->submit($submit,array('uin'=>$uid,'sid'=>$sid));
 		$this->deleteCookie($this->_cookiename);
 		return true;