Oppaitime'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 5.8KB

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