-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.php
267 lines (221 loc) · 9.1 KB
/
server.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
<?php
session_start();
//initializing variables
$username = "";
$email = "";
$errors = array();
//connect to the database
$db = mysqli_connect('localhost', 'XXXXX', 'XXXXXXX', 'registration');
if (isset($_POST['reg_user'])) {
// receive all input values from the form
$username = mysqli_real_escape_string($db, $_POST['username']);
$email = mysqli_real_escape_string($db, $_POST['email']);
$password_1 = mysqli_real_escape_string($db, $_POST['password_1']);
$password_2 = mysqli_real_escape_string($db, $_POST['password_2']);
// form validation: ensure that the form is correctly filled ...
// by adding (array_push()) corresponding error unto $errors array
if (empty($username)) { array_push($errors, "Username is required"); }
if (empty($email)) { array_push($errors, "Email is required"); }
if (empty($password_1)) { array_push($errors, "Password is required"); }
if ($password_1 != $password_2) {
array_push($errors, "The two passwords do not match");
}
//validate email address
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
array_push($errors, "Invalid email provided");
}
//check password for complexity
if(strlen($password_1) < 8){
array_push($errors, "Password too short");
}
if (!preg_match("#[0-9]+#", $password_1)) {
array_push($errors, "Password must contain at least 1 number!");
}
if (!preg_match("#[a-zA-z]+#", $password_1)) {
array_push($errors, "Password must include at least one letter!");
}
// first check the database to make sure
// a user does not already exist with the same username and/or email
$user_check_query = "SELECT * FROM users WHERE username='$username' OR email='$email' LIMIT 1";
$result = mysqli_query($db, $user_check_query);
$user = mysqli_fetch_assoc($result);
if ($user) { // if user exists
if ($user['username'] === $username) {
array_push($errors, "Username already exists");
}
if ($user['email'] === $email) {
array_push($errors, "email already exists");
}
}
// Finally, register user if there are no errors in the form
if (count($errors) == 0) {
$password = password_hash($password_1, PASSWORD_BCRYPT);//encrypt the password before saving in the database
$query = "INSERT INTO users (`username`, `email`, `password`) VALUES('$username', '$email', '$password')";
mysqli_query($db, $query);
$_SESSION['username'] = $username;
$_SESSION['success'] = "You are now logged in";
header('location: index.php');
}
}
// ...
// LOGIN USER
if (isset($_POST['login_user'])) {
$username = mysqli_real_escape_string($db, $_POST['username']);
$password = mysqli_real_escape_string($db, $_POST['password']);
if (empty($username)) {
array_push($errors, "Username is required");
}
if (empty($password)) {
array_push($errors, "Password is required");
}
if (count($errors) == 0) {
$sql = "SELECT * FROM users WHERE username=?";
if($stmt = mysqli_prepare($db,$sql)){
mysqli_stmt_bind_param($stmt, "s", $username);
if (mysqli_stmt_execute($stmt)){
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_array(MYSQLI_ASSOC);
$hash = $row['password'];
if (password_verify($password, $hash)) {
$_SESSION['username'] = $username;
$_SESSION['success'] = "You are now logged in";
$_SESSION['user_id'] = $row['id'];
header('location: index.php');
}
else {
array_push($errors, "Invalid Username/Password");
}
}
else {
array_push($errors, "Invalid Username/Password");
}
}
else {
array_push($errors, "Wrong username/password combination");
}
}
}
// Handle a request to change the password for a User
if (isset($_POST['change-password']))
{
$username = mysqli_real_escape_string($db, $_SESSION['username']);
$currentpassword = mysqli_real_escape_string($db, $_POST['current-pass']);
$confirmpassword = mysqli_real_escape_string($db, $_POST['confirm-pass']);
$newpassword = mysqli_real_escape_string($db, $_POST['new-pass']);
if ($newpassword != $confirmpassword || empty($newpassword))
{
$_SESSION['user-acc-msg'] = $_SESSION['user-acc-msg'] . "\n Passwords do not match or are blank";
array_push($errors, "Passwords do not match or are blank");
header('location: user.php');
exit;
}
$sql = "SELECT * FROM users WHERE username=?";
if($stmt = mysqli_prepare($db,$sql)){
mysqli_stmt_bind_param($stmt, "s", $username);
if (mysqli_stmt_execute($stmt)){
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_array(MYSQLI_ASSOC);
$hash = $row['password'];
if (password_verify($currentpassword, $hash)) {
// $_SESSION['username'] = $username;
// $_SESSION['user_id'] = $row['id'];
$password = password_hash($newpassword, PASSWORD_BCRYPT);//encrypt the password before saving in the database
$update_sql = "UPDATE `users` SET `password`= ? WHERE `id` = ?";
$_stmt = mysqli_prepare($db,$update_sql);
mysqli_stmt_bind_param($_stmt, "si", $password, $row['id']);
$_stmt ->execute();
$_SESSION['user-acc-msg'] = "Password Successfully Updated.";
header('location: user.php');
exit;
}
else {
$_SESSION['user-acc-msg'] = $_SESSION['user-acc-msg'] . "<br> Incorrect Password Provided.";
header('location: user.php');
exit;
}
}
else {
array_push($errors, "Invalid Username/Password");
header('location: user.php');
exit;
}
}
}
// Handle request to change email for a user account
if (isset($_POST['change-email']))
{
$id = mysqli_real_escape_string($db, $_SESSION['user_id']);
$currentpassword = mysqli_real_escape_string($db, $_POST['current-pass']);
$confirmemail = mysqli_real_escape_string($db, $_POST['confirm-email']);
$newemail = mysqli_real_escape_string($db, $_POST['new-email']);
$sql = $sql = "SELECT * FROM users WHERE id = ?";
if($stmt = mysqli_prepare($db,$sql))
{
mysqli_stmt_bind_param($stmt, "i", $id);
if (mysqli_stmt_execute($stmt)){
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_array(MYSQLI_ASSOC);
$hash = $row['password'];
if (password_verify($currentpassword, $hash)) {
//check that two emails match
if ($newemail != $confirmemail)
{
$_SESSION['user-acc-msg'] = "Email addresses do not match.";
header('location: user.php');
exit;
}
//validate email address
if (!filter_var($newemail, FILTER_VALIDATE_EMAIL)) {
$_SESSION['user-acc-msg'] = "*Invalid Email Provided.";
header('location: user.php');
exit;
}
//check that email is not already registered
// first check the database to make sure
// a user does not already exist with the same username and/or email
$user_check_query = "SELECT * FROM users WHERE email='$newemail' LIMIT 1";
$result = mysqli_query($db, $user_check_query);
$user = mysqli_fetch_assoc($result);
if ($user['email'] === $newemail)
{
$_SESSION['user-acc-msg'] = "Email already in use.";
header('location: user.php');
exit;
}
//Update Email Address for Session User
$update_sql = "UPDATE `users` SET `email`= ? WHERE `id` = ?";
$_stmt = mysqli_prepare($db,$update_sql);
mysqli_stmt_bind_param($_stmt, "si", $newemail, $_SESSION['user_id']);
$_stmt ->execute();
$_SESSION['user-acc-msg'] = "Email Successfully Updated";
header('location: user.php');
exit;
}
else
{
$_SESSION['user-acc-msg'] = "Incorrect Password Provided.";
header('location: user.php');
exit;
}
}
}
}
//Handle request to delete portrait files a user has uploaded
if (isset($_POST['delete-marked-files']))
{
$imgcount = 0;
//pull array of image paths from form post
$imgs = $_POST['img'];
foreach ($imgs as $img) {
//unlink each file in imgs array
unlink ($img);
$imgcount = $imgcount + 1;
}
$_SESSION['user-acc-msg'] = $imgcount . " file(s) successfully deleted.";
header('location: user.php');
// print_r($imgs);
}
?>