getArticle($id); $oldBlocks = json_decode($oldArticle->getContent(), true); if (is_array($oldBlocks) && is_array($blocks)) { $oldImages = []; $newImages = []; // Alle Bildpfade aus dem alten Zustand sammeln foreach ($oldBlocks as $oldBlock) { if (isset($oldBlock['type'], $oldBlock['value']) && $oldBlock['type'] === 'image' && !str_starts_with($oldBlock['value'], 'data:image/')) { $oldImages[] = $oldBlock['value']; } } // Alle Bildpfade aus dem neuen Zustand sammeln foreach ($blocks as $newBlock) { if (isset($newBlock['type'], $newBlock['value']) && $newBlock['type'] === 'image' && !str_starts_with($newBlock['value'], 'data:image/')) { $newImages[] = $newBlock['value']; } } // Differenz ermitteln: Welche Pfade waren alt da, sind neu aber weg? $deletedImages = array_diff($oldImages, $newImages); // Diese Dateien physisch vom Server löschen foreach ($deletedImages as $imagePath) { // Extra-Sicherheit: Nur Dateien im eigenen Uploads-Ordner löschen $filename = basename($imagePath); $fullDeletePath = $uploadDir . $filename; if (file_exists($fullDeletePath) && is_file($fullDeletePath)) { unlink($fullDeletePath); } } } } catch (\Throwable $e) { $_SESSION["message"] = $e->getMessage(); header("location: ../../index.php?pfad=updateArticle&id=$id"); exit(); } // --- 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\/(?.*?);/', $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"); } } ?>