Issues (2407)

engine/library/User.php (6 issues)

1
<?php
2
/* 	Divine CMS - Open source CMS for widespread use.
3
    Copyright (c) 2019 Mykola Burakov ([email protected])
4
5
    See SOURCE.txt for other and additional information.
6
7
    This file is part of Divine CMS.
8
9
    This program is free software: you can redistribute it and/or modify
10
    it under the terms of the GNU General Public License as published by
11
    the Free Software Foundation, either version 3 of the License, or
12
    (at your option) any later version.
13
14
    This program is distributed in the hope that it will be useful,
15
    but WITHOUT ANY WARRANTY; without even the implied warranty of
16
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
    GNU General Public License for more details.
18
19
    You should have received a copy of the GNU General Public License
20
    along with this program. If not, see <http://www.gnu.org/licenses/>. */
21
22
namespace Divine\Engine\Library;
23
24
class User
25
{
26
    private $user_id;
27
    private $username;
28
    private $permission = array();
29
30
    public function __construct($registry)
0 ignored issues
show
Expected 2 blank lines before function; 1 found
Loading history...
31
    {
32
        $this->db = $registry->get('db');
0 ignored issues
show
Bug Best Practice introduced by
The property db does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
33
        $this->request = $registry->get('request');
0 ignored issues
show
Bug Best Practice introduced by
The property request does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
34
        $this->session = $registry->get('session');
0 ignored issues
show
Bug Best Practice introduced by
The property session does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
35
36
        if (isset($this->session->data['user_id'])) {
37
            $user_query = $this->db->query("
38
                SELECT * 
39
                FROM user 
40
                WHERE user_id = '" . (int) $this->session->data['user_id'] . "' 
41
                    AND status = '1'
42
            ");
43
44
            if ($user_query->num_rows) {
45
                $this->user_id = $user_query->row['user_id'];
46
                $this->username = $user_query->row['username'];
47
                $this->user_group_id = $user_query->row['user_group_id'];
0 ignored issues
show
Bug Best Practice introduced by
The property user_group_id does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
48
49
                $this->db->query("
50
                    UPDATE user 
51
                    SET ip = '" . $this->db->escape($this->request->server['REMOTE_ADDR']) . "' 
52
                    WHERE user_id = '" . (int) $this->session->data['user_id'] . "'
53
                ");
54
55
                $user_group_query = $this->db->query("
56
                    SELECT permission 
57
                    FROM user_group 
58
                    WHERE user_group_id = '" . (int) $user_query->row['user_group_id'] . "'
59
                ");
60
61
                $permissions = json_decode($user_group_query->row['permission'], true);
62
63
                if (is_array($permissions)) {
64
                    foreach ($permissions as $key => $value) {
65
                        $this->permission[$key] = $value;
66
                    }
67
                }
68
            } else {
69
                $this->logout();
70
            }
71
        }
72
    }
73
74
    public function login($username, $password)
75
    {
76
        $user_query = $this->db->query("
77
            SELECT * 
78
            FROM user 
79
            WHERE username = '" . $this->db->escape($username) . "' 
80
                AND (password = SHA1(CONCAT(salt, SHA1(CONCAT(salt, SHA1('" . $this->db->escape(htmlspecialchars($password, ENT_QUOTES)) . "'))))) 
81
                    OR password = '" . $this->db->escape(md5($password)) . "') 
82
                AND status = '1'
83
        ");
84
85
        if ($user_query->num_rows) {
86
            $this->session->data['user_id'] = $user_query->row['user_id'];
87
88
            $this->user_id = $user_query->row['user_id'];
89
            $this->username = $user_query->row['username'];
90
            $this->user_group_id = $user_query->row['user_group_id'];
0 ignored issues
show
Bug Best Practice introduced by
The property user_group_id does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
91
92
            $user_group_query = $this->db->query("
93
                SELECT permission 
94
                FROM user_group 
95
                WHERE user_group_id = '" . (int) $user_query->row['user_group_id'] . "'
96
            ");
97
98
            $permissions = json_decode($user_group_query->row['permission'], true);
99
100
            if (is_array($permissions)) {
101
                foreach ($permissions as $key => $value) {
102
                    $this->permission[$key] = $value;
103
                }
104
            }
105
106
            return true;
107
        } else {
108
            return false;
109
        }
110
    }
111
112
    public function logout()
113
    {
114
        unset($this->session->data['user_id']);
115
116
        $this->user_id = '';
117
        $this->username = '';
118
    }
119
120
    public function hasPermission($key, $value)
121
    {
122
        if (isset($this->permission[$key])) {
123
            return in_array($value, $this->permission[$key]);
124
        } else {
125
            return false;
126
        }
127
    }
128
129
    public function isLogged()
130
    {
131
        return $this->user_id;
132
    }
133
134
    public function getId()
135
    {
136
        return $this->user_id;
137
    }
138
139
    public function getUserName()
140
    {
141
        return $this->username;
142
    }
143
144
    public function getGroupId()
145
    {
146
        return $this->user_group_id;
147
    }
148
}
149