-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
211 lines (173 loc) · 8.73 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
header("Content-type:text/html;charset=utf-8");
define('VOTE_LIMIT', 3);
define('ON_VOTE', true);
function checkSignature() {
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = "whatthefackand";
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING);
$tmpStr = implode($tmpArr);
$tmpStr = sha1($tmpStr);
if ($tmpStr == $signature) {
return true;
} else {
return false;
}
}
function toUtf($s) {
return mb_convert_encoding($s, "utf-8");
}
///////////////////////////////////////////////////////////
function createResponse() {
/*/////////////////////////////////////////////////////////
接入公众号时使用
if(checkSignature()) {
return $_GET["echostr"];
}
/////////////////////////////////////////////////////////*/
$xmlStr = $GLOBALS["HTTP_RAW_POST_DATA"];
$xml = simplexml_load_string($xmlStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$response = simplexml_load_string("<xml></xml>");
$response->addChild("ToUserName", $xml->FromUserName);
$response->addChild("FromUserName", $xml->ToUserName);
$response->addChild("CreateTime", time());
$response->addChild("MsgType", "text");
if ($xml->MsgType == "event" && $xml->Event == "subscribe") {
$response->addChild("Content", toUtf("欢迎关注 Maxcell 资源在线协会!"));
return $response->asXML();
} else if ($xml->MsgType == "text") {
//bulid database.php yourself
require_once "database.php";
$db_mysqli = new mysqli($db_hostname, $db_user, $db_password, $db_database);
if (!isset($db_mysqli)) {
$response->addChild("Content", toUtf("服务器出错"));
return $response->asXML();
}
$query = preg_replace("/\s(?=\s)/", "\\1", $xml->Content);
$query = trim($query);
$query = explode(" ", $query, 2);
if ($query && $query[0] == "投票") {
if(!ON_VOTE) {
$response->addChild("Content", toUtf("已停止投票,谢谢参与!"));
return $response->asXML();
}
//show list
if ((count($query) == 1 || !isset($query[1])) && $query[0] == "投票") {
$result = $db_mysqli->query("select * from Vote");
$content = toUtf("输入投票和队伍编号可参与投票,如“投票 1 2 3”。(请注意空格)\n\n参赛队伍及作品:") . "\n";
while ($row = $result->fetch_object()) {
$content .= $row->tid . "." . toUtf($row->name) . " - <" . toUtf($row->work) . ">\n";
}
$response->addChild("Content", $content);
return $response->asXML();
}
if ($query[0] == "投票") {
$result = $db_mysqli->query("select * from User where uid = '" . (string)$xml->FromUserName . "'");
$voted_times = $result->num_rows;
if (isset($voted_times) && $voted_times >= VOTE_LIMIT) {
$response->addChild("Content", toUtf("您已经投") . $voted_times . toUtf("次票了,一人只能投")
. VOTE_LIMIT . toUtf("次,谢谢参与!"));
return $response->asXML();
}
$id = explode(" ", $query[1]);
if ((isset($voted_times) ? $voted_times : 0) + count($id) > VOTE_LIMIT) {
$response->addChild("Content", toUtf("您已经投") . (isset($voted_times) ? $voted_times : 0)
. toUtf("次票了,一人只能投") . VOTE_LIMIT . toUtf("次,谢谢参与!"));
return $response->asXML();
}
$count_id = count($id);
if (count(array_unique($id)) != $count_id) {
$response->addChild("Content", toUtf("不能重复投一队,谢谢参与!"));
return $response->asXML();
}
if (isset($voted_times) && $voted_times != 0) {
while ($row = $result->fetch_object()) {
if (in_array($row->tid, $id)) {
$response->addChild("Content", toUtf("不能重复投一队,谢谢参与!"));
return $response->asXML();
}
}
}
if ($count_id != 0) {
foreach ($id as $i) {
if (!($i >= 1 && $i <= 10)) {
$content = toUtf("队伍编号不合法\n\n");
$result = $db_mysqli->query("select * from Vote");
$content .= toUtf("参赛队伍及作品:\n");
while ($row = $result->fetch_object()) {
$content .= $row->tid . "." . toUtf($row->name) . " - <" . toUtf($row->work) . ">\n";
}
$response->addChild("Content", $content);
return $response->asXML();
}
}
foreach ($id as $i) {
$db_mysqli->query("update Vote SET voted = voted + 1 WHERE tid = '" . $i . "'");
$db_mysqli->query("insert into User (uid, tid) values('" . (string)$xml->FromUserName
. "', '$i')");
}
$content = toUtf("投票成功!\n\n");
$content .= toUtf("你投了 ");
foreach ($id as $i) {
$content .= $i . " ";
}
$content .= toUtf("号队伍,谢谢您的参与!");
$response->addChild("Content", $content);
return $response->asXML();
} else {
$result = $db_mysqli->query("select * from Vote");
$content = toUtf("参赛队伍及作品:") . "\n";
while ($row = $result->fetch_object()) {
$content .= $row->tid . ". " . toUtf($row->name) . " - < " . toUtf($row->work) . " >\n";
}
$response->addChild("Content", $content);
return $response->asXML();
}
}
$db_mysqli->close();
} else if ($xml->Content == "节目单") {
$response->MsgType = "news";
$response->addChild("ArticleCount", "1");
$response->addChild("Articles", "");
$response->Articles->addChild("item", "");
$response->Articles->item->addChild("Title", toUtf("橙名夜节目单"), "UTF-8");
$content = toUtf("1227 橙名夜节目单\n");
$response->Articles->item->addChild("Description", $content);
$response->Articles->item->addChild("PicUrl",
"http://maxcellweixin.sinaapp.com/webShow/vote/images/head.jpg");
$response->Articles->item->addChild("Url", "http://www.dwz.cn/showList");
return $response->asXML();
} else if ($xml->Content == "弹幕") {
$response->addChild("Content", toUtf("点击下面的链接发弹幕!")
. "\n\nhttp://danmu.maxcell.com.cn/");
return $response->asXML();
} else if($xml->Content == "橙名夜"){
$content = toUtf("欢迎来到橙名夜!\n\n输入投票和队伍编号可参与投票,如“投票 1 2 3”。\n\n"
. "输入“弹幕”可获取弹幕发送链接。\n\n输入“节目单”可查看橙名夜节目单。\n\n");
$result = $db_mysqli->query("select * from Vote");
$content .= toUtf("参赛队伍及作品:") . "\n";
while ($row = $result->fetch_object()) {
$content .= $row->tid . "." . toUtf($row->name) . " - <" . toUtf($row->work) . ">\n";
}
$response->addChild("Content", $content);
return $response->asXML();
} else {
$content = toUtf("输入格式有误!\n\n输入投票和队伍编号可参与投票,如“投票 1 2 3”。\n\n"
. "输入“弹幕”可获取弹幕发送链接。\n\n输入“节目单”可查看橙名夜节目单。\n\n");
$result = $db_mysqli->query("select * from Vote");
$content .= toUtf("参赛队伍及作品:") . "\n";
while ($row = $result->fetch_object()) {
$content .= $row->tid . "." . toUtf($row->name) . " - <" . toUtf($row->work) . ">\n";
}
$response->addChild("Content", $content);
return $response->asXML();
}
} else {
return "";
}
}
echo createResponse();
?>