xref: /plugin/pureldap/action/expiry.php (revision 0f498d06932ad0cbbdcc8844b96d4913174c7968)
1<?php
2
3/**
4 * DokuWiki Plugin pureldap (Action Component)
5 *
6 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
7 * @author  Andreas Gohr <andi@splitbrain.org>
8 */
9class action_plugin_pureldap_expiry extends \dokuwiki\Extension\ActionPlugin
10{
11    /** @inheritDoc */
12    public function register(Doku_Event_Handler $controller)
13    {
14        $controller->register_hook('DOKUWIKI_STARTED', 'AFTER', $this, 'handlePasswordExpiry');
15    }
16
17    /**
18     * Handle password expiry
19     *
20     * On each page load, check if the user's password is about to expire and display a warning if so.
21     *
22     * @see https://www.dokuwiki.org/devel:events:DOKUWIKI_STARTED
23     * @param Doku_Event $event Event object
24     * @param mixed $param optional parameter passed when event was registered
25     * @return void
26     */
27    public function handlePasswordExpiry(Doku_Event $event, $param)
28    {
29        global $auth;
30        global $ID;
31        global $lang;
32        global $INPUT;
33
34        $user = $INPUT->server->str('REMOTE_USER');
35        if (!$user) return; // no user logged in
36        $userdata = $auth->getUserData($user);
37
38        $warn = $auth->client->getConf('expirywarn'); // days before expiry to warn
39        if (!$warn) return; // no warning configured
40        $max = $auth->client->getMaxPasswordAge(); // max password age in seconds
41        if (!$max) return; // no max password age configured
42        $lastchange = $userdata['lastpwd'] ?? 0; // last password change timestamp
43        if (!$lastchange) return;
44        $expires = $userdata['expires'] ?? false; // password expires
45        if (!$expires) return;
46
47        $warn = $warn * 24 * 60 * 60; // convert to seconds
48        $expiresin = ($lastchange + $max) - time(); // seconds until password expires
49        if ($expiresin > $warn) return; // not yet time to warn
50        $days = ceil($expiresin / (24 * 60 * 60)); // days until password expires
51
52        // prepare and show message
53        $msg = sprintf($this->getLang('pwdexpire'), $days);
54        if ($auth->canDo('modPass')) {
55            $url = wl($ID, ['do' => 'profile']);
56            $msg .= ' <a href="' . $url . '">' . $lang['btn_profile'] . '</a>';
57        }
58        msg($msg);
59    }
60}
61
62