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.

take_new_thread.php 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. declare(strict_types=1);
  3. authorize();
  4. $ENV = ENV::go();
  5. /*
  6. 'new' if the user is creating a new thread
  7. It will be accompanied with:
  8. $_POST['forum']
  9. $_POST['title']
  10. $_POST['body']
  11. and optionally include:
  12. $_POST['question']
  13. $_POST['answers']
  14. the latter of which is an array
  15. */
  16. if (isset($LoggedUser['PostsPerPage'])) {
  17. $PerPage = $LoggedUser['PostsPerPage'];
  18. } else {
  19. $PerPage = POSTS_PER_PAGE;
  20. }
  21. if (isset($_POST['thread']) && !is_number($_POST['thread'])) {
  22. error(0);
  23. }
  24. if (isset($_POST['forum']) && !is_number($_POST['forum'])) {
  25. error(0);
  26. }
  27. // If you're not sending anything, go back
  28. if (empty($_POST['body']) || empty($_POST['title'])) {
  29. header('Location: '.$_SERVER['HTTP_REFERER']);
  30. error();
  31. }
  32. $Body = $_POST['body'];
  33. if ($LoggedUser['DisablePosting']) {
  34. error('Your posting privileges have been removed.');
  35. }
  36. $Title = Format::cut_string(trim($_POST['title']), 150, 1, 0);
  37. $ForumID = $_POST['forum'];
  38. if (!isset($Forums[$ForumID])) {
  39. error(404);
  40. }
  41. if (!Forums::check_forumperm($ForumID, 'Write') || !Forums::check_forumperm($ForumID, 'Create')) {
  42. error(403);
  43. }
  44. if (empty($_POST['question']) || empty($_POST['answers']) || !check_perms('forums_polls_create')) {
  45. $NoPoll = 1;
  46. } else {
  47. $NoPoll = 0;
  48. $Question = trim($_POST['question']);
  49. $Answers = [];
  50. $Votes = [];
  51. //This can cause polls to have answer IDs of 1 3 4 if the second box is empty
  52. foreach ($_POST['answers'] as $i => $Answer) {
  53. if ($Answer === '') {
  54. continue;
  55. }
  56. $Answers[$i + 1] = $Answer;
  57. $Votes[$i + 1] = 0;
  58. }
  59. if (count($Answers) < 2) {
  60. error('You cannot create a poll with only one answer.');
  61. } elseif (count($Answers) > 25) {
  62. error('You cannot create a poll with greater than 25 answers.');
  63. }
  64. }
  65. $DB->query("
  66. INSERT INTO forums_topics
  67. (Title, AuthorID, ForumID, LastPostTime, LastPostAuthorID, CreatedTime)
  68. Values
  69. ('".db_string($Title)."', '".$LoggedUser['ID']."', '$ForumID', NOW(), '".$LoggedUser['ID']."', NOW())");
  70. $TopicID = $DB->inserted_id();
  71. $DB->query("
  72. INSERT INTO forums_posts
  73. (TopicID, AuthorID, AddedTime, Body)
  74. VALUES
  75. ('$TopicID', '".$LoggedUser['ID']."', NOW(), '".db_string($Body)."')");
  76. $PostID = $DB->inserted_id();
  77. $DB->query("
  78. UPDATE forums
  79. SET
  80. NumPosts = NumPosts + 1,
  81. NumTopics = NumTopics + 1,
  82. LastPostID = '$PostID',
  83. LastPostAuthorID = '".$LoggedUser['ID']."',
  84. LastPostTopicID = '$TopicID',
  85. LastPostTime = NOW()
  86. WHERE ID = '$ForumID'");
  87. $DB->query("
  88. UPDATE forums_topics
  89. SET
  90. NumPosts = NumPosts + 1,
  91. LastPostID = '$PostID',
  92. LastPostAuthorID = '".$LoggedUser['ID']."',
  93. LastPostTime = NOW()
  94. WHERE ID = '$TopicID'");
  95. if (isset($_POST['subscribe'])) {
  96. Subscriptions::subscribe($TopicID);
  97. }
  98. //Award a badge if necessary
  99. $DB->query("
  100. SELECT COUNT(ID)
  101. FROM forums_posts
  102. WHERE AuthorID = '$LoggedUser[ID]'");
  103. list($UserPosts) = $DB->next_record(MYSQLI_NUM, false);
  104. foreach ($ENV->AUTOMATED_BADGE_IDS->Posts as $Count => $Badge) {
  105. if ((int) $UserPosts >= $Count) {
  106. $Success = Badges::award_badge($LoggedUser['ID'], $Badge);
  107. if ($Success) {
  108. Misc::send_pm($LoggedUser['ID'], 0, 'You have received a badge!', "You have received a badge for making ".$Count." forum posts.\n\nIt can be enabled from your user settings.");
  109. }
  110. }
  111. }
  112. if (!$NoPoll) { // god, I hate double negatives...
  113. $DB->query("
  114. INSERT INTO forums_polls
  115. (TopicID, Question, Answers)
  116. VALUES
  117. ('$TopicID', '".db_string($Question)."', '".db_string(serialize($Answers))."')");
  118. $Cache->cache_value("polls_$TopicID", array($Question, $Answers, $Votes, null, '0'), 0);
  119. if ($ForumID === STAFF_FORUM) {
  120. send_irc(STAFF_CHAN, 'Poll created by '.$LoggedUser['Username'].": '$Question' ".site_url()."forums.php?action=viewthread&threadid=$TopicID");
  121. }
  122. }
  123. // if cache exists modify it, if not, then it will be correct when selected next, and we can skip this block
  124. if ($Forum = $Cache->get_value("forums_$ForumID")) {
  125. list($Forum, , , $Stickies) = $Forum;
  126. // Remove the last thread from the index
  127. if (count($Forum) === TOPICS_PER_PAGE && $Stickies < TOPICS_PER_PAGE) {
  128. array_pop($Forum);
  129. }
  130. if ($Stickies > 0) {
  131. $Part1 = array_slice($Forum, 0, $Stickies, true); // Stickies
  132. $Part3 = array_slice($Forum, $Stickies, TOPICS_PER_PAGE - $Stickies - 1, true); // Rest of page
  133. } else {
  134. $Part1 = [];
  135. $Part3 = $Forum;
  136. }
  137. $Part2 = array($TopicID => array(
  138. 'ID' => $TopicID,
  139. 'Title' => $Title,
  140. 'AuthorID' => $LoggedUser['ID'],
  141. 'IsLocked' => 0,
  142. 'IsSticky' => 0,
  143. 'NumPosts' => 1,
  144. 'LastPostID' => $PostID,
  145. 'LastPostTime' => sqltime(),
  146. 'LastPostAuthorID' => $LoggedUser['ID'],
  147. 'NoPoll' => $NoPoll
  148. )); // Bumped
  149. $Forum = $Part1 + $Part2 + $Part3;
  150. $Cache->cache_value("forums_$ForumID", array($Forum, '', 0, $Stickies), 0);
  151. // Update the forum root
  152. $Cache->begin_transaction('forums_list');
  153. $Cache->update_row($ForumID, array(
  154. 'NumPosts' => '+1',
  155. 'NumTopics' => '+1',
  156. 'LastPostID' => $PostID,
  157. 'LastPostAuthorID' => $LoggedUser['ID'],
  158. 'LastPostTopicID' => $TopicID,
  159. 'LastPostTime' => sqltime(),
  160. 'Title' => $Title,
  161. 'IsLocked' => 0,
  162. 'IsSticky' => 0
  163. ));
  164. $Cache->commit_transaction(0);
  165. } else {
  166. // If there's no cache, we have no data, and if there's no data
  167. $Cache->delete_value('forums_list');
  168. }
  169. $Cache->begin_transaction("thread_$TopicID".'_catalogue_0');
  170. $Post = array(
  171. 'ID' => $PostID,
  172. 'AuthorID' => $LoggedUser['ID'],
  173. 'AddedTime' => sqltime(),
  174. 'Body' => $Body,
  175. 'EditedUserID' => 0,
  176. 'EditedTime' => null
  177. );
  178. $Cache->insert('', $Post);
  179. $Cache->commit_transaction(0);
  180. $Cache->begin_transaction("thread_$TopicID".'_info');
  181. $Cache->update_row(false, array('Posts' => '+1', 'LastPostAuthorID' => $LoggedUser['ID']));
  182. $Cache->commit_transaction(0);
  183. header("Location: forums.php?action=viewthread&threadid=$TopicID");
  184. die();