-
Notifications
You must be signed in to change notification settings - Fork 12
/
reset.php
172 lines (120 loc) · 4.55 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
<?php
include("config.php");
include("functions.php");
include("session.php");
include("userclass.php");
include("email.php");
if(!isset($_SESSION['temphash'])) {
$_SESSION['temphash'] = hash("sha256", genrand());
}
$error = "";
if($_POST['reset'] == "reset") {
if($_POST['hash'] != $_SESSION['temphash']) {
$error = "there was an error. retry";
// check if it's a valid-ish email. it's not nothing. and it at least contains an @
} else if(isset($_POST['email']) && $_POST['email'] != "" && count(explode("@", $_POST['email'])) == 2) {
$sql = "SELECT * FROM `user` WHERE `email` = '".$mysql->real_escape_string($_POST['email'])."' LIMIT 1";
$user = $mysql->query($sql);
$user = $user->fetch_assoc();
if($user) {
if($user['email']) {
$user_id = $user['id'];
// sha1 of randomness
$reset_hash = sha1($user_id.genrand().sha1($user['salt']).genrand().time().$user['username'].genrand());
$sql = "
UPDATE `user`
SET
`resethash` = '".$mysql->real_escape_string($reset_hash)."',
`canreset` = '1'
WHERE
`id` = '".$mysql->real_escape_string($user_id)."'
LIMIT 1
";
$mysql->query($sql);
$reset_url = $basehost.$baseurl."/"."reset.php?u=$user_id&t=".$reset_hash;
send_email(
$user['email'],
"synccit password reset",
"You've requested a password reset for your synccit account.\r\n\r\n
To confirm your request, follow this link, $reset_url"
);
$error = "password reset email sent";
} else { // considering it's searching via email this should never come up but whatever
$error = "email not found";
}
} else {
// security vulnerability?
// you can search the db for emails
// though you can do this with most things
// a limit should probably be added
$error = "email not found";
}
}
}
if(isset($_GET['u']) && ((int) $_GET['u'] > 0) && isset($_GET['t'])) {
$u = (int) $_GET['u'];
$sql = "SELECT * FROM `user`
WHERE
`id` = '".$mysql->real_escape_string($u)."'
AND
`resethash` = '".$mysql->real_escape_string($_GET['t'])."'
AND
`canreset` = '1'
LIMIT 1";
$user = $mysql->query($sql);
$user = $user->fetch_assoc();
if($user) {
$hideform = 1;
$user_id = $user['id'];
$generated_password = genrand().genrand();
$hashset = create_hash($generated_password);
$pieces = explode(":", $hashset);
$salt = $pieces[2];
$hash = $pieces[3];
$sql = "
UPDATE `user`
SET
`passhash` = '".$mysql->real_escape_string($hash)."',
`salt` = '".$mysql->real_escape_string($salt)."',
`canreset` = '0'
WHERE
`id` = '".$mysql->real_escape_string($user_id)."'
LIMIT 1
";
$reset = $mysql->query($sql);
if($reset) {
send_email(
$user['email'],
"synccit password reset",
"your password has been reset to, ".$generated_password."\r\n\r\n
try logging in with it"
);
$error = "new password has been emailed to you";
} else {
$error = "database error. sorry, try again";
}
} else {
$error = "wrong reset code. try resetting again";
}
}
$hash = $_SESSION['temphash'];
htmlHeader("synccit - reset password");
?>
<div class="fourcol">
<h2>reset password</h2>
</div>
<div class="fourcol">
<span class="error"><?php echo $error; ?></span><br /><br />
<form action="<?php echo RESETURL; ?>" method="post">
<input type="hidden" name="hash" value="<?php echo $hash; ?>" />
<input type="hidden" name="reset" value="reset" />
<?php if(!$hideform) { ?>
<label for="email">email</label><br />
<input type="text" id="email" name="email" value="" class="textcreate" />
<br /><br />
<input type="submit" value="reset" name="reset" class="submit" />
<?php } ?>
</form>
</div>
<?php
htmlFooter();