-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcomment.php
206 lines (157 loc) · 6.7 KB
/
comment.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
require_once("include/bittorrent.php");
require_once "include/user_functions.php";
$action = $_GET["action"];
dbconn(false);
loggedinorreturn();
#do not allow guest users to add comments (too much spam)
if($CURUSER['id'] ==4)
{
stdhead();
stdmsg("Sorry...", "You are not authorized to view this page. Please <a href='login.php'>Login</a> or <a href='signup.php'>Sign up</a> for your own account.");
stdfoot();
exit;
}
if ($action == "add")
{
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$torrentid = 0 + $_POST["tid"];
if (!is_valid_id($torrentid))
stderr("Error", "Invalid ID.");
$res = @mysql_query("SELECT name FROM torrents WHERE id = $torrentid") or sqlerr(__FILE__,__LINE__);
$arr = mysql_fetch_array($res,MYSQL_NUM);
if (!$arr)
stderr("Error", "No torrent with ID.");
$text = trim($_POST["text"]);
if (!$text)
stderr("Error", "Comment body cannot be empty!");
@mysql_query("INSERT INTO comments (user, torrent, added, text, ori_text) VALUES (" .
$CURUSER["id"] . ",$torrentid, " . time() . ", " . sqlesc($text) .
"," . sqlesc($text) . ")");
$newid = mysql_insert_id();
@mysql_query("UPDATE torrents SET comments = comments + 1 WHERE id = $torrentid");
header("Refresh: 0; url=details.php?id=$torrentid&viewcomm=$newid#comm$newid");
die;
}
$torrentid = 0 + $_GET["tid"];
if (!is_valid_id($torrentid))
stderr("Error", "Invalid ID.");
$res = mysql_query("SELECT name FROM torrents WHERE id = $torrentid") or sqlerr(__FILE__,__LINE__);
$arr = mysql_fetch_assoc($res);
if (!$arr)
stderr("Error", "No torrent with ID.");
stdhead("Add a comment to \"" . $arr["name"] . "\"");
print("<h1>Add a comment to \"" . htmlspecialchars($arr["name"]) . "\"</h1>\n");
print("<p><form method=\"post\" action=\"comment.php?action=add\">\n");
print("<input type=\"hidden\" name=\"tid\" value=\"$torrentid\"/>\n");
print("<textarea name=\"text\" rows=\"10\" cols=\"60\"></textarea></p>\n");
print("<p><input type=\"submit\" class='btn' value=\"Do it!\" /></p></form>\n");
$res = mysql_query("SELECT comments.id, text, comments.added, comments.editedby, comments.editedat, username, users.id as user, users.title, users.avatar, users.class, users.donor, users.warned FROM comments LEFT JOIN users ON comments.user = users.id WHERE torrent = $torrentid ORDER BY comments.id DESC LIMIT 5");
$allrows = array();
while ($row = mysql_fetch_assoc($res))
$allrows[] = $row;
if (count($allrows)) {
require_once "include/torrenttable_functions.php";
require_once "include/html_functions.php";
require_once "include/bbcode_functions.php";
print("<h2>Most recent comments, in reverse order</h2>\n");
commenttable($allrows);
}
stdfoot();
die;
}
elseif ($action == "edit")
{
$commentid = 0 + $_GET["cid"];
if (!is_valid_id($commentid))
stderr("Error", "Invalid ID.");
$res = mysql_query("SELECT c.*, t.name FROM comments AS c LEFT JOIN torrents AS t ON c.torrent = t.id WHERE c.id=$commentid") or sqlerr(__FILE__,__LINE__);
$arr = mysql_fetch_assoc($res);
if (!$arr)
stderr("Error", "Invalid ID.");
if ($arr["user"] != $CURUSER["id"] && get_user_class() < UC_MODERATOR)
stderr("Error", "Permission denied.");
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$text = $_POST["text"];
$returnto = htmlspecialchars($_POST["returnto"]);
if ($text == "")
stderr("Error", "Comment body cannot be empty!");
$text = sqlesc($text);
$editedat = time();
mysql_query("UPDATE comments SET text=$text, editedat=$editedat, editedby={$CURUSER['id']} WHERE id=$commentid") or sqlerr(__FILE__, __LINE__);
if ($returnto)
header("Location: $returnto");
else
header("Location: $BASEURL/"); // change later ----------------------
die;
}
stdhead("Edit comment to \"" . $arr["name"] . "\"");
print("<h1>Edit comment to \"" . htmlspecialchars($arr["name"]) . "\"</h1><p>\n");
print("<form method=\"post\" action=\"comment.php?action=edit&cid=$commentid\">\n");
print("<input type=\"hidden\" name=\"returnto\" value=\"" . $_SERVER["HTTP_REFERER"] . "\" />\n");
print("<input type=\"hidden\" name=\"cid\" value=\"$commentid\" />\n");
print("<textarea name=\"text\" rows=\"10\" cols=\"60\">" . htmlspecialchars($arr["text"]) . "</textarea></p>\n");
print("<p><input type=\"submit\" class='btn' value=\"Do it!\" /></p></form>\n");
stdfoot();
die;
}
elseif ($action == "delete")
{
if (get_user_class() < UC_MODERATOR)
stderr("Error", "Permission denied.");
$commentid = 0 + $_GET["cid"];
if (!is_valid_id($commentid))
stderr("Error", "Invalid ID.");
$sure = isset($_GET["sure"]) ? (int)$_GET["sure"] : false;
if (!$sure)
{
$referer = $_SERVER["HTTP_REFERER"];
stderr("Delete comment", "You are about to delete a comment. Click\n" .
"<a href='comment.php?action=delete&cid=$commentid&sure=1" .
($referer ? "&returnto=" . urlencode($referer) : "") .
"'>here</a> if you are sure.");
}
$res = mysql_query("SELECT torrent FROM comments WHERE id=$commentid") or sqlerr(__FILE__,__LINE__);
$arr = mysql_fetch_assoc($res);
if ($arr)
$torrentid = $arr["torrent"];
mysql_query("DELETE FROM comments WHERE id=$commentid") or sqlerr(__FILE__,__LINE__);
if ($torrentid && mysql_affected_rows() > 0)
mysql_query("UPDATE torrents SET comments = comments - 1 WHERE id = $torrentid");
$returnto = $_GET["returnto"];
if ($returnto)
header("Location: $returnto");
else
header("Location: $BASEURL/"); // change later ----------------------
die;
}
elseif ($action == "vieworiginal")
{
if (get_user_class() < UC_MODERATOR)
stderr("Error", "Permission denied.");
$commentid = 0 + $_GET["cid"];
if (!is_valid_id($commentid))
stderr("Error", "Invalid ID.");
$res = mysql_query("SELECT c.*, t.name FROM comments AS c LEFT JOIN torrents AS t ON c.torrent = t.id WHERE c.id=$commentid") or sqlerr(__FILE__,__LINE__);
$arr = mysql_fetch_assoc($res);
if (!$arr)
stderr("Error", "Invalid ID $commentid.");
stdhead("Original comment");
print("<h1>Original contents of comment #$commentid</h1><p>\n");
print("<table width='500' border='1' cellspacing='0' cellpadding='5'>");
print("<tr><td class='comment'>\n");
echo htmlspecialchars($arr["ori_text"]);
print("</td></tr></table>\n");
$returnto = htmlspecialchars($_SERVER["HTTP_REFERER"]);
// $returnto = "details.php?id=$torrentid&viewcomm=$commentid#$commentid";
if ($returnto)
print("<p><font size='small'>(<a href='$returnto'>back</a>)</font></p>\n");
stdfoot();
die;
}
else
stderr("Error", "Unknown action");
die;
?>