Merge branch 'erweiterterEditor' into dev
This commit is contained in:
@@ -24,12 +24,6 @@ require_once '../validator/article-validator.php';
|
||||
$tags = $_POST['tags'] ?? '';
|
||||
|
||||
// -------------------------------- Validierung der Daten: -------------------------
|
||||
if (!articleAuthorValidator($author)) {
|
||||
$_SESSION["message"] = "author_not_valid";
|
||||
header("location: ../../index.php?pfad=createArticle");
|
||||
exit();
|
||||
}
|
||||
|
||||
if (!articleTitleValidator($title)) {
|
||||
$_SESSION["message"] = "invalid_title";
|
||||
header("location: ../../index.php?pfad=createArticle");
|
||||
@@ -64,6 +58,59 @@ require_once '../validator/article-validator.php';
|
||||
$cleanedTags = array_unique($cleanedTags);
|
||||
$cleanedTags = implode(',', $cleanedTags);
|
||||
}
|
||||
|
||||
// ----------------- Base64-Bilder verarbeiten und auf Server 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) {
|
||||
// sicherstellen, dass 'type' und 'value' existieren:
|
||||
if (isset($block['type']) && isset($block['value']) && $block['type'] === 'image' && str_starts_with($block['value'], 'data:image/')) {
|
||||
|
||||
// Base64-String zerlegen
|
||||
$parts = explode(',', $block['value']);
|
||||
|
||||
// falls der String korrupt ist und kein Komma hat
|
||||
if (count($parts) < 2) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$metadata = $parts[0];
|
||||
$base64Data = $parts[1];
|
||||
|
||||
// Dateiendung ermitteln
|
||||
preg_match('/data:image\/(?<extension>.*?);/', $metadata, $matches);
|
||||
$extension = $matches['extension'] ?? 'jpg';
|
||||
if ($extension === 'jpeg') {
|
||||
$extension = 'jpg';
|
||||
}
|
||||
|
||||
// Eindeutigen Dateinamen generieren
|
||||
$fileName = 'img_' . uniqid() . '.' . $extension;
|
||||
$filePath = $uploadDir . $fileName;
|
||||
|
||||
// Datei im /uploads speichern:
|
||||
if (file_put_contents($filePath, base64_decode($base64Data)) !== false) {
|
||||
// temporären Base64-String durch den echten Pfad ersetzen
|
||||
$block['value'] = 'uploads/' . $fileName;
|
||||
} else {
|
||||
$_SESSION["message"] = "image_upload_error";
|
||||
header("location: ../../index.php?pfad=createArticle");
|
||||
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();
|
||||
@@ -72,7 +119,7 @@ require_once '../validator/article-validator.php';
|
||||
// Formulardaten nach erfolgreichem Erstellen aus der Session löschen
|
||||
unset($_SESSION["old_title"], $_SESSION["old_content"], $_SESSION["old_category"], $_SESSION["old_tags"]);
|
||||
|
||||
} catch (Exception $e){
|
||||
} catch (\Throwable $e){
|
||||
$_SESSION["message"] = "internal_error";
|
||||
header("location: ../../index.php?pfad=createArticle");
|
||||
exit();
|
||||
@@ -84,7 +131,4 @@ require_once '../validator/article-validator.php';
|
||||
exit();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
?>
|
||||
@@ -34,12 +34,6 @@ if ($_SERVER["REQUEST_METHOD"] === "POST") {
|
||||
$tags = $_POST['tags'] ?? '';
|
||||
|
||||
// -------------------------------- Validierung der Daten: -------------------------
|
||||
if (!articleAuthorValidator($author)) {
|
||||
$_SESSION["message"] = "author_not_valid";
|
||||
header("location: ../../index.php?pfad=updateArticle&id=$id");
|
||||
exit();
|
||||
}
|
||||
|
||||
if (!articleTitleValidator($title)) {
|
||||
$_SESSION["message"] = "invalid_title";
|
||||
header("location: ../../index.php?pfad=updateArticle&id=$id");
|
||||
@@ -75,16 +69,91 @@ 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);
|
||||
}
|
||||
|
||||
// ----------------- Gelöschte Bilder über die JS-Löschliste entfernen ----------------- TODO: 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($content);
|
||||
$article->setContent($finalContent);
|
||||
$article->setCategory($category);
|
||||
$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();
|
||||
|
||||
@@ -145,9 +145,17 @@ class ArticleManager
|
||||
// Verteilt die 10 Autoren gleichmäßig (ID 1 -> Autor 1, ID 10 -> Autor 10, ID 11 -> Autor 1)
|
||||
$authorEmail = $authors[($id - 1) % 10];
|
||||
|
||||
$blockStructure = [
|
||||
[
|
||||
'type' => 'text',
|
||||
'value' => $data[1] // Der originale Text aus dem Dummy-Array
|
||||
]
|
||||
];
|
||||
$jsonContent = json_encode($blockStructure, JSON_UNESCAPED_UNICODE);
|
||||
|
||||
$articleManager->addArticle(
|
||||
$data[0], // Titel
|
||||
$data[1], // Inhalt
|
||||
$jsonContent, // Inhalt
|
||||
$authorEmail, // Rotierende Autoren-E-Mail
|
||||
$data[2], // Kategorie
|
||||
$data[3] // Tags
|
||||
|
||||
@@ -1,16 +1,5 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Prüft, ob der Autor auch der Eigentümer des Beitrags ist.
|
||||
* @param $author
|
||||
* @return true
|
||||
* TODO: Implement this.
|
||||
*/
|
||||
function articleAuthorValidator($author)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prüft, ob der Titel die folgenden Bedingungen erfüllt:
|
||||
* Buchstaben von a-z; A-Z
|
||||
@@ -33,19 +22,73 @@ function articleTitleValidator($title)
|
||||
}
|
||||
|
||||
/**
|
||||
* Prüft, ob der Contenttext 10-7000 Zeichen enthält.
|
||||
* @param $content
|
||||
* Prüft, ob der Content valides JSON ist UND ob alle enthaltenen Blöcke
|
||||
* die inhaltlichen Kriterien erfüllen:
|
||||
* Textblöcke sind nicht leer
|
||||
* Bilder sind in Bildblöcken vorhanden
|
||||
*
|
||||
* @param mixed $content Der zu prüfende JSON-String
|
||||
* @return bool
|
||||
*/
|
||||
function articleContentValidator($content)
|
||||
{
|
||||
$content = trim($content);
|
||||
$zeichenAnzahl = mb_strlen($content);
|
||||
if ($zeichenAnzahl <= 7000 && $zeichenAnzahl >= 10) {
|
||||
return true;
|
||||
}else{
|
||||
// 1. Grundlegende Typprüfung
|
||||
if (!is_string($content)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 2. Formale JSON-Prüfung (Kompatibel mit PHP 8.2)
|
||||
$blocks = json_decode($content, true);
|
||||
if (json_last_error() !== JSON_ERROR_NONE) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 3. Inhaltliche Validierung der einzelnen Blöcke
|
||||
// Falls das JSON zwar valide, aber kein Array ist (z.B. nur ein String/Zahl)
|
||||
if (!is_array($blocks)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Mindestens ein Block sollte vorhanden sein (optional, verhindert leere Beiträge)
|
||||
if (empty($blocks)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ($blocks as $block) {
|
||||
// Jeder Block muss die Keys 'type' und 'value' besitzen
|
||||
if (!isset($block['type']) || !isset($block['value'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$type = $block['type'];
|
||||
$value = $block['value'];
|
||||
|
||||
if ($type === 'text') {
|
||||
// Validierung für Text: Darf nach dem Trimmen nicht leer sein
|
||||
if (trim($value) === '') {
|
||||
return false;
|
||||
}
|
||||
} elseif ($type === 'image') {
|
||||
// Validierung für Bild: Muss entweder mit uploads/ starten (Bestand)
|
||||
// oder mit data:image/ beginnen (neues Base64-Bild aus dem Editor)
|
||||
if (!is_string($value)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$isValidPath = str_starts_with($value, 'uploads/');
|
||||
$isValidBase64 = str_starts_with($value, 'data:image/');
|
||||
|
||||
if (!$isValidPath && !$isValidBase64) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
// Unbekannter Blocktyp wird zur Sicherheit abgewiesen
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Wenn alle Prüfungen bestanden wurden
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user