PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/dead/comment.php

https://bitbucket.org/nexea/x00n
PHP | 207 lines | 144 code | 62 blank | 1 comment | 36 complexity | 0e8b3fd1410b0393d94673c15862f134 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?
  2. require_once("include/bittorrent.php");
  3. $action = $_GET["action"];
  4. dbconn(false);
  5. loggedinorreturn();
  6. if ($action == "add")
  7. {
  8. if ($_SERVER["REQUEST_METHOD"] == "POST")
  9. {
  10. $torrentid = 0 + $_POST["tid"];
  11. if (!is_valid_id($torrentid))
  12. stderr("Error", "Invalid ID $torrentid.");
  13. $res = mysql_query("SELECT name FROM torrents WHERE id = $torrentid") or sqlerr(__FILE__,__LINE__);
  14. $arr = mysql_fetch_array($res);
  15. if (!$arr)
  16. stderr("Error", "No torrent with ID $torrentid.");
  17. $text = trim($_POST["text"]);
  18. if (!$text)
  19. stderr("Error", "Comment body cannot be empty!");
  20. mysql_query("INSERT INTO comments (user, torrent, added, text, ori_text) VALUES (" .
  21. $CURUSER["id"] . ",$torrentid, '" . get_date_time() . "', " . sqlesc($text) .
  22. "," . sqlesc($text) . ")");
  23. $newid = mysql_insert_id();
  24. mysql_query("UPDATE torrents SET comments = comments + 1 WHERE id = $torrentid");
  25. header("Refresh: 0; url=details?id=$torrentid&viewcomm=$newid#comm$newid");
  26. die;
  27. }
  28. $torrentid = 0 + $_GET["tid"];
  29. if (!is_valid_id($torrentid))
  30. stderr("Error", "Invalid ID $torrentid.");
  31. $res = mysql_query("SELECT name FROM torrents WHERE id = $torrentid") or sqlerr(__FILE__,__LINE__);
  32. $arr = mysql_fetch_array($res);
  33. if (!$arr)
  34. stderr("Error", "No torrent with ID $torrentid.");
  35. stdhead("Add a comment to \"" . $arr["name"] . "\"");
  36. print("<h1>Add a comment to \"" . htmlspecialchars($arr["name"]) . "\"</h1>\n");
  37. print("<p><form method=\"post\" action=\"comment?action=add\">\n");
  38. print("<input type=\"hidden\" name=\"tid\" value=\"$torrentid\"/>\n");
  39. print("<textarea name=\"text\" rows=\"10\" cols=\"60\"></textarea></p>\n");
  40. print("<p><input type=\"submit\" class=\"btn\" value=\"Do it!\" /></p></form>\n");
  41. $res = mysql_query("SELECT comments.id, text, comments.added, username, users.id as user, users.avatar FROM comments LEFT JOIN users ON comments.user = users.id WHERE torrent = $torrentid ORDER BY comments.id DESC LIMIT 5");
  42. $allrows = array();
  43. while ($row = mysql_fetch_array($res))
  44. $allrows[] = $row;
  45. if (count($allrows)) {
  46. print("<h2>Most recent comments, in reverse order</h2>\n");
  47. commenttable($allrows);
  48. }
  49. stdfoot();
  50. die;
  51. }
  52. elseif ($action == "edit")
  53. {
  54. $commentid = 0 + $_GET["cid"];
  55. if (!is_valid_id($commentid))
  56. stderr("Error", "Invalid ID $commentid.");
  57. $res = mysql_query("SELECT c.*, t.name FROM comments AS c JOIN torrents AS t ON c.torrent = t.id WHERE c.id=$commentid") or sqlerr(__FILE__,__LINE__);
  58. $arr = mysql_fetch_array($res);
  59. if (!$arr)
  60. stderr("Error", "Invalid ID $commentid.");
  61. if ($arr["user"] != $CURUSER["id"] && get_user_class() < UC_MODERATOR)
  62. stderr("Error", "Permission denied.");
  63. if ($_SERVER["REQUEST_METHOD"] == "POST")
  64. {
  65. $text = $_POST["text"];
  66. $returnto = $_POST["returnto"];
  67. if ($text == "")
  68. stderr("Error", "Comment body cannot be empty!");
  69. $text = sqlesc($text);
  70. $editedat = sqlesc(get_date_time());
  71. mysql_query("UPDATE comments SET text=$text, editedat=$editedat, editedby=$CURUSER[id] WHERE id=$commentid") or mysql_error();
  72. if ($returnto)
  73. header("Location: $returnto");
  74. else
  75. header("Location: $DEFAULTBASEURL/"); // change later ----------------------
  76. die;
  77. }
  78. stdhead("Edit comment to \"" . $arr["name"] . "\"");
  79. print("<h1>Edit comment to \"" . htmlspecialchars($arr["name"]) . "\"</h1><p>\n");
  80. print("<form method=\"post\" action=\"comment?action=edit&amp;cid=$commentid\">\n");
  81. print("<input type=\"hidden\" name=\"returnto\" value=\"" . $_SERVER["HTTP_REFERER"] . "\" />\n");
  82. print("<input type=\"hidden\" name=\"cid\" value=\"$commentid\" />\n");
  83. print("<textarea name=\"text\" rows=\"10\" cols=\"60\">" . htmlspecialchars($arr["text"]) . "</textarea></p>\n");
  84. print("<p><input type=\"submit\" class=\"btn\" value=\"Do it!\" /></p></form>\n");
  85. stdfoot();
  86. die;
  87. }
  88. elseif ($action == "delete")
  89. {
  90. if (get_user_class() < UC_MODERATOR)
  91. stderr("Error", "Permission denied.");
  92. $commentid = 0 + $_GET["cid"];
  93. if (!is_valid_id($commentid))
  94. stderr("Error", "Invalid ID $commentid.");
  95. $sure = $_GET["sure"];
  96. if (!$sure)
  97. {
  98. $referer = $_SERVER["HTTP_REFERER"];
  99. stderr("Delete comment", "You are about to delete a comment. Click\n" .
  100. "<a href=?action=delete&cid=$commentid&sure=1" .
  101. ($referer ? "&returnto=" . urlencode($referer) : "") .
  102. ">here</a> if you are sure.");
  103. }
  104. $res = mysql_query("SELECT torrent FROM comments WHERE id=$commentid") or sqlerr(__FILE__,__LINE__);
  105. $arr = mysql_fetch_array($res);
  106. if ($arr)
  107. $torrentid = $arr["torrent"];
  108. mysql_query("DELETE FROM comments WHERE id=$commentid") or sqlerr(__FILE__,__LINE__);
  109. if ($torrentid && mysql_affected_rows() > 0)
  110. mysql_query("UPDATE torrents SET comments = comments - 1 WHERE id = $torrentid");
  111. $returnto = $_GET["returnto"];
  112. if ($returnto)
  113. header("Location: $returnto");
  114. else
  115. header("Location: $_SERVER["HTTP_REFERER"]/"); // change later ----------------------
  116. die;
  117. }
  118. elseif ($action == "vieworiginal")
  119. {
  120. if (get_user_class() < UC_MODERATOR)
  121. stderr("Error", "Permission denied.");
  122. $commentid = 0 + $_GET["cid"];
  123. if (!is_valid_id($commentid))
  124. stderr("Error", "Invalid ID $commentid.");
  125. $res = mysql_query("SELECT c.*, t.name FROM comments AS c JOIN torrents AS t ON c.torrent = t.id WHERE c.id=$commentid") or sqlerr(__FILE__,__LINE__);
  126. $arr = mysql_fetch_array($res);
  127. if (!$arr)
  128. stderr("Error", "Invalid ID $commentid.");
  129. stdhead("Original comment");
  130. print("<h1>Original contents of comment #$commentid</h1><p>\n");
  131. print("<table width=\"500\" border=\"1\" cellspacing=\"0\" cellpadding=\"5\">");
  132. print("<tr><td class=\"comment\">\n");
  133. echo htmlspecialchars($arr["ori_text"]);
  134. print("</td></tr></table>\n");
  135. $returnto = $_SERVER["HTTP_REFERER"];
  136. // $returnto = "details?id=$torrentid&amp;viewcomm=$commentid#$commentid";
  137. if ($returnto)
  138. print("<p><font size=\"small\">(<a href=\"$returnto\">back</a>)</font></p>\n");
  139. stdfoot();
  140. die;
  141. }
  142. else
  143. stderr("Error", "Unknown action $action");
  144. die;
  145. ?>