Passed
Push — release-2.1 ( 0c2197...207d2d )
by Jeremy
05:47
created

ManageMembers.php ➔ AdminApprove()   F

Complexity

Conditions 44
Paths > 20000

Size

Total Lines 244

Duplication

Lines 38
Ratio 15.57 %

Importance

Changes 0
Metric Value
cc 44
nc 21120
nop 0
dl 38
loc 244
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Show a list of members or a selection of members.
5
 *
6
 * Simple Machines Forum (SMF)
7
 *
8
 * @package SMF
9
 * @author Simple Machines http://www.simplemachines.org
10
 * @copyright 2018 Simple Machines and individual contributors
11
 * @license http://www.simplemachines.org/about/smf/license.php BSD
12
 *
13
 * @version 2.1 Beta 4
14
 */
15
16
if (!defined('SMF'))
17
	die('No direct access...');
18
19
/**
20
 * The main entrance point for the Manage Members screen.
21
 * As everyone else, it calls a function based on the given sub-action.
22
 * Called by ?action=admin;area=viewmembers.
23
 * Requires the moderate_forum permission.
24
 *
25
 * @uses ManageMembers template
26
 * @uses ManageMembers language file.
27
 */
28
function ViewMembers()
29
{
30
	global $txt, $scripturl, $context, $modSettings, $smcFunc;
31
32
	$subActions = array(
33
		'all' => array('ViewMemberlist', 'moderate_forum'),
34
		'approve' => array('AdminApprove', 'moderate_forum'),
35
		'browse' => array('MembersAwaitingActivation', 'moderate_forum'),
36
		'search' => array('SearchMembers', 'moderate_forum'),
37
		'query' => array('ViewMemberlist', 'moderate_forum'),
38
	);
39
40
	// Load the essentials.
41
	loadLanguage('ManageMembers');
42
	loadTemplate('ManageMembers');
43
44
	// Fetch our activation counts.
45
	GetMemberActivationCounts();
46
47
	// For the page header... do we show activation?
48
	$context['show_activate'] = (!empty($modSettings['registration_method']) && $modSettings['registration_method'] == 1) || !empty($context['awaiting_activation']);
0 ignored issues
show
introduced by
Consider adding parentheses for clarity. Current Interpretation: $context['show_activate'...awaiting_activation'])), Probably Intended Meaning: $context['show_activate'...awaiting_activation']))
Loading history...
49
50
	// What about approval?
51
	$context['show_approve'] = (!empty($modSettings['registration_method']) && $modSettings['registration_method'] == 2) || !empty($context['awaiting_approval']) || !empty($modSettings['approveAccountDeletion']);
52
53
	// Setup the admin tabs.
54
	$context[$context['admin_menu_name']]['tab_data'] = array(
55
		'title' => $txt['admin_members'],
56
		'help' => 'view_members',
57
		'description' => $txt['admin_members_list'],
58
		'tabs' => array(),
59
	);
60
61
	$context['tabs'] = array(
62
		'viewmembers' => array(
63
			'label' => $txt['view_all_members'],
64
			'description' => $txt['admin_members_list'],
65
			'url' => $scripturl . '?action=admin;area=viewmembers;sa=all',
66
			'selected_actions' => array('all'),
67
		),
68
		'search' => array(
69
			'label' => $txt['mlist_search'],
70
			'description' => $txt['admin_members_list'],
71
			'url' => $scripturl . '?action=admin;area=viewmembers;sa=search',
72
			'selected_actions' => array('search', 'query'),
73
		),
74
	);
75
	$context['last_tab'] = 'search';
76
77
	// Do we have approvals
78
	if ($context['show_approve'])
79
	{
80
		$context['tabs']['approve'] = array(
81
			'label' => sprintf($txt['admin_browse_awaiting_approval'], $context['awaiting_approval']),
82
			'description' => $txt['admin_browse_approve_desc'],
83
			'url' => $scripturl . '?action=admin;area=viewmembers;sa=browse;type=approve',
84
		);
85
		$context['last_tab'] = 'approve';
86
	}
87
88
	// Do we have activations to show?
89
	if ($context['show_activate'])
90
	{
91
		$context['tabs']['activate'] = array(
92
			'label' => sprintf($txt['admin_browse_awaiting_activate'], $context['awaiting_activation']),
93
			'description' => $txt['admin_browse_activate_desc'],
94
			'url' => $scripturl . '?action=admin;area=viewmembers;sa=browse;type=activate',
95
		);
96
		$context['last_tab'] = 'activate';
97
	}
98
99
	// Call our hook now, letting customizations add to the subActions and/or modify $context as needed.
100
	call_integration_hook('integrate_manage_members', array(&$subActions));
101
102
	// Default to sub action 'index' or 'settings' depending on permissions.
103
	$context['current_subaction'] = isset($_REQUEST['sa']) && isset($subActions[$_REQUEST['sa']]) ? $_REQUEST['sa'] : 'all';
104
105
	// We know the sub action, now we know what you're allowed to do.
106
	isAllowedTo($subActions[$context['current_subaction']][1]);
107
108
	// Set the last tab.
109
	$context['tabs'][$context['last_tab']]['is_last'] = true;
110
111
	// Find the active tab.
112
	if (isset($context['tabs'][$context['current_subaction']]))
113
		$context['tabs'][$context['current_subaction']]['is_selected'] = true;
114
	elseif (isset($context['current_subaction']))
115
		foreach ($context['tabs'] as $id_tab => $tab_data)
116
			if (!empty($tab_data['selected_actions']) && in_array($context['current_subaction'], $tab_data['selected_actions']))
117
				$context['tabs'][$id_tab]['is_selected'] = true;
118
119
	call_helper($subActions[$context['current_subaction']][0]);
120
}
121
122
/**
123
 * View all members list. It allows sorting on several columns, and deletion of
124
 * selected members. It also handles the search query sent by
125
 * ?action=admin;area=viewmembers;sa=search.
126
 * Called by ?action=admin;area=viewmembers;sa=all or ?action=admin;area=viewmembers;sa=query.
127
 * Requires the moderate_forum permission.
128
 *
129
 * @uses the view_members sub template of the ManageMembers template.
130
 */
131
function ViewMemberlist()
132
{
133
	global $txt, $scripturl, $context, $modSettings, $sourcedir, $smcFunc, $user_info;
134
135
	// Are we performing a delete?
136
	if (isset($_POST['delete_members']) && !empty($_POST['delete']) && allowedTo('profile_remove_any'))
137
	{
138
		checkSession();
139
140
		// Clean the input.
141
		foreach ($_POST['delete'] as $key => $value)
142
		{
143
			// Don't delete yourself, idiot.
144
			if ($value != $user_info['id'])
145
				$delete[$key] = (int) $value;
146
		}
147
148
		if (!empty($delete))
149
		{
150
			// Delete all the selected members.
151
			require_once($sourcedir . '/Subs-Members.php');
152
			deleteMembers($delete, true);
153
		}
154
	}
155
156
	// Check input after a member search has been submitted.
157
	if ($context['current_subaction'] == 'query')
158
	{
159
		// Retrieving the membergroups and postgroups.
160
		$context['membergroups'] = array(
161
			array(
162
				'id' => 0,
163
				'name' => $txt['membergroups_members'],
164
				'can_be_additional' => false
165
			)
166
		);
167
		$context['postgroups'] = array();
168
169
		$request = $smcFunc['db_query']('', '
170
			SELECT id_group, group_name, min_posts
171
			FROM {db_prefix}membergroups
172
			WHERE id_group != {int:moderator_group}
173
			ORDER BY min_posts, CASE WHEN id_group < {int:newbie_group} THEN id_group ELSE 4 END, group_name',
174
			array(
175
				'moderator_group' => 3,
176
				'newbie_group' => 4,
177
			)
178
		);
179
		while ($row = $smcFunc['db_fetch_assoc']($request))
180
		{
181
			if ($row['min_posts'] == -1)
182
				$context['membergroups'][] = array(
183
					'id' => $row['id_group'],
184
					'name' => $row['group_name'],
185
					'can_be_additional' => true
186
				);
187
			else
188
				$context['postgroups'][] = array(
189
					'id' => $row['id_group'],
190
					'name' => $row['group_name']
191
				);
192
		}
193
		$smcFunc['db_free_result']($request);
194
195
		// Some data about the form fields and how they are linked to the database.
196
		$params = array(
197
			'mem_id' => array(
198
				'db_fields' => array('id_member'),
199
				'type' => 'int',
200
				'range' => true
201
			),
202
			'age' => array(
203
				'db_fields' => array('birthdate'),
204
				'type' => 'age',
205
				'range' => true
206
			),
207
			'posts' => array(
208
				'db_fields' => array('posts'),
209
				'type' => 'int',
210
				'range' => true
211
			),
212
			'reg_date' => array(
213
				'db_fields' => array('date_registered'),
214
				'type' => 'date',
215
				'range' => true
216
			),
217
			'last_online' => array(
218
				'db_fields' => array('last_login'),
219
				'type' => 'date',
220
				'range' => true
221
			),
222
			'activated' => array(
223
				'db_fields' => array('CASE WHEN is_activated IN (1, 11) THEN 1 ELSE 0 END'),
224
				'type' => 'checkbox',
225
				'values' => array('0', '1'),
226
			),
227
			'membername' => array(
228
				'db_fields' => array('member_name', 'real_name'),
229
				'type' => 'string'
230
			),
231
			'email' => array(
232
				'db_fields' => array('email_address'),
233
				'type' => 'string'
234
			),
235
			'website' => array(
236
				'db_fields' => array('website_title', 'website_url'),
237
				'type' => 'string'
238
			),
239
			'ip' => array(
240
				'db_fields' => array('member_ip'),
241
				'type' => 'inet'
242
			),
243
			'membergroups' => array(
244
				'db_fields' => array('id_group'),
245
				'type' => 'groups'
246
			),
247
			'postgroups' => array(
248
				'db_fields' => array('id_group'),
249
				'type' => 'groups'
250
			)
251
		);
252
		$range_trans = array(
253
			'--' => '<',
254
			'-' => '<=',
255
			'=' => '=',
256
			'+' => '>=',
257
			'++' => '>'
258
		);
259
260
		call_integration_hook('integrate_view_members_params', array(&$params));
261
262
		$search_params = array();
263
		if ($context['current_subaction'] == 'query' && !empty($_REQUEST['params']) && empty($_POST['types']))
264
			$search_params = $smcFunc['json_decode'](base64_decode($_REQUEST['params']), true);
265
		elseif (!empty($_POST))
266
		{
267
			$search_params['types'] = $_POST['types'];
268
			foreach ($params as $param_name => $param_info)
269
				if (isset($_POST[$param_name]))
270
					$search_params[$param_name] = $_POST[$param_name];
271
		}
272
273
		$search_url_params = isset($search_params) ? base64_encode($smcFunc['json_encode']($search_params)) : null;
274
275
		// @todo Validate a little more.
276
277
		// Loop through every field of the form.
278
		$query_parts = array();
279
		$where_params = array();
280
		foreach ($params as $param_name => $param_info)
281
		{
282
			// Not filled in?
283
			if (!isset($search_params[$param_name]) || $search_params[$param_name] === '')
284
				continue;
285
286
			// Make sure numeric values are really numeric.
287
			if (in_array($param_info['type'], array('int', 'age')))
288
				$search_params[$param_name] = (int) $search_params[$param_name];
289
			// Date values have to match the specified format.
290
			elseif ($param_info['type'] == 'date')
291
			{
292
				// Check if this date format is valid.
293
				if (preg_match('/^\d{4}-\d{1,2}-\d{1,2}$/', $search_params[$param_name]) == 0)
294
					continue;
295
296
				$search_params[$param_name] = strtotime($search_params[$param_name]);
297
			}
298
			elseif ($param_info['type'] == 'inet')
299
			{
300
				$search_params[$param_name] = ip2range($search_params[$param_name]);
301
				if (empty($search_params[$param_name]))
302
					continue;
303
			}
304
305
			// Those values that are in some kind of range (<, <=, =, >=, >).
306
			if (!empty($param_info['range']))
307
			{
308
				// Default to '=', just in case...
309
				if (empty($range_trans[$search_params['types'][$param_name]]))
310
					$search_params['types'][$param_name] = '=';
311
312
				// Handle special case 'age'.
313
				if ($param_info['type'] == 'age')
314
				{
315
					// All people that were born between $lowerlimit and $upperlimit are currently the specified age.
316
					$datearray = getdate(forum_time());
317
					$upperlimit = sprintf('%04d-%02d-%02d', $datearray['year'] - $search_params[$param_name], $datearray['mon'], $datearray['mday']);
318
					$lowerlimit = sprintf('%04d-%02d-%02d', $datearray['year'] - $search_params[$param_name] - 1, $datearray['mon'], $datearray['mday']);
319
					if (in_array($search_params['types'][$param_name], array('-', '--', '=')))
320
					{
321
						$query_parts[] = ($param_info['db_fields'][0]) . ' > {string:' . $param_name . '_minlimit}';
322
						$where_params[$param_name . '_minlimit'] = ($search_params['types'][$param_name] == '--' ? $upperlimit : $lowerlimit);
323
					}
324
					if (in_array($search_params['types'][$param_name], array('+', '++', '=')))
325
					{
326
						$query_parts[] = ($param_info['db_fields'][0]) . ' <= {string:' . $param_name . '_pluslimit}';
327
						$where_params[$param_name . '_pluslimit'] = ($search_params['types'][$param_name] == '++' ? $lowerlimit : $upperlimit);
328
329
						// Make sure that members that didn't set their birth year are not queried.
330
						$query_parts[] = ($param_info['db_fields'][0]) . ' > {date:dec_zero_date}';
331
						$where_params['dec_zero_date'] = '0004-12-31';
332
					}
333
				}
334
				// Special case - equals a date.
335
				elseif ($param_info['type'] == 'date' && $search_params['types'][$param_name] == '=')
336
				{
337
					$query_parts[] = $param_info['db_fields'][0] . ' > ' . $search_params[$param_name] . ' AND ' . $param_info['db_fields'][0] . ' < ' . ($search_params[$param_name] + 86400);
338
				}
339
				else
340
					$query_parts[] = $param_info['db_fields'][0] . ' ' . $range_trans[$search_params['types'][$param_name]] . ' ' . $search_params[$param_name];
341
			}
342
			// Checkboxes.
343
			elseif ($param_info['type'] == 'checkbox')
344
			{
345
				// Each checkbox or no checkbox at all is checked -> ignore.
346
				if (!is_array($search_params[$param_name]) || count($search_params[$param_name]) == 0 || count($search_params[$param_name]) == count($param_info['values']))
347
					continue;
348
349
				$query_parts[] = ($param_info['db_fields'][0]) . ' IN ({array_string:' . $param_name . '_check})';
350
				$where_params[$param_name . '_check'] = $search_params[$param_name];
351
			}
352
			// INET.
353
			elseif ($param_info['type'] == 'inet')
354
			{
355
				if(count($search_params[$param_name]) === 1)
356
				{
357
					$query_parts[] = '(' . $param_info['db_fields'][0] . ' = {inet:' . $param_name . '})';
358
					$where_params[$param_name] = $search_params[$param_name][0];
359
				}
360
				elseif (count($search_params[$param_name]) === 2)
361
				{
362
					$query_parts[] = '(' . $param_info['db_fields'][0] . ' <= {inet:' . $param_name . '_high} and ' . $param_info['db_fields'][0] . ' >= {inet:' . $param_name . '_low})';
363
					$where_params[$param_name.'_low'] = $search_params[$param_name]['low'];
364
					$where_params[$param_name.'_high'] = $search_params[$param_name]['high'];
365
				}
366
				
367
			}
368
			elseif ($param_info['type'] != 'groups')
369
			{
370
				// Replace the wildcard characters ('*' and '?') into MySQL ones.
371
				$parameter = strtolower(strtr($smcFunc['htmlspecialchars']($search_params[$param_name], ENT_QUOTES), array('%' => '\%', '_' => '\_', '*' => '%', '?' => '_')));
372
373
				if ($smcFunc['db_case_sensitive'])
374
					$query_parts[] = '(LOWER(' . implode(') LIKE {string:' . $param_name . '_normal} OR LOWER(', $param_info['db_fields']) . ') LIKE {string:' . $param_name . '_normal})';
375
				else
376
					$query_parts[] = '(' . implode(' LIKE {string:' . $param_name . '_normal} OR ', $param_info['db_fields']) . ' LIKE {string:' . $param_name . '_normal})';
377
				$where_params[$param_name . '_normal'] = '%' . $parameter . '%';
378
			}
379
		}
380
381
		// Set up the membergroup query part.
382
		$mg_query_parts = array();
383
384
		// Primary membergroups, but only if at least was was not selected.
385
		if (!empty($search_params['membergroups'][1]) && count($context['membergroups']) != count($search_params['membergroups'][1]))
386
		{
387
			$mg_query_parts[] = 'mem.id_group IN ({array_int:group_check})';
388
			$where_params['group_check'] = $search_params['membergroups'][1];
389
		}
390
391
		// Additional membergroups (these are only relevant if not all primary groups where selected!).
392
		if (!empty($search_params['membergroups'][2]) && (empty($search_params['membergroups'][1]) || count($context['membergroups']) != count($search_params['membergroups'][1])))
393
			foreach ($search_params['membergroups'][2] as $mg)
394
			{
395
				$mg_query_parts[] = 'FIND_IN_SET({int:add_group_' . $mg . '}, mem.additional_groups) != 0';
396
				$where_params['add_group_' . $mg] = $mg;
397
			}
398
399
		// Combine the one or two membergroup parts into one query part linked with an OR.
400
		if (!empty($mg_query_parts))
401
			$query_parts[] = '(' . implode(' OR ', $mg_query_parts) . ')';
402
403
		// Get all selected post count related membergroups.
404
		if (!empty($search_params['postgroups']) && count($search_params['postgroups']) != count($context['postgroups']))
405
		{
406
			$query_parts[] = 'id_post_group IN ({array_int:post_groups})';
407
			$where_params['post_groups'] = $search_params['postgroups'];
408
		}
409
410
		// Construct the where part of the query.
411
		$where = empty($query_parts) ? '1=1' : implode('
412
			AND ', $query_parts);
413
	}
414
	else
415
		$search_url_params = null;
416
417
	// Construct the additional URL part with the query info in it.
418
	$context['params_url'] = $context['current_subaction'] == 'query' ? ';sa=query;params=' . $search_url_params : '';
419
420
	// Get the title and sub template ready..
421
	$context['page_title'] = $txt['admin_members'];
422
423
	$listOptions = array(
424
		'id' => 'member_list',
425
		'title' => $txt['members_list'],
426
		'items_per_page' => $modSettings['defaultMaxMembers'],
427
		'base_href' => $scripturl . '?action=admin;area=viewmembers' . $context['params_url'],
428
		'default_sort_col' => 'user_name',
429
		'get_items' => array(
430
			'file' => $sourcedir . '/Subs-Members.php',
431
			'function' => 'list_getMembers',
432
			'params' => array(
433
				isset($where) ? $where : '1=1',
434
				isset($where_params) ? $where_params : array(),
435
			),
436
		),
437
		'get_count' => array(
438
			'file' => $sourcedir . '/Subs-Members.php',
439
			'function' => 'list_getNumMembers',
440
			'params' => array(
441
				isset($where) ? $where : '1=1',
442
				isset($where_params) ? $where_params : array(),
443
			),
444
		),
445
		'columns' => array(
446
			'id_member' => array(
447
				'header' => array(
448
					'value' => $txt['member_id'],
449
				),
450
				'data' => array(
451
					'db' => 'id_member',
452
				),
453
				'sort' => array(
454
					'default' => 'id_member',
455
					'reverse' => 'id_member DESC',
456
				),
457
			),
458
			'user_name' => array(
459
				'header' => array(
460
					'value' => $txt['username'],
461
				),
462
				'data' => array(
463
					'sprintf' => array(
464
						'format' => '<a href="' . strtr($scripturl, array('%' => '%%')) . '?action=profile;u=%1$d">%2$s</a>',
465
						'params' => array(
466
							'id_member' => false,
467
							'member_name' => false,
468
						),
469
					),
470
					'class' => 'hidden',
471
				),
472
				'sort' => array(
473
					'default' => 'member_name',
474
					'reverse' => 'member_name DESC',
475
				),
476
			),
477
			'display_name' => array(
478
				'header' => array(
479
					'value' => $txt['display_name'],
480
				),
481
				'data' => array(
482
					'sprintf' => array(
483
						'format' => '<a href="' . strtr($scripturl, array('%' => '%%')) . '?action=profile;u=%1$d">%2$s</a>',
484
						'params' => array(
485
							'id_member' => false,
486
							'real_name' => false,
487
						),
488
					),
489
				),
490
				'sort' => array(
491
					'default' => 'real_name',
492
					'reverse' => 'real_name DESC',
493
				),
494
			),
495
			'email' => array(
496
				'header' => array(
497
					'value' => $txt['email_address'],
498
				),
499
				'data' => array(
500
					'sprintf' => array(
501
						'format' => '<a href="mailto:%1$s">%1$s</a>',
502
						'params' => array(
503
							'email_address' => true,
504
						),
505
					),
506
				),
507
				'sort' => array(
508
					'default' => 'email_address',
509
					'reverse' => 'email_address DESC',
510
				),
511
			),
512
			'ip' => array(
513
				'header' => array(
514
					'value' => $txt['ip_address'],
515
				),
516
				'data' => array(
517
					'sprintf' => array(
518
						'format' => '<a href="' . strtr($scripturl, array('%' => '%%')) . '?action=trackip;searchip=%1$s">%1$s</a>',
519
						'params' => array(
520
							'member_ip' => false,
521
						),
522
					),
523
					'class' => 'hidden',
524
				),
525
				'sort' => array(
526
					'default' => 'member_ip',
527
					'reverse' => 'member_ip DESC',
528
				),
529
			),
530
			'last_active' => array(
531
				'header' => array(
532
					'value' => $txt['viewmembers_online'],
533
				),
534
				'data' => array(
535
					'function' => function($rowData) use ($txt)
536
					{
537
						// Calculate number of days since last online.
538
						if (empty($rowData['last_login']))
539
							$difference = $txt['never'];
540
						else
541
						{
542
							$num_days_difference = jeffsdatediff($rowData['last_login']);
543
544
							// Today.
545
							if (empty($num_days_difference))
546
								$difference = $txt['viewmembers_today'];
547
548
							// Yesterday.
549
							elseif ($num_days_difference == 1)
550
								$difference = sprintf('1 %1$s', $txt['viewmembers_day_ago']);
551
552
							// X days ago.
553
							else
554
								$difference = sprintf('%1$d %2$s', $num_days_difference, $txt['viewmembers_days_ago']);
555
						}
556
557
						// Show it in italics if they're not activated...
558
						if ($rowData['is_activated'] % 10 != 1)
559
							$difference = sprintf('<em title="%1$s">%2$s</em>', $txt['not_activated'], $difference);
560
561
						return $difference;
562
					},
563
					'class' => 'hidden',
564
				),
565
				'sort' => array(
566
					'default' => 'last_login DESC',
567
					'reverse' => 'last_login',
568
				),
569
			),
570
			'posts' => array(
571
				'header' => array(
572
					'value' => $txt['member_postcount'],
573
				),
574
				'data' => array(
575
					'db' => 'posts',
576
					'class' => 'hidden',
577
				),
578
				'sort' => array(
579
					'default' => 'posts',
580
					'reverse' => 'posts DESC',
581
				),
582
			),
583
			'check' => array(
584
				'header' => array(
585
					'value' => '<input type="checkbox" onclick="invertAll(this, this.form);">',
586
					'class' => 'centercol',
587
				),
588
				'data' => array(
589
					'function' => function($rowData) use ($user_info)
590
					{
591
						return '<input type="checkbox" name="delete[]" value="' . $rowData['id_member'] . '"' . ($rowData['id_member'] == $user_info['id'] || $rowData['id_group'] == 1 || in_array(1, explode(',', $rowData['additional_groups'])) ? ' disabled' : '') . '>';
592
					},
593
					'class' => 'centercol',
594
				),
595
			),
596
		),
597
		'form' => array(
598
			'href' => $scripturl . '?action=admin;area=viewmembers' . $context['params_url'],
599
			'include_start' => true,
600
			'include_sort' => true,
601
		),
602
		'additional_rows' => array(
603
			array(
604
				'position' => 'below_table_data',
605
				'value' => '<input type="submit" name="delete_members" value="' . $txt['admin_delete_members'] . '" data-confirm="' . $txt['confirm_delete_members'] . '" class="button you_sure">',
606
			),
607
		),
608
	);
609
610
	// Without enough permissions, don't show 'delete members' checkboxes.
611
	if (!allowedTo('profile_remove_any'))
612
		unset($listOptions['cols']['check'], $listOptions['form'], $listOptions['additional_rows']);
613
614
	require_once($sourcedir . '/Subs-List.php');
615
	createList($listOptions);
616
617
	$context['sub_template'] = 'show_list';
618
	$context['default_list'] = 'member_list';
619
}
620
621
/**
622
 * Search the member list, using one or more criteria.
623
 * Called by ?action=admin;area=viewmembers;sa=search.
624
 * Requires the moderate_forum permission.
625
 * form is submitted to action=admin;area=viewmembers;sa=query.
626
 *
627
 * @uses the search_members sub template of the ManageMembers template.
628
 */
629
function SearchMembers()
630
{
631
	global $context, $txt, $smcFunc;
632
633
	// Get a list of all the membergroups and postgroups that can be selected.
634
	$context['membergroups'] = array(
635
		array(
636
			'id' => 0,
637
			'name' => $txt['membergroups_members'],
638
			'can_be_additional' => false
639
		)
640
	);
641
	$context['postgroups'] = array();
642
643
	$request = $smcFunc['db_query']('', '
644
		SELECT id_group, group_name, min_posts
645
		FROM {db_prefix}membergroups
646
		WHERE id_group != {int:moderator_group}
647
		ORDER BY min_posts, CASE WHEN id_group < {int:newbie_group} THEN id_group ELSE 4 END, group_name',
648
		array(
649
			'moderator_group' => 3,
650
			'newbie_group' => 4,
651
		)
652
	);
653
	while ($row = $smcFunc['db_fetch_assoc']($request))
654
	{
655
		if ($row['min_posts'] == -1)
656
			$context['membergroups'][] = array(
657
				'id' => $row['id_group'],
658
				'name' => $row['group_name'],
659
				'can_be_additional' => true
660
			);
661
		else
662
			$context['postgroups'][] = array(
663
				'id' => $row['id_group'],
664
				'name' => $row['group_name']
665
			);
666
	}
667
	$smcFunc['db_free_result']($request);
668
669
	$context['page_title'] = $txt['admin_members'];
670
	$context['sub_template'] = 'search_members';
671
}
672
673
/**
674
 * List all members who are awaiting approval / activation, sortable on different columns.
675
 * It allows instant approval or activation of (a selection of) members.
676
 * Called by ?action=admin;area=viewmembers;sa=browse;type=approve
677
 *  or ?action=admin;area=viewmembers;sa=browse;type=activate.
678
 * The form submits to ?action=admin;area=viewmembers;sa=approve.
679
 * Requires the moderate_forum permission.
680
 *
681
 * @uses the admin_browse sub template of the ManageMembers template.
682
 */
683
function MembersAwaitingActivation()
684
{
685
	global $txt, $context, $scripturl, $modSettings;
686
	global $sourcedir;
687
688
	// Not a lot here!
689
	$context['page_title'] = $txt['admin_members'];
690
	$context['sub_template'] = 'admin_browse';
691
	$context['browse_type'] = isset($_REQUEST['type']) ? $_REQUEST['type'] : (!empty($modSettings['registration_method']) && $modSettings['registration_method'] == 1 ? 'activate' : 'approve');
692
	if (isset($context['tabs'][$context['browse_type']]))
693
		$context['tabs'][$context['browse_type']]['is_selected'] = true;
694
695
	// Allowed filters are those we can have, in theory.
696
	$context['allowed_filters'] = $context['browse_type'] == 'approve' ? array(3, 4, 5) : array(0, 2);
697
	$context['current_filter'] = isset($_REQUEST['filter']) && in_array($_REQUEST['filter'], $context['allowed_filters']) && !empty($context['activation_numbers'][$_REQUEST['filter']]) ? (int) $_REQUEST['filter'] : -1;
698
699
	// Sort out the different sub areas that we can actually filter by.
700
	$context['available_filters'] = array();
701
	foreach ($context['activation_numbers'] as $type => $amount)
702
	{
703
		// We have some of these...
704
		if (in_array($type, $context['allowed_filters']) && $amount > 0)
705
			$context['available_filters'][] = array(
706
				'type' => $type,
707
				'amount' => $amount,
708
				'desc' => isset($txt['admin_browse_filter_type_' . $type]) ? $txt['admin_browse_filter_type_' . $type] : '?',
709
				'selected' => $type == $context['current_filter']
710
			);
711
	}
712
713
	// If the filter was not sent, set it to whatever has people in it!
714
	if ($context['current_filter'] == -1 && !empty($context['available_filters'][0]['amount']))
715
		$context['current_filter'] = $context['available_filters'][0]['type'];
716
717
	// This little variable is used to determine if we should flag where we are looking.
718
	$context['show_filter'] = ($context['current_filter'] != 0 && $context['current_filter'] != 3) || count($context['available_filters']) > 1;
0 ignored issues
show
introduced by
Consider adding parentheses for clarity. Current Interpretation: $context['show_filter'] ...ailable_filters']) > 1), Probably Intended Meaning: $context['show_filter'] ...ailable_filters']) > 1)
Loading history...
719
720
	// The columns that can be sorted.
721
	$context['columns'] = array(
722
		'id_member' => array('label' => $txt['admin_browse_id']),
723
		'member_name' => array('label' => $txt['admin_browse_username']),
724
		'email_address' => array('label' => $txt['admin_browse_email']),
725
		'member_ip' => array('label' => $txt['admin_browse_ip']),
726
		'date_registered' => array('label' => $txt['admin_browse_registered']),
727
	);
728
729
	// Are we showing duplicate information?
730
	if (isset($_GET['showdupes']))
731
		$_SESSION['showdupes'] = (int) $_GET['showdupes'];
732
	$context['show_duplicates'] = !empty($_SESSION['showdupes']);
733
734
	// Determine which actions we should allow on this page.
735
	if ($context['browse_type'] == 'approve')
736
	{
737
		// If we are approving deleted accounts we have a slightly different list... actually a mirror ;)
738
		if ($context['current_filter'] == 4)
739
			$context['allowed_actions'] = array(
740
				'reject' => $txt['admin_browse_w_approve_deletion'],
741
				'ok' => $txt['admin_browse_w_reject'],
742
			);
743
		else
744
			$context['allowed_actions'] = array(
745
				'ok' => $txt['admin_browse_w_approve'],
746
				'okemail' => $txt['admin_browse_w_approve'] . ' ' . $txt['admin_browse_w_email'],
747
				'require_activation' => $txt['admin_browse_w_approve_require_activate'],
748
				'reject' => $txt['admin_browse_w_reject'],
749
				'rejectemail' => $txt['admin_browse_w_reject'] . ' ' . $txt['admin_browse_w_email'],
750
			);
751
	}
752
	elseif ($context['browse_type'] == 'activate')
753
		$context['allowed_actions'] = array(
754
			'ok' => $txt['admin_browse_w_activate'],
755
			'okemail' => $txt['admin_browse_w_activate'] . ' ' . $txt['admin_browse_w_email'],
756
			'delete' => $txt['admin_browse_w_delete'],
757
			'deleteemail' => $txt['admin_browse_w_delete'] . ' ' . $txt['admin_browse_w_email'],
758
			'remind' => $txt['admin_browse_w_remind'] . ' ' . $txt['admin_browse_w_email'],
759
		);
760
761
	// Create an option list for actions allowed to be done with selected members.
762
	$allowed_actions = '
763
			<option selected value="">' . $txt['admin_browse_with_selected'] . ':</option>
764
			<option value="" disabled>-----------------------------</option>';
765
	foreach ($context['allowed_actions'] as $key => $desc)
766
		$allowed_actions .= '
767
			<option value="' . $key . '">' . $desc . '</option>';
768
769
	// Setup the Javascript function for selecting an action for the list.
770
	$javascript = '
771
		function onSelectChange()
772
		{
773
			if (document.forms.postForm.todo.value == "")
774
				return;
775
776
			var message = "";';
777
778
	// We have special messages for approving deletion of accounts - it's surprisingly logical - honest.
779
	if ($context['current_filter'] == 4)
780
		$javascript .= '
781
			if (document.forms.postForm.todo.value.indexOf("reject") != -1)
782
				message = "' . $txt['admin_browse_w_delete'] . '";
783
			else
784
				message = "' . $txt['admin_browse_w_reject'] . '";';
785
	// Otherwise a nice standard message.
786
	else
787
		$javascript .= '
788
			if (document.forms.postForm.todo.value.indexOf("delete") != -1)
789
				message = "' . $txt['admin_browse_w_delete'] . '";
790
			else if (document.forms.postForm.todo.value.indexOf("reject") != -1)
791
				message = "' . $txt['admin_browse_w_reject'] . '";
792
			else if (document.forms.postForm.todo.value == "remind")
793
				message = "' . $txt['admin_browse_w_remind'] . '";
794
			else
795
				message = "' . ($context['browse_type'] == 'approve' ? $txt['admin_browse_w_approve'] : $txt['admin_browse_w_activate']) . '";';
796
	$javascript .= '
797
			if (confirm(message + " ' . $txt['admin_browse_warn'] . '"))
798
				document.forms.postForm.submit();
799
		}';
800
801
	$listOptions = array(
802
		'id' => 'approve_list',
803
// 		'title' => $txt['members_approval_title'],
804
		'items_per_page' => $modSettings['defaultMaxMembers'],
805
		'base_href' => $scripturl . '?action=admin;area=viewmembers;sa=browse;type=' . $context['browse_type'] . (!empty($context['show_filter']) ? ';filter=' . $context['current_filter'] : ''),
806
		'default_sort_col' => 'date_registered',
807
		'get_items' => array(
808
			'file' => $sourcedir . '/Subs-Members.php',
809
			'function' => 'list_getMembers',
810
			'params' => array(
811
				'is_activated = {int:activated_status}',
812
				array('activated_status' => $context['current_filter']),
813
				$context['show_duplicates'],
814
			),
815
		),
816
		'get_count' => array(
817
			'file' => $sourcedir . '/Subs-Members.php',
818
			'function' => 'list_getNumMembers',
819
			'params' => array(
820
				'is_activated = {int:activated_status}',
821
				array('activated_status' => $context['current_filter']),
822
			),
823
		),
824
		'columns' => array(
825
			'id_member' => array(
826
				'header' => array(
827
					'value' => $txt['member_id'],
828
				),
829
				'data' => array(
830
					'db' => 'id_member',
831
				),
832
				'sort' => array(
833
					'default' => 'id_member',
834
					'reverse' => 'id_member DESC',
835
				),
836
			),
837
			'user_name' => array(
838
				'header' => array(
839
					'value' => $txt['username'],
840
				),
841
				'data' => array(
842
					'sprintf' => array(
843
						'format' => '<a href="' . strtr($scripturl, array('%' => '%%')) . '?action=profile;u=%1$d">%2$s</a>',
844
						'params' => array(
845
							'id_member' => false,
846
							'member_name' => false,
847
						),
848
					),
849
				),
850
				'sort' => array(
851
					'default' => 'member_name',
852
					'reverse' => 'member_name DESC',
853
				),
854
			),
855
			'email' => array(
856
				'header' => array(
857
					'value' => $txt['email_address'],
858
				),
859
				'data' => array(
860
					'sprintf' => array(
861
						'format' => '<a href="mailto:%1$s">%1$s</a>',
862
						'params' => array(
863
							'email_address' => true,
864
						),
865
					),
866
				),
867
				'sort' => array(
868
					'default' => 'email_address',
869
					'reverse' => 'email_address DESC',
870
				),
871
			),
872
			'ip' => array(
873
				'header' => array(
874
					'value' => $txt['ip_address'],
875
				),
876
				'data' => array(
877
					'sprintf' => array(
878
						'format' => '<a href="' . strtr($scripturl, array('%' => '%%')) . '?action=trackip;searchip=%1$s">%1$s</a>',
879
						'params' => array(
880
							'member_ip' => false,
881
						),
882
					),
883
				),
884
				'sort' => array(
885
					'default' => 'member_ip',
886
					'reverse' => 'member_ip DESC',
887
				),
888
			),
889
			'hostname' => array(
890
				'header' => array(
891
					'value' => $txt['hostname'],
892
				),
893
				'data' => array(
894
					'function' => function($rowData)
895
					{
896
						return host_from_ip(inet_dtop($rowData['member_ip']));
0 ignored issues
show
Bug introduced by
It seems like inet_dtop($rowData['member_ip']) can also be of type false; however, parameter $ip of host_from_ip() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

896
						return host_from_ip(/** @scrutinizer ignore-type */ inet_dtop($rowData['member_ip']));
Loading history...
897
					},
898
					'class' => 'smalltext',
899
				),
900
			),
901
			'date_registered' => array(
902
				'header' => array(
903
					'value' => $context['current_filter'] == 4 ? $txt['viewmembers_online'] : $txt['date_registered'],
904
				),
905
				'data' => array(
906
					'function' => function($rowData) use ($context)
907
					{
908
						return timeformat($rowData['' . ($context['current_filter'] == 4 ? 'last_login' : 'date_registered') . '']);
909
					},
910
				),
911
				'sort' => array(
912
					'default' => $context['current_filter'] == 4 ? 'mem.last_login DESC' : 'date_registered DESC',
913
					'reverse' => $context['current_filter'] == 4 ? 'mem.last_login' : 'date_registered',
914
				),
915
			),
916
			'duplicates' => array(
917
				'header' => array(
918
					'value' => $txt['duplicates'],
919
					// Make sure it doesn't go too wide.
920
					'style' => 'width: 20%;',
921
				),
922
				'data' => array(
923
					'function' => function($rowData) use ($scripturl, $txt)
924
					{
925
						$member_links = array();
926
						foreach ($rowData['duplicate_members'] as $member)
927
						{
928
							if ($member['id'])
929
								$member_links[] = '<a href="' . $scripturl . '?action=profile;u=' . $member['id'] . '" ' . (!empty($member['is_banned']) ? 'class="red"' : '') . '>' . $member['name'] . '</a>';
930
							else
931
								$member_links[] = $member['name'] . ' (' . $txt['guest'] . ')';
932
						}
933
						return implode(', ', $member_links);
934
					},
935
					'class' => 'smalltext',
936
				),
937
			),
938
			'check' => array(
939
				'header' => array(
940
					'value' => '<input type="checkbox" onclick="invertAll(this, this.form);">',
941
					'class' => 'centercol',
942
				),
943
				'data' => array(
944
					'sprintf' => array(
945
						'format' => '<input type="checkbox" name="todoAction[]" value="%1$d">',
946
						'params' => array(
947
							'id_member' => false,
948
						),
949
					),
950
					'class' => 'centercol',
951
				),
952
			),
953
		),
954
		'javascript' => $javascript,
955
		'form' => array(
956
			'href' => $scripturl . '?action=admin;area=viewmembers;sa=approve;type=' . $context['browse_type'],
957
			'name' => 'postForm',
958
			'include_start' => true,
959
			'include_sort' => true,
960
			'hidden_fields' => array(
961
				'orig_filter' => $context['current_filter'],
962
			),
963
		),
964
		'additional_rows' => array(
965
			array(
966
				'position' => 'below_table_data',
967
				'value' => '
968
					[<a href="' . $scripturl . '?action=admin;area=viewmembers;sa=browse;showdupes=' . ($context['show_duplicates'] ? 0 : 1) . ';type=' . $context['browse_type'] . (!empty($context['show_filter']) ? ';filter=' . $context['current_filter'] : '') . ';' . $context['session_var'] . '=' . $context['session_id'] . '">' . ($context['show_duplicates'] ? $txt['dont_check_for_duplicate'] : $txt['check_for_duplicate']) . '</a>]
969
					<select name="todo" onchange="onSelectChange();">
970
						' . $allowed_actions . '
971
					</select>
972
					<noscript><input type="submit" value="' . $txt['go'] . '" class="button"><br class="clear_right"></noscript>
973
				',
974
				'class' => 'floatright',
975
			),
976
		),
977
	);
978
979
	// Pick what column to actually include if we're showing duplicates.
980
	if ($context['show_duplicates'])
981
		unset($listOptions['columns']['email']);
982
	else
983
		unset($listOptions['columns']['duplicates']);
984
985
	// Only show hostname on duplicates as it takes a lot of time.
986
	if (!$context['show_duplicates'] || !empty($modSettings['disableHostnameLookup']))
987
		unset($listOptions['columns']['hostname']);
988
989
	// Is there any need to show filters?
990
	if (isset($context['available_filters']) && count($context['available_filters']) > 1)
991
	{
992
		$filterOptions = '
993
			<strong>' . $txt['admin_browse_filter_by'] . ':</strong>
994
			<select name="filter" onchange="this.form.submit();">';
995
		foreach ($context['available_filters'] as $filter)
996
			$filterOptions .= '
997
				<option value="' . $filter['type'] . '"' . ($filter['selected'] ? ' selected' : '') . '>' . $filter['desc'] . ' - ' . $filter['amount'] . ' ' . ($filter['amount'] == 1 ? $txt['user'] : $txt['users']) . '</option>';
998
		$filterOptions .= '
999
			</select>
1000
			<noscript><input type="submit" value="' . $txt['go'] . '" name="filter" class="button"></noscript>';
1001
		$listOptions['additional_rows'][] = array(
1002
			'position' => 'top_of_list',
1003
			'value' => $filterOptions,
1004
			'class' => 'righttext',
1005
		);
1006
	}
1007
1008
	// What about if we only have one filter, but it's not the "standard" filter - show them what they are looking at.
1009
	if (!empty($context['show_filter']) && !empty($context['available_filters']))
1010
		$listOptions['additional_rows'][] = array(
1011
			'position' => 'above_column_headers',
1012
			'value' => '<strong>' . $txt['admin_browse_filter_show'] . ':</strong> ' . ((isset($context['current_filter']) && isset($txt['admin_browse_filter_type_'.$context['current_filter']])) ? $txt['admin_browse_filter_type_'.$context['current_filter']] : $context['available_filters'][0]['desc']),
1013
			'class' => 'filter_row generic_list_wrapper smalltext',
1014
		);
1015
1016
	// Now that we have all the options, create the list.
1017
	require_once($sourcedir . '/Subs-List.php');
1018
	createList($listOptions);
1019
}
1020
1021
/**
1022
 * This function handles the approval, rejection, activation or deletion of members.
1023
 * Called by ?action=admin;area=viewmembers;sa=approve.
1024
 * Requires the moderate_forum permission.
1025
 * Redirects to ?action=admin;area=viewmembers;sa=browse
1026
 * with the same parameters as the calling page.
1027
 */
1028
function AdminApprove()
1029
{
1030
	global $scripturl, $modSettings, $sourcedir, $language, $user_info, $smcFunc;
1031
1032
	// First, check our session.
1033
	checkSession();
1034
1035
	require_once($sourcedir . '/Subs-Post.php');
1036
1037
	// We also need to the login languages here - for emails.
1038
	loadLanguage('Login');
1039
1040
	// Sort out where we are going...
1041
	$current_filter = (int) $_REQUEST['orig_filter'];
1042
1043
	// If we are applying a filter do just that - then redirect.
1044
	if (isset($_REQUEST['filter']) && $_REQUEST['filter'] != $_REQUEST['orig_filter'])
1045
		redirectexit('action=admin;area=viewmembers;sa=browse;type=' . $_REQUEST['type'] . ';sort=' . $_REQUEST['sort'] . ';filter=' . $_REQUEST['filter'] . ';start=' . $_REQUEST['start']);
1046
1047
	// Nothing to do?
1048
	if (!isset($_POST['todoAction']) && !isset($_POST['time_passed']))
1049
		redirectexit('action=admin;area=viewmembers;sa=browse;type=' . $_REQUEST['type'] . ';sort=' . $_REQUEST['sort'] . ';filter=' . $current_filter . ';start=' . $_REQUEST['start']);
1050
1051
	// Are we dealing with members who have been waiting for > set amount of time?
1052
	if (isset($_POST['time_passed']))
1053
	{
1054
		$timeBefore = time() - 86400 * (int) $_POST['time_passed'];
1055
		$condition = '
1056
			AND date_registered < {int:time_before}';
1057
	}
1058
	// Coming from checkboxes - validate the members passed through to us.
1059
	else
1060
	{
1061
		$members = array();
1062
		foreach ($_POST['todoAction'] as $id)
1063
			$members[] = (int) $id;
1064
		$condition = '
1065
			AND id_member IN ({array_int:members})';
1066
	}
1067
1068
	// Get information on each of the members, things that are important to us, like email address...
1069
	$request = $smcFunc['db_query']('', '
1070
		SELECT id_member, member_name, real_name, email_address, validation_code, lngfile
1071
		FROM {db_prefix}members
1072
		WHERE is_activated = {int:activated_status}' . $condition . '
1073
		ORDER BY lngfile',
1074
		array(
1075
			'activated_status' => $current_filter,
1076
			'time_before' => empty($timeBefore) ? 0 : $timeBefore,
1077
			'members' => empty($members) ? array() : $members,
1078
		)
1079
	);
1080
1081
	$member_count = $smcFunc['db_num_rows']($request);
1082
1083
	// If no results then just return!
1084
	if ($member_count == 0)
1085
		redirectexit('action=admin;area=viewmembers;sa=browse;type=' . $_REQUEST['type'] . ';sort=' . $_REQUEST['sort'] . ';filter=' . $current_filter . ';start=' . $_REQUEST['start']);
1086
1087
	$member_info = array();
1088
	$members = array();
1089
	// Fill the info array.
1090
	while ($row = $smcFunc['db_fetch_assoc']($request))
1091
	{
1092
		$members[] = $row['id_member'];
1093
		$member_info[] = array(
1094
			'id' => $row['id_member'],
1095
			'username' => $row['member_name'],
1096
			'name' => $row['real_name'],
1097
			'email' => $row['email_address'],
1098
			'language' => empty($row['lngfile']) || empty($modSettings['userLanguage']) ? $language : $row['lngfile'],
1099
			'code' => $row['validation_code']
1100
		);
1101
	}
1102
	$smcFunc['db_free_result']($request);
1103
1104
	// Are we activating or approving the members?
1105
	if ($_POST['todo'] == 'ok' || $_POST['todo'] == 'okemail')
1106
	{
1107
		// Approve/activate this member.
1108
		$smcFunc['db_query']('', '
1109
			UPDATE {db_prefix}members
1110
			SET validation_code = {string:blank_string}, is_activated = {int:is_activated}
1111
			WHERE is_activated = {int:activated_status}' . $condition,
1112
			array(
1113
				'is_activated' => 1,
1114
				'time_before' => empty($timeBefore) ? 0 : $timeBefore,
1115
				'members' => empty($members) ? array() : $members,
1116
				'activated_status' => $current_filter,
1117
				'blank_string' => '',
1118
			)
1119
		);
1120
1121
		// Do we have to let the integration code know about the activations?
1122
		if (!empty($modSettings['integrate_activate']))
1123
		{
1124
			foreach ($member_info as $member)
1125
				call_integration_hook('integrate_activate', array($member['username']));
1126
		}
1127
1128
		// Check for email.
1129
		if ($_POST['todo'] == 'okemail')
1130
		{
1131
			foreach ($member_info as $member)
1132
			{
1133
				$replacements = array(
1134
					'NAME' => $member['name'],
1135
					'USERNAME' => $member['username'],
1136
					'PROFILELINK' => $scripturl . '?action=profile;u=' . $member['id'],
1137
					'FORGOTPASSWORDLINK' => $scripturl . '?action=reminder',
1138
				);
1139
1140
				$emaildata = loadEmailTemplate('admin_approve_accept', $replacements, $member['language']);
1141
				sendmail($member['email'], $emaildata['subject'], $emaildata['body'], null, 'accapp' . $member['id'], $emaildata['is_html'], 0);
1142
			}
1143
		}
1144
	}
1145
	// Maybe we're sending it off for activation?
1146
	elseif ($_POST['todo'] == 'require_activation')
1147
	{
1148
		require_once($sourcedir . '/Subs-Members.php');
1149
1150
		// We have to do this for each member I'm afraid.
1151
		foreach ($member_info as $member)
1152
		{
1153
			// Generate a random activation code.
1154
			$validation_code = generateValidationCode();
1155
1156
			// Set these members for activation - I know this includes two id_member checks but it's safer than bodging $condition ;).
1157
			$smcFunc['db_query']('', '
1158
				UPDATE {db_prefix}members
1159
				SET validation_code = {string:validation_code}, is_activated = {int:not_activated}
1160
				WHERE is_activated = {int:activated_status}
1161
					' . $condition . '
1162
					AND id_member = {int:selected_member}',
1163
				array(
1164
					'not_activated' => 0,
1165
					'activated_status' => $current_filter,
1166
					'selected_member' => $member['id'],
1167
					'validation_code' => $validation_code,
1168
					'time_before' => empty($timeBefore) ? 0 : $timeBefore,
1169
					'members' => empty($members) ? array() : $members,
1170
				)
1171
			);
1172
1173
			$replacements = array(
1174
				'USERNAME' => $member['name'],
1175
				'ACTIVATIONLINK' => $scripturl . '?action=activate;u=' . $member['id'] . ';code=' . $validation_code,
1176
				'ACTIVATIONLINKWITHOUTCODE' => $scripturl . '?action=activate;u=' . $member['id'],
1177
				'ACTIVATIONCODE' => $validation_code,
1178
			);
1179
1180
			$emaildata = loadEmailTemplate('admin_approve_activation', $replacements, $member['language']);
1181
			sendmail($member['email'], $emaildata['subject'], $emaildata['body'], null, 'accact' . $member['id'], $emaildata['is_html'], 0);
1182
		}
1183
	}
1184
	// Are we rejecting them?
1185
	elseif ($_POST['todo'] == 'reject' || $_POST['todo'] == 'rejectemail')
1186
	{
1187
		require_once($sourcedir . '/Subs-Members.php');
1188
		deleteMembers($members);
1189
1190
		// Send email telling them they aren't welcome?
1191
		if ($_POST['todo'] == 'rejectemail')
1192
		{
1193
			foreach ($member_info as $member)
1194
			{
1195
				$replacements = array(
1196
					'USERNAME' => $member['name'],
1197
				);
1198
1199
				$emaildata = loadEmailTemplate('admin_approve_reject', $replacements, $member['language']);
1200
				sendmail($member['email'], $emaildata['subject'], $emaildata['body'], null, 'accrej', $emaildata['is_html'], 1);
1201
			}
1202
		}
1203
	}
1204
	// A simple delete?
1205
	elseif ($_POST['todo'] == 'delete' || $_POST['todo'] == 'deleteemail')
1206
	{
1207
		require_once($sourcedir . '/Subs-Members.php');
1208
		deleteMembers($members);
1209
1210
		// Send email telling them they aren't welcome?
1211
		if ($_POST['todo'] == 'deleteemail')
1212
		{
1213
			foreach ($member_info as $member)
1214
			{
1215
				$replacements = array(
1216
					'USERNAME' => $member['name'],
1217
				);
1218
1219
				$emaildata = loadEmailTemplate('admin_approve_delete', $replacements, $member['language']);
1220
				sendmail($member['email'], $emaildata['subject'], $emaildata['body'], null, 'accdel', $emaildata['is_html'], 1);
1221
			}
1222
		}
1223
	}
1224
	// Remind them to activate their account?
1225
	elseif ($_POST['todo'] == 'remind')
1226
	{
1227
		foreach ($member_info as $member)
1228
		{
1229
			$replacements = array(
1230
				'USERNAME' => $member['name'],
1231
				'ACTIVATIONLINK' => $scripturl . '?action=activate;u=' . $member['id'] . ';code=' . $member['code'],
1232
				'ACTIVATIONLINKWITHOUTCODE' => $scripturl . '?action=activate;u=' . $member['id'],
1233
				'ACTIVATIONCODE' => $member['code'],
1234
			);
1235
1236
			$emaildata = loadEmailTemplate('admin_approve_remind', $replacements, $member['language']);
1237
			sendmail($member['email'], $emaildata['subject'], $emaildata['body'], null, 'accrem' . $member['id'], $emaildata['is_html'], 1);
1238
		}
1239
	}
1240
1241
	// @todo current_language is never set, no idea what this is for. Remove?
1242
	// Back to the user's language!
1243
	if (isset($current_language) && $current_language != $user_info['language'])
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $current_language seems to never exist and therefore isset should always be false.
Loading history...
1244
	{
1245
		loadLanguage('index');
1246
		loadLanguage('ManageMembers');
1247
	}
1248
1249
	// Log what we did?
1250
	if (!empty($modSettings['modlog_enabled']) && in_array($_POST['todo'], array('ok', 'okemail', 'require_activation', 'remind')))
1251
	{
1252
		$log_action = $_POST['todo'] == 'remind' ? 'remind_member' : 'approve_member';
1253
1254
		require_once($sourcedir . '/Logging.php');
1255
		foreach ($member_info as $member)
1256
			logAction($log_action, array('member' => $member['id']), 'admin');
1257
	}
1258
1259
	// Although updateStats *may* catch this, best to do it manually just in case (Doesn't always sort out unapprovedMembers).
1260
	if (in_array($current_filter, array(3, 4, 5)))
1261
		updateSettings(array('unapprovedMembers' => ($modSettings['unapprovedMembers'] > $member_count ? $modSettings['unapprovedMembers'] - $member_count : 0)));
1262
1263
	// Update the member's stats. (but, we know the member didn't change their name.)
1264
	updateStats('member', false);
1265
1266
	// If they haven't been deleted, update the post group statistics on them...
1267
	if (!in_array($_POST['todo'], array('delete', 'deleteemail', 'reject', 'rejectemail', 'remind')))
1268
		updateStats('postgroups', $members);
1269
1270
	redirectexit('action=admin;area=viewmembers;sa=browse;type=' . $_REQUEST['type'] . ';sort=' . $_REQUEST['sort'] . ';filter=' . $current_filter . ';start=' . $_REQUEST['start']);
1271
}
1272
1273
/**
1274
 * Nifty function to calculate the number of days ago a given date was.
1275
 * Requires a unix timestamp as input, returns an integer.
1276
 * Named in honour of Jeff Lewis, the original creator of...this function.
1277
 *
1278
 * @param int $old The timestamp of the old date
1279
 * @return int The number of days since $old, based on the forum time
1280
 */
1281
function jeffsdatediff($old)
1282
{
1283
	// Get the current time as the user would see it...
1284
	$forumTime = forum_time();
1285
1286
	// Calculate the seconds that have passed since midnight.
1287
	$sinceMidnight = date('H', $forumTime) * 60 * 60 + date('i', $forumTime) * 60 + date('s', $forumTime);
1288
1289
	// Take the difference between the two times.
1290
	$dis = time() - $old;
1291
1292
	// Before midnight?
1293
	if ($dis < $sinceMidnight)
1294
		return 0;
1295
	else
1296
		$dis -= $sinceMidnight;
1297
1298
	// Divide out the seconds in a day to get the number of days.
1299
	return ceil($dis / (24 * 60 * 60));
1300
}
1301
1302
/**
1303
 * Fetches all the activation counts for ViewMembers.
1304
 *
1305
 */
1306
function GetMemberActivationCounts()
1307
{
1308
	global $smcFunc, $context;
1309
1310
	// Get counts on every type of activation - for sections and filtering alike.
1311
	$request = $smcFunc['db_query']('', '
1312
		SELECT COUNT(*) AS total_members, is_activated
1313
		FROM {db_prefix}members
1314
		WHERE is_activated != {int:is_activated}
1315
		GROUP BY is_activated',
1316
		array(
1317
			'is_activated' => 1,
1318
		)
1319
	);
1320
	$context['activation_numbers'] = array();
1321
	$context['awaiting_activation'] = 0;
1322
	$context['awaiting_approval'] = 0;
1323
	while ($row = $smcFunc['db_fetch_assoc']($request))
1324
		$context['activation_numbers'][$row['is_activated']] = $row['total_members'];
1325
	$smcFunc['db_free_result']($request);
1326
1327
	foreach ($context['activation_numbers'] as $activation_type => $total_members)
1328
	{
1329
		if (in_array($activation_type, array(0, 2)))
1330
			$context['awaiting_activation'] += $total_members;
1331
		elseif (in_array($activation_type, array(3, 4, 5)))
1332
			$context['awaiting_approval'] += $total_members;
1333
	}
1334
1335
}
1336
1337
?>