-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreset.php
210 lines (180 loc) · 7.37 KB
/
reset.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
<!DOCTYPE html>
<head>
<title>Reset Password</title>
<link rel="stylesheet" href="css/styles.css">
</head>
<?php include "connection.php"; ?>
<?php
session_start();
$errCtr = 0;
$errMsgs = [];
// Performs validation when the form is submitted
if (isset($_POST['reset'])) { // checks if the form has been submitted
// Stores what was in 'uname' (the username prompt) in variable if it's valid
$username = validUsername($_REQUEST['uname']);
// Stores what was in 'psw1' and 'psw2' (the password prompts) in variables if it's valid
$password = validPassword($_REQUEST['psw1'], $_REQUEST['psw2']);
}
?>
<body>
<div id="wrapper3">
<div id="container">
<?php
resetted();
/**
* Used to check if a username that was entered is valid
*
* @param String $uname represents the username that was entered
*
* @return String returns either null or a valid username
*/
function validUsername(String $uname)
{
global $errCtr, $errMsgs;
// Represents length (not including any whitespace characters such as spaces)
$lenTrimUname = strlen(str_replace(" ", "", $uname));
if (empty(trim($uname))) {
$uname = null;
$errMsgs[] = "\t<p>No username was entered. Note: Any white space characters that were entered were trimmed</p>";
$errCtr++;
} elseif ($lenTrimUname < 2) {
$uname = null;
$errMsgs[] = "\t<p>Invalid username, " . str_replace(" ", "", $uname) . " should be at least 2 characters long and all whitespace characters will be removed</p>";
$errCtr++;
} else {
// Used to remove all whitespace characters
$uname = str_replace(" ", "", $uname);
}
return $uname;
}
/**
* Used to check if a password that was entered is valid
*
* @param String $psw1 represents the password that was entered
* @param String $psw2 represents the password that was re-entered
*
* @return String returns either null or a valid password
*/
function validPassword(String $psw1, String $psw2)
{
global $errCtr, $errMsgs;
// Represents the length
$pattern = "/(?=.*[A-Z|a-z])(?=.*\d)(?=.*[\W|_])[A-Za-z\d\W_]{8,}$/";
$format1 = preg_match($pattern, $psw1) && preg_match("/^\S{8,}$/", $psw1);
$format2 = preg_match($pattern, $psw2) && preg_match("/^\S{8,}$/", $psw2);
if (empty(trim($psw1)) || empty(trim($psw2))) {
$errMsgs[] = "\t<p>Password should be entered and re-entered</p>";
$errCtr++;
$psw1 = null;
} else {
if ($psw1 != $psw2) {
$errMsgs[] = "\t<p>A different password was entered</p>";
$errCtr++;
$psw1 = null;
} elseif (!$format1 || !$format2) {
$errMsgs[] = "\t<p>Invalid password, $psw1 should be at least 8 characters long with no whitespace characters and contain at least one letter, one number, and one special chatacter</p>";
$errCtr++;
$psw1 = null;
}
}
return $psw1;
}
/**
* Checks if the user's account should be created
*
* @return void
*/
function resetted()
{
global $errCtr;
if ($errCtr == 0) {
$resetted = resetPassword();
if ($resetted) {
header("Location: http://localhost/PHPatriots/login.php");
} else {
displayErrors();
}
} else {
displayErrors();
}
}
/**
* Prints error messages and error page
*
* @return void
*/
function displayErrors()
{
global $errMsgs;
echo "\t<h1>Error</h1>\n";
reset($errMsgs);
foreach ($errMsgs as $errMsg) {
echo $errMsg;
}
echo "\t<a href='reset.html'>Click here to try again</a>";
}
/**
* Resets users in the database
*
* @return bool
*/
function resetPassword()
{
global $username, $password, $con, $errMsgs, $errCtr;
try {
$encPassword = encPsw();
if (!password_verify($password, $encPassword)) {
if ($errCtr == 0) {
// Prepare and binds placeholders
$query = "UPDATE Accounts SET password = ? WHERE username = ?";
$prep_update = $con->prepare($query);
$prep_update->bind_param("ss", $psw, $uname);
// Sets parameters and execute prepare statement
$psw = password_hash($password, PASSWORD_DEFAULT);
$uname = $username;
$prep_update->execute();
// End prepare statement
$prep_update->close();
return true;
} else {
return false;
}
} else {
$errMsgs[] = "\t<p>A new password should be used</p>";
return false;
}
} catch (Exception $e) {
echo "<script>console.error('" . $e->getMessage() . "');</script>";
return false;
}
}
/**
* Gets the encrypted password
*
* @return String
*/
function encPsw()
{
global $con, $errCtr, $errMsgs, $username;
$found = false;
$query = "SELECT username, password FROM Accounts";
$result = $con->query($query);
while ($row = $result->fetch_assoc()) {
if ($username == $row["username"]) {
$encPsw = $row["password"];
$found = true;
break;
}
}
if (!$found) {
$errMsgs[] = "\t<p>Account was not found</p>";
$errCtr++;
return null;
} else {
return $encPsw;
}
}
?>
</div>
</div>
</body>