Gazelle/sections/ajax/forum/forum.php

180 lines
5.2 KiB
PHP
Raw Normal View History

2011-10-31 08:00:12 +00:00
<?
/**********|| Page to show individual forums || ********************************\
Things to expect in $_GET:
ForumID: 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
// Check for lame SQL injection attempts
$ForumID = $_GET['forumid'];
2013-04-15 08:00:54 +00:00
if (!is_number($ForumID)) {
2011-10-31 08:00:12 +00:00
print json_encode(array('status' => 'failure'));
die();
}
if (isset($_GET['pp'])) {
2015-11-16 08:00:28 +00:00
$PerPage = (int) $_GET['pp'];
2013-04-15 08:00:54 +00:00
} elseif (isset($LoggedUser['PostsPerPage'])) {
2011-10-31 08:00:12 +00:00
$PerPage = $LoggedUser['PostsPerPage'];
} else {
$PerPage = POSTS_PER_PAGE;
}
2013-05-15 08:00:54 +00:00
list($Page, $Limit) = Format::page_limit(TOPICS_PER_PAGE);
2011-10-31 08:00:12 +00:00
//---------- Get some data to start processing
// Caching anything beyond the first page of any given forum is just wasting ram
// users are more likely to search then to browse to page 2
2013-04-15 08:00:54 +00:00
if ($Page == 1) {
2013-07-02 08:01:37 +00:00
list($Forum,,,$Stickies) = $Cache->get_value("forums_$ForumID");
2011-10-31 08:00:12 +00:00
}
2013-04-15 08:00:54 +00:00
if (!isset($Forum) || !is_array($Forum)) {
2013-05-15 08:00:54 +00:00
$DB->query("
SELECT
2013-11-17 08:00:47 +00:00
ID,
Title,
AuthorID,
IsLocked,
IsSticky,
NumPosts,
LastPostID,
LastPostTime,
LastPostAuthorID
FROM forums_topics
WHERE ForumID = '$ForumID'
ORDER BY IsSticky DESC, LastPostTime DESC
2011-10-31 08:00:12 +00:00
LIMIT $Limit"); // Can be cached until someone makes a new post
2012-06-16 08:00:18 +00:00
$Forum = $DB->to_array('ID',MYSQLI_ASSOC, false);
2013-04-15 08:00:54 +00:00
if ($Page == 1) {
2013-06-04 08:00:34 +00:00
$DB->query("
SELECT COUNT(ID)
FROM forums_topics
2013-07-02 08:01:37 +00:00
WHERE ForumID = '$ForumID'
AND IsSticky = '1'");
2011-10-31 08:00:12 +00:00
list($Stickies) = $DB->next_record();
2013-07-02 08:01:37 +00:00
$Cache->cache_value("forums_$ForumID", array($Forum, '', 0, $Stickies), 0);
2011-10-31 08:00:12 +00:00
}
}
2013-04-15 08:00:54 +00:00
if (!isset($Forums[$ForumID])) {
2013-05-14 08:00:34 +00:00
json_die("failure");
2011-10-31 08:00:12 +00:00
}
// Make sure they're allowed to look at the page
if (!check_perms('site_moderate_forums')) {
2013-04-15 08:00:54 +00:00
if (isset($LoggedUser['CustomForums'][$ForumID]) && $LoggedUser['CustomForums'][$ForumID] === 0) {
2013-05-15 08:00:54 +00:00
json_die("failure", "insufficient permissions to view page");
2013-05-14 08:00:34 +00:00
}
2013-04-15 08:00:54 +00:00
}
if ($LoggedUser['CustomForums'][$ForumID] != 1 && $Forums[$ForumID]['MinClassRead'] > $LoggedUser['Class']) {
2013-05-15 08:00:54 +00:00
json_die("failure", "insufficient permissions to view page");
2011-10-31 08:00:12 +00:00
}
2012-06-16 08:00:18 +00:00
$ForumName = display_str($Forums[$ForumID]['Name']);
2011-10-31 08:00:12 +00:00
$JsonSpecificRules = array();
foreach ($Forums[$ForumID]['SpecificRules'] as $ThreadIDs) {
2013-10-01 23:08:42 +00:00
$Thread = Forums::get_thread_info($ThreadIDs);
2011-10-31 08:00:12 +00:00
$JsonSpecificRules[] = array(
2013-10-30 08:01:19 +00:00
'threadId' => (int)$ThreadIDs,
2012-06-16 08:00:18 +00:00
'thread' => display_str($Thread['Title'])
2011-10-31 08:00:12 +00:00
);
}
2013-05-15 08:00:54 +00:00
$Pages = Format::get_pages($Page, $Forums[$ForumID]['NumTopics'], TOPICS_PER_PAGE, 9);
2011-10-31 08:00:12 +00:00
2013-07-10 00:08:53 +00:00
if (count($Forum) === 0) {
2011-10-31 08:00:12 +00:00
print
json_encode(
array(
'status' => 'success',
2012-06-16 08:00:18 +00:00
'forumName' => $ForumName,
2011-10-31 08:00:12 +00:00
'threads' => array()
)
);
2013-04-15 08:00:54 +00:00
} else {
2011-10-31 08:00:12 +00:00
// forums_last_read_topics is a record of the last post a user read in a topic, and what page that was on
2013-07-10 00:08:53 +00:00
$DB->query("
2013-06-04 08:00:34 +00:00
SELECT
l.TopicID,
l.PostID,
2013-10-30 08:01:19 +00:00
CEIL(
(
2013-11-17 08:00:47 +00:00
SELECT COUNT(p.ID)
FROM forums_posts AS p
WHERE p.TopicID = l.TopicID
AND p.ID <= l.PostID
2013-10-30 08:01:19 +00:00
) / $PerPage
) AS Page
2011-10-31 08:00:12 +00:00
FROM forums_last_read_topics AS l
2013-11-17 08:00:47 +00:00
WHERE l.TopicID IN(".implode(', ', array_keys($Forum)).')
AND l.UserID = \''.$LoggedUser['ID'].'\'');
2011-10-31 08:00:12 +00:00
// Turns the result set into a multi-dimensional array, with
// forums_last_read_topics.TopicID as the key.
// This is done here so we get the benefit of the caching, and we
// don't have to make a database query for each topic on the page
$LastRead = $DB->to_array('TopicID');
2013-02-22 08:00:24 +00:00
2011-10-31 08:00:12 +00:00
$JsonTopics = array();
foreach ($Forum as $Topic) {
2012-03-28 08:00:20 +00:00
list($TopicID, $Title, $AuthorID, $Locked, $Sticky, $PostCount, $LastID, $LastTime, $LastAuthorID) = array_values($Topic);
2011-10-31 08:00:12 +00:00
// handle read/unread posts - the reason we can't cache the whole page
2013-10-30 08:01:19 +00:00
if ((!$Locked || $Sticky)
&& ((empty($LastRead[$TopicID]) || $LastRead[$TopicID]['PostID'] < $LastID)
&& strtotime($LastTime) > $LoggedUser['CatchupTime'])
) {
2011-10-31 08:00:12 +00:00
$Read = 'unread';
} else {
$Read = 'read';
}
2012-10-11 08:00:15 +00:00
$UserInfo = Users::user_info($AuthorID);
2012-03-28 08:00:20 +00:00
$AuthorName = $UserInfo['Username'];
2012-10-11 08:00:15 +00:00
$UserInfo = Users::user_info($LastAuthorID);
2012-03-28 08:00:20 +00:00
$LastAuthorName = $UserInfo['Username'];
2013-06-10 08:01:05 +00:00
// Bug fix for no last time available
if ($LastTime == '0000-00-00 00:00:00') {
$LastTime = '';
}
2013-02-22 08:00:24 +00:00
2011-10-31 08:00:12 +00:00
$JsonTopics[] = array(
2013-10-30 08:01:19 +00:00
'topicId' => (int)$TopicID,
2012-06-16 08:00:18 +00:00
'title' => display_str($Title),
2013-10-30 08:01:19 +00:00
'authorId' => (int)$AuthorID,
2011-10-31 08:00:12 +00:00
'authorName' => $AuthorName,
2013-10-30 08:01:19 +00:00
'locked' => $Locked == 1,
'sticky' => $Sticky == 1,
'postCount' => (int)$PostCount,
'lastID' => ($LastID == null) ? 0 : (int)$LastID,
2011-10-31 08:00:12 +00:00
'lastTime' => $LastTime,
2013-10-30 08:01:19 +00:00
'lastAuthorId' => ($LastAuthorID == null) ? 0 : (int)$LastAuthorID,
'lastAuthorName' => ($LastAuthorName == null) ? '' : $LastAuthorName,
'lastReadPage' => ($LastRead[$TopicID]['Page'] == null) ? 0 : (int)$LastRead[$TopicID]['Page'],
'lastReadPostId' => ($LastRead[$TopicID]['PostID'] == null) ? 0 : (int)$LastRead[$TopicID]['PostID'],
'read' => $Read == 'read'
2011-10-31 08:00:12 +00:00
);
}
print
json_encode(
array(
'status' => 'success',
'response' => array(
2012-06-16 08:00:18 +00:00
'forumName' => $ForumName,
2011-10-31 08:00:12 +00:00
'specificRules' => $JsonSpecificRules,
2013-10-30 08:01:19 +00:00
'currentPage' => (int)$Page,
2013-06-04 08:00:34 +00:00
'pages' => ceil($Forums[$ForumID]['NumTopics'] / TOPICS_PER_PAGE),
2011-10-31 08:00:12 +00:00
'threads' => $JsonTopics
)
)
);
}
2011-11-26 08:00:20 +00:00
?>