-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
151 lines (133 loc) · 4.95 KB
/
index.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
require_once './sql.php';
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->responseMsg();
class wechatCallbackapiTest
{
public function valid()
{
$echoStr = $_GET["echostr"];
//valid signature , option
if($this->checkSignature()){
echo $echoStr;
exit;
}
}
public function responseMsg()
{
//get post data, May be due to the different environments
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
//extract post data
if (!empty($postStr))
{
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$keyword = trim($postObj->Content);
$time = time();
$event = $postObj->Event;
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
$newsTpl1 = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<ArticleCount>1</ArticleCount>
<Articles>
<item>
<Title><![CDATA[xxx]]></Title>
<Description><![CDATA[xxx]]></Description>
<PicUrl><![CDATA[http://xxx/images/fm.jpg]]></PicUrl>
<Url><![CDATA[http://xxx/index.php?openid=".$postObj->FromUserName."]]></Url>
</item>
</Articles>
</xml> ";
if (!empty($event))
{
$sql = "insert into score (`id`,`fenshu`,`paiming`) VALUES ('$fromUsername',1,0)";
_insert_data($sql);
$resultStr = sprintf($newsTpl1, $fromUsername, $toUsername, $time);
echo $resultStr;
}
if (empty($user_flag))
{
if($keyword == "xx")
{
$sql = "SELECT fenshu FROM score WHERE id = '$fromUsername'";
$result=_select_data($sql);
$n=mysql_fetch_array($result);
$q = $n['fenshu'];
if($q==100)
{
$sql = "SELECT paiming FROM score WHERE id='$fromUsername'";
$result=_select_data($sql);
$n=mysql_fetch_array($result);
$p = $n['paiming'];
//$contentStr=$p;
if($p==0)
{
$sql = "SELECT count(id) as shu FROM score WHERE fenshu = '100'";
$result=_select_data($sql);
$n=mysql_fetch_array($result);
$q = $n['shu'];
// $n=mysql_fetch_array($result);
//$q = $n['fenshu'];
//$contentStr=$q;
$contentStr='xxx';
$sql = "UPDATE `score` SET `paiming`= '$q' where id = '$fromUsername'";
_update_data($sql);
}
else
{
$contentStr='xxx';
}
}
else if($q==0)
{
$contentStr='xxx';
}
else
{
$contentStr='xxx';
}
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $contentStr);
echo $resultStr;
}
if (!empty($sql))
{
_insert_data($sql);
}
}
else
{
}
}else {
echo "";
exit;
}
}
private function checkSignature()
{
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
}
?>