Passed
Push — development ( 2d06b1...8e8389 )
by Spuds
01:13 queued 23s
created

Who::action_credits()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 13
rs 10
c 0
b 0
f 0
ccs 0
cts 10
cp 0
crap 2
1
<?php
2
3
/**
4
 * This file is mainly concerned with the Who's Online list.
5
 * Although, it also handles credits. :P
6
 *
7
 * @package   ElkArte Forum
8
 * @copyright ElkArte Forum contributors
9
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause (see accompanying LICENSE.txt file)
10
 *
11
 * This file contains code covered by:
12
 * copyright: 2011 Simple Machines (http://www.simplemachines.org)
13
 *
14
 * @version 2.0 dev
15
 *
16
 */
17
18
namespace ElkArte\Controller;
19
20
use ElkArte\AbstractController;
21
use ElkArte\Exceptions\Exception;
22
use ElkArte\Helper\Util;
23
use ElkArte\Languages\Txt;
24
use ElkArte\MembersList;
25
26
/**
27
 * I woke up in a Soho doorway A policeman knew my name He said "You can go sleep at home
28
 * tonight If you can get up and walk away"
29
 */
30
class Who extends AbstractController
31
{
32
	/**
33
	 * Default action of this class
34
	 *
35
	 * Accessed with ?action=who
36
	 */
37
	public function action_index()
38
	{
39
		// We know how to... peek at who's online
40
		$this->action_who();
41
	}
42
43
	/**
44
	 * Who's online, and what are they doing?
45
	 *
46
	 * What it does:
47
	 *
48
	 * - This function prepares the who's online data for the Who template.
49
	 * - It requires the who_view permission.
50
	 * - It is enabled with the who_enabled setting.
51
	 * - It is accessed via ?action=who.
52
	 *
53
	 * @uses  template_whos_online() sub-template in Who.template
54
	 * @uses  Who language file.
55
	 */
56
	public function action_who()
57
	{
58
		global $context, $txt, $modSettings;
59
60
		// Permissions, permissions, permissions.
61
		isAllowedTo('who_view');
62
63
		// You can't do anything if this is off.
64
		if (empty($modSettings['who_enabled']))
65
		{
66
			throw new Exception('who_off', false);
67
		}
68
69
		// Load the 'Who' template.
70
		theme()->getTemplates()->load('Who');
71
		Txt::load('Who');
72
73
		// Sort out... the column sorting.
74
		$sort_methods = array(
75
			'user' => 'mem.real_name',
76
			'time' => 'lo.log_time'
77
		);
78
79
		$show_methods = array(
80
			'members' => '(lo.id_member != 0)',
81
			'guests' => '(lo.id_member = 0)',
82
			'all' => '1=1',
83
		);
84
85
		// Store the sort methods and the show types for use in the template.
86
		$context['sort_methods'] = array(
87
			'user' => $txt['who_user'],
88
			'time' => $txt['who_time'],
89
		);
90
91
		$context['show_methods'] = array(
92
			'all' => $txt['who_show_all'],
93
			'members' => $txt['who_show_members_only'],
94
			'guests' => $txt['who_show_guests_only'],
95
		);
96
97
		// Can they see spiders too?
98
		if (!empty($modSettings['show_spider_online']) && ($modSettings['show_spider_online'] == 2 || allowedTo('admin_forum')) && !empty($modSettings['spider_name_cache']))
99
		{
100
			$show_methods['spiders'] = '(lo.id_member = 0 AND lo.id_spider > 0)';
101
			$show_methods['guests'] = '(lo.id_member = 0 AND lo.id_spider = 0)';
102
			$context['show_methods']['spiders'] = $txt['who_show_spiders_only'];
103
		}
104
		elseif (empty($modSettings['show_spider_online']) && isset($_SESSION['who_online_filter']) && $_SESSION['who_online_filter'] === 'spiders')
105
		{
106
			unset($_SESSION['who_online_filter']);
107
		}
108
109
		// Does the user prefer a different sort direction?
110
		if (isset($this->_req->query->sort, $sort_methods[$this->_req->query->sort]))
111
		{
112
			$context['sort_by'] = $_SESSION['who_online_sort_by'] = $this->_req->query->sort;
113
			$sort_method = $sort_methods[$this->_req->query->sort];
114
		}
115
		// Did we set a preferred sort order earlier in the session?
116
		elseif (isset($_SESSION['who_online_sort_by']))
117
		{
118
			$context['sort_by'] = $_SESSION['who_online_sort_by'];
119
			$sort_method = $sort_methods[$_SESSION['who_online_sort_by']];
120
		}
121
		// Default to last time online.
122
		else
123
		{
124
			$context['sort_by'] = $_SESSION['who_online_sort_by'] = 'time';
125
			$sort_method = 'lo.log_time';
126
		}
127
128
		$context['sort_direction'] = isset($this->_req->query->asc) || ($this->_req->getQuery('sort_dir', 'trim', '') === 'asc') ? 'up' : 'down';
129
130
		$conditions = array();
131
		if (!allowedTo('moderate_forum'))
132
		{
133
			$conditions[] = '(COALESCE(mem.show_online, 1) = 1)';
134
		}
135
136
		// Fallback to top filter?
137
		if (isset($this->_req->post->submit_top, $this->_req->post->show_top))
138
		{
139
			$this->_req->post->show = $this->_req->post->show_top;
140
		}
141
142
		// Does the user wish to apply a filter?
143
		if (isset($this->_req->post->show, $show_methods[$this->_req->post->show]))
144
		{
145
			$context['show_by'] = $_SESSION['who_online_filter'] = $this->_req->post->show;
146
			$conditions[] = $show_methods[$this->_req->post->show];
147
		}
148
		// Perhaps we saved a filter earlier in the session?
149
		elseif (isset($_SESSION['who_online_filter']))
150
		{
151
			$context['show_by'] = $_SESSION['who_online_filter'];
152
			$conditions[] = $show_methods[$_SESSION['who_online_filter']];
153
		}
154
		else
155
		{
156
			$context['show_by'] = $_SESSION['who_online_filter'] = 'all';
157
		}
158
159
		require_once(SUBSDIR . '/Members.subs.php');
160
		$totalMembers = countMembersOnline($conditions);
161
162
		// Prepare some page index variables.
163
		$start = $this->_req->get('start', 'intval');
164
		$context['page_index'] = constructPageIndex('{scripturl}?action=who;sort=' . $context['sort_by'] . ($context['sort_direction'] === 'up' ? ';asc' : '') . ';show=' . $context['show_by'], $start, $totalMembers, $modSettings['defaultMaxMembers']);
165
		$context['start'] = $start;
166
		$context['sub_template'] = 'whos_online';
167
		theme()->getLayers()->add('whos_selection');
168
169
		// Look for people online, provided they don't mind if you see they are.
170
		$members = onlineMembers($conditions, $sort_method, $context['sort_direction'], $context['start']);
171
172
		$context['members'] = array();
173
		$member_ids = array();
174
		$url_data = array();
175
176
		foreach ($members as $row)
177
		{
178
			$actions = Util::unserialize($row['url']);
179
			if ($actions === false)
180
			{
181
				continue;
182
			}
183
184
			// Send the information to the template.
185
			$context['members'][$row['session']] = array(
186
				'id' => $row['id_member'],
187
				'ip' => allowedTo('moderate_forum') ? $row['ip'] : '',
188
				// It is *going* to be today or yesterday, so why keep that information in there?
189
				'time' => standardTime($row['log_time'], true),
190
				'html_time' => htmlTime($row['log_time']),
191
				'timestamp' => forum_time(true, $row['log_time']),
192
				'query' => $actions,
193
				'is_hidden' => $row['show_online'] == 0,
194
				'id_spider' => $row['id_spider'],
195
				'color' => empty($row['online_color']) ? '' : $row['online_color']
196
			);
197
198
			$url_data[$row['session']] = array($row['url'], $row['id_member']);
199
			$member_ids[] = $row['id_member'];
200
		}
201
202
		// Load the user data for these members (and the guests).
203
		MembersList::load($member_ids);
204
		MembersList::loadGuest();
205
206
		// Are we showing spiders?
207
		$spiderContext = array();
208
		if (!empty($modSettings['show_spider_online']) && ($modSettings['show_spider_online'] == 2 || allowedTo('admin_forum')) && !empty($modSettings['spider_name_cache']))
209
		{
210
			foreach (unserialize($modSettings['spider_name_cache']) as $id => $name)
211
			{
212
				$spiderContext[$id] = array(
213
					'id' => 0,
214
					'name' => $name,
215
					'group' => $txt['spiders'],
216
					'href' => '',
217
					'link' => $name,
218
					'email' => $name,
219
					'is_guest' => true
220
				);
221
			}
222
		}
223
224
		require_once(SUBSDIR . '/Who.subs.php');
225
		$url_data = determineActions($url_data);
226
227
		// Setup the linktree and page title (do it down here because the language files are now loaded..)
228
		$context['page_title'] = $txt['who_title'];
229
		$context['linktree'][] = array(
230
			'url' => getUrl('action', ['action' => 'who']),
231
			'name' => $txt['who_title']
232
		);
233
234
		foreach ($context['members'] as $i => $member)
235
		{
236
			$member_context = MembersList::get($member['id']);
237
238
			// The following happens when $member['id'] is not found among the loaded for any reason
239
			// in such cases we load a guest dummy
240
			if ($member_context->isEmpty())
241
			{
242
				$member_context = MembersList::get(0);
243
			}
244
			else
245
			{
246
				$member_context->loadContext();
247
			}
248
249
			// Keep the IP that came from the database.
250
			$member_context['ip'] = $member['ip'];
251
			$context['members'][$i]['action'] = $url_data[$i] ?? $txt['who_hidden'];
252
253
			if ($member['id'] == 0 && isset($spiderContext[$member['id_spider']]))
254
			{
255
				$context['members'][$i] += $spiderContext[$member['id_spider']];
256
			}
257
			else
258
			{
259
				$context['members'][$i] += $member_context->toArray()['data'];
260
			}
261
262
			if ($member_context['is_guest'])
263
			{
264
				$context['members'][$i]['track_href'] = getUrl('action', ['action' => 'trackip', 'searchip' => $member_context['ip']]);
265
			}
266
			else
267
			{
268
				$context['members'][$i]['track_href'] = getUrl('profile', ['action' => 'profile', 'area' => 'history', 'sa' => 'ip', 'u' => $member['id'], 'name' => $member_context['name'], 'searchip' => $member_context['ip']]);
269
			}
270
		}
271
272
		// Some people can't send personal messages...
273
		$context['can_send_pm'] = allowedTo('pm_send');
274
		$context['can_send_email'] = allowedTo('send_email_to_members');
275
276
		// Any profile fields disabled?
277
		$context['disabled_fields'] = isset($modSettings['disabled_profile_fields']) ? array_flip(explode(',', $modSettings['disabled_profile_fields'])) : array();
278
	}
279
}
280