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.

post_history.php 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. #declare(strict_types=1);
  3. # todo: Go through line by line
  4. function error_out($reason = '')
  5. {
  6. $error = array('status' => 'failure');
  7. if ($reason !== '') {
  8. $error['reason'] = $reason;
  9. }
  10. echo $error;
  11. error();
  12. }
  13. if (!empty($LoggedUser['DisableForums'])) {
  14. error_out('You do not have access to the forums!');
  15. }
  16. $UserID = empty($_GET['userid']) ? $LoggedUser['ID'] : $_GET['userid'];
  17. if (!is_number($UserID)) {
  18. error_out('User does not exist!');
  19. }
  20. if (isset($LoggedUser['PostsPerPage'])) {
  21. $PerPage = $LoggedUser['PostsPerPage'];
  22. } else {
  23. $PerPage = POSTS_PER_PAGE;
  24. }
  25. list($Page, $Limit) = Format::page_limit($PerPage);
  26. $UserInfo = Users::user_info($UserID);
  27. extract(array_intersect_key($UserInfo, array_flip(array('Username', 'Enabled', 'Title', 'Avatar', 'Donor', 'Warned'))));
  28. $ViewingOwn = ($UserID === $LoggedUser['ID']);
  29. $ShowUnread = ($ViewingOwn && (!isset($_GET['showunread']) || !!$_GET['showunread']));
  30. $ShowGrouped = ($ViewingOwn && (!isset($_GET['group']) || !!$_GET['group']));
  31. if ($ShowGrouped) {
  32. $SQL = '
  33. SELECT
  34. SQL_CALC_FOUND_ROWS
  35. MAX(p.ID) AS ID
  36. FROM forums_posts AS p
  37. LEFT JOIN forums_topics AS t ON t.ID = p.TopicID';
  38. if ($ShowUnread) {
  39. $SQL .= '
  40. LEFT JOIN forums_last_read_topics AS l ON l.TopicID = t.ID AND l.UserID = '.$LoggedUser['ID'];
  41. }
  42. $SQL .= '
  43. LEFT JOIN forums AS f ON f.ID = t.ForumID
  44. WHERE p.AuthorID = '.$UserID.'
  45. AND ' . Forums::user_forums_sql();
  46. if ($ShowUnread) {
  47. $SQL .= '
  48. AND ((t.IsLocked = \'0\' OR t.IsSticky = \'1\')
  49. AND (l.PostID < t.LastPostID OR l.PostID IS NULL))';
  50. }
  51. $SQL .= "
  52. GROUP BY t.ID
  53. ORDER BY p.ID DESC
  54. LIMIT $Limit";
  55. $PostIDs = $DB->query($SQL);
  56. $DB->query('SELECT FOUND_ROWS()');
  57. list($Results) = $DB->next_record();
  58. if ($Results > $PerPage * ($Page - 1)) {
  59. $DB->set_query_id($PostIDs);
  60. $PostIDs = $DB->collect('ID');
  61. $SQL = "
  62. SELECT
  63. p.ID,
  64. p.AddedTime,
  65. p.Body,
  66. p.EditedUserID,
  67. p.EditedTime,
  68. ed.Username,
  69. p.TopicID,
  70. t.Title,
  71. t.LastPostID,
  72. l.PostID AS LastRead,
  73. t.IsLocked,
  74. t.IsSticky
  75. FROM forums_posts AS p
  76. LEFT JOIN users_main AS um ON um.ID = p.AuthorID
  77. LEFT JOIN users_info AS ui ON ui.UserID = p.AuthorID
  78. LEFT JOIN users_main AS ed ON ed.ID = p.EditedUserID
  79. JOIN forums_topics AS t ON t.ID = p.TopicID
  80. JOIN forums AS f ON f.ID = t.ForumID
  81. LEFT JOIN forums_last_read_topics AS l ON l.UserID = $UserID AND l.TopicID = t.ID
  82. WHERE p.ID IN (".implode(',', $PostIDs).')
  83. ORDER BY p.ID DESC';
  84. $Posts = $DB->query($SQL);
  85. }
  86. } else {
  87. $SQL = '
  88. SELECT
  89. SQL_CALC_FOUND_ROWS';
  90. if ($ShowGrouped) {
  91. $SQL .= '
  92. *
  93. FROM (
  94. SELECT';
  95. }
  96. $SQL .= '
  97. p.ID,
  98. p.AddedTime,
  99. p.Body,
  100. p.EditedUserID,
  101. p.EditedTime,
  102. ed.Username,
  103. p.TopicID,
  104. t.Title,
  105. t.LastPostID,';
  106. if ($UserID === $LoggedUser['ID']) {
  107. $SQL .= '
  108. l.PostID AS LastRead,';
  109. }
  110. $SQL .= "
  111. t.IsLocked,
  112. t.IsSticky
  113. FROM forums_posts AS p
  114. LEFT JOIN users_main AS um ON um.ID = p.AuthorID
  115. LEFT JOIN users_info AS ui ON ui.UserID = p.AuthorID
  116. LEFT JOIN users_main AS ed ON ed.ID = p.EditedUserID
  117. JOIN forums_topics AS t ON t.ID = p.TopicID
  118. JOIN forums AS f ON f.ID = t.ForumID
  119. LEFT JOIN forums_last_read_topics AS l ON l.UserID = $UserID AND l.TopicID = t.ID
  120. WHERE p.AuthorID = $UserID
  121. AND " . Forums::user_forums_sql();
  122. if ($ShowUnread) {
  123. $SQL .= '
  124. AND ((t.IsLocked = \'0\' OR t.IsSticky = \'1\')
  125. AND (l.PostID < t.LastPostID OR l.PostID IS NULL)
  126. ) ';
  127. }
  128. $SQL .= '
  129. ORDER BY p.ID DESC';
  130. if ($ShowGrouped) {
  131. $SQL .= '
  132. ) AS sub
  133. GROUP BY TopicID
  134. ORDER BY ID DESC';
  135. }
  136. $SQL .= "
  137. LIMIT $Limit";
  138. $Posts = $DB->query($SQL);
  139. $DB->query('SELECT FOUND_ROWS()');
  140. list($Results) = $DB->next_record();
  141. $DB->set_query_id($Posts);
  142. }
  143. $JsonResults = [];
  144. while (list($PostID, $AddedTime, $Body, $EditedUserID, $EditedTime, $EditedUsername, $TopicID, $ThreadTitle, $LastPostID, $LastRead, $Locked, $Sticky) = $DB->next_record()) {
  145. $JsonResults[] = array(
  146. 'postId' => (int)$PostID,
  147. 'topicId' => (int)$TopicID,
  148. 'threadTitle' => $ThreadTitle,
  149. 'lastPostId' => (int)$LastPostID,
  150. 'lastRead' => (int)$LastRead,
  151. 'locked' => $Locked === '1',
  152. 'sticky' => $Sticky === '1',
  153. 'addedTime' => $AddedTime,
  154. 'body' => Text::full_format($Body),
  155. 'bbbody' => $Body,
  156. 'editedUserId' => (int)$EditedUserID,
  157. 'editedTime' => $EditedTime,
  158. 'editedUsername' => $EditedUsername
  159. );
  160. }
  161. echo json_encode(
  162. array(
  163. 'status' => 'success',
  164. 'response' => array(
  165. 'currentPage' => (int)$Page,
  166. 'pages' => ceil($Results / $PerPage),
  167. 'threads' => $JsonResults
  168. )
  169. )
  170. );