1<?php
2/**
3 * DokuWiki Plugin autologoff (Action Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Andreas Gohr <gohr@cosmocode.de>
7 */
8
9// must be run within Dokuwiki
10if(!defined('DOKU_INC')) die();
11
12class action_plugin_autologoff extends DokuWiki_Action_Plugin {
13    /** @var  helper_plugin_autologoff */
14    private $helper;
15
16    public function __construct(){
17        $this->helper = $this->loadHelper('autologoff', false);
18    }
19
20    /**
21     * Registers a callback function for a given event
22     *
23     * @param Doku_Event_Handler $controller DokuWiki's event controller object
24     * @return void
25     */
26    public function register(Doku_Event_Handler $controller) {
27
28        $controller->register_hook('DOKUWIKI_STARTED', 'BEFORE', $this, 'handle_dokuwiki_started');
29        $controller->register_hook('DETAIL_STARTED', 'BEFORE', $this, 'handle_dokuwiki_started');
30        $controller->register_hook('MEDIAMANAGER_STARTED', 'BEFORE', $this, 'handle_dokuwiki_started');
31
32        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handle_ajax');
33    }
34
35    /**
36     * Check for timeouts, remember last activity
37     *
38     * @param Doku_Event $event  event object by reference
39     * @param mixed $param  [the parameters passed as fifth argument to register_hook() when this
40     *                           handler was registered]
41     * @return void
42     */
43
44    public function handle_dokuwiki_started(Doku_Event &$event, $param) {
45        global $ID;
46        global $JSINFO;
47
48        $time = $this->helper->usertime();
49        if(!$time) return;
50
51        // check if the time has expired meanwhile
52        if(isset($_SESSION[DOKU_COOKIE]['autologoff'])) {
53            /** @var int $idle_time */
54            $idle_time = time() - $_SESSION[DOKU_COOKIE]['autologoff'];
55            if( $idle_time >= $time * 60) {
56                msg(sprintf($this->getLang('loggedoff'), hsc($_SERVER['REMOTE_USER'])));
57                unset($_SESSION[DOKU_COOKIE]['autologoff']);
58                $event = new Doku_Event('ACTION_AUTH_AUTOLOGOUT', $idle_time);
59                $event->advise_before(false);
60                auth_logoff();
61                $event->advise_after();
62                send_redirect(wl($ID, '', true, '&'));
63            }
64        }
65
66        // update the time
67        $_SESSION[DOKU_COOKIE]['autologoff'] = time();
68        $JSINFO['autologoff'] = $time;
69    }
70
71    /**
72     * Ajax function returning the remaining time
73     *
74     * @param Doku_Event $event
75     * @param $param
76     */
77    public function handle_ajax(Doku_Event &$event, $param) {
78        if($event->data != 'autologoff') return;
79        $event->preventDefault();
80
81        header('Content-Type: text/plain');
82
83        $time = $this->helper->usertime();
84        if(!$time){
85            echo 0;
86            exit;
87        }
88
89        // user hit button to stay logged in
90        if(isset($_REQUEST['refresh'])){
91            session_start();
92            $_SESSION[DOKU_COOKIE]['autologoff'] = time();
93            session_write_close();
94        }
95
96        echo(($time * 60) - (time() - $_SESSION[DOKU_COOKIE]['autologoff']));
97    }
98}
99
100// vim:ts=4:sw=4:et:
101