Completed
Pull Request — release-2.1 (#4995)
by Jeremy
06:17
created

ManageMembers.php ➔ GetMemberActivationCounts()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 8
nop 0
dl 0
loc 30
rs 9.1288
c 0
b 0
f 0
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
	// Default to sub action 'index' or 'settings' depending on permissions.
41
	$_REQUEST['sa'] = isset($_REQUEST['sa']) && isset($subActions[$_REQUEST['sa']]) ? $_REQUEST['sa'] : 'all';
42
43
	// Load the essentials.
44
	loadLanguage('ManageMembers');
45
	loadTemplate('ManageMembers');
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']);
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
			'is_selected' => $_REQUEST['sa'] == '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
			'is_selected' => $_REQUEST['sa'] == 'search' || $_REQUEST['sa'] == 'query',
73
		),
74
	);
75
	$context['last_tab'] = 'search';
76
77
	// Fetch our activation counts.
78
	GetMemberActivationCounts();
79
80
	// Do we have approvals
81
	if ($context['show_approve'])
82
	{
83
		$context['tabs']['approve'] = array(
84
			'label' => sprintf($txt['admin_browse_awaiting_approval'], $context['awaiting_approval']),
85
			'description' => $txt['admin_browse_approve_desc'],
86
			'url' => $scripturl . '?action=admin;area=viewmembers;sa=browse;type=approve',
87
			'is_selected' => false,
88
		);
89
		$context['last_tab'] = 'approve';
90
	}
91
92
	// Do we have activations to show?
93
	if ($context['show_activate'])
94
	{
95
		$context['tabs']['activate'] = array(
96
			'label' => sprintf($txt['admin_browse_awaiting_activate'], $context['awaiting_activation']),
97
			'description' => $txt['admin_browse_activate_desc'],
98
			'url' => $scripturl . '?action=admin;area=viewmembers;sa=browse;type=activate',
99
			'is_selected' => false,
100
		);
101
		$context['last_tab'] = 'activate';
102
	}
103
104
	// Call our hook now, letting customizations add to the subActions and/or modify $context as needed.
105
	call_integration_hook('integrate_manage_members', array(&$subActions));
106
107
	// We know the sub action, now we know what you're allowed to do.
108
	isAllowedTo($subActions[$_REQUEST['sa']][1]);
109
110
	// Set the last tab.
111
	$context['tabs'][$context['last_tab']]['is_last'] = true;
112
113
	call_helper($subActions[$_REQUEST['sa']][0]);
114
}
115
116
/**
117
 * View all members list. It allows sorting on several columns, and deletion of
118
 * selected members. It also handles the search query sent by
119
 * ?action=admin;area=viewmembers;sa=search.
120
 * Called by ?action=admin;area=viewmembers;sa=all or ?action=admin;area=viewmembers;sa=query.
121
 * Requires the moderate_forum permission.
122
 *
123
 * @uses the view_members sub template of the ManageMembers template.
124
 */
125
function ViewMemberlist()
126
{
127
	global $txt, $scripturl, $context, $modSettings, $sourcedir, $smcFunc, $user_info;
128
129
	// Set the current sub action.
130
	$context['sub_action'] = $_REQUEST['sa'];
131
132
	// Are we performing a delete?
133
	if (isset($_POST['delete_members']) && !empty($_POST['delete']) && allowedTo('profile_remove_any'))
134
	{
135
		checkSession();
136
137
		// Clean the input.
138
		foreach ($_POST['delete'] as $key => $value)
139
		{
140
			// Don't delete yourself, idiot.
141
			if ($value != $user_info['id'])
142
				$delete[$key] = (int) $value;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$delete was never initialized. Although not strictly required by PHP, it is generally a good practice to add $delete = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
143
		}
144
145
		if (!empty($delete))
146
		{
147
			// Delete all the selected members.
148
			require_once($sourcedir . '/Subs-Members.php');
149
			deleteMembers($delete, true);
150
		}
151
	}
152
153
	// Check input after a member search has been submitted.
154
	if ($context['sub_action'] == 'query')
155
	{
156
		// Retrieving the membergroups and postgroups.
157
		$context['membergroups'] = array(
158
			array(
159
				'id' => 0,
160
				'name' => $txt['membergroups_members'],
161
				'can_be_additional' => false
162
			)
163
		);
164
		$context['postgroups'] = array();
165
166
		$request = $smcFunc['db_query']('', '
167
			SELECT id_group, group_name, min_posts
168
			FROM {db_prefix}membergroups
169
			WHERE id_group != {int:moderator_group}
170
			ORDER BY min_posts, CASE WHEN id_group < {int:newbie_group} THEN id_group ELSE 4 END, group_name',
171
			array(
172
				'moderator_group' => 3,
173
				'newbie_group' => 4,
174
			)
175
		);
176 View Code Duplication
		while ($row = $smcFunc['db_fetch_assoc']($request))
177
		{
178
			if ($row['min_posts'] == -1)
179
				$context['membergroups'][] = array(
180
					'id' => $row['id_group'],
181
					'name' => $row['group_name'],
182
					'can_be_additional' => true
183
				);
184
			else
185
				$context['postgroups'][] = array(
186
					'id' => $row['id_group'],
187
					'name' => $row['group_name']
188
				);
189
		}
190
		$smcFunc['db_free_result']($request);
191
192
		// Some data about the form fields and how they are linked to the database.
193
		$params = array(
194
			'mem_id' => array(
195
				'db_fields' => array('id_member'),
196
				'type' => 'int',
197
				'range' => true
198
			),
199
			'age' => array(
200
				'db_fields' => array('birthdate'),
201
				'type' => 'age',
202
				'range' => true
203
			),
204
			'posts' => array(
205
				'db_fields' => array('posts'),
206
				'type' => 'int',
207
				'range' => true
208
			),
209
			'reg_date' => array(
210
				'db_fields' => array('date_registered'),
211
				'type' => 'date',
212
				'range' => true
213
			),
214
			'last_online' => array(
215
				'db_fields' => array('last_login'),
216
				'type' => 'date',
217
				'range' => true
218
			),
219
			'activated' => array(
220
				'db_fields' => array('CASE WHEN is_activated IN (1, 11) THEN 1 ELSE 0 END'),
221
				'type' => 'checkbox',
222
				'values' => array('0', '1'),
223
			),
224
			'membername' => array(
225
				'db_fields' => array('member_name', 'real_name'),
226
				'type' => 'string'
227
			),
228
			'email' => array(
229
				'db_fields' => array('email_address'),
230
				'type' => 'string'
231
			),
232
			'website' => array(
233
				'db_fields' => array('website_title', 'website_url'),
234
				'type' => 'string'
235
			),
236
			'ip' => array(
237
				'db_fields' => array('member_ip'),
238
				'type' => 'inet'
239
			),
240
			'membergroups' => array(
241
				'db_fields' => array('id_group'),
242
				'type' => 'groups'
243
			),
244
			'postgroups' => array(
245
				'db_fields' => array('id_group'),
246
				'type' => 'groups'
247
			)
248
		);
249
		$range_trans = array(
250
			'--' => '<',
251
			'-' => '<=',
252
			'=' => '=',
253
			'+' => '>=',
254
			'++' => '>'
255
		);
256
257
		call_integration_hook('integrate_view_members_params', array(&$params));
258
259
		$search_params = array();
260
		if ($context['sub_action'] == 'query' && !empty($_REQUEST['params']) && empty($_POST['types']))
261
			$search_params = $smcFunc['json_decode'](base64_decode($_REQUEST['params']), true);
262
		elseif (!empty($_POST))
263
		{
264
			$search_params['types'] = $_POST['types'];
265
			foreach ($params as $param_name => $param_info)
266
				if (isset($_POST[$param_name]))
267
					$search_params[$param_name] = $_POST[$param_name];
268
		}
269
270
		$search_url_params = isset($search_params) ? base64_encode($smcFunc['json_encode']($search_params)) : null;
271
272
		// @todo Validate a little more.
273
274
		// Loop through every field of the form.
275
		$query_parts = array();
276
		$where_params = array();
277
		foreach ($params as $param_name => $param_info)
278
		{
279
			// Not filled in?
280
			if (!isset($search_params[$param_name]) || $search_params[$param_name] === '')
281
				continue;
282
283
			// Make sure numeric values are really numeric.
284
			if (in_array($param_info['type'], array('int', 'age')))
285
				$search_params[$param_name] = (int) $search_params[$param_name];
286
			// Date values have to match the specified format.
287 View Code Duplication
			elseif ($param_info['type'] == 'date')
288
			{
289
				// Check if this date format is valid.
290
				if (preg_match('/^\d{4}-\d{1,2}-\d{1,2}$/', $search_params[$param_name]) == 0)
291
					continue;
292
293
				$search_params[$param_name] = strtotime($search_params[$param_name]);
294
			}
295 View Code Duplication
			elseif ($param_info['type'] == 'inet')
296
			{
297
				$search_params[$param_name] = ip2range($search_params[$param_name]);
298
				if (empty($search_params[$param_name]))
299
					continue;
300
			}
301
302
			// Those values that are in some kind of range (<, <=, =, >=, >).
303
			if (!empty($param_info['range']))
304
			{
305
				// Default to '=', just in case...
306
				if (empty($range_trans[$search_params['types'][$param_name]]))
307
					$search_params['types'][$param_name] = '=';
308
309
				// Handle special case 'age'.
310
				if ($param_info['type'] == 'age')
311
				{
312
					// All people that were born between $lowerlimit and $upperlimit are currently the specified age.
313
					$datearray = getdate(forum_time());
314
					$upperlimit = sprintf('%04d-%02d-%02d', $datearray['year'] - $search_params[$param_name], $datearray['mon'], $datearray['mday']);
315
					$lowerlimit = sprintf('%04d-%02d-%02d', $datearray['year'] - $search_params[$param_name] - 1, $datearray['mon'], $datearray['mday']);
316
					if (in_array($search_params['types'][$param_name], array('-', '--', '=')))
317
					{
318
						$query_parts[] = ($param_info['db_fields'][0]) . ' > {string:' . $param_name . '_minlimit}';
319
						$where_params[$param_name . '_minlimit'] = ($search_params['types'][$param_name] == '--' ? $upperlimit : $lowerlimit);
320
					}
321
					if (in_array($search_params['types'][$param_name], array('+', '++', '=')))
322
					{
323
						$query_parts[] = ($param_info['db_fields'][0]) . ' <= {string:' . $param_name . '_pluslimit}';
324
						$where_params[$param_name . '_pluslimit'] = ($search_params['types'][$param_name] == '++' ? $lowerlimit : $upperlimit);
325
326
						// Make sure that members that didn't set their birth year are not queried.
327
						$query_parts[] = ($param_info['db_fields'][0]) . ' > {date:dec_zero_date}';
328
						$where_params['dec_zero_date'] = '0004-12-31';
329
					}
330
				}
331
				// Special case - equals a date.
332
				elseif ($param_info['type'] == 'date' && $search_params['types'][$param_name] == '=')
333
				{
334
					$query_parts[] = $param_info['db_fields'][0] . ' > ' . $search_params[$param_name] . ' AND ' . $param_info['db_fields'][0] . ' < ' . ($search_params[$param_name] + 86400);
335
				}
336
				else
337
					$query_parts[] = $param_info['db_fields'][0] . ' ' . $range_trans[$search_params['types'][$param_name]] . ' ' . $search_params[$param_name];
338
			}
339
			// Checkboxes.
340
			elseif ($param_info['type'] == 'checkbox')
341
			{
342
				// Each checkbox or no checkbox at all is checked -> ignore.
343
				if (!is_array($search_params[$param_name]) || count($search_params[$param_name]) == 0 || count($search_params[$param_name]) == count($param_info['values']))
344
					continue;
345
346
				$query_parts[] = ($param_info['db_fields'][0]) . ' IN ({array_string:' . $param_name . '_check})';
347
				$where_params[$param_name . '_check'] = $search_params[$param_name];
348
			}
349
			// INET.
350
			elseif ($param_info['type'] == 'inet')
351
			{
352
				if(count($search_params[$param_name]) === 1)
353
				{
354
					$query_parts[] = '(' . $param_info['db_fields'][0] . ' = {inet:' . $param_name . '})';
355
					$where_params[$param_name] = $search_params[$param_name][0];
356
				}
357
				elseif (count($search_params[$param_name]) === 2)
358
				{
359
					$query_parts[] = '(' . $param_info['db_fields'][0] . ' <= {inet:' . $param_name . '_high} and ' . $param_info['db_fields'][0] . ' >= {inet:' . $param_name . '_low})';
360
					$where_params[$param_name.'_low'] = $search_params[$param_name]['low'];
361
					$where_params[$param_name.'_high'] = $search_params[$param_name]['high'];
362
				}
363
				
364
			}
365
			elseif ($param_info['type'] != 'groups')
366
			{
367
				// Replace the wildcard characters ('*' and '?') into MySQL ones.
368
				$parameter = strtolower(strtr($smcFunc['htmlspecialchars']($search_params[$param_name], ENT_QUOTES), array('%' => '\%', '_' => '\_', '*' => '%', '?' => '_')));
369
370
				if ($smcFunc['db_case_sensitive'])
371
					$query_parts[] = '(LOWER(' . implode(') LIKE {string:' . $param_name . '_normal} OR LOWER(', $param_info['db_fields']) . ') LIKE {string:' . $param_name . '_normal})';
372
				else
373
					$query_parts[] = '(' . implode(' LIKE {string:' . $param_name . '_normal} OR ', $param_info['db_fields']) . ' LIKE {string:' . $param_name . '_normal})';
374
				$where_params[$param_name . '_normal'] = '%' . $parameter . '%';
375
			}
376
		}
377
378
		// Set up the membergroup query part.
379
		$mg_query_parts = array();
380
381
		// Primary membergroups, but only if at least was was not selected.
382
		if (!empty($search_params['membergroups'][1]) && count($context['membergroups']) != count($search_params['membergroups'][1]))
383
		{
384
			$mg_query_parts[] = 'mem.id_group IN ({array_int:group_check})';
385
			$where_params['group_check'] = $search_params['membergroups'][1];
386
		}
387
388
		// Additional membergroups (these are only relevant if not all primary groups where selected!).
389
		if (!empty($search_params['membergroups'][2]) && (empty($search_params['membergroups'][1]) || count($context['membergroups']) != count($search_params['membergroups'][1])))
390
			foreach ($search_params['membergroups'][2] as $mg)
391
			{
392
				$mg_query_parts[] = 'FIND_IN_SET({int:add_group_' . $mg . '}, mem.additional_groups) != 0';
393
				$where_params['add_group_' . $mg] = $mg;
394
			}
395
396
		// Combine the one or two membergroup parts into one query part linked with an OR.
397
		if (!empty($mg_query_parts))
398
			$query_parts[] = '(' . implode(' OR ', $mg_query_parts) . ')';
399
400
		// Get all selected post count related membergroups.
401
		if (!empty($search_params['postgroups']) && count($search_params['postgroups']) != count($context['postgroups']))
402
		{
403
			$query_parts[] = 'id_post_group IN ({array_int:post_groups})';
404
			$where_params['post_groups'] = $search_params['postgroups'];
405
		}
406
407
		// Construct the where part of the query.
408
		$where = empty($query_parts) ? '1=1' : implode('
409
			AND ', $query_parts);
410
	}
411
	else
412
		$search_url_params = null;
413
414
	// Construct the additional URL part with the query info in it.
415
	$context['params_url'] = $context['sub_action'] == 'query' ? ';sa=query;params=' . $search_url_params : '';
416
417
	// Get the title and sub template ready..
418
	$context['page_title'] = $txt['admin_members'];
419
420
	$listOptions = array(
421
		'id' => 'member_list',
422
		'title' => $txt['members_list'],
423
		'items_per_page' => $modSettings['defaultMaxMembers'],
424
		'base_href' => $scripturl . '?action=admin;area=viewmembers' . $context['params_url'],
425
		'default_sort_col' => 'user_name',
426
		'get_items' => array(
427
			'file' => $sourcedir . '/Subs-Members.php',
428
			'function' => 'list_getMembers',
429
			'params' => array(
430
				isset($where) ? $where : '1=1',
431
				isset($where_params) ? $where_params : array(),
432
			),
433
		),
434
		'get_count' => array(
435
			'file' => $sourcedir . '/Subs-Members.php',
436
			'function' => 'list_getNumMembers',
437
			'params' => array(
438
				isset($where) ? $where : '1=1',
439
				isset($where_params) ? $where_params : array(),
440
			),
441
		),
442
		'columns' => array(
443
			'id_member' => array(
444
				'header' => array(
445
					'value' => $txt['member_id'],
446
				),
447
				'data' => array(
448
					'db' => 'id_member',
449
				),
450
				'sort' => array(
451
					'default' => 'id_member',
452
					'reverse' => 'id_member DESC',
453
				),
454
			),
455
			'user_name' => array(
456
				'header' => array(
457
					'value' => $txt['username'],
458
				),
459
				'data' => array(
460
					'sprintf' => array(
461
						'format' => '<a href="' . strtr($scripturl, array('%' => '%%')) . '?action=profile;u=%1$d">%2$s</a>',
462
						'params' => array(
463
							'id_member' => false,
464
							'member_name' => false,
465
						),
466
					),
467
					'class' => 'hidden',
468
				),
469
				'sort' => array(
470
					'default' => 'member_name',
471
					'reverse' => 'member_name DESC',
472
				),
473
			),
474
			'display_name' => array(
475
				'header' => array(
476
					'value' => $txt['display_name'],
477
				),
478
				'data' => array(
479
					'sprintf' => array(
480
						'format' => '<a href="' . strtr($scripturl, array('%' => '%%')) . '?action=profile;u=%1$d">%2$s</a>',
481
						'params' => array(
482
							'id_member' => false,
483
							'real_name' => false,
484
						),
485
					),
486
				),
487
				'sort' => array(
488
					'default' => 'real_name',
489
					'reverse' => 'real_name DESC',
490
				),
491
			),
492
			'email' => array(
493
				'header' => array(
494
					'value' => $txt['email_address'],
495
				),
496
				'data' => array(
497
					'sprintf' => array(
498
						'format' => '<a href="mailto:%1$s">%1$s</a>',
499
						'params' => array(
500
							'email_address' => true,
501
						),
502
					),
503
				),
504
				'sort' => array(
505
					'default' => 'email_address',
506
					'reverse' => 'email_address DESC',
507
				),
508
			),
509
			'ip' => array(
510
				'header' => array(
511
					'value' => $txt['ip_address'],
512
				),
513
				'data' => array(
514
					'sprintf' => array(
515
						'format' => '<a href="' . strtr($scripturl, array('%' => '%%')) . '?action=trackip;searchip=%1$s">%1$s</a>',
516
						'params' => array(
517
							'member_ip' => false,
518
						),
519
					),
520
					'class' => 'hidden',
521
				),
522
				'sort' => array(
523
					'default' => 'member_ip',
524
					'reverse' => 'member_ip DESC',
525
				),
526
			),
527
			'last_active' => array(
528
				'header' => array(
529
					'value' => $txt['viewmembers_online'],
530
				),
531
				'data' => array(
532
					'function' => function($rowData) use ($txt)
533
					{
534
						// Calculate number of days since last online.
535
						if (empty($rowData['last_login']))
536
							$difference = $txt['never'];
537
						else
538
						{
539
							$num_days_difference = jeffsdatediff($rowData['last_login']);
540
541
							// Today.
542
							if (empty($num_days_difference))
543
								$difference = $txt['viewmembers_today'];
544
545
							// Yesterday.
546
							elseif ($num_days_difference == 1)
547
								$difference = sprintf('1 %1$s', $txt['viewmembers_day_ago']);
548
549
							// X days ago.
550
							else
551
								$difference = sprintf('%1$d %2$s', $num_days_difference, $txt['viewmembers_days_ago']);
552
						}
553
554
						// Show it in italics if they're not activated...
555
						if ($rowData['is_activated'] % 10 != 1)
556
							$difference = sprintf('<em title="%1$s">%2$s</em>', $txt['not_activated'], $difference);
557
558
						return $difference;
559
					},
560
					'class' => 'hidden',
561
				),
562
				'sort' => array(
563
					'default' => 'last_login DESC',
564
					'reverse' => 'last_login',
565
				),
566
			),
567
			'posts' => array(
568
				'header' => array(
569
					'value' => $txt['member_postcount'],
570
				),
571
				'data' => array(
572
					'db' => 'posts',
573
					'class' => 'hidden',
574
				),
575
				'sort' => array(
576
					'default' => 'posts',
577
					'reverse' => 'posts DESC',
578
				),
579
			),
580
			'check' => array(
581
				'header' => array(
582
					'value' => '<input type="checkbox" onclick="invertAll(this, this.form);">',
583
					'class' => 'centercol',
584
				),
585
				'data' => array(
586
					'function' => function($rowData) use ($user_info)
587
					{
588
						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' : '') . '>';
589
					},
590
					'class' => 'centercol',
591
				),
592
			),
593
		),
594
		'form' => array(
595
			'href' => $scripturl . '?action=admin;area=viewmembers' . $context['params_url'],
596
			'include_start' => true,
597
			'include_sort' => true,
598
		),
599
		'additional_rows' => array(
600
			array(
601
				'position' => 'below_table_data',
602
				'value' => '<input type="submit" name="delete_members" value="' . $txt['admin_delete_members'] . '" data-confirm="' . $txt['confirm_delete_members'] . '" class="button you_sure">',
603
			),
604
		),
605
	);
606
607
	// Without enough permissions, don't show 'delete members' checkboxes.
608
	if (!allowedTo('profile_remove_any'))
609
		unset($listOptions['cols']['check'], $listOptions['form'], $listOptions['additional_rows']);
610
611
	require_once($sourcedir . '/Subs-List.php');
612
	createList($listOptions);
613
614
	$context['sub_template'] = 'show_list';
615
	$context['default_list'] = 'member_list';
616
}
617
618
/**
619
 * Search the member list, using one or more criteria.
620
 * Called by ?action=admin;area=viewmembers;sa=search.
621
 * Requires the moderate_forum permission.
622
 * form is submitted to action=admin;area=viewmembers;sa=query.
623
 *
624
 * @uses the search_members sub template of the ManageMembers template.
625
 */
626
function SearchMembers()
627
{
628
	global $context, $txt, $smcFunc;
629
630
	// Get a list of all the membergroups and postgroups that can be selected.
631
	$context['membergroups'] = array(
632
		array(
633
			'id' => 0,
634
			'name' => $txt['membergroups_members'],
635
			'can_be_additional' => false
636
		)
637
	);
638
	$context['postgroups'] = array();
639
640
	$request = $smcFunc['db_query']('', '
641
		SELECT id_group, group_name, min_posts
642
		FROM {db_prefix}membergroups
643
		WHERE id_group != {int:moderator_group}
644
		ORDER BY min_posts, CASE WHEN id_group < {int:newbie_group} THEN id_group ELSE 4 END, group_name',
645
		array(
646
			'moderator_group' => 3,
647
			'newbie_group' => 4,
648
		)
649
	);
650 View Code Duplication
	while ($row = $smcFunc['db_fetch_assoc']($request))
651
	{
652
		if ($row['min_posts'] == -1)
653
			$context['membergroups'][] = array(
654
				'id' => $row['id_group'],
655
				'name' => $row['group_name'],
656
				'can_be_additional' => true
657
			);
658
		else
659
			$context['postgroups'][] = array(
660
				'id' => $row['id_group'],
661
				'name' => $row['group_name']
662
			);
663
	}
664
	$smcFunc['db_free_result']($request);
665
666
	$context['page_title'] = $txt['admin_members'];
667
	$context['sub_template'] = 'search_members';
668
}
669
670
/**
671
 * List all members who are awaiting approval / activation, sortable on different columns.
672
 * It allows instant approval or activation of (a selection of) members.
673
 * Called by ?action=admin;area=viewmembers;sa=browse;type=approve
674
 *  or ?action=admin;area=viewmembers;sa=browse;type=activate.
675
 * The form submits to ?action=admin;area=viewmembers;sa=approve.
676
 * Requires the moderate_forum permission.
677
 *
678
 * @uses the admin_browse sub template of the ManageMembers template.
679
 */
680
function MembersAwaitingActivation()
681
{
682
	global $txt, $context, $scripturl, $modSettings;
683
	global $sourcedir;
684
685
	// Not a lot here!
686
	$context['page_title'] = $txt['admin_members'];
687
	$context['sub_template'] = 'admin_browse';
688
	$context['browse_type'] = isset($_REQUEST['type']) ? $_REQUEST['type'] : (!empty($modSettings['registration_method']) && $modSettings['registration_method'] == 1 ? 'activate' : 'approve');
689
	if (isset($context['tabs'][$context['browse_type']]))
690
		$context['tabs'][$context['browse_type']]['is_selected'] = true;
691
692
	// Allowed filters are those we can have, in theory.
693
	$context['allowed_filters'] = $context['browse_type'] == 'approve' ? array(3, 4, 5) : array(0, 2);
694
	$context['current_filter'] = isset($_REQUEST['filter']) && in_array($_REQUEST['filter'], $context['allowed_filters']) && !empty($context['activation_numbers'][$_REQUEST['filter']]) ? (int) $_REQUEST['filter'] : -1;
695
696
	// Sort out the different sub areas that we can actually filter by.
697
	$context['available_filters'] = array();
698
	foreach ($context['activation_numbers'] as $type => $amount)
699
	{
700
		// We have some of these...
701
		if (in_array($type, $context['allowed_filters']) && $amount > 0)
702
			$context['available_filters'][] = array(
703
				'type' => $type,
704
				'amount' => $amount,
705
				'desc' => isset($txt['admin_browse_filter_type_' . $type]) ? $txt['admin_browse_filter_type_' . $type] : '?',
706
				'selected' => $type == $context['current_filter']
707
			);
708
	}
709
710
	// If the filter was not sent, set it to whatever has people in it!
711
	if ($context['current_filter'] == -1 && !empty($context['available_filters'][0]['amount']))
712
		$context['current_filter'] = $context['available_filters'][0]['type'];
713
714
	// This little variable is used to determine if we should flag where we are looking.
715
	$context['show_filter'] = ($context['current_filter'] != 0 && $context['current_filter'] != 3) || count($context['available_filters']) > 1;
716
717
	// The columns that can be sorted.
718
	$context['columns'] = array(
719
		'id_member' => array('label' => $txt['admin_browse_id']),
720
		'member_name' => array('label' => $txt['admin_browse_username']),
721
		'email_address' => array('label' => $txt['admin_browse_email']),
722
		'member_ip' => array('label' => $txt['admin_browse_ip']),
723
		'date_registered' => array('label' => $txt['admin_browse_registered']),
724
	);
725
726
	// Are we showing duplicate information?
727
	if (isset($_GET['showdupes']))
728
		$_SESSION['showdupes'] = (int) $_GET['showdupes'];
729
	$context['show_duplicates'] = !empty($_SESSION['showdupes']);
730
731
	// Determine which actions we should allow on this page.
732
	if ($context['browse_type'] == 'approve')
733
	{
734
		// If we are approving deleted accounts we have a slightly different list... actually a mirror ;)
735
		if ($context['current_filter'] == 4)
736
			$context['allowed_actions'] = array(
737
				'reject' => $txt['admin_browse_w_approve_deletion'],
738
				'ok' => $txt['admin_browse_w_reject'],
739
			);
740
		else
741
			$context['allowed_actions'] = array(
742
				'ok' => $txt['admin_browse_w_approve'],
743
				'okemail' => $txt['admin_browse_w_approve'] . ' ' . $txt['admin_browse_w_email'],
744
				'require_activation' => $txt['admin_browse_w_approve_require_activate'],
745
				'reject' => $txt['admin_browse_w_reject'],
746
				'rejectemail' => $txt['admin_browse_w_reject'] . ' ' . $txt['admin_browse_w_email'],
747
			);
748
	}
749
	elseif ($context['browse_type'] == 'activate')
750
		$context['allowed_actions'] = array(
751
			'ok' => $txt['admin_browse_w_activate'],
752
			'okemail' => $txt['admin_browse_w_activate'] . ' ' . $txt['admin_browse_w_email'],
753
			'delete' => $txt['admin_browse_w_delete'],
754
			'deleteemail' => $txt['admin_browse_w_delete'] . ' ' . $txt['admin_browse_w_email'],
755
			'remind' => $txt['admin_browse_w_remind'] . ' ' . $txt['admin_browse_w_email'],
756
		);
757
758
	// Create an option list for actions allowed to be done with selected members.
759
	$allowed_actions = '
760
			<option selected value="">' . $txt['admin_browse_with_selected'] . ':</option>
761
			<option value="" disabled>-----------------------------</option>';
762
	foreach ($context['allowed_actions'] as $key => $desc)
763
		$allowed_actions .= '
764
			<option value="' . $key . '">' . $desc . '</option>';
765
766
	// Setup the Javascript function for selecting an action for the list.
767
	$javascript = '
768
		function onSelectChange()
769
		{
770
			if (document.forms.postForm.todo.value == "")
771
				return;
772
773
			var message = "";';
774
775
	// We have special messages for approving deletion of accounts - it's surprisingly logical - honest.
776
	if ($context['current_filter'] == 4)
777
		$javascript .= '
778
			if (document.forms.postForm.todo.value.indexOf("reject") != -1)
779
				message = "' . $txt['admin_browse_w_delete'] . '";
780
			else
781
				message = "' . $txt['admin_browse_w_reject'] . '";';
782
	// Otherwise a nice standard message.
783
	else
784
		$javascript .= '
785
			if (document.forms.postForm.todo.value.indexOf("delete") != -1)
786
				message = "' . $txt['admin_browse_w_delete'] . '";
787
			else if (document.forms.postForm.todo.value.indexOf("reject") != -1)
788
				message = "' . $txt['admin_browse_w_reject'] . '";
789
			else if (document.forms.postForm.todo.value == "remind")
790
				message = "' . $txt['admin_browse_w_remind'] . '";
791
			else
792
				message = "' . ($context['browse_type'] == 'approve' ? $txt['admin_browse_w_approve'] : $txt['admin_browse_w_activate']) . '";';
793
	$javascript .= '
794
			if (confirm(message + " ' . $txt['admin_browse_warn'] . '"))
795
				document.forms.postForm.submit();
796
		}';
797
798
	$listOptions = array(
799
		'id' => 'approve_list',
800
// 		'title' => $txt['members_approval_title'],
801
		'items_per_page' => $modSettings['defaultMaxMembers'],
802
		'base_href' => $scripturl . '?action=admin;area=viewmembers;sa=browse;type=' . $context['browse_type'] . (!empty($context['show_filter']) ? ';filter=' . $context['current_filter'] : ''),
803
		'default_sort_col' => 'date_registered',
804
		'get_items' => array(
805
			'file' => $sourcedir . '/Subs-Members.php',
806
			'function' => 'list_getMembers',
807
			'params' => array(
808
				'is_activated = {int:activated_status}',
809
				array('activated_status' => $context['current_filter']),
810
				$context['show_duplicates'],
811
			),
812
		),
813
		'get_count' => array(
814
			'file' => $sourcedir . '/Subs-Members.php',
815
			'function' => 'list_getNumMembers',
816
			'params' => array(
817
				'is_activated = {int:activated_status}',
818
				array('activated_status' => $context['current_filter']),
819
			),
820
		),
821
		'columns' => array(
822
			'id_member' => array(
823
				'header' => array(
824
					'value' => $txt['member_id'],
825
				),
826
				'data' => array(
827
					'db' => 'id_member',
828
				),
829
				'sort' => array(
830
					'default' => 'id_member',
831
					'reverse' => 'id_member DESC',
832
				),
833
			),
834
			'user_name' => array(
835
				'header' => array(
836
					'value' => $txt['username'],
837
				),
838
				'data' => array(
839
					'sprintf' => array(
840
						'format' => '<a href="' . strtr($scripturl, array('%' => '%%')) . '?action=profile;u=%1$d">%2$s</a>',
841
						'params' => array(
842
							'id_member' => false,
843
							'member_name' => false,
844
						),
845
					),
846
				),
847
				'sort' => array(
848
					'default' => 'member_name',
849
					'reverse' => 'member_name DESC',
850
				),
851
			),
852
			'email' => array(
853
				'header' => array(
854
					'value' => $txt['email_address'],
855
				),
856
				'data' => array(
857
					'sprintf' => array(
858
						'format' => '<a href="mailto:%1$s">%1$s</a>',
859
						'params' => array(
860
							'email_address' => true,
861
						),
862
					),
863
				),
864
				'sort' => array(
865
					'default' => 'email_address',
866
					'reverse' => 'email_address DESC',
867
				),
868
			),
869
			'ip' => array(
870
				'header' => array(
871
					'value' => $txt['ip_address'],
872
				),
873
				'data' => array(
874
					'sprintf' => array(
875
						'format' => '<a href="' . strtr($scripturl, array('%' => '%%')) . '?action=trackip;searchip=%1$s">%1$s</a>',
876
						'params' => array(
877
							'member_ip' => false,
878
						),
879
					),
880
				),
881
				'sort' => array(
882
					'default' => 'member_ip',
883
					'reverse' => 'member_ip DESC',
884
				),
885
			),
886
			'hostname' => array(
887
				'header' => array(
888
					'value' => $txt['hostname'],
889
				),
890
				'data' => array(
891
					'function' => function($rowData)
892
					{
893
						return host_from_ip(inet_dtop($rowData['member_ip']));
0 ignored issues
show
Security Bug introduced by
It seems like inet_dtop($rowData['member_ip']) targeting inet_dtop() can also be of type false; however, host_from_ip() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
894
					},
895
					'class' => 'smalltext',
896
				),
897
			),
898
			'date_registered' => array(
899
				'header' => array(
900
					'value' => $context['current_filter'] == 4 ? $txt['viewmembers_online'] : $txt['date_registered'],
901
				),
902
				'data' => array(
903
					'function' => function($rowData) use ($context)
904
					{
905
						return timeformat($rowData['' . ($context['current_filter'] == 4 ? 'last_login' : 'date_registered') . '']);
906
					},
907
				),
908
				'sort' => array(
909
					'default' => $context['current_filter'] == 4 ? 'mem.last_login DESC' : 'date_registered DESC',
910
					'reverse' => $context['current_filter'] == 4 ? 'mem.last_login' : 'date_registered',
911
				),
912
			),
913
			'duplicates' => array(
914
				'header' => array(
915
					'value' => $txt['duplicates'],
916
					// Make sure it doesn't go too wide.
917
					'style' => 'width: 20%;',
918
				),
919
				'data' => array(
920
					'function' => function($rowData) use ($scripturl, $txt)
921
					{
922
						$member_links = array();
923
						foreach ($rowData['duplicate_members'] as $member)
924
						{
925
							if ($member['id'])
926
								$member_links[] = '<a href="' . $scripturl . '?action=profile;u=' . $member['id'] . '" ' . (!empty($member['is_banned']) ? 'class="red"' : '') . '>' . $member['name'] . '</a>';
927
							else
928
								$member_links[] = $member['name'] . ' (' . $txt['guest'] . ')';
929
						}
930
						return implode(', ', $member_links);
931
					},
932
					'class' => 'smalltext',
933
				),
934
			),
935
			'check' => array(
936
				'header' => array(
937
					'value' => '<input type="checkbox" onclick="invertAll(this, this.form);">',
938
					'class' => 'centercol',
939
				),
940
				'data' => array(
941
					'sprintf' => array(
942
						'format' => '<input type="checkbox" name="todoAction[]" value="%1$d">',
943
						'params' => array(
944
							'id_member' => false,
945
						),
946
					),
947
					'class' => 'centercol',
948
				),
949
			),
950
		),
951
		'javascript' => $javascript,
952
		'form' => array(
953
			'href' => $scripturl . '?action=admin;area=viewmembers;sa=approve;type=' . $context['browse_type'],
954
			'name' => 'postForm',
955
			'include_start' => true,
956
			'include_sort' => true,
957
			'hidden_fields' => array(
958
				'orig_filter' => $context['current_filter'],
959
			),
960
		),
961
		'additional_rows' => array(
962
			array(
963
				'position' => 'below_table_data',
964
				'value' => '
965
					[<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>]
966
					<select name="todo" onchange="onSelectChange();">
967
						' . $allowed_actions . '
968
					</select>
969
					<noscript><input type="submit" value="' . $txt['go'] . '" class="button"><br class="clear_right"></noscript>
970
				',
971
				'class' => 'floatright',
972
			),
973
		),
974
	);
975
976
	// Pick what column to actually include if we're showing duplicates.
977
	if ($context['show_duplicates'])
978
		unset($listOptions['columns']['email']);
979
	else
980
		unset($listOptions['columns']['duplicates']);
981
982
	// Only show hostname on duplicates as it takes a lot of time.
983
	if (!$context['show_duplicates'] || !empty($modSettings['disableHostnameLookup']))
984
		unset($listOptions['columns']['hostname']);
985
986
	// Is there any need to show filters?
987
	if (isset($context['available_filters']) && count($context['available_filters']) > 1)
988
	{
989
		$filterOptions = '
990
			<strong>' . $txt['admin_browse_filter_by'] . ':</strong>
991
			<select name="filter" onchange="this.form.submit();">';
992
		foreach ($context['available_filters'] as $filter)
993
			$filterOptions .= '
994
				<option value="' . $filter['type'] . '"' . ($filter['selected'] ? ' selected' : '') . '>' . $filter['desc'] . ' - ' . $filter['amount'] . ' ' . ($filter['amount'] == 1 ? $txt['user'] : $txt['users']) . '</option>';
995
		$filterOptions .= '
996
			</select>
997
			<noscript><input type="submit" value="' . $txt['go'] . '" name="filter" class="button"></noscript>';
998
		$listOptions['additional_rows'][] = array(
999
			'position' => 'top_of_list',
1000
			'value' => $filterOptions,
1001
			'class' => 'righttext',
1002
		);
1003
	}
1004
1005
	// What about if we only have one filter, but it's not the "standard" filter - show them what they are looking at.
1006
	if (!empty($context['show_filter']) && !empty($context['available_filters']))
1007
		$listOptions['additional_rows'][] = array(
1008
			'position' => 'above_column_headers',
1009
			'value' => '<strong>' . $txt['admin_browse_filter_show'] . ':</strong> ' . $context['available_filters'][0]['desc'],
1010
			'class' => 'smalltext floatright',
1011
		);
1012
1013
	// Now that we have all the options, create the list.
1014
	require_once($sourcedir . '/Subs-List.php');
1015
	createList($listOptions);
1016
}
1017
1018
/**
1019
 * This function handles the approval, rejection, activation or deletion of members.
1020
 * Called by ?action=admin;area=viewmembers;sa=approve.
1021
 * Requires the moderate_forum permission.
1022
 * Redirects to ?action=admin;area=viewmembers;sa=browse
1023
 * with the same parameters as the calling page.
1024
 */
1025
function AdminApprove()
1026
{
1027
	global $scripturl, $modSettings, $sourcedir, $language, $user_info, $smcFunc;
1028
1029
	// First, check our session.
1030
	checkSession();
1031
1032
	require_once($sourcedir . '/Subs-Post.php');
1033
1034
	// We also need to the login languages here - for emails.
1035
	loadLanguage('Login');
1036
1037
	// Sort out where we are going...
1038
	$current_filter = (int) $_REQUEST['orig_filter'];
1039
1040
	// If we are applying a filter do just that - then redirect.
1041
	if (isset($_REQUEST['filter']) && $_REQUEST['filter'] != $_REQUEST['orig_filter'])
1042
		redirectexit('action=admin;area=viewmembers;sa=browse;type=' . $_REQUEST['type'] . ';sort=' . $_REQUEST['sort'] . ';filter=' . $_REQUEST['filter'] . ';start=' . $_REQUEST['start']);
1043
1044
	// Nothing to do?
1045
	if (!isset($_POST['todoAction']) && !isset($_POST['time_passed']))
1046
		redirectexit('action=admin;area=viewmembers;sa=browse;type=' . $_REQUEST['type'] . ';sort=' . $_REQUEST['sort'] . ';filter=' . $current_filter . ';start=' . $_REQUEST['start']);
1047
1048
	// Are we dealing with members who have been waiting for > set amount of time?
1049
	if (isset($_POST['time_passed']))
1050
	{
1051
		$timeBefore = time() - 86400 * (int) $_POST['time_passed'];
1052
		$condition = '
1053
			AND date_registered < {int:time_before}';
1054
	}
1055
	// Coming from checkboxes - validate the members passed through to us.
1056
	else
1057
	{
1058
		$members = array();
1059
		foreach ($_POST['todoAction'] as $id)
1060
			$members[] = (int) $id;
1061
		$condition = '
1062
			AND id_member IN ({array_int:members})';
1063
	}
1064
1065
	// Get information on each of the members, things that are important to us, like email address...
1066
	$request = $smcFunc['db_query']('', '
1067
		SELECT id_member, member_name, real_name, email_address, validation_code, lngfile
1068
		FROM {db_prefix}members
1069
		WHERE is_activated = {int:activated_status}' . $condition . '
1070
		ORDER BY lngfile',
1071
		array(
1072
			'activated_status' => $current_filter,
1073
			'time_before' => empty($timeBefore) ? 0 : $timeBefore,
1074
			'members' => empty($members) ? array() : $members,
1075
		)
1076
	);
1077
1078
	$member_count = $smcFunc['db_num_rows']($request);
1079
1080
	// If no results then just return!
1081
	if ($member_count == 0)
1082
		redirectexit('action=admin;area=viewmembers;sa=browse;type=' . $_REQUEST['type'] . ';sort=' . $_REQUEST['sort'] . ';filter=' . $current_filter . ';start=' . $_REQUEST['start']);
1083
1084
	$member_info = array();
1085
	$members = array();
1086
	// Fill the info array.
1087
	while ($row = $smcFunc['db_fetch_assoc']($request))
1088
	{
1089
		$members[] = $row['id_member'];
1090
		$member_info[] = array(
1091
			'id' => $row['id_member'],
1092
			'username' => $row['member_name'],
1093
			'name' => $row['real_name'],
1094
			'email' => $row['email_address'],
1095
			'language' => empty($row['lngfile']) || empty($modSettings['userLanguage']) ? $language : $row['lngfile'],
1096
			'code' => $row['validation_code']
1097
		);
1098
	}
1099
	$smcFunc['db_free_result']($request);
1100
1101
	// Are we activating or approving the members?
1102
	if ($_POST['todo'] == 'ok' || $_POST['todo'] == 'okemail')
1103
	{
1104
		// Approve/activate this member.
1105
		$smcFunc['db_query']('', '
1106
			UPDATE {db_prefix}members
1107
			SET validation_code = {string:blank_string}, is_activated = {int:is_activated}
1108
			WHERE is_activated = {int:activated_status}' . $condition,
1109
			array(
1110
				'is_activated' => 1,
1111
				'time_before' => empty($timeBefore) ? 0 : $timeBefore,
1112
				'members' => empty($members) ? array() : $members,
1113
				'activated_status' => $current_filter,
1114
				'blank_string' => '',
1115
			)
1116
		);
1117
1118
		// Do we have to let the integration code know about the activations?
1119
		if (!empty($modSettings['integrate_activate']))
1120
		{
1121
			foreach ($member_info as $member)
1122
				call_integration_hook('integrate_activate', array($member['username']));
1123
		}
1124
1125
		// Check for email.
1126
		if ($_POST['todo'] == 'okemail')
1127
		{
1128
			foreach ($member_info as $member)
1129
			{
1130
				$replacements = array(
1131
					'NAME' => $member['name'],
1132
					'USERNAME' => $member['username'],
1133
					'PROFILELINK' => $scripturl . '?action=profile;u=' . $member['id'],
1134
					'FORGOTPASSWORDLINK' => $scripturl . '?action=reminder',
1135
				);
1136
1137
				$emaildata = loadEmailTemplate('admin_approve_accept', $replacements, $member['language']);
1138
				sendmail($member['email'], $emaildata['subject'], $emaildata['body'], null, 'accapp' . $member['id'], $emaildata['is_html'], 0);
1139
			}
1140
		}
1141
	}
1142
	// Maybe we're sending it off for activation?
1143
	elseif ($_POST['todo'] == 'require_activation')
1144
	{
1145
		require_once($sourcedir . '/Subs-Members.php');
1146
1147
		// We have to do this for each member I'm afraid.
1148
		foreach ($member_info as $member)
1149
		{
1150
			// Generate a random activation code.
1151
			$validation_code = generateValidationCode();
1152
1153
			// Set these members for activation - I know this includes two id_member checks but it's safer than bodging $condition ;).
1154
			$smcFunc['db_query']('', '
1155
				UPDATE {db_prefix}members
1156
				SET validation_code = {string:validation_code}, is_activated = {int:not_activated}
1157
				WHERE is_activated = {int:activated_status}
1158
					' . $condition . '
1159
					AND id_member = {int:selected_member}',
1160
				array(
1161
					'not_activated' => 0,
1162
					'activated_status' => $current_filter,
1163
					'selected_member' => $member['id'],
1164
					'validation_code' => $validation_code,
1165
					'time_before' => empty($timeBefore) ? 0 : $timeBefore,
1166
					'members' => empty($members) ? array() : $members,
1167
				)
1168
			);
1169
1170
			$replacements = array(
1171
				'USERNAME' => $member['name'],
1172
				'ACTIVATIONLINK' => $scripturl . '?action=activate;u=' . $member['id'] . ';code=' . $validation_code,
1173
				'ACTIVATIONLINKWITHOUTCODE' => $scripturl . '?action=activate;u=' . $member['id'],
1174
				'ACTIVATIONCODE' => $validation_code,
1175
			);
1176
1177
			$emaildata = loadEmailTemplate('admin_approve_activation', $replacements, $member['language']);
1178
			sendmail($member['email'], $emaildata['subject'], $emaildata['body'], null, 'accact' . $member['id'], $emaildata['is_html'], 0);
1179
		}
1180
	}
1181
	// Are we rejecting them?
1182 View Code Duplication
	elseif ($_POST['todo'] == 'reject' || $_POST['todo'] == 'rejectemail')
1183
	{
1184
		require_once($sourcedir . '/Subs-Members.php');
1185
		deleteMembers($members);
1186
1187
		// Send email telling them they aren't welcome?
1188
		if ($_POST['todo'] == 'rejectemail')
1189
		{
1190
			foreach ($member_info as $member)
1191
			{
1192
				$replacements = array(
1193
					'USERNAME' => $member['name'],
1194
				);
1195
1196
				$emaildata = loadEmailTemplate('admin_approve_reject', $replacements, $member['language']);
1197
				sendmail($member['email'], $emaildata['subject'], $emaildata['body'], null, 'accrej', $emaildata['is_html'], 1);
1198
			}
1199
		}
1200
	}
1201
	// A simple delete?
1202 View Code Duplication
	elseif ($_POST['todo'] == 'delete' || $_POST['todo'] == 'deleteemail')
1203
	{
1204
		require_once($sourcedir . '/Subs-Members.php');
1205
		deleteMembers($members);
1206
1207
		// Send email telling them they aren't welcome?
1208
		if ($_POST['todo'] == 'deleteemail')
1209
		{
1210
			foreach ($member_info as $member)
1211
			{
1212
				$replacements = array(
1213
					'USERNAME' => $member['name'],
1214
				);
1215
1216
				$emaildata = loadEmailTemplate('admin_approve_delete', $replacements, $member['language']);
1217
				sendmail($member['email'], $emaildata['subject'], $emaildata['body'], null, 'accdel', $emaildata['is_html'], 1);
1218
			}
1219
		}
1220
	}
1221
	// Remind them to activate their account?
1222
	elseif ($_POST['todo'] == 'remind')
1223
	{
1224
		foreach ($member_info as $member)
1225
		{
1226
			$replacements = array(
1227
				'USERNAME' => $member['name'],
1228
				'ACTIVATIONLINK' => $scripturl . '?action=activate;u=' . $member['id'] . ';code=' . $member['code'],
1229
				'ACTIVATIONLINKWITHOUTCODE' => $scripturl . '?action=activate;u=' . $member['id'],
1230
				'ACTIVATIONCODE' => $member['code'],
1231
			);
1232
1233
			$emaildata = loadEmailTemplate('admin_approve_remind', $replacements, $member['language']);
1234
			sendmail($member['email'], $emaildata['subject'], $emaildata['body'], null, 'accrem' . $member['id'], $emaildata['is_html'], 1);
1235
		}
1236
	}
1237
1238
	// @todo current_language is never set, no idea what this is for. Remove?
1239
	// Back to the user's language!
1240
	if (isset($current_language) && $current_language != $user_info['language'])
0 ignored issues
show
Bug introduced by
The variable $current_language does not exist. Did you mean $language?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
1241
	{
1242
		loadLanguage('index');
1243
		loadLanguage('ManageMembers');
1244
	}
1245
1246
	// Log what we did?
1247
	if (!empty($modSettings['modlog_enabled']) && in_array($_POST['todo'], array('ok', 'okemail', 'require_activation', 'remind')))
1248
	{
1249
		$log_action = $_POST['todo'] == 'remind' ? 'remind_member' : 'approve_member';
1250
1251
		require_once($sourcedir . '/Logging.php');
1252
		foreach ($member_info as $member)
1253
			logAction($log_action, array('member' => $member['id']), 'admin');
1254
	}
1255
1256
	// Although updateStats *may* catch this, best to do it manually just in case (Doesn't always sort out unapprovedMembers).
1257
	if (in_array($current_filter, array(3, 4, 5)))
1258
		updateSettings(array('unapprovedMembers' => ($modSettings['unapprovedMembers'] > $member_count ? $modSettings['unapprovedMembers'] - $member_count : 0)));
1259
1260
	// Update the member's stats. (but, we know the member didn't change their name.)
1261
	updateStats('member', false);
1262
1263
	// If they haven't been deleted, update the post group statistics on them...
1264
	if (!in_array($_POST['todo'], array('delete', 'deleteemail', 'reject', 'rejectemail', 'remind')))
1265
		updateStats('postgroups', $members);
1266
1267
	redirectexit('action=admin;area=viewmembers;sa=browse;type=' . $_REQUEST['type'] . ';sort=' . $_REQUEST['sort'] . ';filter=' . $current_filter . ';start=' . $_REQUEST['start']);
1268
}
1269
1270
/**
1271
 * Nifty function to calculate the number of days ago a given date was.
1272
 * Requires a unix timestamp as input, returns an integer.
1273
 * Named in honour of Jeff Lewis, the original creator of...this function.
1274
 *
1275
 * @param int $old The timestamp of the old date
1276
 * @return int The number of days since $old, based on the forum time
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|double?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
1277
 */
1278
function jeffsdatediff($old)
1279
{
1280
	// Get the current time as the user would see it...
1281
	$forumTime = forum_time();
1282
1283
	// Calculate the seconds that have passed since midnight.
1284
	$sinceMidnight = date('H', $forumTime) * 60 * 60 + date('i', $forumTime) * 60 + date('s', $forumTime);
1285
1286
	// Take the difference between the two times.
1287
	$dis = time() - $old;
1288
1289
	// Before midnight?
1290
	if ($dis < $sinceMidnight)
1291
		return 0;
1292
	else
1293
		$dis -= $sinceMidnight;
1294
1295
	// Divide out the seconds in a day to get the number of days.
1296
	return ceil($dis / (24 * 60 * 60));
1297
}
1298
1299
/**
1300
 * Fetches all the activation counts for ViewMembers.
1301
 *
1302
 */
1303
function GetMemberActivationCounts()
1304
{
1305
	global $smcFunc, $context;
1306
1307
	// Get counts on every type of activation - for sections and filtering alike.
1308
	$request = $smcFunc['db_query']('', '
1309
		SELECT COUNT(*) AS total_members, is_activated
1310
		FROM {db_prefix}members
1311
		WHERE is_activated != {int:is_activated}
1312
		GROUP BY is_activated',
1313
		array(
1314
			'is_activated' => 1,
1315
		)
1316
	);
1317
	$context['activation_numbers'] = array();
1318
	$context['awaiting_activation'] = 0;
1319
	$context['awaiting_approval'] = 0;
1320
	while ($row = $smcFunc['db_fetch_assoc']($request))
1321
		$context['activation_numbers'][$row['is_activated']] = $row['total_members'];
1322
	$smcFunc['db_free_result']($request);
1323
1324
	foreach ($context['activation_numbers'] as $activation_type => $total_members)
1325
	{
1326
		if (in_array($activation_type, array(0, 2)))
1327
			$context['awaiting_activation'] += $total_members;
1328
		elseif (in_array($activation_type, array(3, 4, 5)))
1329
			$context['awaiting_approval'] += $total_members;
1330
	}
1331
1332
}
1333
1334
?>