This repository has been archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreset.php
160 lines (136 loc) · 5.64 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
<?php
session_start();
if (isset($_SESSION['user_id'])) {
header("Location: home.php");
exit;
}
require 'vendor/autoload.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
include 'config/config.php';
if ($db->connect_error) {
die("Błąd połączenia z bazą danych: " . $db->connect_error);
}
$errorMsg = "";
$successMsg = "";
if (isset($_POST['reset'])) {
$email = $_POST['email'];
$checkUserQuery = "SELECT id FROM users WHERE email = ?";
$checkUserStmt = $db->prepare($checkUserQuery);
$checkUserStmt->bind_param("s", $email);
$checkUserStmt->execute();
$checkUserResult = $checkUserStmt->get_result();
if ($checkUserResult->num_rows > 0) {
$userData = $checkUserResult->fetch_assoc();
$userId = $userData['id'];
$token = bin2hex(random_bytes(16));
$expiration = date("Y-m-d H:i:s", strtotime("+1 hour"));
$insertTokenQuery = "INSERT INTO password_reset (user_id, token, expiration) VALUES (?, ?, ?)";
$insertTokenStmt = $db->prepare($insertTokenQuery);
$insertTokenStmt->bind_param("iss", $userId, $token, $expiration);
$insertTokenStmt->execute();
$insertTokenStmt->close();
$mail = new PHPMailer(true);
try {
$mail->isSMTP();
$mail->Host = 'mx1.mail.com'; // Adres serwera SMTP
$mail->SMTPAuth = true;
$mail->Username = '[email protected]'; // Twój login SMTP
$mail->Password = 'password'; // Twoje hasło SMTP
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->CharSet = 'UTF-8'; // Ustawić kodowanie na UTF-8
$mail->Encoding = 'base64'; // Ustawić kodowanie treści na base64
$mail->setFrom('[email protected]', 'Mail');
$mail->addAddress($email);
$mail->isHTML(true);
$mail->Subject = 'Reset hasła';
$mail->Body = "
<html>
<head>
<style>
.button {
background-color: #2563eb;
color: #ffffff;
text-decoration: none;
text-align: center;
padding: 10px 20px;
display: inline-block;
margin-top: 20px;
border-radius: 5px;
}
.center-logo {
text-align: center;
background-color: #151921;
border-top-left-radius: 5px;
border-top-right-radius: 5px;
}
.logo {
width: 300px; /* Dostosuj szerokość loga */
display: inline-block;
}
</style>
</head>
<body style='background-color: #151921; font-family: Arial, sans-serif; padding: 20px;'>
<div style='background-color: #ffffff; max-width: 600px; margin: 0 auto; border-radius: 5px; box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.1);'>
<div class='center-logo'>
<img src='logo.png' alt='Logo' class='logo'>
</div>
<div style='padding: 20px;'>
<p style='font-size: 16px;'>Aby zresetować hasło, kliknij poniższy przycisk:</p>
<a href='https://site.com/reset.php?token=$token' class='button'>
Zresetuj hasło
</a>
</div>
</div>
</body>
</html>
";
$mail->send();
$successMsg = "Na Twój adres e-mail został wysłany link do resetowania hasła.";
} catch (Exception $e) {
$errorMsg = "Błąd podczas wysyłania e-maila: " . $mail->ErrorInfo;
}
} else {
$errorMsg = "Użytkownik o podanym adresie e-mail nie istnieje.";
}
$checkUserStmt->close();
}
if (isset($_GET['token'])) {
$token = $_GET['token'];
$currentTime = date("Y-m-d H:i:s");
$selectTokenQuery = "SELECT * FROM password_reset WHERE token = ? AND expiration > ?";
$selectTokenStmt = $db->prepare($selectTokenQuery);
$selectTokenStmt->bind_param("ss", $token, $currentTime);
$selectTokenStmt->execute();
$selectTokenResult = $selectTokenStmt->get_result();
if ($selectTokenResult->num_rows > 0) {
$resetPassword = true;
$tokenData = $selectTokenResult->fetch_assoc();
$userId = $tokenData['user_id'];
$expireTokenQuery = "UPDATE password_reset SET expiration = ? WHERE token = ?";
$expireTokenStmt = $db->prepare($expireTokenQuery);
$expireTokenStmt->bind_param("ss", $currentTime, $token);
$expireTokenStmt->execute();
$expireTokenStmt->close();
} else {
$errorMsg = "Nieprawidłowy lub wygasły token resetu hasła.";
}
}
if (isset($_POST['new_password'])) {
$newPassword = $_POST['new_password'];
$hashedPassword = password_hash($newPassword, PASSWORD_DEFAULT);
if (isset($_POST['token'])) {
$token = $_POST['token'];
$updatePasswordQuery = "UPDATE users SET password = ? WHERE id = (SELECT user_id FROM password_reset WHERE token = ?)";
$updatePasswordStmt = $db->prepare($updatePasswordQuery);
$updatePasswordStmt->bind_param("ss", $hashedPassword, $token);
if ($updatePasswordStmt->execute()) {
$successMsg = 'Twoje hasło zostało zresetowane. Możesz się teraz <a href="login.php">zalogować</a>.';
} else {
$errorMsg = "Błąd podczas resetowania hasła: " . $updatePasswordStmt->error;
}
}
}
include "themes/$theme/reset.php";
?>