Update article-validator.php

This commit is contained in:
2026-06-14 21:59:19 +02:00
parent b9c28968fe
commit 8c1e1a64bf
+59 -5
View File
@@ -33,19 +33,73 @@ function articleTitleValidator($title)
} }
/** /**
* Prüft, ob der übergebene Content ein formal valider JSON-String ist. * Prüft, ob der Content valides JSON ist UND ob alle enthaltenen Blöcke
* @param mixed $content Der zu prüfende Inhalt * 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 * @return bool
*/ */
function articleContentValidator($content) function articleContentValidator($content)
{ {
// 1. Grundlegende Typprüfung
if (!is_string($content)) { if (!is_string($content)) {
return false; return false;
} }
// Prüft direkt, ob der String valides JSON enthält // 2. Formale JSON-Prüfung (Kompatibel mit PHP 8.2)
json_decode($content); $blocks = json_decode($content, true);
return json_last_error() === JSON_ERROR_NONE; 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;
} }
/** /**