-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmanage_user.php
More file actions
94 lines (76 loc) · 2.42 KB
/
manage_user.php
File metadata and controls
94 lines (76 loc) · 2.42 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
<?php
include('config.php');
logincheck();
use Carbon\Carbon;
$message = [];
$title = "Create user";
$user = new User;
$categories = Category::all();
if (isset($_GET['id'])) {
$title = "Edit user";
$user = User::find($_GET['id']);
}
function parseExpDate($expdate) {
$expdate = trim($expdate);
if ($expdate == '0000-00-00' || empty($expdate)) {
return '0000-00-00';
}
try {
return Carbon::createFromFormat('m/d/Y', $expdate);
} catch (InvalidArgumentException $e) {
}
try {
return Carbon::createFromFormat('Y-m-d', $expdate);
} catch (InvalidArgumentException $e) {
}
return '0000-00-00';
}
if (isset($_POST['submit'])) {
$user->username = $_POST['username'];
$user->password = $_POST['password'];
$user->exp_date = $_POST['expdate'];
$user->max_connections = $_POST['limit'];
if (isset($_POST['expdate'])) {
$user->exp_date = parseExpDate($_POST['expdate']);
}
$user->active = 0;
if (isset($_POST['active'])) {
$user->active = 1;
}
if (empty($_POST['username'])) {
$message['type'] = "error";
$message['message'] = "username field is empty";
} else if (empty($_POST['password'])) {
$message['type'] = "error";
$message['message'] = "password is empty";
} else if (empty($_POST['category'])) {
$message['type'] = "error";
$message['message'] = "Select one category";
} else {
if (isset($_GET['id'])) {
$message['type'] = "success";
$message['message'] = "User saved";
$user->save();
$user->categories()->sync($_POST['category']);
} else {
$exists = User::where('username', '=', $_POST['username'])->get();
if (count($exists) > 0) {
$message['type'] = "error";
$message['message'] = "Username already in use";
} else {
$message['type'] = "success";
$message['message'] = "User created";
$user->save();
$user->categories()->sync($_POST['category']);
redirect("manage_user.php?id=" . $user->id, 2000);
}
}
}
}
echo $template->view()
->make('manage_user')
->with('user', $user)
->with('categories', $categories)
->with('message', $message)
->with('title', $title)
->render();