Gazelle/sections/ajax/forum/thread.php

293 lines
8.7 KiB
PHP
Raw Normal View History

2013-05-28 08:01:02 +00:00
<?php
2011-10-31 08:00:12 +00:00
//TODO: Normalize thread_*_info don't need to waste all that ram on things that are already in other caches
/**********|| Page to show individual threads || ********************************\
Things to expect in $_GET:
ThreadID: ID of the forum curently being browsed
page: The page the user's on.
page = 1 is the same as no page
********************************************************************************/
//---------- Things to sort out before it can start printing/generating content
2013-05-27 08:00:58 +00:00
include(SERVER_ROOT.'/classes/text.class.php');
2011-10-31 08:00:12 +00:00
$Text = new TEXT;
// Check for lame SQL injection attempts
2013-04-13 08:00:19 +00:00
if (!isset($_GET['threadid']) || !is_number($_GET['threadid'])) {
if (isset($_GET['topicid']) && is_number($_GET['topicid'])) {
2011-10-31 08:00:12 +00:00
$ThreadID = $_GET['topicid'];
2013-04-13 08:00:19 +00:00
} elseif (isset($_GET['postid']) && is_number($_GET['postid'])) {
2011-10-31 08:00:12 +00:00
$DB->query("SELECT TopicID FROM forums_posts WHERE ID = $_GET[postid]");
list($ThreadID) = $DB->next_record();
2013-04-13 08:00:19 +00:00
if ($ThreadID) {
2013-05-14 08:00:34 +00:00
//Redirect postid to threadid when necessary.
2011-10-31 08:00:12 +00:00
header("Location: ajax.php?action=forum&type=viewthread&threadid=$ThreadID&postid=$_GET[postid]");
die();
} else {
print json_encode(array('status' => 'failure'));
2012-02-09 08:00:20 +00:00
die();
2011-10-31 08:00:12 +00:00
}
} else {
print json_encode(array('status' => 'failure'));
2012-02-09 08:00:20 +00:00
die();
2011-10-31 08:00:12 +00:00
}
} else {
$ThreadID = $_GET['threadid'];
}
if (isset($_GET['pp'])) {
$PerPage = $_GET['pp'];
2013-04-13 08:00:19 +00:00
} elseif (isset($LoggedUser['PostsPerPage'])) {
2011-10-31 08:00:12 +00:00
$PerPage = $LoggedUser['PostsPerPage'];
} else {
$PerPage = POSTS_PER_PAGE;
}
//---------- Get some data to start processing
// Thread information, constant across all pages
2013-04-24 08:00:23 +00:00
$ThreadInfo = get_thread_info($ThreadID, true, true, true);
2013-06-10 08:01:05 +00:00
if ($ThreadInfo == NULL) {
json_die('failure', 'no such thread exists');
}
2011-10-31 08:00:12 +00:00
$ForumID = $ThreadInfo['ForumID'];
// Make sure they're allowed to look at the page
2013-04-13 08:00:19 +00:00
if (!check_forumperm($ForumID)) {
2011-10-31 08:00:12 +00:00
print json_encode(array('status' => 'failure'));
2012-02-09 08:00:20 +00:00
die();
2011-10-31 08:00:12 +00:00
}
//Post links utilize the catalogue & key params to prevent issues with custom posts per page
2013-04-13 08:00:19 +00:00
if ($ThreadInfo['Posts'] > $PerPage) {
if (isset($_GET['post']) && is_number($_GET['post'])) {
2011-10-31 08:00:12 +00:00
$PostNum = $_GET['post'];
2013-04-13 08:00:19 +00:00
} elseif (isset($_GET['postid']) && is_number($_GET['postid'])) {
2013-06-04 08:00:34 +00:00
$DB->query("
SELECT COUNT(ID)
FROM forums_posts
WHERE TopicID = $ThreadID
AND ID <= $_GET[postid]");
2011-10-31 08:00:12 +00:00
list($PostNum) = $DB->next_record();
} else {
$PostNum = 1;
}
} else {
$PostNum = 1;
}
2013-06-04 08:00:34 +00:00
list($Page, $Limit) = Format::page_limit($PerPage, min($ThreadInfo['Posts'], $PostNum));
2013-04-13 08:00:19 +00:00
if (($Page - 1) * $PerPage > $ThreadInfo['Posts']) {
2013-06-04 08:00:34 +00:00
$Page = ceil($ThreadInfo['Posts'] / $PerPage);
2012-07-17 08:00:18 +00:00
}
2013-06-04 08:00:34 +00:00
list($CatalogueID,$CatalogueLimit) = Format::catalogue_limit($Page, $PerPage, THREAD_CATALOGUE);
2011-10-31 08:00:12 +00:00
// Cache catalogue from which the page is selected, allows block caches and future ability to specify posts per page
2013-04-13 08:00:19 +00:00
if (!$Catalogue = $Cache->get_value('thread_'.$ThreadID.'_catalogue_'.$CatalogueID)) {
2013-06-04 08:00:34 +00:00
$DB->query("
SELECT
p.ID,
p.AuthorID,
p.AddedTime,
p.Body,
p.EditedUserID,
p.EditedTime
2011-10-31 08:00:12 +00:00
FROM forums_posts as p
2013-06-04 08:00:34 +00:00
WHERE p.TopicID = '$ThreadID'
AND p.ID != '".$ThreadInfo['StickyPostID']."'
2011-10-31 08:00:12 +00:00
LIMIT $CatalogueLimit");
$Catalogue = $DB->to_array(false,MYSQLI_ASSOC);
if (!$ThreadInfo['IsLocked'] || $ThreadInfo['IsSticky']) {
$Cache->cache_value('thread_'.$ThreadID.'_catalogue_'.$CatalogueID, $Catalogue, 0);
}
}
2013-06-04 08:00:34 +00:00
$Thread = Format::catalogue_select($Catalogue, $Page, $PerPage, THREAD_CATALOGUE);
2011-10-31 08:00:12 +00:00
2012-02-10 08:00:19 +00:00
if ($_GET['updatelastread'] != '0') {
2012-07-17 08:00:18 +00:00
$LastPost = end($Thread);
$LastPost = $LastPost['ID'];
reset($Thread);
2013-04-13 08:00:19 +00:00
if ($ThreadInfo['Posts'] <= $PerPage * $Page && $ThreadInfo['StickyPostID'] > $LastPost) {
2012-07-17 08:00:18 +00:00
$LastPost = $ThreadInfo['StickyPostID'];
}
2012-02-10 08:00:19 +00:00
//Handle last read
if (!$ThreadInfo['IsLocked'] || $ThreadInfo['IsSticky']) {
2013-05-27 08:00:58 +00:00
$DB->query("
SELECT PostID
FROM forums_last_read_topics
WHERE UserID='$LoggedUser[ID]'
AND TopicID='$ThreadID'");
2012-02-10 08:00:19 +00:00
list($LastRead) = $DB->next_record();
2013-04-13 08:00:19 +00:00
if ($LastRead < $LastPost) {
2013-05-27 08:00:58 +00:00
$DB->query("
INSERT INTO forums_last_read_topics
(UserID, TopicID, PostID)
VALUES
2013-06-04 08:00:34 +00:00
('$LoggedUser[ID]', '$ThreadID', '".db_string($LastPost)."')
2012-02-10 08:00:19 +00:00
ON DUPLICATE KEY UPDATE PostID='$LastPost'");
}
2011-10-31 08:00:12 +00:00
}
}
2013-06-10 08:01:05 +00:00
// Handle subscriptions
2013-04-13 08:00:19 +00:00
if (($UserSubscriptions = $Cache->get_value('subscriptions_user_'.$LoggedUser['ID'])) === false) {
2013-05-27 08:00:58 +00:00
$DB->query("
SELECT TopicID
FROM users_subscriptions
WHERE UserID = '$LoggedUser[ID]'");
2011-10-31 08:00:12 +00:00
$UserSubscriptions = $DB->collect(0);
2013-06-04 08:00:34 +00:00
$Cache->cache_value('subscriptions_user_'.$LoggedUser['ID'], $UserSubscriptions, 0);
2011-10-31 08:00:12 +00:00
}
2013-04-13 08:00:19 +00:00
if (empty($UserSubscriptions)) {
2011-10-31 08:00:12 +00:00
$UserSubscriptions = array();
}
2013-04-13 08:00:19 +00:00
if (in_array($ThreadID, $UserSubscriptions)) {
2011-10-31 08:00:12 +00:00
$Cache->delete_value('subscriptions_user_new_'.$LoggedUser['ID']);
}
$JsonPoll = array();
if ($ThreadInfo['NoPoll'] == 0) {
2013-06-04 08:00:34 +00:00
if (!list($Question, $Answers, $Votes, $Featured, $Closed) = $Cache->get_value('polls_'.$ThreadID)) {
2013-05-27 08:00:58 +00:00
$DB->query("
SELECT Question, Answers, Featured, Closed
FROM forums_polls
2013-06-04 08:00:34 +00:00
WHERE TopicID='$ThreadID'");
2011-10-31 08:00:12 +00:00
list($Question, $Answers, $Featured, $Closed) = $DB->next_record(MYSQLI_NUM, array(1));
$Answers = unserialize($Answers);
2013-05-27 08:00:58 +00:00
$DB->query("
SELECT Vote, COUNT(UserID)
FROM forums_polls_votes
WHERE TopicID='$ThreadID'
GROUP BY Vote");
2011-10-31 08:00:12 +00:00
$VoteArray = $DB->to_array(false, MYSQLI_NUM);
2013-02-22 08:00:24 +00:00
2011-10-31 08:00:12 +00:00
$Votes = array();
foreach ($VoteArray as $VoteSet) {
2013-06-04 08:00:34 +00:00
list($Key, $Value) = $VoteSet;
2011-10-31 08:00:12 +00:00
$Votes[$Key] = $Value;
}
2013-02-22 08:00:24 +00:00
2013-04-13 08:00:19 +00:00
foreach (array_keys($Answers) as $i) {
2011-10-31 08:00:12 +00:00
if (!isset($Votes[$i])) {
$Votes[$i] = 0;
}
}
2013-06-04 08:00:34 +00:00
$Cache->cache_value('polls_'.$ThreadID, array($Question, $Answers, $Votes, $Featured, $Closed), 0);
2011-10-31 08:00:12 +00:00
}
2013-02-22 08:00:24 +00:00
2011-10-31 08:00:12 +00:00
if (!empty($Votes)) {
$TotalVotes = array_sum($Votes);
$MaxVotes = max($Votes);
} else {
$TotalVotes = 0;
$MaxVotes = 0;
}
2013-02-22 08:00:24 +00:00
2011-10-31 08:00:12 +00:00
$RevealVoters = in_array($ForumID, $ForumsRevealVoters);
//Polls lose the you voted arrow thingy
2013-05-27 08:00:58 +00:00
$DB->query("
SELECT Vote
FROM forums_polls_votes
WHERE UserID='".$LoggedUser['ID']."'
AND TopicID='$ThreadID'");
2011-10-31 08:00:12 +00:00
list($UserResponse) = $DB->next_record();
if (!empty($UserResponse) && $UserResponse != 0) {
$Answers[$UserResponse] = '&raquo; '.$Answers[$UserResponse];
} else {
2013-04-13 08:00:19 +00:00
if (!empty($UserResponse) && $RevealVoters) {
2011-10-31 08:00:12 +00:00
$Answers[$UserResponse] = '&raquo; '.$Answers[$UserResponse];
}
}
2013-02-22 08:00:24 +00:00
2013-06-04 08:00:34 +00:00
$JsonPoll['closed'] = ($Closed == 1);
2011-10-31 08:00:12 +00:00
$JsonPoll['featured'] = $Featured;
$JsonPoll['question'] = $Question;
2011-11-26 08:00:20 +00:00
$JsonPoll['maxVotes'] = (int) $MaxVotes;
2011-10-31 08:00:12 +00:00
$JsonPoll['totalVotes'] = $TotalVotes;
$JsonPollAnswers = array();
2013-02-22 08:00:24 +00:00
2013-05-06 08:00:32 +00:00
foreach ($Answers as $i => $Answer) {
2011-10-31 08:00:12 +00:00
if (!empty($Votes[$i]) && $TotalVotes > 0) {
2013-05-06 08:00:32 +00:00
$Ratio = $Votes[$i] / $MaxVotes;
$Percent = $Votes[$i] / $TotalVotes;
2011-10-31 08:00:12 +00:00
} else {
2013-05-06 08:00:32 +00:00
$Ratio = 0;
$Percent = 0;
2011-10-31 08:00:12 +00:00
}
$JsonPollAnswers[] = array(
'answer' => $Answer,
'ratio' => $Ratio,
'percent' => $Percent
);
}
2013-02-22 08:00:24 +00:00
2011-10-31 08:00:12 +00:00
if ($UserResponse !== null || $Closed || $ThreadInfo['IsLocked'] || $LoggedUser['Class'] < $Forums[$ForumID]['MinClassWrite']) {
2013-02-22 08:00:24 +00:00
$JsonPoll['voted'] = True;
2011-10-31 08:00:12 +00:00
} else {
$JsonPoll['voted'] = False;
}
2013-02-22 08:00:24 +00:00
2011-10-31 08:00:12 +00:00
$JsonPoll['answers'] = $JsonPollAnswers;
}
//Sqeeze in stickypost
2013-04-13 08:00:19 +00:00
if ($ThreadInfo['StickyPostID']) {
if ($ThreadInfo['StickyPostID'] != $Thread[0]['ID']) {
2011-10-31 08:00:12 +00:00
array_unshift($Thread, $ThreadInfo['StickyPost']);
}
2013-05-27 08:00:58 +00:00
if ($ThreadInfo['StickyPostID'] != $Thread[count($Thread) - 1]['ID']) {
2011-10-31 08:00:12 +00:00
$Thread[] = $ThreadInfo['StickyPost'];
}
}
$JsonPosts = array();
foreach ($Thread as $Key => $Post) {
2012-03-28 08:00:20 +00:00
list($PostID, $AuthorID, $AddedTime, $Body, $EditedUserID, $EditedTime) = array_values($Post);
2012-10-11 08:00:15 +00:00
list($AuthorID, $Username, $PermissionID, $Paranoia, $Artist, $Donor, $Warned, $Avatar, $Enabled, $UserTitle) = array_values(Users::user_info($AuthorID));
$UserInfo = Users::user_info($EditedUserID);
2011-10-31 08:00:12 +00:00
$JsonPosts[] = array(
2011-11-26 08:00:20 +00:00
'postId' => (int) $PostID,
2011-10-31 08:00:12 +00:00
'addedTime' => $AddedTime,
2012-07-06 08:00:11 +00:00
'bbBody' => $Body,
2011-10-31 08:00:12 +00:00
'body' => $Text->full_format($Body),
2011-11-26 08:00:20 +00:00
'editedUserId' => (int) $EditedUserID,
2011-10-31 08:00:12 +00:00
'editedTime' => $EditedTime,
2012-03-28 08:00:20 +00:00
'editedUsername' => $UserInfo['Username'],
2011-10-31 08:00:12 +00:00
'author' => array(
2011-11-26 08:00:20 +00:00
'authorId' => (int) $AuthorID,
2011-10-31 08:00:12 +00:00
'authorName' => $Username,
'paranoia' => $Paranoia,
2013-06-04 08:00:34 +00:00
'artist' => ($Artist == 1),
'donor' => ($Donor == 1),
2013-05-27 08:00:58 +00:00
'warned' => ($Warned != '0000-00-00 00:00:00'),
2011-10-31 08:00:12 +00:00
'avatar' => $Avatar,
2013-06-04 08:00:34 +00:00
'enabled' => (($Enabled == 2) ? false : true),
2011-10-31 08:00:12 +00:00
'userTitle' => $UserTitle
),
2013-02-22 08:00:24 +00:00
2011-10-31 08:00:12 +00:00
);
}
print
json_encode(
array(
'status' => 'success',
'response' => array(
2011-11-26 08:00:20 +00:00
'forumId' => (int) $ForumID,
2011-10-31 08:00:12 +00:00
'forumName' => $Forums[$ForumID]['Name'],
2011-11-26 08:00:20 +00:00
'threadId' => (int) $ThreadID,
2012-06-16 08:00:18 +00:00
'threadTitle' => display_str($ThreadInfo['Title']),
2011-10-31 08:00:12 +00:00
'subscribed' => in_array($ThreadID, $UserSubscriptions),
2013-06-04 08:00:34 +00:00
'locked' => ($ThreadInfo['IsLocked'] == 1),
'sticky' => ($ThreadInfo['IsSticky'] == 1),
2011-11-26 08:00:20 +00:00
'currentPage' => (int) $Page,
2013-05-27 08:00:58 +00:00
'pages' => ceil($ThreadInfo['Posts'] / $PerPage),
2013-06-04 08:00:34 +00:00
'poll' => (empty($JsonPoll) ? null : $JsonPoll),
2011-10-31 08:00:12 +00:00
'posts' => $JsonPosts
)
)
2012-02-09 08:00:20 +00:00
);