aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/Seminar_User.php
blob: 4dd59bc203998f0cf11c7a84eedfafaf87989c96 (plain)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
/**
 * Seminar_User.php
 * global object representing current user
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * @author      André Noack <noack@data-quest.de>
 * @copyright   2000 Stud.IP Core-Group
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
*/

class Seminar_User
{
    public $cfg = null; //UserConfig object
    private $user = null; //User object
    //private $last_online_time = null;

    public function __construct($user = null)
    {
        if ($user instanceOf User) {
            $this->user = $user;
        } else {
            $this->user = User::findFull($user);
        }
        if (!isset($this->user)) {
            $this->user = new User();
            $this->user->user_id = 'nobody';
            $this->user->perms = null;
        }
        $this->cfg = UserConfig::get($this->user->user_id);
        //$this->last_online_time = $this->get_last_action();
    }

    public function getAuthenticatedUser()
    {
        return $this->user->id !== 'nobody' ? $this->user : null;
    }

    private function get_last_action()
    {
        if ($this->id && $this->id != 'nobody') {
            $stmt = DBManager::get()->prepare("SELECT last_lifesign FROM user_online WHERE user_id = ?");
            $stmt->execute([$this->id]);
            return $stmt->fetchColumn();
        }
    }

    public function set_last_action($timestamp = 0)
    {
        if ($this->id && $this->id != 'nobody') {
            if ($timestamp <= 0) {
                $current_user_last_lifesign = $_SESSION['USER_LAST_LIFESIGN'] ?? 0;
                if (time() - $current_user_last_lifesign < 180) {
                    return 0;
                }
                $timestamp = time();
            }
            $query = "INSERT INTO user_online (user_id, last_lifesign)
                      VALUES (:user_id, UNIX_TIMESTAMP() - :time_delta)
                      ON DUPLICATE KEY UPDATE last_lifesign = UNIX_TIMESTAMP() - :time_delta";
            $stmt = DBManager::get()->prepare($query);
            $stmt->bindValue(':user_id', $this->id);
            $stmt->bindValue(':time_delta', time() - $timestamp, PDO::PARAM_INT);
            $stmt->execute();
            $_SESSION['USER_LAST_LIFESIGN'] = time() - $timestamp;
            return $stmt->rowCount();
        }
    }

    public function delete()
    {
        if ($this->id && $this->id != 'nobody') {
            $stmt = DBManager::get()->prepare("DELETE FROM user_online WHERE user_id = ?");
            $stmt->execute([$this->id]);
            return $stmt->rowCount();
        }
    }

    public function __get($field)
    {
        if ($field == 'id') {
            return $this->user->user_id;
        }
        return $this->user->$field;
    }

    public function __set($field, $value)
    {
        return null;
    }

    public function __isset($field)
    {
        return isset($this->user->$field);
    }

    public function getFullName($format = 'full')
    {
        return $this->user->getFullName($format);
    }

    /**
     * Returns whether the current needs to accept the terms of use.
     * @return bool
     */
    public function needsToAcceptTerms()
    {
        return $this->id !== 'nobody'
            && Config::get()->SHOW_TERMS_ON_FIRST_LOGIN
            && !$this->cfg->TERMS_ACCEPTED;
    }
}