From cd50840b194c93b91b54e7a54e8429a8f3fa60e2 Mon Sep 17 00:00:00 2001 From: NOrtmann1 Date: Sun, 19 Jul 2026 16:05:05 +0200 Subject: [PATCH] Update add-comment.php --- php/ajax/add-comment.php | 49 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/php/ajax/add-comment.php b/php/ajax/add-comment.php index a985aeb..b71f5c4 100644 --- a/php/ajax/add-comment.php +++ b/php/ajax/add-comment.php @@ -4,6 +4,8 @@ if (session_status() === PHP_SESSION_NONE) { } require_once "../model/CommentManager.php"; +require_once "../model/ArticleManager.php"; +require_once "../../includes/csrf.php"; /** * Prüft, ob die Anfrage durch JavaScript per AJAX gesendet wurde. @@ -96,6 +98,17 @@ if (!isset($_SESSION["user_email"])) { ); } +/* + * 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. */ @@ -128,6 +141,18 @@ if ($articleId === false || $articleId === 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, @@ -152,6 +177,30 @@ if ( ); } +/* + * 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();