File: LastSeenTimeLogger.php

package info (click to toggle)
matomo 5.5.1%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 73,596 kB
  • sloc: php: 231,041; javascript: 102,286; python: 202; xml: 189; sh: 172; makefile: 20; sql: 10
file content (91 lines) | stat: -rw-r--r-- 2,782 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php

/**
 * Matomo - free/libre analytics platform
 *
 * @link    https://matomo.org
 * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

namespace Piwik\Plugins\UsersManager;

use Piwik\Common;
use Piwik\Date;
use Piwik\Piwik;

/**
 * Class that logs the time the current user is accessing the current resource (which is 'now')
 * so it can be retrieved later.
 */
class LastSeenTimeLogger
{
    /**
     * The amount of time in seconds that a last seen value is considered valid. We don't want
     * to update the database for every request made by every user, so we only do it if the time
     * has been at least this many seconds from the last known time.
     */
    public const LAST_TIME_SAVE_DELTA = 300;

    /**
     * Returns the time a user was last seen or `null` if the user has never logged in
     */
    public static function getLastSeenTimeForUser($userName): ?int
    {
        try {
            $userModel = new Model();
            return $userModel->getLastSeenTimestamp($userName);
        } catch (\Exception $e) {
            return null;
        }
    }

    /**
     * Returns the last seen time for all users if it has been set
     */
    public static function getLastSeenTimesForAllUsers(): array
    {
        try {
            $userModel = new Model();
            return $userModel->getLastSeenTimestampForAllSeenUsers();
        } catch (\Exception $e) {
            return [];
        }
    }

    /**
     * Saves the current time for a user as an option if the current request is for something
     * in the reporting UI, the current user is not anonymous, and the time hasn't been saved
     * in the last 5 minutes.
     */
    public static function logCurrentUserLastSeenTime()
    {
        $module = Common::getRequestVar('module', '');
        $currentUserLogin = Piwik::getCurrentUserLogin();

        // only log time for non-anonymous visits to the reporting UI
        if (
            $module == 'API'
            || $module == 'Proxy'
            || Piwik::isUserIsAnonymous()
        ) {
            return;
        }


        try {
            // get the last known time
            $lastSeen = self::getLastSeenTimeForUser($currentUserLogin);

            // do not log if the last known time is less than N seconds from now (so we don't make too many queries)
            if ($lastSeen && (time() - $lastSeen <= self::LAST_TIME_SAVE_DELTA)) {
                return;
            }

            // log last seen time
            $userModel = new Model();
            $userModel->setLastSeenDatetime($currentUserLogin, Date::factory('now')->getDatetime());
        } catch (\Exception $e) {
            // do nothing if getting or setting the timestamp fails, e.g. during an upgrade
        }
    }
}