-
Notifications
You must be signed in to change notification settings - Fork 3
/
wechat.php
124 lines (109 loc) · 4.33 KB
/
wechat.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
// * This program is free software: you can redistribute it and/or modify
// * it under the terms of the GNU General Public License as published by
// * the Free Software Foundation, either version 3 of the License, or
// * (at your option) any later version.
// *
// * This program is distributed in the hope that it will be useful,
// * but WITHOUT ANY WARRANTY; without even the implied warranty of
// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// * GNU General Public License for more details.
// *
// * You should have received a copy of the GNU General Public License
// * along with this program. If not, see <http://www.gnu.org/licenses/>.
// *
// * @author Xianglong He
// * @copyright Copyright (c) 2015 Xianglong He. (http://tec.hxlxz.com)
// * @license http://www.gnu.org/licenses/ GPL v3
// * @version 1.0
// * @discribe RuiRuiQQ-微信主模块
// * 本文件使用了腾讯公司提供的样例代码
?>
<?php
//define your token
define("TOKEN", "KwYUS8EvoIYMPNGL4ChzMuCAo5vLDEc9");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->responseMsg();
//$wechatObj->valid();
class wechatCallbackapiTest
{
public function responseMsg()
{
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
libxml_disable_entity_loader(true);
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$time = time();
$msgType = "text";
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>0</FuncFlag>
</xml>";
require 'config.php';
require 'database.php';
$fromUsername = $mysqli->real_escape_string($postObj->FromUserName);
$toUsername = $mysqli->real_escape_string($postObj->ToUserName);
$CreateTime = $mysqli->real_escape_string($postObj->CreateTime);
$Content = $mysqli->real_escape_string($postObj->Content);
$MsgId = $mysqli->real_escape_string($postObj->MsgId);
$connect = new Memcache; //声明一个新的memcached链接
$connect->addServer($OCSServer, 11211);//添加实例地址 端口号
$contentStr = $connect->get('RuiRuiWechat_'.$postObj->MsgId);
if($contentStr != "")
{
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
die($resultStr);
}
require 'database.php';
//$sql = "INSERT INTO wechat (ToUserName, FromUserName, CreateTime, MsgType, Content, MsgId) VALUES ('$toUsername', '$fromUsername', '$postObj->CreateTime', '$postObj->MsgType', '$postObj->Content', '$postObj->MsgId')";
$sql = "INSERT INTO wechat (FromUserName, CreateTime, Content, MsgId) VALUES ('$fromUsername', '$CreateTime', '$Content', '$MsgId')";
$mysqli->query($sql);
$i = 50;
while($i > 0)
{
$i -= 1;
usleep(100000);
$contentStr = $connect->get('RuiRuiWechat_'.$postObj->MsgId);
if($contentStr != "")
{
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
die($resultStr);
}
}
//$contentStr = "RuiRui is testing, resived:\n".$postObj->Content;
}
private function checkSignature()
{
// you must define TOKEN by yourself
if (!defined("TOKEN")) {
throw new Exception('TOKEN is not defined!');
}
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
// use SORT_STRING rule
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
public function valid()
{
$echoStr = $_GET["echostr"];
//valid signature , option
if($this->checkSignature()){
echo $echoStr;
exit;
}
}
}
?>