forked from matomo-org/matomo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLastSeenTimeLogger.php
71 lines (62 loc) · 2.19 KB
/
LastSeenTimeLogger.php
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
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\UsersManager;
use Piwik\Common;
use Piwik\Option;
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
{
const OPTION_PREFIX = 'UsersManager.lastSeen.';
/**
* 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.
*/
const LAST_TIME_SAVE_DELTA = 300;
/**
* 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 function logCurrentUserLastSeenTime()
{
$module = Common::getRequestVar('module', false);
$currentUserLogin = Piwik::getCurrentUserLogin();
// only log time for non-anonymous visits to the reporting UI
if ($module == 'API'
|| $module == 'Proxy'
|| $currentUserLogin == 'anonymous'
) {
return;
}
// get the last known time
$optionName = self::OPTION_PREFIX . $currentUserLogin;
$lastSeen = Option::get($optionName);
// do not log if last known time is less than N minutes from now (so we don't make too many
// queries)
if (time() - $lastSeen <= self::LAST_TIME_SAVE_DELTA) {
return;
}
// log last seen time (Note: autoload is important so the Option::get above does not result in
// a separate query)
Option::set($optionName, time(), $autoload = 1);
}
/**
* Returns the time a user was last seen or `false` if the user has never logged in.
*/
public static function getLastSeenTimeForUser($userName)
{
$optionName = self::OPTION_PREFIX . $userName;
return Option::get($optionName);
}
}