167 lines
7.0 KiB
PHP
167 lines
7.0 KiB
PHP
<?php
|
|
if (session_status() === PHP_SESSION_NONE) {
|
|
session_start();
|
|
}
|
|
|
|
require_once '../model/LocalArticleManager.php';
|
|
require_once '../model/ArticleManager.php';
|
|
require_once '../model/Article.php';
|
|
require_once '../validator/article-validator.php';
|
|
|
|
if ($_SERVER["REQUEST_METHOD"] === "POST") {
|
|
$_SESSION["old_title"] = $_POST["title"] ?? '';
|
|
$_SESSION["old_content"] = $_POST["content"] ?? '';
|
|
$_SESSION["old_category"] = $_POST["category"] ?? ''; // TODO: die Kategorie im Dropdown setzen, wenn der Editor erneut geöffnet wird.
|
|
$_SESSION["old_tags"] = $_POST["tags"] ?? '';
|
|
|
|
if (isset($_GET["id"]) && !empty($_GET["id"])) {
|
|
$id = $_GET["id"];
|
|
} else {
|
|
$_SESSION["message"] = "missing_id";
|
|
header("location: ../../index.php?pfad=updateArticle");
|
|
exit();
|
|
}
|
|
|
|
if (!isset($_POST["title"]) ||!isset($_POST["content"]) || !isset($_POST["category"])){
|
|
$_SESSION["message"] = "missing_parameters";
|
|
header("location: ../../index.php?pfad=updateArticle&id=$id");
|
|
exit();
|
|
}else{
|
|
$title = $_POST["title"];
|
|
$content = $_POST["content"];
|
|
$author = $_SESSION["user_email"];
|
|
$category = $_POST["category"];
|
|
$tags = $_POST['tags'] ?? '';
|
|
|
|
// -------------------------------- Validierung der Daten: -------------------------
|
|
if (!articleTitleValidator($title)) {
|
|
$_SESSION["message"] = "invalid_title";
|
|
header("location: ../../index.php?pfad=updateArticle&id=$id");
|
|
exit();
|
|
}
|
|
|
|
if (!articleContentValidator($content)) {
|
|
$_SESSION["message"] = "invalid_content";
|
|
header("location: ../../index.php?pfad=updateArticle&id=$id");
|
|
exit();
|
|
}
|
|
|
|
if (!articleCategoryValidator($category)) {
|
|
$_SESSION["message"] = "invalid_category";
|
|
header("location: ../../index.php?pfad=updateArticle&id=$id");
|
|
exit();
|
|
}
|
|
|
|
if (!articleTagValidator($tags)) {
|
|
$_SESSION["message"] = "invalid_tags";
|
|
header("location: ../../index.php?pfad=updateArticle&id=$id");
|
|
exit();
|
|
} else {
|
|
$cleanedTags = [];
|
|
$rawTags = explode(',', $tags);
|
|
foreach ($rawTags as $rawTag) {
|
|
// Leerzeichen am Anfang/Ende des einzelnen Tags entfernen:
|
|
$tag = trim($rawTag);
|
|
$cleanedTags[] = $tag;
|
|
}
|
|
// Duplikate entfernen:
|
|
$cleanedTags = array_unique($cleanedTags);
|
|
$cleanedTags = implode(',', $cleanedTags);
|
|
}
|
|
|
|
// --------------------------------------- Base64-Bilder speichern ---------------------------------------------
|
|
$blocks = json_decode($content, true);
|
|
$uploadDir = __DIR__ . '/../../uploads/';
|
|
|
|
if (!file_exists($uploadDir)) {
|
|
mkdir($uploadDir, 0755, true);
|
|
}
|
|
|
|
// ----------------- Gelöschte Bilder über die JS-Löschliste entfernen -----------------
|
|
if (isset($_POST['deleted_images'])) {
|
|
$deletedImages = json_decode($_POST['deleted_images'], true);
|
|
|
|
// Wir ermitteln den physisch echten, absoluten Pfad zum uploads-Ordner auf der Festplatte
|
|
$uploadDir = realpath(__DIR__ . '/../../uploads') . DIRECTORY_SEPARATOR;
|
|
|
|
if (is_array($deletedImages)) {
|
|
foreach ($deletedImages as $imagePath) {
|
|
// Nur den reinen Dateinamen heraustrennen (z.B. img_65a123.jpg)
|
|
$filename = basename($imagePath);
|
|
$fullDeletePath = $uploadDir . $filename;
|
|
|
|
// Debugging & Löschen:
|
|
if (file_exists($fullDeletePath)) {
|
|
// Versuchen zu löschen. Wenn es fehlschlägt, Fehlermeldung erzwingen
|
|
if (!@unlink($fullDeletePath)) {
|
|
$error = error_get_last();
|
|
die("Datei existiert, aber PHP darf sie nicht löschen! Grund: " . $error['message']);
|
|
}
|
|
} else {
|
|
// Wenn PHP die Datei an diesem Pfad nicht findet, brechen wir zum Debuggen ab
|
|
// die("PHP findet die Datei nicht unter dem Pfad: " . $fullDeletePath);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// ----------------------- NEU hinzugefügte Base64-Bilder: --------------------------
|
|
if (is_array($blocks)) {
|
|
foreach ($blocks as &$block) {
|
|
// Prüfen, ob der Block ein Bild ist und ein NEUES Bild (Base64-Format) enthält
|
|
if (isset($block['type']) && isset($block['value']) && $block['type'] === 'image' && is_string($block['value'])) {
|
|
|
|
if (str_starts_with($block['value'], 'data:image/')) {
|
|
$parts = explode(',', $block['value']);
|
|
if (count($parts) >= 2) {
|
|
$metadata = $parts[0];
|
|
$base64Data = $parts[1];
|
|
|
|
preg_match('/data:image\/(?<extension>.*?);/', $metadata, $matches);
|
|
$extension = $matches['extension'] ?? 'jpg';
|
|
if ($extension === 'jpeg') { $extension = 'jpg'; }
|
|
|
|
$fileName = 'img_' . uniqid() . '.' . $extension;
|
|
$filePath = $uploadDir . $fileName;
|
|
|
|
if (file_put_contents($filePath, base64_decode($base64Data)) !== false) {
|
|
$block['value'] = 'uploads/' . $fileName;
|
|
} else {
|
|
$_SESSION["message"] = "image_upload_error";
|
|
header("location: ../../index.php?pfad=updateArticle&id=$id");
|
|
exit();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
unset($block);
|
|
}
|
|
|
|
// Aktualisiertes Array wieder in JSON konvertieren
|
|
$finalContent = json_encode($blocks, JSON_UNESCAPED_UNICODE);
|
|
|
|
// ----------------- Übertragung der validierten Daten in ArticleManager: ---------------------------
|
|
try {
|
|
$articleManager = ArticleManager::getInstance();
|
|
$article = $articleManager->getArticle($id);
|
|
$article->setTitle($title);
|
|
$article->setContent($finalContent);
|
|
$article->setCategory($category);
|
|
$article->setTags($cleanedTags);
|
|
$articleManager->updateArticle($id ,$article, $author);
|
|
|
|
unset($_SESSION["old_title"], $_SESSION["old_content"], $_SESSION["old_category"], $_SESSION["old_tags"]);
|
|
|
|
} catch (\Throwable $e){
|
|
$_SESSION["message"] = $e->getMessage();
|
|
header("location: ../../index.php?pfad=updateArticle&id=$id");
|
|
exit();
|
|
}
|
|
$_SESSION["message"] = "article_updated";
|
|
// Weiterleitung zur Homepage
|
|
header("location: ../../index.php?pfad=showArticle&id=$id");
|
|
}
|
|
}
|
|
|
|
?>
|