Completed
Push — master ( 9c742a...1858b1 )
by Fèvre
14s
created

Controller/Component/SessionsActivityComponent.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace App\Controller\Component;
3
4
use App\Model\Entity\User;
5
use Cake\Controller\Component;
6
use Cake\Event\Event;
7
use Cake\I18n\Time;
8
use Cake\ORM\Entity;
9
use Cake\ORM\TableRegistry;
10
11
class SessionsActivityComponent extends Component
12
{
13
14
    /**
15
     * Request object
16
     *
17
     * @var \Cake\Network\Request
18
     */
19
    protected $_request;
20
21
    /**
22
     * Instance of the Session object
23
     *
24
     * @return void
25
     */
26
    protected $_session;
27
28
    /**
29
     * Initialize properties.
30
     *
31
     * @param array $config The config data.
32
     *
33
     * @return void
34
     */
35
    public function initialize(array $config)
36
    {
37
        $controller = $this->_registry->getController();
38
        $this->_request = $controller->request;
39
        $this->_session = $controller->request->session();
40
    }
41
42
    /**
43
     * Startup event to trace the user on the website.
44
     *
45
     * @param Event $event The event that was fired.
46
     *
47
     * @return void
48
     */
49
    public function startup(Event $event)
0 ignored issues
show
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
50
    {
51
        if (empty($this->_session->id())) {
52
            $this->_session->start();
53
54
            return;
55
        }
56
        $sessions = TableRegistry::get('Sessions');
57
58
        $prefix = isset($this->_request['prefix']) ? $this->_request['prefix'] . '/' : '';
59
        $controller = $prefix . $this->_request['controller'];
60
        $action = $this->_request['action'];
61
        $params = serialize($this->_request->pass);
62
        $expires = time() + ini_get('session.gc_maxlifetime');
63
64
        //@codingStandardsIgnoreStart
65
        $user_id = $this->_session->read('Auth.User.id');
66
        $user_agent = $this->_request->env('HTTP_USER_AGENT');
67
        $user_ip = $this->_request->clientIp();
68
        $full_url = $this->_request->url;
69
        //@codingStandardIgnoreEnd
70
71
        $modified = new Time();
72
73
        $record = compact('controller', 'action', 'params', 'expires', 'user_id', 'user_agent', 'user_ip', 'full_url', 'modified');
74
75
        $record[$sessions->primaryKey()] = $this->_session->id();
76
        $sessions->save(new Entity($record));
77
    }
78
79
    /**
80
     * Get the list of the users online.
81
     *
82
     * @return array
83
     */
84
    public function getOnlineUsers()
85
    {
86
        $sessions = TableRegistry::get('Sessions');
87
88
        $output = [
89
            'guests' => 0,
90
            'members' => 0,
91
        ];
92
93
        $records = $sessions
94
            ->find('expires')
95
            ->contain([
96
                'Users' => function ($q) {
97
                    return $q->select([
98
                        'id',
99
                        'username'
100
                    ]);
101
                },
102
                'Users.Groups' => function ($q) {
103
                    return $q->select(['css', 'is_member']);
104
                },
105
            ])
106
            ->select(['Sessions.user_id', 'Sessions.expires'])
107
            ->group('Sessions.user_id')
108
            ->toArray();
109
110
        foreach ($records as $key => $record) {
111
            if (is_null($record->user_id)) {
112
                $output['guests']++;
113
114
                unset($records[$key]);
115
                continue;
116
            } else {
117
                $output['members']++;
118
            }
119
        }
120
121
        //Total visitors.
122
        $output['total'] = $output['guests'] + $output['members'];
123
124
        //Visitor records.
125
        $output['records'] = $records;
126
127
        return $output;
128
    }
129
130
    /**
131
     * Determine if the given user is online or offline.
132
     *
133
     * @param \App\Model\Entity\User $user The user to check.
134
     *
135
     * @return bool
136
     */
137
    public function getOnlineStatus(User $user)
138
    {
139
        $sessions = TableRegistry::get('Sessions');
140
        $online = $sessions
141
            ->find('expires')
142
            ->select(['Sessions.expires', 'Sessions.user_id'])
143
            ->where([
144
                'Sessions.user_id' => $user->id
145
            ])
146
            ->first();
147
148
        if (!empty($online)) {
149
            return true;
150
        }
151
152
        return false;
153
    }
154
155
    /**
156
     * Get all the sessions online for the given user.
157
     *
158
     * @param int $user The user id.
159
     *
160
     * @return false|array
161
     */
162
    public function getOnlineSessionsForUser($user)
163
    {
164
        if (empty($user) || is_null($user)) {
165
            return false;
166
        }
167
168
        $this->Sessions = TableRegistry::get('Sessions');
169
170
        $sessions = $this->Sessions
171
            ->find('expires')
172
            ->where([
173
                'Sessions.user_id' => $user
174
            ])
175
            ->toArray();
176
177
        return $sessions;
178
    }
179
180
}
181