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.

comments.class.php 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. <?php
  2. class Comments
  3. {
  4. /*
  5. * For all functions:
  6. * $Page = 'artist', 'collages', 'requests' or 'torrents'
  7. * $PageID = ArtistID, CollageID, RequestID or GroupID, respectively
  8. */
  9. /**
  10. * Post a comment on an artist, request or torrent page.
  11. * @param string $Page
  12. * @param int $PageID
  13. * @param string $Body
  14. * @return int ID of the new comment
  15. */
  16. public static function post($Page, $PageID, $Body)
  17. {
  18. $QueryID = G::$DB->get_query_id();
  19. G::$DB->query("
  20. SELECT
  21. CEIL(
  22. (
  23. SELECT COUNT(ID) + 1
  24. FROM comments
  25. WHERE Page = '$Page'
  26. AND PageID = $PageID
  27. ) / " . TORRENT_COMMENTS_PER_PAGE . "
  28. ) AS Pages");
  29. list($Pages) = G::$DB->next_record();
  30. G::$DB->query("
  31. INSERT INTO comments (Page, PageID, AuthorID, AddedTime, Body)
  32. VALUES ('$Page', $PageID, " . G::$LoggedUser['ID'] . ", NOW(), '" . db_string($Body) . "')");
  33. $PostID = G::$DB->inserted_id();
  34. $CatalogueID = floor((TORRENT_COMMENTS_PER_PAGE * $Pages - TORRENT_COMMENTS_PER_PAGE) / THREAD_CATALOGUE);
  35. G::$Cache->delete_value($Page.'_comments_'.$PageID.'_catalogue_'.$CatalogueID);
  36. G::$Cache->delete_value($Page.'_comments_'.$PageID);
  37. Subscriptions::flush_subscriptions($Page, $PageID);
  38. Subscriptions::quote_notify($Body, $PostID, $Page, $PageID);
  39. G::$DB->set_query_id($QueryID);
  40. return $PostID;
  41. }
  42. /**
  43. * Edit a comment
  44. * @param int $PostID
  45. * @param string $NewBody
  46. * @param bool $SendPM If true, send a PM to the author of the comment informing him about the edit
  47. *
  48. * todo: Move permission check out of here/remove hardcoded error(404)
  49. */
  50. public static function edit($PostID, $NewBody, $SendPM = false)
  51. {
  52. $QueryID = G::$DB->get_query_id();
  53. G::$DB->query("
  54. SELECT
  55. Body,
  56. AuthorID,
  57. Page,
  58. PageID,
  59. AddedTime
  60. FROM comments
  61. WHERE ID = $PostID");
  62. if (!G::$DB->has_results()) {
  63. return false;
  64. }
  65. list($OldBody, $AuthorID, $Page, $PageID, $AddedTime) = G::$DB->next_record();
  66. if (G::$LoggedUser['ID'] != $AuthorID && !check_perms('site_moderate_forums')) {
  67. return false;
  68. }
  69. G::$DB->query("
  70. SELECT CEIL(COUNT(ID) / " . TORRENT_COMMENTS_PER_PAGE . ") AS Page
  71. FROM comments
  72. WHERE Page = '$Page'
  73. AND PageID = $PageID
  74. AND ID <= $PostID");
  75. list($CommPage) = G::$DB->next_record();
  76. // Perform the update
  77. G::$DB->query("
  78. UPDATE comments
  79. SET
  80. Body = '" . db_string($NewBody) . "',
  81. EditedUserID = " . G::$LoggedUser['ID'] . ",
  82. EditedTime = NOW()
  83. WHERE ID = $PostID");
  84. // Update the cache
  85. $CatalogueID = floor((TORRENT_COMMENTS_PER_PAGE * $CommPage - TORRENT_COMMENTS_PER_PAGE) / THREAD_CATALOGUE);
  86. G::$Cache->delete_value($Page . '_comments_' . $PageID . '_catalogue_' . $CatalogueID);
  87. if ($Page == 'collages') {
  88. // On collages, we also need to clear the collage key (collage_$CollageID), because it has the comments in it... (why??)
  89. G::$Cache->delete_value('collage_' . $PageID);
  90. }
  91. G::$DB->query("
  92. INSERT INTO comments_edits (Page, PostID, EditUser, EditTime, Body)
  93. VALUES ('$Page', $PostID, " . G::$LoggedUser['ID'] . ", NOW(), '" . db_string($OldBody) . "')");
  94. G::$DB->set_query_id($QueryID);
  95. if ($SendPM && G::$LoggedUser['ID'] != $AuthorID) {
  96. // Send a PM to the user to notify them of the edit
  97. $PMSubject = "Your comment #$PostID has been edited";
  98. $PMurl = site_url()."comments.php?action=jump&postid=$PostID";
  99. $ProfLink = '[url='.site_url().'user.php?id='.G::$LoggedUser['ID'].']'.G::$LoggedUser['Username'].'[/url]';
  100. $PMBody = "One of your comments has been edited by $ProfLink: [url]{$PMurl}[/url]";
  101. Misc::send_pm($AuthorID, 0, $PMSubject, $PMBody);
  102. }
  103. return true; // todo: This should reflect whether or not the update was actually successful, e.g., by checking G::$DB->affected_rows after the UPDATE query
  104. }
  105. /**
  106. * Delete a comment
  107. * @param int $PostID
  108. */
  109. public static function delete($PostID)
  110. {
  111. $QueryID = G::$DB->get_query_id();
  112. // Get page, pageid
  113. G::$DB->query("SELECT Page, PageID FROM comments WHERE ID = $PostID");
  114. if (!G::$DB->has_results()) {
  115. // no such comment?
  116. G::$DB->set_query_id($QueryID);
  117. return false;
  118. }
  119. list($Page, $PageID) = G::$DB->next_record();
  120. // Get number of pages
  121. G::$DB->query("
  122. SELECT
  123. CEIL(COUNT(ID) / " . TORRENT_COMMENTS_PER_PAGE . ") AS Pages,
  124. CEIL(SUM(IF(ID <= $PostID, 1, 0)) / " . TORRENT_COMMENTS_PER_PAGE . ") AS Page
  125. FROM comments
  126. WHERE Page = '$Page'
  127. AND PageID = $PageID
  128. GROUP BY PageID");
  129. if (!G::$DB->has_results()) {
  130. // The comment $PostID was probably not posted on $Page
  131. G::$DB->set_query_id($QueryID);
  132. return false;
  133. }
  134. list($CommPages, $CommPage) = G::$DB->next_record();
  135. // $CommPages = number of pages in the thread
  136. // $CommPage = which page the post is on
  137. // These are set for cache clearing.
  138. G::$DB->query("
  139. DELETE FROM comments
  140. WHERE ID = $PostID");
  141. G::$DB->query("
  142. DELETE FROM comments_edits
  143. WHERE Page = '$Page'
  144. AND PostID = $PostID");
  145. G::$DB->query("
  146. DELETE FROM users_notify_quoted
  147. WHERE Page = '$Page'
  148. AND PostID = $PostID");
  149. Subscriptions::flush_subscriptions($Page, $PageID);
  150. Subscriptions::flush_quote_notifications($Page, $PageID);
  151. // We need to clear all subsequential catalogues as they've all been bumped with the absence of this post
  152. $ThisCatalogue = floor((TORRENT_COMMENTS_PER_PAGE * $CommPage - TORRENT_COMMENTS_PER_PAGE) / THREAD_CATALOGUE);
  153. $LastCatalogue = floor((TORRENT_COMMENTS_PER_PAGE * $CommPages - TORRENT_COMMENTS_PER_PAGE) / THREAD_CATALOGUE);
  154. for ($i = $ThisCatalogue; $i <= $LastCatalogue; ++$i) {
  155. G::$Cache->delete_value($Page . '_comments_' . $PageID . '_catalogue_' . $i);
  156. }
  157. G::$Cache->delete_value($Page . '_comments_' . $PageID);
  158. if ($Page === 'collages') {
  159. // On collages, we also need to clear the collage key (collage_$CollageID), because it has the comments in it... (why??)
  160. G::$Cache->delete_value("collage_$PageID");
  161. }
  162. G::$DB->set_query_id($QueryID);
  163. return true;
  164. }
  165. /**
  166. * Get the URL to a comment, already knowing the Page and PostID
  167. * @param string $Page
  168. * @param int $PageID
  169. * @param int $PostID
  170. * @return string|bool The URL to the comment or false on error
  171. */
  172. public static function get_url($Page, $PageID, $PostID = null)
  173. {
  174. $Post = (!empty($PostID) ? "&postid=$PostID#post$PostID" : '');
  175. switch ($Page) {
  176. case 'artist':
  177. return "artist.php?id=$PageID$Post";
  178. case 'collages':
  179. return "collages.php?action=comments&collageid=$PageID$Post";
  180. case 'requests':
  181. return "requests.php?action=view&id=$PageID$Post";
  182. case 'torrents':
  183. return "torrents.php?id=$PageID$Post";
  184. default:
  185. return false;
  186. }
  187. }
  188. /**
  189. * Get the URL to a comment
  190. * @param int $PostID
  191. * @return string|bool The URL to the comment or false on error
  192. */
  193. public static function get_url_query($PostID)
  194. {
  195. $QueryID = G::$DB->get_query_id();
  196. G::$DB->query("
  197. SELECT Page, PageID
  198. FROM comments
  199. WHERE ID = $PostID");
  200. if (!G::$DB->has_results()) {
  201. error(404);
  202. }
  203. list($Page, $PageID) = G::$DB->next_record();
  204. G::$DB->set_query_id($QueryID);
  205. return self::get_url($Page, $PageID, $PostID);
  206. }
  207. /**
  208. * Load a page's comments. This takes care of `postid` and (indirectly) `page` parameters passed in $_GET.
  209. * Quote notifications and last read are also handled here, unless $HandleSubscriptions = false is passed.
  210. * @param string $Page
  211. * @param int $PageID
  212. * @param bool $HandleSubscriptions Whether or not to handle subscriptions (last read & quote notifications)
  213. * @return array ($NumComments, $Page, $Thread, $LastRead)
  214. * $NumComments: the total number of comments on this artist/request/torrent group
  215. * $Page: the page we're currently on
  216. * $Thread: an array of all posts on this page
  217. * $LastRead: ID of the last comment read by the current user in this thread;
  218. * will be false if $HandleSubscriptions == false or if there are no comments on this page
  219. */
  220. public static function load($Page, $PageID, $HandleSubscriptions = true)
  221. {
  222. $QueryID = G::$DB->get_query_id();
  223. // Get the total number of comments
  224. $NumComments = G::$Cache->get_value($Page."_comments_$PageID");
  225. if ($NumComments === false) {
  226. G::$DB->query("
  227. SELECT COUNT(ID)
  228. FROM comments
  229. WHERE Page = '$Page'
  230. AND PageID = $PageID");
  231. list($NumComments) = G::$DB->next_record();
  232. G::$Cache->cache_value($Page."_comments_$PageID", $NumComments, 0);
  233. }
  234. // If a postid was passed, we need to determine which page that comment is on.
  235. // Format::page_limit handles a potential $_GET['page']
  236. if (isset($_GET['postid']) && is_number($_GET['postid']) && $NumComments > TORRENT_COMMENTS_PER_PAGE) {
  237. G::$DB->query("
  238. SELECT COUNT(ID)
  239. FROM comments
  240. WHERE Page = '$Page'
  241. AND PageID = $PageID
  242. AND ID <= $_GET[postid]");
  243. list($PostNum) = G::$DB->next_record();
  244. list($CommPage, $Limit) = Format::page_limit(TORRENT_COMMENTS_PER_PAGE, $PostNum);
  245. } else {
  246. list($CommPage, $Limit) = Format::page_limit(TORRENT_COMMENTS_PER_PAGE, $NumComments);
  247. }
  248. // Get the cache catalogue
  249. $CatalogueID = floor((TORRENT_COMMENTS_PER_PAGE * $CommPage - TORRENT_COMMENTS_PER_PAGE) / THREAD_CATALOGUE);
  250. // Cache catalogue from which the page is selected, allows block caches and future ability to specify posts per page
  251. $Catalogue = G::$Cache->get_value($Page.'_comments_'.$PageID.'_catalogue_'.$CatalogueID);
  252. if ($Catalogue === false) {
  253. $CatalogueLimit = $CatalogueID * THREAD_CATALOGUE . ', ' . THREAD_CATALOGUE;
  254. G::$DB->query("
  255. SELECT
  256. c.ID,
  257. c.AuthorID,
  258. c.AddedTime,
  259. c.Body,
  260. c.EditedUserID,
  261. c.EditedTime,
  262. u.Username
  263. FROM comments AS c
  264. LEFT JOIN users_main AS u ON u.ID = c.EditedUserID
  265. WHERE c.Page = '$Page'
  266. AND c.PageID = $PageID
  267. ORDER BY c.ID
  268. LIMIT $CatalogueLimit");
  269. $Catalogue = G::$DB->to_array(false, MYSQLI_ASSOC);
  270. G::$Cache->cache_value($Page.'_comments_'.$PageID.'_catalogue_'.$CatalogueID, $Catalogue, 0);
  271. }
  272. // This is a hybrid to reduce the catalogue down to the page elements: We use the page limit % catalogue
  273. $Thread = array_slice($Catalogue, ((TORRENT_COMMENTS_PER_PAGE * $CommPage - TORRENT_COMMENTS_PER_PAGE) % THREAD_CATALOGUE), TORRENT_COMMENTS_PER_PAGE, true);
  274. if ($HandleSubscriptions && count($Thread) > 0) {
  275. // Quote notifications
  276. $LastPost = end($Thread);
  277. $LastPost = $LastPost['ID'];
  278. $FirstPost = reset($Thread);
  279. $FirstPost = $FirstPost['ID'];
  280. G::$DB->query("
  281. UPDATE users_notify_quoted
  282. SET UnRead = false
  283. WHERE UserID = " . G::$LoggedUser['ID'] . "
  284. AND Page = '$Page'
  285. AND PageID = $PageID
  286. AND PostID >= $FirstPost
  287. AND PostID <= $LastPost");
  288. if (G::$DB->affected_rows()) {
  289. G::$Cache->delete_value('notify_quoted_' . G::$LoggedUser['ID']);
  290. }
  291. // Last read
  292. G::$DB->query("
  293. SELECT PostID
  294. FROM users_comments_last_read
  295. WHERE UserID = " . G::$LoggedUser['ID'] . "
  296. AND Page = '$Page'
  297. AND PageID = $PageID");
  298. list($LastRead) = G::$DB->next_record();
  299. if ($LastRead < $LastPost) {
  300. G::$DB->query("
  301. INSERT INTO users_comments_last_read
  302. (UserID, Page, PageID, PostID)
  303. VALUES
  304. (" . G::$LoggedUser['ID'] . ", '$Page', $PageID, $LastPost)
  305. ON DUPLICATE KEY UPDATE
  306. PostID = $LastPost");
  307. G::$Cache->delete_value('subscriptions_user_new_' . G::$LoggedUser['ID']);
  308. }
  309. } else {
  310. $LastRead = false;
  311. }
  312. G::$DB->set_query_id($QueryID);
  313. return array($NumComments, $CommPage, $Thread, $LastRead);
  314. }
  315. /**
  316. * Merges all comments from $Page/$PageID into $Page/$TargetPageID. This also takes care of quote notifications, subscriptions and cache.
  317. * @param type $Page
  318. * @param type $PageID
  319. * @param type $TargetPageID
  320. */
  321. public static function merge($Page, $PageID, $TargetPageID)
  322. {
  323. $QueryID = G::$DB->get_query_id();
  324. G::$DB->query("
  325. UPDATE comments
  326. SET PageID = $TargetPageID
  327. WHERE Page = '$Page'
  328. AND PageID = $PageID");
  329. // quote notifications
  330. G::$DB->query("
  331. UPDATE users_notify_quoted
  332. SET PageID = $TargetPageID
  333. WHERE Page = '$Page'
  334. AND PageID = $PageID");
  335. // comment subscriptions
  336. Subscriptions::move_subscriptions($Page, $PageID, $TargetPageID);
  337. // cache (we need to clear all comment catalogues)
  338. G::$DB->query("
  339. SELECT
  340. CEIL(COUNT(ID) / " . TORRENT_COMMENTS_PER_PAGE . ") AS Pages
  341. FROM comments
  342. WHERE Page = '$Page'
  343. AND PageID = $TargetPageID
  344. GROUP BY PageID");
  345. list($CommPages) = G::$DB->next_record();
  346. $LastCatalogue = floor((TORRENT_COMMENTS_PER_PAGE * $CommPages - TORRENT_COMMENTS_PER_PAGE) / THREAD_CATALOGUE);
  347. for ($i = 0; $i <= $LastCatalogue; ++$i) {
  348. G::$Cache->delete_value($Page . "_comments_$TargetPageID" . "_catalogue_$i");
  349. }
  350. G::$Cache->delete_value($Page."_comments_$TargetPageID");
  351. G::$DB->set_query_id($QueryID);
  352. }
  353. /**
  354. * Delete all comments on $Page/$PageID (deals with quote notifications and subscriptions as well)
  355. * @param string $Page
  356. * @param int $PageID
  357. * @return boolean
  358. */
  359. public static function delete_page($Page, $PageID)
  360. {
  361. $QueryID = G::$DB->get_query_id();
  362. // get number of pages
  363. G::$DB->query("
  364. SELECT
  365. CEIL(COUNT(ID) / " . TORRENT_COMMENTS_PER_PAGE . ") AS Pages
  366. FROM comments
  367. WHERE Page = '$Page'
  368. AND PageID = $PageID
  369. GROUP BY PageID");
  370. if (!G::$DB->has_results()) {
  371. return false;
  372. }
  373. list($CommPages) = G::$DB->next_record();
  374. // Delete comments
  375. G::$DB->query("
  376. DELETE FROM comments
  377. WHERE Page = '$Page'
  378. AND PageID = $PageID");
  379. // Delete quote notifications
  380. Subscriptions::flush_quote_notifications($Page, $PageID);
  381. G::$DB->query("
  382. DELETE FROM users_notify_quoted
  383. WHERE Page = '$Page'
  384. AND PageID = $PageID");
  385. // Deal with subscriptions
  386. Subscriptions::move_subscriptions($Page, $PageID, null);
  387. // Clear cache
  388. $LastCatalogue = floor((TORRENT_COMMENTS_PER_PAGE * $CommPages - TORRENT_COMMENTS_PER_PAGE) / THREAD_CATALOGUE);
  389. for ($i = 0; $i <= $LastCatalogue; ++$i) {
  390. G::$Cache->delete_value($Page . '_comments_' . $PageID . '_catalogue_' . $i);
  391. }
  392. G::$Cache->delete_value($Page.'_comments_'.$PageID);
  393. G::$DB->set_query_id($QueryID);
  394. return true;
  395. }
  396. }