Issues (1061)

Sources/Memberlist.php (2 issues)

1
<?php
2
3
/**
4
 * This file contains the functions for displaying and searching in the
5
 * members list.
6
 *
7
 * Simple Machines Forum (SMF)
8
 *
9
 * @package SMF
10
 * @author Simple Machines https://www.simplemachines.org
11
 * @copyright 2020 Simple Machines and individual contributors
12
 * @license https://www.simplemachines.org/about/smf/license.php BSD
13
 *
14
 * @version 2.1 RC2
15
 */
16
17
if (!defined('SMF'))
18
	die('No direct access...');
19
20
/**
21
 * Shows a listing of registered members.
22
 * - If a subaction is not specified, lists all registered members.
23
 * - It allows searching for members with the 'search' sub action.
24
 * - It calls MLAll or MLSearch depending on the sub action.
25
 * - Requires the view_mlist permission.
26
 * - Accessed via ?action=mlist.
27
 *
28
 * Uses Memberlist template, main sub template.
29
 */
30
function Memberlist()
31
{
32
	global $scripturl, $txt, $modSettings, $context;
33
34
	// Make sure they can view the memberlist.
35
	isAllowedTo('view_mlist');
36
37
	loadTemplate('Memberlist');
38
39
	$context['listing_by'] = !empty($_GET['sa']) ? $_GET['sa'] : 'all';
40
41
	// $subActions array format:
42
	// 'subaction' => array('label', 'function', 'is_selected')
43
	$subActions = array(
44
		'all' => array($txt['view_all_members'], 'MLAll', $context['listing_by'] == 'all'),
45
		'search' => array($txt['mlist_search'], 'MLSearch', $context['listing_by'] == 'search'),
46
	);
47
48
	// Set up the sort links.
49
	$context['sort_links'] = array();
50
	foreach ($subActions as $act => $text)
51
	{
52
		$context['sort_links'][] = array(
53
			'label' => $text[0],
54
			'action' => $act,
55
			'selected' => $text[2],
56
		);
57
	}
58
59
	$context['num_members'] = $modSettings['totalMembers'];
60
61
	// Set up the columns...
62
	$context['columns'] = array(
63
		'is_online' => array(
64
			'label' => $txt['status'],
65
			'sort' => array(
66
				'down' => allowedTo('moderate_forum') ? 'COALESCE(lo.log_time, 1) ASC, real_name ASC' : 'CASE WHEN mem.show_online THEN COALESCE(lo.log_time, 1) ELSE 1 END ASC, real_name ASC',
67
				'up' => allowedTo('moderate_forum') ? 'COALESCE(lo.log_time, 1) DESC, real_name DESC' : 'CASE WHEN mem.show_online THEN COALESCE(lo.log_time, 1) ELSE 1 END DESC, real_name DESC'
68
			),
69
		),
70
		'real_name' => array(
71
			'label' => $txt['name'],
72
			'class' => 'lefttext',
73
			'sort' => array(
74
				'down' => 'mem.real_name DESC',
75
				'up' => 'mem.real_name ASC'
76
			),
77
		),
78
		'website_url' => array(
79
			'label' => $txt['website'],
80
			'link_with' => 'website',
81
			'sort' => array(
82
				'down' => 'LENGTH(mem.website_url) > 0 ASC, COALESCE(mem.website_url, 1=1) DESC, mem.website_url DESC',
83
				'up' => 'LENGTH(mem.website_url) > 0 DESC, COALESCE(mem.website_url, 1=1) ASC, mem.website_url ASC'
84
			),
85
		),
86
		'id_group' => array(
87
			'label' => $txt['position'],
88
			'sort' => array(
89
				'down' => 'COALESCE(mg.group_name, 1=1) DESC, mg.group_name DESC',
90
				'up' => 'COALESCE(mg.group_name, 1=1) ASC, mg.group_name ASC'
91
			),
92
		),
93
		'registered' => array(
94
			'label' => $txt['date_registered'],
95
			'sort' => array(
96
				'down' => 'mem.date_registered DESC',
97
				'up' => 'mem.date_registered ASC'
98
			),
99
		),
100
		'post_count' => array(
101
			'label' => $txt['posts'],
102
			'default_sort_rev' => true,
103
			'sort' => array(
104
				'down' => 'mem.posts DESC',
105
				'up' => 'mem.posts ASC'
106
			),
107
		)
108
	);
109
110
	$context['custom_profile_fields'] = getCustFieldsMList();
111
112
	if (!empty($context['custom_profile_fields']['columns']))
113
		$context['columns'] += $context['custom_profile_fields']['columns'];
114
115
	$context['colspan'] = 0;
116
	$context['disabled_fields'] = isset($modSettings['disabled_profile_fields']) ? array_flip(explode(',', $modSettings['disabled_profile_fields'])) : array();
117
	foreach ($context['columns'] as $key => $column)
118
	{
119
		if (isset($context['disabled_fields'][$key]) || (isset($column['link_with']) && isset($context['disabled_fields'][$column['link_with']])))
120
		{
121
			unset($context['columns'][$key]);
122
			continue;
123
		}
124
125
		$context['colspan'] += isset($column['colspan']) ? $column['colspan'] : 1;
126
	}
127
128
	// Aesthetic stuff.
129
	end($context['columns']);
130
131
	$context['linktree'][] = array(
132
		'url' => $scripturl . '?action=mlist',
133
		'name' => $txt['members_list']
134
	);
135
136
	$context['can_send_pm'] = allowedTo('pm_send');
137
	$context['can_send_email'] = allowedTo('moderate_forum');
138
139
	// Build the memberlist button array.
140
	$context['memberlist_buttons'] = array(
141
		'view_all_members' => array('text' => 'view_all_members', 'image' => 'mlist.png', 'url' => $scripturl . '?action=mlist' . ';sa=all', 'active' => true),
142
		'mlist_search' => array('text' => 'mlist_search', 'image' => 'mlist.png', 'url' => $scripturl . '?action=mlist' . ';sa=search'),
143
	);
144
145
	// Allow mods to add additional buttons here
146
	call_integration_hook('integrate_memberlist_buttons');
147
148
	// Jump to the sub action.
149
	if (isset($subActions[$context['listing_by']]))
150
		call_helper($subActions[$context['listing_by']][1]);
151
152
	else
153
		call_helper($subActions['all'][1]);
154
}
155
156
/**
157
 * List all members, page by page, with sorting.
158
 * Called from MemberList().
159
 * Can be passed a sort parameter, to order the display of members.
160
 * Calls printMemberListRows to retrieve the results of the query.
161
 */
162
function MLAll()
163
{
164
	global $txt, $scripturl;
165
	global $modSettings, $context, $smcFunc;
166
167
	// The chunk size for the cached index.
168
	$cache_step_size = 500;
169
170
	// Only use caching if:
171
	// 1. there are at least 2k members,
172
	// 2. the default sorting method (real_name) is being used,
173
	// 3. the page shown is high enough to make a DB filesort unprofitable.
174
	$use_cache = $modSettings['totalMembers'] > 2000 && (!isset($_REQUEST['sort']) || $_REQUEST['sort'] === 'real_name') && isset($_REQUEST['start']) && $_REQUEST['start'] > $cache_step_size;
175
176
	if ($use_cache)
177
	{
178
		// Maybe there's something cached already.
179
		if (!empty($modSettings['memberlist_cache']))
180
			$memberlist_cache = $smcFunc['json_decode']($modSettings['memberlist_cache'], true);
181
182
		// The chunk size for the cached index.
183
		$cache_step_size = 500;
184
185
		// Only update the cache if something changed or no cache existed yet.
186
		if (empty($memberlist_cache) || empty($modSettings['memberlist_updated']) || $memberlist_cache['last_update'] < $modSettings['memberlist_updated'])
187
		{
188
			$request = $smcFunc['db_query']('', '
189
				SELECT real_name
190
				FROM {db_prefix}members
191
				WHERE is_activated = {int:is_activated}
192
				ORDER BY real_name',
193
				array(
194
					'is_activated' => 1,
195
				)
196
			);
197
198
			$memberlist_cache = array(
199
				'last_update' => time(),
200
				'num_members' => $smcFunc['db_num_rows']($request),
201
				'index' => array(),
202
			);
203
204
			for ($i = 0, $n = $smcFunc['db_num_rows']($request); $i < $n; $i += $cache_step_size)
205
			{
206
				$smcFunc['db_data_seek']($request, $i);
207
				list($memberlist_cache['index'][$i]) = $smcFunc['db_fetch_row']($request);
208
			}
209
			$smcFunc['db_data_seek']($request, $memberlist_cache['num_members'] - 1);
210
			list ($memberlist_cache['index'][$i]) = $smcFunc['db_fetch_row']($request);
211
			$smcFunc['db_free_result']($request);
212
213
			// Now we've got the cache...store it.
214
			updateSettings(array('memberlist_cache' => $smcFunc['json_encode']($memberlist_cache)));
215
		}
216
217
		$context['num_members'] = $memberlist_cache['num_members'];
218
	}
219
220
	// Without cache we need an extra query to get the amount of members.
221
	else
222
	{
223
		$request = $smcFunc['db_query']('', '
224
			SELECT COUNT(*)
225
			FROM {db_prefix}members
226
			WHERE is_activated = {int:is_activated}',
227
			array(
228
				'is_activated' => 1,
229
			)
230
		);
231
		list ($context['num_members']) = $smcFunc['db_fetch_row']($request);
232
		$smcFunc['db_free_result']($request);
233
	}
234
235
	// Set defaults for sort (real_name) and start. (0)
236
	if (!isset($_REQUEST['sort']) || !isset($context['columns'][$_REQUEST['sort']]))
237
		$_REQUEST['sort'] = 'real_name';
238
239
	if (!is_numeric($_REQUEST['start']))
240
	{
241
		if (preg_match('~^[^\'\\\\/]~' . ($context['utf8'] ? 'u' : ''), $smcFunc['strtolower']($_REQUEST['start']), $match) === 0)
242
			fatal_error('Hacker?', false);
243
244
		$_REQUEST['start'] = $match[0];
245
246
		$request = $smcFunc['db_query']('substring', '
247
			SELECT COUNT(*)
248
			FROM {db_prefix}members
249
			WHERE LOWER(SUBSTRING(real_name, 1, 1)) < {string:first_letter}
250
				AND is_activated = {int:is_activated}',
251
			array(
252
				'is_activated' => 1,
253
				'first_letter' => $_REQUEST['start'],
254
			)
255
		);
256
		list ($_REQUEST['start']) = $smcFunc['db_fetch_row']($request);
257
		$smcFunc['db_free_result']($request);
258
	}
259
260
	$context['letter_links'] = '';
261
	for ($i = 97; $i < 123; $i++)
262
		$context['letter_links'] .= '<a href="' . $scripturl . '?action=mlist;sa=all;start=' . chr($i) . '#letter' . chr($i) . '">' . strtoupper(chr($i)) . '</a> ';
263
264
	// Sort out the column information.
265
	foreach ($context['columns'] as $col => $column_details)
266
	{
267
		$context['columns'][$col]['href'] = $scripturl . '?action=mlist;sort=' . $col . ';start=0';
268
269
		if ((!isset($_REQUEST['desc']) && $col == $_REQUEST['sort']) || ($col != $_REQUEST['sort'] && !empty($column_details['default_sort_rev'])))
270
			$context['columns'][$col]['href'] .= ';desc';
271
272
		$context['columns'][$col]['link'] = '<a href="' . $context['columns'][$col]['href'] . '" rel="nofollow">' . $context['columns'][$col]['label'] . '</a>';
273
		$context['columns'][$col]['selected'] = $_REQUEST['sort'] == $col;
274
	}
275
276
	// Are we sorting the results
277
	$context['sort_by'] = $_REQUEST['sort'];
278
	$context['sort_direction'] = !isset($_REQUEST['desc']) ? 'up' : 'down';
279
280
	// Construct the page index.
281
	$context['page_index'] = constructPageIndex($scripturl . '?action=mlist;sort=' . $_REQUEST['sort'] . (isset($_REQUEST['desc']) ? ';desc' : ''), $_REQUEST['start'], $context['num_members'], $modSettings['defaultMaxMembers']);
282
283
	// Send the data to the template.
284
	$context['start'] = $_REQUEST['start'] + 1;
285
	$context['end'] = min($_REQUEST['start'] + $modSettings['defaultMaxMembers'], $context['num_members']);
286
287
	$context['can_moderate_forum'] = allowedTo('moderate_forum');
288
	$context['page_title'] = sprintf($txt['viewing_members'], $context['start'], $context['end']);
289
	$context['linktree'][] = array(
290
		'url' => $scripturl . '?action=mlist;sort=' . $_REQUEST['sort'] . ';start=' . $_REQUEST['start'],
291
		'name' => &$context['page_title'],
292
		'extra_after' => '(' . sprintf($txt['of_total_members'], $context['num_members']) . ')'
293
	);
294
295
	$limit = $_REQUEST['start'];
296
	$query_parameters = array(
297
		'regular_id_group' => 0,
298
		'is_activated' => 1,
299
		'sort' => $context['columns'][$_REQUEST['sort']]['sort'][$context['sort_direction']],
300
		'blank_string' => '',
301
	);
302
303
	// Using cache allows to narrow down the list to be retrieved.
304
	if ($use_cache && $_REQUEST['sort'] === 'real_name' && !isset($_REQUEST['desc']))
305
	{
306
		$first_offset = $_REQUEST['start'] - ($_REQUEST['start'] % $cache_step_size);
307
		$second_offset = ceil(($_REQUEST['start'] + $modSettings['defaultMaxMembers']) / $cache_step_size) * $cache_step_size;
308
309
		$where = 'mem.real_name BETWEEN {string:real_name_low} AND {string:real_name_high}';
310
		$query_parameters['real_name_low'] = $memberlist_cache['index'][$first_offset];
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $memberlist_cache does not seem to be defined for all execution paths leading up to this point.
Loading history...
311
		$query_parameters['real_name_high'] = $memberlist_cache['index'][$second_offset];
312
		$limit -= $first_offset;
313
	}
314
315
	// Reverse sorting is a bit more complicated...
316
	elseif ($use_cache && $_REQUEST['sort'] === 'real_name')
317
	{
318
		$first_offset = floor(($memberlist_cache['num_members'] - $modSettings['defaultMaxMembers'] - $_REQUEST['start']) / $cache_step_size) * $cache_step_size;
319
		if ($first_offset < 0)
320
			$first_offset = 0;
321
		$second_offset = ceil(($memberlist_cache['num_members'] - $_REQUEST['start']) / $cache_step_size) * $cache_step_size;
322
323
		$where = 'mem.real_name BETWEEN {string:real_name_low} AND {string:real_name_high}';
324
		$query_parameters['real_name_low'] = $memberlist_cache['index'][$first_offset];
325
		$query_parameters['real_name_high'] = $memberlist_cache['index'][$second_offset];
326
		$limit = $second_offset - ($memberlist_cache['num_members'] - $_REQUEST['start']) - ($second_offset > $memberlist_cache['num_members'] ? $cache_step_size - ($memberlist_cache['num_members'] % $cache_step_size) : 0);
327
	}
328
329
	$custom_fields_qry = '';
330
	if (!empty($context['custom_profile_fields']['join'][$_REQUEST['sort']]))
331
		$custom_fields_qry = $context['custom_profile_fields']['join'][$_REQUEST['sort']];
332
333
	// Select the members from the database.
334
	$request = $smcFunc['db_query']('', '
335
		SELECT mem.id_member
336
		FROM {db_prefix}members AS mem' . ($_REQUEST['sort'] === 'is_online' ? '
337
			LEFT JOIN {db_prefix}log_online AS lo ON (lo.id_member = mem.id_member)' : '') . ($_REQUEST['sort'] === 'id_group' ? '
338
			LEFT JOIN {db_prefix}membergroups AS mg ON (mg.id_group = CASE WHEN mem.id_group = {int:regular_id_group} THEN mem.id_post_group ELSE mem.id_group END)' : '') . '
339
			' . $custom_fields_qry . '
340
		WHERE mem.is_activated = {int:is_activated}' . (empty($where) ? '' : '
341
			AND ' . $where) . '
342
		ORDER BY {raw:sort}
343
		LIMIT {int:start}, {int:max}',
344
		array_merge($query_parameters, array(
345
			'sort' => $query_parameters['sort'],
346
			'start' => $limit,
347
			'max' => $modSettings['defaultMaxMembers'],
348
		))
349
	);
350
	printMemberListRows($request);
351
	$smcFunc['db_free_result']($request);
352
353
	// Add anchors at the start of each letter.
354
	if ($_REQUEST['sort'] == 'real_name')
355
	{
356
		$last_letter = '';
357
		foreach ($context['members'] as $i => $dummy)
358
		{
359
			$this_letter = $smcFunc['strtolower']($smcFunc['substr']($context['members'][$i]['name'], 0, 1));
360
361
			if ($this_letter != $last_letter && preg_match('~[a-z]~', $this_letter) === 1)
362
			{
363
				$context['members'][$i]['sort_letter'] = $smcFunc['htmlspecialchars']($this_letter);
364
				$last_letter = $this_letter;
365
			}
366
		}
367
	}
368
}
369
370
/**
371
 * Search for members, or display search results.
372
 * - Called by MemberList().
373
 * - If variable 'search' is empty displays search dialog box, using the search sub template.
374
 * - Calls printMemberListRows to retrieve the results of the query.
375
 */
376
function MLSearch()
377
{
378
	global $txt, $scripturl, $context, $modSettings, $smcFunc;
379
380
	$context['page_title'] = $txt['mlist_search'];
381
	$context['can_moderate_forum'] = allowedTo('moderate_forum');
382
383
	// Can they search custom fields?
384
	$request = $smcFunc['db_query']('', '
385
		SELECT col_name, field_name, field_desc
386
		FROM {db_prefix}custom_fields
387
		WHERE active = {int:active}
388
			' . (allowedTo('admin_forum') ? '' : ' AND private < {int:private_level}') . '
389
			AND can_search = {int:can_search}
390
			AND (field_type = {string:field_type_text} OR field_type = {string:field_type_textarea} OR field_type = {string:field_type_select})',
391
		array(
392
			'active' => 1,
393
			'can_search' => 1,
394
			'private_level' => 2,
395
			'field_type_text' => 'text',
396
			'field_type_textarea' => 'textarea',
397
			'field_type_select' => 'select',
398
		)
399
	);
400
	$context['custom_search_fields'] = array();
401
	while ($row = $smcFunc['db_fetch_assoc']($request))
402
		$context['custom_search_fields'][$row['col_name']] = array(
403
			'colname' => $row['col_name'],
404
			'name' => $row['field_name'],
405
			'desc' => $row['field_desc'],
406
		);
407
	$smcFunc['db_free_result']($request);
408
409
	// They're searching..
410
	if (isset($_REQUEST['search']) && isset($_REQUEST['fields']))
411
	{
412
		$_POST['search'] = trim(isset($_GET['search']) ? $_GET['search'] : $_POST['search']);
413
		$_POST['fields'] = isset($_GET['fields']) ? explode(',', $_GET['fields']) : $_POST['fields'];
414
415
		$context['old_search'] = $_REQUEST['search'];
416
		$context['old_search_value'] = urlencode($_REQUEST['search']);
417
418
		// No fields?  Use default...
419
		if (empty($_POST['fields']))
420
			$_POST['fields'] = array('name');
421
422
		// Set defaults for how the results are sorted
423
		if (!isset($_REQUEST['sort']) || !isset($context['columns'][$_REQUEST['sort']]))
424
			$_REQUEST['sort'] = 'real_name';
425
426
		// Build the column link / sort information.
427
		foreach ($context['columns'] as $col => $column_details)
428
		{
429
			$context['columns'][$col]['href'] = $scripturl . '?action=mlist;sa=search;start=0;sort=' . $col;
430
431
			if ((!isset($_REQUEST['desc']) && $col == $_REQUEST['sort']) || ($col != $_REQUEST['sort'] && !empty($column_details['default_sort_rev'])))
432
				$context['columns'][$col]['href'] .= ';desc';
433
434
			if (isset($_POST['search']) && isset($_POST['fields']))
435
				$context['columns'][$col]['href'] .= ';search=' . $_POST['search'] . ';fields=' . implode(',', $_POST['fields']);
436
437
			$context['columns'][$col]['link'] = '<a href="' . $context['columns'][$col]['href'] . '" rel="nofollow">' . $context['columns'][$col]['label'] . '</a>';
438
			$context['columns'][$col]['selected'] = $_REQUEST['sort'] == $col;
439
		}
440
441
		// set up some things for use in the template
442
		$context['sort_direction'] = !isset($_REQUEST['desc']) ? 'up' : 'down';
443
		$context['sort_by'] = $_REQUEST['sort'];
444
445
		$query_parameters = array(
446
			'regular_id_group' => 0,
447
			'is_activated' => 1,
448
			'blank_string' => '',
449
			'search' => '%' . strtr($smcFunc['htmlspecialchars']($_POST['search'], ENT_QUOTES), array('_' => '\\_', '%' => '\\%', '*' => '%')) . '%',
450
			'sort' => $context['columns'][$_REQUEST['sort']]['sort'][$context['sort_direction']],
451
		);
452
453
		// Search for a name
454
		if (in_array('name', $_POST['fields']))
455
		{
456
			$fields = allowedTo('moderate_forum') ? array('member_name', 'real_name') : array('real_name');
457
			$search_fields[] = 'name';
0 ignored issues
show
Comprehensibility Best Practice introduced by
$search_fields was never initialized. Although not strictly required by PHP, it is generally a good practice to add $search_fields = array(); before regardless.
Loading history...
458
		}
459
		else
460
		{
461
			$fields = array();
462
			$search_fields = array();
463
		}
464
465
		// Search for websites.
466
		if (in_array('website', $_POST['fields']))
467
		{
468
			$fields += array(7 => 'website_title', 'website_url');
469
			$search_fields[] = 'website';
470
		}
471
		// Search for groups.
472
		if (in_array('group', $_POST['fields']))
473
		{
474
			$fields += array(9 => 'COALESCE(group_name, {string:blank_string})');
475
			$search_fields[] = 'group';
476
		}
477
		// Search for an email address?
478
		if (in_array('email', $_POST['fields']) && allowedTo('moderate_forum'))
479
		{
480
			$fields += array(2 => 'email_address');
481
			$search_fields[] = 'email';
482
		}
483
484
		if ($smcFunc['db_case_sensitive'])
485
			foreach ($fields as $key => $field)
486
				$fields[$key] = 'LOWER(' . $field . ')';
487
488
		$customJoin = array();
489
		$customCount = 10;
490
491
		// Any custom fields to search for - these being tricky?
492
		foreach ($_POST['fields'] as $field)
493
		{
494
			$row['col_name'] = substr($field, 5);
495
			if (substr($field, 0, 5) == 'cust_' && isset($context['custom_search_fields'][$row['col_name']]))
496
			{
497
				$customJoin[] = 'LEFT JOIN {db_prefix}themes AS t' . $row['col_name'] . ' ON (t' . $row['col_name'] . '.variable = {string:t' . $row['col_name'] . '} AND t' . $row['col_name'] . '.id_theme = 1 AND t' . $row['col_name'] . '.id_member = mem.id_member)';
498
				$query_parameters['t' . $row['col_name']] = $row['col_name'];
499
				$fields += array($customCount++ => 'COALESCE(t' . $row['col_name'] . '.value, {string:blank_string})');
500
				$search_fields[] = $field;
501
			}
502
		}
503
504
		// No search fields? That means you're trying to hack things
505
		if (empty($search_fields))
506
			fatal_lang_error('invalid_search_string', false);
507
508
		$query = $_POST['search'] == '' ? '= {string:blank_string}' : ($smcFunc['db_case_sensitive'] ? 'LIKE LOWER({string:search})' : 'LIKE {string:search}');
509
510
		$request = $smcFunc['db_query']('', '
511
			SELECT COUNT(*)
512
			FROM {db_prefix}members AS mem
513
				LEFT JOIN {db_prefix}membergroups AS mg ON (mg.id_group = CASE WHEN mem.id_group = {int:regular_id_group} THEN mem.id_post_group ELSE mem.id_group END)' .
514
				(empty($customJoin) ? '' : implode('
515
				', $customJoin)) . '
516
			WHERE (' . implode(' ' . $query . ' OR ', $fields) . ' ' . $query . ')
517
				AND mem.is_activated = {int:is_activated}',
518
			$query_parameters
519
		);
520
		list ($numResults) = $smcFunc['db_fetch_row']($request);
521
		$smcFunc['db_free_result']($request);
522
523
		$context['page_index'] = constructPageIndex($scripturl . '?action=mlist;sa=search;search=' . $_POST['search'] . ';fields=' . implode(',', $_POST['fields']), $_REQUEST['start'], $numResults, $modSettings['defaultMaxMembers']);
524
525
		// Find the members from the database.
526
		$request = $smcFunc['db_query']('', '
527
			SELECT mem.id_member
528
			FROM {db_prefix}members AS mem
529
				LEFT JOIN {db_prefix}log_online AS lo ON (lo.id_member = mem.id_member)
530
				LEFT JOIN {db_prefix}membergroups AS mg ON (mg.id_group = CASE WHEN mem.id_group = {int:regular_id_group} THEN mem.id_post_group ELSE mem.id_group END)' .
531
				(empty($customJoin) ? '' : implode('
532
				', $customJoin)) . '
533
			WHERE (' . implode(' ' . $query . ' OR ', $fields) . ' ' . $query . ')
534
				AND mem.is_activated = {int:is_activated}
535
			ORDER BY {raw:sort}
536
			LIMIT {int:start}, {int:max}',
537
			array_merge($query_parameters, array(
538
				'start' => $_REQUEST['start'],
539
				'max' => $modSettings['defaultMaxMembers'],
540
			))
541
		);
542
		printMemberListRows($request);
543
		$smcFunc['db_free_result']($request);
544
	}
545
	else
546
	{
547
		// These are all the possible fields.
548
		$context['search_fields'] = array(
549
			'name' => $txt['mlist_search_name'],
550
			'email' => $txt['mlist_search_email'],
551
			'website' => $txt['mlist_search_website'],
552
			'group' => $txt['mlist_search_group'],
553
		);
554
555
		// Sorry, but you can't search by email unless you can view emails
556
		if (!allowedTo('moderate_forum'))
557
		{
558
			unset($context['search_fields']['email']);
559
			$context['search_defaults'] = array('name');
560
		}
561
		else
562
		{
563
			$context['search_defaults'] = array('name', 'email');
564
		}
565
566
		foreach ($context['custom_search_fields'] as $field)
567
			$context['search_fields']['cust_' . $field['colname']] = sprintf($txt['mlist_search_by'], $field['name']);
568
569
		$context['sub_template'] = 'search';
570
		$context['old_search'] = isset($_GET['search']) ? $_GET['search'] : (isset($_POST['search']) ? $smcFunc['htmlspecialchars']($_POST['search']) : '');
571
572
		// Since we're nice we also want to default focus on to the search field.
573
		addInlineJavaScript('
574
	$(\'input[name="search"]\').focus();', true);
575
	}
576
577
	$context['linktree'][] = array(
578
		'url' => $scripturl . '?action=mlist;sa=search',
579
		'name' => &$context['page_title']
580
	);
581
582
	// Highlight the correct button, too!
583
	unset($context['memberlist_buttons']['view_all_members']['active']);
584
	$context['memberlist_buttons']['mlist_search']['active'] = true;
585
}
586
587
/**
588
 * Retrieves results of the request passed to it
589
 * Puts results of request into the context for the sub template.
590
 *
591
 * @param resource $request An SQL result resource
592
 */
593
function printMemberListRows($request)
594
{
595
	global $context, $memberContext, $smcFunc, $txt;
596
	global $scripturl, $settings;
597
598
	// Get the most posts.
599
	$result = $smcFunc['db_query']('', '
600
		SELECT MAX(posts)
601
		FROM {db_prefix}members',
602
		array(
603
		)
604
	);
605
	list ($most_posts) = $smcFunc['db_fetch_row']($result);
606
	$smcFunc['db_free_result']($result);
607
608
	// Avoid division by zero...
609
	if ($most_posts == 0)
610
		$most_posts = 1;
611
612
	$members = array();
613
	while ($row = $smcFunc['db_fetch_assoc']($request))
614
		$members[] = $row['id_member'];
615
616
	// Load all the members for display.
617
	loadMemberData($members);
618
619
	$context['members'] = array();
620
	foreach ($members as $member)
621
	{
622
		if (!loadMemberContext($member))
623
			continue;
624
625
		$context['members'][$member] = $memberContext[$member];
626
		$context['members'][$member]['post_percent'] = round(($context['members'][$member]['real_posts'] * 100) / $most_posts);
627
		$context['members'][$member]['registered_date'] = strftime('%Y-%m-%d', $context['members'][$member]['registered_timestamp']);
628
629
		if (!empty($context['custom_profile_fields']['columns']))
630
		{
631
			foreach ($context['custom_profile_fields']['columns'] as $key => $column)
632
			{
633
				// Don't show anything if there isn't anything to show.
634
				if (!isset($context['members'][$member]['options'][$key]))
635
				{
636
					$context['members'][$member]['options'][$key] = '';
637
					continue;
638
				}
639
640
				$currentKey = 0;
641
				if (!empty($column['options']))
642
				{
643
					$fieldOptions = explode(',', $column['options']);
644
					foreach ($fieldOptions as $k => $v)
645
					{
646
						if (empty($currentKey))
647
							$currentKey = $v === $context['members'][$member]['options'][$key] ? $k : 0;
648
					}
649
				}
650
651
				if ($column['bbc'] && !empty($context['members'][$member]['options'][$key]))
652
					$context['members'][$member]['options'][$key] = strip_tags(parse_bbc($context['members'][$member]['options'][$key]));
653
654
				elseif ($column['type'] == 'check')
655
					$context['members'][$member]['options'][$key] = $context['members'][$member]['options'][$key] == 0 ? $txt['no'] : $txt['yes'];
656
657
				// Enclosing the user input within some other text?
658
				if (!empty($column['enclose']))
659
					$context['members'][$member]['options'][$key] = strtr($column['enclose'], array(
660
						'{SCRIPTURL}' => $scripturl,
661
						'{IMAGES_URL}' => $settings['images_url'],
662
						'{DEFAULT_IMAGES_URL}' => $settings['default_images_url'],
663
						'{INPUT}' => $context['members'][$member]['options'][$key],
664
						'{KEY}' => $currentKey
665
					));
666
			}
667
		}
668
	}
669
}
670
671
/**
672
 * Sets the label, sort and join info for every custom field column.
673
 *
674
 * @return array An array of info about the custom fields for the member list
675
 */
676
function getCustFieldsMList()
677
{
678
	global $smcFunc;
679
680
	$cpf = array();
681
682
	$request = $smcFunc['db_query']('', '
683
		SELECT col_name, field_name, field_desc, field_type, field_options, bbc, enclose
684
		FROM {db_prefix}custom_fields
685
		WHERE active = {int:active}
686
			AND show_mlist = {int:show}
687
			AND private < {int:private_level}',
688
		array(
689
			'active' => 1,
690
			'show' => 1,
691
			'private_level' => 2,
692
		)
693
	);
694
695
	while ($row = $smcFunc['db_fetch_assoc']($request))
696
	{
697
		// Get all the data we're gonna need.
698
		$cpf['columns'][$row['col_name']] = array(
699
			'label' => $row['field_name'],
700
			'type' => $row['field_type'],
701
			'options' => $row['field_options'],
702
			'bbc' => !empty($row['bbc']),
703
			'enclose' => $row['enclose'],
704
		);
705
706
		// Get the right sort method depending on the cust field type.
707
		if ($row['field_type'] != 'check')
708
			$cpf['columns'][$row['col_name']]['sort'] = array(
709
				'down' => 'LENGTH(t' . $row['col_name'] . '.value) > 0 ASC, COALESCE(t' . $row['col_name'] . '.value, \'\') DESC',
710
				'up' => 'LENGTH(t' . $row['col_name'] . '.value) > 0 DESC, COALESCE(t' . $row['col_name'] . '.value, \'\') ASC'
711
			);
712
713
		else
714
			$cpf['columns'][$row['col_name']]['sort'] = array(
715
				'down' => 't' . $row['col_name'] . '.value DESC',
716
				'up' => 't' . $row['col_name'] . '.value ASC'
717
			);
718
719
		$cpf['join'][$row['col_name']] = 'LEFT JOIN {db_prefix}themes AS t' . $row['col_name'] . ' ON (t' . $row['col_name'] . '.variable = {literal:' . $row['col_name'] . '} AND t' . $row['col_name'] . '.id_theme = 1 AND t' . $row['col_name'] . '.id_member = mem.id_member)';
720
	}
721
	$smcFunc['db_free_result']($request);
722
723
	return $cpf;
724
}
725
726
?>