Passwort vergessen Funktion
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
require_once "php/model/UserManager.php";
|
||||
require_once "php/validator/user-validator.php";
|
||||
|
||||
$error = null;
|
||||
$success = null;
|
||||
|
||||
if ($_SERVER["REQUEST_METHOD"] === "POST") {
|
||||
|
||||
$email = trim($_POST["email"] ?? "");
|
||||
$plainPassword = $_POST["password"] ?? "";
|
||||
|
||||
if (!userEmailValidator($email)) {
|
||||
$error = "Bitte gib eine gültige E-Mail-Adresse ein.";
|
||||
} elseif (!userPasswordValidator($plainPassword)) {
|
||||
$error = "Das Passwort muss 5 bis 12 Zeichen lang sein.";
|
||||
} else {
|
||||
try {
|
||||
$dao = UserManager::getInstance();
|
||||
|
||||
$token = bin2hex(random_bytes(16));
|
||||
$existingUser = $dao->findUser($email);
|
||||
|
||||
if (!is_dir("data/mails") && !mkdir("data/mails", 0777, true)) {
|
||||
throw new RuntimeException("Ordner data/mails konnte nicht erstellt werden.");
|
||||
}
|
||||
|
||||
if (!is_dir("data/pending-password") && !mkdir("data/pending-password", 0777, true)) {
|
||||
throw new RuntimeException("Ordner data/pending-password konnte nicht erstellt werden.");
|
||||
}
|
||||
|
||||
if (!is_writable("data/mails") || !is_writable("data/pending-password")) {
|
||||
throw new RuntimeException("Ordner sind nicht beschreibbar.");
|
||||
}
|
||||
|
||||
if ($existingUser !== null) {
|
||||
$pendingData = [
|
||||
"email" => $email,
|
||||
"password" => password_hash($plainPassword, PASSWORD_DEFAULT)
|
||||
];
|
||||
|
||||
file_put_contents(
|
||||
"data/pending-password/" . $token . ".json",
|
||||
json_encode($pendingData, JSON_PRETTY_PRINT)
|
||||
);
|
||||
}
|
||||
|
||||
$mailContent = "
|
||||
<h2>Passwort zurücksetzen</h2>
|
||||
<p>Falls Sie diese Anfrage nicht gestellt haben, können Sie diese Nachricht ignorieren.</p>
|
||||
<p>
|
||||
<a href='index.php?pfad=confirm-password&token=$token'>
|
||||
Passwortänderung bestätigen
|
||||
</a>
|
||||
</p>
|
||||
";
|
||||
|
||||
file_put_contents("data/mails/" . $token . ".html", $mailContent);
|
||||
|
||||
$success = 'Weitere Infos finden Sie in der Datei
|
||||
<a href="index.php?pfad=show-mail&token=' . htmlspecialchars($token) . '" target="_blank">xy</a>.';
|
||||
|
||||
} catch (Exception $e) {
|
||||
$error = "Die Passwortänderung konnte nicht verarbeitet werden.";
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user