Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 111
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
online
0.00% covered (danger)
0.00%
0 / 111
0.00% covered (danger)
0.00%
0 / 2
1806
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
2
 handle
0.00% covered (danger)
0.00%
0 / 98
0.00% covered (danger)
0.00%
0 / 1
1722
1<?php
2/**
3 *
4 * This file is part of the phpBB Forum Software package.
5 *
6 * @copyright (c) phpBB Limited <https://www.phpbb.com>
7 * @license GNU General Public License, version 2 (GPL-2.0)
8 *
9 * For full copyright and license information, please see
10 * the docs/CREDITS.txt file.
11 *
12 */
13
14namespace phpbb\members\controller;
15
16use phpbb\auth\auth;
17use phpbb\config\config;
18use phpbb\event\dispatcher;
19use phpbb\exception\http_exception;
20use phpbb\group\helper as group_helper;
21use phpbb\controller\helper;
22use phpbb\language\language;
23use phpbb\members\viewonline_helper;
24use phpbb\pagination;
25use phpbb\request\request;
26use phpbb\template\template;
27use phpbb\user;
28use Symfony\Component\HttpFoundation\Response;
29
30class online
31{
32    /** @var auth */
33    protected $auth;
34
35    /** @var config */
36    protected $config;
37
38    /** @var dispatcher */
39    protected $dispatcher;
40
41    /** @var group_helper */
42    protected $group_helper;
43
44    /** @var viewonline_helper */
45    protected $viewonline_helper;
46
47    /** @var helper */
48    protected $helper;
49
50    /** @var language */
51    protected $language;
52
53    /** @var pagination */
54    protected $pagination;
55
56    /** @var request */
57    protected $request;
58
59    /** @var template */
60    protected $template;
61
62    /** @var user */
63    protected $user;
64
65    /** @var string */
66    protected $phpbb_root_path;
67
68    /** @var string */
69    protected $php_ex;
70
71    /**
72     * online constructor.
73     * @param auth $auth
74     * @param config $config
75     * @param dispatcher $dispatcher
76     * @param group_helper $group_helper
77     * @param viewonline_helper $viewonline_helper
78     * @param helper $helper
79     * @param language $language
80     * @param pagination $pagination
81     * @param request $request
82     * @param template $template
83     * @param user $user
84     * @param string $phpbb_root_path
85     * @param string $php_ex
86     */
87    public function __construct(auth $auth, config $config, dispatcher $dispatcher, group_helper $group_helper, viewonline_helper $viewonline_helper, helper $helper, language $language, pagination $pagination, request $request, template $template, user $user, string $phpbb_root_path, string $php_ex)
88    {
89        $this->auth                    = $auth;
90        $this->config                = $config;
91        $this->dispatcher            = $dispatcher;
92        $this->group_helper            = $group_helper;
93        $this->viewonline_helper    = $viewonline_helper;
94        $this->helper                = $helper;
95        $this->language                = $language;
96        $this->pagination            = $pagination;
97        $this->request                = $request;
98        $this->template                = $template;
99        $this->user                    = $user;
100        $this->phpbb_root_path        = $phpbb_root_path;
101        $this->php_ex                = $php_ex;
102    }
103
104    /**
105     * Controller for /online route
106     *
107     * @return Response a Symfony response object
108     */
109    public function handle(): Response
110    {
111        // Display a listing of board admins, moderators
112        if (!function_exists('display_user_activity'))
113        {
114            include($this->phpbb_root_path . 'includes/functions_display.' . $this->php_ex);
115        }
116
117        // Load language strings
118        $this->language->add_lang('memberlist');
119
120        // Can this user view profiles/memberlist?
121        if (!$this->auth->acl_gets('u_viewprofile', 'a_user', 'a_useradd', 'a_userdel'))
122        {
123            if ($this->user->data['user_id'] != ANONYMOUS)
124            {
125                throw new http_exception(403, 'NO_VIEW_USERS');
126            }
127
128            login_box('', $this->language->lang('LOGIN_EXPLAIN_VIEWONLINE'));
129        }
130
131        // Get and set some variables
132        $mode            = $this->request->variable('mode', '');
133        $session_id        = $this->request->variable('s', '');
134        $start            = $this->request->variable('start', 0);
135        $sort_key        = $this->request->variable('sk', 'b');
136        $sort_dir        = $this->request->variable('sd', 'd');
137        $show_guests    = ($this->config['load_online_guests']) ? $this->request->variable('sg', 0) : 0;
138
139        $sort_key_text = ['a' => $this->language->lang('SORT_USERNAME'), 'b' => $this->language->lang('SORT_JOINED'), 'c' => $this->language->lang('SORT_LOCATION')];
140        $sort_key_sql = ['a' => 'u.username_clean', 'b' => 's.session_time', 'c' => 's.session_page'];
141
142        // Sorting and order
143        if (!isset($sort_key_text[$sort_key]))
144        {
145            $sort_key = 'b';
146        }
147
148        $order_by = $sort_key_sql[$sort_key] . ' ' . (($sort_dir == 'a') ? 'ASC' : 'DESC');
149
150        $this->user->update_session_infos();
151
152        // Get number of online guests (if we do not display them)
153        $guest_counter = (!$show_guests) ? $this->viewonline_helper->get_number_guests() : 0;
154
155        // Get user list (moved into viewonline_helper)
156        $session_data_rowset = $this->viewonline_helper->get_session_data_rowset($show_guests, $order_by, $guest_counter);
157
158        $prev_id = $prev_ip = $user_list = [];
159        $logged_visible_online = $logged_hidden_online = $counter = 0;
160
161        // Get forum IDs for session pages which have only 't' parameter
162        $this->viewonline_helper->get_forum_ids($session_data_rowset);
163
164        foreach ($session_data_rowset as $row)
165        {
166            if ($row['user_id'] != ANONYMOUS && !isset($prev_id[$row['user_id']]))
167            {
168                $view_online = $s_user_hidden = false;
169                $user_colour = ($row['user_colour']) ? ' style="color:#' . $row['user_colour'] . '" class="username-coloured"' : '';
170
171                $username_full = ($row['user_type'] != USER_IGNORE) ? get_username_string('full', $row['user_id'], $row['username'], $row['user_colour']) : '<span' . $user_colour . '>' . $row['username'] . '</span>';
172
173                if (!$row['session_viewonline'])
174                {
175                    $view_online = ($this->auth->acl_get('u_viewonline') || $row['user_id'] === $this->user->data['user_id']) ? true : false;
176                    $logged_hidden_online++;
177
178                    $username_full = '<em>' . $username_full . '</em>';
179                    $s_user_hidden = true;
180                }
181                else
182                {
183                    $view_online = true;
184                    $logged_visible_online++;
185                }
186
187                $prev_id[$row['user_id']] = 1;
188
189                if ($view_online)
190                {
191                    $counter++;
192                }
193
194                if (!$view_online || $counter > $start + $this->config['topics_per_page'] || $counter <= $start)
195                {
196                    continue;
197                }
198            }
199            else if ($show_guests && $row['user_id'] == ANONYMOUS && !isset($prev_ip[$row['session_ip']]))
200            {
201                $prev_ip[$row['session_ip']] = 1;
202                $guest_counter++;
203                $counter++;
204
205                if ($counter > $start + $this->config['topics_per_page'] || $counter <= $start)
206                {
207                    continue;
208                }
209
210                $s_user_hidden = false;
211                $username_full = get_username_string('full', $row['user_id'], $this->language->lang('GUEST'));
212            }
213            else
214            {
215                continue;
216            }
217
218            list($location, $location_url) = $this->viewonline_helper->get_location($row['session_page'], $row['session_forum_id']);
219
220            $session_page = parse_url($row['session_page'], PHP_URL_PATH);
221            $session_page = preg_replace('/^\/app\.php\//', '/', $session_page);
222            $on_page = $this->viewonline_helper->get_user_page($session_page);
223
224            $forum_data = $this->viewonline_helper->get_forum_data();
225
226            /**
227            * Overwrite the location's name and URL, which are displayed in the list
228            *
229            * @event core.viewonline_overwrite_location
230            * @var    array    on_page            File name and query string
231            * @var    array    row                Array with the users sql row
232            * @var    string    location        Page name to displayed in the list
233            * @var    string    location_url    Page url to displayed in the list
234            * @var    array    forum_data        Array with forum data
235            * @since 3.1.0-a1
236            * @changed 3.1.0-a2 Added var forum_data
237            */
238            $vars = ['on_page', 'row', 'location', 'location_url', 'forum_data'];
239            extract($this->dispatcher->trigger_event('core.viewonline_overwrite_location', compact($vars)));
240
241            $template_row = [
242                'USERNAME'             => $row['username'],
243                'USERNAME_COLOUR'    => $row['user_colour'],
244                'USERNAME_FULL'        => $username_full,
245                'LASTUPDATE'        => $this->user->format_date($row['session_time']),
246                'FORUM_LOCATION'    => $location,
247                'USER_IP'            => ($this->auth->acl_get('a_')) ? (($mode == 'lookup' && $session_id == $row['session_id']) ? gethostbyaddr($row['session_ip']) : $row['session_ip']) : '',
248                'USER_BROWSER'        => ($this->auth->acl_get('a_user')) ? $row['session_browser'] : '',
249
250                'U_USER_PROFILE'    => ($row['user_type'] != USER_IGNORE) ? get_username_string('profile', $row['user_id'], '') : '',
251                'U_USER_IP'            => append_sid($this->phpbb_root_path . "viewonline." . $this->php_ex, 'mode=lookup' . (($mode != 'lookup' || $row['session_id'] != $session_id) ? '&amp;s=' . $row['session_id'] : '') . "&amp;sg=$show_guests&amp;start=$start&amp;sk=$sort_key&amp;sd=$sort_dir"),
252                'U_WHOIS'            => $this->helper->route('phpbb_members_online_whois', ['session_id' => $row['session_id']]),
253                'U_FORUM_LOCATION'    => $location_url,
254
255                'S_USER_HIDDEN'        => $s_user_hidden,
256                'S_GUEST'            => ($row['user_id'] == ANONYMOUS) ? true : false,
257                'S_USER_TYPE'        => $row['user_type'],
258            ];
259
260            /**
261            * Modify viewonline template data before it is displayed in the list
262            *
263            * @event core.viewonline_modify_user_row
264            * @var    array    on_page            File name and query string
265            * @var    array    row                Array with the users sql row
266            * @var    array    forum_data        Array with forum data
267            * @var    array    template_row    Array with template variables for the user row
268            * @since 3.1.0-RC4
269            */
270            $vars = ['on_page', 'row', 'forum_data', 'template_row'];
271            extract($this->dispatcher->trigger_event('core.viewonline_modify_user_row', compact($vars)));
272
273            $this->template->assign_block_vars('user_row', $template_row);
274        }
275
276        $this->group_helper->display_legend();
277
278        // Refreshing the page every 60 seconds...
279        meta_refresh(60, $this->helper->route('phpbb_members_online', ['sg' => $show_guests, 'sk' => $sort_key, 'sd' => $sort_dir, 'start' => $start]));
280
281        $start = $this->pagination->validate_start($start, $this->config['topics_per_page'], $counter);
282        $base_url = $this->helper->route('phpbb_members_online', ['sg' => $show_guests, 'sk' => $sort_key, 'sd' => $sort_dir]);
283        $this->pagination->generate_template_pagination($base_url, 'pagination', 'start', $counter, $this->config['topics_per_page'], $start);
284
285        // Send data to template
286        $this->template->assign_vars([
287            'TOTAL_REGISTERED_USERS_ONLINE'    => $this->language->lang('REG_USERS_ONLINE', (int) $logged_visible_online, $this->language->lang('HIDDEN_USERS_ONLINE', (int) $logged_hidden_online)),
288            'TOTAL_GUEST_USERS_ONLINE'        => $this->language->lang('GUEST_USERS_ONLINE', (int) $guest_counter),
289
290            'U_SORT_USERNAME'        => $this->helper->route('phpbb_members_online', ['sg' => (int) $show_guests, 'sk' => 'a', 'sd' => (($sort_key == 'a' && $sort_dir == 'a') ? 'd' : 'a')]),
291            'U_SORT_UPDATED'        => $this->helper->route('phpbb_members_online', ['sg' => (int) $show_guests, 'sk' => 'b', 'sd' => (($sort_key == 'b' && $sort_dir == 'a') ? 'd' : 'a')]),
292            'U_SORT_LOCATION'        => $this->helper->route('phpbb_members_online', ['sg' => (int) $show_guests, 'sk' => 'c', 'sd' => (($sort_key == 'c' && $sort_dir == 'a') ? 'd' : 'a')]),
293
294            'U_SWITCH_GUEST_DISPLAY'    => $this->helper->route('phpbb_members_online', ['sg' => (int) !$show_guests]),
295            'L_SWITCH_GUEST_DISPLAY'    => ($show_guests) ? $this->language->lang('HIDE_GUESTS') : $this->language->lang('DISPLAY_GUESTS'),
296            'S_SWITCH_GUEST_DISPLAY'    => ($this->config['load_online_guests']) ? true : false,
297            'S_VIEWONLINE'                => true,
298        ]);
299
300        $this->template->assign_block_vars('navlinks', [
301            'BREADCRUMB_NAME'    => $this->language->lang('WHO_IS_ONLINE'),
302            'U_BREADCRUMB'        => $this->helper->route('phpbb_members_online'),
303        ]);
304
305        make_jumpbox(append_sid($this->phpbb_root_path . "viewforum." . $this->php_ex));
306
307        // Render
308        return $this->helper->render('viewonline_body.html', $this->language->lang('WHO_IS_ONLINE'));
309    }
310
311}