Reviewnacharbeitung

This commit is contained in:
2026-06-03 20:07:57 +02:00
parent 92574a0f76
commit bfc9ee4d01
3 changed files with 176 additions and 28 deletions
+63 -24
View File
@@ -4,15 +4,44 @@ require_once "php/model/LocalUserDAO.php";
require_once 'php/model/Article.php';
require_once 'php/model/ArticleManager.php';
$error = null;
if (!isset($_SESSION["user"])) {
header("Location: index.php?pfad=login");
exit();
}
$dao = new LocalUserDAO();
$error = null;
/*
* Prüft Vor- und Nachnamen.
* Erlaubt sind Buchstaben, Umlaute, Leerzeichen und Bindestriche.
*/
function isValidName($name): bool {
return preg_match("/^[a-zA-ZäöüÄÖÜß -]{2,50}$/", $name);
}
/*
* Prüft, ob die E-Mail-Adresse ein gültiges Format hat.
*/
function isValidEmailAddress($email): bool {
return filter_var($email, FILTER_VALIDATE_EMAIL) !== false
&& strlen($email) <= 100;
}
/*
* Prüft das neue Passwort.
* Leeres Passwort ist erlaubt, wenn der Nutzer es nicht ändern möchte.
*/
function isValidProfilePassword($password): bool {
if ($password === "") {
return true;
}
return strlen($password) >= 8 && strlen($password) <= 72;
}
try {
$dao = new LocalUserDAO();
$user = $dao->findUser($_SESSION["user_email"] ?? "");
if (!$user) {
@@ -25,32 +54,43 @@ try {
if ($_SERVER["REQUEST_METHOD"] === "POST" && isset($_POST["saveProfile"])) {
$oldEmail = $_SESSION["user_email"];
$newEmail = $_POST["email"] ?? "";
$vorname = $_POST["vorname"] ?? "";
$nachname = $_POST["nachname"] ?? "";
$newEmail = trim($_POST["email"] ?? "");
$vorname = trim($_POST["vorname"] ?? "");
$nachname = trim($_POST["nachname"] ?? "");
$password = $_POST["password"] ?? "";
$existingUser = $dao->findUser($newEmail);
if ($existingUser && $newEmail !== $oldEmail) {
$error = "Diese E-Mail-Adresse wird bereits verwendet.";
if (!isValidName($vorname)) {
$error = "Der Vorname darf nur Buchstaben, Leerzeichen und Bindestriche enthalten und muss 2 bis 50 Zeichen lang sein.";
} elseif (!isValidName($nachname)) {
$error = "Der Nachname darf nur Buchstaben, Leerzeichen und Bindestriche enthalten und muss 2 bis 50 Zeichen lang sein.";
} elseif (!isValidEmailAddress($newEmail)) {
$error = "Bitte gib eine gültige E-Mail-Adresse ein.";
} elseif (!isValidProfilePassword($password)) {
$error = "Das Passwort muss mindestens 8 Zeichen lang sein.";
} else {
$updated = $dao->updateUser(
$oldEmail,
$newEmail,
$vorname,
$nachname,
$password
);
$existingUser = $dao->findUser($newEmail);
if ($updated) {
$_SESSION["user"] = $vorname . " " . $nachname;
$_SESSION["user_email"] = $newEmail;
header("Location: index.php?pfad=profile");
exit();
if ($existingUser && $newEmail !== $oldEmail) {
$error = "Diese E-Mail-Adresse wird bereits verwendet.";
} else {
$error = "Die Daten konnten nicht gespeichert werden.";
$updated = $dao->updateUser(
$oldEmail,
$newEmail,
$vorname,
$nachname,
$password
);
if ($updated) {
$_SESSION["user"] = $vorname . " " . $nachname;
$_SESSION["user_email"] = $newEmail;
header("Location: index.php?pfad=profile");
exit();
} else {
$error = "Die Daten konnten nicht gespeichert werden.";
}
}
}
}
@@ -66,5 +106,4 @@ try {
} catch (Exception $e) {
$_SESSION["message"] = "internal_error";
exit();
}