Passwort vergessen Funktion

This commit is contained in:
2026-06-25 23:40:33 +02:00
parent e71ee8300e
commit 4d0f1de01c
5 changed files with 188 additions and 2 deletions
+61
View File
@@ -0,0 +1,61 @@
<?php
require_once "php/model/UserManager.php";
$token = basename($_GET["token"] ?? "");
$file = "data/pending-password/" . $token . ".json";
$title = "Passwort zurücksetzen";
$message = "";
$link = "";
$isSuccess = false;
if (!file_exists($file)) {
$message = "Der Bestätigungslink ist ungültig oder bereits abgelaufen.";
} else {
$data = json_decode(file_get_contents($file), true);
if ($data === null || empty($data["email"]) || empty($data["password"])) {
$message = "Die Daten zur Passwortänderung konnten nicht gelesen werden.";
} else {
try {
$dao = UserManager::getInstance();
$user = $dao->findUser($data["email"]);
if ($user !== null) {
$dao->updateUser(
$user["email"],
$user["email"],
$user["vorname"],
$user["nachname"],
$data["password"]
);
}
unlink($file);
$title = "Passwort geändert";
$message = "Ihr Passwort wurde erfolgreich geändert. Sie können sich jetzt anmelden.";
$link = '<a class="button confirm-button" href="index.php?pfad=login">Zum Login</a>';
$isSuccess = true;
} catch (Exception $e) {
$message = "Das Passwort konnte nicht geändert werden.";
}
}
}
?>
<main class="login-page">
<div class="login-container">
<h1><?php echo htmlspecialchars($title); ?></h1>
<p class="alert-message <?php echo $isSuccess ? 'is-success' : 'is-error'; ?> confirm-message">
<?php echo htmlspecialchars($message); ?>
</p>
<?php echo $link; ?>
</div>
</main>
+8 -2
View File
@@ -11,8 +11,8 @@ $error = $error ?? null;
<h1>Bitte anmelden</h1>
<?php if ($error): ?>
<p style="color:red;">
<?php if (!empty($error)): ?>
<p class="alert-message is-error">
<?php echo htmlspecialchars($error); ?>
</p>
<?php endif; ?>
@@ -41,6 +41,12 @@ $error = $error ?? null;
anmelden
</button>
<div class="register-link">
<a href="index.php?pfad=password-forgotten">
Passwort vergessen?
</a>
</div>
<div class="register-link">
<a href="index.php?pfad=register">
Noch keinen Account? Jetzt hier registrieren!
+48
View File
@@ -0,0 +1,48 @@
<?php
$error = $error ?? null;
$success = $success ?? null;
?>
<main class="login-page">
<div class="login-container">
<h1>Passwort vergessen</h1>
<?php if (!empty($error)): ?>
<p class="alert-message is-error">
<?php echo htmlspecialchars($error); ?>
</p>
<?php endif; ?>
<?php if (!empty($success)): ?>
<p class="alert-message is-success">
<?php echo $success; ?>
</p>
<?php endif; ?>
<form method="post" action="index.php?pfad=password-forgotten">
<p class="input-label">E-Mail-Adresse:</p>
<input type="email"
name="email"
class="login-input"
placeholder="E-Mail-Adresse"
required>
<p class="input-label">Neues Passwort:</p>
<input type="password"
name="password"
class="login-input"
placeholder="Neues Passwort"
required>
<button type="submit"
name="passwordForgottenSubmit"
class="button">
Passwort zurücksetzen
</button>
</form>
</div>
</main>
+3
View File
@@ -17,6 +17,9 @@ if ($pfad === "login") {
if ($pfad === "register") {
include_once "php/controller/register-controller.php";
}
if ($pfad === "password-forgotten") {
include_once "php/controller/password-forgotten-controller.php";
}
if ($pfad === "logout") {
include_once "php/controller/logout-controller.php";
@@ -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.";
}
}
}