|
|
@@ -1,4 +1,5 @@
|
|
1
|
1
|
<?php
|
|
|
2
|
+
|
|
2
|
3
|
// todo: Normalize thread_*_info don't need to waste all that ram on things that are already in other caches
|
|
3
|
4
|
/**********|| Page to show individual threads || ********************************\
|
|
4
|
5
|
|
|
|
@@ -13,81 +14,79 @@ Things to expect in $_GET:
|
|
13
|
14
|
|
|
14
|
15
|
// Check for lame SQL injection attempts
|
|
15
|
16
|
if (!isset($_GET['threadid']) || !is_number($_GET['threadid'])) {
|
|
16
|
|
- if (isset($_GET['topicid']) && is_number($_GET['topicid'])) {
|
|
17
|
|
- $ThreadID = $_GET['topicid'];
|
|
18
|
|
- } elseif (isset($_GET['postid']) && is_number($_GET['postid'])) {
|
|
19
|
|
- $DB->query("
|
|
|
17
|
+ if (isset($_GET['topicid']) && is_number($_GET['topicid'])) {
|
|
|
18
|
+ $ThreadID = $_GET['topicid'];
|
|
|
19
|
+ } elseif (isset($_GET['postid']) && is_number($_GET['postid'])) {
|
|
|
20
|
+ $DB->query("
|
|
20
|
21
|
SELECT TopicID
|
|
21
|
22
|
FROM forums_posts
|
|
22
|
23
|
WHERE ID = $_GET[postid]");
|
|
23
|
|
- list($ThreadID) = $DB->next_record();
|
|
24
|
|
- if ($ThreadID) {
|
|
25
|
|
- //Redirect postid to threadid when necessary.
|
|
26
|
|
- header("Location: ajax.php?action=forum&type=viewthread&threadid=$ThreadID&postid=$_GET[postid]");
|
|
27
|
|
- die();
|
|
|
24
|
+ list($ThreadID) = $DB->next_record();
|
|
|
25
|
+ if ($ThreadID) {
|
|
|
26
|
+ //Redirect postid to threadid when necessary.
|
|
|
27
|
+ header("Location: ajax.php?action=forum&type=viewthread&threadid=$ThreadID&postid=$_GET[postid]");
|
|
|
28
|
+ die();
|
|
|
29
|
+ } else {
|
|
|
30
|
+ print json_encode(array('status' => 'failure'));
|
|
|
31
|
+ die();
|
|
|
32
|
+ }
|
|
28
|
33
|
} else {
|
|
29
|
|
- print json_encode(array('status' => 'failure'));
|
|
30
|
|
- die();
|
|
|
34
|
+ print json_encode(array('status' => 'failure'));
|
|
|
35
|
+ die();
|
|
31
|
36
|
}
|
|
32
|
|
- } else {
|
|
33
|
|
- print json_encode(array('status' => 'failure'));
|
|
34
|
|
- die();
|
|
35
|
|
- }
|
|
36
|
37
|
} else {
|
|
37
|
|
- $ThreadID = $_GET['threadid'];
|
|
|
38
|
+ $ThreadID = $_GET['threadid'];
|
|
38
|
39
|
}
|
|
39
|
40
|
|
|
40
|
41
|
if (isset($_GET['pp'])) {
|
|
41
|
|
- $PerPage = $_GET['pp'];
|
|
|
42
|
+ $PerPage = $_GET['pp'];
|
|
42
|
43
|
} elseif (isset($LoggedUser['PostsPerPage'])) {
|
|
43
|
|
- $PerPage = $LoggedUser['PostsPerPage'];
|
|
|
44
|
+ $PerPage = $LoggedUser['PostsPerPage'];
|
|
44
|
45
|
} else {
|
|
45
|
|
- $PerPage = POSTS_PER_PAGE;
|
|
|
46
|
+ $PerPage = POSTS_PER_PAGE;
|
|
46
|
47
|
}
|
|
47
|
48
|
|
|
48
|
|
-
|
|
49
|
|
-
|
|
50
|
49
|
//---------- Get some data to start processing
|
|
51
|
50
|
|
|
52
|
51
|
// Thread information, constant across all pages
|
|
53
|
52
|
$ThreadInfo = Forums::get_thread_info($ThreadID, true, true);
|
|
54
|
53
|
if ($ThreadInfo === null) {
|
|
55
|
|
- json_die('failure', 'no such thread exists');
|
|
|
54
|
+ json_die('failure', 'no such thread exists');
|
|
56
|
55
|
}
|
|
57
|
56
|
$ForumID = $ThreadInfo['ForumID'];
|
|
58
|
57
|
|
|
59
|
58
|
// Make sure they're allowed to look at the page
|
|
60
|
59
|
if (!Forums::check_forumperm($ForumID)) {
|
|
61
|
|
- print json_encode(array('status' => 'failure'));
|
|
62
|
|
- die();
|
|
|
60
|
+ print json_encode(array('status' => 'failure'));
|
|
|
61
|
+ die();
|
|
63
|
62
|
}
|
|
64
|
63
|
|
|
65
|
|
-//Post links utilize the catalogue & key params to prevent issues with custom posts per page
|
|
|
64
|
+// Post links utilize the catalogue & key params to prevent issues with custom posts per page
|
|
66
|
65
|
if ($ThreadInfo['Posts'] > $PerPage) {
|
|
67
|
|
- if (isset($_GET['post']) && is_number($_GET['post'])) {
|
|
68
|
|
- $PostNum = $_GET['post'];
|
|
69
|
|
- } elseif (isset($_GET['postid']) && is_number($_GET['postid'])) {
|
|
70
|
|
- $DB->query("
|
|
|
66
|
+ if (isset($_GET['post']) && is_number($_GET['post'])) {
|
|
|
67
|
+ $PostNum = $_GET['post'];
|
|
|
68
|
+ } elseif (isset($_GET['postid']) && is_number($_GET['postid'])) {
|
|
|
69
|
+ $DB->query("
|
|
71
|
70
|
SELECT COUNT(ID)
|
|
72
|
71
|
FROM forums_posts
|
|
73
|
72
|
WHERE TopicID = $ThreadID
|
|
74
|
73
|
AND ID <= $_GET[postid]");
|
|
75
|
|
- list($PostNum) = $DB->next_record();
|
|
76
|
|
- } else {
|
|
77
|
|
- $PostNum = 1;
|
|
78
|
|
- }
|
|
|
74
|
+ list($PostNum) = $DB->next_record();
|
|
|
75
|
+ } else {
|
|
|
76
|
+ $PostNum = 1;
|
|
|
77
|
+ }
|
|
79
|
78
|
} else {
|
|
80
|
|
- $PostNum = 1;
|
|
|
79
|
+ $PostNum = 1;
|
|
81
|
80
|
}
|
|
82
|
81
|
list($Page, $Limit) = Format::page_limit($PerPage, min($ThreadInfo['Posts'], $PostNum));
|
|
83
|
82
|
if (($Page - 1) * $PerPage > $ThreadInfo['Posts']) {
|
|
84
|
|
- $Page = ceil($ThreadInfo['Posts'] / $PerPage);
|
|
|
83
|
+ $Page = ceil($ThreadInfo['Posts'] / $PerPage);
|
|
85
|
84
|
}
|
|
86
|
|
-list($CatalogueID,$CatalogueLimit) = Format::catalogue_limit($Page, $PerPage, THREAD_CATALOGUE);
|
|
|
85
|
+list($CatalogueID, $CatalogueLimit) = Format::catalogue_limit($Page, $PerPage, THREAD_CATALOGUE);
|
|
87
|
86
|
|
|
88
|
87
|
// Cache catalogue from which the page is selected, allows block caches and future ability to specify posts per page
|
|
89
|
88
|
if (!$Catalogue = $Cache->get_value("thread_$ThreadID"."_catalogue_$CatalogueID")) {
|
|
90
|
|
- $DB->query("
|
|
|
89
|
+ $DB->query("
|
|
91
|
90
|
SELECT
|
|
92
|
91
|
p.ID,
|
|
93
|
92
|
p.AuthorID,
|
|
|
@@ -99,155 +98,153 @@ if (!$Catalogue = $Cache->get_value("thread_$ThreadID"."_catalogue_$CatalogueID"
|
|
99
|
98
|
WHERE p.TopicID = '$ThreadID'
|
|
100
|
99
|
AND p.ID != '".$ThreadInfo['StickyPostID']."'
|
|
101
|
100
|
LIMIT $CatalogueLimit");
|
|
102
|
|
- $Catalogue = $DB->to_array(false, MYSQLI_ASSOC);
|
|
103
|
|
- if (!$ThreadInfo['IsLocked'] || $ThreadInfo['IsSticky']) {
|
|
104
|
|
- $Cache->cache_value("thread_$ThreadID"."_catalogue_$CatalogueID", $Catalogue, 0);
|
|
105
|
|
- }
|
|
|
101
|
+ $Catalogue = $DB->to_array(false, MYSQLI_ASSOC);
|
|
|
102
|
+ if (!$ThreadInfo['IsLocked'] || $ThreadInfo['IsSticky']) {
|
|
|
103
|
+ $Cache->cache_value("thread_$ThreadID"."_catalogue_$CatalogueID", $Catalogue, 0);
|
|
|
104
|
+ }
|
|
106
|
105
|
}
|
|
107
|
106
|
$Thread = Format::catalogue_select($Catalogue, $Page, $PerPage, THREAD_CATALOGUE);
|
|
108
|
107
|
|
|
109
|
108
|
if ($_GET['updatelastread'] !== '0') {
|
|
110
|
|
- $LastPost = end($Thread);
|
|
111
|
|
- $LastPost = $LastPost['ID'];
|
|
112
|
|
- reset($Thread);
|
|
113
|
|
- if ($ThreadInfo['Posts'] <= $PerPage * $Page && $ThreadInfo['StickyPostID'] > $LastPost) {
|
|
114
|
|
- $LastPost = $ThreadInfo['StickyPostID'];
|
|
115
|
|
- }
|
|
116
|
|
- //Handle last read
|
|
117
|
|
- if (!$ThreadInfo['IsLocked'] || $ThreadInfo['IsSticky']) {
|
|
118
|
|
- $DB->query("
|
|
|
109
|
+ $LastPost = end($Thread);
|
|
|
110
|
+ $LastPost = $LastPost['ID'];
|
|
|
111
|
+ reset($Thread);
|
|
|
112
|
+ if ($ThreadInfo['Posts'] <= $PerPage * $Page && $ThreadInfo['StickyPostID'] > $LastPost) {
|
|
|
113
|
+ $LastPost = $ThreadInfo['StickyPostID'];
|
|
|
114
|
+ }
|
|
|
115
|
+ // Handle last read
|
|
|
116
|
+ if (!$ThreadInfo['IsLocked'] || $ThreadInfo['IsSticky']) {
|
|
|
117
|
+ $DB->query("
|
|
119
|
118
|
SELECT PostID
|
|
120
|
119
|
FROM forums_last_read_topics
|
|
121
|
120
|
WHERE UserID = '$LoggedUser[ID]'
|
|
122
|
121
|
AND TopicID = '$ThreadID'");
|
|
123
|
|
- list($LastRead) = $DB->next_record();
|
|
124
|
|
- if ($LastRead < $LastPost) {
|
|
125
|
|
- $DB->query("
|
|
|
122
|
+ list($LastRead) = $DB->next_record();
|
|
|
123
|
+ if ($LastRead < $LastPost) {
|
|
|
124
|
+ $DB->query("
|
|
126
|
125
|
INSERT INTO forums_last_read_topics
|
|
127
|
126
|
(UserID, TopicID, PostID)
|
|
128
|
127
|
VALUES
|
|
129
|
128
|
('$LoggedUser[ID]', '$ThreadID', '".db_string($LastPost)."')
|
|
130
|
129
|
ON DUPLICATE KEY UPDATE
|
|
131
|
130
|
PostID = '$LastPost'");
|
|
|
131
|
+ }
|
|
132
|
132
|
}
|
|
133
|
|
- }
|
|
134
|
133
|
}
|
|
135
|
134
|
|
|
136
|
|
-//Handle subscriptions
|
|
|
135
|
+// Handle subscriptions
|
|
137
|
136
|
$UserSubscriptions = Subscriptions::get_subscriptions();
|
|
138
|
137
|
|
|
139
|
138
|
if (empty($UserSubscriptions)) {
|
|
140
|
|
- $UserSubscriptions = [];
|
|
|
139
|
+ $UserSubscriptions = [];
|
|
141
|
140
|
}
|
|
142
|
141
|
|
|
143
|
142
|
if (in_array($ThreadID, $UserSubscriptions)) {
|
|
144
|
|
- $Cache->delete_value('subscriptions_user_new_'.$LoggedUser['ID']);
|
|
|
143
|
+ $Cache->delete_value('subscriptions_user_new_'.$LoggedUser['ID']);
|
|
145
|
144
|
}
|
|
146
|
145
|
|
|
147
|
146
|
$JsonPoll = [];
|
|
148
|
147
|
if ($ThreadInfo['NoPoll'] == 0) {
|
|
149
|
|
- if (!list($Question, $Answers, $Votes, $Featured, $Closed) = $Cache->get_value("polls_$ThreadID")) {
|
|
150
|
|
- $DB->query("
|
|
|
148
|
+ if (!list($Question, $Answers, $Votes, $Featured, $Closed) = $Cache->get_value("polls_$ThreadID")) {
|
|
|
149
|
+ $DB->query("
|
|
151
|
150
|
SELECT Question, Answers, Featured, Closed
|
|
152
|
151
|
FROM forums_polls
|
|
153
|
152
|
WHERE TopicID = '$ThreadID'");
|
|
154
|
|
- list($Question, $Answers, $Featured, $Closed) = $DB->next_record(MYSQLI_NUM, array(1));
|
|
155
|
|
- $Answers = unserialize($Answers);
|
|
156
|
|
- $DB->query("
|
|
|
153
|
+ list($Question, $Answers, $Featured, $Closed) = $DB->next_record(MYSQLI_NUM, array(1));
|
|
|
154
|
+ $Answers = unserialize($Answers);
|
|
|
155
|
+ $DB->query("
|
|
157
|
156
|
SELECT Vote, COUNT(UserID)
|
|
158
|
157
|
FROM forums_polls_votes
|
|
159
|
158
|
WHERE TopicID = '$ThreadID'
|
|
160
|
159
|
GROUP BY Vote");
|
|
161
|
|
- $VoteArray = $DB->to_array(false, MYSQLI_NUM);
|
|
|
160
|
+ $VoteArray = $DB->to_array(false, MYSQLI_NUM);
|
|
162
|
161
|
|
|
163
|
|
- $Votes = [];
|
|
164
|
|
- foreach ($VoteArray as $VoteSet) {
|
|
165
|
|
- list($Key, $Value) = $VoteSet;
|
|
166
|
|
- $Votes[$Key] = $Value;
|
|
167
|
|
- }
|
|
|
162
|
+ $Votes = [];
|
|
|
163
|
+ foreach ($VoteArray as $VoteSet) {
|
|
|
164
|
+ list($Key, $Value) = $VoteSet;
|
|
|
165
|
+ $Votes[$Key] = $Value;
|
|
|
166
|
+ }
|
|
168
|
167
|
|
|
169
|
|
- foreach (array_keys($Answers) as $i) {
|
|
170
|
|
- if (!isset($Votes[$i])) {
|
|
171
|
|
- $Votes[$i] = 0;
|
|
172
|
|
- }
|
|
|
168
|
+ foreach (array_keys($Answers) as $i) {
|
|
|
169
|
+ if (!isset($Votes[$i])) {
|
|
|
170
|
+ $Votes[$i] = 0;
|
|
|
171
|
+ }
|
|
|
172
|
+ }
|
|
|
173
|
+ $Cache->cache_value("polls_$ThreadID", array($Question, $Answers, $Votes, $Featured, $Closed), 0);
|
|
173
|
174
|
}
|
|
174
|
|
- $Cache->cache_value("polls_$ThreadID", array($Question, $Answers, $Votes, $Featured, $Closed), 0);
|
|
175
|
|
- }
|
|
176
|
175
|
|
|
177
|
|
- if (!empty($Votes)) {
|
|
178
|
|
- $TotalVotes = array_sum($Votes);
|
|
179
|
|
- $MaxVotes = max($Votes);
|
|
180
|
|
- } else {
|
|
181
|
|
- $TotalVotes = 0;
|
|
182
|
|
- $MaxVotes = 0;
|
|
183
|
|
- }
|
|
|
176
|
+ if (!empty($Votes)) {
|
|
|
177
|
+ $TotalVotes = array_sum($Votes);
|
|
|
178
|
+ $MaxVotes = max($Votes);
|
|
|
179
|
+ } else {
|
|
|
180
|
+ $TotalVotes = 0;
|
|
|
181
|
+ $MaxVotes = 0;
|
|
|
182
|
+ }
|
|
184
|
183
|
|
|
185
|
|
- $RevealVoters = in_array($ForumID, FORUMS_TO_REVEAL_VOTERS);
|
|
186
|
|
- //Polls lose the you voted arrow thingy
|
|
187
|
|
- $DB->query("
|
|
|
184
|
+ $RevealVoters = in_array($ForumID, FORUMS_TO_REVEAL_VOTERS);
|
|
|
185
|
+ //Polls lose the you voted arrow thingy
|
|
|
186
|
+ $DB->query("
|
|
188
|
187
|
SELECT Vote
|
|
189
|
188
|
FROM forums_polls_votes
|
|
190
|
189
|
WHERE UserID = '".$LoggedUser['ID']."'
|
|
191
|
190
|
AND TopicID = '$ThreadID'");
|
|
192
|
|
- list($UserResponse) = $DB->next_record();
|
|
193
|
|
- if (!empty($UserResponse) && $UserResponse != 0) {
|
|
194
|
|
- $Answers[$UserResponse] = '» '.$Answers[$UserResponse];
|
|
195
|
|
- } else {
|
|
196
|
|
- if (!empty($UserResponse) && $RevealVoters) {
|
|
197
|
|
- $Answers[$UserResponse] = '» '.$Answers[$UserResponse];
|
|
|
191
|
+ list($UserResponse) = $DB->next_record();
|
|
|
192
|
+ if (!empty($UserResponse) && $UserResponse != 0) {
|
|
|
193
|
+ $Answers[$UserResponse] = '» '.$Answers[$UserResponse];
|
|
|
194
|
+ } else {
|
|
|
195
|
+ if (!empty($UserResponse) && $RevealVoters) {
|
|
|
196
|
+ $Answers[$UserResponse] = '» '.$Answers[$UserResponse];
|
|
|
197
|
+ }
|
|
198
|
198
|
}
|
|
199
|
|
- }
|
|
200
|
199
|
|
|
201
|
|
- $JsonPoll['closed'] = ($Closed == 1);
|
|
202
|
|
- $JsonPoll['featured'] = $Featured;
|
|
203
|
|
- $JsonPoll['question'] = $Question;
|
|
204
|
|
- $JsonPoll['maxVotes'] = (int)$MaxVotes;
|
|
205
|
|
- $JsonPoll['totalVotes'] = $TotalVotes;
|
|
206
|
|
- $JsonPollAnswers = [];
|
|
|
200
|
+ $JsonPoll['closed'] = ($Closed == 1);
|
|
|
201
|
+ $JsonPoll['featured'] = $Featured;
|
|
|
202
|
+ $JsonPoll['question'] = $Question;
|
|
|
203
|
+ $JsonPoll['maxVotes'] = (int)$MaxVotes;
|
|
|
204
|
+ $JsonPoll['totalVotes'] = $TotalVotes;
|
|
|
205
|
+ $JsonPollAnswers = [];
|
|
207
|
206
|
|
|
208
|
|
- foreach ($Answers as $i => $Answer) {
|
|
209
|
|
- if (!empty($Votes[$i]) && $TotalVotes > 0) {
|
|
210
|
|
- $Ratio = $Votes[$i] / $MaxVotes;
|
|
211
|
|
- $Percent = $Votes[$i] / $TotalVotes;
|
|
212
|
|
- } else {
|
|
213
|
|
- $Ratio = 0;
|
|
214
|
|
- $Percent = 0;
|
|
215
|
|
- }
|
|
216
|
|
- $JsonPollAnswers[] = array(
|
|
|
207
|
+ foreach ($Answers as $i => $Answer) {
|
|
|
208
|
+ if (!empty($Votes[$i]) && $TotalVotes > 0) {
|
|
|
209
|
+ $Ratio = $Votes[$i] / $MaxVotes;
|
|
|
210
|
+ $Percent = $Votes[$i] / $TotalVotes;
|
|
|
211
|
+ } else {
|
|
|
212
|
+ $Ratio = 0;
|
|
|
213
|
+ $Percent = 0;
|
|
|
214
|
+ }
|
|
|
215
|
+ $JsonPollAnswers[] = array(
|
|
217
|
216
|
'answer' => $Answer,
|
|
218
|
217
|
'ratio' => $Ratio,
|
|
219
|
218
|
'percent' => $Percent
|
|
220
|
219
|
);
|
|
221
|
|
- }
|
|
|
220
|
+ }
|
|
222
|
221
|
|
|
223
|
|
- if ($UserResponse !== null || $Closed || $ThreadInfo['IsLocked'] || $LoggedUser['Class'] < $Forums[$ForumID]['MinClassWrite']) {
|
|
224
|
|
- $JsonPoll['voted'] = True;
|
|
225
|
|
- } else {
|
|
226
|
|
- $JsonPoll['voted'] = False;
|
|
227
|
|
- }
|
|
|
222
|
+ if ($UserResponse !== null || $Closed || $ThreadInfo['IsLocked'] || $LoggedUser['Class'] < $Forums[$ForumID]['MinClassWrite']) {
|
|
|
223
|
+ $JsonPoll['voted'] = true;
|
|
|
224
|
+ } else {
|
|
|
225
|
+ $JsonPoll['voted'] = false;
|
|
|
226
|
+ }
|
|
228
|
227
|
|
|
229
|
|
- $JsonPoll['answers'] = $JsonPollAnswers;
|
|
|
228
|
+ $JsonPoll['answers'] = $JsonPollAnswers;
|
|
230
|
229
|
}
|
|
231
|
230
|
|
|
232
|
|
-//Sqeeze in stickypost
|
|
|
231
|
+// Sqeeze in stickypost
|
|
233
|
232
|
if ($ThreadInfo['StickyPostID']) {
|
|
234
|
|
- if ($ThreadInfo['StickyPostID'] != $Thread[0]['ID']) {
|
|
235
|
|
- array_unshift($Thread, $ThreadInfo['StickyPost']);
|
|
236
|
|
- }
|
|
237
|
|
- if ($ThreadInfo['StickyPostID'] != $Thread[count($Thread) - 1]['ID']) {
|
|
238
|
|
- $Thread[] = $ThreadInfo['StickyPost'];
|
|
239
|
|
- }
|
|
|
233
|
+ if ($ThreadInfo['StickyPostID'] != $Thread[0]['ID']) {
|
|
|
234
|
+ array_unshift($Thread, $ThreadInfo['StickyPost']);
|
|
|
235
|
+ }
|
|
|
236
|
+ if ($ThreadInfo['StickyPostID'] != $Thread[count($Thread) - 1]['ID']) {
|
|
|
237
|
+ $Thread[] = $ThreadInfo['StickyPost'];
|
|
|
238
|
+ }
|
|
240
|
239
|
}
|
|
241
|
240
|
|
|
242
|
241
|
$JsonPosts = [];
|
|
243
|
242
|
foreach ($Thread as $Key => $Post) {
|
|
244
|
|
- list($PostID, $AuthorID, $AddedTime, $Body, $EditedUserID, $EditedTime) = array_values($Post);
|
|
245
|
|
- list($AuthorID, $Username, $PermissionID, $Paranoia, $Artist, $Donor, $Warned, $Avatar, $Enabled, $UserTitle) = array_values(Users::user_info($AuthorID));
|
|
246
|
|
-
|
|
247
|
|
-
|
|
|
243
|
+ list($PostID, $AuthorID, $AddedTime, $Body, $EditedUserID, $EditedTime) = array_values($Post);
|
|
|
244
|
+ list($AuthorID, $Username, $PermissionID, $Paranoia, $Artist, $Donor, $Warned, $Avatar, $Enabled, $UserTitle) = array_values(Users::user_info($AuthorID));
|
|
248
|
245
|
|
|
249
|
|
- $UserInfo = Users::user_info($EditedUserID);
|
|
250
|
|
- $JsonPosts[] = [
|
|
|
246
|
+ $UserInfo = Users::user_info($EditedUserID);
|
|
|
247
|
+ $JsonPosts[] = [
|
|
251
|
248
|
'postId' => (int)$PostID,
|
|
252
|
249
|
'addedTime' => $AddedTime,
|
|
253
|
250
|
'bbBody' => $Body,
|