Issues (1065)

Sources/ManageBans.php (1 issue)

1
<?php
2
3
/**
4
 * This file contains all the functions used for the ban center.
5
 *
6
 * @todo refactor as controller-model
7
 *
8
 * Simple Machines Forum (SMF)
9
 *
10
 * @package SMF
11
 * @author Simple Machines https://www.simplemachines.org
12
 * @copyright 2025 Simple Machines and individual contributors
13
 * @license https://www.simplemachines.org/about/smf/license.php BSD
14
 *
15
 * @version 2.1.5
16
 */
17
18
if (!defined('SMF'))
19
	die('No direct access...');
20
21
/**
22
 * Ban center. The main entrance point for all ban center functions.
23
 * It is accesssed by ?action=admin;area=ban.
24
 * It choses a function based on the 'sa' parameter, like many others.
25
 * The default sub-action is BanList().
26
 * It requires the ban_members permission.
27
 * It initializes the admin tabs.
28
 *
29
 * Uses ManageBans template.
30
 */
31
function Ban()
32
{
33
	global $context, $txt, $scripturl;
34
35
	isAllowedTo('manage_bans');
36
37
	loadTemplate('ManageBans');
38
39
	$subActions = array(
40
		'add' => 'BanEdit',
41
		'browse' => 'BanBrowseTriggers',
42
		'edittrigger' => 'BanEditTrigger',
43
		'edit' => 'BanEdit',
44
		'list' => 'BanList',
45
		'log' => 'BanLog',
46
	);
47
48
	// Tabs for browsing the different ban functions.
49
	$context[$context['admin_menu_name']]['tab_data'] = array(
50
		'title' => $txt['ban_title'],
51
		'help' => 'ban_members',
52
		'description' => $txt['ban_description'],
53
		'tabs' => array(
54
			'list' => array(
55
				'description' => $txt['ban_description'],
56
				'href' => $scripturl . '?action=admin;area=ban;sa=list',
57
			),
58
			'add' => array(
59
				'description' => $txt['ban_description'],
60
				'href' => $scripturl . '?action=admin;area=ban;sa=add',
61
			),
62
			'browse' => array(
63
				'description' => $txt['ban_trigger_browse_description'],
64
				'href' => $scripturl . '?action=admin;area=ban;sa=browse',
65
			),
66
			'log' => array(
67
				'description' => $txt['ban_log_description'],
68
				'href' => $scripturl . '?action=admin;area=ban;sa=log',
69
				'is_last' => true,
70
			),
71
		),
72
	);
73
74
	call_integration_hook('integrate_manage_bans', array(&$subActions));
75
76
	// Default the sub-action to 'view ban list'.
77
	$_REQUEST['sa'] = isset($_REQUEST['sa']) && isset($subActions[$_REQUEST['sa']]) ? $_REQUEST['sa'] : 'list';
78
79
	// Mark the appropriate menu entry as selected
80
	if (array_key_exists($_REQUEST['sa'], $context[$context['admin_menu_name']]['tab_data']['tabs']))
81
		$context[$context['admin_menu_name']]['tab_data']['tabs'][$_REQUEST['sa']]['is_selected'] = true;
82
83
	$context['page_title'] = $txt['ban_title'];
84
	$context['sub_action'] = $_REQUEST['sa'];
85
86
	// Call the right function for this sub-action.
87
	call_helper($subActions[$_REQUEST['sa']]);
88
}
89
90
/**
91
 * Shows a list of bans currently set.
92
 * It is accessed by ?action=admin;area=ban;sa=list.
93
 * It removes expired bans.
94
 * It allows sorting on different criteria.
95
 * It also handles removal of selected ban items.
96
 *
97
 * Uses the main ManageBans template.
98
 */
99
function BanList()
100
{
101
	global $txt, $context, $scripturl;
102
	global $user_info, $sourcedir, $modSettings;
103
104
	// User pressed the 'remove selection button'.
105
	if (!empty($_POST['removeBans']) && !empty($_POST['remove']) && is_array($_POST['remove']))
106
	{
107
		checkSession();
108
109
		// Make sure every entry is a proper integer.
110
		array_map('intval', $_POST['remove']);
111
112
		// Unban them all!
113
		removeBanGroups($_POST['remove']);
114
115
		// No more caching this ban!
116
		updateSettings(array('banLastUpdated' => time()));
117
118
		// Some members might be unbanned now. Update the members table.
119
		updateBanMembers();
120
	}
121
122
	// Create a date string so we don't overload them with date info.
123
	if (preg_match('~%[AaBbCcDdeGghjmuYy](?:[^%]*%[AaBbCcDdeGghjmuYy])*~', $user_info['time_format'], $matches) == 0 || empty($matches[0]))
124
		$context['ban_time_format'] = $user_info['time_format'];
125
	else
126
		$context['ban_time_format'] = $matches[0];
127
128
	$listOptions = array(
129
		'id' => 'ban_list',
130
		'title' => $txt['ban_title'],
131
		'items_per_page' => $modSettings['defaultMaxListItems'],
132
		'base_href' => $scripturl . '?action=admin;area=ban;sa=list',
133
		'default_sort_col' => 'added',
134
		'default_sort_dir' => 'desc',
135
		'get_items' => array(
136
			'function' => 'list_getBans',
137
		),
138
		'get_count' => array(
139
			'function' => 'list_getNumBans',
140
		),
141
		'no_items_label' => $txt['ban_no_entries'],
142
		'columns' => array(
143
			'name' => array(
144
				'header' => array(
145
					'value' => $txt['ban_name'],
146
				),
147
				'data' => array(
148
					'db' => 'name',
149
				),
150
				'sort' => array(
151
					'default' => 'bg.name',
152
					'reverse' => 'bg.name DESC',
153
				),
154
			),
155
			'notes' => array(
156
				'header' => array(
157
					'value' => $txt['ban_notes'],
158
				),
159
				'data' => array(
160
					'db' => 'notes',
161
					'class' => 'smalltext word_break',
162
				),
163
				'sort' => array(
164
					'default' => 'LENGTH(bg.notes) > 0 DESC, bg.notes',
165
					'reverse' => 'LENGTH(bg.notes) > 0, bg.notes DESC',
166
				),
167
			),
168
			'reason' => array(
169
				'header' => array(
170
					'value' => $txt['ban_reason'],
171
				),
172
				'data' => array(
173
					'db' => 'reason',
174
					'class' => 'smalltext word_break',
175
				),
176
				'sort' => array(
177
					'default' => 'LENGTH(bg.reason) > 0 DESC, bg.reason',
178
					'reverse' => 'LENGTH(bg.reason) > 0, bg.reason DESC',
179
				),
180
			),
181
			'added' => array(
182
				'header' => array(
183
					'value' => $txt['ban_added'],
184
				),
185
				'data' => array(
186
					'function' => function($rowData) use ($context)
187
					{
188
						return timeformat($rowData['ban_time'], empty($context['ban_time_format']) ? true : $context['ban_time_format']);
189
					},
190
				),
191
				'sort' => array(
192
					'default' => 'bg.ban_time',
193
					'reverse' => 'bg.ban_time DESC',
194
				),
195
			),
196
			'expires' => array(
197
				'header' => array(
198
					'value' => $txt['ban_expires'],
199
				),
200
				'data' => array(
201
					'function' => function($rowData) use ($txt)
202
					{
203
						// This ban never expires...whahaha.
204
						if ($rowData['expire_time'] === null)
205
							return $txt['never'];
206
207
						// This ban has already expired.
208
						elseif ($rowData['expire_time'] < time())
209
							return sprintf('<span class="red">%1$s</span>', $txt['ban_expired']);
210
211
						// Still need to wait a few days for this ban to expire.
212
						else
213
							return sprintf('%1$d&nbsp;%2$s', ceil(($rowData['expire_time'] - time()) / (60 * 60 * 24)), $txt['ban_days']);
214
					},
215
				),
216
				'sort' => array(
217
					'default' => 'COALESCE(bg.expire_time, 1=1) DESC, bg.expire_time DESC',
218
					'reverse' => 'COALESCE(bg.expire_time, 1=1), bg.expire_time',
219
				),
220
			),
221
			'num_triggers' => array(
222
				'header' => array(
223
					'value' => $txt['ban_triggers'],
224
				),
225
				'data' => array(
226
					'db' => 'num_triggers',
227
				),
228
				'sort' => array(
229
					'default' => 'num_triggers DESC',
230
					'reverse' => 'num_triggers',
231
				),
232
			),
233
			'actions' => array(
234
				'header' => array(
235
					'value' => $txt['ban_actions'],
236
					'class' => 'centercol',
237
				),
238
				'data' => array(
239
					'sprintf' => array(
240
						'format' => '<a href="' . $scripturl . '?action=admin;area=ban;sa=edit;bg=%1$d">' . $txt['modify'] . '</a>',
241
						'params' => array(
242
							'id_ban_group' => false,
243
						),
244
					),
245
					'class' => 'centercol',
246
				),
247
			),
248
			'check' => array(
249
				'header' => array(
250
					'value' => '<input type="checkbox" onclick="invertAll(this, this.form);">',
251
					'class' => 'centercol',
252
				),
253
				'data' => array(
254
					'sprintf' => array(
255
						'format' => '<input type="checkbox" name="remove[]" value="%1$d">',
256
						'params' => array(
257
							'id_ban_group' => false,
258
						),
259
					),
260
					'class' => 'centercol',
261
				),
262
			),
263
		),
264
		'form' => array(
265
			'href' => $scripturl . '?action=admin;area=ban;sa=list',
266
		),
267
		'additional_rows' => array(
268
			array(
269
				'position' => 'top_of_list',
270
				'value' => '<input type="submit" name="removeBans" value="' . $txt['ban_remove_selected'] . '" class="button">',
271
			),
272
			array(
273
				'position' => 'bottom_of_list',
274
				'value' => '<input type="submit" name="removeBans" value="' . $txt['ban_remove_selected'] . '" class="button">',
275
			),
276
		),
277
		'javascript' => '
278
		var removeBans = $("input[name=\'removeBans\']");
279
280
		removeBans.on( "click", function(e) {
281
			var removeItems = $("input[name=\'remove[]\']:checked").length;
282
283
			if (removeItems == 0)
284
			{
285
				e.preventDefault();
286
				return alert("' . $txt['select_item_check'] . '");
287
			}
288
289
290
			return confirm("' . $txt['ban_remove_selected_confirm'] . '");
291
		});',
292
	);
293
294
	require_once($sourcedir . '/Subs-List.php');
295
	createList($listOptions);
296
297
	$context['sub_template'] = 'show_list';
298
	$context['default_list'] = 'ban_list';
299
}
300
301
/**
302
 * Get bans, what else? For the given options.
303
 *
304
 * @param int $start Which item to start with (for pagination purposes)
305
 * @param int $items_per_page How many items to show on each page
306
 * @param string $sort A string telling ORDER BY how to sort the results
307
 * @return array An array of information about the bans for the list
308
 */
309
function list_getBans($start, $items_per_page, $sort)
310
{
311
	global $smcFunc;
312
313
	$request = $smcFunc['db_query']('', '
314
		SELECT bg.id_ban_group, bg.name, bg.ban_time, bg.expire_time, bg.reason, bg.notes, COUNT(bi.id_ban) AS num_triggers
315
		FROM {db_prefix}ban_groups AS bg
316
			LEFT JOIN {db_prefix}ban_items AS bi ON (bi.id_ban_group = bg.id_ban_group)
317
		GROUP BY bg.id_ban_group, bg.name, bg.ban_time, bg.expire_time, bg.reason, bg.notes
318
		ORDER BY {raw:sort}
319
		LIMIT {int:offset}, {int:limit}',
320
		array(
321
			'sort' => $sort,
322
			'offset' => $start,
323
			'limit' => $items_per_page,
324
		)
325
	);
326
	$bans = array();
327
	while ($row = $smcFunc['db_fetch_assoc']($request))
328
		$bans[] = $row;
329
330
	$smcFunc['db_free_result']($request);
331
332
	return $bans;
333
}
334
335
/**
336
 * Get the total number of ban from the ban group table
337
 *
338
 * @return int The total number of bans
339
 */
340
function list_getNumBans()
341
{
342
	global $smcFunc;
343
344
	$request = $smcFunc['db_query']('', '
345
		SELECT COUNT(*) AS num_bans
346
		FROM {db_prefix}ban_groups',
347
		array(
348
		)
349
	);
350
	list ($numBans) = $smcFunc['db_fetch_row']($request);
351
	$smcFunc['db_free_result']($request);
352
353
	return $numBans;
354
}
355
356
/**
357
 * This function is behind the screen for adding new bans and modifying existing ones.
358
 * Adding new bans:
359
 * 	- is accessed by ?action=admin;area=ban;sa=add.
360
 * 	- uses the ban_edit sub template of the ManageBans template.
361
 * Modifying existing bans:
362
 *  - is accessed by ?action=admin;area=ban;sa=edit;bg=x
363
 *  - uses the ban_edit sub template of the ManageBans template.
364
 *  - shows a list of ban triggers for the specified ban.
365
 */
366
function BanEdit()
367
{
368
	global $txt, $modSettings, $context, $scripturl, $smcFunc, $sourcedir;
369
370
	if ((isset($_POST['add_ban']) || isset($_POST['modify_ban']) || isset($_POST['remove_selection'])) && empty($context['ban_errors']))
371
		BanEdit2();
372
373
	$ban_group_id = isset($context['ban']['id']) ? $context['ban']['id'] : (isset($_REQUEST['bg']) ? (int) $_REQUEST['bg'] : 0);
374
375
	// Template needs this to show errors using javascript
376
	loadLanguage('Errors');
377
	createToken('admin-bet');
378
	$context['form_url'] = $scripturl . '?action=admin;area=ban;sa=edit';
379
380
	if (!empty($context['ban_errors']))
381
		foreach ($context['ban_errors'] as $error)
382
			$context['error_messages'][$error] = $txt[$error];
383
384
	else
385
	{
386
		// If we're editing an existing ban, get it from the database.
387
		if (!empty($ban_group_id))
388
		{
389
			$context['ban_group_id'] = $ban_group_id;
390
391
			// We're going to want this for making our list.
392
			require_once($sourcedir . '/Subs-List.php');
393
394
			$listOptions = array(
395
				'id' => 'ban_items',
396
				'base_href' => $scripturl . '?action=admin;area=ban;sa=edit;bg=' . $ban_group_id,
397
				'no_items_label' => $txt['ban_no_triggers'],
398
				'items_per_page' => $modSettings['defaultMaxListItems'],
399
				'get_items' => array(
400
					'function' => 'list_getBanItems',
401
					'params' => array(
402
						'ban_group_id' => $ban_group_id,
403
					),
404
				),
405
				'get_count' => array(
406
					'function' => 'list_getNumBanItems',
407
					'params' => array(
408
						'ban_group_id' => $ban_group_id,
409
					),
410
				),
411
				'columns' => array(
412
					'type' => array(
413
						'header' => array(
414
							'value' => $txt['ban_banned_entity'],
415
							'style' => 'width: 60%;text-align: left;',
416
						),
417
						'data' => array(
418
							'function' => function($ban_item) use ($txt)
419
							{
420
								if (in_array($ban_item['type'], array('ip', 'hostname', 'email')))
421
									return '<strong>' . $txt[$ban_item['type']] . ':</strong>&nbsp;' . $ban_item[$ban_item['type']];
422
								elseif ($ban_item['type'] == 'user')
423
									return '<strong>' . $txt['username'] . ':</strong>&nbsp;' . $ban_item['user']['link'];
424
								else
425
									return '<strong>' . $txt['unknown'] . ':</strong>&nbsp;' . $ban_item['no_bantype_selected'];
426
							},
427
							'style' => 'text-align: left;',
428
						),
429
					),
430
					'hits' => array(
431
						'header' => array(
432
							'value' => $txt['ban_hits'],
433
							'style' => 'width: 15%; text-align: center;',
434
						),
435
						'data' => array(
436
							'db' => 'hits',
437
							'style' => 'text-align: center;',
438
						),
439
					),
440
					'id' => array(
441
						'header' => array(
442
							'value' => $txt['ban_actions'],
443
							'style' => 'width: 15%; text-align: center;',
444
						),
445
						'data' => array(
446
							'function' => function($ban_item) use ($txt, $context, $scripturl)
447
							{
448
								return '<a href="' . $scripturl . '?action=admin;area=ban;sa=edittrigger;bg=' . $context['ban_group_id'] . ';bi=' . $ban_item['id'] . '">' . $txt['ban_edit_trigger'] . '</a>';
449
							},
450
							'style' => 'text-align: center;',
451
						),
452
					),
453
					'checkboxes' => array(
454
						'header' => array(
455
							'value' => '<input type="checkbox" onclick="invertAll(this, this.form, \'ban_items\');">',
456
							'style' => 'width: 5%; text-align: center;',
457
						),
458
						'data' => array(
459
							'sprintf' => array(
460
								'format' => '<input type="checkbox" name="ban_items[]" value="%1$d">',
461
								'params' => array(
462
									'id' => false,
463
								),
464
							),
465
							'style' => 'text-align: center;',
466
						),
467
					),
468
				),
469
				'form' => array(
470
					'href' => $scripturl . '?action=admin;area=ban;sa=edit;bg=' . $ban_group_id,
471
				),
472
				'additional_rows' => array(
473
					array(
474
						'position' => 'above_column_headers',
475
						'value' => '
476
						<input type="submit" name="remove_selection" value="' . $txt['ban_remove_selected_triggers'] . '" class="button"> <a class="button" href="' . $scripturl . '?action=admin;area=ban;sa=edittrigger;bg=' . $ban_group_id . '">' . $txt['ban_add_trigger'] . '</a>',
477
						'style' => 'text-align: right;',
478
					),
479
					array(
480
						'position' => 'above_column_headers',
481
						'value' => '
482
						<input type="hidden" name="bg" value="' . $ban_group_id . '">
483
						<input type="hidden" name="' . $context['session_var'] . '" value="' . $context['session_id'] . '">
484
						<input type="hidden" name="' . $context['admin-bet_token_var'] . '" value="' . $context['admin-bet_token'] . '">',
485
					),
486
					array(
487
						'position' => 'below_table_data',
488
						'value' => '
489
						<input type="submit" name="remove_selection" value="' . $txt['ban_remove_selected_triggers'] . '" class="button"> <a class="button" href="' . $scripturl . '?action=admin;area=ban;sa=edittrigger;bg=' . $ban_group_id . '">' . $txt['ban_add_trigger'] . '</a>',
490
						'style' => 'text-align: right;',
491
					),
492
					array(
493
						'position' => 'below_table_data',
494
						'value' => '
495
						<input type="hidden" name="bg" value="' . $ban_group_id . '">
496
						<input type="hidden" name="' . $context['session_var'] . '" value="' . $context['session_id'] . '">
497
						<input type="hidden" name="' . $context['admin-bet_token_var'] . '" value="' . $context['admin-bet_token'] . '">',
498
					),
499
				),
500
				'javascript' => '
501
		var removeBans = $("input[name=\'remove_selection\']");
502
503
		removeBans.on( "click", function(e) {
504
			var removeItems = $("input[name=\'ban_items[]\']:checked").length;
505
506
			if (removeItems == 0)
507
			{
508
				e.preventDefault();
509
				return alert("' . $txt['select_item_check'] . '");
510
			}
511
512
513
			return confirm("' . $txt['ban_remove_selected_confirm'] . '");
514
		});',
515
			);
516
517
			call_integration_hook('integrate_ban_edit_list', array(&$listOptions));
518
519
			createList($listOptions);
520
		}
521
		// Not an existing one, then it's probably a new one.
522
		else
523
		{
524
			$context['ban'] = array(
525
				'id' => 0,
526
				'name' => '',
527
				'expiration' => array(
528
					'status' => 'never',
529
					'days' => 0
530
				),
531
				'reason' => '',
532
				'notes' => '',
533
				'ban_days' => 0,
534
				'cannot' => array(
535
					'access' => true,
536
					'post' => false,
537
					'register' => false,
538
					'login' => false,
539
				),
540
				'is_new' => true,
541
			);
542
			$context['ban_suggestions'] = array(
543
				'main_ip' => '',
544
				'hostname' => '',
545
				'email' => '',
546
				'member' => array(
547
					'id' => 0,
548
				),
549
			);
550
551
			// Overwrite some of the default form values if a user ID was given.
552
			if (!empty($_REQUEST['u']))
553
			{
554
				$request = $smcFunc['db_query']('', '
555
					SELECT id_member, real_name, member_ip, email_address
556
					FROM {db_prefix}members
557
					WHERE id_member = {int:current_user}
558
					LIMIT 1',
559
					array(
560
						'current_user' => (int) $_REQUEST['u'],
561
					)
562
				);
563
				if ($smcFunc['db_num_rows']($request) > 0)
564
				{
565
					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);
566
					$context['ban_suggestions']['main_ip'] = inet_dtop($context['ban_suggestions']['main_ip']);
567
				}
568
				$smcFunc['db_free_result']($request);
569
570
				if (!empty($context['ban_suggestions']['member']['id']))
571
				{
572
					$context['ban_suggestions']['href'] = $scripturl . '?action=profile;u=' . $context['ban_suggestions']['member']['id'];
573
					$context['ban_suggestions']['member']['link'] = '<a href="' . $context['ban_suggestions']['href'] . '">' . $context['ban_suggestions']['member']['name'] . '</a>';
574
575
					// Default the ban name to the name of the banned member.
576
					$context['ban']['name'] = $context['ban_suggestions']['member']['name'];
577
					// @todo: there should be a better solution...used to lock the "Ban on Username" input when banning from profile
578
					$context['ban']['from_user'] = true;
579
580
					// Would be nice if we could also ban the hostname.
581
					if ((preg_match('/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/', $context['ban_suggestions']['main_ip']) == 1 || isValidIPv6($context['ban_suggestions']['main_ip'])) && empty($modSettings['disableHostnameLookup']))
582
						$context['ban_suggestions']['hostname'] = host_from_ip($context['ban_suggestions']['main_ip']);
583
584
					$context['ban_suggestions']['other_ips'] = banLoadAdditionalIPs($context['ban_suggestions']['member']['id']);
585
				}
586
			}
587
588
			// We come from the mod center.
589
			elseif (isset($_GET['msg']) && !empty($_GET['msg']))
590
			{
591
				$request = $smcFunc['db_query']('', '
592
					SELECT poster_name, poster_ip, poster_email
593
					FROM {db_prefix}messages
594
					WHERE id_msg = {int:message}
595
					LIMIT 1',
596
					array(
597
						'message' => (int) $_REQUEST['msg'],
598
					)
599
				);
600
				if ($smcFunc['db_num_rows']($request) > 0)
601
				{
602
					list ($context['ban_suggestions']['member']['name'], $context['ban_suggestions']['main_ip'], $context['ban_suggestions']['email']) = $smcFunc['db_fetch_row']($request);
603
					$context['ban_suggestions']['main_ip'] = inet_dtop($context['ban_suggestions']['main_ip']);
604
				}
605
				$smcFunc['db_free_result']($request);
606
607
				// Can't hurt to ban base on the guest name...
608
				$context['ban']['name'] = $context['ban_suggestions']['member']['name'];
609
				$context['ban']['from_user'] = true;
610
			}
611
612
			call_integration_hook('integrate_ban_edit_new', array());
613
614
		}
615
	}
616
617
	loadJavaScriptFile('suggest.js', array('minimize' => true), 'smf_suggest');
618
	$context['sub_template'] = 'ban_edit';
619
620
}
621
622
/**
623
 * Retrieves all the ban items belonging to a certain ban group
624
 *
625
 * @param int $start Which item to start with (for pagination purposes)
626
 * @param int $items_per_page How many items to show on each page
627
 * @param int $sort Not used here
628
 * @param int $ban_group_id The ID of the group to get the bans for
629
 * @return array An array with information about the returned ban items
630
 */
631
function list_getBanItems($start = 0, $items_per_page = 0, $sort = 0, $ban_group_id = 0)
632
{
633
	global $context, $smcFunc, $scripturl;
634
635
	$ban_items = array();
636
	$request = $smcFunc['db_query']('', '
637
		SELECT
638
			bi.id_ban, bi.hostname, bi.email_address, bi.id_member, bi.hits,
639
			bi.ip_low, bi.ip_high,
640
			bg.id_ban_group, bg.name, bg.ban_time, bg.expire_time AS expire_time, bg.reason, bg.notes, bg.cannot_access, bg.cannot_register, bg.cannot_login, bg.cannot_post,
641
			COALESCE(mem.id_member, 0) AS id_member, mem.member_name, mem.real_name
642
		FROM {db_prefix}ban_groups AS bg
643
			LEFT JOIN {db_prefix}ban_items AS bi ON (bi.id_ban_group = bg.id_ban_group)
644
			LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = bi.id_member)
645
		WHERE bg.id_ban_group = {int:current_ban}
646
		LIMIT {int:start}, {int:items_per_page}',
647
		array(
648
			'current_ban' => $ban_group_id,
649
			'start' => $start,
650
			'items_per_page' => $items_per_page,
651
		)
652
	);
653
	if ($smcFunc['db_num_rows']($request) == 0)
654
		fatal_lang_error('ban_not_found', false);
655
656
	while ($row = $smcFunc['db_fetch_assoc']($request))
657
	{
658
		if (!isset($context['ban']))
659
		{
660
			$context['ban'] = array(
661
				'id' => $row['id_ban_group'],
662
				'name' => $row['name'],
663
				'expiration' => array(
664
					'status' => $row['expire_time'] === null ? 'never' : ($row['expire_time'] < time() ? 'expired' : 'one_day'),
665
					'days' => $row['expire_time'] > time() ? ($row['expire_time'] - time() < 86400 ? 1 : ceil(($row['expire_time'] - time()) / 86400)) : 0
666
				),
667
				'reason' => $row['reason'],
668
				'notes' => $row['notes'],
669
				'cannot' => array(
670
					'access' => !empty($row['cannot_access']),
671
					'post' => !empty($row['cannot_post']),
672
					'register' => !empty($row['cannot_register']),
673
					'login' => !empty($row['cannot_login']),
674
				),
675
				'is_new' => false,
676
				'hostname' => '',
677
				'email' => '',
678
			);
679
		}
680
681
		if (!empty($row['id_ban']))
682
		{
683
			$ban_items[$row['id_ban']] = array(
684
				'id' => $row['id_ban'],
685
				'hits' => $row['hits'],
686
			);
687
			if (!empty($row['ip_high']))
688
			{
689
				$ban_items[$row['id_ban']]['type'] = 'ip';
690
				$ban_items[$row['id_ban']]['ip'] = range2ip($row['ip_low'], $row['ip_high']);
691
			}
692
			elseif (!empty($row['hostname']))
693
			{
694
				$ban_items[$row['id_ban']]['type'] = 'hostname';
695
				$ban_items[$row['id_ban']]['hostname'] = str_replace('%', '*', $row['hostname']);
696
			}
697
			elseif (!empty($row['email_address']))
698
			{
699
				$ban_items[$row['id_ban']]['type'] = 'email';
700
				$ban_items[$row['id_ban']]['email'] = str_replace('%', '*', $row['email_address']);
701
			}
702
			elseif (!empty($row['id_member']))
703
			{
704
				$ban_items[$row['id_ban']]['type'] = 'user';
705
				$ban_items[$row['id_ban']]['user'] = array(
706
					'id' => $row['id_member'],
707
					'name' => $row['real_name'],
708
					'href' => $scripturl . '?action=profile;u=' . $row['id_member'],
709
					'link' => '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['real_name'] . '</a>',
710
				);
711
			}
712
			// Invalid ban (member probably doesn't exist anymore).
713
			else
714
			{
715
				unset($ban_items[$row['id_ban']]);
716
				removeBanTriggers($row['id_ban']);
717
			}
718
		}
719
	}
720
	$smcFunc['db_free_result']($request);
721
722
	call_integration_hook('integrate_ban_list', array(&$ban_items));
723
724
	return $ban_items;
725
}
726
727
/**
728
 * Gets the number of ban items belonging to a certain ban group
729
 *
730
 * @return int The number of ban items
731
 */
732
function list_getNumBanItems()
733
{
734
	global $smcFunc, $context;
735
736
	$ban_group_id = isset($context['ban_group_id']) ? $context['ban_group_id'] : 0;
737
738
	$request = $smcFunc['db_query']('', '
739
		SELECT COUNT(bi.id_ban)
740
		FROM {db_prefix}ban_groups AS bg
741
			LEFT JOIN {db_prefix}ban_items AS bi ON (bi.id_ban_group = bg.id_ban_group)
742
			LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = bi.id_member)
743
		WHERE bg.id_ban_group = {int:current_ban}',
744
		array(
745
			'current_ban' => $ban_group_id,
746
		)
747
	);
748
	list($banNumber) = $smcFunc['db_fetch_row']($request);
749
	$smcFunc['db_free_result']($request);
750
751
	return $banNumber;
752
}
753
754
/**
755
 * Finds additional IPs related to a certain user
756
 *
757
 * @param int $member_id The ID of the member to get additional IPs for
758
 * @return array An containing two arrays - ips_in_messages (IPs used in posts) and ips_in_errors (IPs used in error messages)
759
 */
760
function banLoadAdditionalIPs($member_id)
761
{
762
	// Borrowing a few language strings from profile.
763
	loadLanguage('Profile');
764
765
	$search_list = array();
766
	call_integration_hook('integrate_load_addtional_ip_ban', array(&$search_list));
767
	$search_list += array('ips_in_messages' => 'banLoadAdditionalIPsMember', 'ips_in_errors' => 'banLoadAdditionalIPsError');
768
769
	$return = array();
770
	foreach ($search_list as $key => $callable)
771
		if (is_callable($callable))
772
			$return[$key] = call_user_func($callable, $member_id);
773
774
	return $return;
775
}
776
777
/**
778
 * Loads additional IPs used by a specific member
779
 *
780
 * @param int $member_id The ID of the member
781
 * @return array An array of IPs used in posts by this member
782
 */
783
function banLoadAdditionalIPsMember($member_id)
784
{
785
	global $smcFunc;
786
787
	// Find some additional IP's used by this member.
788
	$message_ips = array();
789
	$request = $smcFunc['db_query']('', '
790
		SELECT DISTINCT poster_ip
791
		FROM {db_prefix}messages
792
		WHERE id_member = {int:current_user}
793
			AND poster_ip IS NOT NULL
794
		ORDER BY poster_ip',
795
		array(
796
			'current_user' => $member_id,
797
		)
798
	);
799
	while ($row = $smcFunc['db_fetch_assoc']($request))
800
		$message_ips[] = inet_dtop($row['poster_ip']);
801
802
	$smcFunc['db_free_result']($request);
803
804
	return $message_ips;
805
}
806
807
/**
808
 * Loads additional IPs used by a member from the error log
809
 *
810
 * @param int $member_id The ID of the member
811
 * @return array An array of IPs associated with error messages generated by this user
812
 */
813
function banLoadAdditionalIPsError($member_id)
814
{
815
	global $smcFunc;
816
817
	$error_ips = array();
818
	$request = $smcFunc['db_query']('', '
819
		SELECT DISTINCT ip
820
		FROM {db_prefix}log_errors
821
		WHERE id_member = {int:current_user}
822
			AND ip IS NOT NULL
823
		ORDER BY ip',
824
		array(
825
			'current_user' => $member_id,
826
		)
827
	);
828
	while ($row = $smcFunc['db_fetch_assoc']($request))
829
		$error_ips[] = inet_dtop($row['ip']);
830
831
	$smcFunc['db_free_result']($request);
832
833
	return $error_ips;
834
}
835
836
/**
837
 * This function handles submitted forms that add, modify or remove ban triggers.
838
 */
839
function banEdit2()
840
{
841
	global $smcFunc, $context;
842
843
	checkSession();
844
	validateToken('admin-bet');
845
846
	$context['ban_errors'] = array();
847
848
	// Adding or editing a ban group
849
	if (isset($_POST['add_ban']) || isset($_POST['modify_ban']))
850
	{
851
		// Let's collect all the information we need
852
		$ban_info['id'] = isset($_REQUEST['bg']) ? (int) $_REQUEST['bg'] : 0;
853
		$ban_info['is_new'] = empty($ban_info['id']);
854
		$ban_info['expire_date'] = !empty($_POST['expire_date']) ? (int) $_POST['expire_date'] : 0;
855
		$ban_info['expiration'] = array(
856
			'status' => isset($_POST['expiration']) && in_array($_POST['expiration'], array('never', 'one_day', 'expired')) ? $_POST['expiration'] : 'never',
857
			'days' => $ban_info['expire_date'],
858
		);
859
		$ban_info['db_expiration'] = $ban_info['expiration']['status'] == 'never' ? 'NULL' : ($ban_info['expiration']['status'] == 'one_day' ? time() + 24 * 60 * 60 * $ban_info['expire_date'] : 0);
860
		$ban_info['full_ban'] = empty($_POST['full_ban']) ? 0 : 1;
861
		$ban_info['reason'] = !empty($_POST['reason']) ? $smcFunc['htmlspecialchars']($_POST['reason'], ENT_QUOTES) : '';
862
		$ban_info['name'] = !empty($_POST['ban_name']) ? $smcFunc['htmlspecialchars']($_POST['ban_name'], ENT_QUOTES) : '';
863
		$ban_info['notes'] = isset($_POST['notes']) ? $smcFunc['htmlspecialchars']($_POST['notes'], ENT_QUOTES) : '';
864
		$ban_info['notes'] = str_replace(array("\r", "\n", '  '), array('', '<br>', '&nbsp; '), $ban_info['notes']);
865
		$ban_info['cannot']['access'] = empty($ban_info['full_ban']) ? 0 : 1;
866
		$ban_info['cannot']['post'] = !empty($ban_info['full_ban']) || empty($_POST['cannot_post']) ? 0 : 1;
867
		$ban_info['cannot']['register'] = !empty($ban_info['full_ban']) || empty($_POST['cannot_register']) ? 0 : 1;
868
		$ban_info['cannot']['login'] = !empty($ban_info['full_ban']) || empty($_POST['cannot_login']) ? 0 : 1;
869
870
		call_integration_hook('integrate_edit_bans', array(&$ban_info, empty($_REQUEST['bg'])));
871
872
		// Limit 'reason' characters
873
		$ban_info['reason'] = $smcFunc['truncate']($ban_info['reason'], 255);
874
875
		// Adding a new ban group
876
		if (empty($_REQUEST['bg']))
877
			$ban_group_id = insertBanGroup($ban_info);
878
		// Editing an existing ban group
879
		else
880
			$ban_group_id = updateBanGroup($ban_info);
881
882
		if (is_numeric($ban_group_id))
883
		{
884
			$ban_info['id'] = $ban_group_id;
885
			$ban_info['is_new'] = false;
886
		}
887
888
		$context['ban'] = $ban_info;
889
	}
890
891
	if (isset($_POST['ban_suggestions']))
892
		// @TODO: is $_REQUEST['bi'] ever set?
893
		$saved_triggers = saveTriggers($_POST['ban_suggestions'], $ban_info['id'], isset($_REQUEST['u']) ? (int) $_REQUEST['u'] : 0, isset($_REQUEST['bi']) ? (int) $_REQUEST['bi'] : 0);
894
895
	// Something went wrong somewhere... Oh well, let's go back.
896
	if (!empty($context['ban_errors']))
897
	{
898
		$context['ban_suggestions'] = !empty($saved_triggers) ? $saved_triggers : array();
899
		if (isset($_REQUEST['u']))
900
		{
901
			$context['ban']['from_user'] = true;
902
			$context['ban_suggestions'] = array_merge($context['ban_suggestions'], getMemberData((int) $_REQUEST['u']));
903
		}
904
905
		// Not strictly necessary, but it's nice
906
		if (!empty($context['ban_suggestions']['member']['id']))
907
			$context['ban_suggestions']['other_ips'] = banLoadAdditionalIPs($context['ban_suggestions']['member']['id']);
908
		return BanEdit();
909
	}
910
	$context['ban_suggestions']['saved_triggers'] = !empty($saved_triggers) ? $saved_triggers : array();
911
912
	if (isset($_POST['ban_items']))
913
	{
914
		$ban_group_id = isset($_REQUEST['bg']) ? (int) $_REQUEST['bg'] : 0;
915
		array_map('intval', $_POST['ban_items']);
916
917
		removeBanTriggers($_POST['ban_items'], $ban_group_id);
918
	}
919
920
	call_integration_hook('integrate_edit_bans_post', array());
921
922
	// Register the last modified date.
923
	updateSettings(array('banLastUpdated' => time()));
924
925
	// Update the member table to represent the new ban situation.
926
	updateBanMembers();
927
	redirectexit('action=admin;area=ban;sa=edit;bg=' . $ban_group_id);
928
}
929
930
/**
931
 * Saves one or more ban triggers into a ban item: according to the suggestions
932
 * checks the $_POST variable to verify if the trigger is present
933
 *
934
 * @param array $suggestions An array of suggestedtriggers (IP, email, etc.)
935
 * @param int $ban_group The ID of the group we're saving bans for
936
 * @param int $member The ID of the member associated with this ban (if applicable)
937
 * @param int $ban_id The ID of the ban (0 if this is a new ban)
938
 *
939
 * @return array|bool An array with the triggers if there were errors or false on success
940
 */
941
function saveTriggers(array $suggestions, $ban_group, $member = 0, $ban_id = 0)
942
{
943
	global $context;
944
945
	$triggers = array(
946
		'main_ip' => '',
947
		'hostname' => '',
948
		'email' => '',
949
		'member' => array(
950
			'id' => $member,
951
		)
952
	);
953
954
	foreach ($suggestions as $key => $value)
955
	{
956
		if (is_array($value))
957
			$triggers[$key] = $value;
958
		else
959
			$triggers[$value] = !empty($_POST[$value]) ? $_POST[$value] : '';
960
	}
961
962
	$ban_triggers = validateTriggers($triggers);
963
964
	call_integration_hook('integrate_save_triggers', array(&$ban_triggers, &$ban_group));
965
966
	// Time to save!
967
	if (!empty($ban_triggers['ban_triggers']) && empty($context['ban_errors']))
968
	{
969
		if (empty($ban_id))
970
			addTriggers($ban_group, $ban_triggers['ban_triggers'], $ban_triggers['log_info']);
971
		else
972
			updateTriggers($ban_id, $ban_group, array_shift($ban_triggers['ban_triggers']), $ban_triggers['log_info']);
973
	}
974
	if (!empty($context['ban_errors']))
975
		return $triggers;
976
	else
977
		return false;
978
}
979
980
/**
981
 * This function removes a bunch of triggers based on ids
982
 * Doesn't clean the inputs
983
 *
984
 * @param array $items_ids The items to remove
985
 * @param bool|int $group_id The ID of the group these triggers are associated with or false if deleting them from all groups
986
 * @return bool Always returns true
987
 */
988
function removeBanTriggers($items_ids = array(), $group_id = false)
989
{
990
	global $smcFunc, $scripturl;
991
992
	if ($group_id !== false)
993
		$group_id = (int) $group_id;
994
995
	if (empty($group_id) && empty($items_ids))
996
		return false;
997
998
	if (!is_array($items_ids))
999
		$items_ids = array($items_ids);
1000
1001
	$log_info = array();
1002
	$ban_items = array();
1003
1004
	call_integration_hook('integrate_remove_triggers', array(&$items_ids, $group_id));
1005
1006
	// First order of business: Load up the info so we can log this...
1007
	$request = $smcFunc['db_query']('', '
1008
		SELECT
1009
			bi.id_ban, bi.hostname, bi.email_address, bi.id_member, bi.hits,
1010
			bi.ip_low, bi.ip_high,
1011
			COALESCE(mem.id_member, 0) AS id_member, mem.member_name, mem.real_name
1012
		FROM {db_prefix}ban_items AS bi
1013
			LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = bi.id_member)
1014
		WHERE bi.id_ban IN ({array_int:ban_list})',
1015
		array(
1016
			'ban_list' => $items_ids,
1017
		)
1018
	);
1019
1020
	// Get all the info for the log
1021
	while ($row = $smcFunc['db_fetch_assoc']($request))
1022
	{
1023
		if (!empty($row['id_ban']))
1024
		{
1025
			$ban_items[$row['id_ban']] = array(
1026
				'id' => $row['id_ban'],
1027
			);
1028
			if (!empty($row['ip_high']))
1029
			{
1030
				$ban_items[$row['id_ban']]['type'] = 'ip';
1031
				$ban_items[$row['id_ban']]['ip'] = range2ip($row['ip_low'], $row['ip_high']);
1032
1033
				$is_range = (strpos($ban_items[$row['id_ban']]['ip'], '-') !== false || strpos($ban_items[$row['id_ban']]['ip'], '*') !== false);
1034
1035
				$log_info[] = array(
1036
					'bantype' => ($is_range ? 'ip_range' : 'main_ip'),
1037
					'value' => $ban_items[$row['id_ban']]['ip'],
1038
				);
1039
			}
1040
			elseif (!empty($row['hostname']))
1041
			{
1042
				$ban_items[$row['id_ban']]['type'] = 'hostname';
1043
				$ban_items[$row['id_ban']]['hostname'] = str_replace('%', '*', $row['hostname']);
1044
				$log_info[] = array(
1045
					'bantype' => 'hostname',
1046
					'value' => $row['hostname'],
1047
				);
1048
			}
1049
			elseif (!empty($row['email_address']))
1050
			{
1051
				$ban_items[$row['id_ban']]['type'] = 'email';
1052
				$ban_items[$row['id_ban']]['email'] = str_replace('%', '*', $row['email_address']);
1053
				$log_info[] = array(
1054
					'bantype' => 'email',
1055
					'value' => $ban_items[$row['id_ban']]['email'],
1056
				);
1057
			}
1058
			elseif (!empty($row['id_member']))
1059
			{
1060
				$ban_items[$row['id_ban']]['type'] = 'user';
1061
				$ban_items[$row['id_ban']]['user'] = array(
1062
					'id' => $row['id_member'],
1063
					'name' => $row['real_name'],
1064
					'href' => $scripturl . '?action=profile;u=' . $row['id_member'],
1065
					'link' => '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['real_name'] . '</a>',
1066
				);
1067
				$log_info[] = array(
1068
					'bantype' => 'user',
1069
					'value' => $row['id_member'],
1070
				);
1071
			}
1072
		}
1073
	}
1074
1075
	// Log this!
1076
	logTriggersUpdates($log_info, false, true);
1077
1078
	$smcFunc['db_free_result']($request);
1079
1080
	if ($group_id !== false)
1081
	{
1082
		$smcFunc['db_query']('', '
1083
			DELETE FROM {db_prefix}ban_items
1084
			WHERE id_ban IN ({array_int:ban_list})
1085
				AND id_ban_group = {int:ban_group}',
1086
			array(
1087
				'ban_list' => $items_ids,
1088
				'ban_group' => $group_id,
1089
			)
1090
		);
1091
	}
1092
	elseif (!empty($items_ids))
1093
	{
1094
		$smcFunc['db_query']('', '
1095
			DELETE FROM {db_prefix}ban_items
1096
			WHERE id_ban IN ({array_int:ban_list})',
1097
			array(
1098
				'ban_list' => $items_ids,
1099
			)
1100
		);
1101
	}
1102
1103
	return true;
1104
}
1105
1106
/**
1107
 * This function removes a bunch of ban groups based on ids
1108
 * Doesn't clean the inputs
1109
 *
1110
 * @param int[] $group_ids The IDs of the groups to remove
1111
 * @return bool Returns true if successful or false if $group_ids is empty
1112
 */
1113
function removeBanGroups($group_ids)
1114
{
1115
	global $smcFunc;
1116
1117
	if (!is_array($group_ids))
1118
		$group_ids = array($group_ids);
1119
1120
	$group_ids = array_unique($group_ids);
1121
1122
	if (empty($group_ids))
1123
		return false;
1124
1125
	$smcFunc['db_query']('', '
1126
		DELETE FROM {db_prefix}ban_groups
1127
		WHERE id_ban_group IN ({array_int:ban_list})',
1128
		array(
1129
			'ban_list' => $group_ids,
1130
		)
1131
	);
1132
1133
	// Remove all ban triggers for these bans groups
1134
	$request = $smcFunc['db_query']('', '
1135
		SELECT id_ban
1136
		FROM {db_prefix}ban_items
1137
		WHERE id_ban_group IN ({array_int:ban_list})',
1138
		array(
1139
			'ban_list' => $group_ids,
1140
		)
1141
	);
1142
1143
	$id_ban_triggers = array();
1144
	while ($row = $smcFunc['db_fetch_assoc']($request))
1145
	{
1146
		$id_ban_triggers[] = $row['id_ban'];
1147
	}
1148
	$smcFunc['db_free_result']($request);
1149
1150
	removeBanTriggers($id_ban_triggers);
1151
1152
	return true;
1153
}
1154
1155
/**
1156
 * Removes logs - by default truncate the table
1157
 * Doesn't clean the inputs
1158
 *
1159
 * @param array $ids Empty array to clear the ban log or the IDs of the log entries to remove
1160
 * @return bool Returns true if successful or false if $ids is invalid
1161
 */
1162
function removeBanLogs($ids = array())
1163
{
1164
	global $smcFunc;
1165
1166
	if (empty($ids))
1167
		$smcFunc['db_query']('truncate_table', '
1168
			TRUNCATE {db_prefix}log_banned',
1169
			array(
1170
			)
1171
		);
1172
	else
1173
	{
1174
		if (!is_array($ids))
1175
			$ids = array($ids);
1176
1177
		$ids = array_unique($ids);
1178
1179
		if (empty($ids))
1180
			return false;
1181
1182
		$smcFunc['db_query']('', '
1183
			DELETE FROM {db_prefix}log_banned
1184
			WHERE id_ban_log IN ({array_int:ban_list})',
1185
			array(
1186
				'ban_list' => $ids,
1187
			)
1188
		);
1189
	}
1190
1191
	return true;
1192
}
1193
1194
/**
1195
 * This function validates the ban triggers
1196
 *
1197
 * Errors in $context['ban_errors']
1198
 *
1199
 * @param array $triggers The triggers to validate
1200
 * @return array An array of riggers and log info ready to be used
1201
 */
1202
function validateTriggers(&$triggers)
1203
{
1204
	global $context, $smcFunc;
1205
1206
	if (empty($triggers))
1207
		$context['ban_errors'][] = 'ban_empty_triggers';
1208
1209
	$ban_triggers = array();
1210
	$log_info = array();
1211
1212
	foreach ($triggers as $key => $value)
1213
	{
1214
		if (!empty($value))
1215
		{
1216
			if ($key == 'member')
1217
				continue;
1218
1219
			if ($key == 'main_ip')
1220
			{
1221
				$value = trim($value);
1222
				$ip_parts = ip2range($value);
1223
				if (!checkExistingTriggerIP($ip_parts, $value))
1224
					$context['ban_errors'][] = 'invalid_ip';
1225
				else
1226
				{
1227
					$ban_triggers['main_ip'] = array(
1228
						'ip_low' => $ip_parts['low'],
1229
						'ip_high' => $ip_parts['high']
1230
					);
1231
				}
1232
			}
1233
			elseif ($key == 'hostname')
1234
			{
1235
				if (preg_match('/[^\w.\-*]/', $value) == 1)
1236
					$context['ban_errors'][] = 'invalid_hostname';
1237
				else
1238
				{
1239
					// Replace the * wildcard by a MySQL wildcard %.
1240
					$value = substr(str_replace('*', '%', $value), 0, 255);
1241
1242
					$ban_triggers['hostname']['hostname'] = $value;
1243
				}
1244
			}
1245
			elseif ($key == 'email')
1246
			{
1247
				if (preg_match('/[^\w.\-\+*@]/', $value) == 1)
1248
					$context['ban_errors'][] = 'invalid_email';
1249
1250
				// Check the user is not banning an admin.
1251
				$request = $smcFunc['db_query']('', '
1252
					SELECT id_member
1253
					FROM {db_prefix}members
1254
					WHERE (id_group = {int:admin_group} OR FIND_IN_SET({int:admin_group}, additional_groups) != 0)
1255
						AND email_address LIKE {string:email}
1256
					LIMIT 1',
1257
					array(
1258
						'admin_group' => 1,
1259
						'email' => $value,
1260
					)
1261
				);
1262
				if ($smcFunc['db_num_rows']($request) != 0)
1263
					$context['ban_errors'][] = 'no_ban_admin';
1264
				$smcFunc['db_free_result']($request);
1265
1266
				$value = substr(strtolower(str_replace('*', '%', $value)), 0, 255);
1267
1268
				$ban_triggers['email']['email_address'] = $value;
1269
			}
1270
			elseif ($key == 'user')
1271
			{
1272
				$user = preg_replace('~&amp;#(\d{4,5}|[2-9]\d{2,4}|1[2-9]\d);~', '&#$1;', $smcFunc['htmlspecialchars']($value, ENT_QUOTES));
1273
1274
				$request = $smcFunc['db_query']('', '
1275
					SELECT id_member, (id_group = {int:admin_group} OR FIND_IN_SET({int:admin_group}, additional_groups) != 0) AS isAdmin
1276
					FROM {db_prefix}members
1277
					WHERE member_name = {string:username} OR real_name = {string:username}
1278
					LIMIT 1',
1279
					array(
1280
						'admin_group' => 1,
1281
						'username' => $user,
1282
					)
1283
				);
1284
				if ($smcFunc['db_num_rows']($request) == 0)
1285
					$context['ban_errors'][] = 'invalid_username';
1286
				list ($value, $isAdmin) = $smcFunc['db_fetch_row']($request);
1287
				$smcFunc['db_free_result']($request);
1288
1289
				if ($isAdmin && strtolower($isAdmin) != 'f')
1290
				{
1291
					unset($value);
1292
					$context['ban_errors'][] = 'no_ban_admin';
1293
				}
1294
				else
1295
					$ban_triggers['user']['id_member'] = $value;
1296
			}
1297
			elseif (in_array($key, array('ips_in_messages', 'ips_in_errors')))
1298
			{
1299
				// Special case, those two are arrays themselves
1300
				$values = array_unique($value);
1301
				// Don't add the main IP again.
1302
				if (isset($triggers['main_ip']))
1303
					$values = array_diff($values, array($triggers['main_ip']));
1304
				unset($value);
1305
				foreach ($values as $val)
1306
				{
1307
					$val = trim($val);
1308
					$ip_parts = ip2range($val);
1309
					if (!checkExistingTriggerIP($ip_parts, $val))
1310
						$context['ban_errors'][] = 'invalid_ip';
1311
					else
1312
					{
1313
						$ban_triggers[$key][] = array(
1314
							'ip_low' => $ip_parts['low'],
1315
							'ip_high' => $ip_parts['high'],
1316
						);
1317
1318
						$log_info[] = array(
1319
							'value' => $val,
1320
							'bantype' => 'ip_range',
1321
						);
1322
					}
1323
				}
1324
			}
1325
			else
1326
				$context['ban_errors'][] = 'no_bantype_selected';
1327
1328
			if (isset($value) && !is_array($value))
1329
				$log_info[] = array(
1330
					'value' => $value,
1331
					'bantype' => $key,
1332
				);
1333
		}
1334
	}
1335
	return array('ban_triggers' => $ban_triggers, 'log_info' => $log_info);
1336
}
1337
1338
/**
1339
 * This function actually inserts the ban triggers into the database
1340
 *
1341
 * Errors in $context['ban_errors']
1342
 *
1343
 * @param int $group_id The ID of the group to add the triggers to (0 to create a new one)
1344
 * @param array $triggers The triggers to add
1345
 * @param array $logs The log data
1346
 * @return bool Whether or not the action was successful
1347
 */
1348
function addTriggers($group_id = 0, $triggers = array(), $logs = array())
1349
{
1350
	global $smcFunc, $context;
1351
1352
	if (empty($group_id))
1353
		$context['ban_errors'][] = 'ban_id_empty';
1354
1355
	// Preset all values that are required.
1356
	$values = array(
1357
		'id_ban_group' => $group_id,
1358
		'hostname' => '',
1359
		'email_address' => '',
1360
		'id_member' => 0,
1361
		'ip_low' => 'null',
1362
		'ip_high' => 'null',
1363
	);
1364
1365
	$insertKeys = array(
1366
		'id_ban_group' => 'int',
1367
		'hostname' => 'string',
1368
		'email_address' => 'string',
1369
		'id_member' => 'int',
1370
		'ip_low' => 'inet',
1371
		'ip_high' => 'inet',
1372
	);
1373
1374
	$insertTriggers = array();
1375
	foreach ($triggers as $key => $trigger)
1376
	{
1377
		// Exceptions, exceptions, exceptions...always exceptions... :P
1378
		if (in_array($key, array('ips_in_messages', 'ips_in_errors')))
1379
			foreach ($trigger as $real_trigger)
1380
				$insertTriggers[] = array_merge($values, $real_trigger);
1381
		else
1382
			$insertTriggers[] = array_merge($values, $trigger);
1383
	}
1384
1385
	if (empty($insertTriggers))
1386
		$context['ban_errors'][] = 'ban_no_triggers';
1387
1388
	if (!empty($context['ban_errors']))
1389
		return false;
1390
1391
	$smcFunc['db_insert']('',
1392
		'{db_prefix}ban_items',
1393
		$insertKeys,
1394
		$insertTriggers,
1395
		array('id_ban')
1396
	);
1397
1398
	logTriggersUpdates($logs, true);
1399
1400
	return true;
1401
}
1402
1403
/**
1404
 * This function updates an existing ban trigger into the database
1405
 *
1406
 * Errors in $context['ban_errors']
1407
 *
1408
 * @param int $ban_item The ID of the ban item
1409
 * @param int $group_id The ID of the ban group
1410
 * @param array $trigger An array of triggers
1411
 * @param array $logs An array of log info
1412
 */
1413
function updateTriggers($ban_item = 0, $group_id = 0, $trigger = array(), $logs = array())
1414
{
1415
	global $smcFunc, $context;
1416
1417
	if (empty($ban_item))
1418
		$context['ban_errors'][] = 'ban_ban_item_empty';
1419
	if (empty($group_id))
1420
		$context['ban_errors'][] = 'ban_id_empty';
1421
	if (empty($trigger))
1422
		$context['ban_errors'][] = 'ban_no_triggers';
1423
1424
	if (!empty($context['ban_errors']))
1425
		return;
1426
1427
	// Preset all values that are required.
1428
	$values = array(
1429
		'id_ban_group' => $group_id,
1430
		'hostname' => '',
1431
		'email_address' => '',
1432
		'id_member' => 0,
1433
		'ip_low' => 'null',
1434
		'ip_high' => 'null',
1435
	);
1436
1437
	$trigger = array_merge($values, $trigger);
1438
1439
	$smcFunc['db_query']('', '
1440
		UPDATE {db_prefix}ban_items
1441
		SET
1442
			hostname = {string:hostname}, email_address = {string:email_address}, id_member = {int:id_member},
1443
			ip_low = {inet:ip_low}, ip_high = {inet:ip_high}
1444
		WHERE id_ban = {int:ban_item}
1445
			AND id_ban_group = {int:id_ban_group}',
1446
		array_merge($trigger, array(
1447
			'id_ban_group' => $group_id,
1448
			'ban_item' => $ban_item,
1449
		))
1450
	);
1451
1452
	logTriggersUpdates($logs, false);
1453
}
1454
1455
/**
1456
 * A small function to unify logging of triggers (updates and new)
1457
 *
1458
 * @param array $logs an array of logs, each log contains the following keys:
1459
 *                - bantype: a known type of ban (ip_range, hostname, email, user, main_ip)
1460
 *                - value: the value of the bantype (e.g. the IP or the email address banned)
1461
 * @param bool $new Whether the trigger is new or an update of an existing one
1462
 * @param bool $removal Whether the trigger is being deleted
1463
 */
1464
function logTriggersUpdates($logs, $new = true, $removal = false)
1465
{
1466
	if (empty($logs))
1467
		return;
1468
1469
	$log_name_map = array(
1470
		'main_ip' => 'ip_range',
1471
		'hostname' => 'hostname',
1472
		'email' => 'email',
1473
		'user' => 'member',
1474
		'ip_range' => 'ip_range',
1475
	);
1476
1477
	// Log the addion of the ban entries into the moderation log.
1478
	foreach ($logs as $log)
1479
		logAction('ban' . ($removal == true ? 'remove' : ''), array(
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
1480
			$log_name_map[$log['bantype']] => $log['value'],
1481
			'new' => empty($new) ? 0 : 1,
1482
			'remove' => empty($removal) ? 0 : 1,
1483
			'type' => $log['bantype'],
1484
		));
1485
}
1486
1487
/**
1488
 * Updates an existing ban group
1489
 *
1490
 * Errors in $context['ban_errors']
1491
 *
1492
 * @param array $ban_info An array of info about the ban group. Should have name and may also have an id.
1493
 * @return int The ban group's ID
1494
 */
1495
function updateBanGroup($ban_info = array())
1496
{
1497
	global $smcFunc, $context;
1498
1499
	if (empty($ban_info['name']))
1500
		$context['ban_errors'][] = 'ban_name_empty';
1501
	if ($smcFunc['strlen']($ban_info['name']) > 20)
1502
		$context['ban_errors'][] = 'ban_name_is_too_long';
1503
	if (empty($ban_info['id']))
1504
		$context['ban_errors'][] = 'ban_id_empty';
1505
	if (empty($ban_info['cannot']['access']) && empty($ban_info['cannot']['register']) && empty($ban_info['cannot']['post']) && empty($ban_info['cannot']['login']))
1506
		$context['ban_errors'][] = 'ban_unknown_restriction_type';
1507
1508
	if (!empty($ban_info['id']))
1509
	{
1510
		// Verify the ban group exists.
1511
		$request = $smcFunc['db_query']('', '
1512
			SELECT id_ban_group
1513
			FROM {db_prefix}ban_groups
1514
			WHERE id_ban_group = {int:ban_group}
1515
			LIMIT 1',
1516
			array(
1517
				'ban_group' => $ban_info['id']
1518
			)
1519
		);
1520
1521
		if ($smcFunc['db_num_rows']($request) == 0)
1522
			$context['ban_errors'][] = 'ban_not_found';
1523
		$smcFunc['db_free_result']($request);
1524
	}
1525
1526
	if (!empty($ban_info['name']))
1527
	{
1528
		// Make sure the name does not already exist (Of course, if it exists in the ban group we are editing, proceed.)
1529
		$request = $smcFunc['db_query']('', '
1530
			SELECT id_ban_group
1531
			FROM {db_prefix}ban_groups
1532
			WHERE name = {string:new_ban_name}
1533
				AND id_ban_group != {int:ban_group}
1534
			LIMIT 1',
1535
			array(
1536
				'ban_group' => empty($ban_info['id']) ? 0 : $ban_info['id'],
1537
				'new_ban_name' => $ban_info['name'],
1538
			)
1539
		);
1540
		if ($smcFunc['db_num_rows']($request) != 0)
1541
			$context['ban_errors'][] = 'ban_name_exists';
1542
		$smcFunc['db_free_result']($request);
1543
	}
1544
1545
	if (!empty($context['ban_errors']))
1546
		return $ban_info['id'];
1547
1548
	$smcFunc['db_query']('', '
1549
		UPDATE {db_prefix}ban_groups
1550
		SET
1551
			name = {string:ban_name},
1552
			reason = {string:reason},
1553
			notes = {string:notes},
1554
			expire_time = {raw:expiration},
1555
			cannot_access = {int:cannot_access},
1556
			cannot_post = {int:cannot_post},
1557
			cannot_register = {int:cannot_register},
1558
			cannot_login = {int:cannot_login}
1559
		WHERE id_ban_group = {int:id_ban_group}',
1560
		array(
1561
			'expiration' => $ban_info['db_expiration'],
1562
			'cannot_access' => $ban_info['cannot']['access'],
1563
			'cannot_post' => $ban_info['cannot']['post'],
1564
			'cannot_register' => $ban_info['cannot']['register'],
1565
			'cannot_login' => $ban_info['cannot']['login'],
1566
			'id_ban_group' => $ban_info['id'],
1567
			'ban_name' => $ban_info['name'],
1568
			'reason' => $ban_info['reason'],
1569
			'notes' => $ban_info['notes'],
1570
		)
1571
	);
1572
1573
	return $ban_info['id'];
1574
}
1575
1576
/**
1577
 * Creates a new ban group
1578
 * If the group is successfully created the ID is returned
1579
 * On error the error code is returned or false
1580
 *
1581
 * Errors in $context['ban_errors']
1582
 *
1583
 * @param array $ban_info An array containing 'name', which is the name of the ban group
1584
 * @return int The ban group's ID
1585
 */
1586
function insertBanGroup($ban_info = array())
1587
{
1588
	global $smcFunc, $context;
1589
1590
	if (empty($ban_info['name']))
1591
		$context['ban_errors'][] = 'ban_name_empty';
1592
	if ($smcFunc['strlen']($ban_info['name']) > 20)
1593
		$context['ban_errors'][] = 'ban_name_is_too_long';
1594
	if (empty($ban_info['cannot']['access']) && empty($ban_info['cannot']['register']) && empty($ban_info['cannot']['post']) && empty($ban_info['cannot']['login']))
1595
		$context['ban_errors'][] = 'ban_unknown_restriction_type';
1596
1597
	if (!empty($ban_info['name']))
1598
	{
1599
		// Check whether a ban with this name already exists.
1600
		$request = $smcFunc['db_query']('', '
1601
			SELECT id_ban_group
1602
			FROM {db_prefix}ban_groups
1603
			WHERE name = {string:new_ban_name}' . '
1604
			LIMIT 1',
1605
			array(
1606
				'new_ban_name' => $ban_info['name'],
1607
			)
1608
		);
1609
1610
		if ($smcFunc['db_num_rows']($request) == 1)
1611
			$context['ban_errors'][] = 'ban_name_exists';
1612
		$smcFunc['db_free_result']($request);
1613
	}
1614
1615
	if (!empty($context['ban_errors']))
1616
		return;
1617
1618
	// Yes yes, we're ready to add now.
1619
	$ban_info['id'] = $smcFunc['db_insert']('',
1620
		'{db_prefix}ban_groups',
1621
		array(
1622
			'name' => 'string-20', 'ban_time' => 'int', 'expire_time' => 'raw', 'cannot_access' => 'int', 'cannot_register' => 'int',
1623
			'cannot_post' => 'int', 'cannot_login' => 'int', 'reason' => 'string-255', 'notes' => 'string-65534',
1624
		),
1625
		array(
1626
			$ban_info['name'], time(), $ban_info['db_expiration'], $ban_info['cannot']['access'], $ban_info['cannot']['register'],
1627
			$ban_info['cannot']['post'], $ban_info['cannot']['login'], $ban_info['reason'], $ban_info['notes'],
1628
		),
1629
		array('id_ban_group'),
1630
		1
1631
	);
1632
1633
	if (empty($ban_info['id']))
1634
		$context['ban_errors'][] = 'impossible_insert_new_bangroup';
1635
1636
	return $ban_info['id'];
1637
}
1638
1639
/**
1640
 * This function handles the ins and outs of the screen for adding new ban
1641
 * triggers or modifying existing ones.
1642
 * Adding new ban triggers:
1643
 * 	- is accessed by ?action=admin;area=ban;sa=edittrigger;bg=x
1644
 * 	- uses the ban_edit_trigger sub template of ManageBans.
1645
 * Editing existing ban triggers:
1646
 *  - is accessed by ?action=admin;area=ban;sa=edittrigger;bg=x;bi=y
1647
 *  - uses the ban_edit_trigger sub template of ManageBans.
1648
 */
1649
function BanEditTrigger()
1650
{
1651
	global $context, $smcFunc, $scripturl;
1652
1653
	$context['sub_template'] = 'ban_edit_trigger';
1654
	$context['form_url'] = $scripturl . '?action=admin;area=ban;sa=edittrigger';
1655
1656
	$ban_group = isset($_REQUEST['bg']) ? (int) $_REQUEST['bg'] : 0;
1657
	$ban_id = isset($_REQUEST['bi']) ? (int) $_REQUEST['bi'] : 0;
1658
1659
	if (empty($ban_group))
1660
		fatal_lang_error('ban_not_found', false);
1661
1662
	if (isset($_POST['add_new_trigger']) && !empty($_POST['ban_suggestions']))
1663
	{
1664
		saveTriggers($_POST['ban_suggestions'], $ban_group, 0, $ban_id);
1665
		redirectexit('action=admin;area=ban;sa=edit' . (!empty($ban_group) ? ';bg=' . $ban_group : ''));
1666
	}
1667
	elseif (isset($_POST['edit_trigger']) && !empty($_POST['ban_suggestions']))
1668
	{
1669
		saveTriggers($_POST['ban_suggestions'], $ban_group, 0, $ban_id);
1670
1671
		redirectexit('action=admin;area=ban;sa=edit' . (!empty($ban_group) ? ';bg=' . $ban_group : ''));
1672
	}
1673
	elseif (isset($_POST['edit_trigger']))
1674
	{
1675
		removeBanTriggers($ban_id);
1676
		redirectexit('action=admin;area=ban;sa=edit' . (!empty($ban_group) ? ';bg=' . $ban_group : ''));
1677
	}
1678
1679
	loadJavaScriptFile('suggest.js', array('minimize' => true), 'smf_suggest');
1680
1681
	if (empty($ban_id))
1682
	{
1683
		$context['ban_trigger'] = array(
1684
			'id' => 0,
1685
			'group' => $ban_group,
1686
			'ip' => array(
1687
				'value' => '',
1688
				'selected' => true,
1689
			),
1690
			'hostname' => array(
1691
				'selected' => false,
1692
				'value' => '',
1693
			),
1694
			'email' => array(
1695
				'value' => '',
1696
				'selected' => false,
1697
			),
1698
			'banneduser' => array(
1699
				'value' => '',
1700
				'selected' => false,
1701
			),
1702
			'is_new' => true,
1703
		);
1704
	}
1705
	else
1706
	{
1707
		$request = $smcFunc['db_query']('', '
1708
			SELECT
1709
				bi.id_ban, bi.id_ban_group, bi.hostname, bi.email_address, bi.id_member,
1710
				bi.ip_low, bi.ip_high,
1711
				mem.member_name, mem.real_name
1712
			FROM {db_prefix}ban_items AS bi
1713
				LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = bi.id_member)
1714
			WHERE bi.id_ban = {int:ban_item}
1715
				AND bi.id_ban_group = {int:ban_group}
1716
			LIMIT 1',
1717
			array(
1718
				'ban_item' => $ban_id,
1719
				'ban_group' => $ban_group,
1720
			)
1721
		);
1722
		if ($smcFunc['db_num_rows']($request) == 0)
1723
			fatal_lang_error('ban_not_found', false);
1724
		$row = $smcFunc['db_fetch_assoc']($request);
1725
		$smcFunc['db_free_result']($request);
1726
1727
		$context['ban_trigger'] = array(
1728
			'id' => $row['id_ban'],
1729
			'group' => $row['id_ban_group'],
1730
			'ip' => array(
1731
				'value' => empty($row['ip_low']) ? '' : range2ip($row['ip_low'], $row['ip_high']),
1732
				'selected' => !empty($row['ip_low']),
1733
			),
1734
			'hostname' => array(
1735
				'value' => str_replace('%', '*', $row['hostname']),
1736
				'selected' => !empty($row['hostname']),
1737
			),
1738
			'email' => array(
1739
				'value' => str_replace('%', '*', $row['email_address']),
1740
				'selected' => !empty($row['email_address'])
1741
			),
1742
			'banneduser' => array(
1743
				'value' => $row['member_name'],
1744
				'selected' => !empty($row['member_name'])
1745
			),
1746
			'is_new' => false,
1747
		);
1748
	}
1749
1750
	createToken('admin-bet');
1751
}
1752
1753
/**
1754
 * This handles the screen for showing the banned entities
1755
 * It is accessed by ?action=admin;area=ban;sa=browse
1756
 * It uses sub-tabs for browsing by IP, hostname, email or username.
1757
 *
1758
 * Uses a standard list (@see createList())
1759
 */
1760
function BanBrowseTriggers()
1761
{
1762
	global $modSettings, $context, $scripturl, $smcFunc, $txt;
1763
	global $sourcedir, $settings;
1764
1765
	if (!empty($_POST['remove_triggers']) && !empty($_POST['remove']) && is_array($_POST['remove']))
1766
	{
1767
		checkSession();
1768
1769
		removeBanTriggers($_POST['remove']);
1770
1771
		// Rehabilitate some members.
1772
		if ($_REQUEST['entity'] == 'member')
1773
			updateBanMembers();
1774
1775
		// Make sure the ban cache is refreshed.
1776
		updateSettings(array('banLastUpdated' => time()));
1777
	}
1778
1779
	$context['selected_entity'] = isset($_REQUEST['entity']) && in_array($_REQUEST['entity'], array('ip', 'hostname', 'email', 'member')) ? $_REQUEST['entity'] : 'ip';
1780
1781
	$listOptions = array(
1782
		'id' => 'ban_trigger_list',
1783
		'title' => $txt['ban_trigger_browse'],
1784
		'items_per_page' => $modSettings['defaultMaxListItems'],
1785
		'base_href' => $scripturl . '?action=admin;area=ban;sa=browse;entity=' . $context['selected_entity'],
1786
		'default_sort_col' => 'banned_entity',
1787
		'no_items_label' => $txt['ban_no_triggers'],
1788
		'get_items' => array(
1789
			'function' => 'list_getBanTriggers',
1790
			'params' => array(
1791
				$context['selected_entity'],
1792
			),
1793
		),
1794
		'get_count' => array(
1795
			'function' => 'list_getNumBanTriggers',
1796
			'params' => array(
1797
				$context['selected_entity'],
1798
			),
1799
		),
1800
		'columns' => array(
1801
			'banned_entity' => array(
1802
				'header' => array(
1803
					'value' => $txt['ban_banned_entity'],
1804
				),
1805
			),
1806
			'ban_name' => array(
1807
				'header' => array(
1808
					'value' => $txt['ban_name'],
1809
				),
1810
				'data' => array(
1811
					'sprintf' => array(
1812
						'format' => '<a href="' . $scripturl . '?action=admin;area=ban;sa=edit;bg=%1$d">%2$s</a>',
1813
						'params' => array(
1814
							'id_ban_group' => false,
1815
							'name' => false,
1816
						),
1817
					),
1818
				),
1819
				'sort' => array(
1820
					'default' => 'bg.name',
1821
					'reverse' => 'bg.name DESC',
1822
				),
1823
			),
1824
			'hits' => array(
1825
				'header' => array(
1826
					'value' => $txt['ban_hits'],
1827
				),
1828
				'data' => array(
1829
					'db' => 'hits',
1830
				),
1831
				'sort' => array(
1832
					'default' => 'bi.hits DESC',
1833
					'reverse' => 'bi.hits',
1834
				),
1835
			),
1836
			'check' => array(
1837
				'header' => array(
1838
					'value' => '<input type="checkbox" onclick="invertAll(this, this.form);">',
1839
					'class' => 'centercol',
1840
				),
1841
				'data' => array(
1842
					'sprintf' => array(
1843
						'format' => '<input type="checkbox" name="remove[]" value="%1$d">',
1844
						'params' => array(
1845
							'id_ban' => false,
1846
						),
1847
					),
1848
					'class' => 'centercol',
1849
				),
1850
			),
1851
		),
1852
		'form' => array(
1853
			'href' => $scripturl . '?action=admin;area=ban;sa=browse;entity=' . $context['selected_entity'],
1854
			'include_start' => true,
1855
			'include_sort' => true,
1856
		),
1857
		'additional_rows' => array(
1858
			array(
1859
				'position' => 'above_column_headers',
1860
				'value' => '<a href="' . $scripturl . '?action=admin;area=ban;sa=browse;entity=ip">' . ($context['selected_entity'] == 'ip' ? '<img src="' . $settings['images_url'] . '/selected.png" 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.png" 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.png" 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.png" alt="&gt;"> ' : '') . $txt['username'] . '</a>',
1861
			),
1862
			array(
1863
				'position' => 'bottom_of_list',
1864
				'value' => '<input type="submit" name="remove_triggers" value="' . $txt['ban_remove_selected_triggers'] . '" data-confirm="' . $txt['ban_remove_selected_triggers_confirm'] . '" class="button you_sure">',
1865
			),
1866
		),
1867
	);
1868
1869
	// Specific data for the first column depending on the selected entity.
1870
	if ($context['selected_entity'] === 'ip')
1871
	{
1872
		$listOptions['columns']['banned_entity']['data'] = array(
1873
			'function' => function($rowData)
1874
			{
1875
				return range2ip(
1876
					$rowData['ip_low']
1877
					,
1878
					$rowData['ip_high']
1879
				);
1880
			},
1881
		);
1882
		$listOptions['columns']['banned_entity']['sort'] = array(
1883
			'default' => 'bi.ip_low, bi.ip_high, bi.ip_low',
1884
			'reverse' => 'bi.ip_low DESC, bi.ip_high DESC',
1885
		);
1886
	}
1887
	elseif ($context['selected_entity'] === 'hostname')
1888
	{
1889
		$listOptions['columns']['banned_entity']['data'] = array(
1890
			'function' => function($rowData) use ($smcFunc)
1891
			{
1892
				return strtr($smcFunc['htmlspecialchars']($rowData['hostname']), array('%' => '*'));
1893
			},
1894
		);
1895
		$listOptions['columns']['banned_entity']['sort'] = array(
1896
			'default' => 'bi.hostname',
1897
			'reverse' => 'bi.hostname DESC',
1898
		);
1899
	}
1900
	elseif ($context['selected_entity'] === 'email')
1901
	{
1902
		$listOptions['columns']['banned_entity']['data'] = array(
1903
			'function' => function($rowData) use ($smcFunc)
1904
			{
1905
				return strtr($smcFunc['htmlspecialchars']($rowData['email_address']), array('%' => '*'));
1906
			},
1907
		);
1908
		$listOptions['columns']['banned_entity']['sort'] = array(
1909
			'default' => 'bi.email_address',
1910
			'reverse' => 'bi.email_address DESC',
1911
		);
1912
	}
1913
	elseif ($context['selected_entity'] === 'member')
1914
	{
1915
		$listOptions['columns']['banned_entity']['data'] = array(
1916
			'sprintf' => array(
1917
				'format' => '<a href="' . $scripturl . '?action=profile;u=%1$d">%2$s</a>',
1918
				'params' => array(
1919
					'id_member' => false,
1920
					'real_name' => false,
1921
				),
1922
			),
1923
		);
1924
		$listOptions['columns']['banned_entity']['sort'] = array(
1925
			'default' => 'mem.real_name',
1926
			'reverse' => 'mem.real_name DESC',
1927
		);
1928
	}
1929
1930
	// Create the list.
1931
	require_once($sourcedir . '/Subs-List.php');
1932
	createList($listOptions);
1933
1934
	// The list is the only thing to show, so make it the default sub template.
1935
	$context['sub_template'] = 'show_list';
1936
	$context['default_list'] = 'ban_trigger_list';
1937
}
1938
1939
/**
1940
 * Get ban triggers for the given parameters. Callback from $listOptions['get_items'] in BanBrowseTriggers()
1941
 *
1942
 * @param int $start The item to start with (for pagination purposes)
1943
 * @param int $items_per_page How many items to show on each page
1944
 * @param string $sort A string telling ORDER BY how to sort the results
1945
 * @param string $trigger_type The trigger type - can be 'ip', 'hostname' or 'email'
1946
 * @return array An array of ban trigger info for the list
1947
 */
1948
function list_getBanTriggers($start, $items_per_page, $sort, $trigger_type)
1949
{
1950
	global $smcFunc;
1951
1952
	$where = array(
1953
		'ip' => 'bi.ip_low is not null',
1954
		'hostname' => 'bi.hostname != {string:blank_string}',
1955
		'email' => 'bi.email_address != {string:blank_string}',
1956
	);
1957
1958
	$request = $smcFunc['db_query']('', '
1959
		SELECT
1960
			bi.id_ban, bi.ip_low, bi.ip_high, bi.hostname, bi.email_address, bi.hits,
1961
			bg.id_ban_group, bg.name' . ($trigger_type === 'member' ? ',
1962
			mem.id_member, mem.real_name' : '') . '
1963
		FROM {db_prefix}ban_items AS bi
1964
			INNER JOIN {db_prefix}ban_groups AS bg ON (bg.id_ban_group = bi.id_ban_group)' . ($trigger_type === 'member' ? '
1965
			INNER JOIN {db_prefix}members AS mem ON (mem.id_member = bi.id_member)' : '
1966
		WHERE ' . $where[$trigger_type]) . '
1967
		ORDER BY {raw:sort}
1968
		LIMIT {int:start}, {int:max}',
1969
		array(
1970
			'blank_string' => '',
1971
			'sort' => $sort,
1972
			'start' => $start,
1973
			'max' => $items_per_page,
1974
		)
1975
	);
1976
	$ban_triggers = array();
1977
	while ($row = $smcFunc['db_fetch_assoc']($request))
1978
		$ban_triggers[] = $row;
1979
	$smcFunc['db_free_result']($request);
1980
1981
	return $ban_triggers;
1982
}
1983
1984
/**
1985
 * This returns the total number of ban triggers of the given type. Callback for $listOptions['get_count'] in BanBrowseTriggers().
1986
 *
1987
 * @param string $trigger_type The trigger type. Can be 'ip', 'hostname' or 'email'
1988
 * @return int The number of triggers of the specified type
1989
 */
1990
function list_getNumBanTriggers($trigger_type)
1991
{
1992
	global $smcFunc;
1993
1994
	$where = array(
1995
		'ip' => 'bi.ip_low is not null',
1996
		'hostname' => 'bi.hostname != {string:blank_string}',
1997
		'email' => 'bi.email_address != {string:blank_string}',
1998
	);
1999
2000
	$request = $smcFunc['db_query']('', '
2001
		SELECT COUNT(*)
2002
		FROM {db_prefix}ban_items AS bi' . ($trigger_type === 'member' ? '
2003
			INNER JOIN {db_prefix}members AS mem ON (mem.id_member = bi.id_member)' : '
2004
		WHERE ' . $where[$trigger_type]),
2005
		array(
2006
			'blank_string' => '',
2007
		)
2008
	);
2009
	list ($num_triggers) = $smcFunc['db_fetch_row']($request);
2010
	$smcFunc['db_free_result']($request);
2011
2012
	return $num_triggers;
2013
}
2014
2015
/**
2016
 * This handles the listing of ban log entries, and allows their deletion.
2017
 * Shows a list of logged access attempts by banned users.
2018
 * It is accessed by ?action=admin;area=ban;sa=log.
2019
 * How it works:
2020
 *  - allows sorting of several columns.
2021
 *  - also handles deletion of (a selection of) log entries.
2022
 */
2023
function BanLog()
2024
{
2025
	global $scripturl, $context, $sourcedir, $txt, $modSettings;
2026
2027
	// Delete one or more entries.
2028
	if (!empty($_POST['removeAll']) || (!empty($_POST['removeSelected']) && !empty($_POST['remove'])))
2029
	{
2030
		checkSession();
2031
		validateToken('admin-bl');
2032
2033
		// 'Delete all entries' button was pressed.
2034
		if (!empty($_POST['removeAll']))
2035
			removeBanLogs();
2036
		// 'Delete selection' button was pressed.
2037
		else
2038
		{
2039
			array_map('intval', $_POST['remove']);
2040
			removeBanLogs($_POST['remove']);
2041
		}
2042
	}
2043
2044
	$listOptions = array(
2045
		'id' => 'ban_log',
2046
		'title' => $txt['ban_log'],
2047
		'items_per_page' => $modSettings['defaultMaxListItems'],
2048
		'base_href' => $context['admin_area'] == 'ban' ? $scripturl . '?action=admin;area=ban;sa=log' : $scripturl . '?action=admin;area=logs;sa=banlog',
2049
		'default_sort_col' => 'date',
2050
		'get_items' => array(
2051
			'function' => 'list_getBanLogEntries',
2052
		),
2053
		'get_count' => array(
2054
			'function' => 'list_getNumBanLogEntries',
2055
		),
2056
		'no_items_label' => $txt['ban_log_no_entries'],
2057
		'columns' => array(
2058
			'ip' => array(
2059
				'header' => array(
2060
					'value' => $txt['ban_log_ip'],
2061
				),
2062
				'data' => array(
2063
					'sprintf' => array(
2064
						'format' => '<a href="' . $scripturl . '?action=trackip;searchip=%1$s">%1$s</a>',
2065
						'params' => array(
2066
							'ip' => false,
2067
						),
2068
					),
2069
				),
2070
				'sort' => array(
2071
					'default' => 'lb.ip',
2072
					'reverse' => 'lb.ip DESC',
2073
				),
2074
			),
2075
			'email' => array(
2076
				'header' => array(
2077
					'value' => $txt['ban_log_email'],
2078
				),
2079
				'data' => array(
2080
					'db_htmlsafe' => 'email',
2081
				),
2082
				'sort' => array(
2083
					'default' => 'lb.email = \'\', lb.email',
2084
					'reverse' => 'lb.email != \'\', lb.email DESC',
2085
				),
2086
			),
2087
			'member' => array(
2088
				'header' => array(
2089
					'value' => $txt['ban_log_member'],
2090
				),
2091
				'data' => array(
2092
					'sprintf' => array(
2093
						'format' => '<a href="' . $scripturl . '?action=profile;u=%1$d">%2$s</a>',
2094
						'params' => array(
2095
							'id_member' => false,
2096
							'real_name' => false,
2097
						),
2098
					),
2099
				),
2100
				'sort' => array(
2101
					'default' => 'COALESCE(mem.real_name, 1=1), mem.real_name',
2102
					'reverse' => 'COALESCE(mem.real_name, 1=1) DESC, mem.real_name DESC',
2103
				),
2104
			),
2105
			'date' => array(
2106
				'header' => array(
2107
					'value' => $txt['ban_log_date'],
2108
				),
2109
				'data' => array(
2110
					'function' => function($rowData)
2111
					{
2112
						return timeformat($rowData['log_time']);
2113
					},
2114
				),
2115
				'sort' => array(
2116
					'default' => 'lb.log_time DESC',
2117
					'reverse' => 'lb.log_time',
2118
				),
2119
			),
2120
			'check' => array(
2121
				'header' => array(
2122
					'value' => '<input type="checkbox" onclick="invertAll(this, this.form);">',
2123
					'class' => 'centercol',
2124
				),
2125
				'data' => array(
2126
					'sprintf' => array(
2127
						'format' => '<input type="checkbox" name="remove[]" value="%1$d">',
2128
						'params' => array(
2129
							'id_ban_log' => false,
2130
						),
2131
					),
2132
					'class' => 'centercol',
2133
				),
2134
			),
2135
		),
2136
		'form' => array(
2137
			'href' => $context['admin_area'] == 'ban' ? $scripturl . '?action=admin;area=ban;sa=log' : $scripturl . '?action=admin;area=logs;sa=banlog',
2138
			'include_start' => true,
2139
			'include_sort' => true,
2140
			'token' => 'admin-bl',
2141
		),
2142
		'additional_rows' => array(
2143
			array(
2144
				'position' => 'after_title',
2145
				'value' => '
2146
					<input type="submit" name="removeSelected" value="' . $txt['ban_log_remove_selected'] . '" data-confirm="' . $txt['ban_log_remove_selected_confirm'] . '" class="button you_sure">
2147
					<input type="submit" name="removeAll" value="' . $txt['ban_log_remove_all'] . '" data-confirm="' . $txt['ban_log_remove_all_confirm'] . '" class="button you_sure">',
2148
			),
2149
			array(
2150
				'position' => 'bottom_of_list',
2151
				'value' => '
2152
					<input type="submit" name="removeSelected" value="' . $txt['ban_log_remove_selected'] . '" data-confirm="' . $txt['ban_log_remove_selected_confirm'] . '" class="button you_sure">
2153
					<input type="submit" name="removeAll" value="' . $txt['ban_log_remove_all'] . '" data-confirm="' . $txt['ban_log_remove_all_confirm'] . '" class="button you_sure">',
2154
			),
2155
		),
2156
	);
2157
2158
	createToken('admin-bl');
2159
2160
	require_once($sourcedir . '/Subs-List.php');
2161
	createList($listOptions);
2162
2163
	$context['page_title'] = $txt['ban_log'];
2164
	$context['sub_template'] = 'show_list';
2165
	$context['default_list'] = 'ban_log';
2166
}
2167
2168
/**
2169
 * Load a list of ban log entries from the database.
2170
 * (no permissions check). Callback for $listOptions['get_items'] in BanLog()
2171
 *
2172
 * @param int $start The item to start with (for pagination purposes)
2173
 * @param int $items_per_page How many items to show on each page
2174
 * @param string $sort A string telling ORDER BY how to sort the results
2175
 * @return array An array of info about the ban log entries for the list.
2176
 */
2177
function list_getBanLogEntries($start, $items_per_page, $sort)
2178
{
2179
	global $smcFunc;
2180
2181
	$dash = '-';
2182
2183
	$request = $smcFunc['db_query']('', '
2184
		SELECT lb.id_ban_log, lb.id_member, lb.ip AS ip, COALESCE(lb.email, {string:dash}) AS email, lb.log_time, COALESCE(mem.real_name, {string:blank_string}) AS real_name
2185
		FROM {db_prefix}log_banned AS lb
2186
			LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = lb.id_member)
2187
		ORDER BY {raw:sort}
2188
		LIMIT {int:start}, {int:items}',
2189
		array(
2190
			'blank_string' => '',
2191
			'dash' => $dash,
2192
			'sort' => $sort,
2193
			'start' => $start,
2194
			'items' => $items_per_page,
2195
		)
2196
	);
2197
	$log_entries = array();
2198
	while ($row = $smcFunc['db_fetch_assoc']($request))
2199
	{
2200
		$row['ip'] = $row['ip'] === null ? $dash : inet_dtop($row['ip']);
2201
		$log_entries[] = $row;
2202
	}
2203
	$smcFunc['db_free_result']($request);
2204
2205
	return $log_entries;
2206
}
2207
2208
/**
2209
 * This returns the total count of ban log entries. Callback for $listOptions['get_count'] in BanLog().
2210
 *
2211
 * @return int The total number of ban log entries.
2212
 */
2213
function list_getNumBanLogEntries()
2214
{
2215
	global $smcFunc;
2216
2217
	$request = $smcFunc['db_query']('', '
2218
		SELECT COUNT(*)
2219
		FROM {db_prefix}log_banned AS lb',
2220
		array(
2221
		)
2222
	);
2223
	list ($num_entries) = $smcFunc['db_fetch_row']($request);
2224
	$smcFunc['db_free_result']($request);
2225
2226
	return $num_entries;
2227
}
2228
2229
/**
2230
 * Convert a range of given IP number into a single string.
2231
 * It's practically the reverse function of ip2range().
2232
 *
2233
 * @example
2234
 * range2ip(array(10, 10, 10, 0), array(10, 10, 20, 255)) returns '10.10.10-20.*
2235
 *
2236
 * @param array $low The low end of the range in IPv4 format
2237
 * @param array $high The high end of the range in IPv4 format
2238
 * @return string A string indicating the range
2239
 */
2240
function range2ip($low, $high)
2241
{
2242
	$low = inet_dtop($low);
2243
	$high = inet_dtop($high);
2244
2245
	if ($low == '255.255.255.255') return 'unknown';
2246
	if ($low == $high)
2247
		return $low;
2248
	else
2249
		return $low . '-' . $high;
2250
}
2251
2252
/**
2253
 * Checks whether a given IP range already exists in the trigger list.
2254
 * If yes, it returns an error message. Otherwise, it returns an array
2255
 *  optimized for the database.
2256
 *
2257
 * @param array $ip_array An array of IP trigger data
2258
 * @param string $fullip The full IP
2259
 * @return boolean|array False if the trigger array is invalid or the passed array if the value doesn't exist in the database
2260
 */
2261
function checkExistingTriggerIP($ip_array, $fullip = '')
2262
{
2263
	global $smcFunc, $scripturl;
2264
2265
	$values = array(
2266
		'ip_low' => $ip_array['low'],
2267
		'ip_high' => $ip_array['high']
2268
	);
2269
2270
	$request = $smcFunc['db_query']('', '
2271
		SELECT bg.id_ban_group, bg.name
2272
		FROM {db_prefix}ban_groups AS bg
2273
		INNER JOIN {db_prefix}ban_items AS bi ON
2274
			(bi.id_ban_group = bg.id_ban_group)
2275
			AND ip_low = {inet:ip_low} AND ip_high = {inet:ip_high}
2276
		LIMIT 1',
2277
		$values
2278
	);
2279
	if ($smcFunc['db_num_rows']($request) != 0)
2280
	{
2281
		list ($error_id_ban, $error_ban_name) = $smcFunc['db_fetch_row']($request);
2282
		fatal_lang_error('ban_trigger_already_exists', false, array(
2283
			$fullip,
2284
			'<a href="' . $scripturl . '?action=admin;area=ban;sa=edit;bg=' . $error_id_ban . '">' . $error_ban_name . '</a>',
2285
		));
2286
	}
2287
	$smcFunc['db_free_result']($request);
2288
2289
	return $values;
2290
}
2291
2292
/**
2293
 * As it says... this tries to review the list of banned members, to match new bans.
2294
 * Note: is_activated >= 10: a member is banned.
2295
 */
2296
function updateBanMembers()
2297
{
2298
	global $smcFunc;
2299
2300
	$updates = array();
2301
	$allMembers = array();
2302
	$newMembers = array();
2303
2304
	// Start by getting all active bans - it's quicker doing this in parts...
2305
	$request = $smcFunc['db_query']('', '
2306
		SELECT bi.id_member, bi.email_address
2307
		FROM {db_prefix}ban_items AS bi
2308
			INNER JOIN {db_prefix}ban_groups AS bg ON (bg.id_ban_group = bi.id_ban_group)
2309
		WHERE (bi.id_member > {int:no_member} OR bi.email_address != {string:blank_string})
2310
			AND bg.cannot_access = {int:cannot_access_on}
2311
			AND (bg.expire_time IS NULL OR bg.expire_time > {int:current_time})',
2312
		array(
2313
			'no_member' => 0,
2314
			'cannot_access_on' => 1,
2315
			'current_time' => time(),
2316
			'blank_string' => '',
2317
		)
2318
	);
2319
	$memberIDs = array();
2320
	$memberEmails = array();
2321
	$memberEmailWild = array();
2322
	while ($row = $smcFunc['db_fetch_assoc']($request))
2323
	{
2324
		if ($row['id_member'])
2325
			$memberIDs[$row['id_member']] = $row['id_member'];
2326
		if ($row['email_address'])
2327
		{
2328
			// Does it have a wildcard - if so we can't do a IN on it.
2329
			if (strpos($row['email_address'], '%') !== false)
2330
				$memberEmailWild[$row['email_address']] = $row['email_address'];
2331
			else
2332
				$memberEmails[$row['email_address']] = $row['email_address'];
2333
		}
2334
	}
2335
	$smcFunc['db_free_result']($request);
2336
2337
	// Build up the query.
2338
	$queryPart = array();
2339
	$queryValues = array();
2340
	if (!empty($memberIDs))
2341
	{
2342
		$queryPart[] = 'mem.id_member IN ({array_string:member_ids})';
2343
		$queryValues['member_ids'] = $memberIDs;
2344
	}
2345
	if (!empty($memberEmails))
2346
	{
2347
		$queryPart[] = 'mem.email_address IN ({array_string:member_emails})';
2348
		$queryValues['member_emails'] = $memberEmails;
2349
	}
2350
	$count = 0;
2351
	foreach ($memberEmailWild as $email)
2352
	{
2353
		$queryPart[] = 'mem.email_address LIKE {string:wild_' . $count . '}';
2354
		$queryValues['wild_' . $count++] = $email;
2355
	}
2356
2357
	// Find all banned members.
2358
	if (!empty($queryPart))
2359
	{
2360
		$request = $smcFunc['db_query']('', '
2361
			SELECT mem.id_member, mem.is_activated
2362
			FROM {db_prefix}members AS mem
2363
			WHERE ' . implode(' OR ', $queryPart),
2364
			$queryValues
2365
		);
2366
		while ($row = $smcFunc['db_fetch_assoc']($request))
2367
		{
2368
			if (!in_array($row['id_member'], $allMembers))
2369
			{
2370
				$allMembers[] = $row['id_member'];
2371
				// Do they need an update?
2372
				if ($row['is_activated'] < 10)
2373
				{
2374
					$updates[($row['is_activated'] + 10)][] = $row['id_member'];
2375
					$newMembers[] = $row['id_member'];
2376
				}
2377
			}
2378
		}
2379
		$smcFunc['db_free_result']($request);
2380
	}
2381
2382
	// We welcome our new members in the realm of the banned.
2383
	if (!empty($newMembers))
2384
		$smcFunc['db_query']('', '
2385
			DELETE FROM {db_prefix}log_online
2386
			WHERE id_member IN ({array_int:new_banned_members})',
2387
			array(
2388
				'new_banned_members' => $newMembers,
2389
			)
2390
		);
2391
2392
	// Find members that are wrongfully marked as banned.
2393
	$request = $smcFunc['db_query']('', '
2394
		SELECT mem.id_member, mem.is_activated - 10 AS new_value
2395
		FROM {db_prefix}members AS mem
2396
			LEFT JOIN {db_prefix}ban_items AS bi ON (bi.id_member = mem.id_member OR mem.email_address LIKE bi.email_address)
2397
			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}))
2398
		WHERE (bi.id_ban IS NULL OR bg.id_ban_group IS NULL)
2399
			AND mem.is_activated >= {int:ban_flag}',
2400
		array(
2401
			'cannot_access_activated' => 1,
2402
			'current_time' => time(),
2403
			'ban_flag' => 10,
2404
		)
2405
	);
2406
	while ($row = $smcFunc['db_fetch_assoc']($request))
2407
	{
2408
		// Don't do this twice!
2409
		if (!in_array($row['id_member'], $allMembers))
2410
		{
2411
			$updates[$row['new_value']][] = $row['id_member'];
2412
			$allMembers[] = $row['id_member'];
2413
		}
2414
	}
2415
	$smcFunc['db_free_result']($request);
2416
2417
	if (!empty($updates))
2418
		foreach ($updates as $newStatus => $members)
2419
			updateMemberData($members, array('is_activated' => $newStatus));
2420
2421
	// Update the latest member and our total members as banning may change them.
2422
	updateStats('member');
2423
}
2424
2425
/**
2426
 * Gets basic member data for the ban
2427
 *
2428
 * @param int $id The ID of the member to get data for
2429
 * @return array An aray containing the ID, name, main IP and email address of the specified user
2430
 */
2431
function getMemberData($id)
2432
{
2433
	global $smcFunc;
2434
2435
	$suggestions = array();
2436
	$request = $smcFunc['db_query']('', '
2437
		SELECT id_member, real_name, member_ip, email_address
2438
		FROM {db_prefix}members
2439
		WHERE id_member = {int:current_user}
2440
		LIMIT 1',
2441
		array(
2442
			'current_user' => $id,
2443
		)
2444
	);
2445
	if ($smcFunc['db_num_rows']($request) > 0)
2446
	{
2447
		list ($suggestions['member']['id'], $suggestions['member']['name'], $suggestions['main_ip'], $suggestions['email']) = $smcFunc['db_fetch_row']($request);
2448
		$suggestions['main_ip'] = inet_dtop($suggestions['main_ip']);
2449
	}
2450
	$smcFunc['db_free_result']($request);
2451
2452
	return $suggestions;
2453
}
2454
2455
?>