Files
webprogrammierung/php/controller/deleteArticle-controller.php
T

56 lines
1.4 KiB
PHP

<?php
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
require_once __DIR__ . "/../model/ArticleManager.php";
require_once __DIR__ . "/../../includes/csrf.php";
if (!isset($_SESSION["user"])) {
header("Location: index.php?pfad=login");
exit();
}
if ($_SERVER["REQUEST_METHOD"] === "POST") {
// CSRF-Token prüfen, bevor irgendeine Änderung vorgenommen wird
if (!csrf_verify()) {
$_SESSION["message"] = "invalid_csrf_token";
header("location: ../../index.php?pfad=profile");
exit();
}
if (isset($_SESSION["user_email"])) {
$user = $_SESSION["user_email"];
} else {
$_SESSION = [];
session_destroy();
header("Location: ../../index.php");
exit();
}
// Die Beitrags-ID muss eine gültige numerische ID sein.
$id = filter_input(INPUT_POST, "id", FILTER_VALIDATE_INT);
if ($id === false || $id === null) {
$_SESSION["message"] = "missing_id";
header("location: ../../index.php?pfad=profile");
exit();
}
try {
$articleManager = ArticleManager::getInstance();
$articleManager->deleteArticle($id, $user);
} catch (\Exception $e) {
$_SESSION["message"] = $e->getMessage();
header("location: ../../index.php?pfad=profile");
exit();
}
$_SESSION["message"] = "article_deleted";
header("location: ../../index.php?pfad=profile");
exit();
}