-
Notifications
You must be signed in to change notification settings - Fork 11
/
lost-password.php
349 lines (343 loc) · 12.4 KB
/
lost-password.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<?php
require('config.php');
require_once('plugins/cryptor/cryptor.php'); // Encryption / Decryption class
session_start();
$mysql = new mysqli($dbhost,$dbuser,$dbpass,$dbname);
$mysql->query("SET NAMES 'utf8'");
date_default_timezone_set('Europe/Zagreb');
function isLogged() {
global $mysql;
if (!empty($_SESSION['email'])) {
$email = addslashes($_SESSION['email']);
$password = trim($_SESSION['password']);
$query = $mysql->query("SELECT password FROM users WHERE email='$email'");
if ($query->num_rows > 0) {
$data = $query->fetch_assoc();
$hashed_password = $data['password'];
if (password_verify($password,$hashed_password)) {
return true;
}
else {
return false;
}
}
else {
return false;
}
}
else {
return false;
}
}
function ifUserExists($email) {
global $mysql;
$query = $mysql->query("SELECT id FROM users WHERE email='$email'");
if ($query->num_rows > 0) {
return true;
}
else {
return false;
}
}
function sendMail($receiverEmail,$receiverName,$subject,$message,$attachments=null,$protocol='standard',$type='html') {
require('plugins/PHPMailer/class.phpmailer.php');
date_default_timezone_set('Europe/Zagreb');
$mail = new PHPMailer();
$mail->CharSet = 'UTF-8';
if ($protocol == 'SMTP') {
$mail->IsSMTP();
$mail->SMTPDebug = 0; // 0 = off (for production use), 1 = client messages, 2 = client and server messages
$mail->SMTPSecure = 'ssl';
$mail->Debugoutput = 'html';
$mail->Host = '';
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->Username = '';
$mail->Password = '';
}
$mail->SetFrom('','');
$mail->AddReplyTo('','');
$mail->AddAddress($receiverEmail,$receiverName);
$mail->Subject = $subject;
$result = array();
if (is_array($message)) {
$mail->MsgHTML($message['html']);
$mail->AltBody = $message['text'];
}
else {
if ($type == 'text') {
$mail->AltBody = $message;
}
if ($type == 'html') {
$mail->MsgHTML($message);
}
}
if (is_array($attachments)) {
foreach ($attachments as $attachment) {
$mail->AddAttachment($attachment);
}
}
else {
$mail->AddAttachment($attachments);
}
if (!$mail->Send()) {
$result['status'] = false;
$result['errorInfo'] = $mail->ErrorInfo;
}
else {
$result['status'] = true;
}
return $result;
}
function getSettings() {
/* Get global website settings */
global $mysql;
return $mysql->query("SELECT recaptcha_sitekey,recaptcha_secretkey,g_analytics FROM system WHERE id='1'")->fetch_assoc();
}
if (isLogged()) {
if (isset($_REQUEST['redirect'])) {
header("Location: ".urldecode($_REQUEST['redirect']));
}
else {
header("Location: transactions.php");
}
}
else {
$reset = false;
$redirect = false;
$settings = getSettings();
if (isset($_GET['key'])) {
$key = addslashes($_GET['key']);
$query = $mysql->query("SELECT id FROM users WHERE hash='$key'");
if ($query->num_rows > 0) {
$reset = true;
$query = $query->fetch_assoc();
$userID = $query['id'];
}
else {
$error = 'Invalid security key!';
}
}
if (isset($_POST['password']) && isset($_POST['user-id'])) {
$secretKey = $settings['recaptcha_secretkey'];
$recaptcha = $_POST['g-recaptcha-response'];
$verify = json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=$secretKey&response=$recaptcha"));
if ($verify->success) {
$password = trim($_POST['password']);
$userID = addslashes($_POST['user-id']);
if (strlen($password) > 7) {
$hashed_password = password_hash($password,PASSWORD_DEFAULT);
$query = $mysql->query("UPDATE users SET password='$hashed_password' WHERE id='$userID'");
if ($query) {
$redirect = true;
$success = 'Password updated successfully! You can now login with new password!';
}
else {
$error = 'Unable to save new password! Please try again or contact support!';
}
}
else {
$error = 'Password too short! Minimum 8 characters!';
}
}
else {
$error = 'Anti-robot verification not passed. Please try again!';
}
}
if (isset($_POST['email'])) {
$secretKey = $settings['recaptcha_secretkey'];
$recaptcha = $_POST['g-recaptcha-response'];
$verify = json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=$secretKey&response=$recaptcha"));
if ($verify->success) {
$email = trim($_POST['email']);
if (ifUserExists($email)) {
$security_hash = md5(openssl_random_pseudo_bytes(32));
$mysql->query("UPDATE users SET hash='$security_hash' WHERE email='$email'");
$subject = 'Reset password | IOTA Web Wallet';
$message = file_get_contents('https://'.$_SERVER['SERVER_NAME'].'/email/lost-password.php?hash='.$security_hash);
$mailed = sendMail($email,'User',$subject,$message);
if ($mailed['status']) {
$info = 'Email with instructions to reset password has been sent to: <b>'.$email.'</b>. <br />Please open your email account (check <i>spam</i> mail too) and follow instructions!';
}
else {
$error = 'An error occurded while trying to send email with instructions to reset password. Please try again! <br />'.$mailed['errorInfo'];
}
}
else {
$error = 'User dosen\'t exists in database!';
}
}
else {
$error = 'Anti-robot verification not passed. Please try again!';
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Reset password page for IOTA web wallet.">
<meta name="author" content="Nikola Rogina">
<title>Lost password | IOTA Web Wallet</title>
<!-- Bootstrap Core CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- MetisMenu CSS -->
<link href="css/metisMenu.css" rel="stylesheet">
<!-- Custom CSS -->
<link href="css/theme.css" rel="stylesheet">
<!-- Custom Fonts -->
<link href="css/font-awesome.min.css" rel="stylesheet" type="text/css">
<!-- Favicon -->
<link rel="shortcut icon" type="image/x-icon" sizes="16x16 32x32 48x48" href="img/favicon.ico">
<?php echo $settings['g_analytics']; ?>
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
<script src='https://www.google.com/recaptcha/api.js'></script>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-4 col-md-offset-4">
<a href="index.php"><img src="img/logo-black.png" style="width:90%;margin:5% 0 -15% 5%;" alt="IOTA Web Wallet"/></a>
<div class="login-panel panel panel-default">
<div class="panel-heading">
<h3 class="panel-title"><?php if (!$reset) {echo 'Lost password';} else {echo 'Reset password';} ?></h3>
</div>
<div class="panel-body">
<?php
if (!empty($info)) {
echo '<div class="alert alert-info text-center">'.$info.'</div>';
}
if (!empty($success)) {
echo '<div class="alert alert-success text-center">'.$success.'</div>';
}
if (!empty($error)) {
echo '<div class="alert alert-danger text-center">'.$error.'</div>';
}
?>
<form action="<?php echo (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; ?>" method="post" id="<?php if (!$reset) {echo 'lost-password';} else {echo 'reset-password';} ?>">
<fieldset>
<?php if (!$reset) { ?>
<div class="form-group" id="email-div">
<input class="form-control" placeholder="Your email address" id="email" name="email" type="email" autofocus required>
<p class="help-block" style="display:none;"></p>
</div>
<?php } else { ?>
<div class="form-group" id="password-div">
<input class="form-control" placeholder="Password" name="password" id="password" type="password" required>
<p class="help-block" style="display:none;">Minimum 8 characters!</p>
</div>
<div class="form-group" id="password2-div">
<input class="form-control" placeholder="Confirm password" id="password2" type="password" value="" required>
<p class="help-block" style="display:none;">Passwords do not match!</p>
</div>
<input type="hidden" name="user-id" value="<?php echo $userID; ?>" />
<?php } ?>
<div class="form-group" id="recaptcha-div">
<div class="g-recaptcha" data-sitekey="<?php echo $settings['recaptcha_sitekey']; ?>"></div>
<p class="help-block text-center" style="display:none;color:red;">Please confirm your humanity!</p>
</div>
<button type="submit" class="btn btn-lg btn-primary btn-block"><?php if (!$reset) {echo 'Recover';} else {echo 'Reset';} ?> password</button>
<div class="form-group" style="margin:20px 0 -10px 0;">
<p class="help-block pull-left"><a href="register.php">Register</a></p>
<p class="help-block pull-right"><a href="index.php">Login</a></p>
</div>
</fieldset>
</form>
</div>
</div>
</div>
</div>
</div>
<footer class="text-center">
<p>© <?php echo date("Y").' '.$_SERVER['HTTP_HOST']; ?> | Author: <a href="mailto:admin@iota.hr">Nikola Rogina</a></p>
</footer>
<!-- jQuery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="js/jquery.min.js"><\/script>')</script>
<!-- Bootstrap Core JavaScript -->
<script src="js/bootstrap.min.js"></script>
<!-- Metis Menu Plugin JavaScript -->
<script src="js/metisMenu.min.js"></script>
<!-- Custom Theme JavaScript -->
<script src="js/theme.js"></script>
<script>
function validateEmail(email) {
if (email.length > 5) {
var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
return emailReg.test(email);
}
else {
return false;
}
}
$(document).ready(function() {
if (!/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
$(".g-recaptcha").css("margin-left","15px");
}
$('#email').change(function() {
if (!validateEmail($(this).val())) {
$('#email-div').addClass('has-error');
$('#email-div').find('p').html('Please enter a valid email!').show();
}
else {
$('#email-div').removeClass('has-error').addClass('has-success');
$('#email-div').find('p').hide();
}
});
$('#password').change(function() {
if ($(this).val().length < 8) {
$('#password-div').addClass('has-error');
$('#password-div').find('p').show();
}
else {
$('#password-div').removeClass('has-error').addClass('has-success');
$('#password-div').find('p').hide();
}
});
$('#password2').change(function() {
if ($(this).val() !== $('#password').val()) {
$('#password2-div').addClass('has-error');
$('#password2-div').find('p').show();
}
else {
$('#password2-div').removeClass('has-error').addClass('has-success');
$('#password2-div').find('p').hide();
}
});
$("#lost-password").submit(function(e) {
e.preventDefault();
if (grecaptcha.getResponse().length > 0) {
$('#recaptcha-div').find('p').hide();
if (validateEmail($('#email').val())) {
$(this).unbind('submit').submit();
}
}
else {
$('#recaptcha-div').find('p').show();
}
});
$("#reset-password").submit(function(e) {
e.preventDefault();
if (grecaptcha.getResponse().length > 0) {
$('#recaptcha-div').find('p').hide();
if ($('#password').val().length > 7 && $('#password2').val() === $('#password').val()) {
$(this).unbind('submit').submit();
}
}
else {
$('#recaptcha-div').find('p').show();
}
});
});
</script>
</body>
</html>
<?php $mysql->close(); ?>