This repository has been archived by the owner on Dec 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathreset.php
146 lines (100 loc) · 3.14 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
<?php
ini_set('display_errors', 'on');
require 'classes/Session.class.php';
if(isset($_SESSION['id'])){
header("Location:index");
exit();
}
$msg = '';
if(isset($_POST['email'])){
require 'classes/System.class.php';
$pdo = PDO_DB::factory();
$system = new System();
$email = $_POST['email'];
if(!$system->validate($email, 'email')){
$msg = 'Invalid email';
}
$sql = 'SELECT id, login, COUNT(*) AS total FROM users WHERE email = :email LIMIT 1';
$stmt = $pdo->prepare($sql);
$stmt->execute(Array(':email' => $email));
$userInfo = $stmt->fetch(PDO::FETCH_OBJ);
if($userInfo->total == 0){
$msg = 'This email is not registered';
}
$code = uniqid();
$sql = 'INSERT INTO email_reset (userID, code) VALUES (:uid, :code)';
$stmt = $pdo->prepare($sql);
$stmt->execute(Array(':uid' => $userInfo->id, ':code' => $code));
require '/var/www/classes/SES.class.php';
$ses = new SES();
$ses->send('request_reset', Array('to' => $email, 'user' => $userInfo->login, 'code' => $code));
$_SESSION['MSG'] = 'Reset email sent.';
$_SESSION['TYP'] = 'REG';
$_SESSION['MSG_TYPE'] = 'success';
header("Location:index");
exit();
} elseif(isset($_POST['code'])){
$pdo = PDO_DB::factory();
$code = $_POST['code'];
if(strlen($code) != 13){
exit("Bad code");
}
$sql = 'SELECT userID, COUNT(*) AS total FROM email_reset WHERE code = :code LIMIT 1';
$stmt = $pdo->prepare($sql);
$stmt->execute(Array(':code' => $code));
$resetInfo = $stmt->fetch(PDO::FETCH_OBJ);
if($resetInfo->total == 0){
exit("This code is invalid.");
}
if($_POST['pwd'] != $_POST['pwd2']){
exit("Passwords are different");
}
$pwd = $_POST['pwd'];
if(strlen(htmlentities($pwd)) <= 5){
exit("Please use at least 6 characteres");
}
require 'classes/BCrypt.class.php';
$bcrypt = new BCrypt();
$newPwd = $bcrypt->hash(htmlentities($pwd));
$sql = 'UPDATE users SET password = :pwd WHERE id = :uid';
$stmt = $pdo->prepare($sql);
$stmt->execute(Array(':uid' => $resetInfo->userid, ':pwd' => $newPwd));
$sql = 'DELETE FROM email_reset WHERE code = :code';
$stmt = $pdo->prepare($sql);
$stmt->execute(Array(':code' => $code));
exit("Password changed");
} elseif(isset($_GET['code'])){
$pdo = PDO_DB::factory();
$code = $_GET['code'];
if(strlen($code) != 13){
exit("Bad code");
}
$sql = 'SELECT userID, COUNT(*) AS total FROM email_reset WHERE code = :code LIMIT 1';
$stmt = $pdo->prepare($sql);
$stmt->execute(Array(':code' => $code));
$resetInfo = $stmt->fetch(PDO::FETCH_OBJ);
if($resetInfo->total == 0){
exit("This code is invalid.");
}
?>
<form action="" method="POST">
<input type="hidden" name="code" value="<?php echo $_GET['code']; ?>">
Password: <input type="password" name="pwd"> (6 or more characters)<br/>
Repeat plz: <input type="password" name="pwd2"><br/>
<input type="submit" value="Change password">
</form>
<?php
exit();
}
?>
<html>
<head>
</head>
<body>
<?php if($msg != ''){ echo $msg.'<br/><br/>'; } ?>
<form action="reset" method="POST">
<input type="text" name="email" placeholder="Please insert your email"><br/><br/>
<input type="submit" value="Request password reset">
</form>
</body>
</html>