user::main()   F
last analyzed

Complexity

Conditions 147
Paths > 20000

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 692
Code Lines 386

Importance

Changes 0
Metric Value
dl 0
loc 692
rs 2
c 0
b 0
f 0
cc 147
eloc 386
nc 429496.7295
nop 2

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
* This file is part of the VinaBB.vn package.
4
*
5
* @copyright (c) VinaBB <vinabb.vn>
6
* @license GNU General Public License, version 2 (GPL-2.0)
7
*/
8
9
namespace vinabb\web\controllers\user;
10
11
use vinabb\web\includes\constants;
12
13
class user
14
{
15
	/** @var \phpbb\auth\auth */
16
	protected $auth;
17
18
	/** @var \phpbb\config\config */
19
	protected $config;
20
21
	/** @var \phpbb\db\driver\driver_interface */
22
	protected $db;
23
24
	/** @var \phpbb\language\language */
25
	protected $language;
26
27
	/** @var \vinabb\web\controllers\pagination */
28
	protected $pagination;
29
30
	/** @var \phpbb\profilefields\manager */
31
	protected $profile_fields;
32
33
	/** @var \phpbb\request\request */
34
	protected $request;
35
36
	/** @var \phpbb\template\template */
37
	protected $template;
38
39
	/** @var \phpbb\user */
40
	protected $user;
41
42
	/** @var \phpbb\controller\helper */
43
	protected $helper;
44
45
	/** @var \phpbb\group\helper */
46
	protected $group_helper;
47
48
	/** @var string */
49
	protected $root_path;
50
51
	/** @var string */
52
	protected $php_ext;
53
54
	/**
55
	* Constructor
56
	*
57
	* @param \phpbb\auth\auth $auth
58
	* @param \phpbb\config\config $config
59
	* @param \phpbb\db\driver\driver_interface $db
60
	* @param \phpbb\language\language $language
61
	* @param \vinabb\web\controllers\pagination $pagination
62
	* @param \phpbb\profilefields\manager $profile_fields
63
	* @param \phpbb\request\request $request
64
	* @param \phpbb\template\template $template
65
	* @param \phpbb\user $user
66
	* @param \phpbb\controller\helper $helper
67
	* @param \phpbb\group\helper $group_helper
68
	* @param string $root_path
69
	* @param string $php_ext
70
	*/
71
	public function __construct(
72
		\phpbb\auth\auth $auth,
73
		\phpbb\config\config $config,
74
		\phpbb\db\driver\driver_interface $db,
75
		\phpbb\language\language $language,
76
		\vinabb\web\controllers\pagination $pagination,
77
		\phpbb\profilefields\manager $profile_fields,
78
		\phpbb\request\request $request,
79
		\phpbb\template\template $template,
80
		\phpbb\user $user,
81
		\phpbb\controller\helper $helper,
82
		\phpbb\group\helper $group_helper,
83
		$root_path,
84
		$php_ext
85
	)
86
	{
87
		$this->auth = $auth;
88
		$this->config = $config;
89
		$this->db = $db;
90
		$this->language = $language;
91
		$this->pagination = $pagination;
92
		$this->profile_fields = $profile_fields;
93
		$this->request = $request;
94
		$this->template = $template;
95
		$this->user = $user;
96
		$this->helper = $helper;
97
		$this->group_helper = $group_helper;
98
		$this->root_path = $root_path;
99
		$this->php_ext = $php_ext;
100
	}
101
102
	public function main($mode, $page)
103
	{
104
		include "{$this->root_path}includes/functions_display.{$this->php_ext}";
105
106
		// Language
107
		$this->language->add_lang(['memberlist', 'groups']);
108
109
		// Setting a variable to let the style designer know where he is...
110
		$this->template->assign_var('S_IN_MEMBERLIST', true);
111
112
		$page = max(1, floor(str_replace(constants::REWRITE_URL_PAGE, '', $page)));
113
114
		// User types
115
		$user_types = [USER_NORMAL, USER_FOUNDER];
116
117
		if ($this->auth->acl_get('a_user'))
118
		{
119
			$user_types[] = USER_INACTIVE;
120
		}
121
122
		$start = floor(($page - 1) * constants::USERS_PER_PAGE);
123
		$submit = $this->request->is_set_post('submit');
124
		$group_id = $this->request->variable('g', 0);
125
126
		$default_key = 'c';
127
		$sort_key = $this->request->variable('sk', $default_key);
128
		$sort_dir = $this->request->variable('sd', 'a');
129
130
		// Sorting
131
		$sort_key_text = ['a' => $this->user->lang['SORT_USERNAME'], 'c' => $this->user->lang['SORT_JOINED'], 'd' => $this->user->lang['SORT_POST_COUNT']];
132
		$sort_key_sql = ['a' => 'u.username_clean', 'c' => 'u.user_regdate', 'd' => 'u.user_posts'];
133
134
		if ($this->config['jab_enable'])
135
		{
136
			$sort_key_text['k'] = $this->user->lang['JABBER'];
137
			$sort_key_sql['k'] = 'u.user_jabber';
138
		}
139
140
		if ($this->auth->acl_get('a_user'))
141
		{
142
			$sort_key_text['e'] = $this->user->lang['SORT_EMAIL'];
143
			$sort_key_sql['e'] = 'u.user_email';
144
		}
145
146
		if ($this->auth->acl_get('u_viewonline'))
147
		{
148
			$sort_key_text['l'] = $this->user->lang['SORT_LAST_ACTIVE'];
149
			$sort_key_sql['l'] = 'u.user_lastvisit';
150
		}
151
152
		$sort_key_text['m'] = $this->user->lang['SORT_RANK'];
153
		$sort_key_sql['m'] = 'u.user_rank';
154
155
		$sort_dir_text = ['a' => $this->user->lang['ASCENDING'], 'd' => $this->user->lang['DESCENDING']];
156
157
		$s_sort_key = '';
158
		foreach ($sort_key_text as $key => $value)
159
		{
160
			$selected = ($sort_key == $key) ? ' selected="selected"' : '';
161
			$s_sort_key .= '<option value="' . $key . '"' . $selected . '>' . $value . '</option>';
162
		}
163
164
		$s_sort_dir = '';
165
		foreach ($sort_dir_text as $key => $value)
166
		{
167
			$selected = ($sort_dir == $key) ? ' selected="selected"' : '';
168
			$s_sort_dir .= '<option value="' . $key . '"' . $selected . '>' . $value . '</option>';
169
		}
170
171
		// Additional sorting options for user search ... if search is enabled, if not
172
		// then only admins can make use of this (for ACP functionality)
173
		$sql_select = $sql_where_data = $sql_from = $sql_where = $order_by = '';
174
175
		$form			= $this->request->variable('form', '');
176
		$field			= $this->request->variable('field', '');
177
		$select_single 	= $this->request->variable('select_single', false);
178
179
		// Search URL parameters, if any of these are in the URL we do a search
180
		$search_params = ['username', 'email', 'jabber', 'search_group_id', 'joined_select', 'active_select', 'count_select', 'joined', 'active', 'count', 'ip'];
181
182
		// We validate form and field here, only id/class allowed
183
		$form = (!preg_match('/^[a-z0-9_-]+$/i', $form)) ? '' : $form;
184
		$field = (!preg_match('/^[a-z0-9_-]+$/i', $field)) ? '' : $field;
185
		if ((($mode == '' || $mode == 'search') || !empty(array_intersect($this->request->variable_names(\phpbb\request\request_interface::GET), $search_params)) > 0) && ($this->config['load_search'] || $this->auth->acl_get('a_')))
186
		{
187
			$username	= $this->request->variable('username', '', true);
188
			$email		= strtolower($this->request->variable('email', ''));
189
			$jabber		= $this->request->variable('jabber', '');
190
			$search_group_id	= $this->request->variable('search_group_id', 0);
191
192
			// when using these, make sure that we actually have values defined in $find_key_match
193
			$joined_select	= $this->request->variable('joined_select', 'lt');
194
			$active_select	= $this->request->variable('active_select', 'lt');
195
			$count_select	= $this->request->variable('count_select', 'eq');
196
197
			$joined			= explode('-', $this->request->variable('joined', ''));
198
			$active			= explode('-', $this->request->variable('active', ''));
199
			$count			= ($this->request->variable('count', '') !== '') ? $this->request->variable('count', 0) : '';
200
			$ipdomain		= $this->request->variable('ip', '');
201
202
			$find_key_match = ['lt' => '<', 'gt' => '>', 'eq' => '='];
203
204
			$find_count = ['lt' => $this->user->lang['LESS_THAN'], 'eq' => $this->user->lang['EQUAL_TO'], 'gt' => $this->user->lang['MORE_THAN']];
205
			$s_find_count = '';
206
			foreach ($find_count as $key => $value)
207
			{
208
				$selected = ($count_select == $key) ? ' selected="selected"' : '';
209
				$s_find_count .= '<option value="' . $key . '"' . $selected . '>' . $value . '</option>';
210
			}
211
212
			$find_time = ['lt' => $this->user->lang['BEFORE'], 'gt' => $this->user->lang['AFTER']];
213
			$s_find_join_time = '';
214
			foreach ($find_time as $key => $value)
215
			{
216
				$selected = ($joined_select == $key) ? ' selected="selected"' : '';
217
				$s_find_join_time .= '<option value="' . $key . '"' . $selected . '>' . $value . '</option>';
218
			}
219
220
			$s_find_active_time = '';
221
			foreach ($find_time as $key => $value)
222
			{
223
				$selected = ($active_select == $key) ? ' selected="selected"' : '';
224
				$s_find_active_time .= '<option value="' . $key . '"' . $selected . '>' . $value . '</option>';
225
			}
226
227
			$sql_where .= ($username) ? ' AND u.username_clean ' . $this->db->sql_like_expression(str_replace('*', $this->db->get_any_char(), utf8_clean_string($username))) : '';
228
			$sql_where .= ($this->auth->acl_get('a_user') && $email) ? ' AND u.user_email ' . $this->db->sql_like_expression(str_replace('*', $this->db->get_any_char(), $email)) . ' ' : '';
229
			$sql_where .= ($jabber) ? ' AND u.user_jabber ' . $this->db->sql_like_expression(str_replace('*', $this->db->get_any_char(), $jabber)) . ' ' : '';
230
			$sql_where .= (is_numeric($count) && isset($find_key_match[$count_select])) ? ' AND u.user_posts ' . $find_key_match[$count_select] . ' ' . (int) $count . ' ' : '';
231
232
			if (isset($find_key_match[$joined_select]) && count($joined) == 3)
233
			{
234
				$joined_time = gmmktime(0, 0, 0, (int) $joined[1], (int) $joined[2], (int) $joined[0]);
235
236
				if ($joined_time !== false)
237
				{
238
					$sql_where .= " AND u.user_regdate " . $find_key_match[$joined_select] . ' ' . $joined_time;
239
				}
240
			}
241
242
			if (isset($find_key_match[$active_select]) && count($active) == 3 && $this->auth->acl_get('u_viewonline'))
243
			{
244
				$active_time = gmmktime(0, 0, 0, (int) $active[1], (int) $active[2], (int) $active[0]);
245
246
				if ($active_time !== false)
247
				{
248
					$sql_where .= " AND u.user_lastvisit " . $find_key_match[$active_select] . ' ' . $active_time;
249
				}
250
			}
251
252
			$sql_where .= ($search_group_id) ? " AND u.user_id = ug.user_id AND ug.group_id = $search_group_id AND ug.user_pending = 0 " : '';
253
254
			if ($search_group_id)
255
			{
256
				$sql_from = ', ' . USER_GROUP_TABLE . ' ug ';
257
			}
258
259
			if ($ipdomain && $this->auth->acl_getf_global('m_info'))
260
			{
261
				if (strspn($ipdomain, 'abcdefghijklmnopqrstuvwxyz'))
262
				{
263
					$hostnames = gethostbynamel($ipdomain);
264
265
					if ($hostnames !== false)
266
					{
267
						$ips = "'" . implode('\', \'', array_map([$this->db, 'sql_escape'], preg_replace('#([0-9]{1,3}\.[0-9]{1,3}[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})#', "\\1", gethostbynamel($ipdomain)))) . "'";
268
					}
269
					else
270
					{
271
						$ips = false;
272
					}
273
				}
274
				else
275
				{
276
					$ips = "'" . str_replace('*', '%', $this->db->sql_escape($ipdomain)) . "'";
277
				}
278
279
				if ($ips === false)
280
				{
281
					// A minor fudge but it does the job :D
282
					$sql_where .= " AND u.user_id = 0";
283
				}
284
				else
285
				{
286
					$ip_forums = array_keys($this->auth->acl_getf('m_info', true));
287
288
					$sql = 'SELECT DISTINCT poster_id
289
						FROM ' . POSTS_TABLE . '
290
						WHERE poster_ip ' . ((strpos($ips, '%') !== false) ? 'LIKE' : 'IN') . " ($ips)
291
							AND " . $this->db->sql_in_set('forum_id', $ip_forums);
292
					$result = $this->db->sql_query($sql);
293
294
					if ($row = $this->db->sql_fetchrow($result))
295
					{
296
						$ip_sql = [];
297
						do
298
						{
299
							$ip_sql[] = $row['poster_id'];
300
						}
301
						while ($row = $this->db->sql_fetchrow($result));
302
303
						$sql_where .= ' AND ' . $this->db->sql_in_set('u.user_id', $ip_sql);
304
					}
305
					else
306
					{
307
						// A minor fudge but it does the job :D
308
						$sql_where .= " AND u.user_id = 0";
309
					}
310
					unset($ip_forums);
311
312
					$this->db->sql_freeresult($result);
313
				}
314
			}
315
		}
316
317
		$first_char = $this->request->variable('first_char', '');
318
319
		if ($first_char == 'other')
320
		{
321
			for ($i = 97; $i < 123; $i++)
322
			{
323
				$sql_where .= ' AND u.username_clean NOT ' . $this->db->sql_like_expression(chr($i) . $this->db->get_any_char());
324
			}
325
		}
326
		else if ($first_char)
327
		{
328
			$sql_where .= ' AND u.username_clean ' . $this->db->sql_like_expression(substr($first_char, 0, 1) . $this->db->get_any_char());
329
		}
330
331
		// Are we looking at a usergroup? If so, fetch additional info
332
		// and further restrict the user info query
333
		if ($mode == 'group')
334
		{
335
			// We JOIN here to save a query for determining membership for hidden groups. ;)
336
			$sql = 'SELECT g.*, ug.user_id, ug.group_leader
337
				FROM ' . GROUPS_TABLE . ' g
338
				LEFT JOIN ' . USER_GROUP_TABLE . ' ug ON (ug.user_pending = 0 AND ug.user_id = ' . $this->user->data['user_id'] . " AND ug.group_id = $group_id)
339
				WHERE g.group_id = $group_id";
340
			$result = $this->db->sql_query($sql);
341
			$group_row = $this->db->sql_fetchrow($result);
342
			$this->db->sql_freeresult($result);
343
344
			if (!$group_row)
345
			{
346
				trigger_error('NO_GROUP');
347
			}
348
349
			switch ($group_row['group_type'])
350
			{
351
				case GROUP_OPEN:
352
					$group_row['l_group_type'] = 'OPEN';
353
					break;
354
355
				case GROUP_CLOSED:
356
					$group_row['l_group_type'] = 'CLOSED';
357
					break;
358
359
				case GROUP_HIDDEN:
360
					$group_row['l_group_type'] = 'HIDDEN';
361
362
					// Check for membership or special permissions
363
					if (!$this->auth->acl_gets('a_group', 'a_groupadd', 'a_groupdel') && $group_row['user_id'] != $this->user->data['user_id'])
364
					{
365
						trigger_error('NO_GROUP');
366
					}
367
					break;
368
369
				case GROUP_SPECIAL:
370
					$group_row['l_group_type'] = 'SPECIAL';
371
					break;
372
373
				case GROUP_FREE:
374
					$group_row['l_group_type'] = 'FREE';
375
					break;
376
			}
377
378
			$avatar_img = phpbb_get_group_avatar($group_row);
379
380
			// ... same for group rank
381
			$user_rank_data = [
382
				'title'		=> null,
383
				'img'		=> null,
384
				'img_src'	=> null
385
			];
386
387
			if ($group_row['group_rank'])
388
			{
389
				$user_rank_data = phpbb_get_user_rank($group_row, false);
390
391
				if ($user_rank_data['img'])
392
				{
393
					$user_rank_data['img'] .= '<br />';
394
				}
395
			}
396
397
			// include modules for manage groups link display or not
398
			// need to ensure the module is active
399
			$can_manage_group = false;
400
401
			if ($this->user->data['is_registered'] && $group_row['group_leader'])
402
			{
403
				if (!class_exists('p_master'))
404
				{
405
					include "{$this->root_path}includes/functions_module.{$this->php_ext}";
406
				}
407
408
				$module = new p_master;
409
				$module->list_modules('ucp');
410
411
				if ($module->is_active('ucp_groups', 'manage'))
412
				{
413
					$can_manage_group = true;
414
				}
415
				unset($module);
416
			}
417
418
			$this->template->assign_vars([
419
					'GROUP_DESC'	=> generate_text_for_display($group_row['group_desc'], $group_row['group_desc_uid'], $group_row['group_desc_bitfield'], $group_row['group_desc_options']),
420
					'GROUP_NAME'	=> $this->group_helper->get_name($group_row['group_name']),
421
					'GROUP_COLOR'	=> $group_row['group_colour'],
422
					'GROUP_TYPE'	=> $this->user->lang['GROUP_IS_' . $group_row['l_group_type']],
423
					'GROUP_RANK'	=> $user_rank_data['title'],
424
425
					'AVATAR_IMG'	=> $avatar_img,
426
					'RANK_IMG'		=> $user_rank_data['img'],
427
					'RANK_IMG_SRC'	=> $user_rank_data['img_src'],
428
429
					'U_PM'			=> ($this->auth->acl_get('u_sendpm') && $this->auth->acl_get('u_masspm_group') && $group_row['group_receive_pm'] && $this->config['allow_privmsg'] && $this->config['allow_mass_pm']) ? append_sid("{$this->root_path}ucp.{$this->php_ext}", 'i=pm&amp;mode=compose&amp;g=' . $group_id) : '',
430
					'U_MANAGE'		=> ($can_manage_group) ? append_sid("{$this->root_path}ucp.{$this->php_ext}", 'i=ucp_groups&amp;mode=manage') : false
431
			]);
432
433
			$sql_select = ', ug.group_leader';
434
			$sql_from = ', ' . USER_GROUP_TABLE . ' ug ';
435
			$order_by = 'ug.group_leader DESC, ';
436
437
			$sql_where .= " AND ug.user_pending = 0 AND u.user_id = ug.user_id AND ug.group_id = $group_id";
438
			$sql_where_data = " AND u.user_id = ug.user_id AND ug.group_id = $group_id";
439
		}
440
441
		// Sorting and order
442
		if (!isset($sort_key_sql[$sort_key]))
443
		{
444
			$sort_key = $default_key;
445
		}
446
447
		$order_by .= $sort_key_sql[$sort_key] . ' ' . (($sort_dir == 'a') ? 'ASC' : 'DESC');
448
449
		// Unfortunately we must do this here for sorting by rank, else the sort order is applied wrongly
450
		if ($sort_key == 'm')
451
		{
452
			$order_by .= ', u.user_posts DESC';
453
		}
454
455
		// Count the users ...
456
		$sql = 'SELECT COUNT(u.user_id) AS total_users
457
			FROM ' . USERS_TABLE . " u$sql_from
458
			WHERE " . $this->db->sql_in_set('u.user_type', $user_types) . "
459
			$sql_where";
460
		$result = $this->db->sql_query($sql);
461
		$total_users = (int) $this->db->sql_fetchfield('total_users');
462
		$this->db->sql_freeresult($result);
463
464
		// Build a relevant pagination_url
465
		$params = $sort_params = [];
466
467
		// We do not use $this->request->variable() here directly to save some calls (not all variables are set)
468
		$check_params = [
469
			'g'					=> ['g', 0],
470
			'sk'				=> ['sk', $default_key],
471
			'sd'				=> ['sd', 'a'],
472
			'form'				=> ['form', ''],
473
			'field'				=> ['field', ''],
474
			'select_single'		=> ['select_single', $select_single],
475
			'username'			=> ['username', '', true],
476
			'email'				=> ['email', ''],
477
			'jabber'			=> ['jabber', ''],
478
			'search_group_id'	=> ['search_group_id', 0],
479
			'joined_select'		=> ['joined_select', 'lt'],
480
			'active_select'		=> ['active_select', 'lt'],
481
			'count_select'		=> ['count_select', 'eq'],
482
			'joined'			=> ['joined', ''],
483
			'active'			=> ['active', ''],
484
			'count'				=> ($this->request->variable('count', '') !== '') ? ['count', 0] : ['count', ''],
485
			'ip'				=> ['ip', ''],
486
			'first_char'		=> ['first_char', '']
487
		];
488
489
		$u_first_char_params = [];
490
		foreach ($check_params as $key => $call)
491
		{
492
			if (!isset($_REQUEST[$key]))
493
			{
494
				continue;
495
			}
496
497
			$param = call_user_func_array([$this->request, 'variable'], $call);
498
			// Encode strings, convert everything else to int in order to prevent empty parameters.
499
			$param = urlencode($key) . '=' . ((is_string($param)) ? urlencode($param) : (int) $param);
500
			$params[] = $param;
501
502
			if ($key != 'first_char')
503
			{
504
				$u_first_char_params[] = $param;
505
			}
506
			if ($key != 'sk' && $key != 'sd')
507
			{
508
				$sort_params[] = $param;
509
			}
510
		}
511
512
		$u_hide_find_member = append_sid("{$this->root_path}memberlist.{$this->php_ext}", "start=$start" . (!empty($params) ? '&amp;' . implode('&amp;', $params) : ''));
513
514
		if ($mode)
515
		{
516
			$params[] = "mode=$mode";
517
			$u_first_char_params[] = "mode=$mode";
518
		}
519
		$sort_params[] = "mode=$mode";
520
521
		$sort_url = append_sid("{$this->root_path}memberlist.{$this->php_ext}", implode('&amp;', $sort_params));
522
523
		unset($search_params, $sort_params);
524
525
		$u_first_char_params = implode('&amp;', $u_first_char_params);
526
		$u_first_char_params .= ($u_first_char_params) ? '&amp;' : '';
527
528
		$first_characters = [];
529
		$first_characters[''] = $this->user->lang['ALL'];
530
		for ($i = 97; $i < 123; $i++)
531
		{
532
			$first_characters[chr($i)] = chr($i - 32);
533
		}
534
		$first_characters['other'] = $this->user->lang['OTHER'];
535
536
		foreach ($first_characters as $char => $desc)
537
		{
538
			$this->template->assign_block_vars('first_char', [
539
				'DESC'			=> $desc,
540
				'VALUE'			=> $char,
541
				'S_SELECTED'	=> $first_char == $char,
542
				'U_SORT'		=> append_sid("{$this->root_path}memberlist.{$this->php_ext}", $u_first_char_params . 'first_char=' . $char) . '#memberlist'
543
			]);
544
		}
545
546
		// Some search user specific data
547
		if (($mode == '' || $mode == 'search') && ($this->config['load_search'] || $this->auth->acl_get('a_')))
548
		{
549
			$group_selected = $this->request->variable('search_group_id', 0);
550
			$s_group_select = '<option value="0"' . ((!$group_selected) ? ' selected="selected"' : '') . '>&nbsp;</option>';
551
			$group_ids = [];
552
553
			if ($this->auth->acl_gets('a_group', 'a_groupadd', 'a_groupdel'))
554
			{
555
				$sql = 'SELECT group_id, group_name, group_type
556
					FROM ' . GROUPS_TABLE;
557
558
				if (!$this->config['coppa_enable'])
559
				{
560
					$sql .= " WHERE group_name <> 'REGISTERED_COPPA'";
561
				}
562
563
				$sql .= ' ORDER BY group_name ASC';
564
			}
565
			else
566
			{
567
				$sql = 'SELECT g.group_id, g.group_name, g.group_type
568
					FROM ' . GROUPS_TABLE . ' g
569
					LEFT JOIN ' . USER_GROUP_TABLE . ' ug
570
						ON (
571
							g.group_id = ug.group_id
572
							AND ug.user_id = ' . $this->user->data['user_id'] . '
573
							AND ug.user_pending = 0
574
						)
575
					WHERE (g.group_type <> ' . GROUP_HIDDEN . ' OR ug.user_id = ' . $this->user->data['user_id'] . ')';
576
577
				if (!$this->config['coppa_enable'])
578
				{
579
					$sql .= " AND g.group_name <> 'REGISTERED_COPPA'";
580
				}
581
582
				$sql .= ' ORDER BY g.group_name';
583
			}
584
			$result = $this->db->sql_query($sql);
585
586
			while ($row = $this->db->sql_fetchrow($result))
587
			{
588
				$group_ids[] = $row['group_id'];
589
				$s_group_select .= '<option value="' . $row['group_id'] . '"' . (($group_selected == $row['group_id']) ? ' selected="selected"' : '') . '>' . $this->group_helper->get_name($row['group_name']) . '</option>';
590
			}
591
			$this->db->sql_freeresult($result);
592
593
			if ($group_selected !== 0 && !in_array($group_selected, $group_ids))
594
			{
595
				trigger_error('NO_GROUP');
596
			}
597
598
			$this->template->assign_vars([
599
				'USERNAME'	=> $username,
600
				'EMAIL'		=> $email,
601
				'JABBER'	=> $jabber,
602
				'JOINED'	=> implode('-', $joined),
603
				'ACTIVE'	=> implode('-', $active),
604
				'COUNT'		=> $count,
605
				'IP'		=> $ipdomain,
606
607
				'S_IP_SEARCH_ALLOWED'	=> $this->auth->acl_getf_global('m_info'),
608
				'S_EMAIL_SEARCH_ALLOWED'=> $this->auth->acl_get('a_user'),
609
				'S_JABBER_ENABLED'		=> $this->config['jab_enable'],
610
				'S_IN_SEARCH_POPUP'		=> $form && $field,
611
				'S_SEARCH_USER'			=> $mode == 'search' || ($mode == '' && $submit),
612
				'S_FORM_NAME'			=> $form,
613
				'S_FIELD_NAME'			=> $field,
614
				'S_SELECT_SINGLE'		=> $select_single,
615
				'S_COUNT_OPTIONS'		=> $s_find_count,
616
				'S_SORT_OPTIONS'		=> $s_sort_key,
617
				'S_JOINED_TIME_OPTIONS'	=> $s_find_join_time,
618
				'S_ACTIVE_TIME_OPTIONS'	=> $s_find_active_time,
619
				'S_GROUP_SELECT'		=> $s_group_select,
620
				'S_USER_SEARCH_ACTION'	=> append_sid("{$this->root_path}memberlist.{$this->php_ext}", "mode=search&amp;form=$form&amp;field=$field")
621
			]);
622
		}
623
624
		$start = $this->pagination->validate_start($start, constants::USERS_PER_PAGE, $total_users);
625
626
		// Get us some users :D
627
		$sql = "SELECT u.user_id
628
			FROM " . USERS_TABLE . " u
629
				$sql_from
630
			WHERE " . $this->db->sql_in_set('u.user_type', $user_types) . "
631
				$sql_where
632
			ORDER BY $order_by";
633
		$result = $this->db->sql_query_limit($sql, constants::USERS_PER_PAGE, $start);
634
635
		$user_list = [];
636
		while ($row = $this->db->sql_fetchrow($result))
637
		{
638
			$user_list[] = (int) $row['user_id'];
639
		}
640
		$this->db->sql_freeresult($result);
641
642
		// Load custom profile fields
643
		if ($this->config['load_cpf_memberlist'])
644
		{
645
			$cp_row = $this->profile_fields->generate_profile_fields_template_headlines('field_show_on_ml');
646
			foreach ($cp_row as $profile_field)
647
			{
648
				$this->template->assign_block_vars('custom_fields', $profile_field);
649
			}
650
		}
651
652
		$leaders_set = false;
653
		// So, did we get any users?
654
		if (!empty($user_list))
655
		{
656
			// Session time?! Session time...
657
			$sql = 'SELECT session_user_id, MAX(session_time) AS session_time
658
				FROM ' . SESSIONS_TABLE . '
659
				WHERE session_time >= ' . (time() - $this->config['session_length']) . '
660
					AND ' . $this->db->sql_in_set('session_user_id', $user_list) . '
661
				GROUP BY session_user_id';
662
			$result = $this->db->sql_query($sql);
663
664
			$session_times = [];
665
			while ($row = $this->db->sql_fetchrow($result))
666
			{
667
				$session_times[$row['session_user_id']] = $row['session_time'];
668
			}
669
			$this->db->sql_freeresult($result);
670
671
			// Do the SQL thang
672
			if ($mode == 'group')
673
			{
674
				$sql = "SELECT u.*
675
						$sql_select
676
					FROM " . USERS_TABLE . " u
677
						$sql_from
678
					WHERE " . $this->db->sql_in_set('u.user_id', $user_list) . "
679
						$sql_where_data";
680
			}
681
			else
682
			{
683
				$sql = 'SELECT *
684
					FROM ' . USERS_TABLE . '
685
					WHERE ' . $this->db->sql_in_set('user_id', $user_list);
686
			}
687
			$result = $this->db->sql_query($sql);
688
689
			$id_cache = [];
690
			while ($row = $this->db->sql_fetchrow($result))
691
			{
692
				$row['session_time'] = (!empty($session_times[$row['user_id']])) ? $session_times[$row['user_id']] : 0;
693
				$row['last_visit'] = (!empty($row['session_time'])) ? $row['session_time'] : $row['user_lastvisit'];
694
695
				$id_cache[$row['user_id']] = $row;
696
			}
697
			$this->db->sql_freeresult($result);
698
699
			// Load custom profile fields
700
			if ($this->config['load_cpf_memberlist'])
701
			{
702
				// Grab all profile fields from users in id cache for later use - similar to the poster cache
703
				$profile_fields_cache = $this->profile_fields->grab_profile_fields_data($user_list);
704
705
				// Filter the fields we don't want to show
706
				foreach ($profile_fields_cache as $user_id => $user_profile_fields)
707
				{
708
					foreach ($user_profile_fields as $field_ident => $profile_field)
709
					{
710
						if (!$profile_field['data']['field_show_on_ml'])
711
						{
712
							unset($profile_fields_cache[$user_id][$field_ident]);
713
						}
714
					}
715
				}
716
			}
717
718
			// If we sort by last active date we need to adjust the id cache due to user_lastvisit not being the last active date...
719
			if ($sort_key == 'l')
720
			{
721
				usort($user_list,  'phpbb_sort_last_active');
722
			}
723
724
			// do we need to display contact fields as such
725
			$use_contact_fields = true;
726
727
			for ($i = 0, $end = count($user_list); $i < $end; ++$i)
728
			{
729
				$user_id = $user_list[$i];
730
				$row = $id_cache[$user_id];
731
				$is_leader = (isset($row['group_leader']) && $row['group_leader']) ? true : false;
732
				$leaders_set = ($leaders_set || $is_leader);
733
734
				$cp_row = [];
735
				if ($this->config['load_cpf_memberlist'])
736
				{
737
					$cp_row = (isset($profile_fields_cache[$user_id])) ? $this->profile_fields->generate_profile_fields_template_data($profile_fields_cache[$user_id], $use_contact_fields) : [];
738
				}
739
740
				$memberrow = array_merge(phpbb_show_profile($row, false, false, false), [
741
					'ROW_NUMBER'		=> $i + ($start + 1),
742
743
					'S_CUSTOM_PROFILE'	=> !empty($cp_row['row']),
744
					'S_GROUP_LEADER'	=> $is_leader,
745
					'S_INACTIVE'		=> $row['user_type'] == USER_INACTIVE
746
				]);
747
748
				if (!empty($cp_row['row']))
749
				{
750
					$memberrow = array_merge($memberrow, $cp_row['row']);
751
				}
752
753
				$this->template->assign_block_vars('memberrow', $memberrow);
754
755
				if (!empty($cp_row['blockrow']))
756
				{
757
					foreach ($cp_row['blockrow'] as $field_data)
758
					{
759
						$this->template->assign_block_vars('memberrow.custom_fields', $field_data);
760
					}
761
				}
762
763
				unset($id_cache[$user_id]);
764
			}
765
		}
766
767
		$this->pagination->generate_template_pagination('vinabb_web_user_list_route', $params, 'pagination', $total_users, constants::USERS_PER_PAGE, $start);
768
769
		// Generate page
770
		$this->template->assign_vars([
771
			'TOTAL_USERS'	=> $this->language->lang('LIST_USERS', (int) $total_users),
772
773
			'U_FIND_MEMBER'			=> ($this->config['load_search'] || $this->auth->acl_get('a_')) ? append_sid("{$this->root_path}memberlist.{$this->php_ext}", 'mode=search' . (($start) ? "&amp;start=$start" : '') . (!empty($params) ? '&amp;' . implode('&amp;', $params) : '')) : '',
774
			'U_HIDE_FIND_MEMBER'	=> ($mode == 'search' || ($mode == '' && $submit)) ? $u_hide_find_member : '',
775
			'U_LIVE_SEARCH'			=> ($this->config['allow_live_searches']) ? $this->helper->route('vinabb_web_user_livesearch_route') : '',
776
			'U_SORT_USERNAME'		=> $sort_url . '&amp;sk=a&amp;sd=' . (($sort_key == 'a' && $sort_dir == 'a') ? 'd' : 'a'),
777
			'U_SORT_JOINED'			=> $sort_url . '&amp;sk=c&amp;sd=' . (($sort_key == 'c' && $sort_dir == 'd') ? 'a' : 'd'),
778
			'U_SORT_POSTS'			=> $sort_url . '&amp;sk=d&amp;sd=' . (($sort_key == 'd' && $sort_dir == 'd') ? 'a' : 'd'),
779
			'U_SORT_EMAIL'			=> $sort_url . '&amp;sk=e&amp;sd=' . (($sort_key == 'e' && $sort_dir == 'd') ? 'a' : 'd'),
780
			'U_SORT_ACTIVE'			=> ($this->auth->acl_get('u_viewonline')) ? $sort_url . '&amp;sk=l&amp;sd=' . (($sort_key == 'l' && $sort_dir == 'd') ? 'a' : 'd') : '',
781
			'U_SORT_RANK'			=> $sort_url . '&amp;sk=m&amp;sd=' . (($sort_key == 'm' && $sort_dir == 'd') ? 'a' : 'd'),
782
			'U_LIST_CHAR'			=> $sort_url . '&amp;sk=a&amp;sd=' . (($sort_key == 'l' && $sort_dir == 'd') ? 'a' : 'd'),
783
784
			'S_SHOW_GROUP'		=> ($mode == 'group') ? true : false,
785
			'S_VIEWONLINE'		=> $this->auth->acl_get('u_viewonline'),
786
			'S_LEADERS_SET'		=> $leaders_set,
787
			'S_MODE_SELECT'		=> $s_sort_key,
788
			'S_ORDER_SELECT'	=> $s_sort_dir,
789
			'S_MODE_ACTION'		=> $this->helper->route('vinabb_web_user_list_route', $params)
790
		]);
791
792
		return $this->helper->render('memberlist_body.html', $this->language->lang('MEMBERLIST'));
793
	}
794
}
795