From d4020671c1ebf68ed9ce75869368c1365f38762c Mon Sep 17 00:00:00 2001 From: NOrtmann1 Date: Sun, 14 Jun 2026 21:39:09 +0200 Subject: [PATCH] updateArticle --- content/updateArticle.php | 24 +++++++++- php/controller/updateArticle-controller.php | 50 ++++++++++++++++++++- 2 files changed, 71 insertions(+), 3 deletions(-) diff --git a/content/updateArticle.php b/content/updateArticle.php index 1cd6801..bb69e0d 100644 --- a/content/updateArticle.php +++ b/content/updateArticle.php @@ -26,8 +26,30 @@ include_once 'php/controller/showArticle-controller.php'; ?>" placeholder="Titel hier eingeben" required> - + +
+ + +
+ + +
+ + + diff --git a/php/controller/updateArticle-controller.php b/php/controller/updateArticle-controller.php index cc881fd..07a6f08 100644 --- a/php/controller/updateArticle-controller.php +++ b/php/controller/updateArticle-controller.php @@ -75,16 +75,62 @@ if ($_SERVER["REQUEST_METHOD"] === "POST") { $cleanedTags = implode(',', $cleanedTags); } + // ----------------- Base64-Bilder speichern ----------------- + $blocks = json_decode($content, true); + $uploadDir = __DIR__ . '/../../uploads/'; + + if (!file_exists($uploadDir)) { + mkdir($uploadDir, 0755, true); + } + + 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\/(?.*?);/', $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($content); - $article->setCategory($category); + $article->setCategory($finalContent); $article->setTags($cleanedTags); $articleManager->updateArticle($id ,$article, $author); - } catch (Exception $e){ + + 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();