xref: /plugin/lastseen/action.php (revision 0c66c82e53105aa4c75f11f0c3fa59f56f42254a)
1<?php
2
3if (!defined('DOKU_INC')) die();
4
5use dokuwiki\Extension\ActionPlugin;
6use dokuwiki\Extension\EventHandler;
7use dokuwiki\Extension\Event;
8
9/**
10 * Last Seen plugin — activity tracker.
11 *
12 * Records the timestamp of each authenticated request so the admin component
13 * can show when every registered user was last active.
14 */
15
16class action_plugin_lastseen extends ActionPlugin
17{
18    /**
19     * @param EventHandler $controller
20     * @return void
21     */
22    public function register(EventHandler $controller)
23    {
24        // DOKUWIKI_STARTED fires on essentially every request, after
25        // authentication has resolved. By this point REMOTE_USER is set for
26        // any logged-in user — whether they just submitted the login form or
27        // arrived with a persistent ("remember me") cookie. Hooking here
28        // therefore records last *activity*, the superset of last *login*.
29        $controller->register_hook('DOKUWIKI_STARTED', 'AFTER', $this, 'recordActivity');
30    }
31
32    /**
33     * @param Event $event  unused (DOKUWIKI_STARTED carries no data)
34     * @return void
35     */
36    public function recordActivity(Event $event)
37    {
38        global $INPUT;
39
40        $user = $INPUT->server->str('REMOTE_USER');
41        if ($user === '') {
42            return; // anonymous request — nothing to record
43        }
44
45        /** @var helper_plugin_lastseen $hlp */
46        $hlp = plugin_load('helper', 'lastseen');
47        if ($hlp === null) {
48            return;
49        }
50
51        // record() is internally throttled — for most requests this is a
52        // cheap read-and-return with no disk write.
53        $hlp->record($user);
54    }
55}
56