-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathforgotPassword.php
More file actions
189 lines (145 loc) · 7 KB
/
Copy pathforgotPassword.php
File metadata and controls
189 lines (145 loc) · 7 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
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
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer/Exception.php';
require 'PHPMailer/PHPMailer.php';
require 'PHPMailer/SMTP.php';
include "connection.php";
if (isset($_POST["submit"])) {
$email = $_POST["email"];
$stmt = $connection->prepare("SELECT * FROM users WHERE email = ?");
$stmt->bind_param("s", $email);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows === 1) {
$data = $result->fetch_assoc();
$name = $data["firstname"] . " " . $data["lastname"];
$token = bin2hex(random_bytes(32)); // Generating reset_token
$expire_time = date("Y-m-d H:i:s", strtotime("+15 minutes")); // Expire time of the token
$hhmm = substr($expire_time, 11, 5);
// Updating reset_token and reset_expire in Database
$insert_token = $connection->prepare("UPDATE users SET reset_token = ?, reset_expire = ? WHERE email = ?");
$insert_token->bind_param("sss", $token, $expire_time, $email);
$insert_token->execute();
$link = "http://localhost/SEM%206%20-%20WP/Project/setNewPassword.php?token={$token}";
$success_token = bin2hex(random_bytes(32));
$body = file_get_contents("ResetLinkTemplate.html");
$body = str_replace(['{{USER_NAME}}', '{{RESET_URL}}', '{{EXPIRY_TIME}}', '{{RESET_URL}}'], [$name, $link, $hhmm, $link], $body);
$subject = "Your Password Reset Link";
$mail = new PHPMailer(true);
try {
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = '';
$mail->Password = '';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
$mail->Port = 465;
//Recipients
$mail->setFrom("", "");
$mail->addAddress($email, $name);
//Content
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $body;
if ($mail->send()) {
$update_reset_link_sent_token = $connection->prepare("UPDATE users SET reset_link_sent = ? WHERE email = ?");
$update_reset_link_sent_token->bind_param("ss", $success_token, $email);
$update_reset_link_sent_token->execute();
header("location:linkSentSuccessful.php?token=$success_token");
exit();
}
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
} else {
header("location:forgotPassword.php?errorMsg=Email does not exists.");
exit();
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Forgot Password – CEBS</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons/font/bootstrap-icons.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/7.0.1/css/all.min.css" integrity="sha512-2SwdPD6INVrV/lHTZbO2nodKhrnDdJK9/kg2XD1r9uGqPo1cUbujc+IYdlYdEErWNu69gVcYgdxlmVmzTWnetw==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<link href="https://fonts.googleapis.com/css2?family=Playfair+Display:wght@600;700;800&family=DM+Sans:wght@300;400;500;600&display=swap" rel="stylesheet">
<link rel="stylesheet" href="auth.css">
<link rel="stylesheet" href="style.css">
</head>
<body>
<?php
$showNavbar = false;
include "header.php";
?>
<div class="reg-card">
<form action="forgotPassword.php" method="POST" autocomplete="off">
<div class="screen active">
<button type="button" class="back-btn" id="backToLogin" style="font-size: 0.78rem !important;">
<i class="bi bi-arrow-left"></i> Back to Login
</button>
<h2>Forgot Password</h2>
<p class="form-hint">Enter your registered email address. We'll send a link to reset your password.</p>
<div class="form-divider"></div>
<label class="field-label">Email Address</label>
<div class="input-wrap">
<i class="bi bi-envelope icon-left"></i>
<input type="email" name="email" class="form-ctrl" id="forgotEmail" placeholder="john.doe@abcit.edu" required>
<small class="d-none" id="emailError"></small>
</div>
<?php
if (isset($_GET["errorMsg"])) {
echo "<span class='invalid-otp'>$_GET[errorMsg]</span>";
}
?>
<button class="btn-submit" name="submit" type="submit" id="sendOtp"> Submit</button>
<p class="form-footer">Remember your password? <a href="login.php">Back to Login</a></p>
</div>
</div>
</form>
<?php include "footer.php"; ?>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script>
window.history.replaceState(null, '', window.location.pathname);
let emailValid = false;
$("#forgotEmail").on("input", function() {
let emailText = $(this).val().toLowerCase();
$(this).val(emailText);
});
$("#forgotEmail").on("input", function() {
const email = $(this).val().trim();
const basicPattern = /^[a-zA-Z0-9]+([._%+-][a-zA-Z0-9]+)*@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
if (email.length === 0) {
$("#emailError").text("Email address is required.").css("color", "red").removeClass("d-none");
} else if (!email.includes("@")) {
$("#emailError").text("Email must contain '@' symbol.").css("color", "red").removeClass("d-none");
} else if (!email.includes(".")) {
$("#emailError").text("Email must contain a domain extension (e.g. .com).").css("color", "red").removeClass("d-none");
} else if (!basicPattern.test(email)) {
$("#emailError").text("Please enter a valid email address.").css("color", "red").removeClass("d-none");
} else {
$("#emailError").addClass("d-none");
emailValid = true;
return;
}
emailValid = false;
});
$(".btn-submit").on("click", function(e) {
if ($("#emailError").is(":visible")) {
e.preventDefault();
alert("Please fix the highlighted errors.");
}
});
</script>
<script>
let backToLogin = document.getElementById("backToLogin");
backToLogin.addEventListener("click", function() {
window.location = "login.php";
});
</script>
</body>
</html>