1076 lines
81 KiB
PHP
1076 lines
81 KiB
PHP
|
<?php
|
|||
|
|
|||
|
/**
|
|||
|
* [Discuz!] (C)2001-2099 Comsenz Inc.
|
|||
|
* This is NOT a freeware, use is subject to license terms
|
|||
|
*
|
|||
|
* $Id: wechat.lib.class.php 36284 2016-12-12 00:47:50Z nemohou $
|
|||
|
*/
|
|||
|
if (!defined('IN_DISCUZ')) {
|
|||
|
exit('Access Denied');
|
|||
|
}
|
|||
|
|
|||
|
class WeChatServer {
|
|||
|
|
|||
|
private $_token;
|
|||
|
|
|||
|
private $_hooks;
|
|||
|
private $_classes;
|
|||
|
|
|||
|
public function __construct($token, $hooks = array()) {
|
|||
|
$this->_token = $token;
|
|||
|
$this->_hooks = $hooks;
|
|||
|
$this->accessDataPush();
|
|||
|
}
|
|||
|
|
|||
|
private function _activeHook($type) {
|
|||
|
if (!isset($this->_hooks[$type])) {
|
|||
|
return null;
|
|||
|
}
|
|||
|
$hook = & $this->_hooks[$type];
|
|||
|
global $_G;
|
|||
|
if (!in_array($hook['plugin'], $_G['setting']['plugins']['available'])) {
|
|||
|
return null;
|
|||
|
}
|
|||
|
if (!preg_match("/^[\w\_]+$/i", $hook['plugin']) || !preg_match('/^[\w\_\.]+\.php$/i', $hook['include'])) {
|
|||
|
return null;
|
|||
|
}
|
|||
|
include_once DISCUZ_ROOT . 'source/plugin/' . $hook['plugin'] . '/' . $hook['include'];
|
|||
|
if (!class_exists($hook['class'], false)) {
|
|||
|
return null;
|
|||
|
}
|
|||
|
if (!isset($this->classes[$hook['class']])) {
|
|||
|
$this->classes[$hook['class']] = new $hook['class'];
|
|||
|
}
|
|||
|
if (!method_exists($this->classes[$hook['class']], $hook['method'])) {
|
|||
|
return null;
|
|||
|
}
|
|||
|
$param = func_get_args();
|
|||
|
array_shift($param);
|
|||
|
return call_user_func(array($this->classes[$hook['class']], $hook['method']), $param);
|
|||
|
}
|
|||
|
|
|||
|
private function _checkSignature() {
|
|||
|
$signature = $_GET["signature"];
|
|||
|
$timestamp = $_GET["timestamp"];
|
|||
|
$nonce = $_GET["nonce"];
|
|||
|
|
|||
|
$token = $this->_token;
|
|||
|
$tmpArr = array($token, $timestamp, $nonce);
|
|||
|
sort($tmpArr, SORT_STRING);
|
|||
|
$tmpStr = implode($tmpArr);
|
|||
|
$tmpStr = sha1($tmpStr);
|
|||
|
|
|||
|
return $tmpStr == $signature;
|
|||
|
}
|
|||
|
|
|||
|
private function _handlePostObj($postObj) {
|
|||
|
$MsgType = strtolower((string) $postObj->MsgType);
|
|||
|
$result = array(
|
|||
|
'from' => self::$_from_id = (string) htmlspecialchars($postObj->FromUserName),
|
|||
|
'to' => self::$_my_id = (string) htmlspecialchars($postObj->ToUserName),
|
|||
|
'time' => (int) $postObj->CreateTime,
|
|||
|
'type' => (string) $MsgType
|
|||
|
);
|
|||
|
|
|||
|
if (property_exists($postObj, 'MsgId')) {
|
|||
|
$result['id'] = $postObj->MsgId;
|
|||
|
}
|
|||
|
|
|||
|
switch ($result['type']) {
|
|||
|
case 'text':
|
|||
|
$result['content'] = (string) $postObj->Content; // Content <20><>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD>
|
|||
|
break;
|
|||
|
|
|||
|
case 'location':
|
|||
|
$result['X'] = (float) $postObj->Location_X; // Location_X <20><><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>γ<EFBFBD><CEB3>
|
|||
|
$result['Y'] = (float) $postObj->Location_Y; // Location_Y <20><><EFBFBD><EFBFBD>λ<EFBFBD>þ<EFBFBD><C3BE><EFBFBD>
|
|||
|
$result['S'] = (float) $postObj->Scale; // Scale <20><>ͼ<EFBFBD><CDBC><EFBFBD>Ŵ<EFBFBD>С
|
|||
|
$result['I'] = (string) $postObj->Label; // Label <20><><EFBFBD><EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>Ϣ
|
|||
|
break;
|
|||
|
|
|||
|
case 'image':
|
|||
|
$result['url'] = (string) $postObj->PicUrl; // PicUrl ͼƬ<CDBC><C6AC><EFBFBD>ӣ<EFBFBD><D3A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߿<EFBFBD><DFBF><EFBFBD><EFBFBD><EFBFBD>HTTP GET<45><54>ȡ
|
|||
|
$result['mid'] = (string) $postObj->MediaId; // MediaId ͼƬ<CDBC><C6AC>Ϣý<CFA2><C3BD>id<69><64><EFBFBD><EFBFBD><EFBFBD>Ե<EFBFBD><D4B5>ö<EFBFBD>ý<EFBFBD><C3BD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD>ؽӿ<D8BD><D3BF><EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD>ݡ<EFBFBD>
|
|||
|
break;
|
|||
|
|
|||
|
case 'video':
|
|||
|
$result['mid'] = (string) $postObj->MediaId; // MediaId ͼƬ<CDBC><C6AC>Ϣý<CFA2><C3BD>id<69><64><EFBFBD><EFBFBD><EFBFBD>Ե<EFBFBD><D4B5>ö<EFBFBD>ý<EFBFBD><C3BD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD>ؽӿ<D8BD><D3BF><EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD>ݡ<EFBFBD>
|
|||
|
$result['thumbmid'] = (string) $postObj->ThumbMediaId; // ThumbMediaId <20><>Ƶ<EFBFBD><C6B5>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC>ý<EFBFBD><C3BD>id<69><64><EFBFBD><EFBFBD><EFBFBD>Ե<EFBFBD><D4B5>ö<EFBFBD>ý<EFBFBD><C3BD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD>ؽӿ<D8BD><D3BF><EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD>ݡ<EFBFBD>
|
|||
|
break;
|
|||
|
|
|||
|
case 'link':
|
|||
|
$result['title'] = (string) $postObj->Title;
|
|||
|
$result['desc'] = (string) $postObj->Description;
|
|||
|
$result['url'] = (string) $postObj->Url;
|
|||
|
break;
|
|||
|
|
|||
|
case 'voice':
|
|||
|
$result['mid'] = (string) $postObj->MediaId;
|
|||
|
$result['format'] = (string) $postObj->Format;
|
|||
|
if (property_exists($postObj, Recognition)) {
|
|||
|
$result['txt'] = (string) $postObj->Recognition;
|
|||
|
}
|
|||
|
break;
|
|||
|
|
|||
|
case 'event':
|
|||
|
$result['event'] = strtolower((string) $postObj->Event);
|
|||
|
switch ($result['event']) {
|
|||
|
|
|||
|
case 'subscribe':
|
|||
|
case 'scan':
|
|||
|
if (property_exists($postObj, EventKey)) {
|
|||
|
$result['key'] = str_replace(
|
|||
|
'qrscene_', '', (string) $postObj->EventKey
|
|||
|
);
|
|||
|
$result['ticket'] = (string) $postObj->Ticket;
|
|||
|
}
|
|||
|
break;
|
|||
|
|
|||
|
case 'location':
|
|||
|
$result['la'] = (string) $postObj->Latitude;
|
|||
|
$result['lo'] = (string) $postObj->Longitude;
|
|||
|
$result['p'] = (string) $postObj->Precision;
|
|||
|
break;
|
|||
|
|
|||
|
case 'click':
|
|||
|
$result['key'] = (string) $postObj->EventKey;
|
|||
|
break;
|
|||
|
case 'masssendjobfinish':
|
|||
|
$result['msg_id'] = (string) $postObj->MsgID;
|
|||
|
$result['status'] = (string) $postObj->Status;
|
|||
|
$result['totalcount'] = (string) $postObj->TotalCount;
|
|||
|
$result['filtercount'] = (string) $postObj->FilterCount;
|
|||
|
$result['sentcount'] = (string) $postObj->SentCount;
|
|||
|
$result['errorcount'] = (string) $postObj->ErrorCount;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
return $result;
|
|||
|
}
|
|||
|
|
|||
|
private function accessDataPush() {
|
|||
|
if (!$this->_checkSignature()) {
|
|||
|
if (!headers_sent()) {
|
|||
|
header('HTTP/1.1 404 Not Found');
|
|||
|
header('Status: 404 Not Found');
|
|||
|
}
|
|||
|
$this->_activeHook('404');
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
$postdata = file_get_contents("php://input");
|
|||
|
if ($postdata) {
|
|||
|
if (!$this->_checkSignature()) {
|
|||
|
return;
|
|||
|
}
|
|||
|
$postObj = simplexml_load_string($postdata, 'SimpleXMLElement', LIBXML_NOCDATA);
|
|||
|
$postObj = $this->_handlePostObj($postObj);
|
|||
|
|
|||
|
$this->_activeHook('receiveAllStart', $postObj);
|
|||
|
|
|||
|
if (isset($postObj['event'])) {
|
|||
|
$hookName = 'receiveEvent::' . $postObj['event'];
|
|||
|
} else {
|
|||
|
$hookName = 'receiveMsg::' . $postObj['type'];
|
|||
|
}
|
|||
|
$this->_activeHook($hookName, $postObj);
|
|||
|
|
|||
|
$this->_activeHook('receiveAllEnd', $postObj);
|
|||
|
} elseif (isset($_GET['echostr'])) {
|
|||
|
|
|||
|
$this->_activeHook('accessCheckSuccess');
|
|||
|
if (!headers_sent()) {
|
|||
|
header('Content-Type: text/plain');
|
|||
|
}
|
|||
|
echo preg_replace('/[^a-z0-9]/i', '', $_GET['echostr']);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private static $_from_id;
|
|||
|
private static $_my_id;
|
|||
|
|
|||
|
private static function _format2xml($nodes) {
|
|||
|
$xml = '<xml>'
|
|||
|
. '<ToUserName><![CDATA[%s]]></ToUserName>'
|
|||
|
. '<FromUserName><![CDATA[%s]]></FromUserName>'
|
|||
|
. '<CreateTime>%s</CreateTime>'
|
|||
|
. '%s'
|
|||
|
. '</xml>';
|
|||
|
$return = sprintf(
|
|||
|
$xml, self::$_from_id, self::$_my_id, time(), $nodes
|
|||
|
);
|
|||
|
return diconv($return, CHARSET, 'UTF-8');
|
|||
|
}
|
|||
|
|
|||
|
public static function getXml4Txt($txt) {
|
|||
|
$xml = '<MsgType><![CDATA[text]]></MsgType>'
|
|||
|
. '<Content><![CDATA[%s]]></Content>';
|
|||
|
return self::_format2xml(
|
|||
|
sprintf(
|
|||
|
$xml, $txt
|
|||
|
)
|
|||
|
);
|
|||
|
}
|
|||
|
|
|||
|
public static function getXml4ImgByMid($mid) {
|
|||
|
$xml = '<MsgType><![CDATA[image]]></MsgType>'
|
|||
|
. '<Image>'
|
|||
|
. '<MediaId><![CDATA[%s]]></MediaId>'
|
|||
|
. '</Image>';
|
|||
|
return self::_format2xml(
|
|||
|
sprintf(
|
|||
|
$xml, $mid
|
|||
|
)
|
|||
|
);
|
|||
|
}
|
|||
|
|
|||
|
public static function getXml4VoiceByMid($mid) {
|
|||
|
$xml = '<MsgType><![CDATA[voice]]></MsgType>'
|
|||
|
. '<Voice>'
|
|||
|
. '<MediaId><![CDATA[%s]]></MediaId>'
|
|||
|
. '</Voice>';
|
|||
|
return self::_format2xml(
|
|||
|
sprintf(
|
|||
|
$xml, $mid
|
|||
|
)
|
|||
|
);
|
|||
|
}
|
|||
|
|
|||
|
public static function getXml4VideoByMid($mid, $title, $desc = '') {
|
|||
|
$desc = '' !== $desc ? $desc : $title;
|
|||
|
$xml = '<MsgType><![CDATA[video]]></MsgType>'
|
|||
|
. '<Video>'
|
|||
|
. '<MediaId><![CDATA[%s]]></MediaId>'
|
|||
|
. '<Title><![CDATA[%s]]></Title>'
|
|||
|
. '<Description><![CDATA[%s]]></Description>'
|
|||
|
. '</Video>';
|
|||
|
|
|||
|
return self::_format2xml(
|
|||
|
sprintf(
|
|||
|
$xml, $mid, $title, $desc
|
|||
|
)
|
|||
|
);
|
|||
|
}
|
|||
|
|
|||
|
public static function getXml4MusicByUrl($url, $thumbmid, $title, $desc = '', $hqurl = '') {
|
|||
|
$xml = '<MsgType><![CDATA[music]]></MsgType>'
|
|||
|
. '<Music>'
|
|||
|
. '<Title><![CDATA[%s]]></Title>'
|
|||
|
. '<Description><![CDATA[%s]]></Description>'
|
|||
|
. '<MusicUrl><![CDATA[%s]]></MusicUrl>'
|
|||
|
. '<HQMusicUrl><![CDATA[%s]]></HQMusicUrl>'
|
|||
|
. '<ThumbMediaId><![CDATA[%s]]></ThumbMediaId>'
|
|||
|
. '</Music>';
|
|||
|
|
|||
|
return self::_format2xml(
|
|||
|
sprintf(
|
|||
|
$xml, $title, '' === $desc ? $title : $desc, $url, $hqurl ? $hqurl : $url, $thumbmid
|
|||
|
)
|
|||
|
);
|
|||
|
}
|
|||
|
|
|||
|
public static function getXml4RichMsgByArray($list) {
|
|||
|
$max = 10;
|
|||
|
$i = 0;
|
|||
|
$ii = count($list);
|
|||
|
$list_xml = '';
|
|||
|
while ($i < $ii && $i < $max) {
|
|||
|
$item = $list[$i++];
|
|||
|
$list_xml .=
|
|||
|
sprintf(
|
|||
|
'<item>'
|
|||
|
. '<Title><![CDATA[%s]]></Title> '
|
|||
|
. '<Description><![CDATA[%s]]></Description>'
|
|||
|
. '<PicUrl><![CDATA[%s]]></PicUrl>'
|
|||
|
. '<Url><![CDATA[%s]]></Url>'
|
|||
|
. '</item>', $item['title'], $item['desc'], $item['pic'], $item['url']
|
|||
|
);
|
|||
|
}
|
|||
|
|
|||
|
$xml = '<MsgType><![CDATA[news]]></MsgType>'
|
|||
|
. '<ArticleCount>%s</ArticleCount>'
|
|||
|
. '<Articles>%s</Articles>';
|
|||
|
|
|||
|
return self::_format2xml(
|
|||
|
sprintf(
|
|||
|
$xml, $i, $list_xml
|
|||
|
)
|
|||
|
);
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
class WeChatClient {
|
|||
|
|
|||
|
public static $_URL_API_ROOT = 'https://api.weixin.qq.com';
|
|||
|
public static $_URL_FILE_API_ROOT = 'http://file.api.weixin.qq.com';
|
|||
|
public static $_URL_QR_ROOT = 'http://mp.weixin.qq.com';
|
|||
|
public static $_QRCODE_TICKET_DEFAULT_ID = 1;
|
|||
|
public static $ERRCODE_MAP = array(
|
|||
|
'-1' => '系统繁忙',
|
|||
|
'0' => '请求成功',
|
|||
|
'40001' => '获取access_token时AppSecret错误,或者access_token无效',
|
|||
|
'40002' => '不合法的凭证类型',
|
|||
|
'40003' => '不合法的OpenID',
|
|||
|
'40004' => '不合法的媒体文件类型',
|
|||
|
'40005' => '不合法的文件类型',
|
|||
|
'40006' => '不合法的文件大小',
|
|||
|
'40007' => '不合法的媒体文件id',
|
|||
|
'40008' => '不合法的消息类型',
|
|||
|
'40009' => '不合法的图片文件大小',
|
|||
|
'40010' => '不合法的语音文件大小',
|
|||
|
'40011' => '不合法的视频文件大小',
|
|||
|
'40012' => '不合法的缩略图文件大小',
|
|||
|
'40013' => '不合法的APPID',
|
|||
|
'40014' => '不合法的access_token',
|
|||
|
'40015' => '不合法的菜单类型',
|
|||
|
'40016' => '不合法的按钮个数',
|
|||
|
'40017' => '不合法的按钮个数',
|
|||
|
'40018' => '不合法的按钮名字长度',
|
|||
|
'40019' => '不合法的按钮KEY长度',
|
|||
|
'40020' => '不合法的按钮URL长度',
|
|||
|
'40021' => '不合法的菜单版本号',
|
|||
|
'40022' => '不合法的子菜单级数',
|
|||
|
'40023' => '不合法的子菜单按钮个数',
|
|||
|
'40024' => '不合法的子菜单按钮类型',
|
|||
|
'40025' => '不合法的子菜单按钮名字长度',
|
|||
|
'40026' => '不合法的子菜单按钮KEY长度',
|
|||
|
'40027' => '不合法的子菜单按钮URL长度',
|
|||
|
'40028' => '不合法的自定义菜单使用用户',
|
|||
|
'40029' => '不合法的oauth_code',
|
|||
|
'40030' => '不合法的refresh_token',
|
|||
|
'40031' => '不合法的openid列表',
|
|||
|
'40032' => '不合法的openid列表长度',
|
|||
|
'40033' => '不合法的请求字符,不能包含\uxxxx格式的字符',
|
|||
|
'40035' => '不合法的参数',
|
|||
|
'40038' => '不合法的请求格式',
|
|||
|
'40039' => '不合法的URL长度',
|
|||
|
'40050' => '不合法的分组id',
|
|||
|
'40051' => '分组名字不合法',
|
|||
|
'41001' => '缺少access_token参数',
|
|||
|
'41002' => '缺少appid参数',
|
|||
|
'41003' => '缺少refresh_token参数',
|
|||
|
'41004' => '缺少secret参数',
|
|||
|
'41005' => '缺少多媒体文件数据',
|
|||
|
'41006' => '缺少media_id参数',
|
|||
|
'41007' => '缺少子菜单数据',
|
|||
|
'41008' => '缺少oauth code',
|
|||
|
'41009' => '缺少openid',
|
|||
|
'42001' => 'access_token超时',
|
|||
|
'42002' => 'refresh_token超时',
|
|||
|
'42003' => 'oauth_code超时',
|
|||
|
'43001' => '需要GET请求',
|
|||
|
'43002' => '需要POST请求',
|
|||
|
'43003' => '需要HTTPS请求',
|
|||
|
'43004' => '需要接收者关注',
|
|||
|
'43005' => '需要好友关系',
|
|||
|
'44001' => '多媒体文件为空',
|
|||
|
'44002' => 'POST的数据包为空',
|
|||
|
'44003' => '图文消息内容为空',
|
|||
|
'44004' => '文本消息内容为空',
|
|||
|
'45001' => '多媒体文件大小超过限制',
|
|||
|
'45002' => '消息内容超过限制',
|
|||
|
'45003' => '标题字段超过限制',
|
|||
|
'45004' => '描述字段超过限制',
|
|||
|
'45005' => '链接字段超过限制',
|
|||
|
'45006' => '图片链接字段超过限制',
|
|||
|
'45007' => '语音播放时间超过限制',
|
|||
|
'45008' => '图文消息超过限制',
|
|||
|
'45009' => '接口调用超过限制',
|
|||
|
'45010' => '创建菜单个数超过限制',
|
|||
|
'45015' => '回复时间超过限制',
|
|||
|
'45016' => '系统分组,不允许修改',
|
|||
|
'45017' => '分组名字过长',
|
|||
|
'45018' => '分组数量超过上限',
|
|||
|
'46001' => '不存在媒体数据',
|
|||
|
'46002' => '不存在的菜单版本',
|
|||
|
'46003' => '不存在的菜单数据',
|
|||
|
'46004' => '不存在的用户',
|
|||
|
'47001' => '解析JSON/XML内容错误',
|
|||
|
'48001' => 'api功能未授权',
|
|||
|
'50001' => '用户未授权该api',
|
|||
|
);
|
|||
|
public static $_USERINFO_LANG = 'en';
|
|||
|
private $_appid;
|
|||
|
private $_appsecret;
|
|||
|
private static $_accessTokenCache = array();
|
|||
|
private static $ERROR_LOGS = array();
|
|||
|
private static $ERROR_NO = 0;
|
|||
|
|
|||
|
public function __construct($appid, $appsecret = '') {
|
|||
|
if ($appsecret) {
|
|||
|
$this->_appid = $appid;
|
|||
|
$this->_appsecret = $appsecret;
|
|||
|
} else {
|
|||
|
$info = WeChatHook::getAppInfo($appid);
|
|||
|
$this->_appid = $info['appId'];
|
|||
|
$this->_appsecret = $info['appSecret'];
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public static function error() {
|
|||
|
return self::$ERRCODE_MAP[self::$ERROR_NO] ? self::$ERRCODE_MAP[self::$ERROR_NO] : self::$ERROR_NO;
|
|||
|
}
|
|||
|
|
|||
|
public static function checkIsSuc($res) {
|
|||
|
$result = true;
|
|||
|
if (is_string($res)) {
|
|||
|
$res = json_decode($res, true);
|
|||
|
}
|
|||
|
if (isset($res['errcode']) && ( 0 !== (int) $res['errcode'])) {
|
|||
|
array_push(self::$ERROR_LOGS, $res);
|
|||
|
$result = false;
|
|||
|
self::$ERROR_NO = $res['errcode'];
|
|||
|
}
|
|||
|
return $result;
|
|||
|
}
|
|||
|
|
|||
|
public static function get($url) {
|
|||
|
$ch = curl_init();
|
|||
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|||
|
# curl_setopt($ch, CURLOPT_HEADER, 1);
|
|||
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|||
|
|
|||
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
|
|||
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
|
|||
|
|
|||
|
if (!curl_exec($ch)) {
|
|||
|
error_log(curl_error($ch));
|
|||
|
$data = '';
|
|||
|
} else {
|
|||
|
$data = curl_multi_getcontent($ch);
|
|||
|
}
|
|||
|
curl_close($ch);
|
|||
|
return $data;
|
|||
|
}
|
|||
|
|
|||
|
private static function post($url, $data) {
|
|||
|
if (!function_exists('curl_init')) {
|
|||
|
return '';
|
|||
|
}
|
|||
|
$ch = curl_init();
|
|||
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|||
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|||
|
# curl_setopt( $ch, CURLOPT_HEADER, 1);
|
|||
|
|
|||
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
|
|||
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
|
|||
|
|
|||
|
curl_setopt($ch, CURLOPT_POST, 1);
|
|||
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
|
|||
|
$data = curl_exec($ch);
|
|||
|
if (!$data) {
|
|||
|
error_log(curl_error($ch));
|
|||
|
}
|
|||
|
curl_close($ch);
|
|||
|
return $data;
|
|||
|
}
|
|||
|
|
|||
|
public function getAccessToken($tokenOnly = 1, $nocache = 0) {
|
|||
|
global $_G;
|
|||
|
$myTokenInfo = null;
|
|||
|
$appid = $this->_appid;
|
|||
|
$appsecret = $this->_appsecret;
|
|||
|
$cachename = 'wechatat_' . $appid;
|
|||
|
loadcache($cachename);
|
|||
|
|
|||
|
if ($nocache || empty(self::$_accessTokenCache[$appid])) {
|
|||
|
self::$_accessTokenCache[$appid] = $_G['cache'][$cachename];
|
|||
|
}
|
|||
|
|
|||
|
if (!empty(self::$_accessTokenCache[$appid])) {
|
|||
|
$myTokenInfo = self::$_accessTokenCache[$appid];
|
|||
|
if (time() < $myTokenInfo['expiration']) {
|
|||
|
return $tokenOnly ? $myTokenInfo['token'] : $myTokenInfo;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
$url = self::$_URL_API_ROOT . "/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret";
|
|||
|
|
|||
|
$json = self::get($url);
|
|||
|
$res = json_decode($json, true);
|
|||
|
|
|||
|
if (self::checkIsSuc($res)) {
|
|||
|
self::$_accessTokenCache[$appid] = $myTokenInfo = array(
|
|||
|
'token' => $res['access_token'],
|
|||
|
'expiration' => time() + (int) $res['expires_in']
|
|||
|
);
|
|||
|
savecache($cachename, $myTokenInfo);
|
|||
|
}
|
|||
|
return $tokenOnly ? $myTokenInfo['token'] : $myTokenInfo;
|
|||
|
}
|
|||
|
|
|||
|
public function setAccessToken($tokenInfo) {
|
|||
|
if ($tokenInfo) {
|
|||
|
$appid = $this->_appid;
|
|||
|
self::$_accessTokenCache[$appid] = array(
|
|||
|
'token' => $tokenInfo['token'],
|
|||
|
'expire' => $tokenInfo['expire']
|
|||
|
);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public function upload($type, $file_path, $mediaidOnly = 1) {
|
|||
|
$access_token = $this->getAccessToken();
|
|||
|
$url = self::$_URL_FILE_API_ROOT . "/cgi-bin/media/upload?access_token=$access_token&type=$type";
|
|||
|
|
|||
|
$res = self::post($url, array('media' => "@$file_path"));
|
|||
|
$res = json_decode($res, true);
|
|||
|
|
|||
|
if (self::checkIsSuc($res)) {
|
|||
|
return $mediaidOnly ? $res['media_id'] : $res;
|
|||
|
}
|
|||
|
return null;
|
|||
|
}
|
|||
|
|
|||
|
public function download($mid) {
|
|||
|
$access_token = $this->getAccessToken();
|
|||
|
$url = self::$_URL_FILE_API_ROOT . "/cgi-bin/media/get?access_token=$access_token&media_id=$mid";
|
|||
|
|
|||
|
return self::get($url);
|
|||
|
}
|
|||
|
|
|||
|
public function getMenu() {
|
|||
|
|
|||
|
$access_token = $this->getAccessToken();
|
|||
|
$url = self::$_URL_API_ROOT . "/cgi-bin/menu/get?access_token=$access_token";
|
|||
|
|
|||
|
$json = self::get($url);
|
|||
|
|
|||
|
$res = json_decode($json, true);
|
|||
|
if (self::checkIsSuc($res)) {
|
|||
|
return $res;
|
|||
|
}
|
|||
|
return null;
|
|||
|
}
|
|||
|
|
|||
|
public function deleteMenu() {
|
|||
|
$access_token = $this->getAccessToken();
|
|||
|
$url = self::$_URL_API_ROOT . "/cgi-bin/menu/delete?access_token=$access_token";
|
|||
|
|
|||
|
$res = self::get($url);
|
|||
|
return self::checkIsSuc($res);
|
|||
|
}
|
|||
|
|
|||
|
public function setMenu($myMenu) {
|
|||
|
$access_token = $this->getAccessToken();
|
|||
|
$url = self::$_URL_API_ROOT . "/cgi-bin/menu/create?access_token=$access_token";
|
|||
|
|
|||
|
if (defined('JSON_UNESCAPED_UNICODE')) {
|
|||
|
$json = is_string($myMenu) ? $myMenu : json_encode($myMenu, JSON_UNESCAPED_UNICODE);
|
|||
|
} else {
|
|||
|
$json = is_string($myMenu) ? $myMenu : json_encode($myMenu);
|
|||
|
}
|
|||
|
|
|||
|
$json = urldecode($json);
|
|||
|
$res = self::post($url, $json);
|
|||
|
|
|||
|
return self::checkIsSuc($res);
|
|||
|
}
|
|||
|
|
|||
|
private function _send($to, $type, $data) {
|
|||
|
$access_token = $this->getAccessToken();
|
|||
|
$url = self::$_URL_API_ROOT . "/cgi-bin/message/custom/send?access_token=$access_token";
|
|||
|
|
|||
|
$json = json_encode(
|
|||
|
array(
|
|||
|
'touser' => $to,
|
|||
|
'msgtype' => $type,
|
|||
|
$type => $data
|
|||
|
)
|
|||
|
);
|
|||
|
|
|||
|
$res = self::post($url, $json);
|
|||
|
|
|||
|
return self::checkIsSuc($res);
|
|||
|
}
|
|||
|
|
|||
|
public function sendTextMsg($to, $msg) {
|
|||
|
return $this->_send($to, 'text', array('content' => $msg));
|
|||
|
}
|
|||
|
|
|||
|
public function sendImgMsg($to, $mid) {
|
|||
|
return $this->_send($to, 'image', array('media_id' => $mid));
|
|||
|
}
|
|||
|
|
|||
|
public function sendVoice($to, $mid) {
|
|||
|
return $this->_send($to, 'voice', array('media_id' => $mid));
|
|||
|
}
|
|||
|
|
|||
|
public function sendVideo($to, $mid, $title, $desc) {
|
|||
|
return $this->_send($to, 'video', array(
|
|||
|
'media_id' => $mid,
|
|||
|
'title' => $title,
|
|||
|
'description' => $desc
|
|||
|
));
|
|||
|
}
|
|||
|
|
|||
|
public function sendMusic($to, $url, $thumb_mid, $title, $desc = '', $hq_url = '') {
|
|||
|
return $this->_send($to, 'music', array(
|
|||
|
'media_id' => $mid,
|
|||
|
'title' => $title,
|
|||
|
'description' => $desc || $title,
|
|||
|
'musicurl' => $url,
|
|||
|
'thumb_media_id' => $thumb_mid,
|
|||
|
'hqmusicurl' => $hq_url || $url
|
|||
|
));
|
|||
|
}
|
|||
|
|
|||
|
static private function _filterForRichMsg($articles) {
|
|||
|
$i = 0;
|
|||
|
$ii = len($articles);
|
|||
|
$list = array('title', 'desc', 'url', 'thumb_url');
|
|||
|
$result = array();
|
|||
|
while ($i < $ii) {
|
|||
|
$currentArticle = $articles[$i++];
|
|||
|
try {
|
|||
|
array_push($result, array(
|
|||
|
'title' => $currentArticle['title'],
|
|||
|
'description' => $currentArticle['desc'],
|
|||
|
'url' => $currentArticle['url'],
|
|||
|
'picurl' => $currentArticle['thumb_url']
|
|||
|
));
|
|||
|
} catch (Exception $e) {
|
|||
|
|
|||
|
}
|
|||
|
}
|
|||
|
return $result;
|
|||
|
}
|
|||
|
|
|||
|
public function uploadNews($articles) {
|
|||
|
$i = 0;
|
|||
|
$ii = count($articles);
|
|||
|
$result = array();
|
|||
|
while ($i < $ii) {
|
|||
|
$currentArticle = $articles[$i++];
|
|||
|
try {
|
|||
|
array_push($result, array(
|
|||
|
'thumb_media_id' => $currentArticle['thumb_media_id'],
|
|||
|
'title' => $this->convertToUtf($currentArticle['title']),
|
|||
|
'content' => $this->convertToUtf($currentArticle['content']),
|
|||
|
'author' => $this->convertToUtf($currentArticle['author']),
|
|||
|
'content_source_url' => $this->convertToUtf($currentArticle['url']),
|
|||
|
'digest' => $this->convertToUtf($currentArticle['desc']),
|
|||
|
'show_cover_pic' => 1
|
|||
|
));
|
|||
|
} catch (Exception $e) {
|
|||
|
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
$access_token = $this->getAccessToken();
|
|||
|
$url = self::$_URL_API_ROOT . "/cgi-bin/media/uploadnews?access_token=$access_token";
|
|||
|
if (defined('JSON_UNESCAPED_UNICODE')) {
|
|||
|
$json = json_encode(array('articles' => $result), JSON_UNESCAPED_UNICODE);
|
|||
|
} else {
|
|||
|
$json = json_encode(array('articles' => $result));
|
|||
|
}
|
|||
|
|
|||
|
$json = urldecode($json);
|
|||
|
|
|||
|
$res = self::post($url, $json);
|
|||
|
if (self::checkIsSuc($res)) {
|
|||
|
return json_decode($res, true);
|
|||
|
} else {
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public function sendMassMsg($msg) {
|
|||
|
$access_token = $this->getAccessToken();
|
|||
|
$url = self::$_URL_API_ROOT . "/cgi-bin/message/mass/sendall?access_token=$access_token";
|
|||
|
$post = array();
|
|||
|
$post['filter'] = array('group_id' => $msg['group_id']);
|
|||
|
if ($msg['type'] == 'media') {
|
|||
|
$post['mpnews'] = array('media_id' => $msg['media_id']);
|
|||
|
$post['msgtype'] = 'mpnews';
|
|||
|
} else {
|
|||
|
$post['text'] = array('content' => $this->convertToUtf($msg['text']));
|
|||
|
$post['msgtype'] = 'text';
|
|||
|
}
|
|||
|
|
|||
|
if (defined('JSON_UNESCAPED_UNICODE')) {
|
|||
|
$json = json_encode($post, JSON_UNESCAPED_UNICODE);
|
|||
|
} else {
|
|||
|
$json = json_encode($post);
|
|||
|
}
|
|||
|
|
|||
|
$json = urldecode($json);
|
|||
|
|
|||
|
$res = self::post($url, $json);
|
|||
|
if (self::checkIsSuc($res)) {
|
|||
|
return json_decode($res, true);
|
|||
|
} else {
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
function convertToUtf($str) {
|
|||
|
return urlencode(diconv($str, CHARSET, 'UTF-8'));
|
|||
|
}
|
|||
|
|
|||
|
public function sendRichMsg($to, $articles) {
|
|||
|
|
|||
|
return $this->_send($to, 'news', array(
|
|||
|
'articles' => self::_filterForRichMsg($articles)
|
|||
|
));
|
|||
|
}
|
|||
|
|
|||
|
public function createGroup($name) {
|
|||
|
$access_token = $this->getAccessToken();
|
|||
|
$url = self::$_URL_API_ROOT . "/cgi-bin/groups/create?access_token=$access_token";
|
|||
|
|
|||
|
$res = self::post($url, json_encode(array(
|
|||
|
'group' => array('name' => $name)
|
|||
|
)));
|
|||
|
|
|||
|
$res = json_decode($res, true);
|
|||
|
return self::checkIsSuc($res) ? $res['group']['id'] : null;
|
|||
|
}
|
|||
|
|
|||
|
public function renameGroup($gid, $name) {
|
|||
|
$access_token = $this->getAccessToken();
|
|||
|
$url = self::$_URL_API_ROOT . "/cgi-bin/groups/update?access_token=$access_token";
|
|||
|
|
|||
|
$res = self::post($url, json_encode(array(
|
|||
|
'group' => array(
|
|||
|
'id' => $gid,
|
|||
|
'name' => $name
|
|||
|
)
|
|||
|
)));
|
|||
|
|
|||
|
$res = json_decode($res, true);
|
|||
|
return self::checkIsSuc($res);
|
|||
|
}
|
|||
|
|
|||
|
public function moveUserById($uid, $gid) {
|
|||
|
$access_token = $this->getAccessToken();
|
|||
|
$url = self::$_URL_API_ROOT . "/cgi-bin/groups/members/update?access_token=$access_token";
|
|||
|
|
|||
|
$res = self::post(
|
|||
|
$url, json_encode(
|
|||
|
array(
|
|||
|
'openid' => $mid,
|
|||
|
'to_groupid' => $gid
|
|||
|
)
|
|||
|
)
|
|||
|
);
|
|||
|
|
|||
|
$res = json_decode($res, true);
|
|||
|
return self::checkIsSuc($res);
|
|||
|
}
|
|||
|
|
|||
|
public function getAllGroups() {
|
|||
|
$access_token = $this->getAccessToken();
|
|||
|
$url = self::$_URL_API_ROOT . "/cgi-bin/groups/get?access_token=$access_token";
|
|||
|
|
|||
|
$res = json_decode(self::get($url), true);
|
|||
|
|
|||
|
if (self::checkIsSuc($res)) {
|
|||
|
return $res['groups'];
|
|||
|
} else {
|
|||
|
return null;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public function getGroupidByUserid($uid) {
|
|||
|
$access_token = $this->getAccessToken();
|
|||
|
$url = self::$_URL_API_ROOT . "/cgi-bin/groups/getid?access_token=$access_token";
|
|||
|
|
|||
|
$res = self::post($url, json_encode(array(
|
|||
|
'openid' => $mid
|
|||
|
)));
|
|||
|
|
|||
|
$res = json_decode($res, true);
|
|||
|
return self::checkIsSuc($res) ? $res['groupid'] : null;
|
|||
|
}
|
|||
|
|
|||
|
public function getUserInfoById($uid, $lang = '') {
|
|||
|
if (!$lang) {
|
|||
|
$lang = self::$_USERINFO_LANG;
|
|||
|
}
|
|||
|
$access_token = $this->getAccessToken();
|
|||
|
$url = self::$_URL_API_ROOT . "/cgi-bin/user/info?access_token=$access_token&openid=$uid&lang=$lang";
|
|||
|
|
|||
|
$res = json_decode(self::get($url), true);
|
|||
|
|
|||
|
return self::checkIsSuc($res) ? $res : null;
|
|||
|
}
|
|||
|
|
|||
|
public function getFollowersList($next_id = '') {
|
|||
|
$access_token = $this->getAccessToken();
|
|||
|
$extend = '';
|
|||
|
if ($next_id) {
|
|||
|
$extend = "&next_openid=$next_id";
|
|||
|
}
|
|||
|
$url = self::$_URL_API_ROOT . "/cgi-bin/user/get?access_token=${access_token}$extend";
|
|||
|
|
|||
|
$res = json_decode(
|
|||
|
self::get($url), true
|
|||
|
);
|
|||
|
|
|||
|
return self::checkIsSuc($res) ? array(
|
|||
|
'total' => $res['total'],
|
|||
|
'list' => $res['data']['openid'],
|
|||
|
'next_id' => isset($res['next_openid']) ? $res['next_openid'] : null
|
|||
|
) : null;
|
|||
|
}
|
|||
|
|
|||
|
public function getOAuthConnectUri($redirect_uri, $state = '', $scope = 'snsapi_base') {
|
|||
|
$redirect_uri = urlencode($redirect_uri);
|
|||
|
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$this->_appid}&redirect_uri={$redirect_uri}&response_type=code&scope={$scope}&state={$state}#wechat_redirect";
|
|||
|
return $url;
|
|||
|
}
|
|||
|
|
|||
|
public function getAccessTokenByCode($code) {
|
|||
|
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$this->_appid}&secret={$this->_appsecret}&code=$code&grant_type=authorization_code";
|
|||
|
$res = json_decode(self::get($url), true);
|
|||
|
return $res;
|
|||
|
}
|
|||
|
|
|||
|
public function refreshAccessTocken($refresh_token) {
|
|||
|
$url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid={$this->_appid}&grant_type=refresh_token&refresh_token=$refresh_token";
|
|||
|
$res = json_decode(self::get($url), true);
|
|||
|
return $res;
|
|||
|
}
|
|||
|
|
|||
|
public function getUserInfoByAuth($access_token, $openid, $lang = 'zh_CN') {
|
|||
|
$url = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=$lang";
|
|||
|
$res = json_decode(self::get($url), true);
|
|||
|
return $res;
|
|||
|
}
|
|||
|
|
|||
|
public static function getQrcodeImgByTicket($ticket) {
|
|||
|
return self::get($this->getQrcodeImgUrlByTicket($ticket));
|
|||
|
}
|
|||
|
|
|||
|
public static function getQrcodeImgUrlByTicket($ticket) {
|
|||
|
$ticket = urlencode($ticket);
|
|||
|
return self::$_URL_QR_ROOT . "/cgi-bin/showqrcode?ticket=$ticket";
|
|||
|
}
|
|||
|
|
|||
|
public function getQrcodeTicket($options = array()) {
|
|||
|
$access_token = $this->getAccessToken();
|
|||
|
|
|||
|
$scene_id = isset($options['scene_id']) ? (int) $options['scene_id'] : 0;
|
|||
|
$expire = isset($options['expire']) ? (int) $options['expire'] : 0;
|
|||
|
$ticketOnly = isset($options['ticketOnly']) ? $options['ticketOnly'] : 1;
|
|||
|
|
|||
|
$url = self::$_URL_API_ROOT . "/cgi-bin/qrcode/create?access_token=$access_token";
|
|||
|
$data = array(
|
|||
|
'action_name' => 'QR_LIMIT_SCENE',
|
|||
|
'action_info' => array(
|
|||
|
'scene' => array(
|
|||
|
'scene_id' => $scene_id
|
|||
|
)
|
|||
|
)
|
|||
|
);
|
|||
|
if ($expire) {
|
|||
|
$data['expire_seconds'] = $expire;
|
|||
|
$data['action_name'] = 'QR_SCENE';
|
|||
|
}
|
|||
|
|
|||
|
if ($data['action_name'] == 'QR_LIMIT_SCENE' && $scene_id > 100000) {
|
|||
|
$data['action_info']['scene']['scene_id'] = self::$_QRCODE_TICKET_DEFAULT_ID;
|
|||
|
}
|
|||
|
|
|||
|
$data = json_encode($data);
|
|||
|
|
|||
|
$res = self::post($url, $data);
|
|||
|
$res = json_decode($res, true);
|
|||
|
|
|||
|
if (self::checkIsSuc($res)) {
|
|||
|
return $ticketOnly ? $res['ticket'] : array(
|
|||
|
'ticket' => $res['ticket'],
|
|||
|
'expire' => $res['expire_seconds']
|
|||
|
);
|
|||
|
}
|
|||
|
return null;
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
class WeChatEmoji {
|
|||
|
|
|||
|
public static function clear($str) {
|
|||
|
$config = self::getList();
|
|||
|
$str = str_replace($config, '', $str);
|
|||
|
return diconv($str, 'UTF-8', CHARSET);
|
|||
|
}
|
|||
|
|
|||
|
public static function getList() {
|
|||
|
return array(
|
|||
|
"\xee\x98\xbe", "\xee\x98\xbf", "\xee\x99\x80", "\xee\x99\x81", "\xee\x99\x82", "\xee\x99\x83", "\xee\x99\x84", "\xee\x99\x85", "\xee\x9a\xb3", "[\xe5\xa4\x95\xe7\x84\xbc\xe3\x81\x91]", "[\xe8\x99\xb9]", "[\xe9\x9b\xaa\xe7\xb5\x90\xe6\x99\xb6]", "\xee\x98\xbe\xee\x98\xbf", "\xee\x9c\xbf", "[\xe7\x81\xab\xe5\xb1\xb1]", "[\xe5\x9c\xb0\xe7\x90\x83]", "\xee\x9a\x9c", "\xee\x9a\x9d", "\xee\x9a\x9e", "\xee\x9a\x9f", "\xee\x9a\xa0", "[\xe2\x98\x86]", "\xe2\x98\x86\xe5\xbd\xa1", "\xee\x9a\xba", "\xee\x9c\x9f", "\xee\x9c\x9c", "\xee\x99\x86", "\xee\x99\x87", "\xee\x99\x88", "\xee\x99\x89", "\xee\x99\x8a", "\xee\x99\x8b", "\xee\x99\x8c", "\xee\x99\x8d", "\xee\x99\x8e", "\xee\x99\x8f", "\xee\x99\x90", "\xee\x99\x91", "[\xe8\x9b\x87\xe4\xbd\xbf\xe5\xba\xa7]", "\xee\x9d\x81", "\xee\x9d\x83", "\xee\x9d\x86", "\xee\x9d\x87", "\xee\x9d\x88", "[\xe3\x83\x90\xe3\x83\xa9]", "[\xe9\xa2\xa8\xe3\x81\xab\xe8\x88\x9e\xe3\x81\x86\xe8\x91\x89]", "[\xe3\x83\x8f\xe3\x82\xa4\xe3\x83\x93\xe3\x82\xb9\xe3\x82\xab\xe3\x82\xb9]", "[\xe3\x81\xb2\xe3\x81\xbe\xe3\x82\x8f\xe3\x82\x8a]", "[\xe3\x83\xa4\xe3\x82\xb7]", "[\xe3\x82\xb5\xe3\x83\x9c\xe3\x83\x86\xe3\x83\xb3]", "[\xe7\xa8\xb2\xe7\xa9\x82]", "[\xe3\x81\xa8\xe3\x81\x86\xe3\x82\x82\xe3\x82\x8d\xe3\x81\x93\xe3\x81\x97]", "[\xe3\x82\xad\xe3\x83\x8e\xe3\x82\xb3]", "[\xe6\xa0\x97]", "[\xe8\x8a\xb1]", "\xee\x9d\x82", "\xee\x9d\x84", "\xee\x9d\x85", "[\xe3\x81\xbf\xe3\x81\x8b\xe3\x82\x93]", "[\xe3\x82\xa4\xe3\x83\x81\xe3\x82\xb4]", "[\xe3\x82\xb9\xe3\x82\xa4\xe3\x82\xab]", "[\xe3\x83\x88\xe3\x83\x9e\xe3\x83\x88]", "[\xe3\x83\x8a\xe3\x82\xb9]", "[\xe3\x83\xa1\xe3\x83\xad\xe3\x83\xb3]", "[\xe3\x83\x91\xe3\x82\xa4\xe3\x83\x8a\xe3\x83\x83\xe3\x83\x97\xe3\x83\xab]", "[\xe3\x83\x96\xe3\x83\x89\xe3\x82\xa6]", "[\xe3\x83\xa2\xe3\x83\xa2]", "\xee\x9a\x91", "\xee\x9a\x92", "[\xe9\xbc\xbb]", "\xee\x9b\xb9", "\xee\x9c\xa8", "\xee\x9c\x90", "[\xe3\x83\x9e\xe3\x83\x8b\xe3\x82\xad\xe3\x83\xa5\xe3\x82\xa2]", "[\xe3\x82\xa8\xe3\x82\xb9\xe3\x83\x86]", "\xee\x99\xb5", "[\xe5\xba\x8a\xe5\xb1\x8b]", "\xee\x9a\xb1", "\xee\x9b\xb0", "[\xe5\xae\xb6\xe6\x97\x8f]", "[\xe3\x82\xab\xe3\x83\x83\xe3\x83\x97\xe3\x83\xab]", "[\xe8\xad\xa6\xe5\xae\x98]", "[\xe3\x83\x90\xe3\x83\x8b\xe3\x83\xbc]", "[\xe8\x8a\xb1\xe5\xab\x81]", "[\xe7\x99\xbd\xe4\xba\xba]", "[\xe4\xb8\xad\xe5\x9b\xbd\xe4\xba\xba]", "[\xe3\x82\xa4\xe3\x83\xb3\xe3\x83\x89\xe4\xba\xba]", "[\xe3\x81\x8a\xe3\x81\x98\xe3\x81\x84\xe3\x81\x95\xe3\x82\x93]", "[\xe3\x81\x8a\xe3\x81\xb0\xe3\x81\x82\xe3\x81\x95\xe3\x82\x93]", "[\xe8\xb5\xa4\xe3\x81\xa1\xe3\x82\x83\xe3\x82\x93]", "[\xe5\xb7\xa5\xe4\xba\x8b\xe7\x8f\xbe\xe5\xa0\xb4\xe3\x81\xae\xe4\xba\xba]", "[\xe3\x81\x8a\xe5\xa7\xab\xe6\xa7\x98]", "[\xe3\x81\xaa\xe3\x81\xbe\xe3\x81\xaf\xe3\x81\x92]", "[\xe5\xa4\xa9\xe7\x8b\x97]", "[\xe3\x81\x8a\xe5\x8c\x96\xe3\x81\x91]", "[\xe5\xa4\xa9\xe4\xbd\xbf]", "[UFO]", "[\xe5\xae\x87\xe5\xae\x99\xe4\xba\xba]", "[\xe3\x82\xa2\xe3\x82\xaf\xe3\x83\x9e]", "[\xe3\x83\x89\xe3\x82\xaf\xe3\x83\xad]", "[\xe6\xa1\x88\xe5\x86\x85]", "[\xe8\xa1\x9b\xe5\x85\xb5]", "[\xe3\x83\x80\xe3\x83\xb3\xe3\x82\xb9]", "\xee\x9d\x8e", "[\xe3\x83\x98\xe3\x83\x93]", "\xee\x9d\x94", "[\xe3\x83\x8b\xe3\x83\xaf\xe3\x83\x88\xe3\x83\xaa]", "[\xe3\x82\xa4\xe3\x83\x8e\xe3\x82\xb7\xe3\x82\xb7]", "[\xe3\x83\xa9\xe3\x82\xaf\xe3\x83\x80]", "[\xe3\x82\xbe\xe3\x82\xa6]", "[\xe3\x82\xb3\xe3\x82\xa2\xe3\x83\xa9]", "[\xe3\x82\xb5\xe3\x83\xab]", "[\xe3\x83\x92\xe3\x83\x84\xe3\x82\xb8]", "[\xe3\x82\xbf\xe3\x82\xb3]", "[\xe5\xb7\xbb\xe8\xb2\x9d]", "[\xe3\x82\xb2\xe3\x82\xb8\xe3\x82\xb2\xe3\x82\xb8]", "[\xe3\x82\xa2\xe3\x83\xaa]", "[\xe3\x83\x9f\xe3\x83\x84\xe3\x83\x90\xe3\x83\x81]", "[\xe3\x81\xa6\xe3\x82\x93\xe3\x81\xa8\xe3\x81\x86\xe8\x99\xab]", "\xee\x9d\x91", "[\xe3\x82\xab\xe3\x83\xa1]", "\xee\x9d\x8f", "\xee\x9d\x90", "\xee\x9a\xa1", "[\xe3\x82\xa4\xe3\x83\xab\xe3\x82\xab]", "[\xe3\x83\x8d\xe3\x82\xba\xe3\x83\x9f]", "[\xe3\x83\x88\xe3\x83\xa9]", "\xee\x9a\xa2", "[\xe3\x82\xaf\xe3\x82\xb8\xe3\x83\xa9]", "\xee\x9d\x95", "[\xe3\x82\xaf\xe3\x83\x9e]", "[\xe3\x83\x8f\xe3\x83\xa0\xe3\x82\xb9\xe3\x82\xbf\xe3\x83\xbc]", "[\xe7\x89\x9b]", "[\x
|
|||
|
);
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
class WeChatHook {
|
|||
|
|
|||
|
public static function bindOpenId($uid, $openid, $isregister = 0) {
|
|||
|
C::t('#wechat#common_member_wechat')->insert(array('uid' => $uid, 'status' => 2, 'openid' => $openid, 'isregister' => $isregister), false, true);
|
|||
|
}
|
|||
|
|
|||
|
public static function updateAppInfo($extId, $appId = '', $appSecret = '') {
|
|||
|
global $_G;
|
|||
|
$wechatappInfos = unserialize($_G['setting']['wechatappInfos']);
|
|||
|
if ($appId) {
|
|||
|
$wechatappInfos[$extId] = array('appId' => $appId, 'appSecret' => $appSecret);
|
|||
|
} else {
|
|||
|
unset($wechatappInfos[$extId]);
|
|||
|
}
|
|||
|
$settings = array('wechatappInfos' => serialize($wechatappInfos));
|
|||
|
C::t('common_setting')->update_batch($settings);
|
|||
|
updatecache('setting');
|
|||
|
}
|
|||
|
|
|||
|
public static function getAppInfo($extId) {
|
|||
|
global $_G;
|
|||
|
$wechatappInfos = unserialize($_G['setting']['wechatappInfos']);
|
|||
|
if (isset($wechatappInfos[$extId])) {
|
|||
|
return $wechatappInfos[$extId];
|
|||
|
} else {
|
|||
|
return array();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public static function updateResponse($data, $extId = '') {
|
|||
|
$response = self::getResponse($extId);
|
|||
|
foreach ($data as $key => $value) {
|
|||
|
if ($value) {
|
|||
|
if ($value['plugin'] && $value['include'] && $value['class'] && $value['method']) {
|
|||
|
$response[$key] = $value;
|
|||
|
}
|
|||
|
} else {
|
|||
|
unset($response[$key]);
|
|||
|
}
|
|||
|
}
|
|||
|
if (!$extId) {
|
|||
|
$settings = array('wechatresponse' => serialize($response));
|
|||
|
} else {
|
|||
|
global $_G;
|
|||
|
$wechatresponseExts = unserialize($_G['setting']['wechatresponseExts']);
|
|||
|
if ($data) {
|
|||
|
$wechatresponseExts[$extId] = $response;
|
|||
|
} else {
|
|||
|
unset($wechatresponseExts[$extId]);
|
|||
|
}
|
|||
|
$settings = array('wechatresponseExts' => serialize($wechatresponseExts));
|
|||
|
}
|
|||
|
C::t('common_setting')->update_batch($settings);
|
|||
|
updatecache('setting');
|
|||
|
return $response;
|
|||
|
}
|
|||
|
|
|||
|
public static function getResponse($extId = '') {
|
|||
|
global $_G;
|
|||
|
if (!$extId) {
|
|||
|
return unserialize($_G['setting']['wechatresponse']);
|
|||
|
} else {
|
|||
|
$wechatresponseExts = unserialize($_G['setting']['wechatresponseExts']);
|
|||
|
return $wechatresponseExts[$extId];
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public static function updateRedirect($value) {
|
|||
|
if (!$value || $value['plugin'] && $value['include'] && $value['class'] && $value['method']) {
|
|||
|
$settings = array('wechatredirect' => $value);
|
|||
|
C::t('common_setting')->update_batch($settings);
|
|||
|
updatecache('setting');
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public static function getRedirect() {
|
|||
|
global $_G;
|
|||
|
return unserialize($_G['setting']['wechatredirect']);
|
|||
|
}
|
|||
|
|
|||
|
public static function getViewPluginId() {
|
|||
|
global $_G;
|
|||
|
return $_G['setting']['wechatviewpluginid'];
|
|||
|
}
|
|||
|
|
|||
|
public static function updateViewPluginId($value) {
|
|||
|
$settings = array('wechatviewpluginid' => $value);
|
|||
|
C::t('common_setting')->update_batch($settings);
|
|||
|
updatecache('setting');
|
|||
|
}
|
|||
|
|
|||
|
public static function updateAPIHook($datas) {
|
|||
|
$apihook = self::getAPIHook();
|
|||
|
foreach ($datas as $data) {
|
|||
|
foreach ($data as $key => $value) {
|
|||
|
if (!$value['plugin']) {
|
|||
|
continue;
|
|||
|
}
|
|||
|
list($module, $hookname) = explode('_', $key);
|
|||
|
if ($value['include'] && $value['class'] && $value['method']) {
|
|||
|
$v = $value;
|
|||
|
unset($v['plugin']);
|
|||
|
$v['allow'] = 1;
|
|||
|
$apihook[$module][$hookname][$value['plugin']] = $v;
|
|||
|
} else {
|
|||
|
unset($apihook[$module][$hookname][$value['plugin']]);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
$settings = array('mobileapihook' => serialize($apihook));
|
|||
|
C::t('common_setting')->update_batch($settings);
|
|||
|
updatecache('setting');
|
|||
|
return $apihook;
|
|||
|
}
|
|||
|
|
|||
|
public static function getAPIHook($getplugin = '') {
|
|||
|
global $_G;
|
|||
|
$data = unserialize($_G['setting']['mobileapihook']);
|
|||
|
if (!$getplugin) {
|
|||
|
return $data;
|
|||
|
} else {
|
|||
|
foreach ($data as $key => $hooknames) {
|
|||
|
foreach ($hooknames as $hookname => $plugins) {
|
|||
|
foreach ($plugins as $plugin => $value) {
|
|||
|
if ($getplugin != $plugin) {
|
|||
|
unset($data[$key][$hookname][$plugin]);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
return $data;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public static function delAPIHook($getplugin) {
|
|||
|
if (!$getplugin) {
|
|||
|
return;
|
|||
|
}
|
|||
|
$getplugins = (array) $getplugin;
|
|||
|
$apihook = self::getAPIHook();
|
|||
|
foreach ($apihook as $key => $hooknames) {
|
|||
|
foreach ($hooknames as $hookname => $plugins) {
|
|||
|
foreach ($plugins as $plugin => $value) {
|
|||
|
if (in_array($plugin, $getplugins)) {
|
|||
|
unset($apihook[$key][$hookname][$plugin]);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
$settings = array('mobileapihook' => serialize($apihook));
|
|||
|
C::t('common_setting')->update_batch($settings);
|
|||
|
updatecache('setting');
|
|||
|
return $apihook;
|
|||
|
}
|
|||
|
|
|||
|
public static function getPluginUrl($pluginid, $param = array()) {
|
|||
|
global $_G;
|
|||
|
if (in_array('plugin', $_G['setting']['rewritestatus'])) {
|
|||
|
$url = $_G['siteurl'] . rewriteoutput('plugin', 1, 'wechat', 'access') . '?';
|
|||
|
} else {
|
|||
|
$url = $_G['siteurl'] . 'plugin.php?id=wechat:access&';
|
|||
|
}
|
|||
|
$url .= 'pluginid=' . urlencode($pluginid) . '¶m=' . urlencode(base64_encode(http_build_query($param)));
|
|||
|
return $url;
|
|||
|
}
|
|||
|
|
|||
|
}
|