-
Notifications
You must be signed in to change notification settings - Fork 0
/
pw_recovery_fns.php
212 lines (203 loc) · 7.66 KB
/
pw_recovery_fns.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
<?php
function user_exists($email, &$db) {
//echo "user exists function called. <br><br>";
try {
$sql = 'SELECT userid
FROM sb_users
WHERE email = :email';
$stmt = $db->prepare($sql);
$stmt->bindValue(':email', $email);
$stmt->execute();
$stmt->bindColumn('userid', $userid);
$errorInfo = $stmt->errorInfo();
if (isset($errorInfo[2])) {
$error = $errorInfo[2];
}
} catch (Exception $e) {
$error = $e->getMessage();
}
if (isset($error)) {
//echo "user exists db error: $error <br><br>";
return [false, $error];
}
$stmt->fetch(PDO::FETCH_BOUND);
if ($userid) {
//echo "user id found by user exists function: $userid <br><br>";
return [true];
} else {
//echo "no user id found by user exists function. <br><br>";
return [false, 'Request failed: No such user.'];
}
}
function generate_email($email, $key) {
//echo "generate email function called. <br><br>";
// generate email message
$message = array();
$message[] = "This email was requested on the Aquilino Arts Storyboard password reset page.";
$message[] = "If you did not request it you are safe to ignore it.";
$message[] = "If you did request it, please click or paste the link below to be taken to a password reset form.";
$message[] = "http://localhost/storyboardapp/password.php?key=$key";
$message[] = "This link will expire in one hour.";
//echo "message constructed: ";
//echo implode(' ', $message);
//echo " <br><br>";
// generate email headers
$headers = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/plain; charset=iso-8859-1";
$headers[] = "From: Aquilino Arts Storyboard <storyboard@aquilinoarts.com>";
$headers[] = "Bcc: Rebecca Weber <reveweber@gmail.com>";
$headers[] = "Reply-To: Aquilino Arts Storyboard <storyboard@aquilinoarts.com>";
$headers[] = "Subject: Storyboard account password recovery";
$headers[] = "X-Mailer: PHP/".phpversion();
//echo "headers constructed: ";
//echo implode(' ', $headers);
//echo " <br><br>";
// send message
return mail($email, "Storyboard account password recovery", implode("\r\n", $message), implode("\r\n", $headers));
}
function generate_request($email, &$db) {
//echo "generate request function called. <br><br>";
$user_confirmed = user_exists($email, $db);
if ($user_confirmed[0]) {
$requestkey = mt_rand(10000000, mt_getrandmax());
$time = time();
//echo "user confirmed to exist; request key generated: $requestkey <br><br>";
try {
$sql = 'INSERT INTO password_reset_requests
(email, requestkey, time)
VALUES (:email, :requestkey, :time)';
$stmt = $db->prepare($sql);
$stmt->bindValue(':email', $email);
$stmt->bindValue(':requestkey', $requestkey);
$stmt->bindValue(':time', $time);
$stmt->execute();
$errorInfo = $stmt->errorInfo();
if (isset($errorInfo[2])) {
$error = $errorInfo[2];
}
} catch (Exception $e) {
$error = $e->getMessage();
}
if (isset($error) ) {
//echo "database error in request insertion: $error <br><br>";
return [false, $error];
}
$requestid = $db->lastInsertId();
if (!isset($requestid)) {
//echo "error: no request id returned by db. <br><br>";
return [false, 'Error: Could not add request to database.'];
} else {
//echo "made it! have a request id: $requestid <br><br>";
$key = $requestid . '-' . $requestkey;
//echo "about to generate an email with address $email and key $key <br><br>";
if (generate_email($email, $key)) {
//echo "email sent. <br><br>";
return [true];
} else {
//echo "email not sent. <br><br>";
return [false, "Error: email could not be sent."];
}
}
} else {
//echo "user does not exist. <br><br>";
return $user_confirmed; // already in the form [false, error]
}
}
function mark_request_used($requestid, &$db) {
//echo "mark request used function called. <br><br>";
try {
$sql = 'UPDATE password_reset_requests
SET used = 1
WHERE requestid = :requestid';
$stmt = $db->prepare($sql);
$stmt->bindValue(':requestid', $requestid);
$stmt->execute();
$errorInfo = $stmt->errorInfo();
if (isset($errorInfo[2])) {
$error = $errorInfo[2];
}
} catch (Exception $e) {
$error = $e->getMessage();
}
if($error) {
//echo "error in marking request used in db: $error <br><br>";
return [false, $error];
} else {
//echo "request marked used in db. <br><br>";
return [true];
}
}
function check_request($key, &$db) {
//echo "check request function called. <br><br>";
$key_pieces = explode('-', $key);
$requestid = $key_pieces[0];
$givenrequestkey = $key_pieces[1];
//echo "request key disassembled into id $requestid and key $givenrequestkey <br><br>";
try {
$sql = 'SELECT email, requestkey, time, used
FROM password_reset_requests
WHERE requestid = :requestid';
$stmt = $db->prepare($sql);
$stmt->bindValue(':requestid', $requestid);
$stmt->execute();
$stmt->bindColumn('email', $email);
$stmt->bindColumn('requestkey', $requestkey);
$stmt->bindColumn('time', $time);
$stmt->bindColumn('used', $used);
$errorInfo = $stmt->errorInfo();
if (isset($errorInfo[2])) {
$error = $errorInfo[2];
}
} catch (Exception $e) {
$error = $e->getMessage();
}
if (isset($error)) {
//echo "db selection error: $error <br><br>";
return [false, $error];
}
$stmt->fetch(PDO::FETCH_BOUND);
if (isset($email)) {
//echo "we got a response! email $email requestkey $requestkey timestamp $time used $used <br><br>";
if ($used) {
return [false, "Error: This request has already been used."];
}
if (time() > ($time + 360)) {
return [false, "Error: This request has expired."];
}
if ($requestkey != $givenrequestkey) {
return [false, "Error: Request failed identification test."];
}
$_SESSION['request_email'] = $email;
$mark_used = mark_request_used($requestid, $db);
return $mark_used;
} else {
return [false, 'Error: No password request matching given information or error pulling data from database.'];
}
}
function reset_password($email, $password, &$db) {
//echo "in the reset password function. <br><br>";
$password_hash = password_hash($password, PASSWORD_DEFAULT);
try {
$sql = 'UPDATE sb_users
SET password_hash = :password_hash
WHERE email = :email';
$stmt = $db->prepare($sql);
$stmt->bindValue(':password_hash', $password_hash);
$stmt->bindValue(':email', $email);
$stmt->execute();
$errorInfo = $stmt->errorInfo();
if (isset($errorInfo[2])) {
$error = $errorInfo[2];
}
} catch (Exception $e) {
$error = $e->getMessage();
}
if($error) {
//echo "error in database update: $error <br><br>";
return [false, $error];
} else {
//echo "password updated. <br><br>";
return [true];
}
}