comparison forum/Sources/ManageBans.php @ 76:e3e11437ecea website

Add forum code
author Chris Cannam
date Sun, 07 Jul 2013 11:25:48 +0200
parents
children
comparison
equal deleted inserted replaced
75:72f59aa7e503 76:e3e11437ecea
1 <?php
2
3 /**
4 * Simple Machines Forum (SMF)
5 *
6 * @package SMF
7 * @author Simple Machines http://www.simplemachines.org
8 * @copyright 2011 Simple Machines
9 * @license http://www.simplemachines.org/about/smf/license.php BSD
10 *
11 * @version 2.0
12 */
13
14 if (!defined('SMF'))
15 die('Hacking attempt...');
16
17 /* This file contains all the functions used for the ban center.
18
19 void Ban()
20 - the main entrance point for all ban center functions.
21 - is accesssed by ?action=admin;area=ban.
22 - choses a function based on the 'sa' parameter.
23 - defaults to BanList().
24 - requires the ban_members permission.
25 - initializes the admin tabs.
26 - load the ManageBans template.
27
28 void BanList()
29 - shows a list of bans currently set.
30 - is accesssed by ?action=admin;area=ban;sa=list.
31 - uses the main ManageBans template.
32 - removes expired bans.
33 - allows sorting on different criteria.
34 - also handles removal of selected ban items.
35
36 void BanEdit()
37 - the screen for adding new bans and modifying existing ones.
38 - adding new bans:
39 - is accesssed by ?action=admin;area=ban;sa=add.
40 - uses the ban_edit sub template of the ManageBans template.
41 - modifying existing bans:
42 - is accesssed by ?action=admin;area=ban;sa=edit;bg=x
43 - uses the ban_edit sub template of the ManageBans template.
44 - shows a list of ban triggers for the specified ban.
45 - handles submitted forms that add, modify or remove ban triggers.
46
47 void BanEditTrigger()
48 - the screen for adding new ban triggers or modifying existing ones.
49 - adding new ban triggers:
50 - is accessed by ?action=admin;area=ban;sa=edittrigger;bg=x
51 - uses the ban_edit_trigger sub template of ManageBans.
52 - editing existing ban triggers:
53 - is accessed by ?action=admin;area=ban;sa=edittrigger;bg=x;bi=y
54 - uses the ban_edit_trigger sub template of ManageBans.
55
56 void BanBrowseTriggers()
57 - screen for showing the banned enities
58 - is accessed by ?action=admin;area=ban;sa=browse
59 - uses the browse_triggers sub template of the ManageBans template.
60 - uses sub-tabs for browsing by IP, hostname, email or username.
61
62 array BanLog()
63 - show a list of logged access attempts by banned users.
64 - is accessed by ?action=admin;area=ban;sa=log.
65 - allows sorting of several columns.
66 - also handles deletion of (a selection of) log entries.
67
68 string range2ip(array $low, array $high)
69 - reverse function of ip2range().
70 - converts a given array of IP numbers to a single string
71 - range2ip(array(10, 10, 10, 0), array(10, 10, 20, 255)) returns
72 '10.10.10-20.*
73
74 array checkExistingTriggerIP(array $ip_array, string $fullip)
75 - checks whether a given IP range already exists in the trigger list.
76 - if yes, it returns an error message. Otherwise, it returns
77 an array optimized for the database.
78
79 void updateBanMembers()
80 - updates the members table to match the new bans.
81 - is_activated >= 10: a member is banned.
82 */
83
84 // Ban center.
85 function Ban()
86 {
87 global $context, $txt, $scripturl;
88
89 isAllowedTo('manage_bans');
90
91 loadTemplate('ManageBans');
92
93 $subActions = array(
94 'add' => 'BanEdit',
95 'browse' => 'BanBrowseTriggers',
96 'edittrigger' => 'BanEditTrigger',
97 'edit' => 'BanEdit',
98 'list' => 'BanList',
99 'log' => 'BanLog',
100 );
101
102 // Default the sub-action to 'view ban list'.
103 $_REQUEST['sa'] = isset($_REQUEST['sa']) && isset($subActions[$_REQUEST['sa']]) ? $_REQUEST['sa'] : 'list';
104
105 $context['page_title'] = $txt['ban_title'];
106 $context['sub_action'] = $_REQUEST['sa'];
107
108 // Tabs for browsing the different ban functions.
109 $context[$context['admin_menu_name']]['tab_data'] = array(
110 'title' => $txt['ban_title'],
111 'help' => 'ban_members',
112 'description' => $txt['ban_description'],
113 'tabs' => array(
114 'list' => array(
115 'description' => $txt['ban_description'],
116 'href' => $scripturl . '?action=admin;area=ban;sa=list',
117 'is_selected' => $_REQUEST['sa'] == 'list' || $_REQUEST['sa'] == 'edit' || $_REQUEST['sa'] == 'edittrigger',
118 ),
119 'add' => array(
120 'description' => $txt['ban_description'],
121 'href' => $scripturl . '?action=admin;area=ban;sa=add',
122 'is_selected' => $_REQUEST['sa'] == 'add',
123 ),
124 'browse' => array(
125 'description' => $txt['ban_trigger_browse_description'],
126 'href' => $scripturl . '?action=admin;area=ban;sa=browse',
127 'is_selected' => $_REQUEST['sa'] == 'browse',
128 ),
129 'log' => array(
130 'description' => $txt['ban_log_description'],
131 'href' => $scripturl . '?action=admin;area=ban;sa=log',
132 'is_selected' => $_REQUEST['sa'] == 'log',
133 'is_last' => true,
134 ),
135 ),
136 );
137
138 // Call the right function for this sub-acton.
139 $subActions[$_REQUEST['sa']]();
140 }
141
142 // List all the bans.
143 function BanList()
144 {
145 global $txt, $context, $ban_request, $ban_counts, $scripturl;
146 global $user_info, $smcFunc, $sourcedir;
147
148 // User pressed the 'remove selection button'.
149 if (!empty($_POST['removeBans']) && !empty($_POST['remove']) && is_array($_POST['remove']))
150 {
151 checkSession();
152
153 // Make sure every entry is a proper integer.
154 foreach ($_POST['remove'] as $index => $ban_id)
155 $_POST['remove'][(int) $index] = (int) $ban_id;
156
157 // Unban them all!
158 $smcFunc['db_query']('', '
159 DELETE FROM {db_prefix}ban_groups
160 WHERE id_ban_group IN ({array_int:ban_list})',
161 array(
162 'ban_list' => $_POST['remove'],
163 )
164 );
165 $smcFunc['db_query']('', '
166 DELETE FROM {db_prefix}ban_items
167 WHERE id_ban_group IN ({array_int:ban_list})',
168 array(
169 'ban_list' => $_POST['remove'],
170 )
171 );
172
173 // No more caching this ban!
174 updateSettings(array('banLastUpdated' => time()));
175
176 // Some members might be unbanned now. Update the members table.
177 updateBanMembers();
178 }
179
180 // Create a date string so we don't overload them with date info.
181 if (preg_match('~%[AaBbCcDdeGghjmuYy](?:[^%]*%[AaBbCcDdeGghjmuYy])*~', $user_info['time_format'], $matches) == 0 || empty($matches[0]))
182 $context['ban_time_format'] = $user_info['time_format'];
183 else
184 $context['ban_time_format'] = $matches[0];
185
186 $listOptions = array(
187 'id' => 'ban_list',
188 'items_per_page' => 20,
189 'base_href' => $scripturl . '?action=admin;area=ban;sa=list',
190 'default_sort_col' => 'added',
191 'default_sort_dir' => 'desc',
192 'get_items' => array(
193 'function' => 'list_getBans',
194 ),
195 'get_count' => array(
196 'function' => 'list_getNumBans',
197 ),
198 'no_items_label' => $txt['ban_no_entries'],
199 'columns' => array(
200 'name' => array(
201 'header' => array(
202 'value' => $txt['ban_name'],
203 ),
204 'data' => array(
205 'db' => 'name',
206 ),
207 'sort' => array(
208 'default' => 'bg.name',
209 'reverse' => 'bg.name DESC',
210 ),
211 ),
212 'notes' => array(
213 'header' => array(
214 'value' => $txt['ban_notes'],
215 ),
216 'data' => array(
217 'db' => 'notes',
218 'class' => 'smalltext',
219 ),
220 'sort' => array(
221 'default' => 'LENGTH(bg.notes) > 0 DESC, bg.notes',
222 'reverse' => 'LENGTH(bg.notes) > 0, bg.notes DESC',
223 ),
224 ),
225 'reason' => array(
226 'header' => array(
227 'value' => $txt['ban_reason'],
228 ),
229 'data' => array(
230 'db' => 'reason',
231 'class' => 'smalltext',
232 ),
233 'sort' => array(
234 'default' => 'LENGTH(bg.reason) > 0 DESC, bg.reason',
235 'reverse' => 'LENGTH(bg.reason) > 0, bg.reason DESC',
236 ),
237 ),
238 'added' => array(
239 'header' => array(
240 'value' => $txt['ban_added'],
241 ),
242 'data' => array(
243 'function' => create_function('$rowData', '
244 global $context;
245
246 return timeformat($rowData[\'ban_time\'], empty($context[\'ban_time_format\']) ? true : $context[\'ban_time_format\']);
247 '),
248 ),
249 'sort' => array(
250 'default' => 'bg.ban_time',
251 'reverse' => 'bg.ban_time DESC',
252 ),
253 ),
254 'expires' => array(
255 'header' => array(
256 'value' => $txt['ban_expires'],
257 ),
258 'data' => array(
259 'function' => create_function('$rowData', '
260 global $txt;
261
262 // This ban never expires...whahaha.
263 if ($rowData[\'expire_time\'] === null)
264 return $txt[\'never\'];
265
266 // This ban has already expired.
267 elseif ($rowData[\'expire_time\'] < time())
268 return sprintf(\'<span style="color: red">%1$s</span>\', $txt[\'ban_expired\']);
269
270 // Still need to wait a few days for this ban to expire.
271 else
272 return sprintf(\'%1$d&nbsp;%2$s\', ceil(($rowData[\'expire_time\'] - time()) / (60 * 60 * 24)), $txt[\'ban_days\']);
273 '),
274 ),
275 'sort' => array(
276 'default' => 'IFNULL(bg.expire_time, 1=1) DESC, bg.expire_time DESC',
277 'reverse' => 'IFNULL(bg.expire_time, 1=1), bg.expire_time',
278 ),
279 ),
280 'num_triggers' => array(
281 'header' => array(
282 'value' => $txt['ban_triggers'],
283 ),
284 'data' => array(
285 'db' => 'num_triggers',
286 'style' => 'text-align: center;',
287 ),
288 'sort' => array(
289 'default' => 'num_triggers DESC',
290 'reverse' => 'num_triggers',
291 ),
292 ),
293 'actions' => array(
294 'header' => array(
295 'value' => $txt['ban_actions'],
296 ),
297 'data' => array(
298 'sprintf' => array(
299 'format' => '<a href="' . $scripturl . '?action=admin;area=ban;sa=edit;bg=%1$d">' . $txt['modify'] . '</a>',
300 'params' => array(
301 'id_ban_group' => false,
302 ),
303 ),
304 'style' => 'text-align: center;',
305 ),
306 ),
307 'check' => array(
308 'header' => array(
309 'value' => '<input type="checkbox" onclick="invertAll(this, this.form);" class="input_check" />',
310 ),
311 'data' => array(
312 'sprintf' => array(
313 'format' => '<input type="checkbox" name="remove[]" value="%1$d" class="input_check" />',
314 'params' => array(
315 'id_ban_group' => false,
316 ),
317 ),
318 'style' => 'text-align: center',
319 ),
320 ),
321 ),
322 'form' => array(
323 'href' => $scripturl . '?action=admin;area=ban;sa=list',
324 ),
325 'additional_rows' => array(
326 array(
327 'position' => 'below_table_data',
328 'value' => '<input type="submit" name="removeBans" value="' . $txt['ban_remove_selected'] . '" onclick="return confirm(\'' . $txt['ban_remove_selected_confirm'] . '\');" class="button_submit" />',
329 'style' => 'text-align: right;',
330 ),
331 ),
332 );
333
334 require_once($sourcedir . '/Subs-List.php');
335 createList($listOptions);
336
337 $context['sub_template'] = 'show_list';
338 $context['default_list'] = 'ban_list';
339 }
340
341 function list_getBans($start, $items_per_page, $sort)
342 {
343 global $smcFunc;
344
345 $request = $smcFunc['db_query']('', '
346 SELECT bg.id_ban_group, bg.name, bg.ban_time, bg.expire_time, bg.reason, bg.notes, COUNT(bi.id_ban) AS num_triggers
347 FROM {db_prefix}ban_groups AS bg
348 LEFT JOIN {db_prefix}ban_items AS bi ON (bi.id_ban_group = bg.id_ban_group)
349 GROUP BY bg.id_ban_group, bg.name, bg.ban_time, bg.expire_time, bg.reason, bg.notes
350 ORDER BY {raw:sort}
351 LIMIT {int:offset}, {int:limit}',
352 array(
353 'sort' => $sort,
354 'offset' => $start,
355 'limit' => $items_per_page,
356 )
357 );
358 $bans = array();
359 while ($row = $smcFunc['db_fetch_assoc']($request))
360 $bans[] = $row;
361
362 $smcFunc['db_free_result']($request);
363
364 return $bans;
365 }
366
367 function list_getNumBans()
368 {
369 global $smcFunc;
370
371 $request = $smcFunc['db_query']('', '
372 SELECT COUNT(*) AS num_bans
373 FROM {db_prefix}ban_groups',
374 array(
375 )
376 );
377 list ($numBans) = $smcFunc['db_fetch_row']($request);
378 $smcFunc['db_free_result']($request);
379
380 return $numBans;
381 }
382
383 function BanEdit()
384 {
385 global $txt, $modSettings, $context, $ban_request, $scripturl, $smcFunc;
386
387 $_REQUEST['bg'] = empty($_REQUEST['bg']) ? 0 : (int) $_REQUEST['bg'];
388
389 // Adding or editing a ban trigger?
390 if (!empty($_POST['add_new_trigger']) || !empty($_POST['edit_trigger']))
391 {
392 checkSession();
393
394 $newBan = !empty($_POST['add_new_trigger']);
395 $values = array(
396 'id_ban_group' => $_REQUEST['bg'],
397 'hostname' => '',
398 'email_address' => '',
399 'id_member' => 0,
400 'ip_low1' => 0,
401 'ip_high1' => 0,
402 'ip_low2' => 0,
403 'ip_high2' => 0,
404 'ip_low3' => 0,
405 'ip_high3' => 0,
406 'ip_low4' => 0,
407 'ip_high4' => 0,
408 );
409
410 // Preset all values that are required.
411 if ($newBan)
412 {
413 $insertKeys = array(
414 'id_ban_group' => 'int',
415 'hostname' => 'string',
416 'email_address' => 'string',
417 'id_member' => 'int',
418 'ip_low1' => 'int',
419 'ip_high1' => 'int',
420 'ip_low2' => 'int',
421 'ip_high2' => 'int',
422 'ip_low3' => 'int',
423 'ip_high3' => 'int',
424 'ip_low4' => 'int',
425 'ip_high4' => 'int',
426 );
427 }
428 else
429 $updateString = '
430 hostname = {string:hostname}, email_address = {string:email_address}, id_member = {int:id_member},
431 ip_low1 = {int:ip_low1}, ip_high1 = {int:ip_high1},
432 ip_low2 = {int:ip_low2}, ip_high2 = {int:ip_high2},
433 ip_low3 = {int:ip_low3}, ip_high3 = {int:ip_high3},
434 ip_low4 = {int:ip_low4}, ip_high4 = {int:ip_high4}';
435
436 if ($_POST['bantype'] == 'ip_ban')
437 {
438 $ip = trim($_POST['ip']);
439 $ip_parts = ip2range($ip);
440 $ip_check = checkExistingTriggerIP($ip_parts, $ip);
441 if (!$ip_check)
442 fatal_lang_error('invalid_ip', false);
443 $values = array_merge($values, $ip_check);
444
445 $modlogInfo['ip_range'] = $_POST['ip'];
446 }
447 elseif ($_POST['bantype'] == 'hostname_ban')
448 {
449 if (preg_match('/[^\w.\-*]/', $_POST['hostname']) == 1)
450 fatal_lang_error('invalid_hostname', false);
451
452 // Replace the * wildcard by a MySQL compatible wildcard %.
453 $_POST['hostname'] = str_replace('*', '%', $_POST['hostname']);
454
455 $values['hostname'] = $_POST['hostname'];
456
457 $modlogInfo['hostname'] = $_POST['hostname'];
458 }
459 elseif ($_POST['bantype'] == 'email_ban')
460 {
461 if (preg_match('/[^\w.\-\+*@]/', $_POST['email']) == 1)
462 fatal_lang_error('invalid_email', false);
463 $_POST['email'] = strtolower(str_replace('*', '%', $_POST['email']));
464
465 // Check the user is not banning an admin.
466 $request = $smcFunc['db_query']('', '
467 SELECT id_member
468 FROM {db_prefix}members
469 WHERE (id_group = {int:admin_group} OR FIND_IN_SET({int:admin_group}, additional_groups) != 0)
470 AND email_address LIKE {string:email}
471 LIMIT 1',
472 array(
473 'admin_group' => 1,
474 'email' => $_POST['email'],
475 )
476 );
477 if ($smcFunc['db_num_rows']($request) != 0)
478 fatal_lang_error('no_ban_admin', 'critical');
479 $smcFunc['db_free_result']($request);
480
481 $values['email_address'] = $_POST['email'];
482
483 $modlogInfo['email'] = $_POST['email'];
484 }
485 elseif ($_POST['bantype'] == 'user_ban')
486 {
487 $_POST['user'] = preg_replace('~&amp;#(\d{4,5}|[2-9]\d{2,4}|1[2-9]\d);~', '&#$1;', $smcFunc['htmlspecialchars']($_POST['user'], ENT_QUOTES));
488
489 $request = $smcFunc['db_query']('', '
490 SELECT id_member, (id_group = {int:admin_group} OR FIND_IN_SET({int:admin_group}, additional_groups) != 0) AS isAdmin
491 FROM {db_prefix}members
492 WHERE member_name = {string:user_name} OR real_name = {string:user_name}
493 LIMIT 1',
494 array(
495 'admin_group' => 1,
496 'user_name' => $_POST['user'],
497 )
498 );
499 if ($smcFunc['db_num_rows']($request) == 0)
500 fatal_lang_error('invalid_username', false);
501 list ($memberid, $isAdmin) = $smcFunc['db_fetch_row']($request);
502 $smcFunc['db_free_result']($request);
503
504 if ($isAdmin && $isAdmin != 'f')
505 fatal_lang_error('no_ban_admin', 'critical');
506
507 $values['id_member'] = $memberid;
508
509 $modlogInfo['member'] = $memberid;
510 }
511 else
512 fatal_lang_error('no_bantype_selected', false);
513
514 if ($newBan)
515 $smcFunc['db_insert']('',
516 '{db_prefix}ban_items',
517 $insertKeys,
518 $values,
519 array('id_ban')
520 );
521 else
522 $smcFunc['db_query']('', '
523 UPDATE {db_prefix}ban_items
524 SET ' . $updateString . '
525 WHERE id_ban = {int:ban_item}
526 AND id_ban_group = {int:id_ban_group}',
527 array_merge($values, array(
528 'ban_item' => (int) $_REQUEST['bi'],
529 ))
530 );
531
532 // Log the addion of the ban entry into the moderation log.
533 logAction('ban', $modlogInfo + array(
534 'new' => $newBan,
535 'type' => $_POST['bantype'],
536 ));
537
538 // Register the last modified date.
539 updateSettings(array('banLastUpdated' => time()));
540
541 // Update the member table to represent the new ban situation.
542 updateBanMembers();
543 }
544
545 // The user pressed 'Remove selected ban entries'.
546 elseif (!empty($_POST['remove_selection']) && !empty($_POST['ban_items']) && is_array($_POST['ban_items']))
547 {
548 checkSession();
549
550 // Making sure every deleted ban item is an integer.
551 foreach ($_POST['ban_items'] as $key => $value)
552 $_POST['ban_items'][$key] = (int) $value;
553
554 $smcFunc['db_query']('', '
555 DELETE FROM {db_prefix}ban_items
556 WHERE id_ban IN ({array_int:ban_list})
557 AND id_ban_group = {int:ban_group}',
558 array(
559 'ban_list' => $_POST['ban_items'],
560 'ban_group' => $_REQUEST['bg'],
561 )
562 );
563
564 // It changed, let the settings and the member table know.
565 updateSettings(array('banLastUpdated' => time()));
566 updateBanMembers();
567 }
568
569 // Modify OR add a ban.
570 elseif (!empty($_POST['modify_ban']) || !empty($_POST['add_ban']))
571 {
572 checkSession();
573
574 $addBan = !empty($_POST['add_ban']);
575 if (empty($_POST['ban_name']))
576 fatal_lang_error('ban_name_empty', false);
577
578 // Let's not allow HTML in ban names, it's more evil than beneficial.
579 $_POST['ban_name'] = $smcFunc['htmlspecialchars']($_POST['ban_name'], ENT_QUOTES);
580
581 // Check whether a ban with this name already exists.
582 $request = $smcFunc['db_query']('', '
583 SELECT id_ban_group
584 FROM {db_prefix}ban_groups
585 WHERE name = {string:new_ban_name}' . ($addBan ? '' : '
586 AND id_ban_group != {int:ban_group}') . '
587 LIMIT 1',
588 array(
589 'ban_group' => $_REQUEST['bg'],
590 'new_ban_name' => $_POST['ban_name'],
591 )
592 );
593 if ($smcFunc['db_num_rows']($request) == 1)
594 fatal_lang_error('ban_name_exists', false, array($_POST['ban_name']));
595 $smcFunc['db_free_result']($request);
596
597 $_POST['reason'] = $smcFunc['htmlspecialchars']($_POST['reason'], ENT_QUOTES);
598 $_POST['notes'] = $smcFunc['htmlspecialchars']($_POST['notes'], ENT_QUOTES);
599 $_POST['notes'] = str_replace(array("\r", "\n", ' '), array('', '<br />', '&nbsp; '), $_POST['notes']);
600 $_POST['expiration'] = $_POST['expiration'] == 'never' ? 'NULL' : ($_POST['expiration'] == 'expired' ? '0' : ($_POST['expire_date'] != $_POST['old_expire'] ? time() + 24 * 60 * 60 * (int) $_POST['expire_date'] : 'expire_time'));
601 $_POST['full_ban'] = empty($_POST['full_ban']) ? '0' : '1';
602 $_POST['cannot_post'] = !empty($_POST['full_ban']) || empty($_POST['cannot_post']) ? '0' : '1';
603 $_POST['cannot_register'] = !empty($_POST['full_ban']) || empty($_POST['cannot_register']) ? '0' : '1';
604 $_POST['cannot_login'] = !empty($_POST['full_ban']) || empty($_POST['cannot_login']) ? '0' : '1';
605
606 if ($addBan)
607 {
608 // Adding some ban triggers?
609 if ($addBan && !empty($_POST['ban_suggestion']) && is_array($_POST['ban_suggestion']))
610 {
611 $ban_triggers = array();
612 $ban_logs = array();
613 if (in_array('main_ip', $_POST['ban_suggestion']) && !empty($_POST['main_ip']))
614 {
615 $ip = trim($_POST['main_ip']);
616 $ip_parts = ip2range($ip);
617 if (!checkExistingTriggerIP($ip_parts, $ip))
618 fatal_lang_error('invalid_ip', false);
619
620 $ban_triggers[] = array(
621 $ip_parts[0]['low'],
622 $ip_parts[0]['high'],
623 $ip_parts[1]['low'],
624 $ip_parts[1]['high'],
625 $ip_parts[2]['low'],
626 $ip_parts[2]['high'],
627 $ip_parts[3]['low'],
628 $ip_parts[3]['high'],
629 '',
630 '',
631 0,
632 );
633
634 $ban_logs[] = array(
635 'ip_range' => $_POST['main_ip'],
636 );
637 }
638 if (in_array('hostname', $_POST['ban_suggestion']) && !empty($_POST['hostname']))
639 {
640 if (preg_match('/[^\w.\-*]/', $_POST['hostname']) == 1)
641 fatal_lang_error('invalid_hostname', false);
642
643 // Replace the * wildcard by a MySQL wildcard %.
644 $_POST['hostname'] = str_replace('*', '%', $_POST['hostname']);
645
646 $ban_triggers[] = array(
647 0, 0, 0, 0, 0, 0, 0, 0,
648 substr($_POST['hostname'], 0, 255),
649 '',
650 0,
651 );
652 $ban_logs[] = array(
653 'hostname' => $_POST['hostname'],
654 );
655 }
656 if (in_array('email', $_POST['ban_suggestion']) && !empty($_POST['email']))
657 {
658 if (preg_match('/[^\w.\-\+*@]/', $_POST['email']) == 1)
659 fatal_lang_error('invalid_email', false);
660 $_POST['email'] = strtolower(str_replace('*', '%', $_POST['email']));
661
662 $ban_triggers[] = array(
663 0, 0, 0, 0, 0, 0, 0, 0,
664 '',
665 substr($_POST['email'], 0, 255),
666 0,
667 );
668 $ban_logs[] = array(
669 'email' => $_POST['email'],
670 );
671 }
672 if (in_array('user', $_POST['ban_suggestion']) && (!empty($_POST['bannedUser']) || !empty($_POST['user'])))
673 {
674 // We got a username, let's find its ID.
675 if (empty($_POST['bannedUser']))
676 {
677 $_POST['user'] = preg_replace('~&amp;#(\d{4,5}|[2-9]\d{2,4}|1[2-9]\d);~', '&#$1;', $smcFunc['htmlspecialchars']($_POST['user'], ENT_QUOTES));
678
679 $request = $smcFunc['db_query']('', '
680 SELECT id_member, (id_group = {int:admin_group} OR FIND_IN_SET({int:admin_group}, additional_groups) != 0) AS isAdmin
681 FROM {db_prefix}members
682 WHERE member_name = {string:username} OR real_name = {string:username}
683 LIMIT 1',
684 array(
685 'admin_group' => 1,
686 'username' => $_POST['user'],
687 )
688 );
689 if ($smcFunc['db_num_rows']($request) == 0)
690 fatal_lang_error('invalid_username', false);
691 list ($_POST['bannedUser'], $isAdmin) = $smcFunc['db_fetch_row']($request);
692 $smcFunc['db_free_result']($request);
693
694 if ($isAdmin && $isAdmin != 'f')
695 fatal_lang_error('no_ban_admin', 'critical');
696 }
697
698 $ban_triggers[] = array(
699 0, 0, 0, 0, 0, 0, 0, 0,
700 '',
701 '',
702 (int) $_POST['bannedUser'],
703 );
704 $ban_logs[] = array(
705 'member' => $_POST['bannedUser'],
706 );
707 }
708
709 if (!empty($_POST['ban_suggestion']['ips']) && is_array($_POST['ban_suggestion']['ips']))
710 {
711 $_POST['ban_suggestion']['ips'] = array_unique($_POST['ban_suggestion']['ips']);
712
713 // Don't add the main IP again.
714 if (in_array('main_ip', $_POST['ban_suggestion']))
715 $_POST['ban_suggestion']['ips'] = array_diff($_POST['ban_suggestion']['ips'], array($_POST['main_ip']));
716
717 foreach ($_POST['ban_suggestion']['ips'] as $ip)
718 {
719 $ip_parts = ip2range($ip);
720
721 // They should be alright, but just to be sure...
722 if (count($ip_parts) != 4)
723 fatal_lang_error('invalid_ip', false);
724
725 $ban_triggers[] = array(
726 $ip_parts[0]['low'],
727 $ip_parts[0]['high'],
728 $ip_parts[1]['low'],
729 $ip_parts[1]['high'],
730 $ip_parts[2]['low'],
731 $ip_parts[2]['high'],
732 $ip_parts[3]['low'],
733 $ip_parts[3]['high'],
734 '',
735 '',
736 0,
737 );
738 $ban_logs[] = array(
739 'ip_range' => $ip,
740 );
741 }
742 }
743 }
744
745 // Yes yes, we're ready to add now.
746 $smcFunc['db_insert']('',
747 '{db_prefix}ban_groups',
748 array(
749 'name' => 'string-20', 'ban_time' => 'int', 'expire_time' => 'raw', 'cannot_access' => 'int', 'cannot_register' => 'int',
750 'cannot_post' => 'int', 'cannot_login' => 'int', 'reason' => 'string-255', 'notes' => 'string-65534',
751 ),
752 array(
753 $_POST['ban_name'], time(), $_POST['expiration'], $_POST['full_ban'], $_POST['cannot_register'],
754 $_POST['cannot_post'], $_POST['cannot_login'], $_POST['reason'], $_POST['notes'],
755 ),
756 array('id_ban_group')
757 );
758 $_REQUEST['bg'] = $smcFunc['db_insert_id']('{db_prefix}ban_groups', 'id_ban_group');
759
760 // Now that the ban group is added, add some triggers as well.
761 if (!empty($ban_triggers) && !empty($_REQUEST['bg']))
762 {
763 // Put in the ban group ID.
764 foreach ($ban_triggers as $k => $trigger)
765 array_unshift($ban_triggers[$k], $_REQUEST['bg']);
766
767 // Log what we are doing!
768 foreach ($ban_logs as $log_details)
769 logAction('ban', $log_details + array('new' => 1));
770
771 $smcFunc['db_insert']('',
772 '{db_prefix}ban_items',
773 array(
774 'id_ban_group' => 'int', 'ip_low1' => 'int', 'ip_high1' => 'int', 'ip_low2' => 'int', 'ip_high2' => 'int',
775 'ip_low3' => 'int', 'ip_high3' => 'int', 'ip_low4' => 'int', 'ip_high4' => 'int', 'hostname' => 'string-255',
776 'email_address' => 'string-255', 'id_member' => 'int',
777 ),
778 $ban_triggers,
779 array('id_ban')
780 );
781 }
782 }
783 else
784 $smcFunc['db_query']('', '
785 UPDATE {db_prefix}ban_groups
786 SET
787 name = {string:ban_name},
788 reason = {string:reason},
789 notes = {string:notes},
790 expire_time = {raw:expiration},
791 cannot_access = {int:cannot_access},
792 cannot_post = {int:cannot_post},
793 cannot_register = {int:cannot_register},
794 cannot_login = {int:cannot_login}
795 WHERE id_ban_group = {int:id_ban_group}',
796 array(
797 'expiration' => $_POST['expiration'],
798 'cannot_access' => $_POST['full_ban'],
799 'cannot_post' => $_POST['cannot_post'],
800 'cannot_register' => $_POST['cannot_register'],
801 'cannot_login' => $_POST['cannot_login'],
802 'id_ban_group' => $_REQUEST['bg'],
803 'ban_name' => $_POST['ban_name'],
804 'reason' => $_POST['reason'],
805 'notes' => $_POST['notes'],
806 )
807 );
808
809 // No more caching, we have something new here.
810 updateSettings(array('banLastUpdated' => time()));
811 updateBanMembers();
812 }
813
814 // If we're editing an existing ban, get it from the database.
815 if (!empty($_REQUEST['bg']))
816 {
817 $context['ban_items'] = array();
818 $request = $smcFunc['db_query']('', '
819 SELECT
820 bi.id_ban, bi.hostname, bi.email_address, bi.id_member, bi.hits,
821 bi.ip_low1, bi.ip_high1, bi.ip_low2, bi.ip_high2, bi.ip_low3, bi.ip_high3, bi.ip_low4, bi.ip_high4,
822 bg.id_ban_group, bg.name, bg.ban_time, bg.expire_time, bg.reason, bg.notes, bg.cannot_access, bg.cannot_register, bg.cannot_login, bg.cannot_post,
823 IFNULL(mem.id_member, 0) AS id_member, mem.member_name, mem.real_name
824 FROM {db_prefix}ban_groups AS bg
825 LEFT JOIN {db_prefix}ban_items AS bi ON (bi.id_ban_group = bg.id_ban_group)
826 LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = bi.id_member)
827 WHERE bg.id_ban_group = {int:current_ban}',
828 array(
829 'current_ban' => $_REQUEST['bg'],
830 )
831 );
832 if ($smcFunc['db_num_rows']($request) == 0)
833 fatal_lang_error('ban_not_found', false);
834
835 while ($row = $smcFunc['db_fetch_assoc']($request))
836 {
837 if (!isset($context['ban']))
838 {
839 $context['ban'] = array(
840 'id' => $row['id_ban_group'],
841 'name' => $row['name'],
842 'expiration' => array(
843 'status' => $row['expire_time'] === null ? 'never' : ($row['expire_time'] < time() ? 'expired' : 'still_active_but_we_re_counting_the_days'),
844 'days' => $row['expire_time'] > time() ? floor(($row['expire_time'] - time()) / 86400) : 0
845 ),
846 'reason' => $row['reason'],
847 'notes' => $row['notes'],
848 'cannot' => array(
849 'access' => !empty($row['cannot_access']),
850 'post' => !empty($row['cannot_post']),
851 'register' => !empty($row['cannot_register']),
852 'login' => !empty($row['cannot_login']),
853 ),
854 'is_new' => false,
855 );
856 }
857 if (!empty($row['id_ban']))
858 {
859 $context['ban_items'][$row['id_ban']] = array(
860 'id' => $row['id_ban'],
861 'hits' => $row['hits'],
862 );
863 if (!empty($row['ip_high1']))
864 {
865 $context['ban_items'][$row['id_ban']]['type'] = 'ip';
866 $context['ban_items'][$row['id_ban']]['ip'] = range2ip(array($row['ip_low1'], $row['ip_low2'], $row['ip_low3'], $row['ip_low4']), array($row['ip_high1'], $row['ip_high2'], $row['ip_high3'], $row['ip_high4']));
867 }
868 elseif (!empty($row['hostname']))
869 {
870 $context['ban_items'][$row['id_ban']]['type'] = 'hostname';
871 $context['ban_items'][$row['id_ban']]['hostname'] = str_replace('%', '*', $row['hostname']);
872 }
873 elseif (!empty($row['email_address']))
874 {
875 $context['ban_items'][$row['id_ban']]['type'] = 'email';
876 $context['ban_items'][$row['id_ban']]['email'] = str_replace('%', '*', $row['email_address']);
877 }
878 elseif (!empty($row['id_member']))
879 {
880 $context['ban_items'][$row['id_ban']]['type'] = 'user';
881 $context['ban_items'][$row['id_ban']]['user'] = array(
882 'id' => $row['id_member'],
883 'name' => $row['real_name'],
884 'href' => $scripturl . '?action=profile;u=' . $row['id_member'],
885 'link' => '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['real_name'] . '</a>',
886 );
887 }
888 // Invalid ban (member probably doesn't exist anymore).
889 else
890 {
891 unset($context['ban_items'][$row['id_ban']]);
892 $smcFunc['db_query']('', '
893 DELETE FROM {db_prefix}ban_items
894 WHERE id_ban = {int:current_ban}',
895 array(
896 'current_ban' => $row['id_ban'],
897 )
898 );
899 }
900 }
901 }
902 $smcFunc['db_free_result']($request);
903 }
904 // Not an existing one, then it's probably a new one.
905 else
906 {
907 $context['ban'] = array(
908 'id' => 0,
909 'name' => '',
910 'expiration' => array(
911 'status' => 'never',
912 'days' => 0
913 ),
914 'reason' => '',
915 'notes' => '',
916 'ban_days' => 0,
917 'cannot' => array(
918 'access' => true,
919 'post' => false,
920 'register' => false,
921 'login' => false,
922 ),
923 'is_new' => true,
924 );
925 $context['ban_suggestions'] = array(
926 'main_ip' => '',
927 'hostname' => '',
928 'email' => '',
929 'member' => array(
930 'id' => 0,
931 ),
932 );
933
934 // Overwrite some of the default form values if a user ID was given.
935 if (!empty($_REQUEST['u']))
936 {
937 $request = $smcFunc['db_query']('', '
938 SELECT id_member, real_name, member_ip, email_address
939 FROM {db_prefix}members
940 WHERE id_member = {int:current_user}
941 LIMIT 1',
942 array(
943 'current_user' => (int) $_REQUEST['u'],
944 )
945 );
946 if ($smcFunc['db_num_rows']($request) > 0)
947 list ($context['ban_suggestions']['member']['id'], $context['ban_suggestions']['member']['name'], $context['ban_suggestions']['main_ip'], $context['ban_suggestions']['email']) = $smcFunc['db_fetch_row']($request);
948 $smcFunc['db_free_result']($request);
949
950 if (!empty($context['ban_suggestions']['member']['id']))
951 {
952 $context['ban_suggestions']['href'] = $scripturl . '?action=profile;u=' . $context['ban_suggestions']['member']['id'];
953 $context['ban_suggestions']['member']['link'] = '<a href="' . $context['ban_suggestions']['href'] . '">' . $context['ban_suggestions']['member']['name'] . '</a>';
954
955 // Default the ban name to the name of the banned member.
956 $context['ban']['name'] = $context['ban_suggestions']['member']['name'];
957
958 // Would be nice if we could also ban the hostname.
959 if (preg_match('/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/', $context['ban_suggestions']['main_ip']) == 1 && empty($modSettings['disableHostnameLookup']))
960 $context['ban_suggestions']['hostname'] = host_from_ip($context['ban_suggestions']['main_ip']);
961
962 // Find some additional IP's used by this member.
963 $context['ban_suggestions']['message_ips'] = array();
964 $request = $smcFunc['db_query']('ban_suggest_message_ips', '
965 SELECT DISTINCT poster_ip
966 FROM {db_prefix}messages
967 WHERE id_member = {int:current_user}
968 AND poster_ip RLIKE {string:poster_ip_regex}
969 ORDER BY poster_ip',
970 array(
971 'current_user' => (int) $_REQUEST['u'],
972 'poster_ip_regex' => '^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$',
973 )
974 );
975 while ($row = $smcFunc['db_fetch_assoc']($request))
976 $context['ban_suggestions']['message_ips'][] = $row['poster_ip'];
977 $smcFunc['db_free_result']($request);
978
979 $context['ban_suggestions']['error_ips'] = array();
980 $request = $smcFunc['db_query']('ban_suggest_error_ips', '
981 SELECT DISTINCT ip
982 FROM {db_prefix}log_errors
983 WHERE id_member = {int:current_user}
984 AND ip RLIKE {string:poster_ip_regex}
985 ORDER BY ip',
986 array(
987 'current_user' => (int) $_REQUEST['u'],
988 'poster_ip_regex' => '^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$',
989 )
990 );
991 while ($row = $smcFunc['db_fetch_assoc']($request))
992 $context['ban_suggestions']['error_ips'][] = $row['ip'];
993 $smcFunc['db_free_result']($request);
994
995 // Borrowing a few language strings from profile.
996 loadLanguage('Profile');
997 }
998 }
999 }
1000
1001 // Template needs this to show errors using javascript
1002 loadLanguage('Errors');
1003
1004 // If we're in wireless mode remove the admin template layer and use a special template.
1005 if (WIRELESS && WIRELESS_PROTOCOL != 'wap')
1006 {
1007 $context['sub_template'] = WIRELESS_PROTOCOL . '_ban_edit';
1008 foreach ($context['template_layers'] as $k => $v)
1009 if (strpos($v, 'generic_menu') === 0)
1010 unset($context['template_layers'][$k]);
1011 }
1012 else
1013 $context['sub_template'] = 'ban_edit';
1014 }
1015
1016 function BanEditTrigger()
1017 {
1018 global $context, $smcFunc;
1019
1020 $context['sub_template'] = 'ban_edit_trigger';
1021
1022 if (empty($_REQUEST['bg']))
1023 fatal_lang_error('ban_not_found', false);
1024
1025 if (empty($_REQUEST['bi']))
1026 {
1027 $context['ban_trigger'] = array(
1028 'id' => 0,
1029 'group' => (int) $_REQUEST['bg'],
1030 'ip' => array(
1031 'value' => '',
1032 'selected' => true,
1033 ),
1034 'hostname' => array(
1035 'selected' => false,
1036 'value' => '',
1037 ),
1038 'email' => array(
1039 'value' => '',
1040 'selected' => false,
1041 ),
1042 'banneduser' => array(
1043 'value' => '',
1044 'selected' => false,
1045 ),
1046 'is_new' => true,
1047 );
1048 }
1049 else
1050 {
1051 $request = $smcFunc['db_query']('', '
1052 SELECT
1053 bi.id_ban, bi.id_ban_group, bi.hostname, bi.email_address, bi.id_member,
1054 bi.ip_low1, bi.ip_high1, bi.ip_low2, bi.ip_high2, bi.ip_low3, bi.ip_high3, bi.ip_low4, bi.ip_high4,
1055 mem.member_name, mem.real_name
1056 FROM {db_prefix}ban_items AS bi
1057 LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = bi.id_member)
1058 WHERE bi.id_ban = {int:ban_item}
1059 AND bi.id_ban_group = {int:ban_group}
1060 LIMIT 1',
1061 array(
1062 'ban_item' => (int) $_REQUEST['bi'],
1063 'ban_group' => (int) $_REQUEST['bg'],
1064 )
1065 );
1066 if ($smcFunc['db_num_rows']($request) == 0)
1067 fatal_lang_error('ban_not_found', false);
1068 $row = $smcFunc['db_fetch_assoc']($request);
1069 $smcFunc['db_free_result']($request);
1070
1071 $context['ban_trigger'] = array(
1072 'id' => $row['id_ban'],
1073 'group' => $row['id_ban_group'],
1074 'ip' => array(
1075 'value' => empty($row['ip_low1']) ? '' : range2ip(array($row['ip_low1'], $row['ip_low2'], $row['ip_low3'], $row['ip_low4']), array($row['ip_high1'], $row['ip_high2'], $row['ip_high3'], $row['ip_high4'])),
1076 'selected' => !empty($row['ip_low1']),
1077 ),
1078 'hostname' => array(
1079 'value' => str_replace('%', '*', $row['hostname']),
1080 'selected' => !empty($row['hostname']),
1081 ),
1082 'email' => array(
1083 'value' => str_replace('%', '*', $row['email_address']),
1084 'selected' => !empty($row['email_address'])
1085 ),
1086 'banneduser' => array(
1087 'value' => $row['member_name'],
1088 'selected' => !empty($row['member_name'])
1089 ),
1090 'is_new' => false,
1091 );
1092 }
1093 }
1094
1095 function BanBrowseTriggers()
1096 {
1097 global $modSettings, $context, $scripturl, $smcFunc, $txt;
1098 global $sourcedir, $settings;
1099
1100 if (!empty($_POST['remove_triggers']) && !empty($_POST['remove']) && is_array($_POST['remove']))
1101 {
1102 checkSession();
1103
1104 // Clean the integers.
1105 foreach ($_POST['remove'] as $key => $value)
1106 $_POST['remove'][$key] = $value;
1107
1108 $smcFunc['db_query']('', '
1109 DELETE FROM {db_prefix}ban_items
1110 WHERE id_ban IN ({array_int:ban_list})',
1111 array(
1112 'ban_list' => $_POST['remove'],
1113 )
1114 );
1115
1116 // Rehabilitate some members.
1117 if ($_REQUEST['entity'] == 'member')
1118 updateBanMembers();
1119
1120 // Make sure the ban cache is refreshed.
1121 updateSettings(array('banLastUpdated' => time()));
1122 }
1123
1124 $context['selected_entity'] = isset($_REQUEST['entity']) && in_array($_REQUEST['entity'], array('ip', 'hostname', 'email', 'member')) ? $_REQUEST['entity'] : 'ip';
1125
1126 $listOptions = array(
1127 'id' => 'ban_trigger_list',
1128 'title' => $txt['ban_trigger_browse'],
1129 'items_per_page' => $modSettings['defaultMaxMessages'],
1130 'base_href' => $scripturl . '?action=admin;area=ban;sa=browse;entity=' . $context['selected_entity'],
1131 'default_sort_col' => 'banned_entity',
1132 'no_items_label' => $txt['ban_no_triggers'],
1133 'get_items' => array(
1134 'function' => 'list_getBanTriggers',
1135 'params' => array(
1136 $context['selected_entity'],
1137 ),
1138 ),
1139 'get_count' => array(
1140 'function' => 'list_getNumBanTriggers',
1141 'params' => array(
1142 $context['selected_entity'],
1143 ),
1144 ),
1145 'columns' => array(
1146 'banned_entity' => array(
1147 'header' => array(
1148 'value' => $txt['ban_banned_entity'],
1149 ),
1150 ),
1151 'ban_name' => array(
1152 'header' => array(
1153 'value' => $txt['ban_name'],
1154 ),
1155 'data' => array(
1156 'sprintf' => array(
1157 'format' => '<a href="' . $scripturl . '?action=admin;area=ban;sa=edit;bg=%1$d">%2$s</a>',
1158 'params' => array(
1159 'id_ban_group' => false,
1160 'name' => false,
1161 ),
1162 ),
1163 ),
1164 'sort' => array(
1165 'default' => 'bg.name',
1166 'reverse' => 'bg.name DESC',
1167 ),
1168 ),
1169 'hits' => array(
1170 'header' => array(
1171 'value' => $txt['ban_hits'],
1172 ),
1173 'data' => array(
1174 'db' => 'hits',
1175 'style' => 'text-align: center;',
1176 ),
1177 'sort' => array(
1178 'default' => 'bi.hits DESC',
1179 'reverse' => 'bi.hits',
1180 ),
1181 ),
1182 'check' => array(
1183 'header' => array(
1184 'value' => '<input type="checkbox" onclick="invertAll(this, this.form);" class="input_check" />',
1185 ),
1186 'data' => array(
1187 'sprintf' => array(
1188 'format' => '<input type="checkbox" name="remove[]" value="%1$d" class="input_check" />',
1189 'params' => array(
1190 'id_ban' => false,
1191 ),
1192 ),
1193 'style' => 'text-align: center',
1194 ),
1195 ),
1196 ),
1197 'form' => array(
1198 'href' => $scripturl . '?action=admin;area=ban;sa=browse;entity=' . $context['selected_entity'],
1199 'include_start' => true,
1200 'include_sort' => true,
1201 ),
1202 'additional_rows' => array(
1203 array(
1204 'position' => 'above_column_headers',
1205 'value' => '<a href="' . $scripturl . '?action=admin;area=ban;sa=browse;entity=ip">' . ($context['selected_entity'] == 'ip' ? '<img src="' . $settings['images_url'] . '/selected.gif" alt="&gt;" /> ' : '') . $txt['ip'] . '</a>&nbsp;|&nbsp;<a href="' . $scripturl . '?action=admin;area=ban;sa=browse;entity=hostname">' . ($context['selected_entity'] == 'hostname' ? '<img src="' . $settings['images_url'] . '/selected.gif" alt="&gt;" /> ' : '') . $txt['hostname'] . '</a>&nbsp;|&nbsp;<a href="' . $scripturl . '?action=admin;area=ban;sa=browse;entity=email">' . ($context['selected_entity'] == 'email' ? '<img src="' . $settings['images_url'] . '/selected.gif" alt="&gt;" /> ' : '') . $txt['email'] . '</a>&nbsp;|&nbsp;<a href="' . $scripturl . '?action=admin;area=ban;sa=browse;entity=member">' . ($context['selected_entity'] == 'member' ? '<img src="' . $settings['images_url'] . '/selected.gif" alt="&gt;" /> ' : '') . $txt['username'] . '</a>',
1206 ),
1207 array(
1208 'position' => 'below_table_data',
1209 'value' => '<input type="submit" name="remove_triggers" value="' . $txt['ban_remove_selected_triggers'] . '" onclick="return confirm(\'' . $txt['ban_remove_selected_triggers_confirm'] . '\');" class="button_submit" />',
1210 'style' => 'text-align: right;',
1211 ),
1212 ),
1213 );
1214
1215 // Specific data for the first column depending on the selected entity.
1216 if ($context['selected_entity'] === 'ip')
1217 {
1218 $listOptions['columns']['banned_entity']['data'] = array(
1219 'function' => create_function('$rowData', '
1220 return range2ip(array(
1221 $rowData[\'ip_low1\'],
1222 $rowData[\'ip_low2\'],
1223 $rowData[\'ip_low3\'],
1224 $rowData[\'ip_low4\']
1225 ), array(
1226 $rowData[\'ip_high1\'],
1227 $rowData[\'ip_high2\'],
1228 $rowData[\'ip_high3\'],
1229 $rowData[\'ip_high4\']
1230 ));
1231 '),
1232 );
1233 $listOptions['columns']['banned_entity']['sort'] = array(
1234 'default' => 'bi.ip_low1, bi.ip_high1, bi.ip_low2, bi.ip_high2, bi.ip_low3, bi.ip_high3, bi.ip_low4, bi.ip_high4',
1235 'reverse' => 'bi.ip_low1 DESC, bi.ip_high1 DESC, bi.ip_low2 DESC, bi.ip_high2 DESC, bi.ip_low3 DESC, bi.ip_high3 DESC, bi.ip_low4 DESC, bi.ip_high4 DESC',
1236 );
1237 }
1238 elseif ($context['selected_entity'] === 'hostname')
1239 {
1240 $listOptions['columns']['banned_entity']['data'] = array(
1241 'function' => create_function('$rowData', '
1242 global $smcFunc;
1243 return strtr($smcFunc[\'htmlspecialchars\']($rowData[\'hostname\']), array(\'%\' => \'*\'));
1244 '),
1245 );
1246 $listOptions['columns']['banned_entity']['sort'] = array(
1247 'default' => 'bi.hostname',
1248 'reverse' => 'bi.hostname DESC',
1249 );
1250 }
1251 elseif ($context['selected_entity'] === 'email')
1252 {
1253 $listOptions['columns']['banned_entity']['data'] = array(
1254 'function' => create_function('$rowData', '
1255 global $smcFunc;
1256 return strtr($smcFunc[\'htmlspecialchars\']($rowData[\'email_address\']), array(\'%\' => \'*\'));
1257 '),
1258 );
1259 $listOptions['columns']['banned_entity']['sort'] = array(
1260 'default' => 'bi.email_address',
1261 'reverse' => 'bi.email_address DESC',
1262 );
1263 }
1264 elseif ($context['selected_entity'] === 'member')
1265 {
1266 $listOptions['columns']['banned_entity']['data'] = array(
1267 'sprintf' => array(
1268 'format' => '<a href="' . $scripturl . '?action=profile;u=%1$d">%2$s</a>',
1269 'params' => array(
1270 'id_member' => false,
1271 'real_name' => false,
1272 ),
1273 ),
1274 );
1275 $listOptions['columns']['banned_entity']['sort'] = array(
1276 'default' => 'mem.real_name',
1277 'reverse' => 'mem.real_name DESC',
1278 );
1279 }
1280
1281 // Create the list.
1282 require_once($sourcedir . '/Subs-List.php');
1283 createList($listOptions);
1284
1285 // The list is the only thing to show, so make it the default sub template.
1286 $context['sub_template'] = 'show_list';
1287 $context['default_list'] = 'ban_trigger_list';
1288 }
1289
1290 function list_getBanTriggers($start, $items_per_page, $sort, $trigger_type)
1291 {
1292 global $smcFunc;
1293
1294 $where = array(
1295 'ip' => 'bi.ip_low1 > 0',
1296 'hostname' => 'bi.hostname != {string:blank_string}',
1297 'email' => 'bi.email_address != {string:blank_string}',
1298 );
1299
1300 $request = $smcFunc['db_query']('', '
1301 SELECT
1302 bi.id_ban, bi.ip_low1, bi.ip_high1, bi.ip_low2, bi.ip_high2, bi.ip_low3, bi.ip_high3, bi.ip_low4, bi.ip_high4, bi.hostname, bi.email_address, bi.hits,
1303 bg.id_ban_group, bg.name' . ($trigger_type === 'member' ? ',
1304 mem.id_member, mem.real_name' : '') . '
1305 FROM {db_prefix}ban_items AS bi
1306 INNER JOIN {db_prefix}ban_groups AS bg ON (bg.id_ban_group = bi.id_ban_group)' . ($trigger_type === 'member' ? '
1307 INNER JOIN {db_prefix}members AS mem ON (mem.id_member = bi.id_member)' : '
1308 WHERE ' . $where[$trigger_type]) . '
1309 ORDER BY ' . $sort . '
1310 LIMIT ' . $start . ', ' . $items_per_page,
1311 array(
1312 'blank_string' => '',
1313 )
1314 );
1315 $ban_triggers = array();
1316 while ($row = $smcFunc['db_fetch_assoc']($request))
1317 $ban_triggers[] = $row;
1318 $smcFunc['db_free_result']($request);
1319
1320 return $ban_triggers;
1321 }
1322
1323 function list_getNumBanTriggers($trigger_type)
1324 {
1325 global $smcFunc;
1326
1327 $where = array(
1328 'ip' => 'bi.ip_low1 > 0',
1329 'hostname' => 'bi.hostname != {string:blank_string}',
1330 'email' => 'bi.email_address != {string:blank_string}',
1331 );
1332
1333 $request = $smcFunc['db_query']('', '
1334 SELECT COUNT(*)
1335 FROM {db_prefix}ban_items AS bi' . ($trigger_type === 'member' ? '
1336 INNER JOIN {db_prefix}members AS mem ON (mem.id_member = bi.id_member)' : '
1337 WHERE ' . $where[$trigger_type]),
1338 array(
1339 'blank_string' => '',
1340 )
1341 );
1342 list ($num_triggers) = $smcFunc['db_fetch_row']($request);
1343 $smcFunc['db_free_result']($request);
1344
1345 return $num_triggers;
1346 }
1347
1348 function BanLog()
1349 {
1350 global $scripturl, $context, $smcFunc, $sourcedir, $txt;
1351 global $context;
1352
1353 // Delete one or more entries.
1354 if (!empty($_POST['removeAll']) || (!empty($_POST['removeSelected']) && !empty($_POST['remove'])))
1355 {
1356 checkSession();
1357
1358 // 'Delete all entries' button was pressed.
1359 if (!empty($_POST['removeAll']))
1360 $smcFunc['db_query']('truncate_table', '
1361 TRUNCATE {db_prefix}log_banned',
1362 array(
1363 )
1364 );
1365
1366 // 'Delte selection' button was pressed.
1367 else
1368 {
1369 // Make sure every entry is integer.
1370 foreach ($_POST['remove'] as $index => $log_id)
1371 $_POST['remove'][$index] = (int) $log_id;
1372
1373 $smcFunc['db_query']('', '
1374 DELETE FROM {db_prefix}log_banned
1375 WHERE id_ban_log IN ({array_int:ban_list})',
1376 array(
1377 'ban_list' => $_POST['remove'],
1378 )
1379 );
1380 }
1381 }
1382
1383 $listOptions = array(
1384 'id' => 'ban_log',
1385 'items_per_page' => 30,
1386 'base_href' => $context['admin_area'] == 'ban' ? $scripturl . '?action=admin;area=ban;sa=log' : $scripturl . '?action=admin;area=logs;sa=banlog',
1387 'default_sort_col' => 'date',
1388 'get_items' => array(
1389 'function' => 'list_getBanLogEntries',
1390 ),
1391 'get_count' => array(
1392 'function' => 'list_getNumBanLogEntries',
1393 ),
1394 'no_items_label' => $txt['ban_log_no_entries'],
1395 'columns' => array(
1396 'ip' => array(
1397 'header' => array(
1398 'value' => $txt['ban_log_ip'],
1399 ),
1400 'data' => array(
1401 'sprintf' => array(
1402 'format' => '<a href="' . $scripturl . '?action=trackip;searchip=%1$s">%1$s</a>',
1403 'params' => array(
1404 'ip' => false,
1405 ),
1406 ),
1407 ),
1408 'sort' => array(
1409 'default' => 'lb.ip',
1410 'reverse' => 'lb.ip DESC',
1411 ),
1412 ),
1413 'email' => array(
1414 'header' => array(
1415 'value' => $txt['ban_log_email'],
1416 ),
1417 'data' => array(
1418 'db_htmlsafe' => 'email',
1419 ),
1420 'sort' => array(
1421 'default' => 'lb.email = \'\', lb.email',
1422 'reverse' => 'lb.email != \'\', lb.email DESC',
1423 ),
1424 ),
1425 'member' => array(
1426 'header' => array(
1427 'value' => $txt['ban_log_member'],
1428 ),
1429 'data' => array(
1430 'sprintf' => array(
1431 'format' => '<a href="' . $scripturl . '?action=profile;u=%1$d">%2$s</a>',
1432 'params' => array(
1433 'id_member' => false,
1434 'real_name' => false,
1435 ),
1436 ),
1437 ),
1438 'sort' => array(
1439 'default' => 'IFNULL(mem.real_name, 1=1), mem.real_name',
1440 'reverse' => 'IFNULL(mem.real_name, 1=1) DESC, mem.real_name DESC',
1441 ),
1442 ),
1443 'date' => array(
1444 'header' => array(
1445 'value' => $txt['ban_log_date'],
1446 ),
1447 'data' => array(
1448 'function' => create_function('$rowData', '
1449 return timeformat($rowData[\'log_time\']);
1450 '),
1451 ),
1452 'sort' => array(
1453 'default' => 'lb.log_time DESC',
1454 'reverse' => 'lb.log_time',
1455 ),
1456 ),
1457 'check' => array(
1458 'header' => array(
1459 'value' => '<input type="checkbox" onclick="invertAll(this, this.form);" class="input_check" />',
1460 ),
1461 'data' => array(
1462 'sprintf' => array(
1463 'format' => '<input type="checkbox" name="remove[]" value="%1$d" class="input_check" />',
1464 'params' => array(
1465 'id_ban_log' => false,
1466 ),
1467 ),
1468 'style' => 'text-align: center',
1469 ),
1470 ),
1471 ),
1472 'form' => array(
1473 'href' => $context['admin_area'] == 'ban' ? $scripturl . '?action=admin;area=ban;sa=log' : $scripturl . '?action=admin;area=logs;sa=banlog',
1474 'include_start' => true,
1475 'include_sort' => true,
1476 ),
1477 'additional_rows' => array(
1478 array(
1479 'position' => 'below_table_data',
1480 'value' => '
1481 <input type="submit" name="removeSelected" value="' . $txt['ban_log_remove_selected'] . '" onclick="return confirm(\'' . $txt['ban_log_remove_selected_confirm'] . '\');" class="button_submit" />
1482 <input type="submit" name="removeAll" value="' . $txt['ban_log_remove_all'] . '" onclick="return confirm(\'' . $txt['ban_log_remove_all_confirm'] . '\');" class="button_submit" />',
1483 'style' => 'text-align: right;',
1484 ),
1485 ),
1486 );
1487
1488 require_once($sourcedir . '/Subs-List.php');
1489 createList($listOptions);
1490
1491 $context['page_title'] = $txt['ban_log'];
1492 $context['sub_template'] = 'show_list';
1493 $context['default_list'] = 'ban_log';
1494 }
1495
1496 function list_getBanLogEntries($start, $items_per_page, $sort)
1497 {
1498 global $smcFunc;
1499
1500 $request = $smcFunc['db_query']('', '
1501 SELECT lb.id_ban_log, lb.id_member, IFNULL(lb.ip, {string:dash}) AS ip, IFNULL(lb.email, {string:dash}) AS email, lb.log_time, IFNULL(mem.real_name, {string:blank_string}) AS real_name
1502 FROM {db_prefix}log_banned AS lb
1503 LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = lb.id_member)
1504 ORDER BY ' . $sort . '
1505 LIMIT ' . $start . ', ' . $items_per_page,
1506 array(
1507 'blank_string' => '',
1508 'dash' => '-',
1509 )
1510 );
1511 $log_entries = array();
1512 while ($row = $smcFunc['db_fetch_assoc']($request))
1513 $log_entries[] = $row;
1514 $smcFunc['db_free_result']($request);
1515
1516 return $log_entries;
1517 }
1518
1519 function list_getNumBanLogEntries()
1520 {
1521 global $smcFunc;
1522
1523 $request = $smcFunc['db_query']('', '
1524 SELECT COUNT(*)
1525 FROM {db_prefix}log_banned AS lb',
1526 array(
1527 )
1528 );
1529 list ($num_entries) = $smcFunc['db_fetch_row']($request);
1530 $smcFunc['db_free_result']($request);
1531
1532 return $num_entries;
1533 }
1534
1535 function range2ip($low, $high)
1536 {
1537 if (count($low) != 4 || count($high) != 4)
1538 return '';
1539
1540 $ip = array();
1541 for ($i = 0; $i < 4; $i++)
1542 {
1543 if ($low[$i] == $high[$i])
1544 $ip[$i] = $low[$i];
1545 elseif ($low[$i] == '0' && $high[$i] == '255')
1546 $ip[$i] = '*';
1547 else
1548 $ip[$i] = $low[$i] . '-' . $high[$i];
1549 }
1550
1551 // Pretending is fun... the IP can't be this, so use it for 'unknown'.
1552 if ($ip == array(255, 255, 255, 255))
1553 return 'unknown';
1554
1555 return implode('.', $ip);
1556 }
1557
1558 function checkExistingTriggerIP($ip_array, $fullip = '')
1559 {
1560 global $smcFunc, $scripturl;
1561
1562 if (count($ip_array) == 4)
1563 $values = array(
1564 'ip_low1' => $ip_array[0]['low'],
1565 'ip_high1' => $ip_array[0]['high'],
1566 'ip_low2' => $ip_array[1]['low'],
1567 'ip_high2' => $ip_array[1]['high'],
1568 'ip_low3' => $ip_array[2]['low'],
1569 'ip_high3' => $ip_array[2]['high'],
1570 'ip_low4' => $ip_array[3]['low'],
1571 'ip_high4' => $ip_array[3]['high'],
1572 );
1573 else
1574 return false;
1575
1576 $request = $smcFunc['db_query']('', '
1577 SELECT bg.id_ban_group, bg.name
1578 FROM {db_prefix}ban_groups AS bg
1579 INNER JOIN {db_prefix}ban_items AS bi ON
1580 (bi.id_ban_group = bg.id_ban_group)
1581 AND ip_low1 = {int:ip_low1} AND ip_high1 = {int:ip_high1}
1582 AND ip_low2 = {int:ip_low2} AND ip_high2 = {int:ip_high2}
1583 AND ip_low3 = {int:ip_low3} AND ip_high3 = {int:ip_high3}
1584 AND ip_low4 = {int:ip_low4} AND ip_high4 = {int:ip_high4}
1585 LIMIT 1',
1586 $values
1587 );
1588 if ($smcFunc['db_num_rows']($request) != 0)
1589 {
1590 list ($error_id_ban, $error_ban_name) = $smcFunc['db_fetch_row']($request);
1591 fatal_lang_error('ban_trigger_already_exists', false, array(
1592 $fullip,
1593 '<a href="' . $scripturl . '?action=admin;area=ban;sa=edit;bg=' . $error_id_ban . '">' . $error_ban_name . '</a>',
1594 ));
1595 }
1596 $smcFunc['db_free_result']($request);
1597
1598 return $values;
1599 }
1600
1601 function updateBanMembers()
1602 {
1603 global $smcFunc;
1604
1605 $updates = array();
1606 $allMembers = array();
1607 $newMembers = array();
1608
1609 // Start by getting all active bans - it's quicker doing this in parts...
1610 $request = $smcFunc['db_query']('', '
1611 SELECT bi.id_member, bi.email_address
1612 FROM {db_prefix}ban_items AS bi
1613 INNER JOIN {db_prefix}ban_groups AS bg ON (bg.id_ban_group = bi.id_ban_group)
1614 WHERE (bi.id_member > {int:no_member} OR bi.email_address != {string:blank_string})
1615 AND bg.cannot_access = {int:cannot_access_on}
1616 AND (bg.expire_time IS NULL OR bg.expire_time > {int:current_time})',
1617 array(
1618 'no_member' => 0,
1619 'cannot_access_on' => 1,
1620 'current_time' => time(),
1621 'blank_string' => '',
1622 )
1623 );
1624 $memberIDs = array();
1625 $memberEmails = array();
1626 $memberEmailWild = array();
1627 while ($row = $smcFunc['db_fetch_assoc']($request))
1628 {
1629 if ($row['id_member'])
1630 $memberIDs[$row['id_member']] = $row['id_member'];
1631 if ($row['email_address'])
1632 {
1633 // Does it have a wildcard - if so we can't do a IN on it.
1634 if (strpos($row['email_address'], '%') !== false)
1635 $memberEmailWild[$row['email_address']] = $row['email_address'];
1636 else
1637 $memberEmails[$row['email_address']] = $row['email_address'];
1638 }
1639 }
1640 $smcFunc['db_free_result']($request);
1641
1642 // Build up the query.
1643 $queryPart = array();
1644 $queryValues = array();
1645 if (!empty($memberIDs))
1646 {
1647 $queryPart[] = 'mem.id_member IN ({array_string:member_ids})';
1648 $queryValues['member_ids'] = $memberIDs;
1649 }
1650 if (!empty($memberEmails))
1651 {
1652 $queryPart[] = 'mem.email_address IN ({array_string:member_emails})';
1653 $queryValues['member_emails'] = $memberEmails;
1654 }
1655 $count = 0;
1656 foreach ($memberEmailWild as $email)
1657 {
1658 $queryPart[] = 'mem.email_address LIKE {string:wild_' . $count . '}';
1659 $queryValues['wild_' . $count++] = $email;
1660 }
1661
1662 // Find all banned members.
1663 if (!empty($queryPart))
1664 {
1665 $request = $smcFunc['db_query']('', '
1666 SELECT mem.id_member, mem.is_activated
1667 FROM {db_prefix}members AS mem
1668 WHERE ' . implode( ' OR ', $queryPart),
1669 $queryValues
1670 );
1671 while ($row = $smcFunc['db_fetch_assoc']($request))
1672 {
1673 if (!in_array($row['id_member'], $allMembers))
1674 {
1675 $allMembers[] = $row['id_member'];
1676 // Do they need an update?
1677 if ($row['is_activated'] < 10)
1678 {
1679 $updates[($row['is_activated'] + 10)][] = $row['id_member'];
1680 $newMembers[] = $row['id_member'];
1681 }
1682 }
1683 }
1684 $smcFunc['db_free_result']($request);
1685 }
1686
1687 // We welcome our new members in the realm of the banned.
1688 if (!empty($newMembers))
1689 $smcFunc['db_query']('', '
1690 DELETE FROM {db_prefix}log_online
1691 WHERE id_member IN ({array_int:new_banned_members})',
1692 array(
1693 'new_banned_members' => $newMembers,
1694 )
1695 );
1696
1697 // Find members that are wrongfully marked as banned.
1698 $request = $smcFunc['db_query']('', '
1699 SELECT mem.id_member, mem.is_activated - 10 AS new_value
1700 FROM {db_prefix}members AS mem
1701 LEFT JOIN {db_prefix}ban_items AS bi ON (bi.id_member = mem.id_member OR mem.email_address LIKE bi.email_address)
1702 LEFT JOIN {db_prefix}ban_groups AS bg ON (bg.id_ban_group = bi.id_ban_group AND bg.cannot_access = {int:cannot_access_activated} AND (bg.expire_time IS NULL OR bg.expire_time > {int:current_time}))
1703 WHERE (bi.id_ban IS NULL OR bg.id_ban_group IS NULL)
1704 AND mem.is_activated >= {int:ban_flag}',
1705 array(
1706 'cannot_access_activated' => 1,
1707 'current_time' => time(),
1708 'ban_flag' => 10,
1709 )
1710 );
1711 while ($row = $smcFunc['db_fetch_assoc']($request))
1712 {
1713 // Don't do this twice!
1714 if (!in_array($row['id_member'], $allMembers))
1715 {
1716 $updates[$row['new_value']][] = $row['id_member'];
1717 $allMembers[] = $row['id_member'];
1718 }
1719 }
1720 $smcFunc['db_free_result']($request);
1721
1722 if (!empty($updates))
1723 foreach ($updates as $newStatus => $members)
1724 updateMemberData($members, array('is_activated' => $newStatus));
1725
1726 // Update the latest member and our total members as banning may change them.
1727 updateStats('member');
1728 }
1729
1730 ?>