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.6KB

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