-
Notifications
You must be signed in to change notification settings - Fork 0
/
resetPassword.php
255 lines (184 loc) · 8.5 KB
/
resetPassword.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
<?php
//------------------------------>> CENTRALIZED TECHFEST NAME WITH YEAR
require_once "config/techfestName.php";
//------------------------------>> DB CONFIG
require_once "config/configPDO.php";
// ------------------------>> START CONNECTION
session_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?php echo $techfestName ?> | RESET PASSOWRD</title>
<!-- Include Header Scripts -->
<?php include_once "includes/headerScripts.php";?>
</head>
<body>
<?php
if (isset($_GET['token'])) {
$token = htmlspecialchars($_GET['token']);
if (isset($_POST['resetPassword'])) {
# Avoid XSS Attack
$userType = htmlspecialchars($_POST['userType']);
$newPassword = htmlspecialchars($_POST['newPassword']);
$confirmNewPassword = htmlspecialchars($_POST['confirmNewPassword']);
try {
if ($newPassword === $confirmNewPassword) {
$newPassword = password_hash($newPassword, PASSWORD_BCRYPT);
$confirmNewPassword = password_hash($confirmNewPassword, PASSWORD_BCRYPT);
//---------------------------------------->> USER TYPE = USER
if ($userType == "User") {
# Sql Query
$sql1 = "SELECT * FROM user_information WHERE token = :token";
# Preparing Query
$result1 = $conn->prepare($sql1);
# Binding Value
$result1->bindValue(":token", $token);
# Executing Query
$result1->execute();
# Fetch Data from Database
$row1 = $result1->fetch(PDO::FETCH_ASSOC);
$dbtokenDate = strtotime($row1['tokenDate']);
$currentDatetime = date("Y-m-d H:i:s");
$currentDatetimeMain = strtotime($currentDatetime);
# Checking Wether token expired or not
if ($dbtokenDate >= $currentDatetimeMain) {
$sql = "UPDATE user_information SET password = :newPassword WHERE token = :token";
$result = $conn->prepare($sql);
$result->bindValue(":newPassword", $newPassword);
$result->bindValue(":token", $token);
$result->execute();
if ($result) {
echo "<script>Swal.fire({
icon: 'success',
title: 'Successful',
text: 'Your Password Reset Successful, Please Login to Continue'
})</script>";
} else {
echo "<script>Swal.fire({
icon: 'error',
title: 'Error',
text: 'Something Went Wrong'
})</script>";
}
} else {
echo "<script>Swal.fire({
icon: 'warning',
title: 'Token Time Expired',
text: 'Please Request Again'
})</script>";
}
//---------------------------------------->> USER TYPE = ADMIN
} else {
# Sql Query
$sql3 = "SELECT * FROM admin_information WHERE token = :token";
# Preparing Query
$result3 = $conn->prepare($sql3);
# Binding Values
$result3->bindValue(":token", $token);
# Executing Query
$result3->execute();
$row2 = $result3->fetch(PDO::FETCH_ASSOC);
$dbtokenDate = strtotime($row2['tokenDate']);
$currentDatetime = date("Y-m-d H:i:s");
$currentDatetimeMain = strtotime($currentDatetime);
# Checking Weither token expired or not
if ($dbtokenDate >= $currentDatetimeMain) {
# SQL Query
$sql4 = "UPDATE admin_information SET adminPassword= :newPassword WHERE token = :token";
# Preparing Query
$result4 = $conn->query($sql4);
# Binding Value
$result4->bindValue(":newPassword", $newPassword);
$result4->bindValue(":token", $token);
# Executing Query
$result4->execute();
if ($result4) {
echo "<script>Swal.fire({
icon: 'success',
title: 'Successful',
text: 'Your Password Reset Successful, Please Login to Continue'
})</script>";
} else {
echo "<script>Swal.fire({
icon: 'error',
title: 'Error',
text: 'Something Went Wrong'
})</script>";
}
} else {
echo "<script>Swal.fire({
icon: 'warning',
title: 'Token Time Expired',
text: 'Please Request Again'
})</script>";
}
}
} else {
echo "<script>Swal.fire({
icon: 'warning',
title: 'Warning',
text: 'New Password and Confirm Password are not same'
})</script>";
}
} catch (PDOException $e) {
echo "<script>alert('We are sorry, there seems to be a problem with our systems. Please try again.');</script>";
# Development Purpose Error Only
echo "Error " . $e->getMessage();
}
}
} else {
header("Location: login.php");
}
?>
<main class="container">
<div class="row">
<section class="col-md-6 my-5 offset-md-3">
<div class="card shadow p-5">
<h3 class="text-center font-time text-uppercase">
Reset Password
</h3>
<hr>
<div class="card-body">
<form action="" method="post" id="resetPasswordForm">
<div class="form-group">
<label>User Type</label>
<select class="form-control" name="userType" id="userType">
<option value="User">Normal User</option>
<option value="Administrator">Administrator</option>
</select>
</div>
<div class="form-group">
<label>Enter New Password</label>
<input type="password" class="form-control" name="newPassword" id="newPassword">
<small class="text-danger">Password should Contain atleast 8 Character, Minimum
one uppercase letter,
Minimum one lowercase letter,
Minimum one number, Minimum one special character. </small>
</div>
<div class="form-group">
<label>Confirm New Password</label>
<input type="password" class="form-control" name="confirmNewPassword" id="confirmNewPassword">
</div>
<button type="submit" class="btn btn-danger mt-3 rounded-pill btn-block"
name="resetPassword">
Reset Password
</button>
<h6 class="font-sans mt-3">Go to <a href="login.php">login</a> page</h6>
</form>
</div>
</div>
</section>
</div>
</main>
<!-- Include Footer & Footer Scripts -->
<?php
include_once "includes/footer.php";
include_once "includes/footerScripts.php";
?>
<!-- Close Database Connection -->
<?php $conn = null;?>
</body>
</html>