Files
webprogrammierung/php/controller/profile-controller.php
T
2026-06-02 22:42:47 +02:00

70 lines
1.9 KiB
PHP

<?php
require_once "php/model/LocalUserDAO.php";
require_once 'php/model/Article.php';
require_once 'php/model/ArticleManager.php';
if (!isset($_SESSION["user"])) {
header("Location: index.php?pfad=login");
exit();
}
$dao = new LocalUserDAO();
$error = null;
try {
$user = $dao->findUser($_SESSION["user_email"] ?? "");
if (!$user) {
$_SESSION = [];
session_destroy();
header("Location: index.php?pfad=login");
exit();
}
if ($_SERVER["REQUEST_METHOD"] === "POST" && isset($_POST["saveProfile"])) {
$oldEmail = $_SESSION["user_email"];
$newEmail = $_POST["email"] ?? "";
$vorname = $_POST["vorname"] ?? "";
$nachname = $_POST["nachname"] ?? "";
$password = $_POST["password"] ?? "";
$existingUser = $dao->findUser($newEmail);
if ($existingUser && $newEmail !== $oldEmail) {
$error = "Diese E-Mail-Adresse wird bereits verwendet.";
} else {
$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.";
}
}
}
$user = $dao->findUser($_SESSION["user_email"] ?? "");
$articleManager = ArticleManager::getInstance();
$userArticles = $articleManager->getArticlesByAuthor($_SESSION["user_email"]);
if (!isset($userArticles)) {
$_SESSION["message"] = "user_has_no_articles";
}
} catch (Exception $e) {
$_SESSION["message"] = "internal_error";
exit();
}