forked from torrentpier/torrentpier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
poll.php
147 lines (127 loc) · 4.51 KB
/
poll.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
<?php
/**
* TorrentPier – Bull-powered BitTorrent tracker engine
*
* @copyright Copyright (c) 2005-2018 TorrentPier (https://torrentpier.com)
* @link https://github.com/torrentpier/torrentpier for the canonical source repository
* @license https://github.com/torrentpier/torrentpier/blob/master/LICENSE MIT License
*/
define('BB_SCRIPT', 'vote');
require __DIR__ . '/common.php';
$user->session_start(array('req_login' => true));
$mode = (string)@$_POST['mode'];
$topic_id = (int)@$_POST['topic_id'];
$forum_id = (int)@$_POST['forum_id'];
$vote_id = (int)@$_POST['vote_id'];
$return_topic_url = TOPIC_URL . $topic_id;
$return_topic_url .= !empty($_POST['start']) ? "&start=" . (int)$_POST['start'] : '';
set_die_append_msg($forum_id, $topic_id);
$poll = new TorrentPier\Legacy\Poll();
// проверка валидности $topic_id
if (!$topic_id) {
bb_die('Invalid topic_id');
}
if (!$t_data = DB()->fetch_row("SELECT * FROM " . BB_TOPICS . " WHERE topic_id = $topic_id LIMIT 1")) {
bb_die('Topic not found');
}
// проверка прав
if ($mode != 'poll_vote') {
if ($t_data['topic_poster'] != $userdata['user_id']) {
if (!IS_AM) {
bb_die($lang['NOT_AUTHORISED']);
}
}
}
// проверка на возможность вносить изменения
if ($mode == 'poll_delete') {
if ($t_data['topic_time'] < TIMENOW - $bb_cfg['poll_max_days'] * 86400) {
bb_die(sprintf($lang['NEW_POLL_DAYS'], $bb_cfg['poll_max_days']));
}
if (!IS_ADMIN && ($t_data['topic_vote'] != POLL_FINISHED)) {
bb_die($lang['CANNOT_DELETE_POLL']);
}
}
switch ($mode) {
// голосование
case 'poll_vote':
if (!$t_data['topic_vote']) {
bb_die($lang['POST_HAS_NO_POLL']);
}
if ($t_data['topic_status'] == TOPIC_LOCKED) {
bb_die($lang['TOPIC_LOCKED_SHORT']);
}
if (!poll_is_active($t_data)) {
bb_die($lang['NEW_POLL_ENDED']);
}
if (!$vote_id) {
bb_die($lang['NO_VOTE_OPTION']);
}
if (DB()->fetch_row("SELECT 1 FROM " . BB_POLL_USERS . " WHERE topic_id = $topic_id AND user_id = {$userdata['user_id']} LIMIT 1")) {
bb_die($lang['ALREADY_VOTED']);
}
DB()->query("
UPDATE " . BB_POLL_VOTES . " SET
vote_result = vote_result + 1
WHERE topic_id = $topic_id
AND vote_id = $vote_id
LIMIT 1
");
if (DB()->affected_rows() != 1) {
bb_die($lang['NO_VOTE_OPTION']);
}
DB()->query("INSERT IGNORE INTO " . BB_POLL_USERS . " (topic_id, user_id, vote_ip, vote_dt) VALUES ($topic_id, {$userdata['user_id']}, '" . USER_IP . "', " . TIMENOW . ")");
CACHE('bb_poll_data')->rm("poll_$topic_id");
bb_die($lang['VOTE_CAST']);
break;
// возобновить возможность голосовать
case 'poll_start':
if (!$t_data['topic_vote']) {
bb_die($lang['POST_HAS_NO_POLL']);
}
DB()->query("UPDATE " . BB_TOPICS . " SET topic_vote = 1 WHERE topic_id = $topic_id");
bb_die($lang['NEW_POLL_START']);
break;
// завершить опрос
case 'poll_finish':
if (!$t_data['topic_vote']) {
bb_die($lang['POST_HAS_NO_POLL']);
}
DB()->query("UPDATE " . BB_TOPICS . " SET topic_vote = " . POLL_FINISHED . " WHERE topic_id = $topic_id");
bb_die($lang['NEW_POLL_END']);
break;
// удаление
case 'poll_delete':
if (!$t_data['topic_vote']) {
bb_die($lang['POST_HAS_NO_POLL']);
}
$poll->delete_poll($topic_id);
bb_die($lang['NEW_POLL_DELETE']);
break;
// добавление
case 'poll_add':
if ($t_data['topic_vote']) {
bb_die($lang['NEW_POLL_ALREADY']);
}
$poll->build_poll_data($_POST);
if ($poll->err_msg) {
bb_die($poll->err_msg);
}
$poll->insert_votes_into_db($topic_id);
bb_die($lang['NEW_POLL_ADDED']);
break;
// редакторование
case 'poll_edit':
if (!$t_data['topic_vote']) {
bb_die($lang['POST_HAS_NO_POLL']);
}
$poll->build_poll_data($_POST);
if ($poll->err_msg) {
bb_die($poll->err_msg);
}
$poll->insert_votes_into_db($topic_id);
CACHE('bb_poll_data')->rm("poll_$topic_id");
bb_die($lang['NEW_POLL_RESULTS']);
break;
default:
bb_die('Invalid mode: ' . htmlCHR($mode));
}