151 lines
3.9 KiB
PHP
151 lines
3.9 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Prüft, ob der Titel die folgenden Bedingungen erfüllt:
|
|
* Buchstaben von a-z; A-Z
|
|
* Zahlen von 0-9
|
|
* Umlaute äöüÄÖÜß
|
|
* Satzeichen .,!?:;()"„“«»_+-
|
|
* 5-120 Zeichen
|
|
* @param $title
|
|
* @return bool
|
|
*/
|
|
function articleTitleValidator($title)
|
|
{
|
|
$title = trim($title);
|
|
$titlePattern = '/^[a-zA-Z0-9äöüÄÖÜß\s.,!?:;()\'"„“«»_+-]{5,120}$/u';
|
|
if (preg_match($titlePattern, $title)) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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)
|
|
{
|
|
// 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;
|
|
}
|
|
|
|
/**
|
|
* Prüft, ob die Kategorie eine erlaubt Kategorie ist.
|
|
* @param $category
|
|
* @return bool
|
|
*/
|
|
function articleCategoryValidator($category)
|
|
{
|
|
$allowedCategories = [
|
|
'deutsch', 'englisch', 'franzoesisch', 'latein', 'literatur',
|
|
'mathe', 'biologie', 'chemie', 'physik', 'informatik', 'astronomie',
|
|
'geschichte', 'erdkunde', 'sozialkunde', 'wirtschaft', 'religion',
|
|
'ethik', 'philosophie', 'psychologie', 'kunst', 'musik', 'theater',
|
|
'technik', 'werken', 'hauswirtschaft', 'sport'
|
|
];
|
|
if (in_array($category, $allowedCategories, true)) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Prüft, ob die Tags die folgenden Bedingungen erfüllen:
|
|
* Buchstaben von a-z; A-Z
|
|
* Zahlen von 0-9
|
|
* Umlaute äöüÄÖÜß
|
|
* Satzeichen -
|
|
* 2-50 Zeichen
|
|
* @param $tags
|
|
* @return bool
|
|
*/
|
|
function articleTagValidator($tags)
|
|
{
|
|
if (!isset($tags)) {
|
|
$tags = '';
|
|
}
|
|
|
|
$rawTags = explode(',', $tags);
|
|
|
|
foreach ($rawTags as $rawTag) {
|
|
// Leerzeichen am Anfang/Ende des einzelnen Tags entfernen:
|
|
$tag = trim($rawTag);
|
|
|
|
// leere Elemente überspringen:
|
|
if ($tag === '') {
|
|
continue;
|
|
}
|
|
|
|
// Tag mit Regex prüfen:
|
|
$tagPattern = '/^[a-zA-Z0-9äöüÄÖÜß\s-]{2,50}$/u';
|
|
if (!preg_match($tagPattern, $tag)) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
?>
|