-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathreset.php
291 lines (248 loc) · 12.4 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
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
<?php
include "mail.php";
include "dbconfig.php";
session_start();
$resetToken = generateResetToken();
$reset = false;
$rUID = "";
// Destination page (destination.php)
$receivedData = isset($_GET['data']) ? urldecode($_GET['data']) : false; // Decode the URL parameter
if ($receivedData != false) {
// Prepare the SQL statement with a parameter placeholder
$sql = "SELECT `rUID`, `time` FROM `reset` WHERE `token` = ?";
$stmt = $conn->prepare($sql);
// Bind the parameter to the statement
$stmt->bind_param("s", $receivedData);
// Execute the query
$stmt->execute();
// Get the result
$result = $stmt->get_result();
// Check if the query returned any rows
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
// Access the values of UID and Username columns
$rUID = $row['rUID'];
$reset = true;
error_log("rUID :" . $rUID . " retrieved from :" . $receivedData);
// Store $rUID in the session variable
$_SESSION['rUID'] = $rUID;
}
error_log("after IUD: " . $rUID);
}
function sendPasswordResetEmail($email, $resetToken)
{
// Get the current page's protocol (http or https)
$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http';
// Get the host (localhost or domain name)
$host = $_SERVER['HTTP_HOST'];
// Get the path and script name (e.g., /project/example.php)
$path = $_SERVER['REQUEST_URI'];
// Construct the full URL
$rootDirectory = $protocol . '://' . $host . $path;
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
$to_email = $email;
$subject = "Reset Password";
$body = '
<p>Hello,</p>
<p>You have requested to reset your password. Please click on the link below to reset your password:</p>
<p><a href=" ' . $rootDirectory . '?data=' . $resetToken . '">Reset Password</a></p>
<p>If you did not request this, please ignore this email.</p>
<p>Regards,<br>Distributed Assignment</p>
';
$from_email = "[email protected]";
if (sendEmail($to_email, $subject, $body, $from_email)) {
return "Email sent successfully.";
} else {
return "Email sending failed.";
}
} else {
return "Email address is not valid.";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login</title>
<link rel="stylesheet" href="sent.css">
<link rel="stylesheet" href="sign.css">
</head>
<body>
<header>
<h1>Distributed systems Assignment Login Page</h1>
</header>
<div class="login-form">
<h2>Reset password</h2>
<form action="reset.php" method="post">
<?php if ($receivedData == 1) : ?>
<div class="form-group">
<label for="username">Enter your email adress</label>
<input type="text" id="email" name="email" required>
</div>
<button type="submit" name="send">send reset email</button>
<?php elseif ($reset) : ?>
<div class="form-group">
<label for="password">New Password</label>
<input type="password" id="password" name="password" required>
</div>
<div class="form-group">
<label for="cpassword">Confirm New Password</label>
<input type="password" id="cpassword" name="cpassword" required>
</div>
<button type="submit" name="change">reset password</button>
<?php elseif ($receivedData == 2) : ?>
<div class="form-group">
<label for="username">Enter your email adress</label>
<input type="text" id="email" name="email" required>
</div>
<button type="submit" name="send">send reset email</button>
<?php else : ?>
<div class="container">
<div class="alert">
<h2>Password has been reset successfully</h2>
<p>A confirmation email has been sent.</p>
<a href="login.php" class="btn">Return to Login</a>
</div>
</div>
<?php endif; ?>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (isset($_POST['send'])) {
$email = $_POST['email'];
// Prepare the SQL statement with a parameter placeholder
$sql = "SELECT `user_id`, `user_name` FROM user WHERE email = ?";
$stmt = $conn->prepare($sql);
// Bind the parameter to the statement
$stmt->bind_param("s", $email);
// Execute the query
$stmt->execute();
// Get the result
$result = $stmt->get_result();
// Check if the query returned any rows
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
// Access the values of UID and Username columns
$rUID = $row['user_id'];
$username = $row['user_name'];
// Values to insert or update
$token = $resetToken;
$time = date("Y-m-d H:i:s"); // Current date and time
// Check if the rUID exists in the reset table
$sql_check = "SELECT * FROM `reset` WHERE `rUID` = ?";
$stmt_check = $conn->prepare($sql_check);
$stmt_check->bind_param("s", $rUID);
$stmt_check->execute();
$result_check = $stmt_check->get_result();
if ($result_check->num_rows > 0) {
// rUID exists, update the record with current time and token
$sql_update = "UPDATE `reset` SET `token` = ?, `time` = ? WHERE `rUID` = ?";
$stmt_update = $conn->prepare($sql_update);
$stmt_update->bind_param("sss", $token, $time, $rUID);
$stmt_update->execute();
echo "Record updated successfully.";
} else {
// rUID does not exist, insert a new record with current time and token
$sql_insert = "INSERT INTO `reset`(`rUID`, `token`, `time`) VALUES (?, ?, ?)";
$stmt_insert = $conn->prepare($sql_insert);
$stmt_insert->bind_param("sss", $rUID, $token, $time);
$stmt_insert->execute();
echo "Record inserted successfully.";
}
echo sendPasswordResetEmail($email, $resetToken);
// Close the statements and connection
$stmt_check->close();
if (isset($stmt_update)) $stmt_update->close();
if (isset($stmt_insert)) $stmt_insert->close();
echo '<script>window.location.href = "sent.html"</script>';
} else {
echo "No account with this email.";
$receivedData = 2;
}
// Close statement and connection
$stmt->close();
$conn->close();
} elseif (isset($_POST['change'])) {
// change password button was clicked
error_log("submit 2.");
try {
$cpassword = $_POST['cpassword'];
$password = $_POST['password'];
$rUID = $_SESSION["rUID"];
if ($password == $cpassword) {
error_log("passwords match.");
// Hash the password
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
// SQL query with placeholders
$sql = "UPDATE `user` SET `password` = ? WHERE `user_id` = ?";
// Prepare and bind the statement
$stmt = $conn->prepare($sql);
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
$stmt->bind_param("ss", $hashedPassword, $rUID);
// Prepare the SQL statement with a parameter placeholder
$sql1 = "SELECT `email`, `user_name` FROM user WHERE user_id = ?";
$stmt1 = $conn->prepare($sql1);
// Bind the parameter to the statement
$stmt1->bind_param("s", $rUID);
// Execute the query
$stmt1->execute();
error_log("Email & username query executed in search for UID:" . $rUID);
// Get the result
$result1 = $stmt1->get_result();
// error_log("Get result".$result1);
// Check if the query returned any rows
if ($result1->num_rows > 0) {
$row = $result1->fetch_assoc();
// Access the values of UID and Username columns
$email = $row['email'];
$username = $row['user_name'];
error_log("User found" . $email);
// Execute the statement
if ($stmt->execute()) {
error_log("New password saved.");
$to_email = $email;
$subject = "SUCCESS";
$body = "
<h1>Password Reset Successful</h1>
<p>Dear '.$username',</p>
<p>Your password has been successfully reset. If you did not request this reset, please contact us immediately.</p>
<p>Best regards,<br>Distributed systems</p>
";
$from_email = "[email protected]";
if (sendEmail($to_email, $subject, $body, $from_email)) {
echo "Email sent successfully.";
error_log("confirmation email sent");
} else {
echo "Email sending failed.";
}
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
} else {
error_log("reset code not found");
}
// Close statement and connection
$stmt->close();
echo '<script>window.location.href = "login.php"</script>';
} else {
echo "passwords do not match";
}
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage();
}
$conn->close();
} else {
// Neither button was clicked
echo "No button clicked.";
$conn->close();
}
}
?>
</form>
</div>
<footer>
<p>© Distributed systems assignment.</p>
</footer>
</body>
</html>