BioTorrents.de’s version of Gazelle
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

forum.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. #declare(strict_types=1);
  3. # todo: Go through line by line
  4. /**********|| Page to show individual forums || ********************************\
  5. Things to expect in $_GET:
  6. ForumID: ID of the forum curently being browsed
  7. page: The page the user's on.
  8. page = 1 is the same as no page
  9. ********************************************************************************/
  10. //---------- Things to sort out before it can start printing/generating content
  11. // Check for lame SQL injection attempts
  12. $ForumID = $_GET['forumid'];
  13. if (!is_number($ForumID)) {
  14. echo json_encode(array('status' => 'failure'));
  15. error();
  16. }
  17. if (isset($_GET['pp'])) {
  18. $PerPage = intval($_GET['pp']);
  19. } elseif (isset($LoggedUser['PostsPerPage'])) {
  20. $PerPage = $LoggedUser['PostsPerPage'];
  21. } else {
  22. $PerPage = POSTS_PER_PAGE;
  23. }
  24. list($Page, $Limit) = Format::page_limit(TOPICS_PER_PAGE);
  25. //---------- Get some data to start processing
  26. // Caching anything beyond the first page of any given forum is just wasting ram
  27. // users are more likely to search then to browse to page 2
  28. if ($Page === 1) {
  29. list($Forum, , , $Stickies) = $Cache->get_value("forums_$ForumID");
  30. }
  31. if (!isset($Forum) || !is_array($Forum)) {
  32. $DB->query("
  33. SELECT
  34. ID,
  35. Title,
  36. AuthorID,
  37. IsLocked,
  38. IsSticky,
  39. NumPosts,
  40. LastPostID,
  41. LastPostTime,
  42. LastPostAuthorID
  43. FROM forums_topics
  44. WHERE ForumID = '$ForumID'
  45. ORDER BY IsSticky DESC, LastPostTime DESC
  46. LIMIT $Limit"); // Can be cached until someone makes a new post
  47. $Forum = $DB->to_array('ID', MYSQLI_ASSOC, false);
  48. if ($Page === 1) {
  49. $DB->query("
  50. SELECT COUNT(ID)
  51. FROM forums_topics
  52. WHERE ForumID = '$ForumID'
  53. AND IsSticky = '1'");
  54. list($Stickies) = $DB->next_record();
  55. $Cache->cache_value("forums_$ForumID", array($Forum, '', 0, $Stickies), 0);
  56. }
  57. }
  58. if (!isset($Forums[$ForumID])) {
  59. json_die("failure");
  60. }
  61. // Make sure they're allowed to look at the page
  62. if (!check_perms('site_moderate_forums')) {
  63. if (isset($LoggedUser['CustomForums'][$ForumID]) && $LoggedUser['CustomForums'][$ForumID] === 0) {
  64. json_die("failure", "insufficient permissions to view page");
  65. }
  66. }
  67. if ($LoggedUser['CustomForums'][$ForumID] != 1 && $Forums[$ForumID]['MinClassRead'] > $LoggedUser['Class']) {
  68. json_die("failure", "insufficient permissions to view page");
  69. }
  70. $ForumName = display_str($Forums[$ForumID]['Name']);
  71. $JsonSpecificRules = [];
  72. foreach ($Forums[$ForumID]['SpecificRules'] as $ThreadIDs) {
  73. $Thread = Forums::get_thread_info($ThreadIDs);
  74. $JsonSpecificRules[] = array(
  75. 'threadId' => (int)$ThreadIDs,
  76. 'thread' => display_str($Thread['Title'])
  77. );
  78. }
  79. $Pages = Format::get_pages($Page, $Forums[$ForumID]['NumTopics'], TOPICS_PER_PAGE, 9);
  80. if (count($Forum) === 0) {
  81. print
  82. json_encode(
  83. array(
  84. 'status' => 'success',
  85. 'forumName' => $ForumName,
  86. 'threads' => []
  87. )
  88. );
  89. } else {
  90. // forums_last_read_topics is a record of the last post a user read in a topic, and what page that was on
  91. $DB->query("
  92. SELECT
  93. l.TopicID,
  94. l.PostID,
  95. CEIL(
  96. (
  97. SELECT COUNT(p.ID)
  98. FROM forums_posts AS p
  99. WHERE p.TopicID = l.TopicID
  100. AND p.ID <= l.PostID
  101. ) / $PerPage
  102. ) AS Page
  103. FROM forums_last_read_topics AS l
  104. WHERE l.TopicID IN(".implode(', ', array_keys($Forum)).')
  105. AND l.UserID = \''.$LoggedUser['ID'].'\'');
  106. // Turns the result set into a multi-dimensional array, with
  107. // forums_last_read_topics.TopicID as the key.
  108. // This is done here so we get the benefit of the caching, and we
  109. // don't have to make a database query for each topic on the page
  110. $LastRead = $DB->to_array('TopicID');
  111. $JsonTopics = [];
  112. foreach ($Forum as $Topic) {
  113. list($TopicID, $Title, $AuthorID, $Locked, $Sticky, $PostCount, $LastID, $LastTime, $LastAuthorID) = array_values($Topic);
  114. // Handle read/unread posts - the reason we can't cache the whole page
  115. if ((!$Locked || $Sticky)
  116. && ((empty($LastRead[$TopicID]) || $LastRead[$TopicID]['PostID'] < $LastID)
  117. && strtotime($LastTime) > $LoggedUser['CatchupTime'])
  118. ) {
  119. $Read = 'unread';
  120. } else {
  121. $Read = 'read';
  122. }
  123. $UserInfo = Users::user_info($AuthorID);
  124. $AuthorName = $UserInfo['Username'];
  125. $UserInfo = Users::user_info($LastAuthorID);
  126. $LastAuthorName = $UserInfo['Username'];
  127. // Bug fix for no last time available
  128. if (!$LastTime) {
  129. $LastTime = '';
  130. }
  131. $JsonTopics[] = array(
  132. 'topicId' => (int)$TopicID,
  133. 'title' => display_str($Title),
  134. 'authorId' => (int)$AuthorID,
  135. 'authorName' => $AuthorName,
  136. 'locked' => $Locked === 1,
  137. 'sticky' => $Sticky === 1,
  138. 'postCount' => (int)$PostCount,
  139. 'lastID' => ($LastID === null) ? 0 : (int)$LastID,
  140. 'lastTime' => $LastTime,
  141. 'lastAuthorId' => ($LastAuthorID === null) ? 0 : (int)$LastAuthorID,
  142. 'lastAuthorName' => ($LastAuthorName === null) ? '' : $LastAuthorName,
  143. 'lastReadPage' => ($LastRead[$TopicID]['Page'] === null) ? 0 : (int)$LastRead[$TopicID]['Page'],
  144. 'lastReadPostId' => ($LastRead[$TopicID]['PostID'] === null) ? 0 : (int)$LastRead[$TopicID]['PostID'],
  145. 'read' => $Read === 'read'
  146. );
  147. }
  148. print
  149. json_encode(
  150. array(
  151. 'status' => 'success',
  152. 'response' => array(
  153. 'forumName' => $ForumName,
  154. 'specificRules' => $JsonSpecificRules,
  155. 'currentPage' => (int)$Page,
  156. 'pages' => ceil($Forums[$ForumID]['NumTopics'] / TOPICS_PER_PAGE),
  157. 'threads' => $JsonTopics
  158. )
  159. )
  160. );
  161. }