1<?php
2/**
3 * DokuWiki Plugin autologoff (Helper 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 helper_plugin_autologoff extends DokuWiki_Plugin {
13
14    private $configfile;
15
16    public function __construct() {
17        $this->configfile = DOKU_CONF . '/autologoff.conf';
18    }
19
20    /**
21     * Loads the configuration from config file
22     *
23     * @return array
24     */
25    public function load_config() {
26        $conf = array();
27        foreach((array) confToHash($this->configfile) as $usergroup => $time) {
28            $conf[rawurldecode($usergroup)] = (int) $time;
29        }
30        ksort($conf);
31        return $conf;
32    }
33
34    /**
35     * Adds another entry to tend of the config file
36     *
37     * @param $usergroup
38     * @param $time
39     */
40    public function add_entry($usergroup, $time) {
41        $time = (int) $time;
42        if ($usergroup == '') {
43            msg($this->getLang('groupCannotBeEmpty'), -1);
44            return;
45        }
46        if($time !== 0 && $time < 2) {
47            msg($this->getLang('mintime'), -1);
48            $time = 2;
49        }
50        $usergroup = auth_nameencode($usergroup, true);
51
52        io_saveFile($this->configfile, "$usergroup\t$time\n", true);
53    }
54
55    /**
56     * Removes an entry for the given group or user from config file
57     *
58     * @param $usergroup
59     */
60    public function remove_entry($usergroup){
61        $grep = preg_quote(auth_nameencode($usergroup, true), '/');
62        $grep = '/^'.$grep.'\\t/';
63
64        io_deleteFromFile($this->configfile, $grep, true);
65    }
66
67    /**
68     * Returns the configured time for the current user (in minutes)
69     *
70     * @return int
71     */
72    public function usertime() {
73        global $INFO;
74        global $auth;
75        if(!isset($_SERVER['REMOTE_USER'])) return 0;
76
77        // make sure we have group info on the current user
78        if(isset($INFO) && isset($INFO['userinfo'])){
79            $groups = $INFO['userinfo']['grps'];
80        }else{
81            $info = $auth->getUserData($_SERVER['REMOTE_USER']);
82            $groups = $info['grps'];
83        }
84
85        $config = $this->load_config();
86        $maxtime = 0;
87        foreach($config as $usergroup => $time) {
88            if(!auth_isMember($usergroup, $_SERVER['REMOTE_USER'], (array) $groups)) continue;
89            if($time == 0) return 0;
90            if($time > $maxtime) $maxtime = $time;
91        }
92
93        return $maxtime;
94    }
95}
96
97// vim:ts=4:sw=4:et:
98