200 lines
4.5 KiB
PHP
200 lines
4.5 KiB
PHP
<?php
|
|
if (session_status() === PHP_SESSION_NONE) {
|
|
session_start();
|
|
}
|
|
|
|
require_once "../model/CommentManager.php";
|
|
require_once "../model/UserManager.php";
|
|
|
|
/**
|
|
* Prüft, ob die Anfrage durch JavaScript per AJAX gesendet wurde.
|
|
*/
|
|
$isAjaxRequest = isset($_SERVER["HTTP_X_REQUESTED_WITH"])
|
|
&& strtolower($_SERVER["HTTP_X_REQUESTED_WITH"]) === "xmlhttprequest";
|
|
|
|
/**
|
|
* Gibt das Ergebnis entweder als JSON zurück oder leitet
|
|
* bei einem normalen Formularaufruf wieder zum Beitrag zurück.
|
|
*
|
|
* @param bool $success War das Speichern erfolgreich?
|
|
* @param string $message Rückmeldung für den Benutzer
|
|
* @param int|null $articleId ID des Beitrags
|
|
* @param array $additionalData Zusätzliche Daten für AJAX
|
|
*/
|
|
function sendCommentResponse(
|
|
$success,
|
|
$message,
|
|
$articleId,
|
|
$additionalData = []
|
|
) {
|
|
global $isAjaxRequest;
|
|
|
|
if ($isAjaxRequest) {
|
|
header("Content-Type: application/json; charset=utf-8");
|
|
|
|
echo json_encode(
|
|
array_merge(
|
|
[
|
|
"success" => $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
|
|
);
|
|
}
|
|
|
|
/*
|
|
* 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
|
|
);
|
|
}
|
|
|
|
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
|
|
);
|
|
}
|
|
|
|
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
|
|
);
|
|
} |