Browse Source

分析公众账号基本信息(昵称,LOGO,二维码,账号类型,appid,appsecret)

hackingangle 10 years ago
parent
commit
3291eb6d49
2 changed files with 303 additions and 0 deletions
  1. 172 0
      io.class.php
  2. 131 0
      wechatext.class.php

+ 172 - 0
io.class.php

@@ -0,0 +1,172 @@
+<?php
+/**
+ * IO 处理
+ *
+ * @package    classes
+ * @author     zhongyy <regulusyun@gmail.com>
+ * @copyright  Copyright (c) 2010-10-5
+ * @license    http://www.gnu.org/licenses/gpl.html     GPL 3
+ */
+abstract class Io
+{
+
+    /**
+     * 修正路径分隔符为操作系统的正确形式
+     *
+     * @param  string  $path
+     * @return string
+     */
+    public static function strip($path)
+    {
+        return preg_replace('/[\\\\\/]+/', DIRECTORY_SEPARATOR, (string) $path);
+    }
+
+    /**
+     * 创建目录(递归创建)
+     *
+     * @param  string  $dir   目录路径
+     * @param  int     $mode  访问权限
+     * @return string|FALSE
+     */
+    public static function mkdir($dir, $mode = 0777)
+    {
+        $dir = Io::strip($dir);
+
+        if ( ! is_dir($dir))
+        {
+            $mk = @mkdir($dir, 0777, TRUE);
+            if ($mk === FALSE)
+            {
+                return FALSE;
+            }
+        }
+
+        return $dir;
+    }
+
+    /**
+     * 删除目录(递归删除)
+     *
+     * @param  string  $dir
+     * @return bool
+     */
+    public static function rmdir($dir)
+    {
+        $dir = Io::strip($dir);
+
+        if (is_dir($dir))
+        {
+            $dirs = Io::scan($dir);
+            if (is_array($dirs))
+            {
+                $flag = TRUE;
+                foreach ($dirs as $file)
+                {
+                    $file = "$dir/$file";
+                    if (is_dir($file))
+                    {
+                        $flag = Io::rmdir($file);
+                    }
+                    else
+                    {
+                        $flag = @unlink($file);
+                    }
+                    if ($flag == FALSE)
+                    {
+                        break;
+                    }
+                }
+            }
+            return @rmdir($dir);
+        }
+
+        return FALSE;
+    }
+
+    /**
+     * 扫描目录下所有的文件/目录
+     *
+     * @param  string  $dir     指定的目录
+     * @param  array   $ignore  需要跳过的文件/目录
+     * @return array|FALSE
+     */
+    public static function scan($dir, array $ignore = array('.svn'))
+    {
+        $dir = Io::strip($dir);
+
+        if (is_dir($dir))
+        {
+            $dirs = scandir($dir);
+            foreach ($dirs as $k => $v)
+            {
+                if ($v == '.' OR $v == '..' OR in_array($v, $ignore))
+                {
+                    unset($dirs[$k]);
+                }
+            }
+            return $dirs;
+        }
+
+        return FALSE;
+    }
+
+    /**
+     * 复制文件/目录
+     *
+     * @param  string   $from      源文件/目录
+     * @param  string   $to        目标文件/目录
+     * @param  boolean  $override  是否覆盖
+     * @param  array    $ignore    需要跳过的文件/目录
+     * @return boolean
+     */
+    public static function copy($from, $to, $override = TRUE, array $ignore = array('.svn'))
+    {
+        $from = Io::strip($from);
+        $to   = Io::strip($to);
+
+        if (is_file($from))
+        {
+            Io::mkdir(dirname($to));
+            if (is_file($to) AND ! $override) // 已经存在且不允许覆盖
+            {
+                return TRUE;
+            }
+            else
+            {
+                return @copy($from, $to);
+            }
+
+        }
+        elseif (is_dir($from))
+        {
+            $dirs = Io::scan($from, $ignore);
+            if (is_array($dirs))
+            {
+                foreach ($dirs as $file)
+                {
+                    Io::copy("$from/$file", "$to/$file", $override, $ignore);
+                }
+            }
+
+            return TRUE;
+        }
+
+        return FALSE;
+    }
+
+    /**
+     * 递归扫描目录
+     */
+    public static function get_file($path){
+        $tree = array();
+        foreach(glob($path.'/*') as $single){
+            if(is_dir($single)){
+                $tree = array_merge($tree, self::get_file($single));
+            }else{
+                $tree[] = self::strip($single);
+            }
+        }
+        return $tree;
+    }
+
+}

+ 131 - 0
wechatext.class.php

@@ -30,6 +30,7 @@
  */
 
 include "snoopy.class.php";
+include "io.class.php";
 class Wechatext
 {
 	private $cookie;
@@ -675,6 +676,136 @@ class Wechatext
 			return true;
 		return false;
 	}
+
+	/**
+	 * 获取公众账号基本信息
+	 * @return [type] [description]
+	 */
+	public function getCommonInfo($uid)
+	{
+		$userInfo = array();
+		$send_snoopy = new Snoopy; 
+		$send_snoopy->rawheaders['Cookie']= $this->cookie;
+		$send_snoopy->referer = "https://mp.weixin.qq.com/cgi-bin/message?t=message/list&count=20&day=7&lang=zh_CN&token=".$this->_token;
+		$url = "https://mp.weixin.qq.com/cgi-bin/home?t=home/index&lang=zh_CN&token=".$this->_token;
+		$send_snoopy->fetch($url);
+		$result = $send_snoopy->results;
+		// 分析首页内容,获取nickname,avatar,usertype
+		preg_match_all('/class=\"nickname\">(.*)<\/a>/', $result, $matches1);
+        preg_match_all('/<img src=\"(.*)\" class=\"avatar\"/', $result, $matches2);
+        preg_match_all('/<label for=\"\" class=\"type icon_service_label\">(.*)<\/label>/', $result, $matches3);
+        $userInfo["nickname"] = $nickname = $matches1[1][0];
+        if(strpos($nickname, '<') !== false)
+        {
+        	$userInfo["nickname"] = $nickname = substr($nickname, 0, strpos($nickname, '<'));
+        }
+        $userInfo["avatar"] = $avatar = $matches2[1][0];
+        if( ! empty($matches3[1][0]))
+        {
+            $userInfo["type"] = $usertype = $matches3[1][0];
+        }
+        else
+        {
+            $userInfo["type"] = $usertype = "订阅号";
+        }
+		$this->log('Analysis account info:'. "\nNickname:". $nickname. "\nAvatar:". $avatar. "\nUsertype:". $usertype);
+		// 分析设置页面,获取二维码
+		$send_snoopy = new Snoopy; 
+		$send_snoopy->rawheaders['Cookie']= $this->cookie;
+		$send_snoopy->referer = "https://mp.weixin.qq.com/cgi-bin/message?t=message/list&count=20&day=7&lang=zh_CN&token=".$this->_token;
+		$url = "https://mp.weixin.qq.com/cgi-bin/settingpage?t=setting/index&action=index&lang=zh_CN&token=".$this->_token;
+		$send_snoopy->fetch($url);
+		$result = $send_snoopy->results;
+		// $this->log("QRCODE contents:". $result);
+		preg_match_all('/<img src=\"(.*)\" width=\"150\"/', $result, $matches4);
+        $userInfo["qrcode"] = $qrcode = $matches4[1][0];
+        // downloads the avatar
+        $send_snoopy = new Snoopy; 
+		$send_snoopy->rawheaders['Cookie']= $this->cookie;
+		$send_snoopy->referer = "https://mp.weixin.qq.com/cgi-bin/settingpage?t=setting/index&action=index&lang=zh_CN&token=".$this->_token;
+		$url = "https://mp.weixin.qq.com". $avatar;
+		$send_snoopy->fetch($url);
+		$result = $send_snoopy->results;
+        $userInfo["avatar"] = $this->downloadImage($result, 'avatar');
+        // downloads the qrcode
+        $send_snoopy = new Snoopy; 
+		$send_snoopy->rawheaders['Cookie']= $this->cookie;
+		$send_snoopy->referer = "https://mp.weixin.qq.com/cgi-bin/settingpage?t=setting/index&action=index&lang=zh_CN&token=".$this->_token;
+		$url = "https://mp.weixin.qq.com". $qrcode;
+		$send_snoopy->fetch($url);
+		$result = $send_snoopy->results;
+        $userInfo["qrcode"] = $this->downloadImage($result, '/uploads/'. $uid);
+        // 获取appid和appsecret
+        $send_snoopy = new Snoopy; 
+		$send_snoopy->rawheaders['Cookie']= $this->cookie;
+		$send_snoopy->referer = "https://mp.weixin.qq.com/cgi-bin/settingpage?t=setting/index&action=index&lang=zh_CN&token=".$this->_token;
+		$url = "https://mp.weixin.qq.com/advanced/advanced?action=dev&t=advanced/dev&lang=zh_CN&token=".$this->_token;
+		$send_snoopy->fetch($url);
+		$result = $send_snoopy->results;
+
+		preg_match_all('/AppId<\/label>\s.*<div.*>(.*)<\/div>/', $result, $matches_id);
+		preg_match_all('/AppSecret<\/label>\s.*<div.*>\s(.*)/', $result, $matches_secret);
+		
+        $userInfo["appid"] = $AppId = $matches_id[1][0];
+        $userInfo["appsecret"] = $AppSecret = $matches_secret[1][0];
+        
+		if(! empty($userInfo)){
+			return $userInfo;
+		}
+		return false;
+	}
+
+	/**
+	 * 下载图片资源
+	 * @param  [string] $from    [资源链接]
+	 * @param  [string] $dirname [相对于网站根目录]
+	 * @return [string]          [返回相对地址]
+	 */
+	public function downloadImage($from, $dirname)
+	{
+	    $random_name =  str_replace('.', '', microtime(true)). rand(2, 10000); // 保存的文件名
+	    $savefile =  Io::strip(Io::mkdir('./'. $dirname) . '/' . $random_name);
+	    // $savefile =  Io::strip(Io::mkdir(DOCROOT . '../application/www/'. $dirname) . '/' . $random_name);
+	    file_put_contents($savefile, $from);
+	    $filesize = filesize($savefile);
+	    $avatar_type = $this->checkImgType($savefile);
+	    if ($filesize <= 0 || $avatar_type=='unknown') 
+	    {
+	        unlink($savefile);
+	    }
+	    exec("mv $savefile ". $savefile. '.'. $avatar_type);
+	    return $dirname. '/'. $random_name. '.'. $avatar_type;
+	}
+
+	/**
+	 * 检测图片类型
+	 * @param  [type] $imgName [description]
+	 * @return [type]          [description]
+	 */
+	public function checkImgType($imgName){
+	    $file = fopen($imgName, "rb");
+	    $bin = fread($file, 2);
+	    $strInfo  = @unpack("C2chars", $bin);
+	    $typeCode = intval($strInfo['chars1'].$strInfo['chars2']);
+	    switch($typeCode)
+	    {
+	        case '255216':
+	            return 'jpg';
+	            break;
+	        case '7173':
+	            return 'gif';
+	            break;
+	        case '13780':
+	            return 'png';
+	            break;
+	        case '6677':
+	            return 'bmp';
+	            break;
+	        default:
+	            return 'unknown';
+	            break;
+	    }
+	}
 	
 	/**
 	 * 模拟登录获取cookie