Vanilla commit.
[tinybbs.git] / action.php
blob560a126251b69be475a6bdcaafe803c618356323
1 <?php
3 // This file is for non-content actions.
4 require('includes/header.php');
5 force_id();
7 // Take the action ...
8 switch($_GET['action'])
10 // Normal actions ...
11 case 'watch_topic':
13 if( ! ctype_digit($_GET['id']))
15 add_error('Invalid ID.', true);
18 $id = $_GET['id'];
19 $page_title = 'Watch topic';
21 if(isset($_POST['id']))
23 $check_watchlist = $link->prepare('SELECT 1 FROM watchlists WHERE uid = ? AND topic_id = ?');
24 $check_watchlist->bind_param('si', $_SESSION['UID'], $id);
25 $check_watchlist->execute();
26 $check_watchlist->store_result();
27 if($check_watchlist->num_rows == 0)
29 $add_watchlist = $link->prepare('INSERT INTO watchlists (uid, topic_id) VALUES (?, ?)');
30 $add_watchlist->bind_param('si', $_SESSION['UID'], $_POST['id']);
31 $add_watchlist->execute();
32 $add_watchlist->close();
34 $check_watchlist->close();
36 redirect('Topic added to your watchlist.');
39 break;
41 //Priveleged actions.
43 case 'delete_page':
45 if( ! $administrator)
47 add_error('You are not wise enough.', true);
50 if( ! ctype_digit($_GET['id']))
52 add_error('Invalid ID.', true);
55 $id = $_GET['id'];
56 $page_title = 'Delete page';
58 if(isset($_POST['id']))
60 $file_uid_ban = $link->prepare('DELETE FROM pages WHERE id = ?');
61 $file_uid_ban->bind_param('i', $id);
62 $file_uid_ban->execute();
63 $file_uid_ban->close();
65 redirect('Page deleted.');
68 break;
70 case 'ban_uid':
72 if( ! $moderator && ! $administrator)
74 add_error('You are not wise enough.', true);
77 if( ! id_exists($_GET['id']))
79 add_error('There is no such user.', true);
82 $id = $_GET['id'];
83 $page_title = 'Ban poster ' . $id;
85 if(isset($_POST['id']))
87 $file_uid_ban = $link->prepare('INSERT INTO uid_bans (uid, filed) VALUES (?, ?) ON DUPLICATE KEY UPDATE filed = ?');
88 $file_uid_ban->bind_param('sii', $id, $_SERVER['REQUEST_TIME'], $_SERVER['REQUEST_TIME']);
89 $file_uid_ban->execute();
90 $file_uid_ban->close();
92 redirect('User ID banned.');
95 break;
97 case 'unban_uid':
99 if( ! $moderator && ! $administrator)
101 add_error('You are not wise enough.', true);
104 if( ! id_exists($_GET['id']))
106 add_error('There is no such user.', true);
109 $id = $_GET['id'];
110 $page_title = 'Unban poster ' . $id;
112 if(isset($_POST['id']))
114 remove_id_ban($id);
116 redirect('User ID unbanned.');
119 break;
121 case 'unban_ip':
123 if( ! $moderator && ! $administrator)
125 add_error('You are not wise enough.', true);
128 if( ! filter_var($_GET['id'], FILTER_VALIDATE_IP))
130 add_error('That is not a valid IP address.', true);
133 $id = $_GET['id'];
134 $page_title = 'Unban IP address ' . $id;
136 if(isset($_POST['id']))
138 remove_ip_ban($id);
140 redirect('IP address unbanned.');
143 break;
145 case 'delete_topic':
147 if( ! $moderator && ! $administrator)
149 add_error('You are not wise enough.', true);
151 if( ! ctype_digit($_GET['id']))
153 add_error('Invalid topic ID.', true);
156 $id = $_GET['id'];
157 $page_title = 'Delete topic';
159 if(isset($_POST['id']))
161 // Move record to user's trash.
162 $archive_topic = $link->prepare('INSERT INTO trash (uid, headline, body, time) SELECT topics.author, topics.headline, topics.body, UNIX_TIMESTAMP() FROM topics WHERE topics.id = ?;');
163 $archive_topic->bind_param('i', $id);
164 $archive_topic->execute();
165 $archive_topic->close();
167 // And delete it from the main table.
168 $delete_topic = $link->prepare('DELETE FROM topics WHERE id = ?');
169 $delete_topic->bind_param('i', $id);
170 $delete_topic->execute();
171 $delete_topic->close();
173 redirect('Topic archived and deleted.', '');
176 break;
178 case 'delete_reply':
180 if( ! $moderator && ! $administrator)
182 add_error('You are not wise enough.', true);
184 if( ! ctype_digit($_GET['id']))
186 add_error('Invalid reply ID.', true);
189 $id = $_GET['id'];
190 $page_title = 'Delete reply';
192 if(isset($_POST['id']))
194 $fetch_parent = $link->prepare('SELECT parent_id FROM replies WHERE id = ?');
195 $fetch_parent->bind_param('i', $id);
196 $fetch_parent->execute();
197 $fetch_parent->bind_result($parent_id);
198 $fetch_parent->fetch();
199 $fetch_parent->close();
201 if( ! $parent_id)
203 add_error('No such reply.', true);
206 // Move record to user's trash.
207 $archive_reply = $link->prepare('INSERT INTO trash (uid, body, time) SELECT replies.author, replies.body, UNIX_TIMESTAMP() FROM replies WHERE replies.id = ?;');
208 $archive_reply->bind_param('i', $id);
209 $archive_reply->execute();
210 $archive_reply->close();
212 // And delete it from the main table.
213 $delete_reply = $link->prepare('DELETE FROM replies WHERE id = ?');
214 $delete_reply->bind_param('i', $id);
215 $delete_reply->execute();
216 $delete_reply->close();
218 // Reduce the parent's reply count.
219 $decrement = $link->prepare('UPDATE topics SET replies = replies - 1 WHERE id = ?');
220 $decrement->bind_param('i', $parent_id);
221 $decrement->execute();
222 $decrement->close();
224 redirect('Reply archived and deleted.');
227 break;
229 case 'delete_ip_ids':
231 if( ! $moderator && ! $administrator)
233 add_error('You are not wise enough.', true);
236 if( ! filter_var($_GET['id'], FILTER_VALIDATE_IP))
238 add_error('That is not a valid IP address.', true);
241 $id = $_GET['id'];
242 $page_title = 'Delete IDs assigned to <a href="/IP_address/' . $id . '">' . $id . '</a>';
244 if(isset($_POST['id']))
246 $delete_ids = $link->prepare('DELETE FROM users WHERE ip_address = ?');
247 $delete_ids->bind_param('s', $id);
248 $delete_ids->execute();
249 $delete_ids->close();
251 redirect('IDs deleted.');
254 break;
256 case 'nuke_id':
258 if( ! $moderator && ! $administrator)
260 add_error('You are not wise enough.', true);
263 if( ! id_exists($_GET['id']))
265 add_error('There is no such user.', true);
268 $id = $_GET['id'];
269 $page_title = 'Nuke all posts by <a href="/profile/' . $id . '">' . $id . '</a>';
271 if(isset($_POST['id']))
273 // Delete replies.
274 $fetch_parents = $link->prepare('SELECT parent_id FROM replies WHERE author = ?');
275 $fetch_parents->bind_param('s', $id);
276 $fetch_parents->execute();
277 $fetch_parents->bind_result($parent_id);
279 $victim_parents = array();
280 while($fetch_parents->fetch())
282 $victim_parents[] = $parent_id;
284 $fetch_parents->close();
286 $delete_replies = $link->prepare('DELETE FROM replies WHERE author = ?');
287 $delete_replies->bind_param('s', $id);
288 $delete_replies->execute();
289 $delete_replies->close();
291 $decrement = $link->prepare('UPDATE topics SET replies = replies - 1 WHERE id = ?');
292 foreach($victim_parents as $parent_id)
294 $decrement->bind_param('i', $parent_id);
295 $decrement->execute();
297 $decrement->close();
299 // Delete topics.
300 $delete_topics = $link->prepare('DELETE FROM topics WHERE author = ?');
301 $delete_topics->bind_param('s', $id);
302 $delete_topics->execute();
303 $delete_topics->close();
305 redirect('All topics and replies by ' . $id . ' have been deleted.');
308 break;
310 case 'nuke_ip':
312 if( ! $moderator && ! $administrator)
314 add_error('You are not wise enough.', true);
317 if( ! filter_var($_GET['id'], FILTER_VALIDATE_IP))
319 add_error('That is not a valid IP address.', true);
322 $id = $_GET['id'];
323 $page_title = 'Nuke all posts by <a href="/IP_address/' . $id . '">' . $id . '</a>';
325 if(isset($_POST['id']))
327 // Delete replies.
328 $fetch_parents = $link->prepare('SELECT parent_id FROM replies WHERE author_ip = ?');
329 $fetch_parents->bind_param('s', $id);
330 $fetch_parents->execute();
331 $fetch_parents->bind_result($parent_id);
333 $victim_parents = array();
334 while($fetch_parents->fetch())
336 $victim_parents[] = $parent_id;
338 $fetch_parents->close();
340 $delete_replies = $link->prepare('DELETE FROM replies WHERE author_ip = ?');
341 $delete_replies->bind_param('s', $id);
342 $delete_replies->execute();
343 $delete_replies->close();
345 $decrement = $link->prepare('UPDATE topics SET replies = replies - 1 WHERE id = ?');
346 foreach($victim_parents as $parent_id)
348 $decrement->bind_param('i', $parent_id);
349 $decrement->execute();
351 $decrement->close();
353 // Delete topics.
354 $delete_topics = $link->prepare('DELETE FROM topics WHERE author_ip = ?');
355 $delete_topics->bind_param('s', $id);
356 $delete_topics->execute();
357 $delete_topics->close();
359 redirect('All topics and replies by ' . $id . ' have been deleted.');
362 break;
364 default:
365 add_error('No valid action specified.', true);
368 echo '<p>Really?</p> <form action="" method="post"> <div> <input type="hidden" name="id" value="' . $id . '" /> <input type="submit" value="Do it" /> </div>';
370 require('includes/footer.php');