$success, "message" => $message ], $additionalData ) ); exit(); } /* * Bei deaktiviertem JavaScript wird die Rückmeldung * in der Session gespeichert und die Beitragsseite neu geladen. */ $_SESSION["comment_message"] = $message; $_SESSION["comment_message_type"] = $success ? "success" : "error"; if ($articleId !== null) { header( "Location: ../../index.php?pfad=showArticle&id=" . urlencode((string) $articleId) . "#comments" ); } else { header("Location: ../../index.php"); } exit(); } /* * Nur POST-Anfragen dürfen Kommentare erstellen. */ if ($_SERVER["REQUEST_METHOD"] !== "POST") { sendCommentResponse( false, "Ungültige Anfrage.", null ); } /* * Die Beitrags-ID wird zuerst eingelesen, * damit bei Fehlern wieder zum Beitrag zurückgeleitet werden kann. */ $articleId = filter_input( INPUT_POST, "article_id", FILTER_VALIDATE_INT ); /* * Ein Benutzer muss angemeldet sein. */ if (!isset($_SESSION["user_email"])) { sendCommentResponse( false, "Du musst angemeldet sein, um zu kommentieren.", $articleId !== false ? $articleId : null ); } /* * CSRF-Token prüfen, bevor irgendeine Änderung vorgenommen wird. */ if (!csrf_verify()) { sendCommentResponse( false, "Deine Sitzung ist abgelaufen. Bitte lade die Seite neu und versuche es erneut.", $articleId !== false ? $articleId : null ); } /* * Weitere Formulardaten einlesen. */ $content = trim($_POST["content"] ?? ""); $parentCommentId = filter_input( INPUT_POST, "parent_comment_id", FILTER_VALIDATE_INT ); /* * Ein leerer Wert bedeutet, dass es sich um einen * normalen Hauptkommentar handelt. */ if ( !isset($_POST["parent_comment_id"]) || $_POST["parent_comment_id"] === "" || $_POST["parent_comment_id"] === "0" ) { $parentCommentId = null; } if ($articleId === false || $articleId === null) { sendCommentResponse( false, "Der zugehörige Beitrag ist ungültig.", null ); } /* * Der Beitrag muss tatsächlich existieren. */ $existingArticle = ArticleManager::getInstance()->getArticle($articleId); if ($existingArticle === null) { sendCommentResponse( false, "Der zugehörige Beitrag wurde nicht gefunden.", null ); } if ($content === "") { sendCommentResponse( false, "Der Kommentar darf nicht leer sein.", $articleId ); } /* * Eine ungültige Eltern-ID darf nicht gespeichert werden. */ if ( isset($_POST["parent_comment_id"]) && $_POST["parent_comment_id"] !== "" && $_POST["parent_comment_id"] !== "0" && $parentCommentId === false ) { sendCommentResponse( false, "Der ausgewählte Kommentar ist ungültig.", $articleId ); } /* * Falls eine Eltern-ID angegeben wurde, muss dieser Kommentar * tatsächlich existieren und zum selben Beitrag gehören. */ if ($parentCommentId !== null) { $existingComments = CommentManager::getInstance()->getCommentsByArticle($articleId); $parentExists = false; foreach ($existingComments as $existingComment) { if ($existingComment->getId() === $parentCommentId) { $parentExists = true; break; } } if (!$parentExists) { sendCommentResponse( false, "Der ausgewählte Kommentar wurde nicht gefunden.", $articleId ); } } try { $commentManager = CommentManager::getInstance(); $commentId = $commentManager->addComment( $articleId, $_SESSION["user_email"], $content, $parentCommentId ); $userManager = UserManager::getInstance(); $user = $userManager->findUser($_SESSION["user_email"]); $authorName = $_SESSION["user_email"]; if ($user !== null) { $vorname = trim($user["vorname"] ?? ""); $nachname = trim($user["nachname"] ?? ""); $fullName = trim($vorname . " " . $nachname); if ($fullName !== "") { $authorName = $fullName; } } sendCommentResponse( true, "Der Kommentar wurde erfolgreich gespeichert.", $articleId, [ "commentId" => $commentId, "author" => $authorName, "content" => $content, "created" => date("Y-m-d H:i:s"), "parentCommentId" => $parentCommentId ] ); } catch (Throwable $e) { sendCommentResponse( false, "Der Kommentar konnte nicht gespeichert werden.", $articleId ); }