|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* Divine CMS - Open source CMS for widespread use. |
|
4
|
|
|
Copyright (c) 2019 Mykola Burakov ([email protected]) |
|
5
|
|
|
|
|
6
|
|
|
See SOURCE.txt for other and additional information. |
|
7
|
|
|
|
|
8
|
|
|
This file is part of Divine CMS. |
|
9
|
|
|
|
|
10
|
|
|
This program is free software: you can redistribute it and/or modify |
|
11
|
|
|
it under the terms of the GNU General Public License as published by |
|
12
|
|
|
the Free Software Foundation, either version 3 of the License, or |
|
13
|
|
|
(at your option) any later version. |
|
14
|
|
|
|
|
15
|
|
|
This program is distributed in the hope that it will be useful, |
|
16
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
17
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
18
|
|
|
GNU General Public License for more details. |
|
19
|
|
|
|
|
20
|
|
|
You should have received a copy of the GNU General Public License |
|
21
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
|
22
|
|
|
|
|
23
|
|
|
class ModelUserUser extends \Divine\Engine\Core\Model |
|
|
|
|
|
|
24
|
|
|
{ |
|
25
|
|
|
public function addUser($data) |
|
|
|
|
|
|
26
|
|
|
{ |
|
27
|
|
|
$this->db->query(" |
|
28
|
|
|
INSERT INTO `user` |
|
29
|
|
|
SET username = '" . $this->db->escape($data['username']) . "', |
|
30
|
|
|
user_group_id = '" . (int)$data['user_group_id'] . "', |
|
31
|
|
|
salt = '" . $this->db->escape($salt = (new \Tokenly\TokenGenerator\TokenGenerator())->generateToken(9, 'SR')) . "', |
|
32
|
|
|
password = '" . $this->db->escape(sha1($salt . sha1($salt . sha1($data['password'])))) . "', |
|
33
|
|
|
firstname = '" . $this->db->escape($data['firstname']) . "', |
|
34
|
|
|
lastname = '" . $this->db->escape($data['lastname']) . "', |
|
35
|
|
|
email = '" . $this->db->escape($data['email']) . "', |
|
36
|
|
|
image = '" . $this->db->escape($data['image']) . "', |
|
37
|
|
|
status = '" . (int)$data['status'] . "', |
|
38
|
|
|
date_added = NOW() |
|
39
|
|
|
"); |
|
40
|
|
|
|
|
41
|
|
|
return $this->db->getLastId(); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function editUser($user_id, $data) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->db->query(" |
|
47
|
|
|
UPDATE `user` |
|
48
|
|
|
SET username = '" . $this->db->escape($data['username']) . "', |
|
49
|
|
|
user_group_id = '" . (int)$data['user_group_id'] . "', |
|
50
|
|
|
firstname = '" . $this->db->escape($data['firstname']) . "', |
|
51
|
|
|
lastname = '" . $this->db->escape($data['lastname']) . "', |
|
52
|
|
|
email = '" . $this->db->escape($data['email']) . "', |
|
53
|
|
|
image = '" . $this->db->escape($data['image']) . "', |
|
54
|
|
|
status = '" . (int)$data['status'] . "' |
|
55
|
|
|
WHERE user_id = '" . (int)$user_id . "' |
|
56
|
|
|
"); |
|
57
|
|
|
|
|
58
|
|
|
if ($data['password']) { |
|
59
|
|
|
$this->db->query(" |
|
60
|
|
|
UPDATE `user` |
|
61
|
|
|
SET salt = '" . $this->db->escape($salt = (new \Tokenly\TokenGenerator\TokenGenerator())->generateToken(9, 'SR')) . "', |
|
62
|
|
|
password = '" . $this->db->escape(sha1($salt . sha1($salt . sha1($data['password'])))) . "' |
|
63
|
|
|
WHERE user_id = '" . (int)$user_id . "' |
|
64
|
|
|
"); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function editPassword($user_id, $password) |
|
69
|
|
|
{ |
|
70
|
|
|
$this->db->query(" |
|
71
|
|
|
UPDATE `user` |
|
72
|
|
|
SET salt = '" . $this->db->escape($salt = (new \Tokenly\TokenGenerator\TokenGenerator())->generateToken(9, 'SR')) . "', |
|
73
|
|
|
password = '" . $this->db->escape(sha1($salt . sha1($salt . sha1($password)))) . "', |
|
74
|
|
|
code = '' |
|
75
|
|
|
WHERE user_id = '" . (int)$user_id . "' |
|
76
|
|
|
"); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function editCode($email, $code) |
|
80
|
|
|
{ |
|
81
|
|
|
$this->db->query(" |
|
82
|
|
|
UPDATE `user` |
|
83
|
|
|
SET code = '" . $this->db->escape($code) . "' |
|
84
|
|
|
WHERE LCASE(email) = '" . $this->db->escape(\voku\helper\UTF8::strtolower($email)) . "' |
|
85
|
|
|
"); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function deleteUser($user_id) |
|
89
|
|
|
{ |
|
90
|
|
|
$this->db->query(" |
|
91
|
|
|
DELETE |
|
92
|
|
|
FROM `user` |
|
93
|
|
|
WHERE user_id = '" . (int)$user_id . "' |
|
94
|
|
|
"); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function getUser($user_id) |
|
98
|
|
|
{ |
|
99
|
|
|
$query = $this->db->query(" |
|
100
|
|
|
SELECT *, |
|
101
|
|
|
( |
|
102
|
|
|
SELECT ug.name |
|
103
|
|
|
FROM `user_group` ug |
|
104
|
|
|
WHERE ug.user_group_id = u.user_group_id) AS user_group |
|
105
|
|
|
FROM `user` u |
|
106
|
|
|
WHERE u.user_id = '" . (int)$user_id . "' |
|
107
|
|
|
"); |
|
108
|
|
|
|
|
109
|
|
|
return $query->row; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function getUserByUsername($username) |
|
113
|
|
|
{ |
|
114
|
|
|
$query = $this->db->query(" |
|
115
|
|
|
SELECT * |
|
116
|
|
|
FROM `user` |
|
117
|
|
|
WHERE username = '" . $this->db->escape($username) . "' |
|
118
|
|
|
"); |
|
119
|
|
|
|
|
120
|
|
|
return $query->row; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function getUserByEmail($email) |
|
124
|
|
|
{ |
|
125
|
|
|
$query = $this->db->query(" |
|
126
|
|
|
SELECT DISTINCT * |
|
127
|
|
|
FROM `user` |
|
128
|
|
|
WHERE LCASE(email) = '" . $this->db->escape(\voku\helper\UTF8::strtolower($email)) . "' |
|
129
|
|
|
"); |
|
130
|
|
|
|
|
131
|
|
|
return $query->row; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
public function getUserByCode($code) |
|
135
|
|
|
{ |
|
136
|
|
|
$query = $this->db->query(" |
|
137
|
|
|
SELECT * |
|
138
|
|
|
FROM `user` |
|
139
|
|
|
WHERE code = '" . $this->db->escape($code) . "' |
|
140
|
|
|
AND code != '' |
|
141
|
|
|
"); |
|
142
|
|
|
|
|
143
|
|
|
return $query->row; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
public function getUsers($data = array()) |
|
147
|
|
|
{ |
|
148
|
|
|
$sql = " |
|
|
|
|
|
|
149
|
|
|
SELECT * |
|
150
|
|
|
FROM `user` |
|
151
|
|
|
"; |
|
152
|
|
|
|
|
153
|
|
|
$sort_data = array( |
|
154
|
|
|
'username', |
|
155
|
|
|
'status', |
|
156
|
|
|
'date_added' |
|
157
|
|
|
); |
|
158
|
|
|
|
|
159
|
|
|
if (isset($data['sort']) && in_array($data['sort'], $sort_data)) { |
|
160
|
|
|
$sql .= " ORDER BY " . $data['sort']; |
|
|
|
|
|
|
161
|
|
|
} else { |
|
162
|
|
|
$sql .= " ORDER BY username"; |
|
|
|
|
|
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
if (isset($data['order']) && ($data['order'] == 'DESC')) { |
|
166
|
|
|
$sql .= " DESC"; |
|
|
|
|
|
|
167
|
|
|
} else { |
|
168
|
|
|
$sql .= " ASC"; |
|
|
|
|
|
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
if (isset($data['start']) || isset($data['limit'])) { |
|
172
|
|
|
if ($data['start'] < 0) { |
|
173
|
|
|
$data['start'] = 0; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
if ($data['limit'] < 1) { |
|
177
|
|
|
$data['limit'] = 20; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit']; |
|
|
|
|
|
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
$query = $this->db->query($sql); |
|
184
|
|
|
|
|
185
|
|
|
return $query->rows; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
public function getTotalUsers() |
|
189
|
|
|
{ |
|
190
|
|
|
$query = $this->db->query(" |
|
|
|
|
|
|
191
|
|
|
SELECT COUNT(*) AS total |
|
192
|
|
|
FROM `user` |
|
193
|
|
|
"); |
|
194
|
|
|
|
|
195
|
|
|
return $query->row['total']; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
public function getTotalUsersByGroupId($user_group_id) |
|
199
|
|
|
{ |
|
200
|
|
|
$query = $this->db->query(" |
|
201
|
|
|
SELECT COUNT(*) AS total |
|
202
|
|
|
FROM `user` |
|
203
|
|
|
WHERE user_group_id = '" . (int)$user_group_id . "' |
|
204
|
|
|
"); |
|
205
|
|
|
|
|
206
|
|
|
return $query->row['total']; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
public function getTotalUsersByEmail($email) |
|
210
|
|
|
{ |
|
211
|
|
|
$query = $this->db->query(" |
|
212
|
|
|
SELECT COUNT(*) AS total |
|
213
|
|
|
FROM `user` |
|
214
|
|
|
WHERE LCASE(email) = '" . $this->db->escape(\voku\helper\UTF8::strtolower($email)) . "' |
|
215
|
|
|
"); |
|
216
|
|
|
|
|
217
|
|
|
return $query->row['total']; |
|
218
|
|
|
} |
|
219
|
|
|
} |
|
220
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.