forked from elabftw/elabftw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin-exec.php
503 lines (482 loc) · 18.3 KB
/
admin-exec.php
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
<?php
/********************************************************************************
* *
* Copyright 2012 Nicolas CARPi (nicolas.carpi@gmail.com) *
* http://www.elabftw.net/ *
* *
********************************************************************************/
/********************************************************************************
* This file is part of eLabFTW. *
* *
* eLabFTW is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Affero General Public License as *
* published by the Free Software Foundation, either version 3 of *
* the License, or (at your option) any later version. *
* *
* eLabFTW is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied *
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR *
* PURPOSE. See the GNU Affero General Public License for more details. *
* *
* You should have received a copy of the GNU Affero General Public *
* License along with eLabFTW. If not, see <http://www.gnu.org/licenses/>. *
* *
********************************************************************************/
/* admin-exec.php - for administration of the elab */
require_once 'inc/common.php';
// only admin can use this
if ($_SESSION['is_admin'] != 1) {
die('You are not admin !');
}
// formkey stuff
require_once 'lib/classes/formkey.class.php';
$formKey = new formKey();
// for success messages
$infos_arr = array();
// for error messages
$errors_arr = array();
$errflag = false;
// VALIDATE USERS
if ($_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_POST['validate'])) {
// sql to validate users
$sql = "UPDATE users SET validated = 1 WHERE userid = :userid";
$req = $pdo->prepare($sql);
// check we only have int in validate array
if (!filter_var_array($_POST['validate'], FILTER_VALIDATE_INT)) {
die();
}
// sql to get email of the user
$sql_email = "SELECT email FROM users WHERE userid = :userid";
$req_email = $pdo->prepare($sql_email);
// we loop the validate array
foreach ($_POST['validate'] as $user) {
// bind parameters of the user
$req_email->bindParam(':userid', $user, PDO::PARAM_INT);
$req->bindParam(':userid', $user, PDO::PARAM_INT);
// validate the user
$req->execute();
$infos_arr[] = 'Validated user with user ID : '.$user;
// get email
$req_email->execute();
$user = $req_email->fetch();
// now let's get the URL so we can have a nice link in the email
$url = 'https://'.$_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].$_SERVER['PHP_SELF'];
$url = str_replace('admin-exec.php', 'login.php', $url);
// we send an email to each validated new user
require_once 'lib/swift_required.php';
// Create the message
$message = Swift_Message::newInstance()
// Give the message a subject
->setSubject('[eLabFTW] New user registred')
// Set the From address with an associative array
->setFrom(array('elabftw.net@gmail.com' => 'eLabFTW'))
// Set the To addresses with an associative array
->setTo(array($user['email'] => 'Your account has been activated.'))
// Give it a body
->setBody(
'Hi,
Your account on eLabFTW has been activated. You can now login:
'.$url.'
Thanks for using eLabFTW :)
~~
Email sent by eLabFTW
http://www.elabftw.net
Free open-source Lab Manager'
);
$transport = Swift_SmtpTransport::newInstance(
get_config('smtp_address'),
get_config('smtp_port'),
get_config('smtp_encryption')
)
->setUsername(get_config('smtp_username'))
->setPassword(get_config('smtp_password'));
$mailer = Swift_Mailer::newInstance($transport);
// now we try to send the email
try {
$mailer->send($message);
} catch (Exception $e) {
// log the error
$logline = date('Y-m-d H:i:s') . ' - ' . $e->getMessage() . PHP_EOL;
file_put_contents('errors.log', $logline, FILE_APPEND);
$errflag = true;
}
if ($errflag) {
$msg_arr[] = 'There was a problem sending the email. Error was logged.';
$_SESSION['errors'] = $msg_arr;
header('location: admin.php');
exit();
}
}
$_SESSION['infos'] = $infos_arr;
header('Location: admin.php');
exit();
}
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['lab_name'])) {
// MAIN CONFIGURATION FORM
if (isset($_POST['lab_name'])) {
$lab_name = filter_var($_POST['lab_name'], FILTER_SANITIZE_STRING);
}
if ($_POST['admin_validate'] == 1) {
$admin_validate = 1;
} else {
$admin_validate = 0;
}
if ($_POST['deletable_xp'] == 1) {
$deletable_xp = 1;
} else {
$deletable_xp = 0;
}
if ($_POST['debug'] == 1) {
$debug = 1;
} else {
$debug = 0;
}
if (isset($_POST['link_name'])) {
$link_name = filter_var($_POST['link_name'], FILTER_SANITIZE_STRING);
}
if (isset($_POST['link_href'])) {
$link_href = filter_var($_POST['link_href'], FILTER_SANITIZE_STRING);
}
if (isset($_POST['path'])) {
$path = filter_var($_POST['path'], FILTER_SANITIZE_STRING);
}
if (isset($_POST['proxy'])) {
$proxy = filter_var($_POST['proxy'], FILTER_SANITIZE_STRING);
}
if (isset($_POST['smtp_address'])) {
$smtp_address = filter_var($_POST['smtp_address'], FILTER_SANITIZE_STRING);
}
if (isset($_POST['smtp_encryption'])) {
$smtp_encryption = filter_var($_POST['smtp_encryption'], FILTER_SANITIZE_STRING);
}
if (isset($_POST['smtp_port']) && is_pos_int($_POST['smtp_port'])) {
$smtp_port = $_POST['smtp_port'];
}
if (isset($_POST['smtp_username'])) {
$smtp_username = filter_var($_POST['smtp_username'], FILTER_SANITIZE_STRING);
}
if (isset($_POST['smtp_password'])) {
$smtp_password = filter_var($_POST['smtp_password'], FILTER_SANITIZE_STRING);
}
if (isset($_POST['login_tries'])) {
$login_tries = filter_var($_POST['login_tries'], FILTER_SANITIZE_STRING);
}
if (isset($_POST['ban_time'])) {
$ban_time = filter_var($_POST['ban_time'], FILTER_SANITIZE_STRING);
}
// build request array
$updates = array(
'lab_name' => $lab_name,
'admin_validate' => $admin_validate,
'deletable_xp' => $deletable_xp,
'debug' => $debug,
'link_name' => $link_name,
'link_href' => $link_href,
'path' => $path,
'proxy' => $proxy,
'smtp_address' => $smtp_address,
'smtp_encryption' => $smtp_encryption,
'smtp_port' => $smtp_port,
'smtp_username' => $smtp_username,
'smtp_password' => $smtp_password,
'login_tries' => $login_tries,
'ban_time' => $ban_time
);
$values = array();
foreach ($updates as $name => $value) {
$sql = "UPDATE config SET conf_value = '".$value."' WHERE conf_name = '".$name."';";
$req = $pdo->prepare($sql);
$result = $req->execute();
}
if ($result) {
$infos_arr[] = 'Configuration updated successfully.';
$_SESSION['infos'] = $infos_arr;
header('Location: admin.php');
exit();
} else {
$errors_arr[] = 'There was a problem in the SQL request. Report a bug !';
$_SESSION['errors'] = $errors_arr;
header('Location: admin.php');
}
}
// EDIT USER
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['userid'])) {
if (!is_pos_int($_POST['userid'])) {
$msg_arr[] = 'Userid is not valid.';
$errflag = true;
}
if ($errflag) {
$_SESSION['errors'] = $msg_arr;
header("location: admin.php");
die();
}
$userid = $_POST['userid'];
// Put everything lowercase and first letter uppercase
$firstname = ucwords(strtolower(filter_var($_POST['firstname'], FILTER_SANITIZE_STRING)));
// Lastname in uppercase
$lastname = strtoupper(filter_var($_POST['lastname'], FILTER_SANITIZE_STRING));
$username = filter_var($_POST['username'], FILTER_SANITIZE_STRING);
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
if ($_POST['is_admin'] == 1) {
$is_admin = 1;
} else {
$is_admin = 0;
}
if ($_POST['can_lock'] == 1) {
$can_lock = 1;
} else {
$can_lock = 0;
}
if ($_POST['validated'] == 1) {
$validated = 1;
} else {
$validated = 0;
}
// reset password
if (isset($_POST['new_password']) && !empty($_POST['new_password']) && isset($_POST['confirm_new_password'])) {
// check if passwords match
if ($_POST['new_password'] == $_POST['confirm_new_password']) {
// Good to go
// Create salt
$salt = hash("sha512", uniqid(rand(), true));
// Create hash
$passwordHash = hash("sha512", $salt.$_POST['new_password']);
$sql = "UPDATE users SET password = :password, salt = :salt WHERE userid = :userid";
$req = $pdo->prepare($sql);
$result = $req->execute(array(
'userid' => $userid,
'password' => $passwordHash,
'salt' => $salt
));
if ($result) {
$infos_arr[] = 'User password updated successfully.';
$_SESSION['infos'] = $infos_arr;
} else {
$errors_arr[] = 'There was a problem in the SQL update of the password.';
$_SESSION['errors'] = $errors_arr;
}
} else { // passwords do not match
$errors_arr[] = 'Passwords do not match !';
$_SESSION['errors'] = $errors_arr;
}
}
$sql = "UPDATE users SET
firstname = :firstname,
lastname = :lastname,
username = :username,
email = :email,
is_admin = :is_admin,
can_lock = :can_lock,
validated = :validated
WHERE userid = :userid";
$req = $pdo->prepare($sql);
$result = $req->execute(array(
'firstname' => $firstname,
'lastname' => $lastname,
'username' => $username,
'email' => $email,
'is_admin' => $is_admin,
'can_lock' => $can_lock,
'validated' => $validated,
'userid' => $userid
));
if ($result) {
if (empty($errors_arr)) {
$infos_arr[] = 'User infos updated successfully.';
$_SESSION['infos'] = $infos_arr;
header('Location: admin.php');
exit();
} else {
header('Location: admin.php');
}
} else { //sql fail
$errors_arr[] = 'There was a problem in the SQL request. Report a bug !';
$_SESSION['errors'] = $errors_arr;
header('Location: admin.php');
exit();
}
}
// STATUS
if (isset($_POST['status_name']) && is_pos_int($_POST['status_id'])) {
$status_id = $_POST['status_id'];
$status_name = filter_var($_POST['status_name'], FILTER_SANITIZE_STRING);
// we remove the # of the hexacode and sanitize string
$status_color = filter_var(substr($_POST['status_color'], 1, 6), FILTER_SANITIZE_STRING);
if (isset($_POST['status_is_default']) && $_POST['status_is_default'] === 'on') {
$status_is_default = true;
// if we set true to status_is_default somewhere, it's best to remove all other default
// so we won't have two default status
$sql = "UPDATE status SET
is_default = false";
$req = $pdo->prepare($sql);
$req->execute();
} else {
$status_is_default = false;
}
// now we update the status
$sql = "UPDATE status SET
name = :name,
color = :color,
is_default = :is_default
WHERE id = :id";
$req = $pdo->prepare($sql);
$result = $req->execute(array(
'name' => $status_name,
'color' => $status_color,
'is_default' => $status_is_default,
'id' => $status_id
));
if ($result) {
$infos_arr[] = 'Status updated successfully.';
$_SESSION['infos'] = $infos_arr;
header('Location: admin.php');
exit();
} else { //sql fail
$infos_arr[] = 'There was a problem in the SQL request. Report a bug !';
$_SESSION['errors'] = $infos_arr;
header('Location: admin.php');
exit();
}
}
// add new status
if (isset($_POST['new_status_name'])) {
$status_name = filter_var($_POST['new_status_name'], FILTER_SANITIZE_STRING);
// we remove the # of the hexacode and sanitize string
$status_color = filter_var(substr($_POST['new_status_color'], 1, 6), FILTER_SANITIZE_STRING);
$sql = "INSERT INTO status(name, color) VALUES(:name, :color)";
$req = $pdo->prepare($sql);
$result = $req->execute(array(
'name' => $status_name,
'color' => $status_color
));
if ($result) {
$infos_arr[] = 'New status added successfully.';
$_SESSION['infos'] = $infos_arr;
header('Location: admin.php');
exit();
} else { //sql fail
$infos_arr[] = 'There was a problem in the SQL request. Report a bug !';
$_SESSION['errors'] = $infos_arr;
header('Location: admin.php');
exit();
}
}
// ITEMS TYPES
if (isset($_POST['item_type_name']) && is_pos_int($_POST['item_type_id'])) {
$item_type_id = $_POST['item_type_id'];
$item_type_name = filter_var($_POST['item_type_name'], FILTER_SANITIZE_STRING);
// we remove the # of the hexacode and sanitize string
$item_type_bgcolor = filter_var(substr($_POST['item_type_bgcolor'], 1, 6), FILTER_SANITIZE_STRING);
$item_type_template = check_body($_POST['item_type_template']);
$sql = "UPDATE items_types SET
name = :name,
bgcolor = :bgcolor,
template = :template
WHERE id = :id";
$req = $pdo->prepare($sql);
$result = $req->execute(array(
'name' => $item_type_name,
'bgcolor' => $item_type_bgcolor,
'template' => $item_type_template,
'id' => $item_type_id
));
if ($result) {
$infos_arr[] = 'New item category updated successfully.';
$_SESSION['infos'] = $infos_arr;
header('Location: admin.php#items_types');
exit();
} else { //sql fail
$infos_arr[] = 'There was a problem in the SQL request. Report a bug !';
$_SESSION['errors'] = $infos_arr;
header('Location: admin.php');
exit();
}
}
// ADD NEW ITEM TYPE
if (isset($_POST['new_item_type']) && is_pos_int($_POST['new_item_type'])) {
$item_type_name = filter_var($_POST['new_item_type_name'], FILTER_SANITIZE_STRING);
if (strlen($item_type_name) < 1) {
$infos_arr[] = 'You need to put a title !';
$_SESSION['errors'] = $infos_arr;
header('Location: admin.php');
exit();
}
// we remove the # of the hexacode and sanitize string
$item_type_bgcolor = filter_var(substr($_POST['new_item_type_bgcolor'], 1, 6), FILTER_SANITIZE_STRING);
$item_type_template = check_body($_POST['new_item_type_template']);
$sql = "INSERT INTO items_types(name, bgcolor, template) VALUES(:name, :bgcolor, :template)";
$req = $pdo->prepare($sql);
$result = $req->execute(array(
'name' => $item_type_name,
'bgcolor' => $item_type_bgcolor,
'template' => $item_type_template
));
if ($result) {
$infos_arr[] = 'New item category added successfully.';
$_SESSION['infos'] = $infos_arr;
header('Location: admin.php#items_types');
exit();
} else { //sql fail
$infos_arr[] = 'There was a problem in the SQL request. Report a bug !';
$_SESSION['errors'] = $infos_arr;
header('Location: admin.php');
exit();
}
}
// DELETE USER
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['delete_user'])) {
// Check the form_key
if (!isset($_POST['form_key']) || !$formKey->validate()) {
// form key is invalid
$msg_arr[] = 'The form key is invalid !';
$errflag = true;
}
if (filter_var($_POST['delete_user'], FILTER_VALIDATE_EMAIL)) {
$email = $_POST['delete_user'];
} else {
$msg_arr[] = 'Email is not valid';
$errflag = true;
}
if ($errflag) {
$_SESSION['errors'] = $msg_arr;
header("location: admin.php");
die();
}
// look which user has this email address
$sql = "SELECT userid FROM users WHERE email LIKE :email";
$req = $pdo->prepare($sql);
$req->execute(array(
'email' => $email
));
$user = $req->fetch();
$userid = $user['userid'];
// DELETE USER
$sql = "DELETE FROM users WHERE userid = ".$userid;
$req = $pdo->prepare($sql);
$req->execute();
$sql = "DELETE FROM experiments_tags WHERE userid = ".$userid;
$req = $pdo->prepare($sql);
$req->execute();
$sql = "DELETE FROM experiments WHERE userid = ".$userid;
$req = $pdo->prepare($sql);
$req->execute();
// get all filenames
$sql = "SELECT long_name FROM uploads WHERE userid = :userid AND type = :type";
$req = $pdo->prepare($sql);
$req->execute(array(
'userid' => $userid,
'type' => 'exp'
));
while ($uploads = $req->fetch()) {
// Delete file
$filepath = 'uploads/'.$uploads['long_name'];
unlink($filepath);
}
$sql = "DELETE FROM uploads WHERE userid = ".$userid;
$req = $pdo->prepare($sql);
$req->execute();
$infos_arr[] = 'Everything was purged successfully.';
$_SESSION['infos'] = $infos_arr;
header('Location: admin.php');
exit();
}