浏览代码

fix Wechatext auth unavailable, fixed issue #5

dodgepudding 12 年之前
父节点
当前提交
c731915f8c
共有 4 个文件被更改,包括 49 次插入42 次删除
  1. 4 4
      Thinkphp/Snoopy.class.php
  2. 15 12
      Thinkphp/Wechatext.class.php
  3. 4 4
      snoopy.class.php
  4. 26 22
      wechatext.class.php

+ 4 - 4
Thinkphp/Snoopy.class.php

@@ -223,7 +223,7 @@ class Snoopy
 					if($this->maxredirs > $this->_redirectdepth)
 					{
 						// only follow redirect if it's on this site, or offsiteok is true
-						if(preg_match("|^http://".preg_quote($this->host)."|i",$this->_redirectaddr) || $this->offsiteok)
+						if(preg_match("|^https://".preg_quote($this->host)."|i",$this->_redirectaddr) || $this->offsiteok)
 						{
 							/* follow the redirect */
 							$this->_redirectdepth++;
@@ -386,7 +386,7 @@ class Snoopy
 							$this->_redirectaddr = $this->_expandlinks($this->_redirectaddr,$URI_PARTS["scheme"]."://".$URI_PARTS["host"]);
 
 						// only follow redirect if it's on this site, or offsiteok is true
-						if(preg_match("|^http://".preg_quote($this->host)."|i",$this->_redirectaddr) || $this->offsiteok)
+						if(preg_match("|^https://".preg_quote($this->host)."|i",$this->_redirectaddr) || $this->offsiteok)
 						{
 							/* follow the redirect */
 							$this->_redirectdepth++;
@@ -1007,7 +1007,7 @@ class Snoopy
 			}
 			$data = curl_exec($ch);
 			if ($data === false) {
-				$this->error = "Error: Curl error  ".$curl_error($ch);
+				$this->error = "Error: Curl error  ".curl_error($ch);
 					return false;
 			}
 			$parts = explode("\r\n\r\n",$data);
@@ -1056,7 +1056,7 @@ class Snoopy
 				if(!preg_match("|\:\/\/|",$matches[2]))
 				{
 					// no host in the path, so prepend
-					$this->_redirectaddr = $URI_PARTS["scheme"]."://".$this->host.":".$this->port;
+					$this->_redirectaddr = $URI_PARTS["scheme"]."://".$this->host;
 					// eliminate double slash
 					if(!preg_match("|^/|",$matches[2]))
 						$this->_redirectaddr .= "/".$matches[2];

+ 15 - 12
Thinkphp/Wechatext.class.php

@@ -147,7 +147,7 @@ class Wechatext
 		$send_snoopy = new Snoopy; 
 		$send_snoopy->rawheaders['Cookie']= $this->cookie;
 		$send_snoopy->referer = "http://mp.weixin.qq.com/cgi-bin/getmessage?t=wxm-message&lang=zh_CN&count=50&token=".$this->_token;
-		$submit = "http://mp.weixin.qq.com/cgi-bin/getcontactinfo?t=ajax-getcontactinfo&lang=zh_CN&fakeid=".$id;
+		$submit = "https://mp.weixin.qq.com/cgi-bin/getcontactinfo?t=ajax-getcontactinfo&lang=zh_CN&fakeid=".$id;
 		$post = array('ajax'=>1,'token'=>$this->_token);
 		$send_snoopy->submit($submit,$post);
 		$this->log($send_snoopy->results);
@@ -173,20 +173,21 @@ class Wechatext
 		$this->log($snoopy->headers);
 		foreach ($snoopy->headers as $key => $value) {
 			$value = trim($value);
-			if(strpos($value,'Set-Cookie: ') || strpos($value,'Set-Cookie: ')===0){
-				$tmp = str_replace("Set-Cookie: ","",$value);
-				$tmp = str_replace("Path=/","",$tmp);
-				$cookie.=$tmp;
-			}
+			if(preg_match('/^set-cookie:[\s]+([^=]+)=([^;]+)/i', $value,$match))
+				$cookie .=$match[1].'='.$match[2].'; ';
 		}
 		if ($cookie) {
 			$send_snoopy = new Snoopy; 
 			$send_snoopy->rawheaders['Cookie']= $cookie;
-			$url = "http://mp.weixin.qq.com/cgi-bin/indexpage?t=wxm-index&lang=zh_CN";
+			$send_snoopy->maxredirs = 0;
+			$url = "https://mp.weixin.qq.com/cgi-bin/indexpage?t=wxm-index&lang=zh_CN";
 			$send_snoopy->fetch($url);
-			preg_match("/token:\s+\"(\d+)\"/i",$send_snoopy->results,$matches);
+			$header = implode(',',$send_snoopy->headers);
+			$this->log('header:'.print_r($send_snoopy->headers,true));
+			preg_match("/token=(\d+)/i",$header,$matches);
 			if($matches){
 				$this->_token = $matches[1];
+				$this->log('token:'.$this->_token);
 			}
 		}
 		$this->saveCookie($this->_cookiename,$cookie);
@@ -212,11 +213,13 @@ class Wechatext
 		$data = S($filename);
 		if($data){
 			$send_snoopy = new Snoopy; 
-			$send_snoopy->rawheaders['Cookie']= $data;
-			$url = "http://mp.weixin.qq.com/cgi-bin/indexpage?t=wxm-index&lang=zh_CN";
+			$send_snoopy->rawheaders['Cookie']= $cookie;
+			$send_snoopy->maxredirs = 0;
+			$url = "https://mp.weixin.qq.com/cgi-bin/indexpage?t=wxm-index&lang=zh_CN";
 			$send_snoopy->fetch($url);
-			preg_match("/token:\s+\"(\d+)\"/i",$send_snoopy->results,$matches);
-			$this->log(print_r($matches,true));
+			$header = implode(',',$send_snoopy->headers);
+			$this->log('header:'.print_r($send_snoopy->headers,true));
+			preg_match("/token=(\d+)/i",$header,$matches);
 			if(empty($matches)){
 				return $this->login();
 			}else{

+ 4 - 4
snoopy.class.php

@@ -223,7 +223,7 @@ class Snoopy
 					if($this->maxredirs > $this->_redirectdepth)
 					{
 						// only follow redirect if it's on this site, or offsiteok is true
-						if(preg_match("|^http://".preg_quote($this->host)."|i",$this->_redirectaddr) || $this->offsiteok)
+						if(preg_match("|^https://".preg_quote($this->host)."|i",$this->_redirectaddr) || $this->offsiteok)
 						{
 							/* follow the redirect */
 							$this->_redirectdepth++;
@@ -386,7 +386,7 @@ class Snoopy
 							$this->_redirectaddr = $this->_expandlinks($this->_redirectaddr,$URI_PARTS["scheme"]."://".$URI_PARTS["host"]);
 
 						// only follow redirect if it's on this site, or offsiteok is true
-						if(preg_match("|^http://".preg_quote($this->host)."|i",$this->_redirectaddr) || $this->offsiteok)
+						if(preg_match("|^https://".preg_quote($this->host)."|i",$this->_redirectaddr) || $this->offsiteok)
 						{
 							/* follow the redirect */
 							$this->_redirectdepth++;
@@ -1007,7 +1007,7 @@ class Snoopy
 			}
 			$data = curl_exec($ch);
 			if ($data === false) {
-				$this->error = "Error: Curl error  ".$curl_error($ch);
+				$this->error = "Error: Curl error  ".curl_error($ch);
 					return false;
 			}
 			$parts = explode("\r\n\r\n",$data);
@@ -1056,7 +1056,7 @@ class Snoopy
 				if(!preg_match("|\:\/\/|",$matches[2]))
 				{
 					// no host in the path, so prepend
-					$this->_redirectaddr = $URI_PARTS["scheme"]."://".$this->host.":".$this->port;
+					$this->_redirectaddr = $URI_PARTS["scheme"]."://".$this->host;
 					// eliminate double slash
 					if(!preg_match("|^/|",$matches[2]))
 						$this->_redirectaddr .= "/".$matches[2];

+ 26 - 22
wechatext.class.php

@@ -50,9 +50,9 @@ class Wechatext
 		$post['token'] = $this->_token;
 		$post['content'] = $content;
 		$post['ajax'] = 1;
-        $send_snoopy->referer = "http://mp.weixin.qq.com/cgi-bin/singlemsgpage?fromfakeid={$id}&msgid=&source=&count=20&t=wxm-singlechat&lang=zh_CN";
+        $send_snoopy->referer = "https://mp.weixin.qq.com/cgi-bin/singlemsgpage?fromfakeid={$id}&msgid=&source=&count=20&t=wxm-singlechat&lang=zh_CN";
 		$send_snoopy->rawheaders['Cookie']= $this->cookie;
-		$submit = "http://mp.weixin.qq.com/cgi-bin/singlesend?t=ajax-response";
+		$submit = "https://mp.weixin.qq.com/cgi-bin/singlesend?t=ajax-response";
 		$send_snoopy->submit($submit,$post);
 		$this->log($send_snoopy->results);
 		return $send_snoopy->results;
@@ -75,9 +75,9 @@ class Wechatext
 			$post['content'] = $content;
 			$post['token'] = $this->_token;
 			$post['ajax'] = 1;
-            $send_snoopy->referer = "http://mp.weixin.qq.com/cgi-bin/singlemsgpage?fromfakeid={$value}&msgid=&source=&count=20&t=wxm-singlechat&lang=zh_CN";
+            $send_snoopy->referer = "https://mp.weixin.qq.com/cgi-bin/singlemsgpage?fromfakeid={$value}&msgid=&source=&count=20&t=wxm-singlechat&lang=zh_CN";
 			$send_snoopy->rawheaders['Cookie']= $this->cookie;
-			$submit = "http://mp.weixin.qq.com/cgi-bin/singlesend?t=ajax-response";
+			$submit = "https://mp.weixin.qq.com/cgi-bin/singlesend?t=ajax-response";
 			$post['tofakeid'] = $value;
 			$send_snoopy->submit($submit,$post);
 			$tmp = $send_snoopy->results;
@@ -99,12 +99,12 @@ class Wechatext
 	 */
 	public function sendNews($account,$title,$summary,$content,$pic,$srcurl='') {
 		$send_snoopy = new Snoopy;
-		$send_snoopy->referer = "http://mp.weixin.qq.com/cgi-bin/indexpage?t=wxm-upload&lang=zh_CN&type=2&formId=1";
+		$send_snoopy->referer = "https://mp.weixin.qq.com/cgi-bin/indexpage?t=wxm-upload&lang=zh_CN&type=2&formId=1";
 		$post = array('formId'=>'');
 		$postfile = array('uploadfile'=>$pic);
 		$send_snoopy->rawheaders['Cookie']= $this->cookie;
 		$send_snoopy->set_submit_multipart();
-		$submit = "http://mp.weixin.qq.com/cgi-bin/uploadmaterial?cgi=uploadmaterial&type=2&t=iframe-uploadfile&lang=zh_CN&formId=1";
+		$submit = "https://mp.weixin.qq.com/cgi-bin/uploadmaterial?cgi=uploadmaterial&type=2&t=iframe-uploadfile&lang=zh_CN&formId=1";
 		$send_snoopy->submit($submit,$post,$postfile);
 		$tmp = $send_snoopy->results;
 		$this->log($tmp);
@@ -112,10 +112,10 @@ class Wechatext
 		if (isset($matches[1])) {
 			$photoid = $matches[1];
 			$send_snoopy = new Snoopy;
-			$submit = "http://mp.weixin.qq.com/cgi-bin/operate_appmsg?sub=preview&t=ajax-appmsg-preview";
+			$submit = "https://mp.weixin.qq.com/cgi-bin/operate_appmsg?sub=preview&t=ajax-appmsg-preview";
 			$send_snoopy->set_submit_normal();
 			$send_snoopy->rawheaders['Cookie']= $this->cookie;
-			$send_snoopy->referer = 'http://mp.weixin.qq.com/cgi-bin/operate_appmsg?sub=edit&t=wxm-appmsgs-edit-new&type=10&subtype=3&lang=zh_CN';
+			$send_snoopy->referer = 'https://mp.weixin.qq.com/cgi-bin/operate_appmsg?sub=edit&t=wxm-appmsgs-edit-new&type=10&subtype=3&lang=zh_CN';
 			$post = array(
 					'AppMsgId'=>'',
 					'ajax'=>1,
@@ -147,8 +147,8 @@ class Wechatext
 	{
 		$send_snoopy = new Snoopy; 
 		$send_snoopy->rawheaders['Cookie']= $this->cookie;
-		$send_snoopy->referer = "http://mp.weixin.qq.com/cgi-bin/getmessage?t=wxm-message&lang=zh_CN&count=50&token=".$this->_token;
-		$submit = "http://mp.weixin.qq.com/cgi-bin/getcontactinfo?t=ajax-getcontactinfo&lang=zh_CN&fakeid=".$id;
+		$send_snoopy->referer = "https://mp.weixin.qq.com/cgi-bin/getmessage?t=wxm-message&lang=zh_CN&count=50&token=".$this->_token;
+		$submit = "https://mp.weixin.qq.com/cgi-bin/getcontactinfo?t=ajax-getcontactinfo&lang=zh_CN&fakeid=".$id;
 		$post = array('ajax'=>1,'token'=>$this->_token);
 		$send_snoopy->submit($submit,$post);
 		$this->log($send_snoopy->results);
@@ -165,7 +165,7 @@ class Wechatext
 	 */
 	public function login(){
 		$snoopy = new Snoopy; 
-		$submit = "http://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN";
+		$submit = "https://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN";
 		$post["username"] = $this->_account;
 		$post["pwd"] = md5($this->_password);
 		$post["f"] = "json";
@@ -174,20 +174,21 @@ class Wechatext
 		$this->log($snoopy->headers);
 		foreach ($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);
-				$cookie.=$tmp;
-			}
+			if(preg_match('/^set-cookie:[\s]+([^=]+)=([^;]+)/i', $value,$match))
+				$cookie .=$match[1].'='.$match[2].'; ';
 		}
 		if ($cookie) {
 			$send_snoopy = new Snoopy; 
 			$send_snoopy->rawheaders['Cookie']= $cookie;
-			$url = "http://mp.weixin.qq.com/cgi-bin/indexpage?t=wxm-index&lang=zh_CN";
+			$send_snoopy->maxredirs = 0;
+			$url = "https://mp.weixin.qq.com/cgi-bin/indexpage?t=wxm-index&lang=zh_CN";
 			$send_snoopy->fetch($url);
-			preg_match("/token:\s+\"(\d+)\"/i",$send_snoopy->results,$matches);
+			$header = implode(',',$send_snoopy->headers);
+			$this->log('header:'.print_r($send_snoopy->headers,true));
+			preg_match("/token=(\d+)/i",$header,$matches);
 			if($matches){
 				$this->_token = $matches[1];
+				$this->log('token:'.$this->_token);
 			}
 		}
 		$this->saveCookie($this->_cookiename,$cookie);
@@ -221,14 +222,17 @@ class Wechatext
 		if($data){
 			$send_snoopy = new Snoopy; 
 			$send_snoopy->rawheaders['Cookie']= $data;
-			$url = "http://mp.weixin.qq.com/cgi-bin/indexpage?t=wxm-index&lang=zh_CN";
+			$send_snoopy->maxredirs = 0;
+			$url = "https://mp.weixin.qq.com/cgi-bin/indexpage?t=wxm-index&lang=zh_CN";
 			$send_snoopy->fetch($url);
-			preg_match("/token:\s+\"(\d+)\"/i",$send_snoopy->results,$matches);
-			$this->log(print_r($matches,true));
+			$header = implode(',',$send_snoopy->headers);
+			$this->log('header:'.print_r($send_snoopy->headers,true));
+			preg_match("/token=(\d+)/i",$header,$matches);
 			if(empty($matches)){
 				return $this->login();
 			}else{
 				$this->_token = $matches[1];
+				$this->log('token:'.$this->_token);
 				return $data;
 			}
 		}else{
@@ -244,7 +248,7 @@ class Wechatext
 	{
 		$send_snoopy = new Snoopy; 
 		$post = array('ajax'=>1,'token'=>$this->_token);
-		$submit = "http://mp.weixin.qq.com/cgi-bin/getregions?id=1017&t=ajax-getregions&lang=zh_CN";
+		$submit = "https://mp.weixin.qq.com/cgi-bin/getregions?id=1017&t=ajax-getregions&lang=zh_CN";
 		$send_snoopy->rawheaders['Cookie']= $this->cookie;
 		$send_snoopy->submit($submit,$post);
 		$result = $send_snoopy->results;