summaryrefslogtreecommitdiff
path: root/includes/sys_auth.php
blob: 91edca776b29bd40ae50e1eedb070247d45d641b (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php

use Engelsystem\Database\DB;

/**
 *  Testet ob ein User eingeloggt ist und lädt die entsprechenden Privilegien
 */
function load_auth()
{
    global $user, $privileges;

    $user = null;
    $session = session();

    if ($session->has('uid')) {
        $user = DB::selectOne('SELECT * FROM `User` WHERE `UID`=? LIMIT 1', [$session->get('uid')]);
        if (!empty($user)) {
            // User ist eingeloggt, Datensatz zur Verfügung stellen und Timestamp updaten
            DB::update('
                UPDATE `User`
                SET `lastLogIn` = ?
                WHERE `UID` = ?
                LIMIT 1
            ', [
                time(),
                $session->get('uid'),
            ]);
            $privileges = privileges_for_user($user['UID']);
            return;
        }

        $session->remove('uid');
    }

    // guest privileges
    $privileges = privileges_for_group(-10);
}

/**
 * generate a salt (random string) of arbitrary length suitable for the use with crypt()
 *
 * @param int $length
 * @return string
 */
function generate_salt($length = 16)
{
    $alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
    $salt = '';
    for ($i = 0; $i < $length; $i++) {
        $salt .= $alphabet[rand(0, strlen($alphabet) - 1)];
    }
    return $salt;
}

/**
 * set the password of a user
 *
 * @param int    $uid
 * @param string $password
 */
function set_password($uid, $password)
{
    DB::update('
        UPDATE `User`
        SET `Passwort` = ?,
        `password_recovery_token`=NULL
        WHERE `UID` = ?
        LIMIT 1
        ',
        [
            crypt($password, config('crypt_alg') . '$' . generate_salt(16) . '$'),
            $uid
        ]
    );
}

/**
 * verify a password given a precomputed salt.
 * if $uid is given and $salt is an old-style salt (plain md5), we convert it automatically
 *
 * @param string $password
 * @param string $salt
 * @param int    $uid
 * @return bool
 */
function verify_password($password, $salt, $uid = null)
{
    $crypt_alg = config('crypt_alg');
    $correct = false;
    if (substr($salt, 0, 1) == '$') {
        // new-style crypt()
        $correct = crypt($password, $salt) == $salt;
    } elseif (substr($salt, 0, 7) == '{crypt}') {
        // old-style crypt() with DES and static salt - not used anymore
        $correct = crypt($password, '77') == $salt;
    } elseif (strlen($salt) == 32) {
        // old-style md5 without salt - not used anymore
        $correct = md5($password) == $salt;
    }

    if ($correct && substr($salt, 0, strlen($crypt_alg)) != $crypt_alg && intval($uid)) {
        // this password is stored in another format than we want it to be.
        // let's update it!
        // we duplicate the query from the above set_password() function to have the extra safety of checking
        // the old hash
        DB::update('
                UPDATE `User`
                SET `Passwort` = ?
                WHERE `UID` = ?
                AND `Passwort` = ?
                LIMIT 1
            ',
            [
                crypt($password, $crypt_alg . '$' . generate_salt() . '$'),
                $uid,
                $salt,
            ]
        );
    }
    return $correct;
}

/**
 * @param int $user_id
 * @return array
 */
function privileges_for_user($user_id)
{
    $privileges = [];
    $user_privileges = DB::select('
        SELECT `Privileges`.`name`
        FROM `User`
        JOIN `UserGroups` ON (`User`.`UID` = `UserGroups`.`uid`)
        JOIN `GroupPrivileges` ON (`UserGroups`.`group_id` = `GroupPrivileges`.`group_id`)
        JOIN `Privileges` ON (`GroupPrivileges`.`privilege_id` = `Privileges`.`id`)
        WHERE `User`.`UID`=?
    ', [$user_id]);
    foreach ($user_privileges as $user_privilege) {
        $privileges[] = $user_privilege['name'];
    }
    return $privileges;
}

/**
 * @param int $group_id
 * @return array
 */
function privileges_for_group($group_id)
{
    $privileges = [];
    $groups_privileges = DB::select('
        SELECT `name`
        FROM `GroupPrivileges`
        JOIN `Privileges` ON (`GroupPrivileges`.`privilege_id` = `Privileges`.`id`)
        WHERE `group_id`=?
    ', [$group_id]);
    foreach ($groups_privileges as $guest_privilege) {
        $privileges[] = $guest_privilege['name'];
    }
    return $privileges;
}