Test Setup Failed
Push — release-2.1 ( 545d09...f28109 )
by Mathias
16:19 queued 10s
created

statPanel()   D

Complexity

Conditions 17

Size

Total Lines 208
Code Lines 103

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 17
eloc 103
nop 1
dl 0
loc 208
rs 4.1733
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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

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

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Simple Machines Forum (SMF)
5
 *
6
 * @package SMF
7
 * @author Simple Machines https://www.simplemachines.org
8
 * @copyright 2020 Simple Machines and individual contributors
9
 * @license https://www.simplemachines.org/about/smf/license.php BSD
10
 *
11
 * @version 2.1 RC3
12
 */
13
14
if (!defined('SMF'))
15
	die('No direct access...');
16
17
/**
18
 * View a summary.
19
 *
20
 * @param int $memID The ID of the member
21
 */
22
function summary($memID)
23
{
24
	global $context, $memberContext, $txt, $modSettings, $user_profile, $sourcedir, $scripturl, $smcFunc;
25
26
	// Attempt to load the member's profile data.
27
	if (!loadMemberContext($memID) || !isset($memberContext[$memID]))
28
		fatal_lang_error('not_a_user', false, 404);
0 ignored issues
show
Bug introduced by
404 of type integer is incompatible with the type array expected by parameter $sprintf of fatal_lang_error(). ( Ignorable by Annotation )

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

28
		fatal_lang_error('not_a_user', false, /** @scrutinizer ignore-type */ 404);
Loading history...
29
30
	// Set up the stuff and load the user.
31
	$context += array(
32
		'page_title' => sprintf($txt['profile_of_username'], $memberContext[$memID]['name']),
33
		'can_send_pm' => allowedTo('pm_send'),
34
		'can_have_buddy' => allowedTo('profile_extra_own') && !empty($modSettings['enable_buddylist']),
35
		'can_issue_warning' => allowedTo('issue_warning') && $modSettings['warning_settings'][0] == 1,
36
		'can_view_warning' => (allowedTo('moderate_forum') || allowedTo('issue_warning') || allowedTo('view_warning_any') || ($context['user']['is_owner'] && allowedTo('view_warning_own')) && $modSettings['warning_settings'][0] === 1)
37
	);
38
	$context['member'] = &$memberContext[$memID];
39
40
	// Set a canonical URL for this page.
41
	$context['canonical_url'] = $scripturl . '?action=profile;u=' . $memID;
42
43
	// Are there things we don't show?
44
	$context['disabled_fields'] = isset($modSettings['disabled_profile_fields']) ? array_flip(explode(',', $modSettings['disabled_profile_fields'])) : array();
45
	// Menu tab
46
	$context[$context['profile_menu_name']]['tab_data'] = array(
47
		'title' => $txt['summary'],
48
		'icon' => 'profile_hd.png'
49
	);
50
51
	// See if they have broken any warning levels...
52
	list ($modSettings['warning_enable'], $modSettings['user_limit']) = explode(',', $modSettings['warning_settings']);
53
	if (!empty($modSettings['warning_mute']) && $modSettings['warning_mute'] <= $context['member']['warning'])
54
		$context['warning_status'] = $txt['profile_warning_is_muted'];
55
	elseif (!empty($modSettings['warning_moderate']) && $modSettings['warning_moderate'] <= $context['member']['warning'])
56
		$context['warning_status'] = $txt['profile_warning_is_moderation'];
57
	elseif (!empty($modSettings['warning_watch']) && $modSettings['warning_watch'] <= $context['member']['warning'])
58
		$context['warning_status'] = $txt['profile_warning_is_watch'];
59
60
	// They haven't even been registered for a full day!?
61
	$days_registered = (int) ((time() - $user_profile[$memID]['date_registered']) / (3600 * 24));
62
	if (empty($user_profile[$memID]['date_registered']) || $days_registered < 1)
63
		$context['member']['posts_per_day'] = $txt['not_applicable'];
64
	else
65
		$context['member']['posts_per_day'] = comma_format($context['member']['real_posts'] / $days_registered, 3);
66
67
	// Set the age...
68
	if (empty($context['member']['birth_date']) || substr($context['member']['birth_date'], 0, 4) < 1002)
69
	{
70
		$context['member'] += array(
71
			'age' => $txt['not_applicable'],
72
			'today_is_birthday' => false
73
		);
74
	}
75
	else
76
	{
77
		list ($birth_year, $birth_month, $birth_day) = sscanf($context['member']['birth_date'], '%d-%d-%d');
78
		$datearray = getdate(forum_time());
79
		$context['member'] += array(
80
			'age' => $birth_year <= 1004 ? $txt['not_applicable'] : $datearray['year'] - $birth_year - (($datearray['mon'] > $birth_month || ($datearray['mon'] == $birth_month && $datearray['mday'] >= $birth_day)) ? 0 : 1),
81
			'today_is_birthday' => $datearray['mon'] == $birth_month && $datearray['mday'] == $birth_day && $birth_year > 1004
82
		);
83
	}
84
85
	if (allowedTo('moderate_forum'))
86
	{
87
		// Make sure it's a valid ip address; otherwise, don't bother...
88
		if (preg_match('/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/', $memberContext[$memID]['ip']) == 1 && empty($modSettings['disableHostnameLookup']))
89
			$context['member']['hostname'] = host_from_ip($memberContext[$memID]['ip']);
90
		else
91
			$context['member']['hostname'] = '';
92
93
		$context['can_see_ip'] = true;
94
	}
95
	else
96
		$context['can_see_ip'] = false;
97
98
	// Are they hidden?
99
	$context['member']['is_hidden'] = empty($user_profile[$memID]['show_online']);
100
	$context['member']['show_last_login'] = allowedTo('admin_forum') || !$context['member']['is_hidden'];
101
102
	if (!empty($modSettings['who_enabled']) && $context['member']['show_last_login'])
103
	{
104
		include_once($sourcedir . '/Who.php');
105
		$action = determineActions($user_profile[$memID]['url']);
106
107
		if ($action !== false)
0 ignored issues
show
introduced by
The condition $action !== false is always true.
Loading history...
108
			$context['member']['action'] = $action;
109
	}
110
111
	// If the user is awaiting activation, and the viewer has permission - setup some activation context messages.
112
	if ($context['member']['is_activated'] % 10 != 1 && allowedTo('moderate_forum'))
113
	{
114
		$context['activate_type'] = $context['member']['is_activated'];
115
		// What should the link text be?
116
		$context['activate_link_text'] = in_array($context['member']['is_activated'], array(3, 4, 5, 13, 14, 15)) ? $txt['account_approve'] : $txt['account_activate'];
117
118
		// Should we show a custom message?
119
		$context['activate_message'] = isset($txt['account_activate_method_' . $context['member']['is_activated'] % 10]) ? $txt['account_activate_method_' . $context['member']['is_activated'] % 10] : $txt['account_not_activated'];
120
121
		// If they can be approved, we need to set up a token for them.
122
		$context['token_check'] = 'profile-aa' . $memID;
123
		createToken($context['token_check'], 'get');
124
125
		// Puerile comment
126
		$context['activate_link'] = $scripturl . '?action=admin;area=viewmembers;sa=browse;type=approve;' . $context['session_var'] . '=' . $context['session_id'] . ';' . $context[$context['token_check'] . '_token_var'] . '=' . $context[$context['token_check'] . '_token'];
127
	}
128
129
	// Is the signature even enabled on this forum?
130
	$context['signature_enabled'] = substr($modSettings['signature_settings'], 0, 1) == 1;
131
132
	// Prevent signature images from going outside the box.
133
	if ($context['signature_enabled'])
134
	{
135
		list ($sig_limits, $sig_bbc) = explode(':', $modSettings['signature_settings']);
136
		$sig_limits = explode(',', $sig_limits);
137
138
		if (!empty($sig_limits[5]) || !empty($sig_limits[6]))
139
			addInlineCss('
140
	.signature img { ' . (!empty($sig_limits[5]) ? 'max-width: ' . (int) $sig_limits[5] . 'px; ' : '') . (!empty($sig_limits[6]) ? 'max-height: ' . (int) $sig_limits[6] . 'px; ' : '') . '}');
141
	}
142
143
	// How about, are they banned?
144
	$context['member']['bans'] = array();
145
	if (allowedTo('moderate_forum'))
146
	{
147
		// Can they edit the ban?
148
		$context['can_edit_ban'] = allowedTo('manage_bans');
149
150
		$ban_query = array();
151
		$ban_query_vars = array(
152
			'time' => time(),
153
		);
154
		$ban_query[] = 'id_member = ' . $context['member']['id'];
155
		$ban_query[] = ' {inet:ip} BETWEEN bi.ip_low and bi.ip_high';
156
		$ban_query_vars['ip'] = $memberContext[$memID]['ip'];
157
		// Do we have a hostname already?
158
		if (!empty($context['member']['hostname']))
159
		{
160
			$ban_query[] = '({string:hostname} LIKE hostname)';
161
			$ban_query_vars['hostname'] = $context['member']['hostname'];
162
		}
163
		// Check their email as well...
164
		if (strlen($context['member']['email']) != 0)
165
		{
166
			$ban_query[] = '({string:email} LIKE bi.email_address)';
167
			$ban_query_vars['email'] = $context['member']['email'];
168
		}
169
170
		// So... are they banned?  Dying to know!
171
		$request = $smcFunc['db_query']('', '
172
			SELECT bg.id_ban_group, bg.name, bg.cannot_access, bg.cannot_post,
173
				bg.cannot_login, bg.reason
174
			FROM {db_prefix}ban_items AS bi
175
				INNER JOIN {db_prefix}ban_groups AS bg ON (bg.id_ban_group = bi.id_ban_group AND (bg.expire_time IS NULL OR bg.expire_time > {int:time}))
176
			WHERE (' . implode(' OR ', $ban_query) . ')',
177
			$ban_query_vars
178
		);
179
		while ($row = $smcFunc['db_fetch_assoc']($request))
180
		{
181
			// Work out what restrictions we actually have.
182
			$ban_restrictions = array();
183
			foreach (array('access', 'login', 'post') as $type)
184
				if ($row['cannot_' . $type])
185
					$ban_restrictions[] = $txt['ban_type_' . $type];
186
187
			// No actual ban in place?
188
			if (empty($ban_restrictions))
189
				continue;
190
191
			// Prepare the link for context.
192
			$ban_explanation = sprintf($txt['user_cannot_due_to'], implode(', ', $ban_restrictions), '<a href="' . $scripturl . '?action=admin;area=ban;sa=edit;bg=' . $row['id_ban_group'] . '">' . $row['name'] . '</a>');
193
194
			$context['member']['bans'][$row['id_ban_group']] = array(
195
				'reason' => empty($row['reason']) ? '' : '<br><br><strong>' . $txt['ban_reason'] . ':</strong> ' . $row['reason'],
196
				'cannot' => array(
197
					'access' => !empty($row['cannot_access']),
198
					'post' => !empty($row['cannot_post']),
199
					'login' => !empty($row['cannot_login']),
200
				),
201
				'explanation' => $ban_explanation,
202
			);
203
		}
204
		$smcFunc['db_free_result']($request);
205
	}
206
	loadCustomFields($memID);
207
208
	$context['print_custom_fields'] = array();
209
210
	// Any custom profile fields?
211
	if (!empty($context['custom_fields']))
212
		foreach ($context['custom_fields'] as $custom)
213
			$context['print_custom_fields'][$context['cust_profile_fields_placement'][$custom['placement']]][] = $custom;
214
215
}
216
217
/**
218
 * Fetch the alerts a member currently has.
219
 *
220
 * @param int $memID The ID of the member.
221
 * @param mixed $to_fetch Alerts to fetch: true/false for all/unread, or a list of one or more IDs.
222
 * @param array $limit Maximum number of alerts to fetch (0 for no limit).
223
 * @param array $offset Number of alerts to skip for pagination. Ignored if $to_fetch is a list of IDs.
224
 * @param bool $with_avatar Whether to load the avatar of the alert sender.
225
 * @param bool $show_links Whether to show links in the constituent parts of the alert meessage.
226
 * @return array An array of information about the fetched alerts.
227
 */
228
function fetch_alerts($memID, $to_fetch = false, $limit = 0, $offset = 0, $with_avatar = false, $show_links = false)
229
{
230
	global $smcFunc, $txt, $scripturl, $user_info, $user_profile, $modSettings, $context;
231
232
	// Are we being asked for some specific alerts?
233
	$alertIDs = is_bool($to_fetch) ? array() : array_filter(array_map('intval', (array) $to_fetch));
234
235
	// Basic sanitation.
236
	$memID = (int) $memID;
237
	$unread = $to_fetch === false;
238
	$limit = max(0, (int) $limit);
239
	$offset = !empty($alertIDs) ? 0 : max(0, (int) $offset);
240
	$with_avatar = !empty($with_avatar);
241
	$show_links = !empty($show_links);
242
243
	// Arrays we'll need.
244
	$alerts = array();
245
	$senders = array();
246
	$profiles = array();
247
	$profile_alerts = array();
248
	$possible_msgs = array();
249
	$possible_topics = array();
250
251
	// Get the basic alert info.
252
	$request = $smcFunc['db_query']('', '
253
		SELECT a.id_alert, a.alert_time, a.is_read, a.extra,
254
			a.content_type, a.content_id, a.content_action,
255
			mem.id_member AS sender_id, COALESCE(mem.real_name, a.member_name) AS sender_name' . ($with_avatar ? ',
256
			mem.email_address AS sender_email, mem.avatar AS sender_avatar, f.filename AS sender_filename' : '') . '
257
		FROM {db_prefix}user_alerts AS a
258
			LEFT JOIN {db_prefix}members AS mem ON (a.id_member_started = mem.id_member)' . ($with_avatar ? '
259
			LEFT JOIN {db_prefix}attachments AS f ON (mem.id_member = f.id_member)' : '') . '
260
		WHERE a.id_member = {int:id_member}' . ($unread ? '
261
			AND a.is_read = 0' : '') . (!empty($alertIDs) ? '
262
			AND a.id_alert IN ({array_int:alertIDs})' : '') . '
263
		ORDER BY id_alert DESC' . (!empty($limit) ? '
264
		LIMIT {int:limit}' : '') . (!empty($offset) ?'
265
		OFFSET {int:offset}' : ''),
266
		array(
267
			'id_member' => $memID,
268
			'alertIDs' => $alertIDs,
269
			'limit' => $limit,
270
			'offset' => $offset,
271
		)
272
	);
273
	while ($row = $smcFunc['db_fetch_assoc']($request))
274
	{
275
		$id_alert = array_shift($row);
276
		$row['time'] = timeformat($row['alert_time']);
277
		$row['extra'] = !empty($row['extra']) ? $smcFunc['json_decode']($row['extra'], true) : array();
278
		$alerts[$id_alert] = $row;
279
280
		if (!empty($row['sender_email']))
281
		{
282
			$senders[$row['sender_id']] = array(
283
				'email' => $row['sender_email'],
284
				'avatar' => $row['sender_avatar'],
285
				'filename' => $row['sender_filename'],
286
			);
287
		}
288
289
		if ($row['content_type'] == 'profile')
290
		{
291
			$profiles[] = $row['content_id'];
292
			$profile_alerts[] = $id_alert;
293
		}
294
295
		// For these types, we need to check whether they can actually see the content.
296
		if ($row['content_type'] == 'msg')
297
		{
298
			$alerts[$id_alert]['visible'] = false;
299
			$possible_msgs[$id_alert] = $row['content_id'];
300
		}
301
		elseif (in_array($row['content_type'], array('topic', 'board')))
302
		{
303
			$alerts[$id_alert]['visible'] = false;
304
			$possible_topics[$id_alert] = $row['content_id'];
305
		}
306
		// For the rest, they can always see it.
307
		else
308
			$alerts[$id_alert]['visible'] = true;
309
310
		// Are we showing multiple links or one big main link ?
311
		$alerts[$id_alert]['show_links'] = $show_links || (isset($row['extra']['show_links']) && $row['extra']['show_links']);
312
313
		// Set an appropriate icon.
314
		$alerts[$id_alert]['icon'] = set_alert_icon($alerts[$id_alert]);
315
	}
316
	$smcFunc['db_free_result']($request);
317
318
	// Look up member info of anyone we need it for.
319
	if (!empty($profiles))
320
		loadMemberData($profiles, false, 'minimal');
321
322
	// Get the senders' avatars.
323
	if ($with_avatar)
324
	{
325
		foreach ($senders as $sender_id => $sender)
326
			$senders[$sender_id]['avatar'] = set_avatar_data($sender);
327
328
		$context['avatar_url'] = $modSettings['avatar_url'];
329
	}
330
331
	// Now go through and actually make with the text.
332
	loadLanguage('Alerts');
333
334
	// Some sprintf formats for generating links/strings.
335
	// 'required' is an array of keys in $alert['extra'] that should be used to generate the message, ordered to match the sprintf formats.
336
	// 'link' and 'text' are the sprintf formats that will be used when $alert['show_links'] is true or false, respectively.
337
	$formats['msg_msg'] = array(
0 ignored issues
show
Comprehensibility Best Practice introduced by
$formats was never initialized. Although not strictly required by PHP, it is generally a good practice to add $formats = array(); before regardless.
Loading history...
338
		'required' => array('content_subject', 'topic', 'msg'),
339
		'link' => '<a href="{scripturl}?topic=%2$d.msg%3$d#msg%3$d">%1$s</a>',
340
		'text' => '<strong>%1$s</strong>',
341
	);
342
	$formats['topic_msg'] = array(
343
		'required' => array('content_subject', 'topic', 'topic_suffix'),
344
		'link' => '<a href="{scripturl}?topic=%2$d.%3$s">%1$s</a>',
345
		'text' => '<strong>%1$s</strong>',
346
	);
347
	$formats['board_msg'] = array(
348
		'required' => array('board_name', 'board'),
349
		'link' => '<a href="{scripturl}?board=%2$d.0">%1$s</a>',
350
		'text' => '<strong>%1$s</strong>',
351
	);
352
	$formats['profile_msg'] = array(
353
		'required' => array('user_name', 'user_id'),
354
		'link' => '<a href="{scripturl}?action=profile;u=%2$d">%1$s</a>',
355
		'text' => '<strong>%1$s</strong>',
356
	);
357
358
	// Hooks might want to do something snazzy around their own content types - including enforcing permissions if appropriate.
359
	call_integration_hook('integrate_fetch_alerts', array(&$alerts, &$formats));
360
361
	// Substitute $scripturl into the link formats. (Done here to make life easier for hooked mods.)
362
	foreach ($formats as &$format_type)
363
		$format_type = str_replace('{scripturl}', $scripturl, $format_type);
364
365
	// If we need to check board access, use the correct board access filter for the member in question.
366
	if ((!isset($user_info['query_see_board']) || $user_info['id'] != $memID) && (!empty($possible_msgs) || !empty($possible_topics)))
367
		$qb = build_query_board($memID);
368
	else
369
		$qb['query_see_board'] = '{query_see_board}';
0 ignored issues
show
Comprehensibility Best Practice introduced by
$qb was never initialized. Although not strictly required by PHP, it is generally a good practice to add $qb = array(); before regardless.
Loading history...
370
371
	// For anything that needs more info and/or wants us to check board or topic access, let's do that.
372
	if (!empty($possible_msgs))
373
	{
374
		$flipped_msgs = array();
375
		foreach ($possible_msgs as $id_alert => $id_msg)
376
		{
377
			if (!isset($flipped_msgs[$id_msg]))
378
				$flipped_msgs[$id_msg] = array();
379
380
			$flipped_msgs[$id_msg][] = $id_alert;
381
		}
382
383
		$request = $smcFunc['db_query']('', '
384
			SELECT m.id_msg, m.id_topic, m.subject, b.id_board, b.name AS board_name
385
			FROM {db_prefix}messages AS m
386
				INNER JOIN {db_prefix}boards AS b ON (m.id_board = b.id_board)
387
			WHERE ' . $qb['query_see_board'] . '
388
				AND m.id_msg IN ({array_int:msgs})
389
			ORDER BY m.id_msg',
390
			array(
391
				'msgs' => $possible_msgs,
392
			)
393
		);
394
		while ($row = $smcFunc['db_fetch_assoc']($request))
395
		{
396
			foreach ($flipped_msgs[$row['id_msg']] as $id_alert)
397
			{
398
				$alerts[$id_alert]['content_data'] = $row;
399
				$alerts[$id_alert]['visible'] = true;
400
			}
401
		}
402
		$smcFunc['db_free_result']($request);
403
	}
404
	if (!empty($possible_topics))
405
	{
406
		$flipped_topics = array();
407
		foreach ($possible_topics as $id_alert => $id_topic)
408
		{
409
			if (!isset($flipped_topics[$id_topic]))
410
				$flipped_topics[$id_topic] = array();
411
412
			$flipped_topics[$id_topic][] = $id_alert;
413
		}
414
415
		$request = $smcFunc['db_query']('', '
416
			SELECT m.id_msg, t.id_topic, m.subject, b.id_board, b.name AS board_name
417
			FROM {db_prefix}topics AS t
418
				INNER JOIN {db_prefix}messages AS m ON (t.id_first_msg = m.id_msg)
419
				INNER JOIN {db_prefix}boards AS b ON (t.id_board = b.id_board)
420
			WHERE ' . $qb['query_see_board'] . '
421
				AND t.id_topic IN ({array_int:topics})',
422
			array(
423
				'topics' => $possible_topics,
424
			)
425
		);
426
		while ($row = $smcFunc['db_fetch_assoc']($request))
427
		{
428
			foreach ($flipped_topics[$row['id_topic']] as $id_alert)
429
			{
430
				$alerts[$id_alert]['content_data'] = $row;
431
				$alerts[$id_alert]['visible'] = true;
432
			}
433
		}
434
		$smcFunc['db_free_result']($request);
435
	}
436
437
	// Now to go back through the alerts, reattach this extra information and then try to build the string out of it (if a hook didn't already)
438
	foreach ($alerts as $id_alert => $dummy)
439
	{
440
		// There's no point showing alerts for inaccessible content.
441
		if (!$alerts[$id_alert]['visible'])
442
		{
443
			unset($alerts[$id_alert]);
444
			continue;
445
		}
446
		else
447
			unset($alerts[$id_alert]['visible']);
448
449
		// Did a mod already take care of this one?
450
		if (!empty($alerts[$id_alert]['text']))
451
			continue;
452
453
		// For developer convenience.
454
		$alert = &$alerts[$id_alert];
455
456
		// The info in extra might outdated if the topic was moved, the message's subject was changed, etc.
457
		if (!empty($alert['content_data']))
458
		{
459
			$data = $alert['content_data'];
460
461
			// Make sure msg, topic, and board info are correct.
462
			$patterns = array();
463
			$replacements = array();
464
			foreach (array('msg', 'topic', 'board') as $item)
465
			{
466
				if (isset($data['id_' . $item]))
467
				{
468
					$separator = $item == 'msg' ? '=?' : '=';
469
470
					if (isset($alert['extra']['content_link']) && strpos($alert['extra']['content_link'], $item . $separator) !== false && strpos($alert['extra']['content_link'], $item . $separator . $data['id_' . $item]) === false)
471
					{
472
						$patterns[] = '/\b' . $item . $separator . '\d+/';
473
						$replacements[] = $item . $separator . $data['id_' . $item];
474
					}
475
476
					$alert['extra'][$item] = $data['id_' . $item];
477
				}
478
			}
479
			if (!empty($patterns))
480
				$alert['extra']['content_link'] = preg_replace($patterns, $replacements, $alert['extra']['content_link']);
481
482
			// Make sure the subject is correct.
483
			if (isset($data['subject']))
484
				$alert['extra']['content_subject'] = $data['subject'];
485
486
			// Keep track of this so we can use it below.
487
			if (isset($data['board_name']))
488
				$alert['extra']['board_name'] = $data['board_name'];
489
490
			unset($alert['content_data']);
491
		}
492
493
		// Do we want to link to the topic in general or the new messages specifically?
494
		if (isset($possible_topics[$id_alert]) && in_array($alert['content_action'], array('reply', 'topic', 'unapproved_reply')))
495
				$alert['extra']['topic_suffix'] = 'new;topicseen#new';
496
		elseif (isset($alert['extra']['topic']))
497
			$alert['extra']['topic_suffix'] = '0';
498
499
		// Make sure profile alerts have what they need.
500
		if (in_array($id_alert, $profile_alerts))
501
		{
502
			if (empty($alert['extra']['user_id']))
503
				$alert['extra']['user_id'] = $alert['content_id'];
504
505
			if (isset($user_profile[$alert['extra']['user_id']]))
506
				$alert['extra']['user_name'] = $user_profile[$alert['extra']['user_id']]['real_name'];
507
		}
508
509
		// If we loaded the sender's profile, we may as well use it.
510
		$sender_id = !empty($alert['sender_id']) ? $alert['sender_id'] : 0;
511
		if (isset($user_profile[$sender_id]))
512
			$alert['sender_name'] = $user_profile[$sender_id]['real_name'];
513
514
		// If requested, include the sender's avatar data.
515
		if ($with_avatar && !empty($senders[$sender_id]))
516
			$alert['sender'] = $senders[$sender_id];
517
518
		// Next, build the message strings.
519
		foreach ($formats as $msg_type => $format_info)
520
		{
521
			// Get the values to use in the formatted string, in the right order.
522
			$msg_values = array_replace(
523
				array_fill_keys($format_info['required'], ''),
524
				array_intersect_key($alert['extra'], array_flip($format_info['required']))
525
			);
526
527
			// Assuming all required values are present, build the message.
528
			if (!in_array('', $msg_values))
529
				$alert['extra'][$msg_type] = vsprintf($formats[$msg_type][$alert['show_links'] ? 'link' : 'text'], $msg_values);
530
531
			elseif (in_array($msg_type, array('msg_msg', 'topic_msg', 'board_msg')))
532
				$alert['extra'][$msg_type] = $txt[$msg_type == 'board_msg' ? 'board_na' : 'topic_na'];
533
			else
534
				$alert['extra'][$msg_type] = '(' . $txt['not_applicable'] . ')';
535
		}
536
537
		// Show the formatted time in alerts about subscriptions.
538
		if ($alert['content_type'] == 'paidsubs' && isset($alert['extra']['end_time']))
539
		{
540
			// If the subscription already expired, say so.
541
			if ($alert['extra']['end_time'] < time())
542
				$alert['content_action'] = 'expired';
543
544
			// Present a nicely formatted date.
545
			$alert['extra']['end_time'] = timeformat($alert['extra']['end_time']);
546
		}
547
548
		// Now set the main URL that this alert should take the user to.
549
		$alert['target_href'] = '';
550
551
		// Priority goes to explicitly specified links.
552
		if (isset($alert['extra']['content_link']))
553
			$alert['target_href'] = $alert['extra']['content_link'];
554
555
		elseif (isset($alert['extra']['report_link']))
556
			$alert['target_href'] = $scripturl . $alert['extra']['report_link'];
557
558
		// Next, try determining the link based on the content action.
559
		if (empty($alert['target_href']) && in_array($alert['content_action'], array('register_approval', 'group_request', 'buddy_request')))
560
		{
561
			switch ($alert['content_action'])
562
			{
563
				case 'register_approval':
564
					$alert['target_href'] = $scripturl . '?action=admin;area=viewmembers;sa=browse;type=approve';
565
					break;
566
567
				case 'group_request':
568
					$alert['target_href'] = $scripturl . '?action=moderate;area=groups;sa=requests';
569
					break;
570
571
				case 'buddy_request':
572
					if (!empty($alert['id_member_started']))
573
						$alert['target_href'] = $scripturl . '?action=profile;u=' . $alert['id_member_started'];
574
					break;
575
576
				default:
577
					break;
578
			}
579
		}
580
581
		// Or maybe we can determine the link based on the content type.
582
		if (empty($alert['target_href']) && in_array($alert['content_type'], array('msg', 'member', 'event')))
583
		{
584
			switch ($alert['content_type'])
585
			{
586
				case 'msg':
587
					if (!empty($alert['content_id']))
588
						$alert['target_href'] = $scripturl . '?msg=' . $alert['content_id'];
589
					break;
590
591
				case 'member':
592
					if (!empty($alert['id_member_started']))
593
						$alert['target_href'] = $scripturl . '?action=profile;u=' . $alert['id_member_started'];
594
					break;
595
596
				case 'event':
597
					if (!empty($alert['extra']['event_id']))
598
						$alert['target_href'] = $scripturl . '?action=calendar;event=' . $alert['extra']['event_id'];
599
					break;
600
601
				default:
602
					break;
603
			}
604
605
		}
606
607
		// Finally, set this alert's text string.
608
		$string = 'alert_' . $alert['content_type'] . '_' . $alert['content_action'];
609
610
		// This kludge exists because the alert content_types prior to 2.1 RC3 were a bit haphazard.
611
		// This can be removed once all the translated language files have been updated.
612
		if (!isset($txt[$string]))
613
		{
614
			if (strpos($alert['content_action'], 'unapproved_') === 0)
615
				$string = 'alert_' . $alert['content_action'];
616
617
			if ($alert['content_type'] === 'member' && in_array($alert['content_action'], array('report', 'report_reply')))
618
				$string = 'alert_profile_' . $alert['content_action'];
619
620
			if ($alert['content_type'] === 'member' && $alert['content_action'] === 'buddy_request')
621
				$string = 'alert_buddy_' . $alert['content_action'];
622
		}
623
624
		if (isset($txt[$string]))
625
		{
626
			$substitutions = array(
627
				'{scripturl}' => $scripturl,
628
				'{member_link}' => !empty($sender_id) && $alert['show_links'] ? '<a href="' . $scripturl . '?action=profile;u=' . $sender_id . '">' . $alert['sender_name'] . '</a>' : '<strong>' . $alert['sender_name'] . '</strong>',
629
			);
630
631
			if (is_array($alert['extra']))
632
			{
633
				foreach ($alert['extra'] as $k => $v)
634
					$substitutions['{' . $k . '}'] = $v;
635
			}
636
637
			$alert['text'] = strtr($txt[$string], $substitutions);
638
		}
639
640
		// Unset the reference variable to avoid any surprises in subsequent loops.
641
		unset($alert);
642
	}
643
644
	return $alerts;
645
}
646
647
/**
648
 * Shows all alerts for a member
649
 *
650
 * @param int $memID The ID of the member
651
 */
652
function showAlerts($memID)
653
{
654
	global $context, $smcFunc, $txt, $sourcedir, $scripturl, $options;
655
656
	require_once($sourcedir . '/Profile-Modify.php');
657
658
	// Are we opening a specific alert? (i.e.: ?action=profile;area=showalerts;alert=12345)
659
	if (!empty($_REQUEST['alert']))
660
	{
661
		$alert_id = (int) $_REQUEST['alert'];
662
		$alerts = fetch_alerts($memID, $alert_id);
663
		$alert = array_pop($alerts);
664
665
		/*
666
		 * MOD AUTHORS:
667
		 * To control this redirect, use the 'integrate_fetch_alerts' hook to
668
		 * set the value of $alert['extra']['content_link'], which will become
669
		 * the value for $alert['target_href'].
670
		 */
671
672
		// In case it failed to determine this alert's link
673
		if (empty($alert['target_href']))
674
			redirectexit('action=profile;area=showalerts');
675
676
		// Mark the alert as read while we're at it.
677
		alert_mark($memID, $alert_id, 1);
678
679
		// Take the user to the content
680
		redirectexit($alert['target_href']);
681
	}
682
683
	// Prepare the pagination vars.
684
	$maxIndex = 10;
685
	$context['start'] = (int) isset($_REQUEST['start']) ? $_REQUEST['start'] : 0;
686
	$count = alert_count($memID);
687
688
	// Fix invalid 'start' offsets.
689
	if ($context['start'] > $count)
690
		$context['start'] = $count - ($count % $maxIndex);
691
	else
692
		$context['start'] = $context['start'] - ($context['start'] % $maxIndex);
693
694
	// Get the alerts.
695
	$context['alerts'] = fetch_alerts($memID, true, $maxIndex, $context['start'], true, true);
0 ignored issues
show
Bug introduced by
$maxIndex of type integer is incompatible with the type array expected by parameter $limit of fetch_alerts(). ( Ignorable by Annotation )

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

695
	$context['alerts'] = fetch_alerts($memID, true, /** @scrutinizer ignore-type */ $maxIndex, $context['start'], true, true);
Loading history...
696
	$toMark = false;
697
	$action = '';
698
699
	//  Are we using checkboxes?
700
	$context['showCheckboxes'] = !empty($options['display_quick_mod']) && $options['display_quick_mod'] == 1;
701
702
	// Create the pagination.
703
	$context['pagination'] = constructPageIndex($scripturl . '?action=profile;area=showalerts;u=' . $memID, $context['start'], $count, $maxIndex, false);
704
705
	// Set some JavaScript for checking all alerts at once.
706
	if ($context['showCheckboxes'])
707
		addInlineJavaScript('
708
		$(function(){
709
			$(\'#select_all\').on(\'change\', function() {
710
				var checkboxes = $(\'ul.quickbuttons\').find(\':checkbox\');
711
				if($(this).prop(\'checked\')) {
712
					checkboxes.prop(\'checked\', true);
713
				}
714
				else {
715
					checkboxes.prop(\'checked\', false);
716
				}
717
			});
718
		});', true);
719
720
	// The quickbuttons
721
	foreach ($context['alerts'] as $id => $alert)
722
	{
723
		$context['alerts'][$id]['quickbuttons'] = array(
724
			'delete' => array(
725
				'label' => $txt['delete'],
726
				'href' => $scripturl . '?action=profile;u=' . $context['id_member'] . ';area=showalerts;do=remove;aid=' . $id . ';' . $context['session_var'] . '=' . $context['session_id'] . (!empty($context['start']) ? ';start=' . $context['start'] : ''),
727
				'class' => 'you_sure',
728
				'icon' => 'remove_button'
729
			),
730
			'mark' => array(
731
				'label' => $alert['is_read'] != 0 ? $txt['mark_unread'] : $txt['mark_read_short'],
732
				'href' => $scripturl . '?action=profile;u=' . $context['id_member'] . ';area=showalerts;do=' . ($alert['is_read'] != 0 ? 'unread' : 'read') . ';aid=' . $id . ';' . $context['session_var'] . '=' . $context['session_id'] . (!empty($context['start']) ? ';start=' . $context['start'] : ''),
733
				'icon' => $alert['is_read'] != 0 ? 'unread_button' : 'read_button',
734
			),
735
			'view' => array(
736
				'label' => $txt['view'],
737
				'href' => $alert['target_href'],
738
				'icon' => 'move',
739
			),
740
			'quickmod' => array(
741
				'content' => '<input type="checkbox" name="mark[' . $id . ']" value="' . $id . '">',
742
				'show' => $context['showCheckboxes']
743
			)
744
		);
745
	}
746
747
	// The Delete all unread link.
748
	$context['alert_purge_link'] = $scripturl . '?action=profile;u=' . $context['id_member'] . ';area=showalerts;do=purge;' . $context['session_var'] . '=' . $context['session_id'] . (!empty($context['start']) ? ';start=' . $context['start'] : '');
749
750
	// Set a nice message.
751
	if (!empty($_SESSION['update_message']))
752
	{
753
		$context['update_message'] = $txt['profile_updated_own'];
754
		unset($_SESSION['update_message']);
755
	}
756
757
	// Saving multiple changes?
758
	if (isset($_GET['save']) && !empty($_POST['mark']))
759
	{
760
		// Get the values.
761
		$toMark = array_map('intval', (array) $_POST['mark']);
762
763
		// Which action?
764
		$action = !empty($_POST['mark_as']) ? $smcFunc['htmlspecialchars']($smcFunc['htmltrim']($_POST['mark_as'])) : '';
765
	}
766
767
	// A single change.
768
	if (!empty($_GET['do']) && !empty($_GET['aid']))
769
	{
770
		$toMark = (int) $_GET['aid'];
771
		$action = $smcFunc['htmlspecialchars']($smcFunc['htmltrim']($_GET['do']));
772
	}
773
	// Delete all read alerts.
774
	elseif (!empty($_GET['do']) && $_GET['do'] === 'purge')
775
		$action = 'purge';
776
777
	// Save the changes.
778
	if (!empty($action) && (!empty($toMark) || $action === 'purge'))
779
	{
780
		checkSession('request');
781
782
		// Call it!
783
		if ($action == 'remove')
784
			alert_delete($toMark, $memID);
785
786
		elseif ($action == 'purge')
787
			alert_purge($memID);
788
789
		else
790
			alert_mark($memID, $toMark, $action == 'read' ? 1 : 0);
791
792
		// Set a nice update message.
793
		$_SESSION['update_message'] = true;
794
795
		// Redirect.
796
		redirectexit('action=profile;area=showalerts;u=' . $memID . (!empty($context['start']) ? ';start=' . $context['start'] : ''));
797
	}
798
}
799
800
/**
801
 * Show all posts by a member
802
 *
803
 * @todo This function needs to be split up properly.
804
 *
805
 * @param int $memID The ID of the member
806
 */
807
function showPosts($memID)
808
{
809
	global $txt, $user_info, $scripturl, $modSettings, $options;
810
	global $context, $user_profile, $sourcedir, $smcFunc, $board;
811
812
	// Some initial context.
813
	$context['start'] = (int) $_REQUEST['start'];
814
	$context['current_member'] = $memID;
815
816
	// Create the tabs for the template.
817
	$context[$context['profile_menu_name']]['tab_data'] = array(
818
		'title' => $txt['showPosts'],
819
		'description' => $txt['showPosts_help'],
820
		'icon' => 'profile_hd.png',
821
		'tabs' => array(
822
			'messages' => array(
823
			),
824
			'topics' => array(
825
			),
826
			'unwatchedtopics' => array(
827
			),
828
			'attach' => array(
829
			),
830
		),
831
	);
832
833
	// Shortcut used to determine which $txt['show*'] string to use for the title, based on the SA
834
	$title = array(
835
		'attach' => 'Attachments',
836
		'unwatchedtopics' => 'Unwatched',
837
		'topics' => 'Topics'
838
	);
839
840
	// Set the page title
841
	if (isset($_GET['sa']) && array_key_exists($_GET['sa'], $title))
842
		$context['page_title'] = $txt['show' . $title[$_GET['sa']]];
843
	else
844
		$context['page_title'] = $txt['showPosts'];
845
846
	$context['page_title'] .= ' - ' . $user_profile[$memID]['real_name'];
847
848
	// Is the load average too high to allow searching just now?
849
	if (!empty($context['load_average']) && !empty($modSettings['loadavg_show_posts']) && $context['load_average'] >= $modSettings['loadavg_show_posts'])
850
		fatal_lang_error('loadavg_show_posts_disabled', false);
851
852
	// If we're specifically dealing with attachments use that function!
853
	if (isset($_GET['sa']) && $_GET['sa'] == 'attach')
854
		return showAttachments($memID);
855
	// Instead, if we're dealing with unwatched topics (and the feature is enabled) use that other function.
856
	elseif (isset($_GET['sa']) && $_GET['sa'] == 'unwatchedtopics')
857
		return showUnwatched($memID);
0 ignored issues
show
Bug introduced by
Are you sure the usage of showUnwatched($memID) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
858
859
	// Are we just viewing topics?
860
	$context['is_topics'] = isset($_GET['sa']) && $_GET['sa'] == 'topics' ? true : false;
861
862
	// If just deleting a message, do it and then redirect back.
863
	if (isset($_GET['delete']) && !$context['is_topics'])
864
	{
865
		checkSession('get');
866
867
		// We need msg info for logging.
868
		$request = $smcFunc['db_query']('', '
869
			SELECT subject, id_member, id_topic, id_board
870
			FROM {db_prefix}messages
871
			WHERE id_msg = {int:id_msg}',
872
			array(
873
				'id_msg' => (int) $_GET['delete'],
874
			)
875
		);
876
		$info = $smcFunc['db_fetch_row']($request);
877
		$smcFunc['db_free_result']($request);
878
879
		// Trying to remove a message that doesn't exist.
880
		if (empty($info))
881
			redirectexit('action=profile;u=' . $memID . ';area=showposts;start=' . $_GET['start']);
882
883
		// We can be lazy, since removeMessage() will check the permissions for us.
884
		require_once($sourcedir . '/RemoveTopic.php');
885
		removeMessage((int) $_GET['delete']);
886
887
		// Add it to the mod log.
888
		if (allowedTo('delete_any') && (!allowedTo('delete_own') || $info[1] != $user_info['id']))
889
			logAction('delete', array('topic' => $info[2], 'subject' => $info[0], 'member' => $info[1], 'board' => $info[3]));
890
891
		// Back to... where we are now ;).
892
		redirectexit('action=profile;u=' . $memID . ';area=showposts;start=' . $_GET['start']);
893
	}
894
895
	// Default to 10.
896
	if (empty($_REQUEST['viewscount']) || !is_numeric($_REQUEST['viewscount']))
897
		$_REQUEST['viewscount'] = '10';
898
899
	if ($context['is_topics'])
900
		$request = $smcFunc['db_query']('', '
901
			SELECT COUNT(*)
902
			FROM {db_prefix}topics AS t' . '
903
			WHERE {query_see_topic_board}
904
				AND t.id_member_started = {int:current_member}' . (!empty($board) ? '
905
				AND t.id_board = {int:board}' : '') . (!$modSettings['postmod_active'] || $context['user']['is_owner'] ? '' : '
906
				AND t.approved = {int:is_approved}'),
907
			array(
908
				'current_member' => $memID,
909
				'is_approved' => 1,
910
				'board' => $board,
911
			)
912
		);
913
	else
914
		$request = $smcFunc['db_query']('', '
915
			SELECT COUNT(id_msg)
916
			FROM {db_prefix}messages AS m
917
			WHERE {query_see_message_board} AND m.id_member = {int:current_member}' . (!empty($board) ? '
918
				AND m.id_board = {int:board}' : '') . (!$modSettings['postmod_active'] || $context['user']['is_owner'] ? '' : '
919
				AND m.approved = {int:is_approved}'),
920
			array(
921
				'current_member' => $memID,
922
				'is_approved' => 1,
923
				'board' => $board,
924
			)
925
		);
926
	list ($msgCount) = $smcFunc['db_fetch_row']($request);
927
	$smcFunc['db_free_result']($request);
928
929
	$request = $smcFunc['db_query']('', '
930
		SELECT MIN(id_msg), MAX(id_msg)
931
		FROM {db_prefix}messages AS m
932
		WHERE m.id_member = {int:current_member}' . (!empty($board) ? '
933
			AND m.id_board = {int:board}' : '') . (!$modSettings['postmod_active'] || $context['user']['is_owner'] ? '' : '
934
			AND m.approved = {int:is_approved}'),
935
		array(
936
			'current_member' => $memID,
937
			'is_approved' => 1,
938
			'board' => $board,
939
		)
940
	);
941
	list ($min_msg_member, $max_msg_member) = $smcFunc['db_fetch_row']($request);
942
	$smcFunc['db_free_result']($request);
943
944
	$range_limit = '';
945
946
	if ($context['is_topics'])
947
		$maxPerPage = empty($modSettings['disableCustomPerPage']) && !empty($options['topics_per_page']) ? $options['topics_per_page'] : $modSettings['defaultMaxTopics'];
948
	else
949
		$maxPerPage = empty($modSettings['disableCustomPerPage']) && !empty($options['messages_per_page']) ? $options['messages_per_page'] : $modSettings['defaultMaxMessages'];
950
951
	$maxIndex = $maxPerPage;
952
953
	// Make sure the starting place makes sense and construct our friend the page index.
954
	$context['page_index'] = constructPageIndex($scripturl . '?action=profile;u=' . $memID . ';area=showposts' . ($context['is_topics'] ? ';sa=topics' : '') . (!empty($board) ? ';board=' . $board : ''), $context['start'], $msgCount, $maxIndex);
955
	$context['current_page'] = $context['start'] / $maxIndex;
956
957
	// Reverse the query if we're past 50% of the pages for better performance.
958
	$start = $context['start'];
959
	$reverse = $_REQUEST['start'] > $msgCount / 2;
960
	if ($reverse)
961
	{
962
		$maxIndex = $msgCount < $context['start'] + $maxPerPage + 1 && $msgCount > $context['start'] ? $msgCount - $context['start'] : $maxPerPage;
963
		$start = $msgCount < $context['start'] + $maxPerPage + 1 || $msgCount < $context['start'] + $maxPerPage ? 0 : $msgCount - $context['start'] - $maxPerPage;
964
	}
965
966
	// Guess the range of messages to be shown.
967
	if ($msgCount > 1000)
968
	{
969
		$margin = floor(($max_msg_member - $min_msg_member) * (($start + $maxPerPage) / $msgCount) + .1 * ($max_msg_member - $min_msg_member));
970
		// Make a bigger margin for topics only.
971
		if ($context['is_topics'])
972
		{
973
			$margin *= 5;
974
			$range_limit = $reverse ? 't.id_first_msg < ' . ($min_msg_member + $margin) : 't.id_first_msg > ' . ($max_msg_member - $margin);
975
		}
976
		else
977
			$range_limit = $reverse ? 'm.id_msg < ' . ($min_msg_member + $margin) : 'm.id_msg > ' . ($max_msg_member - $margin);
978
	}
979
980
	// Find this user's posts.  The left join on categories somehow makes this faster, weird as it looks.
981
	$looped = false;
982
	while (true)
983
	{
984
		if ($context['is_topics'])
985
		{
986
			$request = $smcFunc['db_query']('', '
987
				SELECT
988
					b.id_board, b.name AS bname, c.id_cat, c.name AS cname, t.id_member_started, t.id_first_msg, t.id_last_msg,
989
					t.approved, m.body, m.smileys_enabled, m.subject, m.poster_time, m.id_topic, m.id_msg
990
				FROM {db_prefix}topics AS t
991
					INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
992
					LEFT JOIN {db_prefix}categories AS c ON (c.id_cat = b.id_cat)
993
					INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)
994
				WHERE t.id_member_started = {int:current_member}' . (!empty($board) ? '
995
					AND t.id_board = {int:board}' : '') . (empty($range_limit) ? '' : '
996
					AND ' . $range_limit) . '
997
					AND {query_see_board}' . (!$modSettings['postmod_active'] || $context['user']['is_owner'] ? '' : '
998
					AND t.approved = {int:is_approved} AND m.approved = {int:is_approved}') . '
999
				ORDER BY t.id_first_msg ' . ($reverse ? 'ASC' : 'DESC') . '
1000
				LIMIT {int:start}, {int:max}',
1001
				array(
1002
					'current_member' => $memID,
1003
					'is_approved' => 1,
1004
					'board' => $board,
1005
					'start' => $start,
1006
					'max' => $maxIndex,
1007
				)
1008
			);
1009
		}
1010
		else
1011
		{
1012
			$request = $smcFunc['db_query']('', '
1013
				SELECT
1014
					b.id_board, b.name AS bname, c.id_cat, c.name AS cname, m.id_topic, m.id_msg,
1015
					t.id_member_started, t.id_first_msg, t.id_last_msg, m.body, m.smileys_enabled,
1016
					m.subject, m.poster_time, m.approved
1017
				FROM {db_prefix}messages AS m
1018
					INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic)
1019
					INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
1020
					LEFT JOIN {db_prefix}categories AS c ON (c.id_cat = b.id_cat)
1021
				WHERE m.id_member = {int:current_member}' . (!empty($board) ? '
1022
					AND b.id_board = {int:board}' : '') . (empty($range_limit) ? '' : '
1023
					AND ' . $range_limit) . '
1024
					AND {query_see_board}' . (!$modSettings['postmod_active'] || $context['user']['is_owner'] ? '' : '
1025
					AND t.approved = {int:is_approved} AND m.approved = {int:is_approved}') . '
1026
				ORDER BY m.id_msg ' . ($reverse ? 'ASC' : 'DESC') . '
1027
				LIMIT {int:start}, {int:max}',
1028
				array(
1029
					'current_member' => $memID,
1030
					'is_approved' => 1,
1031
					'board' => $board,
1032
					'start' => $start,
1033
					'max' => $maxIndex,
1034
				)
1035
			);
1036
		}
1037
1038
		// Make sure we quit this loop.
1039
		if ($smcFunc['db_num_rows']($request) === $maxIndex || $looped || $range_limit == '')
1040
			break;
1041
		$looped = true;
1042
		$range_limit = '';
1043
	}
1044
1045
	// Start counting at the number of the first message displayed.
1046
	$counter = $reverse ? $context['start'] + $maxIndex + 1 : $context['start'];
1047
	$context['posts'] = array();
1048
	$board_ids = array('own' => array(), 'any' => array());
1049
	while ($row = $smcFunc['db_fetch_assoc']($request))
1050
	{
1051
		// Censor....
1052
		censorText($row['body']);
1053
		censorText($row['subject']);
1054
1055
		// Do the code.
1056
		$row['body'] = parse_bbc($row['body'], $row['smileys_enabled'], $row['id_msg']);
1057
1058
		// And the array...
1059
		$context['posts'][$counter += $reverse ? -1 : 1] = array(
1060
			'body' => $row['body'],
1061
			'counter' => $counter,
1062
			'category' => array(
1063
				'name' => $row['cname'],
1064
				'id' => $row['id_cat']
1065
			),
1066
			'board' => array(
1067
				'name' => $row['bname'],
1068
				'id' => $row['id_board']
1069
			),
1070
			'topic' => $row['id_topic'],
1071
			'subject' => $row['subject'],
1072
			'start' => 'msg' . $row['id_msg'],
1073
			'time' => timeformat($row['poster_time']),
1074
			'timestamp' => forum_time(true, $row['poster_time']),
1075
			'id' => $row['id_msg'],
1076
			'can_reply' => false,
1077
			'can_mark_notify' => !$context['user']['is_guest'],
1078
			'can_delete' => false,
1079
			'delete_possible' => ($row['id_first_msg'] != $row['id_msg'] || $row['id_last_msg'] == $row['id_msg']) && (empty($modSettings['edit_disable_time']) || $row['poster_time'] + $modSettings['edit_disable_time'] * 60 >= time()),
1080
			'approved' => $row['approved'],
1081
			'css_class' => $row['approved'] ? 'windowbg' : 'approvebg',
1082
		);
1083
1084
		if ($user_info['id'] == $row['id_member_started'])
1085
			$board_ids['own'][$row['id_board']][] = $counter;
1086
		$board_ids['any'][$row['id_board']][] = $counter;
1087
	}
1088
	$smcFunc['db_free_result']($request);
1089
1090
	// All posts were retrieved in reverse order, get them right again.
1091
	if ($reverse)
1092
		$context['posts'] = array_reverse($context['posts'], true);
1093
1094
	// These are all the permissions that are different from board to board..
1095
	if ($context['is_topics'])
1096
		$permissions = array(
1097
			'own' => array(
1098
				'post_reply_own' => 'can_reply',
1099
			),
1100
			'any' => array(
1101
				'post_reply_any' => 'can_reply',
1102
			)
1103
		);
1104
	else
1105
		$permissions = array(
1106
			'own' => array(
1107
				'post_reply_own' => 'can_reply',
1108
				'delete_own' => 'can_delete',
1109
			),
1110
			'any' => array(
1111
				'post_reply_any' => 'can_reply',
1112
				'delete_any' => 'can_delete',
1113
			)
1114
		);
1115
1116
	// Create an array for the permissions.
1117
	$boards_can = boardsAllowedTo(array_keys(iterator_to_array(
1118
		new RecursiveIteratorIterator(new RecursiveArrayIterator($permissions)))
1119
	), true, false);
1120
1121
	// For every permission in the own/any lists...
1122
	foreach ($permissions as $type => $list)
1123
	{
1124
		foreach ($list as $permission => $allowed)
1125
		{
1126
			// Get the boards they can do this on...
1127
			$boards = $boards_can[$permission];
1128
1129
			// Hmm, they can do it on all boards, can they?
1130
			if (!empty($boards) && $boards[0] == 0)
1131
				$boards = array_keys($board_ids[$type]);
1132
1133
			// Now go through each board they can do the permission on.
1134
			foreach ($boards as $board_id)
1135
			{
1136
				// There aren't any posts displayed from this board.
1137
				if (!isset($board_ids[$type][$board_id]))
1138
					continue;
1139
1140
				// Set the permission to true ;).
1141
				foreach ($board_ids[$type][$board_id] as $counter)
1142
					$context['posts'][$counter][$allowed] = true;
1143
			}
1144
		}
1145
	}
1146
1147
	// Clean up after posts that cannot be deleted and quoted.
1148
	$quote_enabled = empty($modSettings['disabledBBC']) || !in_array('quote', explode(',', $modSettings['disabledBBC']));
1149
	foreach ($context['posts'] as $counter => $dummy)
1150
	{
1151
		$context['posts'][$counter]['can_delete'] &= $context['posts'][$counter]['delete_possible'];
1152
		$context['posts'][$counter]['can_quote'] = $context['posts'][$counter]['can_reply'] && $quote_enabled;
1153
	}
1154
1155
	// Allow last minute changes.
1156
	call_integration_hook('integrate_profile_showPosts');
1157
1158
	foreach ($context['posts'] as $key => $post)
1159
	{
1160
		$context['posts'][$key]['quickbuttons'] = array(
1161
			'reply' => array(
1162
				'label' => $txt['reply'],
1163
				'href' => $scripturl.'?action=post;topic='.$post['topic'].'.'.$post['start'],
1164
				'icon' => 'reply_button',
1165
				'show' => $post['can_reply']
1166
			),
1167
			'quote' => array(
1168
				'label' => $txt['quote_action'],
1169
				'href' => $scripturl.'?action=post;topic='.$post['topic'].'.'.$post['start'].';quote='.$post['id'],
1170
				'icon' => 'quote',
1171
				'show' => $post['can_quote']
1172
			),
1173
			'remove' => array(
1174
				'label' => $txt['remove'],
1175
				'href' => $scripturl.'?action=deletemsg;msg='.$post['id'].';topic='.$post['topic'].';profile;u='.$context['member']['id'].';start='.$context['start'].';'.$context['session_var'].'='.$context['session_id'],
1176
				'javascript' => 'data-confirm="'.$txt['remove_message'].'"',
1177
				'class' => 'you_sure',
1178
				'icon' => 'remove_button',
1179
				'show' => $post['can_delete']
1180
			)
1181
		);
1182
	}
1183
}
1184
1185
/**
1186
 * Show all the attachments belonging to a member.
1187
 *
1188
 * @param int $memID The ID of the member
1189
 */
1190
function showAttachments($memID)
1191
{
1192
	global $txt, $scripturl, $modSettings;
1193
	global $sourcedir;
1194
1195
	// OBEY permissions!
1196
	$boardsAllowed = boardsAllowedTo('view_attachments');
1197
1198
	// Make sure we can't actually see anything...
1199
	if (empty($boardsAllowed))
1200
		$boardsAllowed = array(-1);
1201
1202
	require_once($sourcedir . '/Subs-List.php');
1203
1204
	// This is all the information required to list attachments.
1205
	$listOptions = array(
1206
		'id' => 'attachments',
1207
		'width' => '100%',
1208
		'items_per_page' => $modSettings['defaultMaxListItems'],
1209
		'no_items_label' => $txt['show_attachments_none'],
1210
		'base_href' => $scripturl . '?action=profile;area=showposts;sa=attach;u=' . $memID,
1211
		'default_sort_col' => 'filename',
1212
		'get_items' => array(
1213
			'function' => 'list_getAttachments',
1214
			'params' => array(
1215
				$boardsAllowed,
1216
				$memID,
1217
			),
1218
		),
1219
		'get_count' => array(
1220
			'function' => 'list_getNumAttachments',
1221
			'params' => array(
1222
				$boardsAllowed,
1223
				$memID,
1224
			),
1225
		),
1226
		'data_check' => array(
1227
			'class' => function($data)
1228
			{
1229
				return $data['approved'] ? '' : 'approvebg';
1230
			}
1231
		),
1232
		'columns' => array(
1233
			'filename' => array(
1234
				'header' => array(
1235
					'value' => $txt['show_attach_filename'],
1236
					'class' => 'lefttext',
1237
					'style' => 'width: 25%;',
1238
				),
1239
				'data' => array(
1240
					'sprintf' => array(
1241
						'format' => '<a href="' . $scripturl . '?action=dlattach;topic=%1$d.0;attach=%2$d">%3$s</a>',
1242
						'params' => array(
1243
							'topic' => true,
1244
							'id' => true,
1245
							'filename' => false,
1246
						),
1247
					),
1248
				),
1249
				'sort' => array(
1250
					'default' => 'a.filename',
1251
					'reverse' => 'a.filename DESC',
1252
				),
1253
			),
1254
			'downloads' => array(
1255
				'header' => array(
1256
					'value' => $txt['show_attach_downloads'],
1257
					'style' => 'width: 12%;',
1258
				),
1259
				'data' => array(
1260
					'db' => 'downloads',
1261
					'comma_format' => true,
1262
				),
1263
				'sort' => array(
1264
					'default' => 'a.downloads',
1265
					'reverse' => 'a.downloads DESC',
1266
				),
1267
			),
1268
			'subject' => array(
1269
				'header' => array(
1270
					'value' => $txt['message'],
1271
					'class' => 'lefttext',
1272
					'style' => 'width: 30%;',
1273
				),
1274
				'data' => array(
1275
					'sprintf' => array(
1276
						'format' => '<a href="' . $scripturl . '?msg=%1$d">%2$s</a>',
1277
						'params' => array(
1278
							'msg' => true,
1279
							'subject' => false,
1280
						),
1281
					),
1282
				),
1283
				'sort' => array(
1284
					'default' => 'm.subject',
1285
					'reverse' => 'm.subject DESC',
1286
				),
1287
			),
1288
			'posted' => array(
1289
				'header' => array(
1290
					'value' => $txt['show_attach_posted'],
1291
					'class' => 'lefttext',
1292
				),
1293
				'data' => array(
1294
					'db' => 'posted',
1295
					'timeformat' => true,
1296
				),
1297
				'sort' => array(
1298
					'default' => 'm.poster_time',
1299
					'reverse' => 'm.poster_time DESC',
1300
				),
1301
			),
1302
		),
1303
	);
1304
1305
	// Create the request list.
1306
	createList($listOptions);
1307
}
1308
1309
/**
1310
 * Get a list of attachments for a member. Callback for the list in showAttachments()
1311
 *
1312
 * @param int $start Which item to start with (for pagination purposes)
1313
 * @param int $items_per_page How many items to show on each page
1314
 * @param string $sort A string indicating how to sort the results
1315
 * @param array $boardsAllowed An array containing the IDs of the boards they can see
1316
 * @param int $memID The ID of the member
1317
 * @return array An array of information about the attachments
1318
 */
1319
function list_getAttachments($start, $items_per_page, $sort, $boardsAllowed, $memID)
1320
{
1321
	global $smcFunc, $board, $modSettings, $context;
1322
1323
	// Retrieve some attachments.
1324
	$request = $smcFunc['db_query']('', '
1325
		SELECT a.id_attach, a.id_msg, a.filename, a.downloads, a.approved, m.id_msg, m.id_topic,
1326
			m.id_board, m.poster_time, m.subject, b.name
1327
		FROM {db_prefix}attachments AS a
1328
			INNER JOIN {db_prefix}messages AS m ON (m.id_msg = a.id_msg)
1329
			INNER JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board AND {query_see_board})
1330
		WHERE a.attachment_type = {int:attachment_type}
1331
			AND a.id_msg != {int:no_message}
1332
			AND m.id_member = {int:current_member}' . (!empty($board) ? '
1333
			AND b.id_board = {int:board}' : '') . (!in_array(0, $boardsAllowed) ? '
1334
			AND b.id_board IN ({array_int:boards_list})' : '') . (!$modSettings['postmod_active'] || $context['user']['is_owner'] ? '' : '
1335
			AND m.approved = {int:is_approved}') . '
1336
		ORDER BY {raw:sort}
1337
		LIMIT {int:offset}, {int:limit}',
1338
		array(
1339
			'boards_list' => $boardsAllowed,
1340
			'attachment_type' => 0,
1341
			'no_message' => 0,
1342
			'current_member' => $memID,
1343
			'is_approved' => 1,
1344
			'board' => $board,
1345
			'sort' => $sort,
1346
			'offset' => $start,
1347
			'limit' => $items_per_page,
1348
		)
1349
	);
1350
	$attachments = array();
1351
	while ($row = $smcFunc['db_fetch_assoc']($request))
1352
		$attachments[] = array(
1353
			'id' => $row['id_attach'],
1354
			'filename' => $row['filename'],
1355
			'downloads' => $row['downloads'],
1356
			'subject' => censorText($row['subject']),
1357
			'posted' => $row['poster_time'],
1358
			'msg' => $row['id_msg'],
1359
			'topic' => $row['id_topic'],
1360
			'board' => $row['id_board'],
1361
			'board_name' => $row['name'],
1362
			'approved' => $row['approved'],
1363
		);
1364
1365
	$smcFunc['db_free_result']($request);
1366
1367
	return $attachments;
1368
}
1369
1370
/**
1371
 * Gets the total number of attachments for a member
1372
 *
1373
 * @param array $boardsAllowed An array of the IDs of the boards they can see
1374
 * @param int $memID The ID of the member
1375
 * @return int The number of attachments
1376
 */
1377
function list_getNumAttachments($boardsAllowed, $memID)
1378
{
1379
	global $board, $smcFunc, $modSettings, $context;
1380
1381
	// Get the total number of attachments they have posted.
1382
	$request = $smcFunc['db_query']('', '
1383
		SELECT COUNT(*)
1384
		FROM {db_prefix}attachments AS a
1385
			INNER JOIN {db_prefix}messages AS m ON (m.id_msg = a.id_msg)
1386
			INNER JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board AND {query_see_board})
1387
		WHERE a.attachment_type = {int:attachment_type}
1388
			AND a.id_msg != {int:no_message}
1389
			AND m.id_member = {int:current_member}' . (!empty($board) ? '
1390
			AND b.id_board = {int:board}' : '') . (!in_array(0, $boardsAllowed) ? '
1391
			AND b.id_board IN ({array_int:boards_list})' : '') . (!$modSettings['postmod_active'] || $context['user']['is_owner'] ? '' : '
1392
			AND m.approved = {int:is_approved}'),
1393
		array(
1394
			'boards_list' => $boardsAllowed,
1395
			'attachment_type' => 0,
1396
			'no_message' => 0,
1397
			'current_member' => $memID,
1398
			'is_approved' => 1,
1399
			'board' => $board,
1400
		)
1401
	);
1402
	list ($attachCount) = $smcFunc['db_fetch_row']($request);
1403
	$smcFunc['db_free_result']($request);
1404
1405
	return $attachCount;
1406
}
1407
1408
/**
1409
 * Show all the unwatched topics.
1410
 *
1411
 * @param int $memID The ID of the member
1412
 */
1413
function showUnwatched($memID)
1414
{
1415
	global $txt, $user_info, $scripturl, $modSettings, $context, $options, $sourcedir;
1416
1417
	// Only the owner can see the list (if the function is enabled of course)
1418
	if ($user_info['id'] != $memID)
1419
		return;
1420
1421
	require_once($sourcedir . '/Subs-List.php');
1422
1423
	// And here they are: the topics you don't like
1424
	$listOptions = array(
1425
		'id' => 'unwatched_topics',
1426
		'width' => '100%',
1427
		'items_per_page' => (empty($modSettings['disableCustomPerPage']) && !empty($options['topics_per_page'])) ? $options['topics_per_page'] : $modSettings['defaultMaxTopics'],
1428
		'no_items_label' => $txt['unwatched_topics_none'],
1429
		'base_href' => $scripturl . '?action=profile;area=showposts;sa=unwatchedtopics;u=' . $memID,
1430
		'default_sort_col' => 'started_on',
1431
		'get_items' => array(
1432
			'function' => 'list_getUnwatched',
1433
			'params' => array(
1434
				$memID,
1435
			),
1436
		),
1437
		'get_count' => array(
1438
			'function' => 'list_getNumUnwatched',
1439
			'params' => array(
1440
				$memID,
1441
			),
1442
		),
1443
		'columns' => array(
1444
			'subject' => array(
1445
				'header' => array(
1446
					'value' => $txt['subject'],
1447
					'class' => 'lefttext',
1448
					'style' => 'width: 30%;',
1449
				),
1450
				'data' => array(
1451
					'sprintf' => array(
1452
						'format' => '<a href="' . $scripturl . '?topic=%1$d.0">%2$s</a>',
1453
						'params' => array(
1454
							'id_topic' => false,
1455
							'subject' => false,
1456
						),
1457
					),
1458
				),
1459
				'sort' => array(
1460
					'default' => 'm.subject',
1461
					'reverse' => 'm.subject DESC',
1462
				),
1463
			),
1464
			'started_by' => array(
1465
				'header' => array(
1466
					'value' => $txt['started_by'],
1467
					'style' => 'width: 15%;',
1468
				),
1469
				'data' => array(
1470
					'db' => 'started_by',
1471
				),
1472
				'sort' => array(
1473
					'default' => 'mem.real_name',
1474
					'reverse' => 'mem.real_name DESC',
1475
				),
1476
			),
1477
			'started_on' => array(
1478
				'header' => array(
1479
					'value' => $txt['on'],
1480
					'class' => 'lefttext',
1481
					'style' => 'width: 20%;',
1482
				),
1483
				'data' => array(
1484
					'db' => 'started_on',
1485
					'timeformat' => true,
1486
				),
1487
				'sort' => array(
1488
					'default' => 'm.poster_time',
1489
					'reverse' => 'm.poster_time DESC',
1490
				),
1491
			),
1492
			'last_post_by' => array(
1493
				'header' => array(
1494
					'value' => $txt['last_post'],
1495
					'style' => 'width: 15%;',
1496
				),
1497
				'data' => array(
1498
					'db' => 'last_post_by',
1499
				),
1500
				'sort' => array(
1501
					'default' => 'mem.real_name',
1502
					'reverse' => 'mem.real_name DESC',
1503
				),
1504
			),
1505
			'last_post_on' => array(
1506
				'header' => array(
1507
					'value' => $txt['on'],
1508
					'class' => 'lefttext',
1509
					'style' => 'width: 20%;',
1510
				),
1511
				'data' => array(
1512
					'db' => 'last_post_on',
1513
					'timeformat' => true,
1514
				),
1515
				'sort' => array(
1516
					'default' => 'm.poster_time',
1517
					'reverse' => 'm.poster_time DESC',
1518
				),
1519
			),
1520
		),
1521
	);
1522
1523
	// Create the request list.
1524
	createList($listOptions);
1525
1526
	$context['sub_template'] = 'show_list';
1527
	$context['default_list'] = 'unwatched_topics';
1528
}
1529
1530
/**
1531
 * Gets information about unwatched (disregarded) topics. Callback for the list in show_unwatched
1532
 *
1533
 * @param int $start The item to start with (for pagination purposes)
1534
 * @param int $items_per_page How many items to show on each page
1535
 * @param string $sort A string indicating how to sort the results
1536
 * @param int $memID The ID of the member
1537
 * @return array An array of information about the unwatched topics
1538
 */
1539
function list_getUnwatched($start, $items_per_page, $sort, $memID)
1540
{
1541
	global $smcFunc;
1542
1543
	// Get the list of topics we can see
1544
	$request = $smcFunc['db_query']('', '
1545
		SELECT lt.id_topic
1546
		FROM {db_prefix}log_topics as lt
1547
			LEFT JOIN {db_prefix}topics as t ON (lt.id_topic = t.id_topic)
1548
			LEFT JOIN {db_prefix}messages as m ON (t.id_first_msg = m.id_msg)' . (in_array($sort, array('mem.real_name', 'mem.real_name DESC', 'mem.poster_time', 'mem.poster_time DESC')) ? '
1549
			LEFT JOIN {db_prefix}members as mem ON (m.id_member = mem.id_member)' : '') . '
1550
		WHERE lt.id_member = {int:current_member}
1551
			AND unwatched = 1
1552
			AND {query_see_message_board}
1553
		ORDER BY {raw:sort}
1554
		LIMIT {int:offset}, {int:limit}',
1555
		array(
1556
			'current_member' => $memID,
1557
			'sort' => $sort,
1558
			'offset' => $start,
1559
			'limit' => $items_per_page,
1560
		)
1561
	);
1562
1563
	$topics = array();
1564
	while ($row = $smcFunc['db_fetch_assoc']($request))
1565
		$topics[] = $row['id_topic'];
1566
1567
	$smcFunc['db_free_result']($request);
1568
1569
	// Any topics found?
1570
	$topicsInfo = array();
1571
	if (!empty($topics))
1572
	{
1573
		$request = $smcFunc['db_query']('', '
1574
			SELECT mf.subject, mf.poster_time as started_on, COALESCE(memf.real_name, mf.poster_name) as started_by, ml.poster_time as last_post_on, COALESCE(meml.real_name, ml.poster_name) as last_post_by, t.id_topic
1575
			FROM {db_prefix}topics AS t
1576
				INNER JOIN {db_prefix}messages AS ml ON (ml.id_msg = t.id_last_msg)
1577
				INNER JOIN {db_prefix}messages AS mf ON (mf.id_msg = t.id_first_msg)
1578
				LEFT JOIN {db_prefix}members AS meml ON (meml.id_member = ml.id_member)
1579
				LEFT JOIN {db_prefix}members AS memf ON (memf.id_member = mf.id_member)
1580
			WHERE t.id_topic IN ({array_int:topics})',
1581
			array(
1582
				'topics' => $topics,
1583
			)
1584
		);
1585
		while ($row = $smcFunc['db_fetch_assoc']($request))
1586
			$topicsInfo[] = $row;
1587
		$smcFunc['db_free_result']($request);
1588
	}
1589
1590
	return $topicsInfo;
1591
}
1592
1593
/**
1594
 * Count the number of topics in the unwatched list
1595
 *
1596
 * @param int $memID The ID of the member
1597
 * @return int The number of unwatched topics
1598
 */
1599
function list_getNumUnwatched($memID)
1600
{
1601
	global $smcFunc;
1602
1603
	// Get the total number of attachments they have posted.
1604
	$request = $smcFunc['db_query']('', '
1605
		SELECT COUNT(*)
1606
		FROM {db_prefix}log_topics as lt
1607
		LEFT JOIN {db_prefix}topics as t ON (lt.id_topic = t.id_topic)
1608
		WHERE lt.id_member = {int:current_member}
1609
			AND lt.unwatched = 1
1610
			AND {query_see_topic_board}',
1611
		array(
1612
			'current_member' => $memID,
1613
		)
1614
	);
1615
	list ($unwatchedCount) = $smcFunc['db_fetch_row']($request);
1616
	$smcFunc['db_free_result']($request);
1617
1618
	return $unwatchedCount;
1619
}
1620
1621
/**
1622
 * Gets the user stats for display
1623
 *
1624
 * @param int $memID The ID of the member
1625
 */
1626
function statPanel($memID)
1627
{
1628
	global $txt, $scripturl, $context, $user_profile, $user_info, $modSettings, $smcFunc;
1629
1630
	$context['page_title'] = $txt['statPanel_showStats'] . ' ' . $user_profile[$memID]['real_name'];
1631
1632
	// Is the load average too high to allow searching just now?
1633
	if (!empty($context['load_average']) && !empty($modSettings['loadavg_userstats']) && $context['load_average'] >= $modSettings['loadavg_userstats'])
1634
		fatal_lang_error('loadavg_userstats_disabled', false);
1635
1636
	// General user statistics.
1637
	$timeDays = floor($user_profile[$memID]['total_time_logged_in'] / 86400);
1638
	$timeHours = floor(($user_profile[$memID]['total_time_logged_in'] % 86400) / 3600);
1639
	$context['time_logged_in'] = ($timeDays > 0 ? $timeDays . $txt['total_time_logged_days'] : '') . ($timeHours > 0 ? $timeHours . $txt['total_time_logged_hours'] : '') . floor(($user_profile[$memID]['total_time_logged_in'] % 3600) / 60) . $txt['total_time_logged_minutes'];
1640
	$context['num_posts'] = comma_format($user_profile[$memID]['posts']);
1641
	// Menu tab
1642
	$context[$context['profile_menu_name']]['tab_data'] = array(
1643
		'title' => $txt['statPanel_generalStats'] . ' - ' . $context['member']['name'],
1644
		'icon' => 'stats_info.png'
1645
	);
1646
1647
	// Number of topics started and Number polls started
1648
	$result = $smcFunc['db_query']('', '
1649
		SELECT COUNT(*), COUNT( CASE WHEN id_poll != {int:no_poll} THEN 1 ELSE NULL END )
1650
		FROM {db_prefix}topics
1651
		WHERE id_member_started = {int:current_member}' . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? '
1652
			AND id_board != {int:recycle_board}' : ''),
1653
		array(
1654
			'current_member' => $memID,
1655
			'recycle_board' => $modSettings['recycle_board'],
1656
			'no_poll' => 0,
1657
		)
1658
	);
1659
	list ($context['num_topics'], $context['num_polls']) = $smcFunc['db_fetch_row']($result);
1660
	$smcFunc['db_free_result']($result);
1661
1662
	// Number polls voted in.
1663
	$result = $smcFunc['db_query']('distinct_poll_votes', '
1664
		SELECT COUNT(DISTINCT id_poll)
1665
		FROM {db_prefix}log_polls
1666
		WHERE id_member = {int:current_member}',
1667
		array(
1668
			'current_member' => $memID,
1669
		)
1670
	);
1671
	list ($context['num_votes']) = $smcFunc['db_fetch_row']($result);
1672
	$smcFunc['db_free_result']($result);
1673
1674
	// Format the numbers...
1675
	$context['num_topics'] = comma_format($context['num_topics']);
1676
	$context['num_polls'] = comma_format($context['num_polls']);
1677
	$context['num_votes'] = comma_format($context['num_votes']);
1678
1679
	// Grab the board this member posted in most often.
1680
	$result = $smcFunc['db_query']('', '
1681
		SELECT
1682
			b.id_board, MAX(b.name) AS name, MAX(b.num_posts) AS num_posts, COUNT(*) AS message_count
1683
		FROM {db_prefix}messages AS m
1684
			INNER JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board)
1685
		WHERE m.id_member = {int:current_member}
1686
			AND b.count_posts = {int:count_enabled}
1687
			AND {query_see_board}
1688
		GROUP BY b.id_board
1689
		ORDER BY message_count DESC
1690
		LIMIT 10',
1691
		array(
1692
			'current_member' => $memID,
1693
			'count_enabled' => 0,
1694
		)
1695
	);
1696
	$context['popular_boards'] = array();
1697
	while ($row = $smcFunc['db_fetch_assoc']($result))
1698
	{
1699
		$context['popular_boards'][$row['id_board']] = array(
1700
			'id' => $row['id_board'],
1701
			'posts' => $row['message_count'],
1702
			'href' => $scripturl . '?board=' . $row['id_board'] . '.0',
1703
			'link' => '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['name'] . '</a>',
1704
			'posts_percent' => $user_profile[$memID]['posts'] == 0 ? 0 : ($row['message_count'] * 100) / $user_profile[$memID]['posts'],
1705
			'total_posts' => $row['num_posts'],
1706
			'total_posts_member' => $user_profile[$memID]['posts'],
1707
		);
1708
	}
1709
	$smcFunc['db_free_result']($result);
1710
1711
	// Now get the 10 boards this user has most often participated in.
1712
	$result = $smcFunc['db_query']('profile_board_stats', '
1713
		SELECT
1714
			b.id_board, MAX(b.name) AS name, b.num_posts, COUNT(*) AS message_count,
1715
			CASE WHEN COUNT(*) > MAX(b.num_posts) THEN 1 ELSE COUNT(*) / MAX(b.num_posts) END * 100 AS percentage
1716
		FROM {db_prefix}messages AS m
1717
			INNER JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board)
1718
		WHERE m.id_member = {int:current_member}
1719
			AND {query_see_board}
1720
		GROUP BY b.id_board, b.num_posts
1721
		ORDER BY percentage DESC
1722
		LIMIT 10',
1723
		array(
1724
			'current_member' => $memID,
1725
		)
1726
	);
1727
	$context['board_activity'] = array();
1728
	while ($row = $smcFunc['db_fetch_assoc']($result))
1729
	{
1730
		$context['board_activity'][$row['id_board']] = array(
1731
			'id' => $row['id_board'],
1732
			'posts' => $row['message_count'],
1733
			'href' => $scripturl . '?board=' . $row['id_board'] . '.0',
1734
			'link' => '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['name'] . '</a>',
1735
			'percent' => comma_format((float) $row['percentage'], 2),
1736
			'posts_percent' => (float) $row['percentage'],
1737
			'total_posts' => $row['num_posts'],
1738
		);
1739
	}
1740
	$smcFunc['db_free_result']($result);
1741
1742
	// Posting activity by time.
1743
	$result = $smcFunc['db_query']('user_activity_by_time', '
1744
		SELECT
1745
			HOUR(FROM_UNIXTIME(poster_time + {int:time_offset})) AS hour,
1746
			COUNT(*) AS post_count
1747
		FROM (
1748
			SELECT poster_time, id_msg 
1749
			FROM {db_prefix}messages WHERE id_member = {int:current_member}
1750
			ORDER BY id_msg DESC
1751
			LIMIT {int:max_messages}
1752
		) a
1753
		GROUP BY hour',
1754
		array(
1755
			'current_member' => $memID,
1756
			'time_offset' => (($user_info['time_offset'] + $modSettings['time_offset']) * 3600),
1757
			'max_messages' => 1001,
1758
		)
1759
	);
1760
	$maxPosts = $realPosts = 0;
1761
	$context['posts_by_time'] = array();
1762
	while ($row = $smcFunc['db_fetch_assoc']($result))
1763
	{
1764
		// Cast as an integer to remove the leading 0.
1765
		$row['hour'] = (int) $row['hour'];
1766
1767
		$maxPosts = max($row['post_count'], $maxPosts);
1768
		$realPosts += $row['post_count'];
1769
1770
		$context['posts_by_time'][$row['hour']] = array(
1771
			'hour' => $row['hour'],
1772
			'hour_format' => stripos($user_info['time_format'], '%p') === false ? $row['hour'] : date('g a', mktime($row['hour'])),
1773
			'posts' => $row['post_count'],
1774
			'posts_percent' => 0,
1775
			'is_last' => $row['hour'] == 23,
1776
		);
1777
	}
1778
	$smcFunc['db_free_result']($result);
1779
1780
	if ($maxPosts > 0)
1781
		for ($hour = 0; $hour < 24; $hour++)
1782
		{
1783
			if (!isset($context['posts_by_time'][$hour]))
1784
				$context['posts_by_time'][$hour] = array(
1785
					'hour' => $hour,
1786
					'hour_format' => stripos($user_info['time_format'], '%p') === false ? $hour : date('g a', mktime($hour)),
1787
					'posts' => 0,
1788
					'posts_percent' => 0,
1789
					'relative_percent' => 0,
1790
					'is_last' => $hour == 23,
1791
				);
1792
			else
1793
			{
1794
				$context['posts_by_time'][$hour]['posts_percent'] = round(($context['posts_by_time'][$hour]['posts'] * 100) / $realPosts);
1795
				$context['posts_by_time'][$hour]['relative_percent'] = round(($context['posts_by_time'][$hour]['posts'] * 100) / $maxPosts);
1796
			}
1797
		}
1798
1799
	// Put it in the right order.
1800
	ksort($context['posts_by_time']);
1801
1802
	/**
1803
	 * Adding new entries:
1804
	 * 'key' => array(
1805
	 * 		'text' => string, // The text that will be shown next to the entry.
1806
	 * 		'url' => string, // OPTIONAL: The entry will be a url
1807
	 * ),
1808
	 *
1809
	 * 'key' will be used to look up the language string as $txt['statPanel_' . $key].
1810
	 * Make sure to add a new entry when writing your mod!
1811
	 */
1812
	$context['text_stats'] = array(
1813
		'total_time_online' => array(
1814
			'text' => $context['time_logged_in'],
1815
		),
1816
		'total_posts' => array(
1817
			'text' => $context['num_posts'] . ' ' . $txt['statPanel_posts'],
1818
			'url' => $scripturl . '?action=profile;area=showposts;sa=messages;u=' . $memID
1819
		),
1820
		'total_topics' => array(
1821
			'text' => $context['num_topics'] . ' ' . $txt['statPanel_topics'],
1822
			'url' => $scripturl . '?action=profile;area=showposts;sa=topics;u=' . $memID
1823
		),
1824
		'users_polls' => array(
1825
			'text' => $context['num_polls'] . ' ' . $txt['statPanel_polls'],
1826
		),
1827
		'users_votes' => array(
1828
			'text' => $context['num_votes'] . ' ' . $txt['statPanel_votes']
1829
		)
1830
	);
1831
1832
	// Custom stats (just add a template_layer to add it to the template!)
1833
	call_integration_hook('integrate_profile_stats', array($memID, &$context['text_stats']));
1834
}
1835
1836
/**
1837
 * Loads up the information for the "track user" section of the profile
1838
 *
1839
 * @param int $memID The ID of the member
1840
 */
1841
function tracking($memID)
1842
{
1843
	global $context, $txt, $modSettings, $user_profile;
1844
1845
	$subActions = array(
1846
		'activity' => array('trackActivity', $txt['trackActivity'], 'moderate_forum'),
1847
		'ip' => array('TrackIP', $txt['trackIP'], 'moderate_forum'),
1848
		'edits' => array('trackEdits', $txt['trackEdits'], 'moderate_forum'),
1849
		'groupreq' => array('trackGroupReq', $txt['trackGroupRequests'], 'approve_group_requests'),
1850
		'logins' => array('TrackLogins', $txt['trackLogins'], 'moderate_forum'),
1851
	);
1852
1853
	foreach ($subActions as $sa => $action)
1854
	{
1855
		if (!allowedTo($action[2]))
1856
			unset($subActions[$sa]);
1857
	}
1858
1859
	// Create the tabs for the template.
1860
	$context[$context['profile_menu_name']]['tab_data'] = array(
1861
		'title' => $txt['tracking'],
1862
		'description' => $txt['tracking_description'],
1863
		'icon' => 'profile_hd.png',
1864
		'tabs' => array(
1865
			'activity' => array(),
1866
			'ip' => array(),
1867
			'edits' => array(),
1868
			'groupreq' => array(),
1869
			'logins' => array(),
1870
		),
1871
	);
1872
1873
	// Moderation must be on to track edits.
1874
	if (empty($modSettings['userlog_enabled']))
1875
		unset($context[$context['profile_menu_name']]['tab_data']['edits'], $subActions['edits']);
1876
1877
	// Group requests must be active to show it...
1878
	if (empty($modSettings['show_group_membership']))
1879
		unset($context[$context['profile_menu_name']]['tab_data']['groupreq'], $subActions['groupreq']);
1880
1881
	if (empty($subActions))
1882
		fatal_lang_error('no_access', false);
1883
1884
	$keys = array_keys($subActions);
1885
	$default = array_shift($keys);
1886
	$context['tracking_area'] = isset($_GET['sa']) && isset($subActions[$_GET['sa']]) ? $_GET['sa'] : $default;
1887
1888
	// Set a page title.
1889
	$context['page_title'] = $txt['trackUser'] . ' - ' . $subActions[$context['tracking_area']][1] . ' - ' . $user_profile[$memID]['real_name'];
1890
1891
	// Pass on to the actual function.
1892
	$context['sub_template'] = $subActions[$context['tracking_area']][0];
1893
	$call = call_helper($subActions[$context['tracking_area']][0], true);
1894
1895
	if (!empty($call))
1896
		call_user_func($call, $memID);
0 ignored issues
show
Bug introduced by
It seems like $call can also be of type boolean; however, parameter $function of call_user_func() does only seem to accept callable, maybe add an additional type check? ( Ignorable by Annotation )

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

1896
		call_user_func(/** @scrutinizer ignore-type */ $call, $memID);
Loading history...
1897
}
1898
1899
/**
1900
 * Handles tracking a user's activity
1901
 *
1902
 * @param int $memID The ID of the member
1903
 */
1904
function trackActivity($memID)
1905
{
1906
	global $scripturl, $txt, $modSettings, $sourcedir;
1907
	global $user_profile, $context, $smcFunc;
1908
1909
	// Verify if the user has sufficient permissions.
1910
	isAllowedTo('moderate_forum');
1911
1912
	$context['last_ip'] = $user_profile[$memID]['member_ip'];
1913
	if ($context['last_ip'] != $user_profile[$memID]['member_ip2'])
1914
		$context['last_ip2'] = $user_profile[$memID]['member_ip2'];
1915
	$context['member']['name'] = $user_profile[$memID]['real_name'];
1916
1917
	// Set the options for the list component.
1918
	$listOptions = array(
1919
		'id' => 'track_user_list',
1920
		'title' => $txt['errors_by'] . ' ' . $context['member']['name'],
1921
		'items_per_page' => $modSettings['defaultMaxListItems'],
1922
		'no_items_label' => $txt['no_errors_from_user'],
1923
		'base_href' => $scripturl . '?action=profile;area=tracking;sa=user;u=' . $memID,
1924
		'default_sort_col' => 'date',
1925
		'get_items' => array(
1926
			'function' => 'list_getUserErrors',
1927
			'params' => array(
1928
				'le.id_member = {int:current_member}',
1929
				array('current_member' => $memID),
1930
			),
1931
		),
1932
		'get_count' => array(
1933
			'function' => 'list_getUserErrorCount',
1934
			'params' => array(
1935
				'id_member = {int:current_member}',
1936
				array('current_member' => $memID),
1937
			),
1938
		),
1939
		'columns' => array(
1940
			'ip_address' => array(
1941
				'header' => array(
1942
					'value' => $txt['ip_address'],
1943
				),
1944
				'data' => array(
1945
					'sprintf' => array(
1946
						'format' => '<a href="' . $scripturl . '?action=profile;area=tracking;sa=ip;searchip=%1$s;u=' . $memID . '">%1$s</a>',
1947
						'params' => array(
1948
							'ip' => false,
1949
						),
1950
					),
1951
				),
1952
				'sort' => array(
1953
					'default' => 'le.ip',
1954
					'reverse' => 'le.ip DESC',
1955
				),
1956
			),
1957
			'message' => array(
1958
				'header' => array(
1959
					'value' => $txt['message'],
1960
				),
1961
				'data' => array(
1962
					'sprintf' => array(
1963
						'format' => '%1$s<br><a href="%2$s">%2$s</a>',
1964
						'params' => array(
1965
							'message' => false,
1966
							'url' => false,
1967
						),
1968
					),
1969
				),
1970
			),
1971
			'date' => array(
1972
				'header' => array(
1973
					'value' => $txt['date'],
1974
				),
1975
				'data' => array(
1976
					'db' => 'time',
1977
				),
1978
				'sort' => array(
1979
					'default' => 'le.id_error DESC',
1980
					'reverse' => 'le.id_error',
1981
				),
1982
			),
1983
		),
1984
		'additional_rows' => array(
1985
			array(
1986
				'position' => 'after_title',
1987
				'value' => $txt['errors_desc'],
1988
			),
1989
		),
1990
	);
1991
1992
	// Create the list for viewing.
1993
	require_once($sourcedir . '/Subs-List.php');
1994
	createList($listOptions);
1995
1996
	// @todo cache this
1997
	// If this is a big forum, or a large posting user, let's limit the search.
1998
	if ($modSettings['totalMessages'] > 50000 && $user_profile[$memID]['posts'] > 500)
1999
	{
2000
		$request = $smcFunc['db_query']('', '
2001
			SELECT MAX(id_msg)
2002
			FROM {db_prefix}messages AS m
2003
			WHERE m.id_member = {int:current_member}',
2004
			array(
2005
				'current_member' => $memID,
2006
			)
2007
		);
2008
		list ($max_msg_member) = $smcFunc['db_fetch_row']($request);
2009
		$smcFunc['db_free_result']($request);
2010
2011
		// There's no point worrying ourselves with messages made yonks ago, just get recent ones!
2012
		$min_msg_member = max(0, $max_msg_member - $user_profile[$memID]['posts'] * 3);
2013
	}
2014
2015
	// Default to at least the ones we know about.
2016
	$ips = array(
2017
		$user_profile[$memID]['member_ip'],
2018
		$user_profile[$memID]['member_ip2'],
2019
	);
2020
2021
	// @todo cache this
2022
	// Get all IP addresses this user has used for his messages.
2023
	$request = $smcFunc['db_query']('', '
2024
		SELECT poster_ip
2025
		FROM {db_prefix}messages
2026
		WHERE id_member = {int:current_member}
2027
		' . (isset($min_msg_member) ? '
2028
			AND id_msg >= {int:min_msg_member} AND id_msg <= {int:max_msg_member}' : '') . '
2029
		GROUP BY poster_ip',
2030
		array(
2031
			'current_member' => $memID,
2032
			'min_msg_member' => !empty($min_msg_member) ? $min_msg_member : 0,
2033
			'max_msg_member' => !empty($max_msg_member) ? $max_msg_member : 0,
2034
		)
2035
	);
2036
	$context['ips'] = array();
2037
	while ($row = $smcFunc['db_fetch_assoc']($request))
2038
	{
2039
		$context['ips'][] = '<a href="' . $scripturl . '?action=profile;area=tracking;sa=ip;searchip=' . inet_dtop($row['poster_ip']) . ';u=' . $memID . '">' . inet_dtop($row['poster_ip']) . '</a>';
2040
		$ips[] = inet_dtop($row['poster_ip']);
2041
	}
2042
	$smcFunc['db_free_result']($request);
2043
2044
	// Now also get the IP addresses from the error messages.
2045
	$request = $smcFunc['db_query']('', '
2046
		SELECT COUNT(*) AS error_count, ip
2047
		FROM {db_prefix}log_errors
2048
		WHERE id_member = {int:current_member}
2049
		GROUP BY ip',
2050
		array(
2051
			'current_member' => $memID,
2052
		)
2053
	);
2054
	$context['error_ips'] = array();
2055
	while ($row = $smcFunc['db_fetch_assoc']($request))
2056
	{
2057
		$row['ip'] = inet_dtop($row['ip']);
2058
		$context['error_ips'][] = '<a href="' . $scripturl . '?action=profile;area=tracking;sa=ip;searchip=' . $row['ip'] . ';u=' . $memID . '">' . $row['ip'] . '</a>';
2059
		$ips[] = $row['ip'];
2060
	}
2061
	$smcFunc['db_free_result']($request);
2062
2063
	// Find other users that might use the same IP.
2064
	$ips = array_unique($ips);
2065
	$context['members_in_range'] = array();
2066
	if (!empty($ips))
2067
	{
2068
		// Get member ID's which are in messages...
2069
		$request = $smcFunc['db_query']('', '
2070
			SELECT DISTINCT mem.id_member
2071
			FROM {db_prefix}messages AS m
2072
				INNER JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
2073
			WHERE m.poster_ip IN ({array_inet:ip_list})
2074
				AND mem.id_member != {int:current_member}',
2075
			array(
2076
				'current_member' => $memID,
2077
				'ip_list' => $ips,
2078
			)
2079
		);
2080
		$message_members = array();
2081
		while ($row = $smcFunc['db_fetch_assoc']($request))
2082
			$message_members[] = $row['id_member'];
2083
		$smcFunc['db_free_result']($request);
2084
2085
		// Fetch their names, cause of the GROUP BY doesn't like giving us that normally.
2086
		if (!empty($message_members))
2087
		{
2088
			$request = $smcFunc['db_query']('', '
2089
				SELECT id_member, real_name
2090
				FROM {db_prefix}members
2091
				WHERE id_member IN ({array_int:message_members})',
2092
				array(
2093
					'message_members' => $message_members,
2094
					'ip_list' => $ips,
2095
				)
2096
			);
2097
			while ($row = $smcFunc['db_fetch_assoc']($request))
2098
				$context['members_in_range'][$row['id_member']] = '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['real_name'] . '</a>';
2099
			$smcFunc['db_free_result']($request);
2100
		}
2101
2102
		$request = $smcFunc['db_query']('', '
2103
			SELECT id_member, real_name
2104
			FROM {db_prefix}members
2105
			WHERE id_member != {int:current_member}
2106
				AND member_ip IN ({array_inet:ip_list})',
2107
			array(
2108
				'current_member' => $memID,
2109
				'ip_list' => $ips,
2110
			)
2111
		);
2112
		while ($row = $smcFunc['db_fetch_assoc']($request))
2113
			$context['members_in_range'][$row['id_member']] = '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['real_name'] . '</a>';
2114
		$smcFunc['db_free_result']($request);
2115
	}
2116
}
2117
2118
/**
2119
 * Get the number of user errors
2120
 *
2121
 * @param string $where A query to limit which errors are counted
2122
 * @param array $where_vars The parameters for $where
2123
 * @return int Number of user errors
2124
 */
2125
function list_getUserErrorCount($where, $where_vars = array())
2126
{
2127
	global $smcFunc;
2128
2129
	$request = $smcFunc['db_query']('', '
2130
		SELECT COUNT(id_error)
2131
		FROM {db_prefix}log_errors
2132
		WHERE ' . $where,
2133
		$where_vars
2134
	);
2135
	list ($count) = $smcFunc['db_fetch_row']($request);
2136
	$smcFunc['db_free_result']($request);
2137
2138
	return (int) $count;
2139
}
2140
2141
/**
2142
 * Gets all of the errors generated by a user's actions. Callback for the list in track_activity
2143
 *
2144
 * @param int $start Which item to start with (for pagination purposes)
2145
 * @param int $items_per_page How many items to show on each page
2146
 * @param string $sort A string indicating how to sort the results
2147
 * @param string $where A query indicating how to filter the results (eg 'id_member={int:id_member}')
2148
 * @param array $where_vars An array of parameters for $where
2149
 * @return array An array of information about the error messages
2150
 */
2151
function list_getUserErrors($start, $items_per_page, $sort, $where, $where_vars = array())
2152
{
2153
	global $smcFunc, $txt, $scripturl;
2154
2155
	// Get a list of error messages from this ip (range).
2156
	$request = $smcFunc['db_query']('', '
2157
		SELECT
2158
			le.log_time, le.ip, le.url, le.message, COALESCE(mem.id_member, 0) AS id_member,
2159
			COALESCE(mem.real_name, {string:guest_title}) AS display_name, mem.member_name
2160
		FROM {db_prefix}log_errors AS le
2161
			LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = le.id_member)
2162
		WHERE ' . $where . '
2163
		ORDER BY {raw:sort}
2164
		LIMIT {int:start}, {int:max}',
2165
		array_merge($where_vars, array(
2166
			'guest_title' => $txt['guest_title'],
2167
			'sort' => $sort,
2168
			'start' => $start,
2169
			'max' => $items_per_page,
2170
		))
2171
	);
2172
	$error_messages = array();
2173
	while ($row = $smcFunc['db_fetch_assoc']($request))
2174
		$error_messages[] = array(
2175
			'ip' => inet_dtop($row['ip']),
2176
			'member_link' => $row['id_member'] > 0 ? '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['display_name'] . '</a>' : $row['display_name'],
2177
			'message' => strtr($row['message'], array('&lt;span class=&quot;remove&quot;&gt;' => '', '&lt;/span&gt;' => '')),
2178
			'url' => $row['url'],
2179
			'time' => timeformat($row['log_time']),
2180
			'timestamp' => forum_time(true, $row['log_time']),
2181
		);
2182
	$smcFunc['db_free_result']($request);
2183
2184
	return $error_messages;
2185
}
2186
2187
/**
2188
 * Gets the number of posts made from a particular IP
2189
 *
2190
 * @param string $where A query indicating which posts to count
2191
 * @param array $where_vars The parameters for $where
2192
 * @return int Count of messages matching the IP
2193
 */
2194
function list_getIPMessageCount($where, $where_vars = array())
2195
{
2196
	global $smcFunc, $user_info;
2197
2198
	$request = $smcFunc['db_query']('', '
2199
		SELECT COUNT(id_msg)
2200
		FROM {db_prefix}messages AS m
2201
		WHERE {query_see_message_board} AND ' . $where,
2202
		$where_vars
2203
	);
2204
	list ($count) = $smcFunc['db_fetch_row']($request);
2205
	$smcFunc['db_free_result']($request);
2206
2207
	return (int) $count;
2208
}
2209
2210
/**
2211
 * Gets all the posts made from a particular IP
2212
 *
2213
 * @param int $start Which item to start with (for pagination purposes)
2214
 * @param int $items_per_page How many items to show on each page
2215
 * @param string $sort A string indicating how to sort the results
2216
 * @param string $where A query to filter which posts are returned
2217
 * @param array $where_vars An array of parameters for $where
2218
 * @return array An array containing information about the posts
2219
 */
2220
function list_getIPMessages($start, $items_per_page, $sort, $where, $where_vars = array())
2221
{
2222
	global $smcFunc, $scripturl, $user_info;
2223
2224
	// Get all the messages fitting this where clause.
2225
	$request = $smcFunc['db_query']('', '
2226
		SELECT
2227
			m.id_msg, m.poster_ip, COALESCE(mem.real_name, m.poster_name) AS display_name, mem.id_member,
2228
			m.subject, m.poster_time, m.id_topic, m.id_board
2229
		FROM {db_prefix}messages AS m
2230
			LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
2231
		WHERE {query_see_message_board} AND ' . $where . '
2232
		ORDER BY {raw:sort}
2233
		LIMIT {int:start}, {int:max}',
2234
		array_merge($where_vars, array(
2235
			'sort' => $sort,
2236
			'start' => $start,
2237
			'max' => $items_per_page,
2238
		))
2239
	);
2240
	$messages = array();
2241
	while ($row = $smcFunc['db_fetch_assoc']($request))
2242
		$messages[] = array(
2243
			'ip' => inet_dtop($row['poster_ip']),
2244
			'member_link' => empty($row['id_member']) ? $row['display_name'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['display_name'] . '</a>',
2245
			'board' => array(
2246
				'id' => $row['id_board'],
2247
				'href' => $scripturl . '?board=' . $row['id_board']
2248
			),
2249
			'topic' => $row['id_topic'],
2250
			'id' => $row['id_msg'],
2251
			'subject' => $row['subject'],
2252
			'time' => timeformat($row['poster_time']),
2253
			'timestamp' => forum_time(true, $row['poster_time'])
2254
		);
2255
	$smcFunc['db_free_result']($request);
2256
2257
	return $messages;
2258
}
2259
2260
/**
2261
 * Handles tracking a particular IP address
2262
 *
2263
 * @param int $memID The ID of a member whose IP we want to track
2264
 */
2265
function TrackIP($memID = 0)
2266
{
2267
	global $user_profile, $scripturl, $txt, $user_info, $modSettings, $sourcedir;
2268
	global $context, $options, $smcFunc;
2269
2270
	// Can the user do this?
2271
	isAllowedTo('moderate_forum');
2272
2273
	if ($memID == 0)
2274
	{
2275
		$context['ip'] = ip2range($user_info['ip']);
2276
		loadTemplate('Profile');
2277
		loadLanguage('Profile');
2278
		$context['sub_template'] = 'trackIP';
2279
		$context['page_title'] = $txt['profile'];
2280
		$context['base_url'] = $scripturl . '?action=trackip';
2281
	}
2282
	else
2283
	{
2284
		$context['ip'] = ip2range($user_profile[$memID]['member_ip']);
2285
		$context['base_url'] = $scripturl . '?action=profile;area=tracking;sa=ip;u=' . $memID;
2286
	}
2287
2288
	// Searching?
2289
	if (isset($_REQUEST['searchip']))
2290
		$context['ip'] = ip2range(trim($_REQUEST['searchip']));
2291
2292
	if (count($context['ip']) !== 2)
2293
		fatal_lang_error('invalid_tracking_ip', false);
2294
2295
	$ip_string = array('{inet:ip_address_low}', '{inet:ip_address_high}');
2296
	$fields = array(
2297
		'ip_address_low' => $context['ip']['low'],
2298
		'ip_address_high' => $context['ip']['high'],
2299
	);
2300
2301
	$ip_var = $context['ip'];
2302
2303
	if ($context['ip']['low'] !== $context['ip']['high'])
2304
		$context['ip'] = $context['ip']['low'] . ' - ' . $context['ip']['high'];
2305
	else
2306
		$context['ip'] = $context['ip']['low'];
2307
2308
	if (empty($context['tracking_area']))
2309
		$context['page_title'] = $txt['trackIP'] . ' - ' . $context['ip'];
2310
2311
	$request = $smcFunc['db_query']('', '
2312
		SELECT id_member, real_name AS display_name, member_ip
2313
		FROM {db_prefix}members
2314
		WHERE member_ip >= ' . $ip_string[0] . ' and member_ip <= ' . $ip_string[1],
2315
		$fields
2316
	);
2317
	$context['ips'] = array();
2318
	while ($row = $smcFunc['db_fetch_assoc']($request))
2319
		$context['ips'][inet_dtop($row['member_ip'])][] = '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['display_name'] . '</a>';
2320
	$smcFunc['db_free_result']($request);
2321
2322
	ksort($context['ips']);
2323
2324
	// For messages we use the "messages per page" option
2325
	$maxPerPage = empty($modSettings['disableCustomPerPage']) && !empty($options['messages_per_page']) ? $options['messages_per_page'] : $modSettings['defaultMaxMessages'];
2326
2327
	// Gonna want this for the list.
2328
	require_once($sourcedir . '/Subs-List.php');
2329
2330
	// Start with the user messages.
2331
	$listOptions = array(
2332
		'id' => 'track_message_list',
2333
		'title' => $txt['messages_from_ip'] . ' ' . $context['ip'],
2334
		'start_var_name' => 'messageStart',
2335
		'items_per_page' => $maxPerPage,
2336
		'no_items_label' => $txt['no_messages_from_ip'],
2337
		'base_href' => $context['base_url'] . ';searchip=' . $context['ip'],
2338
		'default_sort_col' => 'date',
2339
		'get_items' => array(
2340
			'function' => 'list_getIPMessages',
2341
			'params' => array(
2342
				'm.poster_ip >= ' . $ip_string[0] . ' and m.poster_ip <= ' . $ip_string[1],
2343
				$fields,
2344
			),
2345
		),
2346
		'get_count' => array(
2347
			'function' => 'list_getIPMessageCount',
2348
			'params' => array(
2349
				'm.poster_ip >= ' . $ip_string[0] . ' and m.poster_ip <= ' . $ip_string[1],
2350
				$fields,
2351
			),
2352
		),
2353
		'columns' => array(
2354
			'ip_address' => array(
2355
				'header' => array(
2356
					'value' => $txt['ip_address'],
2357
				),
2358
				'data' => array(
2359
					'sprintf' => array(
2360
						'format' => '<a href="' . $context['base_url'] . ';searchip=%1$s">%1$s</a>',
2361
						'params' => array(
2362
							'ip' => false,
2363
						),
2364
					),
2365
				),
2366
				'sort' => array(
2367
					'default' => 'm.poster_ip',
2368
					'reverse' => 'm.poster_ip DESC',
2369
				),
2370
			),
2371
			'poster' => array(
2372
				'header' => array(
2373
					'value' => $txt['poster'],
2374
				),
2375
				'data' => array(
2376
					'db' => 'member_link',
2377
				),
2378
			),
2379
			'subject' => array(
2380
				'header' => array(
2381
					'value' => $txt['subject'],
2382
				),
2383
				'data' => array(
2384
					'sprintf' => array(
2385
						'format' => '<a href="' . $scripturl . '?topic=%1$s.msg%2$s#msg%2$s" rel="nofollow">%3$s</a>',
2386
						'params' => array(
2387
							'topic' => false,
2388
							'id' => false,
2389
							'subject' => false,
2390
						),
2391
					),
2392
				),
2393
			),
2394
			'date' => array(
2395
				'header' => array(
2396
					'value' => $txt['date'],
2397
				),
2398
				'data' => array(
2399
					'db' => 'time',
2400
				),
2401
				'sort' => array(
2402
					'default' => 'm.id_msg DESC',
2403
					'reverse' => 'm.id_msg',
2404
				),
2405
			),
2406
		),
2407
		'additional_rows' => array(
2408
			array(
2409
				'position' => 'after_title',
2410
				'value' => $txt['messages_from_ip_desc'],
2411
			),
2412
		),
2413
	);
2414
2415
	// Create the messages list.
2416
	createList($listOptions);
2417
2418
	// Set the options for the error lists.
2419
	$listOptions = array(
2420
		'id' => 'track_user_list',
2421
		'title' => $txt['errors_from_ip'] . ' ' . $context['ip'],
2422
		'start_var_name' => 'errorStart',
2423
		'items_per_page' => $modSettings['defaultMaxListItems'],
2424
		'no_items_label' => $txt['no_errors_from_ip'],
2425
		'base_href' => $context['base_url'] . ';searchip=' . $context['ip'],
2426
		'default_sort_col' => 'date2',
2427
		'get_items' => array(
2428
			'function' => 'list_getUserErrors',
2429
			'params' => array(
2430
				'le.ip >= ' . $ip_string[0] . ' and le.ip <= ' . $ip_string[1],
2431
				$fields,
2432
			),
2433
		),
2434
		'get_count' => array(
2435
			'function' => 'list_getUserErrorCount',
2436
			'params' => array(
2437
				'ip >= ' . $ip_string[0] . ' and ip <= ' . $ip_string[1],
2438
				$fields,
2439
			),
2440
		),
2441
		'columns' => array(
2442
			'ip_address2' => array(
2443
				'header' => array(
2444
					'value' => $txt['ip_address'],
2445
				),
2446
				'data' => array(
2447
					'sprintf' => array(
2448
						'format' => '<a href="' . $context['base_url'] . ';searchip=%1$s">%1$s</a>',
2449
						'params' => array(
2450
							'ip' => false,
2451
						),
2452
					),
2453
				),
2454
				'sort' => array(
2455
					'default' => 'le.ip',
2456
					'reverse' => 'le.ip DESC',
2457
				),
2458
			),
2459
			'display_name' => array(
2460
				'header' => array(
2461
					'value' => $txt['display_name'],
2462
				),
2463
				'data' => array(
2464
					'db' => 'member_link',
2465
				),
2466
			),
2467
			'message' => array(
2468
				'header' => array(
2469
					'value' => $txt['message'],
2470
				),
2471
				'data' => array(
2472
					'sprintf' => array(
2473
						'format' => '%1$s<br><a href="%2$s">%2$s</a>',
2474
						'params' => array(
2475
							'message' => false,
2476
							'url' => false,
2477
						),
2478
					),
2479
				),
2480
			),
2481
			'date2' => array(
2482
				'header' => array(
2483
					'value' => $txt['date'],
2484
				),
2485
				'data' => array(
2486
					'db' => 'time',
2487
				),
2488
				'sort' => array(
2489
					'default' => 'le.id_error DESC',
2490
					'reverse' => 'le.id_error',
2491
				),
2492
			),
2493
		),
2494
		'additional_rows' => array(
2495
			array(
2496
				'position' => 'after_title',
2497
				'value' => $txt['errors_from_ip_desc'],
2498
			),
2499
		),
2500
	);
2501
2502
	// Create the error list.
2503
	createList($listOptions);
2504
2505
	// Allow 3rd party integrations to add in their own lists or whatever.
2506
	$context['additional_track_lists'] = array();
2507
	call_integration_hook('integrate_profile_trackip', array($ip_string, $ip_var));
2508
2509
	$context['single_ip'] = ($ip_var['low'] === $ip_var['high']);
2510
	if ($context['single_ip'])
2511
	{
2512
		$context['whois_servers'] = array(
2513
			'apnic' => array(
2514
				'name' => $txt['whois_apnic'],
2515
				'url' => 'https://wq.apnic.net/apnic-bin/whois.pl?searchtext=' . $context['ip'],
2516
			),
2517
			'arin' => array(
2518
				'name' => $txt['whois_arin'],
2519
				'url' => 'https://whois.arin.net/rest/ip/' . $context['ip'],
2520
			),
2521
			'lacnic' => array(
2522
				'name' => $txt['whois_lacnic'],
2523
				'url' => 'https://lacnic.net/cgi-bin/lacnic/whois?query=' . $context['ip'],
2524
			),
2525
			'ripe' => array(
2526
				'name' => $txt['whois_ripe'],
2527
				'url' => 'https://apps.db.ripe.net/search/query.html?searchtext=' . $context['ip'],
2528
			),
2529
		);
2530
	}
2531
}
2532
2533
/**
2534
 * Tracks a user's logins.
2535
 *
2536
 * @param int $memID The ID of the member
2537
 */
2538
function TrackLogins($memID = 0)
2539
{
2540
	global $scripturl, $txt, $sourcedir, $context;
2541
2542
	// Gonna want this for the list.
2543
	require_once($sourcedir . '/Subs-List.php');
2544
2545
	if ($memID == 0)
2546
		$context['base_url'] = $scripturl . '?action=trackip';
2547
	else
2548
		$context['base_url'] = $scripturl . '?action=profile;area=tracking;sa=ip;u=' . $memID;
2549
2550
	// Start with the user messages.
2551
	$listOptions = array(
2552
		'id' => 'track_logins_list',
2553
		'title' => $txt['trackLogins'],
2554
		'no_items_label' => $txt['trackLogins_none_found'],
2555
		'base_href' => $context['base_url'],
2556
		'get_items' => array(
2557
			'function' => 'list_getLogins',
2558
			'params' => array(
2559
				'id_member = {int:current_member}',
2560
				array('current_member' => $memID),
2561
			),
2562
		),
2563
		'get_count' => array(
2564
			'function' => 'list_getLoginCount',
2565
			'params' => array(
2566
				'id_member = {int:current_member}',
2567
				array('current_member' => $memID),
2568
			),
2569
		),
2570
		'columns' => array(
2571
			'time' => array(
2572
				'header' => array(
2573
					'value' => $txt['date'],
2574
				),
2575
				'data' => array(
2576
					'db' => 'time',
2577
				),
2578
			),
2579
			'ip' => array(
2580
				'header' => array(
2581
					'value' => $txt['ip_address'],
2582
				),
2583
				'data' => array(
2584
					'sprintf' => array(
2585
						'format' => '<a href="' . $context['base_url'] . ';searchip=%1$s">%1$s</a> (<a href="' . $context['base_url'] . ';searchip=%2$s">%2$s</a>) ',
2586
						'params' => array(
2587
							'ip' => false,
2588
							'ip2' => false
2589
						),
2590
					),
2591
				),
2592
			),
2593
		),
2594
		'additional_rows' => array(
2595
			array(
2596
				'position' => 'after_title',
2597
				'value' => $txt['trackLogins_desc'],
2598
			),
2599
		),
2600
	);
2601
2602
	// Create the messages list.
2603
	createList($listOptions);
2604
2605
	$context['sub_template'] = 'show_list';
2606
	$context['default_list'] = 'track_logins_list';
2607
}
2608
2609
/**
2610
 * Finds the total number of tracked logins for a particular user
2611
 *
2612
 * @param string $where A query to limit which logins are counted
2613
 * @param array $where_vars An array of parameters for $where
2614
 * @return int count of messages matching the IP
2615
 */
2616
function list_getLoginCount($where, $where_vars = array())
2617
{
2618
	global $smcFunc;
2619
2620
	$request = $smcFunc['db_query']('', '
2621
		SELECT COUNT(*) AS message_count
2622
		FROM {db_prefix}member_logins
2623
		WHERE id_member = {int:id_member}',
2624
		array(
2625
			'id_member' => $where_vars['current_member'],
2626
		)
2627
	);
2628
	list ($count) = $smcFunc['db_fetch_row']($request);
2629
	$smcFunc['db_free_result']($request);
2630
2631
	return (int) $count;
2632
}
2633
2634
/**
2635
 * Callback for the list in trackLogins.
2636
 *
2637
 * @param int $start Which item to start with (not used here)
2638
 * @param int $items_per_page How many items to show on each page (not used here)
2639
 * @param string $sort A string indicating
2640
 * @param string $where A query to filter results (not used here)
2641
 * @param array $where_vars An array of parameters for $where. Only 'current_member' (the ID of the member) is used here
2642
 * @return array An array of information about user logins
2643
 */
2644
function list_getLogins($start, $items_per_page, $sort, $where, $where_vars = array())
2645
{
2646
	global $smcFunc;
2647
2648
	$request = $smcFunc['db_query']('', '
2649
		SELECT time, ip, ip2
2650
		FROM {db_prefix}member_logins
2651
		WHERE id_member = {int:id_member}
2652
		ORDER BY time DESC',
2653
		array(
2654
			'id_member' => $where_vars['current_member'],
2655
		)
2656
	);
2657
	$logins = array();
2658
	while ($row = $smcFunc['db_fetch_assoc']($request))
2659
		$logins[] = array(
2660
			'time' => timeformat($row['time']),
2661
			'ip' => inet_dtop($row['ip']),
2662
			'ip2' => inet_dtop($row['ip2']),
2663
		);
2664
	$smcFunc['db_free_result']($request);
2665
2666
	return $logins;
2667
}
2668
2669
/**
2670
 * Tracks a user's profile edits
2671
 *
2672
 * @param int $memID The ID of the member
2673
 */
2674
function trackEdits($memID)
2675
{
2676
	global $scripturl, $txt, $modSettings, $sourcedir, $context, $smcFunc;
2677
2678
	require_once($sourcedir . '/Subs-List.php');
2679
2680
	// Get the names of any custom fields.
2681
	$request = $smcFunc['db_query']('', '
2682
		SELECT col_name, field_name, bbc
2683
		FROM {db_prefix}custom_fields',
2684
		array(
2685
		)
2686
	);
2687
	$context['custom_field_titles'] = array();
2688
	while ($row = $smcFunc['db_fetch_assoc']($request))
2689
		$context['custom_field_titles']['customfield_' . $row['col_name']] = array(
2690
			'title' => $row['field_name'],
2691
			'parse_bbc' => $row['bbc'],
2692
		);
2693
	$smcFunc['db_free_result']($request);
2694
2695
	// Set the options for the error lists.
2696
	$listOptions = array(
2697
		'id' => 'edit_list',
2698
		'title' => $txt['trackEdits'],
2699
		'items_per_page' => $modSettings['defaultMaxListItems'],
2700
		'no_items_label' => $txt['trackEdit_no_edits'],
2701
		'base_href' => $scripturl . '?action=profile;area=tracking;sa=edits;u=' . $memID,
2702
		'default_sort_col' => 'time',
2703
		'get_items' => array(
2704
			'function' => 'list_getProfileEdits',
2705
			'params' => array(
2706
				$memID,
2707
			),
2708
		),
2709
		'get_count' => array(
2710
			'function' => 'list_getProfileEditCount',
2711
			'params' => array(
2712
				$memID,
2713
			),
2714
		),
2715
		'columns' => array(
2716
			'action' => array(
2717
				'header' => array(
2718
					'value' => $txt['trackEdit_action'],
2719
				),
2720
				'data' => array(
2721
					'db' => 'action_text',
2722
				),
2723
			),
2724
			'before' => array(
2725
				'header' => array(
2726
					'value' => $txt['trackEdit_before'],
2727
				),
2728
				'data' => array(
2729
					'db' => 'before',
2730
				),
2731
			),
2732
			'after' => array(
2733
				'header' => array(
2734
					'value' => $txt['trackEdit_after'],
2735
				),
2736
				'data' => array(
2737
					'db' => 'after',
2738
				),
2739
			),
2740
			'time' => array(
2741
				'header' => array(
2742
					'value' => $txt['date'],
2743
				),
2744
				'data' => array(
2745
					'db' => 'time',
2746
				),
2747
				'sort' => array(
2748
					'default' => 'id_action DESC',
2749
					'reverse' => 'id_action',
2750
				),
2751
			),
2752
			'applicator' => array(
2753
				'header' => array(
2754
					'value' => $txt['trackEdit_applicator'],
2755
				),
2756
				'data' => array(
2757
					'db' => 'member_link',
2758
				),
2759
			),
2760
		),
2761
	);
2762
2763
	// Create the error list.
2764
	createList($listOptions);
2765
2766
	$context['sub_template'] = 'show_list';
2767
	$context['default_list'] = 'edit_list';
2768
}
2769
2770
/**
2771
 * How many edits?
2772
 *
2773
 * @param int $memID The ID of the member
2774
 * @return int The number of profile edits
2775
 */
2776
function list_getProfileEditCount($memID)
2777
{
2778
	global $smcFunc;
2779
2780
	$request = $smcFunc['db_query']('', '
2781
		SELECT COUNT(*) AS edit_count
2782
		FROM {db_prefix}log_actions
2783
		WHERE id_log = {int:log_type}
2784
			AND id_member = {int:owner}',
2785
		array(
2786
			'log_type' => 2,
2787
			'owner' => $memID,
2788
		)
2789
	);
2790
	list ($edit_count) = $smcFunc['db_fetch_row']($request);
2791
	$smcFunc['db_free_result']($request);
2792
2793
	return (int) $edit_count;
2794
}
2795
2796
/**
2797
 * Loads up information about a user's profile edits. Callback for the list in trackEdits()
2798
 *
2799
 * @param int $start Which item to start with (for pagination purposes)
2800
 * @param int $items_per_page How many items to show on each page
2801
 * @param string $sort A string indicating how to sort the results
2802
 * @param int $memID The ID of the member
2803
 * @return array An array of information about the profile edits
2804
 */
2805
function list_getProfileEdits($start, $items_per_page, $sort, $memID)
2806
{
2807
	global $smcFunc, $txt, $scripturl, $context;
2808
2809
	// Get a list of error messages from this ip (range).
2810
	$request = $smcFunc['db_query']('', '
2811
		SELECT
2812
			id_action, id_member, ip, log_time, action, extra
2813
		FROM {db_prefix}log_actions
2814
		WHERE id_log = {int:log_type}
2815
			AND id_member = {int:owner}
2816
		ORDER BY {raw:sort}
2817
		LIMIT {int:start}, {int:max}',
2818
		array(
2819
			'log_type' => 2,
2820
			'owner' => $memID,
2821
			'sort' => $sort,
2822
			'start' => $start,
2823
			'max' => $items_per_page,
2824
		)
2825
	);
2826
	$edits = array();
2827
	$members = array();
2828
	while ($row = $smcFunc['db_fetch_assoc']($request))
2829
	{
2830
		$extra = $smcFunc['json_decode']($row['extra'], true);
2831
		if (!empty($extra['applicator']))
2832
			$members[] = $extra['applicator'];
2833
2834
		// Work out what the name of the action is.
2835
		if (isset($txt['trackEdit_action_' . $row['action']]))
2836
			$action_text = $txt['trackEdit_action_' . $row['action']];
2837
		elseif (isset($txt[$row['action']]))
2838
			$action_text = $txt[$row['action']];
2839
		// Custom field?
2840
		elseif (isset($context['custom_field_titles'][$row['action']]))
2841
			$action_text = $context['custom_field_titles'][$row['action']]['title'];
2842
		else
2843
			$action_text = $row['action'];
2844
2845
		// Parse BBC?
2846
		$parse_bbc = isset($context['custom_field_titles'][$row['action']]) && $context['custom_field_titles'][$row['action']]['parse_bbc'] ? true : false;
2847
2848
		$edits[] = array(
2849
			'id' => $row['id_action'],
2850
			'ip' => inet_dtop($row['ip']),
2851
			'id_member' => !empty($extra['applicator']) ? $extra['applicator'] : 0,
2852
			'member_link' => $txt['trackEdit_deleted_member'],
2853
			'action' => $row['action'],
2854
			'action_text' => $action_text,
2855
			'before' => !empty($extra['previous']) ? ($parse_bbc ? parse_bbc($extra['previous']) : $extra['previous']) : '',
2856
			'after' => !empty($extra['new']) ? ($parse_bbc ? parse_bbc($extra['new']) : $extra['new']) : '',
2857
			'time' => timeformat($row['log_time']),
2858
		);
2859
	}
2860
	$smcFunc['db_free_result']($request);
2861
2862
	// Get any member names.
2863
	if (!empty($members))
2864
	{
2865
		$request = $smcFunc['db_query']('', '
2866
			SELECT
2867
				id_member, real_name
2868
			FROM {db_prefix}members
2869
			WHERE id_member IN ({array_int:members})',
2870
			array(
2871
				'members' => $members,
2872
			)
2873
		);
2874
		$members = array();
2875
		while ($row = $smcFunc['db_fetch_assoc']($request))
2876
			$members[$row['id_member']] = $row['real_name'];
2877
		$smcFunc['db_free_result']($request);
2878
2879
		foreach ($edits as $key => $value)
2880
			if (isset($members[$value['id_member']]))
2881
				$edits[$key]['member_link'] = '<a href="' . $scripturl . '?action=profile;u=' . $value['id_member'] . '">' . $members[$value['id_member']] . '</a>';
2882
	}
2883
2884
	return $edits;
2885
}
2886
2887
/**
2888
 * Display the history of group requests made by the user whose profile we are viewing.
2889
 *
2890
 * @param int $memID The ID of the member
2891
 */
2892
function trackGroupReq($memID)
2893
{
2894
	global $scripturl, $txt, $modSettings, $sourcedir, $context;
2895
2896
	require_once($sourcedir . '/Subs-List.php');
2897
2898
	// Set the options for the error lists.
2899
	$listOptions = array(
2900
		'id' => 'request_list',
2901
		'title' => sprintf($txt['trackGroupRequests_title'], $context['member']['name']),
2902
		'items_per_page' => $modSettings['defaultMaxListItems'],
2903
		'no_items_label' => $txt['requested_none'],
2904
		'base_href' => $scripturl . '?action=profile;area=tracking;sa=groupreq;u=' . $memID,
2905
		'default_sort_col' => 'time_applied',
2906
		'get_items' => array(
2907
			'function' => 'list_getGroupRequests',
2908
			'params' => array(
2909
				$memID,
2910
			),
2911
		),
2912
		'get_count' => array(
2913
			'function' => 'list_getGroupRequestsCount',
2914
			'params' => array(
2915
				$memID,
2916
			),
2917
		),
2918
		'columns' => array(
2919
			'group' => array(
2920
				'header' => array(
2921
					'value' => $txt['requested_group'],
2922
				),
2923
				'data' => array(
2924
					'db' => 'group_name',
2925
				),
2926
			),
2927
			'group_reason' => array(
2928
				'header' => array(
2929
					'value' => $txt['requested_group_reason'],
2930
				),
2931
				'data' => array(
2932
					'db' => 'group_reason',
2933
				),
2934
			),
2935
			'time_applied' => array(
2936
				'header' => array(
2937
					'value' => $txt['requested_group_time'],
2938
				),
2939
				'data' => array(
2940
					'db' => 'time_applied',
2941
					'timeformat' => true,
2942
				),
2943
				'sort' => array(
2944
					'default' => 'time_applied DESC',
2945
					'reverse' => 'time_applied',
2946
				),
2947
			),
2948
			'outcome' => array(
2949
				'header' => array(
2950
					'value' => $txt['requested_group_outcome'],
2951
				),
2952
				'data' => array(
2953
					'db' => 'outcome',
2954
				),
2955
			),
2956
		),
2957
	);
2958
2959
	// Create the error list.
2960
	createList($listOptions);
2961
2962
	$context['sub_template'] = 'show_list';
2963
	$context['default_list'] = 'request_list';
2964
}
2965
2966
/**
2967
 * How many edits?
2968
 *
2969
 * @param int $memID The ID of the member
2970
 * @return int The number of profile edits
2971
 */
2972
function list_getGroupRequestsCount($memID)
2973
{
2974
	global $smcFunc, $user_info;
2975
2976
	$request = $smcFunc['db_query']('', '
2977
		SELECT COUNT(*) AS req_count
2978
		FROM {db_prefix}log_group_requests AS lgr
2979
		WHERE id_member = {int:memID}
2980
			AND ' . ($user_info['mod_cache']['gq'] == '1=1' ? $user_info['mod_cache']['gq'] : 'lgr.' . $user_info['mod_cache']['gq']),
2981
		array(
2982
			'memID' => $memID,
2983
		)
2984
	);
2985
	list ($report_count) = $smcFunc['db_fetch_row']($request);
2986
	$smcFunc['db_free_result']($request);
2987
2988
	return (int) $report_count;
2989
}
2990
2991
/**
2992
 * Loads up information about a user's group requests. Callback for the list in trackGroupReq()
2993
 *
2994
 * @param int $start Which item to start with (for pagination purposes)
2995
 * @param int $items_per_page How many items to show on each page
2996
 * @param string $sort A string indicating how to sort the results
2997
 * @param int $memID The ID of the member
2998
 * @return array An array of information about the user's group requests
2999
 */
3000
function list_getGroupRequests($start, $items_per_page, $sort, $memID)
3001
{
3002
	global $smcFunc, $txt, $scripturl, $user_info;
3003
3004
	$groupreq = array();
3005
3006
	$request = $smcFunc['db_query']('', '
3007
		SELECT
3008
			lgr.id_group, mg.group_name, mg.online_color, lgr.time_applied, lgr.reason, lgr.status,
3009
			ma.id_member AS id_member_acted, COALESCE(ma.member_name, lgr.member_name_acted) AS act_name, lgr.time_acted, lgr.act_reason
3010
		FROM {db_prefix}log_group_requests AS lgr
3011
			LEFT JOIN {db_prefix}members AS ma ON (lgr.id_member_acted = ma.id_member)
3012
			INNER JOIN {db_prefix}membergroups AS mg ON (lgr.id_group = mg.id_group)
3013
		WHERE lgr.id_member = {int:memID}
3014
			AND ' . ($user_info['mod_cache']['gq'] == '1=1' ? $user_info['mod_cache']['gq'] : 'lgr.' . $user_info['mod_cache']['gq']) . '
3015
		ORDER BY {raw:sort}
3016
		LIMIT {int:start}, {int:max}',
3017
		array(
3018
			'memID' => $memID,
3019
			'sort' => $sort,
3020
			'start' => $start,
3021
			'max' => $items_per_page,
3022
		)
3023
	);
3024
	while ($row = $smcFunc['db_fetch_assoc']($request))
3025
	{
3026
		$this_req = array(
3027
			'group_name' => empty($row['online_color']) ? $row['group_name'] : '<span style="color:' . $row['online_color'] . '">' . $row['group_name'] . '</span>',
3028
			'group_reason' => $row['reason'],
3029
			'time_applied' => $row['time_applied'],
3030
		);
3031
		switch ($row['status'])
3032
		{
3033
			case 0:
3034
				$this_req['outcome'] = $txt['outcome_pending'];
3035
				break;
3036
			case 1:
3037
				$member_link = empty($row['id_member_acted']) ? $row['act_name'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member_acted'] . '">' . $row['act_name'] . '</a>';
3038
				$this_req['outcome'] = sprintf($txt['outcome_approved'], $member_link, timeformat($row['time_acted']));
3039
				break;
3040
			case 2:
3041
				$member_link = empty($row['id_member_acted']) ? $row['act_name'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member_acted'] . '">' . $row['act_name'] . '</a>';
3042
				$this_req['outcome'] = sprintf(!empty($row['act_reason']) ? $txt['outcome_refused_reason'] : $txt['outcome_refused'], $member_link, timeformat($row['time_acted']), $row['act_reason']);
3043
				break;
3044
		}
3045
3046
		$groupreq[] = $this_req;
3047
	}
3048
	$smcFunc['db_free_result']($request);
3049
3050
	return $groupreq;
3051
}
3052
3053
/**
3054
 * Shows which permissions a user has
3055
 *
3056
 * @param int $memID The ID of the member
3057
 */
3058
function showPermissions($memID)
3059
{
3060
	global $txt, $board;
3061
	global $user_profile, $context, $sourcedir, $smcFunc;
3062
3063
	// Verify if the user has sufficient permissions.
3064
	isAllowedTo('manage_permissions');
3065
3066
	loadLanguage('ManagePermissions');
3067
	loadLanguage('Admin');
3068
	loadTemplate('ManageMembers');
3069
3070
	// Load all the permission profiles.
3071
	require_once($sourcedir . '/ManagePermissions.php');
3072
	loadPermissionProfiles();
3073
3074
	$context['member']['id'] = $memID;
3075
	$context['member']['name'] = $user_profile[$memID]['real_name'];
3076
3077
	$context['page_title'] = $txt['showPermissions'];
3078
	$board = empty($board) ? 0 : (int) $board;
3079
	$context['board'] = $board;
3080
3081
	// Determine which groups this user is in.
3082
	if (empty($user_profile[$memID]['additional_groups']))
3083
		$curGroups = array();
3084
	else
3085
		$curGroups = explode(',', $user_profile[$memID]['additional_groups']);
3086
	$curGroups[] = $user_profile[$memID]['id_group'];
3087
	$curGroups[] = $user_profile[$memID]['id_post_group'];
3088
3089
	// Load a list of boards for the jump box - except the defaults.
3090
	$request = $smcFunc['db_query']('order_by_board_order', '
3091
		SELECT b.id_board, b.name, b.id_profile, b.member_groups, COALESCE(mods.id_member, modgs.id_group, 0) AS is_mod
3092
		FROM {db_prefix}boards AS b
3093
			LEFT JOIN {db_prefix}moderators AS mods ON (mods.id_board = b.id_board AND mods.id_member = {int:current_member})
3094
			LEFT JOIN {db_prefix}moderator_groups AS modgs ON (modgs.id_board = b.id_board AND modgs.id_group IN ({array_int:current_groups}))
3095
		WHERE {query_see_board}',
3096
		array(
3097
			'current_member' => $memID,
3098
			'current_groups' => $curGroups,
3099
		)
3100
	);
3101
	$context['boards'] = array();
3102
	$context['no_access_boards'] = array();
3103
	while ($row = $smcFunc['db_fetch_assoc']($request))
3104
	{
3105
		if (count(array_intersect($curGroups, explode(',', $row['member_groups']))) === 0 && !$row['is_mod'])
3106
			$context['no_access_boards'][] = array(
3107
				'id' => $row['id_board'],
3108
				'name' => $row['name'],
3109
				'is_last' => false,
3110
			);
3111
		elseif ($row['id_profile'] != 1 || $row['is_mod'])
3112
			$context['boards'][$row['id_board']] = array(
3113
				'id' => $row['id_board'],
3114
				'name' => $row['name'],
3115
				'selected' => $board == $row['id_board'],
3116
				'profile' => $row['id_profile'],
3117
				'profile_name' => $context['profiles'][$row['id_profile']]['name'],
3118
			);
3119
	}
3120
	$smcFunc['db_free_result']($request);
3121
3122
	require_once($sourcedir . '/Subs-Boards.php');
3123
	sortBoards($context['boards']);
3124
3125
	if (!empty($context['no_access_boards']))
3126
		$context['no_access_boards'][count($context['no_access_boards']) - 1]['is_last'] = true;
3127
3128
	$context['member']['permissions'] = array(
3129
		'general' => array(),
3130
		'board' => array()
3131
	);
3132
3133
	// If you're an admin we know you can do everything, we might as well leave.
3134
	$context['member']['has_all_permissions'] = in_array(1, $curGroups);
3135
	if ($context['member']['has_all_permissions'])
3136
		return;
3137
3138
	$denied = array();
3139
3140
	// Get all general permissions.
3141
	$result = $smcFunc['db_query']('', '
3142
		SELECT p.permission, p.add_deny, mg.group_name, p.id_group
3143
		FROM {db_prefix}permissions AS p
3144
			LEFT JOIN {db_prefix}membergroups AS mg ON (mg.id_group = p.id_group)
3145
		WHERE p.id_group IN ({array_int:group_list})
3146
		ORDER BY p.add_deny DESC, p.permission, mg.min_posts, CASE WHEN mg.id_group < {int:newbie_group} THEN mg.id_group ELSE 4 END, mg.group_name',
3147
		array(
3148
			'group_list' => $curGroups,
3149
			'newbie_group' => 4,
3150
		)
3151
	);
3152
	while ($row = $smcFunc['db_fetch_assoc']($result))
3153
	{
3154
		// We don't know about this permission, it doesn't exist :P.
3155
		if (!isset($txt['permissionname_' . $row['permission']]))
3156
			continue;
3157
3158
		if (empty($row['add_deny']))
3159
			$denied[] = $row['permission'];
3160
3161
		// Permissions that end with _own or _any consist of two parts.
3162
		if (in_array(substr($row['permission'], -4), array('_own', '_any')) && isset($txt['permissionname_' . substr($row['permission'], 0, -4)]))
3163
			$name = $txt['permissionname_' . substr($row['permission'], 0, -4)] . ' - ' . $txt['permissionname_' . $row['permission']];
3164
		else
3165
			$name = $txt['permissionname_' . $row['permission']];
3166
3167
		// Add this permission if it doesn't exist yet.
3168
		if (!isset($context['member']['permissions']['general'][$row['permission']]))
3169
			$context['member']['permissions']['general'][$row['permission']] = array(
3170
				'id' => $row['permission'],
3171
				'groups' => array(
3172
					'allowed' => array(),
3173
					'denied' => array()
3174
				),
3175
				'name' => $name,
3176
				'is_denied' => false,
3177
				'is_global' => true,
3178
			);
3179
3180
		// Add the membergroup to either the denied or the allowed groups.
3181
		$context['member']['permissions']['general'][$row['permission']]['groups'][empty($row['add_deny']) ? 'denied' : 'allowed'][] = $row['id_group'] == 0 ? $txt['membergroups_members'] : $row['group_name'];
3182
3183
		// Once denied is always denied.
3184
		$context['member']['permissions']['general'][$row['permission']]['is_denied'] |= empty($row['add_deny']);
3185
	}
3186
	$smcFunc['db_free_result']($result);
3187
3188
	$request = $smcFunc['db_query']('', '
3189
		SELECT
3190
			bp.add_deny, bp.permission, bp.id_group, mg.group_name' . (empty($board) ? '' : ',
3191
			b.id_profile, CASE WHEN (mods.id_member IS NULL AND modgs.id_group IS NULL) THEN 0 ELSE 1 END AS is_moderator') . '
3192
		FROM {db_prefix}board_permissions AS bp' . (empty($board) ? '' : '
3193
			INNER JOIN {db_prefix}boards AS b ON (b.id_board = {int:current_board})
3194
			LEFT JOIN {db_prefix}moderators AS mods ON (mods.id_board = b.id_board AND mods.id_member = {int:current_member})
3195
			LEFT JOIN {db_prefix}moderator_groups AS modgs ON (modgs.id_board = b.id_board AND modgs.id_group IN ({array_int:group_list}))') . '
3196
			LEFT JOIN {db_prefix}membergroups AS mg ON (mg.id_group = bp.id_group)
3197
		WHERE bp.id_profile = {raw:current_profile}
3198
			AND bp.id_group IN ({array_int:group_list}' . (empty($board) ? ')' : ', {int:moderator_group})
3199
			AND (mods.id_member IS NOT NULL OR modgs.id_group IS NOT NULL OR bp.id_group != {int:moderator_group})'),
3200
		array(
3201
			'current_board' => $board,
3202
			'group_list' => $curGroups,
3203
			'current_member' => $memID,
3204
			'current_profile' => empty($board) ? '1' : 'b.id_profile',
3205
			'moderator_group' => 3,
3206
		)
3207
	);
3208
3209
	while ($row = $smcFunc['db_fetch_assoc']($request))
3210
	{
3211
		// We don't know about this permission, it doesn't exist :P.
3212
		if (!isset($txt['permissionname_' . $row['permission']]))
3213
			continue;
3214
3215
		// The name of the permission using the format 'permission name' - 'own/any topic/event/etc.'.
3216
		if (in_array(substr($row['permission'], -4), array('_own', '_any')) && isset($txt['permissionname_' . substr($row['permission'], 0, -4)]))
3217
			$name = $txt['permissionname_' . substr($row['permission'], 0, -4)] . ' - ' . $txt['permissionname_' . $row['permission']];
3218
		else
3219
			$name = $txt['permissionname_' . $row['permission']];
3220
3221
		// Create the structure for this permission.
3222
		if (!isset($context['member']['permissions']['board'][$row['permission']]))
3223
			$context['member']['permissions']['board'][$row['permission']] = array(
3224
				'id' => $row['permission'],
3225
				'groups' => array(
3226
					'allowed' => array(),
3227
					'denied' => array()
3228
				),
3229
				'name' => $name,
3230
				'is_denied' => false,
3231
				'is_global' => empty($board),
3232
			);
3233
3234
		$context['member']['permissions']['board'][$row['permission']]['groups'][empty($row['add_deny']) ? 'denied' : 'allowed'][$row['id_group']] = $row['id_group'] == 0 ? $txt['membergroups_members'] : $row['group_name'];
3235
3236
		$context['member']['permissions']['board'][$row['permission']]['is_denied'] |= empty($row['add_deny']);
3237
	}
3238
	$smcFunc['db_free_result']($request);
3239
}
3240
3241
/**
3242
 * View a member's warnings
3243
 *
3244
 * @param int $memID The ID of the member
3245
 */
3246
function viewWarning($memID)
3247
{
3248
	global $modSettings, $context, $sourcedir, $txt, $scripturl;
3249
3250
	// Firstly, can we actually even be here?
3251
	if (!($context['user']['is_owner'] && allowedTo('view_warning_own')) && !allowedTo('view_warning_any') && !allowedTo('issue_warning') && !allowedTo('moderate_forum'))
3252
		fatal_lang_error('no_access', false);
3253
3254
	// Make sure things which are disabled stay disabled.
3255
	$modSettings['warning_watch'] = !empty($modSettings['warning_watch']) ? $modSettings['warning_watch'] : 110;
3256
	$modSettings['warning_moderate'] = !empty($modSettings['warning_moderate']) && !empty($modSettings['postmod_active']) ? $modSettings['warning_moderate'] : 110;
3257
	$modSettings['warning_mute'] = !empty($modSettings['warning_mute']) ? $modSettings['warning_mute'] : 110;
3258
3259
	// Let's use a generic list to get all the current warnings, and use the issue warnings grab-a-granny thing.
3260
	require_once($sourcedir . '/Subs-List.php');
3261
	require_once($sourcedir . '/Profile-Actions.php');
3262
3263
	$listOptions = array(
3264
		'id' => 'view_warnings',
3265
		'title' => $txt['profile_viewwarning_previous_warnings'],
3266
		'items_per_page' => $modSettings['defaultMaxListItems'],
3267
		'no_items_label' => $txt['profile_viewwarning_no_warnings'],
3268
		'base_href' => $scripturl . '?action=profile;area=viewwarning;sa=user;u=' . $memID,
3269
		'default_sort_col' => 'log_time',
3270
		'get_items' => array(
3271
			'function' => 'list_getUserWarnings',
3272
			'params' => array(
3273
				$memID,
3274
			),
3275
		),
3276
		'get_count' => array(
3277
			'function' => 'list_getUserWarningCount',
3278
			'params' => array(
3279
				$memID,
3280
			),
3281
		),
3282
		'columns' => array(
3283
			'log_time' => array(
3284
				'header' => array(
3285
					'value' => $txt['profile_warning_previous_time'],
3286
				),
3287
				'data' => array(
3288
					'db' => 'time',
3289
				),
3290
				'sort' => array(
3291
					'default' => 'lc.log_time DESC',
3292
					'reverse' => 'lc.log_time',
3293
				),
3294
			),
3295
			'reason' => array(
3296
				'header' => array(
3297
					'value' => $txt['profile_warning_previous_reason'],
3298
					'style' => 'width: 50%;',
3299
				),
3300
				'data' => array(
3301
					'db' => 'reason',
3302
				),
3303
			),
3304
			'level' => array(
3305
				'header' => array(
3306
					'value' => $txt['profile_warning_previous_level'],
3307
				),
3308
				'data' => array(
3309
					'db' => 'counter',
3310
				),
3311
				'sort' => array(
3312
					'default' => 'lc.counter DESC',
3313
					'reverse' => 'lc.counter',
3314
				),
3315
			),
3316
		),
3317
		'additional_rows' => array(
3318
			array(
3319
				'position' => 'after_title',
3320
				'value' => $txt['profile_viewwarning_desc'],
3321
				'class' => 'smalltext',
3322
				'style' => 'padding: 2ex;',
3323
			),
3324
		),
3325
	);
3326
3327
	// Create the list for viewing.
3328
	require_once($sourcedir . '/Subs-List.php');
3329
	createList($listOptions);
3330
3331
	// Create some common text bits for the template.
3332
	$context['level_effects'] = array(
3333
		0 => '',
3334
		$modSettings['warning_watch'] => $txt['profile_warning_effect_own_watched'],
3335
		$modSettings['warning_moderate'] => $txt['profile_warning_effect_own_moderated'],
3336
		$modSettings['warning_mute'] => $txt['profile_warning_effect_own_muted'],
3337
	);
3338
	$context['current_level'] = 0;
3339
	foreach ($context['level_effects'] as $limit => $dummy)
3340
		if ($context['member']['warning'] >= $limit)
3341
			$context['current_level'] = $limit;
3342
}
3343
3344
/**
3345
 * Sets the icon for a fetched alert.
3346
 *
3347
 * @param array The alert that we want to set an icon for.
0 ignored issues
show
Bug introduced by
The type The was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
3348
 */
3349
function set_alert_icon($alert)
3350
{
3351
	global $settings;
3352
3353
	switch ($alert['content_type'])
3354
	{
3355
		case 'topic':
3356
		case 'board':
3357
			{
3358
				switch ($alert['content_action'])
3359
				{
3360
					case 'reply':
3361
					case 'topic':
3362
						$class = 'main_icons posts';
3363
						break;
3364
3365
					case 'move':
3366
						$src = $settings['images_url'] . '/post/moved.png';
3367
						break;
3368
3369
					case 'remove':
3370
						$class = 'main_icons delete';
3371
						break;
3372
3373
					case 'lock':
3374
					case 'unlock':
3375
						$class = 'main_icons lock';
3376
						break;
3377
3378
					case 'sticky':
3379
					case 'unsticky':
3380
						$class = 'main_icons sticky';
3381
						break;
3382
3383
					case 'split':
3384
						$class = 'main_icons split_button';
3385
						break;
3386
3387
					case 'merge':
3388
						$class = 'main_icons merge';
3389
						break;
3390
3391
					case 'unapproved_topic':
3392
					case 'unapproved_post':
3393
						$class = 'main_icons post_moderation_moderate';
3394
						break;
3395
3396
					default:
3397
						$class = 'main_icons posts';
3398
						break;
3399
				}
3400
			}
3401
			break;
3402
3403
		case 'msg':
3404
			{
3405
				switch ($alert['content_action'])
3406
				{
3407
					case 'like':
3408
						$class = 'main_icons like';
3409
						break;
3410
3411
					case 'mention':
3412
						$class = 'main_icons im_on';
3413
						break;
3414
3415
					case 'quote':
3416
						$class = 'main_icons quote';
3417
						break;
3418
3419
					case 'unapproved_attachment':
3420
						$class = 'main_icons post_moderation_attach';
3421
						break;
3422
3423
					case 'report':
3424
					case 'report_reply':
3425
						$class = 'main_icons post_moderation_moderate';
3426
						break;
3427
3428
					default:
3429
						$class = 'main_icons posts';
3430
						break;
3431
				}
3432
			}
3433
			break;
3434
3435
		case 'member':
3436
			{
3437
				switch ($alert['content_action'])
3438
				{
3439
					case 'register_standard':
3440
					case 'register_approval':
3441
					case 'register_activation':
3442
						$class = 'main_icons members';
3443
						break;
3444
3445
					case 'report':
3446
					case 'report_reply':
3447
						$class = 'main_icons members_watched';
3448
						break;
3449
3450
					case 'buddy_request':
3451
						$class = 'main_icons people';
3452
						break;
3453
3454
					case 'group_request':
3455
						$class = 'main_icons members_request';
3456
						break;
3457
3458
					default:
3459
						$class = 'main_icons members';
3460
						break;
3461
				}
3462
			}
3463
			break;
3464
3465
		case 'groupr':
3466
			$class = 'main_icons members_request';
3467
			break;
3468
3469
		case 'event':
3470
			$class = 'main_icons calendar';
3471
			break;
3472
3473
		case 'paidsubs':
3474
			$class = 'main_icons paid';
3475
			break;
3476
3477
		case 'birthday':
3478
			$src = $settings['images_url'] . '/cake.png';
3479
			break;
3480
3481
		default:
3482
			$class = 'main_icons alerts';
3483
			break;
3484
	}
3485
3486
	if (isset($class))
3487
		return '<span class="alert_icon ' . $class . '"></span>';
3488
	elseif (isset($src))
3489
		return '<img class="alert_icon" src="' . $src . '">';
3490
	else
3491
		return '';
3492
}
3493
3494
?>