Completed
Pull Request — release-2.1 (#6024)
by John
15:33
created

list_getProfileEdits()   F

Complexity

Conditions 17
Paths 2050

Size

Total Lines 80
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 17
eloc 46
c 0
b 0
f 0
nop 4
dl 0
loc 80
rs 1.0499
nc 2050

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 RC2
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 bool|int[] $to_fetch Alerts to fetch: true/false for all/unread, or a list of one or more IDs.
222
 * @param int $limit Maximum number of alerts to fetch (0 for no limit).
223
 * @param int $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
		switch ($row['content_type'])
297
		{
298
			case 'msg':
299
				$alerts[$id_alert]['visible'] = false;
300
				$possible_msgs[$id_alert] = $row['content_id'];
301
				break;
302
			case 'topic':
303
			case 'board':
304
				$alerts[$id_alert]['visible'] = false;
305
				$possible_topics[$id_alert] = $row['content_id'];
306
				break;
307
			default:
308
				$alerts[$id_alert]['visible'] = true;
309
				break;
310
		}
311
312
		// Are we showing multiple links or one big main link ?
313
		$alerts[$id_alert]['show_links'] = $show_links || (isset($row['extra']['show_links']) && $row['extra']['show_links']);
314
315
		// Set an appropriate icon.
316
		$alerts[$id_alert]['icon'] = set_alert_icon($alerts[$id_alert]);
317
	}
318
	$smcFunc['db_free_result']($request);
319
320
	// Look up member info of anyone we need it for.
321
	if (!empty($profiles))
322
		loadMemberData($profiles, false, 'minimal');
323
324
	// Get the senders' avatars.
325
	if ($with_avatar)
326
	{
327
		foreach ($senders as $sender_id => $sender)
328
			$senders[$sender_id]['avatar'] = set_avatar_data($sender);
329
330
		$context['avatar_url'] = $modSettings['avatar_url'];
331
	}
332
333
	// Now go through and actually make with the text.
334
	loadLanguage('Alerts');
335
336
	// Some sprintf formats for generating links/strings.
337
	// 'required' is an array of keys in $alert['extra'] that should be used to generate the message, ordered to match the sprintf formats.
338
	// 'link' and 'text' are the sprintf formats that will be used when $alert['show_links'] is true or false, respectively.
339
	$formats = array(
340
		'msg_msg' => array(
341
			'required' => array('content_subject', 'topic', 'msg'),
342
			'link' => '<a href="{scripturl}?topic=%2$d.msg%3$d#msg%3$d">%1$s</a>',
343
			'text' => '<strong>%1$s</strong>',
344
		),
345
		'topic_msg' => array(
346
			'required' => array('content_subject', 'topic', 'topic_suffix'),
347
			'link' => '<a href="{scripturl}?topic=%2$d.%3$s">%1$s</a>',
348
			'text' => '<strong>%1$s</strong>',
349
		),
350
		'board_msg' => array(
351
			'required' => array('board_name', 'board'),
352
			'link' => '<a href="{scripturl}?board=%2$d.0">%1$s</a>',
353
			'text' => '<strong>%1$s</strong>',
354
		),
355
		'profile_msg' => array(
356
			'required' => array('user_name', 'user_id'),
357
			'link' => '<a href="{scripturl}?action=profile;u=%2$d">%1$s</a>',
358
			'text' => '<strong>%1$s</strong>',
359
		),
360
	);
361
362
	// Hooks might want to do something snazzy around their own content types - including enforcing permissions if appropriate.
363
	call_integration_hook('integrate_fetch_alerts', array(&$alerts, &$formats));
364
365
	// Substitute $scripturl into the link formats. (Done here to make life easier for hooked mods.)
366
	foreach ($formats as &$format_type)
367
		$format_type = str_replace('{scripturl}', $scripturl, $format_type);
368
369
	// If we need to check board access, use the correct board access filter for the member in question.
370
	$qb = array(
371
		'query_see_board' => '{query_see_board}',
372
	);
373
	if ((!isset($user_info['query_see_board']) || $user_info['id'] != $memID) && (!empty($possible_msgs) || !empty($possible_topics)))
374
		$qb = build_query_board($memID);
375
376
	// For anything that needs more info and/or wants us to check board or topic access, let's do that.
377
	if (!empty($possible_msgs))
378
	{
379
		$request = $smcFunc['db_query']('', '
380
			SELECT m.id_msg, m.id_topic, m.subject, b.id_board, b.name AS board_name
381
			FROM {db_prefix}messages AS m
382
				INNER JOIN {db_prefix}boards AS b ON (m.id_board = b.id_board)
383
			WHERE ' . $qb['query_see_board'] . '
384
				AND m.id_msg IN ({array_int:msgs})
385
			ORDER BY m.id_msg',
386
			array(
387
				'msgs' => $possible_msgs,
388
			)
389
		);
390
		while ($row = $smcFunc['db_fetch_assoc']($request))
391
		{
392
			foreach ($possible_msgs as $id_alert => $id_msg)
393
				if ($id_msg == $row['id_msg'])
394
				{
395
					$alerts[$id_alert]['content_data'] = $row;
396
					$alerts[$id_alert]['visible'] = true;
397
				}
398
		}
399
		$smcFunc['db_free_result']($request);
400
	}
401
	if (!empty($possible_topics))
402
	{
403
		$request = $smcFunc['db_query']('', '
404
			SELECT m.id_msg, t.id_topic, m.subject, b.id_board, b.name AS board_name
405
			FROM {db_prefix}topics AS t
406
				INNER JOIN {db_prefix}messages AS m ON (t.id_first_msg = m.id_msg)
407
				INNER JOIN {db_prefix}boards AS b ON (t.id_board = b.id_board)
408
			WHERE ' . $qb['query_see_board'] . '
409
				AND t.id_topic IN ({array_int:topics})',
410
			array(
411
				'topics' => $possible_topics,
412
			)
413
		);
414
		while ($row = $smcFunc['db_fetch_assoc']($request))
415
		{
416
			foreach ($possible_topics as $id_alert => $id_topic)
417
				if ($id_topic == $row['id_topic'])
418
				{
419
					$alerts[$id_alert]['content_data'] = $row;
420
					$alerts[$id_alert]['visible'] = true;
421
				}
422
		}
423
		$smcFunc['db_free_result']($request);
424
	}
425
426
	// 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)
427
	foreach ($alerts as $id_alert => $dummy)
428
	{
429
		// There's no point showing alerts for inaccessible content.
430
		if (!$alerts[$id_alert]['visible'])
431
		{
432
			unset($alerts[$id_alert]);
433
			continue;
434
		}
435
		else
436
			unset($alerts[$id_alert]['visible']);
437
438
		// Did a mod already take care of this one?
439
		if (!empty($alerts[$id_alert]['text']))
440
			continue;
441
442
		// For developer convenience.
443
		$alert = &$alerts[$id_alert];
444
445
		if (isset($alert['extra']['content_link']))
446
			$alert['extra']['content_link'] = strtr($alert['extra']['content_link'], array(
447
				'{SCRIPTURL}' => $scripturl,
448
				'{CONTENT_ID}' => $alert['extra']['content_id'],
449
			));
450
451
		// The info in extra might outdated if the topic was moved, the message's subject was changed, etc.
452
		if (!empty($alert['content_data']))
453
		{
454
			$data = $alert['content_data'];
455
456
			// Make sure msg, topic, and board info are correct.
457
			$patterns = array();
458
			$replacements = array();
459
			foreach (array('msg', 'topic', 'board') as $item)
460
			{
461
				if (isset($data['id_' . $item]))
462
				{
463
					$separator = $item == 'msg' ? '=?' : '=';
464
465
					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)
466
					{
467
						$patterns[] = '/\b' . $item . $separator . '\d+/';
468
						$replacements[] = $item . $separator . $data['id_' . $item];
469
					}
470
471
					$alert['extra'][$item] = $data['id_' . $item];
472
				}
473
			}
474
			if (!empty($patterns))
475
				$alert['extra']['content_link'] = preg_replace($patterns, $replacements, $alert['extra']['content_link']);
476
477
			// Make sure the subject is correct.
478
			if (isset($data['subject']))
479
				$alert['extra']['content_subject'] = $data['subject'];
480
481
			// Keep track of this so we can use it below.
482
			if (isset($data['board_name']))
483
				$alert['extra']['board_name'] = $data['board_name'];
484
485
			unset($alert['content_data']);
486
		}
487
488
		// Do we want to link to the topic in general or the new messages specifically?
489
		if (isset($possible_topics[$id_alert]) && in_array($alert['content_action'], array('reply', 'topic', 'unapproved_reply')))
490
			$alert['extra']['topic_suffix'] = 'new;topicseen#new';
491
		elseif (isset($alert['extra']['topic']))
492
			$alert['extra']['topic_suffix'] = '0';
493
494
		// Make sure profile alerts have what they need.
495
		if (in_array($id_alert, $profile_alerts))
496
		{
497
			if (empty($alert['extra']['user_id']))
498
				$alert['extra']['user_id'] = $alert['content_id'];
499
500
			if (isset($user_profile[$alert['extra']['user_id']]))
501
				$alert['extra']['user_name'] = $user_profile[$alert['extra']['user_id']]['real_name'];
502
		}
503
504
		// If we loaded the sender's profile, we may as well use it.
505
		$sender_id = !empty($alert['sender_id']) ? $alert['sender_id'] : 0;
506
		if (isset($user_profile[$sender_id]))
507
			$alert['sender_name'] = $user_profile[$sender_id]['real_name'];
508
509
		// If requested, include the sender's avatar data.
510
		if ($with_avatar && !empty($senders[$sender_id]))
511
			$alert['sender'] = $senders[$sender_id];
512
513
		// Next, build the message strings.
514
		foreach ($formats as $msg_type => $format_info)
515
		{
516
			// Get the values to use in the formatted string, in the right order.
517
			$msg_values = array_replace(
518
				array_fill_keys($format_info['required'], ''),
519
				array_intersect_key($alert['extra'], array_flip($format_info['required']))
520
			);
521
522
			// Assuming all required values are present, build the message.
523
			if (!in_array('', $msg_values))
524
				$alert['extra'][$msg_type] = vsprintf($formats[$msg_type][$alert['show_links'] ? 'link' : 'text'], $msg_values);
525
526
			elseif (in_array($msg_type, array('msg_msg', 'topic_msg', 'board_msg')))
527
				$alert['extra'][$msg_type] = $txt[$msg_type == 'board_msg' ? 'board_na' : 'topic_na'];
528
			else
529
				$alert['extra'][$msg_type] = '(' . $txt['not_applicable'] . ')';
530
		}
531
532
		// Show the formatted time in alerts about subscriptions.
533
		if ($alert['content_type'] == 'paidsubs' && isset($alert['extra']['end_time']))
534
		{
535
			// If the subscription already expired, say so.
536
			if ($alert['extra']['end_time'] < time())
537
				$alert['content_action'] = 'expired';
538
539
			// Present a nicely formatted date.
540
			$alert['extra']['end_time'] = timeformat($alert['extra']['end_time']);
541
		}
542
543
		// Now set the main URL that this alert should take the user to.
544
		$alert['target_href'] = '';
545
546
		// Priority goes to explicitly specified links.
547
		if (isset($alert['extra']['content_link']))
548
			$alert['target_href'] = $alert['extra']['content_link'];
549
550
		// Finally, set this alert's text string.
551
		$string = 'alert_' . $alert['content_type'] . '_' . $alert['content_action'];
552
553
		// This kludge exists because the alert content_types prior to 2.1 RC3 were a bit haphazard.
554
		// This can be removed once all the translated language files have been updated.
555
		if (!isset($txt[$string]))
556
		{
557
			if (strpos($alert['content_action'], 'unapproved_') === 0)
558
				$string = 'alert_topic_' . $alert['content_action'];
559
560
			if ($alert['content_type'] === 'member' && in_array($alert['content_action'], array('report', 'report_reply')))
561
				$string = 'alert_profile_' . $alert['content_action'];
562
563
			if ($alert['content_type'] === 'member' && $alert['content_action'] === 'buddy_request')
564
				$string = 'alert_buddy_' . $alert['content_action'];
565
		}
566
567
		if (isset($txt[$string]))
568
		{
569
			$substitutions = array(
570
				'{scripturl}' => $scripturl,
571
				'{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>',
572
			);
573
574
			if (is_array($alert['extra']))
575
			{
576
				foreach ($alert['extra'] as $k => $v)
577
					$substitutions['{' . $k . '}'] = $v;
578
			}
579
580
			$alert['text'] = strtr($txt[$string], $substitutions);
581
		}
582
583
		// Unset the reference variable to avoid any surprises in subsequent loops.
584
		unset($alert);
585
	}
586
587
	return $alerts;
588
}
589
590
/**
591
 * Shows all alerts for a member
592
 *
593
 * @param int $memID The ID of the member
594
 */
595
function showAlerts($memID)
596
{
597
	global $context, $smcFunc, $txt, $sourcedir, $scripturl, $options;
598
599
	require_once($sourcedir . '/Profile-Modify.php');
600
601
	// Are we opening a specific alert? (i.e.: ?action=profile;area=showalerts;alert=12345)
602
	if (!empty($_REQUEST['alert']))
603
	{
604
		$alert_id = (int) $_REQUEST['alert'];
605
		$alerts = fetch_alerts($memID, $alert_id);
0 ignored issues
show
Bug introduced by
$alert_id of type integer is incompatible with the type boolean|integer[] expected by parameter $to_fetch 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

605
		$alerts = fetch_alerts($memID, /** @scrutinizer ignore-type */ $alert_id);
Loading history...
606
		$alert = array_pop($alerts);
607
608
		/*
609
		 * MOD AUTHORS:
610
		 * To control this redirect, use the 'integrate_fetch_alerts' hook to
611
		 * set the value of $alert['extra']['content_link'], which will become
612
		 * the value for $alert['target_href'].
613
		 */
614
615
		// In case it failed to determine this alert's link
616
		if (empty($alert['target_href']))
617
			redirectexit('action=profile;area=showalerts');
618
619
		// Mark the alert as read while we're at it.
620
		alert_mark($memID, $alert_id, 1);
621
622
		// Take the user to the content
623
		redirectexit($alert['target_href']);
624
	}
625
626
	// Prepare the pagination vars.
627
	$maxIndex = 10;
628
	$context['start'] = (int) isset($_REQUEST['start']) ? $_REQUEST['start'] : 0;
629
	$count = alert_count($memID);
630
631
	// Fix invalid 'start' offsets.
632
	if ($context['start'] > $count)
633
		$context['start'] = $count - ($count % $maxIndex);
634
	else
635
		$context['start'] = $context['start'] - ($context['start'] % $maxIndex);
636
637
	// Get the alerts.
638
	$context['alerts'] = fetch_alerts($memID, true, $maxIndex, $context['start'], true, true);
639
	$toMark = false;
640
	$action = '';
641
642
	// Are we using checkboxes?
643
	$context['showCheckboxes'] = !empty($options['display_quick_mod']) && $options['display_quick_mod'] == 1;
644
645
	// Create the pagination.
646
	$context['pagination'] = constructPageIndex($scripturl . '?action=profile;area=showalerts;u=' . $memID, $context['start'], $count, $maxIndex, false);
647
648
	// Set some JavaScript for checking all alerts at once.
649
	if ($context['showCheckboxes'])
650
		addInlineJavaScript('
651
		$(function(){
652
			$(\'#select_all\').on(\'change\', function() {
653
				var checkboxes = $(\'ul.quickbuttons\').find(\':checkbox\');
654
				if($(this).prop(\'checked\')) {
655
					checkboxes.prop(\'checked\', true);
656
				}
657
				else {
658
					checkboxes.prop(\'checked\', false);
659
				}
660
			});
661
		});', true);
662
663
	// The quickbuttons
664
	foreach ($context['alerts'] as $id => $alert)
665
	{
666
		$context['alerts'][$id]['quickbuttons'] = array(
667
			'delete' => array(
668
				'label' => $txt['delete'],
669
				'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'] : ''),
670
				'javascript' => 'class="you_sure"',
671
				'icon' => 'remove_button'
672
			),
673
			'mark' => array(
674
				'label' => $alert['is_read'] != 0 ? $txt['mark_unread'] : $txt['mark_read_short'],
675
				'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'] : ''),
676
				'icon' => $alert['is_read'] != 0 ? 'unread_button' : 'read_button',
677
			),
678
			'view' => array(
679
				'label' => $txt['view'],
680
				'href' => $alert['target_href'],
681
				'icon' => 'move',
682
			),
683
			'quickmod' => array(
684
				'content' => '<input type="checkbox" name="mark[' . $id . ']" value="' . $id . '">',
685
				'show' => $context['showCheckboxes']
686
			)
687
		);
688
	}
689
690
	// The Delete all unread link.
691
	$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'] : '');
692
693
	// Set a nice message.
694
	if (!empty($_SESSION['update_message']))
695
	{
696
		$context['update_message'] = $txt['profile_updated_own'];
697
		unset($_SESSION['update_message']);
698
	}
699
700
	// Saving multiple changes?
701
	if (isset($_GET['save']) && !empty($_POST['mark']))
702
	{
703
		// Get the values.
704
		$toMark = array_map('intval', (array) $_POST['mark']);
705
706
		// Which action?
707
		$action = !empty($_POST['mark_as']) ? $smcFunc['htmlspecialchars']($smcFunc['htmltrim']($_POST['mark_as'])) : '';
708
	}
709
710
	// A single change.
711
	if (!empty($_GET['do']) && !empty($_GET['aid']))
712
	{
713
		$toMark = (int) $_GET['aid'];
714
		$action = $smcFunc['htmlspecialchars']($smcFunc['htmltrim']($_GET['do']));
715
	}
716
	// Delete all read alerts.
717
	elseif (!empty($_GET['do']) && $_GET['do'] === 'purge')
718
		$action = 'purge';
719
720
	// Save the changes.
721
	if (!empty($action) && (!empty($toMark) || $action === 'purge'))
722
	{
723
		checkSession('request');
724
725
		// Call it!
726
		if ($action == 'remove')
727
			alert_delete($toMark, $memID);
728
729
		elseif ($action == 'purge')
730
			alert_purge($memID);
731
732
		else
733
			alert_mark($memID, $toMark, $action == 'read' ? 1 : 0);
734
735
		// Set a nice update message.
736
		$_SESSION['update_message'] = true;
737
738
		// Redirect.
739
		redirectexit('action=profile;area=showalerts;u=' . $memID . (!empty($context['start']) ? ';start=' . $context['start'] : ''));
740
	}
741
}
742
743
/**
744
 * Show all posts by a member
745
 *
746
 * @todo This function needs to be split up properly.
747
 *
748
 * @param int $memID The ID of the member
749
 */
750
function showPosts($memID)
751
{
752
	global $txt, $user_info, $scripturl, $modSettings, $options;
753
	global $context, $user_profile, $sourcedir, $smcFunc, $board;
754
755
	// Some initial context.
756
	$context['start'] = (int) $_REQUEST['start'];
757
	$context['current_member'] = $memID;
758
759
	// Create the tabs for the template.
760
	$context[$context['profile_menu_name']]['tab_data'] = array(
761
		'title' => $txt['showPosts'],
762
		'description' => $txt['showPosts_help'],
763
		'icon' => 'profile_hd.png',
764
		'tabs' => array(
765
			'messages' => array(
766
			),
767
			'topics' => array(
768
			),
769
			'unwatchedtopics' => array(
770
			),
771
			'attach' => array(
772
			),
773
		),
774
	);
775
776
	// Shortcut used to determine which $txt['show*'] string to use for the title, based on the SA
777
	$title = array(
778
		'attach' => 'Attachments',
779
		'unwatchedtopics' => 'Unwatched',
780
		'topics' => 'Topics'
781
	);
782
783
	// Set the page title
784
	if (isset($_GET['sa']) && array_key_exists($_GET['sa'], $title))
785
		$context['page_title'] = $txt['show' . $title[$_GET['sa']]];
786
	else
787
		$context['page_title'] = $txt['showPosts'];
788
789
	$context['page_title'] .= ' - ' . $user_profile[$memID]['real_name'];
790
791
	// Is the load average too high to allow searching just now?
792
	if (!empty($context['load_average']) && !empty($modSettings['loadavg_show_posts']) && $context['load_average'] >= $modSettings['loadavg_show_posts'])
793
		fatal_lang_error('loadavg_show_posts_disabled', false);
794
795
	// If we're specifically dealing with attachments use that function!
796
	if (isset($_GET['sa']) && $_GET['sa'] == 'attach')
797
		return showAttachments($memID);
798
	// Instead, if we're dealing with unwatched topics (and the feature is enabled) use that other function.
799
	elseif (isset($_GET['sa']) && $_GET['sa'] == 'unwatchedtopics')
800
		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...
801
802
	// Are we just viewing topics?
803
	$context['is_topics'] = isset($_GET['sa']) && $_GET['sa'] == 'topics' ? true : false;
804
805
	// If just deleting a message, do it and then redirect back.
806
	if (isset($_GET['delete']) && !$context['is_topics'])
807
	{
808
		checkSession('get');
809
810
		// We need msg info for logging.
811
		$request = $smcFunc['db_query']('', '
812
			SELECT subject, id_member, id_topic, id_board
813
			FROM {db_prefix}messages
814
			WHERE id_msg = {int:id_msg}',
815
			array(
816
				'id_msg' => (int) $_GET['delete'],
817
			)
818
		);
819
		$info = $smcFunc['db_fetch_row']($request);
820
		$smcFunc['db_free_result']($request);
821
822
		// Trying to remove a message that doesn't exist.
823
		if (empty($info))
824
			redirectexit('action=profile;u=' . $memID . ';area=showposts;start=' . $_GET['start']);
825
826
		// We can be lazy, since removeMessage() will check the permissions for us.
827
		require_once($sourcedir . '/RemoveTopic.php');
828
		removeMessage((int) $_GET['delete']);
829
830
		// Add it to the mod log.
831
		if (allowedTo('delete_any') && (!allowedTo('delete_own') || $info[1] != $user_info['id']))
832
			logAction('delete', array('topic' => $info[2], 'subject' => $info[0], 'member' => $info[1], 'board' => $info[3]));
833
834
		// Back to... where we are now ;).
835
		redirectexit('action=profile;u=' . $memID . ';area=showposts;start=' . $_GET['start']);
836
	}
837
838
	// Default to 10.
839
	if (empty($_REQUEST['viewscount']) || !is_numeric($_REQUEST['viewscount']))
840
		$_REQUEST['viewscount'] = '10';
841
842
	if ($context['is_topics'])
843
		$request = $smcFunc['db_query']('', '
844
			SELECT COUNT(*)
845
			FROM {db_prefix}topics AS t' . '
846
			WHERE {query_see_topic_board}
847
				AND t.id_member_started = {int:current_member}' . (!empty($board) ? '
848
				AND t.id_board = {int:board}' : '') . (!$modSettings['postmod_active'] || $context['user']['is_owner'] ? '' : '
849
				AND t.approved = {int:is_approved}'),
850
			array(
851
				'current_member' => $memID,
852
				'is_approved' => 1,
853
				'board' => $board,
854
			)
855
		);
856
	else
857
		$request = $smcFunc['db_query']('', '
858
			SELECT COUNT(id_msg)
859
			FROM {db_prefix}messages AS m
860
			WHERE {query_see_message_board} AND m.id_member = {int:current_member}' . (!empty($board) ? '
861
				AND m.id_board = {int:board}' : '') . (!$modSettings['postmod_active'] || $context['user']['is_owner'] ? '' : '
862
				AND m.approved = {int:is_approved}'),
863
			array(
864
				'current_member' => $memID,
865
				'is_approved' => 1,
866
				'board' => $board,
867
			)
868
		);
869
	list ($msgCount) = $smcFunc['db_fetch_row']($request);
870
	$smcFunc['db_free_result']($request);
871
872
	$request = $smcFunc['db_query']('', '
873
		SELECT MIN(id_msg), MAX(id_msg)
874
		FROM {db_prefix}messages AS m
875
		WHERE m.id_member = {int:current_member}' . (!empty($board) ? '
876
			AND m.id_board = {int:board}' : '') . (!$modSettings['postmod_active'] || $context['user']['is_owner'] ? '' : '
877
			AND m.approved = {int:is_approved}'),
878
		array(
879
			'current_member' => $memID,
880
			'is_approved' => 1,
881
			'board' => $board,
882
		)
883
	);
884
	list ($min_msg_member, $max_msg_member) = $smcFunc['db_fetch_row']($request);
885
	$smcFunc['db_free_result']($request);
886
887
	$range_limit = '';
888
889
	if ($context['is_topics'])
890
		$maxPerPage = empty($modSettings['disableCustomPerPage']) && !empty($options['topics_per_page']) ? $options['topics_per_page'] : $modSettings['defaultMaxTopics'];
891
	else
892
		$maxPerPage = empty($modSettings['disableCustomPerPage']) && !empty($options['messages_per_page']) ? $options['messages_per_page'] : $modSettings['defaultMaxMessages'];
893
894
	$maxIndex = $maxPerPage;
895
896
	// Make sure the starting place makes sense and construct our friend the page index.
897
	$context['page_index'] = constructPageIndex($scripturl . '?action=profile;u=' . $memID . ';area=showposts' . ($context['is_topics'] ? ';sa=topics' : '') . (!empty($board) ? ';board=' . $board : ''), $context['start'], $msgCount, $maxIndex);
898
	$context['current_page'] = $context['start'] / $maxIndex;
899
900
	// Reverse the query if we're past 50% of the pages for better performance.
901
	$start = $context['start'];
902
	$reverse = $_REQUEST['start'] > $msgCount / 2;
903
	if ($reverse)
904
	{
905
		$maxIndex = $msgCount < $context['start'] + $maxPerPage + 1 && $msgCount > $context['start'] ? $msgCount - $context['start'] : $maxPerPage;
906
		$start = $msgCount < $context['start'] + $maxPerPage + 1 || $msgCount < $context['start'] + $maxPerPage ? 0 : $msgCount - $context['start'] - $maxPerPage;
907
	}
908
909
	// Guess the range of messages to be shown.
910
	if ($msgCount > 1000)
911
	{
912
		$margin = floor(($max_msg_member - $min_msg_member) * (($start + $maxPerPage) / $msgCount) + .1 * ($max_msg_member - $min_msg_member));
913
		// Make a bigger margin for topics only.
914
		if ($context['is_topics'])
915
		{
916
			$margin *= 5;
917
			$range_limit = $reverse ? 't.id_first_msg < ' . ($min_msg_member + $margin) : 't.id_first_msg > ' . ($max_msg_member - $margin);
918
		}
919
		else
920
			$range_limit = $reverse ? 'm.id_msg < ' . ($min_msg_member + $margin) : 'm.id_msg > ' . ($max_msg_member - $margin);
921
	}
922
923
	// Find this user's posts.  The left join on categories somehow makes this faster, weird as it looks.
924
	$looped = false;
925
	while (true)
926
	{
927
		if ($context['is_topics'])
928
		{
929
			$request = $smcFunc['db_query']('', '
930
				SELECT
931
					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,
932
					t.approved, m.body, m.smileys_enabled, m.subject, m.poster_time, m.id_topic, m.id_msg
933
				FROM {db_prefix}topics AS t
934
					INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
935
					LEFT JOIN {db_prefix}categories AS c ON (c.id_cat = b.id_cat)
936
					INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)
937
				WHERE t.id_member_started = {int:current_member}' . (!empty($board) ? '
938
					AND t.id_board = {int:board}' : '') . (empty($range_limit) ? '' : '
939
					AND ' . $range_limit) . '
940
					AND {query_see_board}' . (!$modSettings['postmod_active'] || $context['user']['is_owner'] ? '' : '
941
					AND t.approved = {int:is_approved} AND m.approved = {int:is_approved}') . '
942
				ORDER BY t.id_first_msg ' . ($reverse ? 'ASC' : 'DESC') . '
943
				LIMIT {int:start}, {int:max}',
944
				array(
945
					'current_member' => $memID,
946
					'is_approved' => 1,
947
					'board' => $board,
948
					'start' => $start,
949
					'max' => $maxIndex,
950
				)
951
			);
952
		}
953
		else
954
		{
955
			$request = $smcFunc['db_query']('', '
956
				SELECT
957
					b.id_board, b.name AS bname, c.id_cat, c.name AS cname, m.id_topic, m.id_msg,
958
					t.id_member_started, t.id_first_msg, t.id_last_msg, m.body, m.smileys_enabled,
959
					m.subject, m.poster_time, m.approved
960
				FROM {db_prefix}messages AS m
961
					INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic)
962
					INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
963
					LEFT JOIN {db_prefix}categories AS c ON (c.id_cat = b.id_cat)
964
				WHERE m.id_member = {int:current_member}' . (!empty($board) ? '
965
					AND b.id_board = {int:board}' : '') . (empty($range_limit) ? '' : '
966
					AND ' . $range_limit) . '
967
					AND {query_see_board}' . (!$modSettings['postmod_active'] || $context['user']['is_owner'] ? '' : '
968
					AND t.approved = {int:is_approved} AND m.approved = {int:is_approved}') . '
969
				ORDER BY m.id_msg ' . ($reverse ? 'ASC' : 'DESC') . '
970
				LIMIT {int:start}, {int:max}',
971
				array(
972
					'current_member' => $memID,
973
					'is_approved' => 1,
974
					'board' => $board,
975
					'start' => $start,
976
					'max' => $maxIndex,
977
				)
978
			);
979
		}
980
981
		// Make sure we quit this loop.
982
		if ($smcFunc['db_num_rows']($request) === $maxIndex || $looped || $range_limit == '')
983
			break;
984
		$looped = true;
985
		$range_limit = '';
986
	}
987
988
	// Start counting at the number of the first message displayed.
989
	$counter = $reverse ? $context['start'] + $maxIndex + 1 : $context['start'];
990
	$context['posts'] = array();
991
	$board_ids = array('own' => array(), 'any' => array());
992
	while ($row = $smcFunc['db_fetch_assoc']($request))
993
	{
994
		// Censor....
995
		censorText($row['body']);
996
		censorText($row['subject']);
997
998
		// Do the code.
999
		$row['body'] = parse_bbc($row['body'], $row['smileys_enabled'], $row['id_msg']);
1000
1001
		// And the array...
1002
		$context['posts'][$counter += $reverse ? -1 : 1] = array(
1003
			'body' => $row['body'],
1004
			'counter' => $counter,
1005
			'category' => array(
1006
				'name' => $row['cname'],
1007
				'id' => $row['id_cat']
1008
			),
1009
			'board' => array(
1010
				'name' => $row['bname'],
1011
				'id' => $row['id_board']
1012
			),
1013
			'topic' => $row['id_topic'],
1014
			'subject' => $row['subject'],
1015
			'start' => 'msg' . $row['id_msg'],
1016
			'time' => timeformat($row['poster_time']),
1017
			'timestamp' => forum_time(true, $row['poster_time']),
1018
			'id' => $row['id_msg'],
1019
			'can_reply' => false,
1020
			'can_mark_notify' => !$context['user']['is_guest'],
1021
			'can_delete' => false,
1022
			'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()),
1023
			'approved' => $row['approved'],
1024
			'css_class' => $row['approved'] ? 'windowbg' : 'approvebg',
1025
		);
1026
1027
		if ($user_info['id'] == $row['id_member_started'])
1028
			$board_ids['own'][$row['id_board']][] = $counter;
1029
		$board_ids['any'][$row['id_board']][] = $counter;
1030
	}
1031
	$smcFunc['db_free_result']($request);
1032
1033
	// All posts were retrieved in reverse order, get them right again.
1034
	if ($reverse)
1035
		$context['posts'] = array_reverse($context['posts'], true);
1036
1037
	// These are all the permissions that are different from board to board..
1038
	if ($context['is_topics'])
1039
		$permissions = array(
1040
			'own' => array(
1041
				'post_reply_own' => 'can_reply',
1042
			),
1043
			'any' => array(
1044
				'post_reply_any' => 'can_reply',
1045
			)
1046
		);
1047
	else
1048
		$permissions = array(
1049
			'own' => array(
1050
				'post_reply_own' => 'can_reply',
1051
				'delete_own' => 'can_delete',
1052
			),
1053
			'any' => array(
1054
				'post_reply_any' => 'can_reply',
1055
				'delete_any' => 'can_delete',
1056
			)
1057
		);
1058
1059
	// Create an array for the permissions.
1060
	$boards_can = boardsAllowedTo(array_keys(iterator_to_array(
1061
		new RecursiveIteratorIterator(new RecursiveArrayIterator($permissions)))
1062
	), true, false);
1063
1064
	// For every permission in the own/any lists...
1065
	foreach ($permissions as $type => $list)
1066
	{
1067
		foreach ($list as $permission => $allowed)
1068
		{
1069
			// Get the boards they can do this on...
1070
			$boards = $boards_can[$permission];
1071
1072
			// Hmm, they can do it on all boards, can they?
1073
			if (!empty($boards) && $boards[0] == 0)
1074
				$boards = array_keys($board_ids[$type]);
1075
1076
			// Now go through each board they can do the permission on.
1077
			foreach ($boards as $board_id)
1078
			{
1079
				// There aren't any posts displayed from this board.
1080
				if (!isset($board_ids[$type][$board_id]))
1081
					continue;
1082
1083
				// Set the permission to true ;).
1084
				foreach ($board_ids[$type][$board_id] as $counter)
1085
					$context['posts'][$counter][$allowed] = true;
1086
			}
1087
		}
1088
	}
1089
1090
	// Clean up after posts that cannot be deleted and quoted.
1091
	$quote_enabled = empty($modSettings['disabledBBC']) || !in_array('quote', explode(',', $modSettings['disabledBBC']));
1092
	foreach ($context['posts'] as $counter => $dummy)
1093
	{
1094
		$context['posts'][$counter]['can_delete'] &= $context['posts'][$counter]['delete_possible'];
1095
		$context['posts'][$counter]['can_quote'] = $context['posts'][$counter]['can_reply'] && $quote_enabled;
1096
	}
1097
1098
	// Allow last minute changes.
1099
	call_integration_hook('integrate_profile_showPosts');
1100
1101
	foreach ($context['posts'] as $key => $post)
1102
	{
1103
		$context['posts'][$key]['quickbuttons'] = array(
1104
			'reply' => array(
1105
				'label' => $txt['reply'],
1106
				'href' => $scripturl.'?action=post;topic='.$post['topic'].'.'.$post['start'],
1107
				'icon' => 'reply_button',
1108
				'show' => $post['can_reply']
1109
			),
1110
			'quote' => array(
1111
				'label' => $txt['quote_action'],
1112
				'href' => $scripturl.'?action=post;topic='.$post['topic'].'.'.$post['start'].';quote='.$post['id'],
1113
				'icon' => 'quote',
1114
				'show' => $post['can_quote']
1115
			),
1116
			'remove' => array(
1117
				'label' => $txt['remove'],
1118
				'href' => $scripturl.'?action=deletemsg;msg='.$post['id'].';topic='.$post['topic'].';profile;u='.$context['member']['id'].';start='.$context['start'].';'.$context['session_var'].'='.$context['session_id'],
1119
				'javascript' => 'data-confirm="'.$txt['remove_message'].'" class="you_sure"',
1120
				'icon' => 'remove_button',
1121
				'show' => $post['can_delete']
1122
			)
1123
		);
1124
	}
1125
}
1126
1127
/**
1128
 * Show all the attachments belonging to a member.
1129
 *
1130
 * @param int $memID The ID of the member
1131
 */
1132
function showAttachments($memID)
1133
{
1134
	global $txt, $scripturl, $modSettings;
1135
	global $sourcedir;
1136
1137
	// OBEY permissions!
1138
	$boardsAllowed = boardsAllowedTo('view_attachments');
1139
1140
	// Make sure we can't actually see anything...
1141
	if (empty($boardsAllowed))
1142
		$boardsAllowed = array(-1);
1143
1144
	require_once($sourcedir . '/Subs-List.php');
1145
1146
	// This is all the information required to list attachments.
1147
	$listOptions = array(
1148
		'id' => 'attachments',
1149
		'width' => '100%',
1150
		'items_per_page' => $modSettings['defaultMaxListItems'],
1151
		'no_items_label' => $txt['show_attachments_none'],
1152
		'base_href' => $scripturl . '?action=profile;area=showposts;sa=attach;u=' . $memID,
1153
		'default_sort_col' => 'filename',
1154
		'get_items' => array(
1155
			'function' => 'list_getAttachments',
1156
			'params' => array(
1157
				$boardsAllowed,
1158
				$memID,
1159
			),
1160
		),
1161
		'get_count' => array(
1162
			'function' => 'list_getNumAttachments',
1163
			'params' => array(
1164
				$boardsAllowed,
1165
				$memID,
1166
			),
1167
		),
1168
		'data_check' => array(
1169
			'class' => function($data)
1170
			{
1171
				return $data['approved'] ? '' : 'approvebg';
1172
			}
1173
		),
1174
		'columns' => array(
1175
			'filename' => array(
1176
				'header' => array(
1177
					'value' => $txt['show_attach_filename'],
1178
					'class' => 'lefttext',
1179
					'style' => 'width: 25%;',
1180
				),
1181
				'data' => array(
1182
					'sprintf' => array(
1183
						'format' => '<a href="' . $scripturl . '?action=dlattach;topic=%1$d.0;attach=%2$d">%3$s</a>',
1184
						'params' => array(
1185
							'topic' => true,
1186
							'id' => true,
1187
							'filename' => false,
1188
						),
1189
					),
1190
				),
1191
				'sort' => array(
1192
					'default' => 'a.filename',
1193
					'reverse' => 'a.filename DESC',
1194
				),
1195
			),
1196
			'downloads' => array(
1197
				'header' => array(
1198
					'value' => $txt['show_attach_downloads'],
1199
					'style' => 'width: 12%;',
1200
				),
1201
				'data' => array(
1202
					'db' => 'downloads',
1203
					'comma_format' => true,
1204
				),
1205
				'sort' => array(
1206
					'default' => 'a.downloads',
1207
					'reverse' => 'a.downloads DESC',
1208
				),
1209
			),
1210
			'subject' => array(
1211
				'header' => array(
1212
					'value' => $txt['message'],
1213
					'class' => 'lefttext',
1214
					'style' => 'width: 30%;',
1215
				),
1216
				'data' => array(
1217
					'sprintf' => array(
1218
						'format' => '<a href="' . $scripturl . '?msg=%1$d">%2$s</a>',
1219
						'params' => array(
1220
							'msg' => true,
1221
							'subject' => false,
1222
						),
1223
					),
1224
				),
1225
				'sort' => array(
1226
					'default' => 'm.subject',
1227
					'reverse' => 'm.subject DESC',
1228
				),
1229
			),
1230
			'posted' => array(
1231
				'header' => array(
1232
					'value' => $txt['show_attach_posted'],
1233
					'class' => 'lefttext',
1234
				),
1235
				'data' => array(
1236
					'db' => 'posted',
1237
					'timeformat' => true,
1238
				),
1239
				'sort' => array(
1240
					'default' => 'm.poster_time',
1241
					'reverse' => 'm.poster_time DESC',
1242
				),
1243
			),
1244
		),
1245
	);
1246
1247
	// Create the request list.
1248
	createList($listOptions);
1249
}
1250
1251
/**
1252
 * Get a list of attachments for a member. Callback for the list in showAttachments()
1253
 *
1254
 * @param int $start Which item to start with (for pagination purposes)
1255
 * @param int $items_per_page How many items to show on each page
1256
 * @param string $sort A string indicating how to sort the results
1257
 * @param array $boardsAllowed An array containing the IDs of the boards they can see
1258
 * @param int $memID The ID of the member
1259
 * @return array An array of information about the attachments
1260
 */
1261
function list_getAttachments($start, $items_per_page, $sort, $boardsAllowed, $memID)
1262
{
1263
	global $smcFunc, $board, $modSettings, $context;
1264
1265
	// Retrieve some attachments.
1266
	$request = $smcFunc['db_query']('', '
1267
		SELECT a.id_attach, a.id_msg, a.filename, a.downloads, a.approved, m.id_msg, m.id_topic,
1268
			m.id_board, m.poster_time, m.subject, b.name
1269
		FROM {db_prefix}attachments AS a
1270
			INNER JOIN {db_prefix}messages AS m ON (m.id_msg = a.id_msg)
1271
			INNER JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board AND {query_see_board})
1272
		WHERE a.attachment_type = {int:attachment_type}
1273
			AND a.id_msg != {int:no_message}
1274
			AND m.id_member = {int:current_member}' . (!empty($board) ? '
1275
			AND b.id_board = {int:board}' : '') . (!in_array(0, $boardsAllowed) ? '
1276
			AND b.id_board IN ({array_int:boards_list})' : '') . (!$modSettings['postmod_active'] || $context['user']['is_owner'] ? '' : '
1277
			AND m.approved = {int:is_approved}') . '
1278
		ORDER BY {raw:sort}
1279
		LIMIT {int:offset}, {int:limit}',
1280
		array(
1281
			'boards_list' => $boardsAllowed,
1282
			'attachment_type' => 0,
1283
			'no_message' => 0,
1284
			'current_member' => $memID,
1285
			'is_approved' => 1,
1286
			'board' => $board,
1287
			'sort' => $sort,
1288
			'offset' => $start,
1289
			'limit' => $items_per_page,
1290
		)
1291
	);
1292
	$attachments = array();
1293
	while ($row = $smcFunc['db_fetch_assoc']($request))
1294
		$attachments[] = array(
1295
			'id' => $row['id_attach'],
1296
			'filename' => $row['filename'],
1297
			'downloads' => $row['downloads'],
1298
			'subject' => censorText($row['subject']),
1299
			'posted' => $row['poster_time'],
1300
			'msg' => $row['id_msg'],
1301
			'topic' => $row['id_topic'],
1302
			'board' => $row['id_board'],
1303
			'board_name' => $row['name'],
1304
			'approved' => $row['approved'],
1305
		);
1306
1307
	$smcFunc['db_free_result']($request);
1308
1309
	return $attachments;
1310
}
1311
1312
/**
1313
 * Gets the total number of attachments for a member
1314
 *
1315
 * @param array $boardsAllowed An array of the IDs of the boards they can see
1316
 * @param int $memID The ID of the member
1317
 * @return int The number of attachments
1318
 */
1319
function list_getNumAttachments($boardsAllowed, $memID)
1320
{
1321
	global $board, $smcFunc, $modSettings, $context;
1322
1323
	// Get the total number of attachments they have posted.
1324
	$request = $smcFunc['db_query']('', '
1325
		SELECT COUNT(*)
1326
		FROM {db_prefix}attachments AS a
1327
			INNER JOIN {db_prefix}messages AS m ON (m.id_msg = a.id_msg)
1328
			INNER JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board AND {query_see_board})
1329
		WHERE a.attachment_type = {int:attachment_type}
1330
			AND a.id_msg != {int:no_message}
1331
			AND m.id_member = {int:current_member}' . (!empty($board) ? '
1332
			AND b.id_board = {int:board}' : '') . (!in_array(0, $boardsAllowed) ? '
1333
			AND b.id_board IN ({array_int:boards_list})' : '') . (!$modSettings['postmod_active'] || $context['user']['is_owner'] ? '' : '
1334
			AND m.approved = {int:is_approved}'),
1335
		array(
1336
			'boards_list' => $boardsAllowed,
1337
			'attachment_type' => 0,
1338
			'no_message' => 0,
1339
			'current_member' => $memID,
1340
			'is_approved' => 1,
1341
			'board' => $board,
1342
		)
1343
	);
1344
	list ($attachCount) = $smcFunc['db_fetch_row']($request);
1345
	$smcFunc['db_free_result']($request);
1346
1347
	return $attachCount;
1348
}
1349
1350
/**
1351
 * Show all the unwatched topics.
1352
 *
1353
 * @param int $memID The ID of the member
1354
 */
1355
function showUnwatched($memID)
1356
{
1357
	global $txt, $user_info, $scripturl, $modSettings, $context, $options, $sourcedir;
1358
1359
	// Only the owner can see the list (if the function is enabled of course)
1360
	if ($user_info['id'] != $memID)
1361
		return;
1362
1363
	require_once($sourcedir . '/Subs-List.php');
1364
1365
	// And here they are: the topics you don't like
1366
	$listOptions = array(
1367
		'id' => 'unwatched_topics',
1368
		'width' => '100%',
1369
		'items_per_page' => (empty($modSettings['disableCustomPerPage']) && !empty($options['topics_per_page'])) ? $options['topics_per_page'] : $modSettings['defaultMaxTopics'],
1370
		'no_items_label' => $txt['unwatched_topics_none'],
1371
		'base_href' => $scripturl . '?action=profile;area=showposts;sa=unwatchedtopics;u=' . $memID,
1372
		'default_sort_col' => 'started_on',
1373
		'get_items' => array(
1374
			'function' => 'list_getUnwatched',
1375
			'params' => array(
1376
				$memID,
1377
			),
1378
		),
1379
		'get_count' => array(
1380
			'function' => 'list_getNumUnwatched',
1381
			'params' => array(
1382
				$memID,
1383
			),
1384
		),
1385
		'columns' => array(
1386
			'subject' => array(
1387
				'header' => array(
1388
					'value' => $txt['subject'],
1389
					'class' => 'lefttext',
1390
					'style' => 'width: 30%;',
1391
				),
1392
				'data' => array(
1393
					'sprintf' => array(
1394
						'format' => '<a href="' . $scripturl . '?topic=%1$d.0">%2$s</a>',
1395
						'params' => array(
1396
							'id_topic' => false,
1397
							'subject' => false,
1398
						),
1399
					),
1400
				),
1401
				'sort' => array(
1402
					'default' => 'm.subject',
1403
					'reverse' => 'm.subject DESC',
1404
				),
1405
			),
1406
			'started_by' => array(
1407
				'header' => array(
1408
					'value' => $txt['started_by'],
1409
					'style' => 'width: 15%;',
1410
				),
1411
				'data' => array(
1412
					'db' => 'started_by',
1413
				),
1414
				'sort' => array(
1415
					'default' => 'mem.real_name',
1416
					'reverse' => 'mem.real_name DESC',
1417
				),
1418
			),
1419
			'started_on' => array(
1420
				'header' => array(
1421
					'value' => $txt['on'],
1422
					'class' => 'lefttext',
1423
					'style' => 'width: 20%;',
1424
				),
1425
				'data' => array(
1426
					'db' => 'started_on',
1427
					'timeformat' => true,
1428
				),
1429
				'sort' => array(
1430
					'default' => 'm.poster_time',
1431
					'reverse' => 'm.poster_time DESC',
1432
				),
1433
			),
1434
			'last_post_by' => array(
1435
				'header' => array(
1436
					'value' => $txt['last_post'],
1437
					'style' => 'width: 15%;',
1438
				),
1439
				'data' => array(
1440
					'db' => 'last_post_by',
1441
				),
1442
				'sort' => array(
1443
					'default' => 'mem.real_name',
1444
					'reverse' => 'mem.real_name DESC',
1445
				),
1446
			),
1447
			'last_post_on' => array(
1448
				'header' => array(
1449
					'value' => $txt['on'],
1450
					'class' => 'lefttext',
1451
					'style' => 'width: 20%;',
1452
				),
1453
				'data' => array(
1454
					'db' => 'last_post_on',
1455
					'timeformat' => true,
1456
				),
1457
				'sort' => array(
1458
					'default' => 'm.poster_time',
1459
					'reverse' => 'm.poster_time DESC',
1460
				),
1461
			),
1462
		),
1463
	);
1464
1465
	// Create the request list.
1466
	createList($listOptions);
1467
1468
	$context['sub_template'] = 'show_list';
1469
	$context['default_list'] = 'unwatched_topics';
1470
}
1471
1472
/**
1473
 * Gets information about unwatched (disregarded) topics. Callback for the list in show_unwatched
1474
 *
1475
 * @param int $start The item to start with (for pagination purposes)
1476
 * @param int $items_per_page How many items to show on each page
1477
 * @param string $sort A string indicating how to sort the results
1478
 * @param int $memID The ID of the member
1479
 * @return array An array of information about the unwatched topics
1480
 */
1481
function list_getUnwatched($start, $items_per_page, $sort, $memID)
1482
{
1483
	global $smcFunc;
1484
1485
	// Get the list of topics we can see
1486
	$request = $smcFunc['db_query']('', '
1487
		SELECT lt.id_topic
1488
		FROM {db_prefix}log_topics as lt
1489
			LEFT JOIN {db_prefix}topics as t ON (lt.id_topic = t.id_topic)
1490
			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')) ? '
1491
			LEFT JOIN {db_prefix}members as mem ON (m.id_member = mem.id_member)' : '') . '
1492
		WHERE lt.id_member = {int:current_member}
1493
			AND unwatched = 1
1494
			AND {query_see_message_board}
1495
		ORDER BY {raw:sort}
1496
		LIMIT {int:offset}, {int:limit}',
1497
		array(
1498
			'current_member' => $memID,
1499
			'sort' => $sort,
1500
			'offset' => $start,
1501
			'limit' => $items_per_page,
1502
		)
1503
	);
1504
1505
	$topics = array();
1506
	while ($row = $smcFunc['db_fetch_assoc']($request))
1507
		$topics[] = $row['id_topic'];
1508
1509
	$smcFunc['db_free_result']($request);
1510
1511
	// Any topics found?
1512
	$topicsInfo = array();
1513
	if (!empty($topics))
1514
	{
1515
		$request = $smcFunc['db_query']('', '
1516
			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
1517
			FROM {db_prefix}topics AS t
1518
				INNER JOIN {db_prefix}messages AS ml ON (ml.id_msg = t.id_last_msg)
1519
				INNER JOIN {db_prefix}messages AS mf ON (mf.id_msg = t.id_first_msg)
1520
				LEFT JOIN {db_prefix}members AS meml ON (meml.id_member = ml.id_member)
1521
				LEFT JOIN {db_prefix}members AS memf ON (memf.id_member = mf.id_member)
1522
			WHERE t.id_topic IN ({array_int:topics})',
1523
			array(
1524
				'topics' => $topics,
1525
			)
1526
		);
1527
		while ($row = $smcFunc['db_fetch_assoc']($request))
1528
			$topicsInfo[] = $row;
1529
		$smcFunc['db_free_result']($request);
1530
	}
1531
1532
	return $topicsInfo;
1533
}
1534
1535
/**
1536
 * Count the number of topics in the unwatched list
1537
 *
1538
 * @param int $memID The ID of the member
1539
 * @return int The number of unwatched topics
1540
 */
1541
function list_getNumUnwatched($memID)
1542
{
1543
	global $smcFunc;
1544
1545
	// Get the total number of attachments they have posted.
1546
	$request = $smcFunc['db_query']('', '
1547
		SELECT COUNT(*)
1548
		FROM {db_prefix}log_topics as lt
1549
		LEFT JOIN {db_prefix}topics as t ON (lt.id_topic = t.id_topic)
1550
		WHERE lt.id_member = {int:current_member}
1551
			AND lt.unwatched = 1
1552
			AND {query_see_topic_board}',
1553
		array(
1554
			'current_member' => $memID,
1555
		)
1556
	);
1557
	list ($unwatchedCount) = $smcFunc['db_fetch_row']($request);
1558
	$smcFunc['db_free_result']($request);
1559
1560
	return $unwatchedCount;
1561
}
1562
1563
/**
1564
 * Gets the user stats for display
1565
 *
1566
 * @param int $memID The ID of the member
1567
 */
1568
function statPanel($memID)
1569
{
1570
	global $txt, $scripturl, $context, $user_profile, $user_info, $modSettings, $smcFunc;
1571
1572
	$context['page_title'] = $txt['statPanel_showStats'] . ' ' . $user_profile[$memID]['real_name'];
1573
1574
	// Is the load average too high to allow searching just now?
1575
	if (!empty($context['load_average']) && !empty($modSettings['loadavg_userstats']) && $context['load_average'] >= $modSettings['loadavg_userstats'])
1576
		fatal_lang_error('loadavg_userstats_disabled', false);
1577
1578
	// General user statistics.
1579
	$timeDays = floor($user_profile[$memID]['total_time_logged_in'] / 86400);
1580
	$timeHours = floor(($user_profile[$memID]['total_time_logged_in'] % 86400) / 3600);
1581
	$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'];
1582
	$context['num_posts'] = comma_format($user_profile[$memID]['posts']);
1583
	// Menu tab
1584
	$context[$context['profile_menu_name']]['tab_data'] = array(
1585
		'title' => $txt['statPanel_generalStats'] . ' - ' . $context['member']['name'],
1586
		'icon' => 'stats_info.png'
1587
	);
1588
1589
	// Number of topics started and Number polls started
1590
	$result = $smcFunc['db_query']('', '
1591
		SELECT COUNT(*), COUNT( CASE WHEN id_poll != {int:no_poll} THEN 1 ELSE NULL END )
1592
		FROM {db_prefix}topics
1593
		WHERE id_member_started = {int:current_member}' . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? '
1594
			AND id_board != {int:recycle_board}' : ''),
1595
		array(
1596
			'current_member' => $memID,
1597
			'recycle_board' => $modSettings['recycle_board'],
1598
			'no_poll' => 0,
1599
		)
1600
	);
1601
	list ($context['num_topics'], $context['num_polls']) = $smcFunc['db_fetch_row']($result);
1602
	$smcFunc['db_free_result']($result);
1603
1604
	// Number polls voted in.
1605
	$result = $smcFunc['db_query']('distinct_poll_votes', '
1606
		SELECT COUNT(DISTINCT id_poll)
1607
		FROM {db_prefix}log_polls
1608
		WHERE id_member = {int:current_member}',
1609
		array(
1610
			'current_member' => $memID,
1611
		)
1612
	);
1613
	list ($context['num_votes']) = $smcFunc['db_fetch_row']($result);
1614
	$smcFunc['db_free_result']($result);
1615
1616
	// Format the numbers...
1617
	$context['num_topics'] = comma_format($context['num_topics']);
1618
	$context['num_polls'] = comma_format($context['num_polls']);
1619
	$context['num_votes'] = comma_format($context['num_votes']);
1620
1621
	// Grab the board this member posted in most often.
1622
	$result = $smcFunc['db_query']('', '
1623
		SELECT
1624
			b.id_board, MAX(b.name) AS name, MAX(b.num_posts) AS num_posts, COUNT(*) AS message_count
1625
		FROM {db_prefix}messages AS m
1626
			INNER JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board)
1627
		WHERE m.id_member = {int:current_member}
1628
			AND b.count_posts = {int:count_enabled}
1629
			AND {query_see_board}
1630
		GROUP BY b.id_board
1631
		ORDER BY message_count DESC
1632
		LIMIT 10',
1633
		array(
1634
			'current_member' => $memID,
1635
			'count_enabled' => 0,
1636
		)
1637
	);
1638
	$context['popular_boards'] = array();
1639
	while ($row = $smcFunc['db_fetch_assoc']($result))
1640
	{
1641
		$context['popular_boards'][$row['id_board']] = array(
1642
			'id' => $row['id_board'],
1643
			'posts' => $row['message_count'],
1644
			'href' => $scripturl . '?board=' . $row['id_board'] . '.0',
1645
			'link' => '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['name'] . '</a>',
1646
			'posts_percent' => $user_profile[$memID]['posts'] == 0 ? 0 : ($row['message_count'] * 100) / $user_profile[$memID]['posts'],
1647
			'total_posts' => $row['num_posts'],
1648
			'total_posts_member' => $user_profile[$memID]['posts'],
1649
		);
1650
	}
1651
	$smcFunc['db_free_result']($result);
1652
1653
	// Now get the 10 boards this user has most often participated in.
1654
	$result = $smcFunc['db_query']('profile_board_stats', '
1655
		SELECT
1656
			b.id_board, MAX(b.name) AS name, b.num_posts, COUNT(*) AS message_count,
1657
			CASE WHEN COUNT(*) > MAX(b.num_posts) THEN 1 ELSE COUNT(*) / MAX(b.num_posts) END * 100 AS percentage
1658
		FROM {db_prefix}messages AS m
1659
			INNER JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board)
1660
		WHERE m.id_member = {int:current_member}
1661
			AND {query_see_board}
1662
		GROUP BY b.id_board, b.num_posts
1663
		ORDER BY percentage DESC
1664
		LIMIT 10',
1665
		array(
1666
			'current_member' => $memID,
1667
		)
1668
	);
1669
	$context['board_activity'] = array();
1670
	while ($row = $smcFunc['db_fetch_assoc']($result))
1671
	{
1672
		$context['board_activity'][$row['id_board']] = array(
1673
			'id' => $row['id_board'],
1674
			'posts' => $row['message_count'],
1675
			'href' => $scripturl . '?board=' . $row['id_board'] . '.0',
1676
			'link' => '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['name'] . '</a>',
1677
			'percent' => comma_format((float) $row['percentage'], 2),
1678
			'posts_percent' => (float) $row['percentage'],
1679
			'total_posts' => $row['num_posts'],
1680
		);
1681
	}
1682
	$smcFunc['db_free_result']($result);
1683
1684
	// Posting activity by time.
1685
	$result = $smcFunc['db_query']('user_activity_by_time', '
1686
		SELECT
1687
			HOUR(FROM_UNIXTIME(poster_time + {int:time_offset})) AS hour,
1688
			COUNT(*) AS post_count
1689
		FROM {db_prefix}messages
1690
		WHERE id_member = {int:current_member}' . ($modSettings['totalMessages'] > 100000 ? '
1691
			AND id_topic > {int:top_ten_thousand_topics}' : '') . '
1692
		GROUP BY hour',
1693
		array(
1694
			'current_member' => $memID,
1695
			'top_ten_thousand_topics' => $modSettings['totalTopics'] - 10000,
1696
			'time_offset' => (($user_info['time_offset'] + $modSettings['time_offset']) * 3600),
1697
		)
1698
	);
1699
	$maxPosts = $realPosts = 0;
1700
	$context['posts_by_time'] = array();
1701
	while ($row = $smcFunc['db_fetch_assoc']($result))
1702
	{
1703
		// Cast as an integer to remove the leading 0.
1704
		$row['hour'] = (int) $row['hour'];
1705
1706
		$maxPosts = max($row['post_count'], $maxPosts);
1707
		$realPosts += $row['post_count'];
1708
1709
		$context['posts_by_time'][$row['hour']] = array(
1710
			'hour' => $row['hour'],
1711
			'hour_format' => stripos($user_info['time_format'], '%p') === false ? $row['hour'] : date('g a', mktime($row['hour'])),
1712
			'posts' => $row['post_count'],
1713
			'posts_percent' => 0,
1714
			'is_last' => $row['hour'] == 23,
1715
		);
1716
	}
1717
	$smcFunc['db_free_result']($result);
1718
1719
	if ($maxPosts > 0)
1720
		for ($hour = 0; $hour < 24; $hour++)
1721
		{
1722
			if (!isset($context['posts_by_time'][$hour]))
1723
				$context['posts_by_time'][$hour] = array(
1724
					'hour' => $hour,
1725
					'hour_format' => stripos($user_info['time_format'], '%p') === false ? $hour : date('g a', mktime($hour)),
1726
					'posts' => 0,
1727
					'posts_percent' => 0,
1728
					'relative_percent' => 0,
1729
					'is_last' => $hour == 23,
1730
				);
1731
			else
1732
			{
1733
				$context['posts_by_time'][$hour]['posts_percent'] = round(($context['posts_by_time'][$hour]['posts'] * 100) / $realPosts);
1734
				$context['posts_by_time'][$hour]['relative_percent'] = round(($context['posts_by_time'][$hour]['posts'] * 100) / $maxPosts);
1735
			}
1736
		}
1737
1738
	// Put it in the right order.
1739
	ksort($context['posts_by_time']);
1740
1741
	/**
1742
	 * Adding new entries:
1743
	 * 'key' => array(
1744
	 * 		'text' => string, // The text that will be shown next to the entry.
1745
	 * 		'url' => string, // OPTIONAL: The entry will be a url
1746
	 * ),
1747
	 *
1748
	 * 'key' will be used to look up the language string as $txt['statPanel_' . $key].
1749
	 * Make sure to add a new entry when writing your mod!
1750
	 */
1751
	$context['text_stats'] = array(
1752
		'total_time_online' => array(
1753
			'text' => $context['time_logged_in'],
1754
		),
1755
		'total_posts' => array(
1756
			'text' => $context['num_posts'] . ' ' . $txt['statPanel_posts'],
1757
			'url' => $scripturl . '?action=profile;area=showposts;sa=messages;u=' . $memID
1758
		),
1759
		'total_topics' => array(
1760
			'text' => $context['num_topics'] . ' ' . $txt['statPanel_topics'],
1761
			'url' => $scripturl . '?action=profile;area=showposts;sa=topics;u=' . $memID
1762
		),
1763
		'users_polls' => array(
1764
			'text' => $context['num_polls'] . ' ' . $txt['statPanel_polls'],
1765
		),
1766
		'users_votes' => array(
1767
			'text' => $context['num_votes'] . ' ' . $txt['statPanel_votes']
1768
		)
1769
	);
1770
1771
	// Custom stats (just add a template_layer to add it to the template!)
1772
	call_integration_hook('integrate_profile_stats', array($memID, &$context['text_stats']));
1773
}
1774
1775
/**
1776
 * Loads up the information for the "track user" section of the profile
1777
 *
1778
 * @param int $memID The ID of the member
1779
 */
1780
function tracking($memID)
1781
{
1782
	global $context, $txt, $modSettings, $user_profile;
1783
1784
	$subActions = array(
1785
		'activity' => array('trackActivity', $txt['trackActivity'], 'moderate_forum'),
1786
		'ip' => array('TrackIP', $txt['trackIP'], 'moderate_forum'),
1787
		'edits' => array('trackEdits', $txt['trackEdits'], 'moderate_forum'),
1788
		'groupreq' => array('trackGroupReq', $txt['trackGroupRequests'], 'approve_group_requests'),
1789
		'logins' => array('TrackLogins', $txt['trackLogins'], 'moderate_forum'),
1790
	);
1791
1792
	foreach ($subActions as $sa => $action)
1793
	{
1794
		if (!allowedTo($action[2]))
1795
			unset($subActions[$sa]);
1796
	}
1797
1798
	// Create the tabs for the template.
1799
	$context[$context['profile_menu_name']]['tab_data'] = array(
1800
		'title' => $txt['tracking'],
1801
		'description' => $txt['tracking_description'],
1802
		'icon' => 'profile_hd.png',
1803
		'tabs' => array(
1804
			'activity' => array(),
1805
			'ip' => array(),
1806
			'edits' => array(),
1807
			'groupreq' => array(),
1808
			'logins' => array(),
1809
		),
1810
	);
1811
1812
	// Moderation must be on to track edits.
1813
	if (empty($modSettings['userlog_enabled']))
1814
		unset($context[$context['profile_menu_name']]['tab_data']['edits'], $subActions['edits']);
1815
1816
	// Group requests must be active to show it...
1817
	if (empty($modSettings['show_group_membership']))
1818
		unset($context[$context['profile_menu_name']]['tab_data']['groupreq'], $subActions['groupreq']);
1819
1820
	if (empty($subActions))
1821
		fatal_lang_error('no_access', false);
1822
1823
	$keys = array_keys($subActions);
1824
	$default = array_shift($keys);
1825
	$context['tracking_area'] = isset($_GET['sa']) && isset($subActions[$_GET['sa']]) ? $_GET['sa'] : $default;
1826
1827
	// Set a page title.
1828
	$context['page_title'] = $txt['trackUser'] . ' - ' . $subActions[$context['tracking_area']][1] . ' - ' . $user_profile[$memID]['real_name'];
1829
1830
	// Pass on to the actual function.
1831
	$context['sub_template'] = $subActions[$context['tracking_area']][0];
1832
	$call = call_helper($subActions[$context['tracking_area']][0], true);
1833
1834
	if (!empty($call))
1835
		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

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