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.

comments.class.php 16KB

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