-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathService.php
More file actions
340 lines (247 loc) · 10 KB
/
Service.php
File metadata and controls
340 lines (247 loc) · 10 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
<?php
namespace MerapiPanel\Module\User;
use MerapiPanel\Box;
use MerapiPanel\Box\Module\__Fragment;
use MerapiPanel\Database\DB;
use MerapiPanel\Utility\Util;
use PDO;
class Service extends __Fragment
{
protected $module;
function onCreate(Box\Module\Entity\Module $module)
{
$this->module = $module;
}
function getRoleNames()
{
return Util::getRoles();
}
function update($id, $name, $email, $password, $confirmPassword, $role, $status)
{
if (!$this->module->getRoles()->isAllowed(1)) {
throw new \Exception('Permission denied');
}
if (empty($id)) {
throw new \Exception('Missing required parameter: id');
}
$user = DB::table("users")->select("*")->where("id")->equals($id)->execute()->fetch(PDO::FETCH_ASSOC);
if (!$user) {
throw new \Exception("User not found");
}
if (!empty($name)) {
$user['name'] = $name;
}
if (!empty($email)) {
$user['email'] = $email;
}
if (!empty($password)) {
if (strlen($password) < 6) {
throw new \Exception("Password must be at least 6 characters long");
}
if (!empty($confirmPassword) && $password != $confirmPassword) {
throw new \Exception("Passwords don't match");
}
$user['password'] = password_hash($password, PASSWORD_BCRYPT);
}
if (!empty($role)) {
$user['role'] = $role;
}
if (in_array($status, [0, 1, 2])) {
$user['status'] = $status;
}
$user['update_date'] = date('Y-m-d H:i:s');
DB::table("users")->update($user)->where("id")->equals($id)->execute();
return $user;
}
function add($name, $email, $password, $confirmPassword, $role, $status, $sendConfirmation)
{
if (!$this->module->getRoles()->isAllowed(1)) {
throw new \Exception('Permission denied');
}
if (empty($name)) {
throw new \Exception('Missing required parameter: name');
}
if (empty($email)) {
throw new \Exception('Missing required parameter: email');
}
if (empty($role)) {
throw new \Exception('Missing required parameter: role');
}
if (!in_array($status, [0, 1, 2])) {
throw new \Exception('Missing required parameter: status');
}
if (empty($password)) {
throw new \Exception('Missing required parameter: password');
}
if (strlen($password ?? "") < 6) {
throw new \Exception('Passwords must be at least 6 characters long');
}
if ($password != $confirmPassword) {
throw new \Exception("Passwords don't match");
}
if (DB::table("users")->select("id")->where("email")->equals($email)->execute()->rowCount() > 0) {
throw new \Exception("Email already exists");
}
if (
DB::table("users")->insert([
"name" => $name,
"email" => $email,
"password" => password_hash($password, PASSWORD_BCRYPT),
"role" => $role,
"status" => $status
])->execute() && DB::instance()->lastInsertId() > 0
) {
if ($sendConfirmation) {
$this->module->sendConfirmation($email);
}
return [
"id" => DB::instance()->lastInsertId(),
"name" => $name,
"email" => $email,
"password" => password_hash($password, PASSWORD_BCRYPT),
"role" => $role,
"status" => $status
];
}
throw new \Exception("Failed to add user");
}
public function fetch($columns = ["id", "name", "email", "role", "status", "post_date", "update_date"], $where = ["id" => 1])
{
$SQL = "SELECT " . implode(",", array_map(function ($item) {
return "`" . $item . "`";
}, $columns)) . " FROM `users` WHERE " . implode(" AND ", array_map(function ($item) {
return "`" . $item . "` = ?";
}, array_keys($where)));
$stmt = DB::instance()->prepare($SQL);
$stmt->execute(array_values($where));
$user = $stmt->fetch(PDO::FETCH_ASSOC);
if ($user && isset($user['email'])) {
$user['avatar'] = "https://gravatar.com/avatar/" . md5(strtolower(trim($user['email']))) . "?s=200";
if (file_exists(__DIR__ . "/avatars/" . $user['id'] . ".webp")) {
$user['avatar'] = $this->fullPathToRelativePath(__DIR__ . "/avatars/" . $user['id'] . ".webp", $_ENV['__MP_CWD__']);
}
$user['logedin'] = Box::module("Auth")->isLogedin($user['id']);
}
return $user;
}
public function fetchAll(
$columns = ["id", "name", "email", "role", "status", "post_date", "update_date"],
$page = null,
$limit = null,
$search = null
) {
// Validate $limit to prevent division by zero
if ($limit <= 0) {
// Fetch all results if $limit is zero or negative
$limit = null;
}
// Construct the main SQL query
$mainSQL = "SELECT " . ($columns ? implode(",", $columns) : "*") . " FROM users"
. ($search ? " WHERE name LIKE '%{$search}%' OR email LIKE '%{$search}%' OR role LIKE '%{$search}%' " : "")
. " ORDER BY id DESC";
// Construct the count SQL query
$countSQL = "SELECT COUNT(*) AS total FROM users"
. ($search ? " WHERE name LIKE '%{$search}%' OR email LIKE '%{$search}%' OR role LIKE '%{$search}%' " : "");
// Execute the count query to get total results
$totalCount = DB::instance()->query($countSQL)->fetchColumn();
if (!$totalCount) {
return [
'users' => [],
'totalPages' => 0,
'totalResults' => 0
];
}
// Calculate total pages based on total results and limit per page
$totalPages = ($limit > 0) ? ceil($totalCount / $limit) : 0;
// Construct the main SQL query with pagination
$offset = ($page - 1) * $limit;
$SQL = $mainSQL . ($limit ? " LIMIT $offset, $limit" : "");
// Execute the main query to fetch users
$users = DB::instance()->query($SQL)->fetchAll(PDO::FETCH_ASSOC);
foreach ($users as $key => $user) {
$users[$key]['avatar'] = "https://gravatar.com/avatar/" . md5(strtolower(trim($user['email']))) . "?s=200";
if (file_exists(__DIR__ . "/avatars/" . $users[$key]['id'] . ".webp")) {
$users[$key]['avatar'] = $this->fullPathToRelativePath(__DIR__ . "/avatars/" . $users[$key]['id'] . ".webp", $_ENV['__MP_CWD__']);
}
$users[$key]['logedin'] = Box::module("Auth")->isLogedin($user['id']);
}
// Return the users, total pages, and total results
return [
'users' => $users,
'totalPages' => $totalPages,
'totalResults' => $totalCount
];
}
function sendConfirmation($email)
{
error_log("Sending confirmation email to $email");
}
function delete($id)
{
if (!$this->module->getRoles()->isAllowed(1)) {
throw new \Exception('Permission denied');
}
DB::table("users")->delete()->where("id")->equals($id)->execute();
}
function uploadAvatar()
{
if (!$this->module->getConfig()->get("profile.change_avatar")) {
throw new \Exception('Change avatar is disabled');
}
$user = Box::module("Auth")->Session->getUser();
if (!$user || !$user['id']) {
throw new \Exception("Unauthorized");
}
$max_size = 1024 * 1024; // 1MB
$allowed_ext = ["jpg", "jpeg", "png", "svg", "webp"];
$file = $_FILES['avatar'];
if (!$file) {
throw new \Exception("File not found");
}
$file_name = $file['name'];
$file_tmp_name = $file['tmp_name'];
$file_size = $file['size'];
$file_error = $file['error'];
if ($file_error > 0) {
throw new \Exception("Error uploading file");
}
if ($file_size > $max_size) {
throw new \Exception("File is too big, please choose file under 1MB");
}
$file_ext = explode(".", $file_name);
$file_ext = strtolower(end($file_ext));
if (!in_array($file_ext, $allowed_ext)) {
throw new \Exception("File type not allowed");
}
$file_new_name = $user['id'] . ".webp";
if (!file_exists(__DIR__ . "/avatars/")) {
mkdir(__DIR__ . "/avatars/", 0777, true); // Create folder if it doesn't exist
}
move_uploaded_file($file_tmp_name, __DIR__ . "/avatars/" . $file_new_name);
return $this->fullPathToRelativePath(__DIR__ . "/avatars/" . $file_new_name, $_ENV['__MP_CWD__']) . "?t=" . time();
}
function deleteAvatar()
{
if (!$this->module->getConfig()->get("profile.change_avatar")) {
throw new \Exception('Change avatar is disabled');
}
$user = Box::module("Auth")->Session->getUser();
if (!$user || !$user['id']) {
throw new \Exception("Unauthorized");
}
if (file_exists(__DIR__ . "/avatars/" . $user['id'] . ".webp")) {
unlink(__DIR__ . "/avatars/" . $user['id'] . ".webp");
}
return "https://gravatar.com/avatar/" . md5(strtolower(trim($user['email']))) . "?s=200";
}
function fullPathToRelativePath($fullPath, $basePath)
{
// Normalize directory separators and remove trailing slashes
$fullPath = rtrim(str_replace('\\', '/', $fullPath), '/');
$basePath = rtrim(str_replace('\\', '/', $basePath), '/');
// Construct the relative path
$relativePath = str_replace($basePath, '', $fullPath);
// Return the relative path
return $relativePath;
}
}