Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
29.63% covered (danger)
29.63%
8 / 27
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
viewonline_helper
29.63% covered (danger)
29.63%
8 / 27
66.67% covered (warning)
66.67%
2 / 3
44.85
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_forum_ids
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
42
 get_user_page
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
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;
15
16use phpbb\filesystem\helper as filesystem_helper;
17
18/**
19 * Class to handle viewonline related tasks
20 */
21class viewonline_helper
22{
23    /** @var \phpbb\db\driver\driver_interface */
24    protected $db;
25
26    /**
27     * @param \phpbb\db\driver\driver_interface $db
28     */
29    public function __construct(\phpbb\db\driver\driver_interface $db)
30    {
31        $this->db = $db;
32    }
33
34    /**
35     * Get forum IDs for topics
36     *
37     * Retrieve forum IDs and add the data into the session data array
38     * Array structure matches sql_fethrowset() result array
39     *
40     * @param array $session_data_rowset Users' session data array
41     * @return void
42     */
43    public function get_forum_ids(array &$session_data_rowset): void
44    {
45        $topic_ids = $match = [];
46        foreach ($session_data_rowset as $number => $row)
47        {
48            if ($row['session_forum_id'] == 0 && preg_match('#t=([0-9]+)#', $row['session_page'], $match))
49            {
50                $topic_ids[$number] = (int) $match[1];
51            }
52        }
53
54        if (count($topic_ids = array_unique($topic_ids)))
55        {
56            $sql_ary = [
57                'SELECT'    => 't.topic_id, t.forum_id',
58                'FROM'        => [
59                    TOPICS_TABLE => 't',
60                ],
61                'WHERE'        => $this->db->sql_in_set('t.topic_id', $topic_ids),
62                'ORDER_BY'    => 't.topic_id',
63            ];
64            $result = $this->db->sql_query($this->db->sql_build_query('SELECT', $sql_ary));
65            $forum_ids_rowset = $this->db->sql_fetchrowset($result);
66            $this->db->sql_freeresult($result);
67
68            foreach ($forum_ids_rowset as $forum_ids_row)
69            {
70                $session_data_row_number = array_search((int) $forum_ids_row['topic_id'], $topic_ids);
71                $session_data_rowset[$session_data_row_number]['session_forum_id'] = (int) $forum_ids_row['forum_id'];
72            }
73        }
74    }
75
76    /**
77     * Get user page
78     *
79     * @param string $session_page User's session page
80     * @return array Match array filled by preg_match()
81     */
82    public function get_user_page($session_page)
83    {
84        $session_page = filesystem_helper::clean_path($session_page);
85        if (strpos($session_page, './') === 0)
86        {
87            $session_page = substr($session_page, 2);
88        }
89
90        preg_match('#^((\.\./)*([a-z0-9/_-]+))#i', $session_page, $on_page);
91        if (empty($on_page))
92        {
93            $on_page[1] = '';
94        }
95
96        return $on_page;
97    }
98}