-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathforgot.php
156 lines (116 loc) · 4.06 KB
/
forgot.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
<?php
declare(strict_types=1);
session_start();
ob_start();
include "includes/connect.php";
include "includes/functions.php";
?>
<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
// Load Composer's autoloader
include(__DIR__ . "/vendor/autoload.php");
?>
<!-- Mail Section (Mailtrap API) -->
<?php
$dotenv = Dotenv\Dotenv::createUnsafeImmutable(__DIR__);
$dotenv->load(); //works here
//Mailtrap Credentials
$SMTP_HOST = getenv('SMTP_HOST');
$SMTP_PORT = getenv('SMTP_PORT');
$SMTP_USER = getenv('SMTP_USER');
$SMTP_PASSWORD = getenv('SMTP_PASSWORD');
$SMTP_ENCRYPTION = PHPMailer::ENCRYPTION_STARTTLS;
$mail = new PHPMailer(true);
//echo get_class($mail); //find out if class is available
?>
<?php
if (isset($_POST['forgot_submit'])) {
$email = validate($_POST['email']);
//Error messages
if (empty($email)) {
redirect("forgot.php?error=emptyFields");
exit();
}
//check if email exists
if (email_does_not_exist($email) == 'true') {
redirect("forgot.php?error=no_email");
exit();
}
//Creating tokens (see if same system can be used for OTP order and reservation - part 2)
$length = 50;
$token = bin2hex(openssl_random_pseudo_bytes($length));
//Updating database with token values
$stmt = prepare_query("UPDATE users SET token=? WHERE user_email=?");
$stmt->bindParam(1, $token, PDO::PARAM_STR);
$stmt->bindParam(2, $email, PDO::PARAM_STR);
$stmt->execute();
unset($stmt); //close off prepared statements
//Setting up PHP Mailer for Mailtrap API
try {
//access class
//$mail->SMTPDebug = SMTP::DEBUG_SERVER;
$mail->isSMTP();
$mail->Host = $SMTP_HOST;
$mail->Username = $SMTP_USER;
$mail->Password = $SMTP_PASSWORD;
$mail->Port = $SMTP_PORT;
$mail->SMTPSecure = $SMTP_ENCRYPTION;
$mail->SMTPAuth = true;
//Recipients
$mail->setFrom('[email protected]', 'Restaurant Admin');
$mail->addAddress($email);
// Content
$mail->isHTML(true);
$mail->Subject = 'Password Reset Email';
$mail->Body = "<p>Please click here to reset your password: -
<a href='http://localhost/radon/reset.php?email={$email}&token={$token}' target='_blank'>http://localhost/radon/reset.php?email={$email}&token={$token}</a>
</p>";
$mail->send();
redirect("forgot.php?success=forgot_sent");
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Forgot Password</title>
<link rel="stylesheet" type="text/css" href="css\fontawesome-all.min.css">
<link rel="stylesheet" type="text/css" href="css\style_login.css">
</head>
<body>
<div class="container">
<div class="header">
<!-- Display error messages -->
<?php display_error_message(); ?>
<!-- Display success messages -->
<?php display_success_message(); ?>
<h2>Password Recovery</h2>
</div>
<div class="main">
<form action="" method="post">
<span>
<i class="fa fa-user"></i>
<input type="email" placeholder="Enter Email" name="email">
</span><br>
<button type="submit" name="forgot_submit" class="btn btn-primary">Password Recovery</button>
<br>
<span> <a href="#"></a></span><br>
<br>
</form>
</div>
</div>
</body>
</html>
<?php
//close database connection - initialize object to null
$pdo = null;
ob_end_flush();
?>
<!--<a href='http://www.jawwadfida.com/phpDemo/food/reset.php?email={$email}&token={$token}' target='_blank'>http://www.jawwadfida.com/phpDemo/food/reset.php?email={$email}&token={$token}</a>
</p>"-->