From d5f30176c4ed71bbc863b60de8c78e5a10691822 Mon Sep 17 00:00:00 2001 From: NOrtmann1 Date: Mon, 1 Jun 2026 13:41:22 +0200 Subject: [PATCH] article-validator.php --- php/controller/updateArticle-controller.php | 135 ++++++++------------ php/validator/article-validator.php | 104 +++++++++++++++ 2 files changed, 154 insertions(+), 85 deletions(-) create mode 100644 php/validator/article-validator.php diff --git a/php/controller/updateArticle-controller.php b/php/controller/updateArticle-controller.php index 807a45d..086de7e 100644 --- a/php/controller/updateArticle-controller.php +++ b/php/controller/updateArticle-controller.php @@ -3,6 +3,7 @@ session_start(); require_once '../model/LocalArticleManager.php'; require_once '../model/ArticleManager.php'; require_once '../model/Article.php'; +require_once '../validator/article-validator.php'; if ($_SERVER["REQUEST_METHOD"] === "POST") { $_SESSION["old_title"] = $_POST["title"] ?? ''; @@ -19,111 +20,75 @@ if ($_SERVER["REQUEST_METHOD"] === "POST") { } elseif(!isset($id)) { $_SESSION["message"] = "missing_id"; }else{ - - // ------------------------ Validierung des Autors: ---------------------------- - $author = "max.mustermann"; // TODO: später aus Session den angemeldeten Nutzer beziehen. - $validatedAuthor = $author; - - // --------------------- Eingabevalidierung des Titels: ------------------------- $title = $_POST["title"]; - // Mit Regex prüfen: - $titlePattern = '/^[a-zA-Z0-9äöüÄÖÜß\s.,!?:;()\'"„“«»_+-]{5,120}$/u'; //Erlaubt: Buchstaben, Zahlen, Standardsatzzeichen; 5-120 Zeichen - if (preg_match($titlePattern, $title)) { - $validatedTitle = $title; - } else { - $_SESSION["message"] = "invalid_title"; - header("location: ../../index.php?pfad=createArticle"); - exit(); - } - if (!preg_match('/^[a-zA-Z0-9äöüÄÖÜß\s.,!?:;()\'"„“«»_+-]{5,120}$/u', $title)) { - $errors['title'] = "Der Titel enthält ungültige Zeichen oder erfüllt die Länge von 5-120 Zeichen nicht."; - } - - // --------------------- Eingabeüberprüfung des Contents: ----------------------- $content = $_POST["content"]; - $zeichenAnzahl = mb_strlen($content); - if ($zeichenAnzahl <= 7000 && $zeichenAnzahl > 10) { - $validatedContent = $content; - }else{ - $_SESSION["message"] = "invalid_content"; - header("location: ../../index.php?pfad=createArticle"); - exit(); - } - - // --------------------- -Eingabevalidierung der Kategorie: -------------------- + $author = "max.mustermann"; // TODO: später aus Session den angemeldeten Nutzer beziehen. $category = $_POST["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)) { - $_SESSION["message"] = "invalid_category"; - header("location: ../../index.php?pfad=createArticle"); - exit(); - } - $validatedCategory = $category; - - // -------------------------- Eingabevalidierung der tags: ---------------------- if (isset($_POST['tags'])) { $tags = $_POST['tags']; } else { $tags = ''; } - $validatedTags = []; - $rawTags = explode(',', $tags); // String mit Kommas in array... - 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'; //Erlaubt: Buchstaben, Zahlen, Bindestriche, Leerzeichen; 2-50 Zeichen - if (preg_match($tagPattern, $tag)) { - $validatedTags[] = $tag; - } else { - $_SESSION["message"] = "invalid_tags"; - header("location: ../../index.php?pfad=createArticle"); - exit(); - } + // -------------------------------- Validierung der Daten: ------------------------- + if (!articleAuthorValidator($author)) { + $_SESSION["message"] = "author_not_valid"; + header("location: ../../index.php?pfad=createArticle"); + exit(); } - // Duplikate entfernen: - $validatedTags = array_unique($validatedTags); - $validatedTags = implode(',', $validatedTags); - // ----------------- Übertragung der validierten Daten in ArticleManager: --------------------------- - if (!isset($validatedTitle) || !isset($validatedContent) || !isset($validatedAuthor) || !isset($validatedCategory) || !isset($validatedTags)) { - $_SESSION["message"] = "validation_missing"; + if (!articleTitleValidator($title)) { + $_SESSION["message"] = "invalid_title"; + header("location: ../../index.php?pfad=createArticle"); + exit(); + } + + if (!articleContentValidator($content)) { + $_SESSION["message"] = "invalid_content"; + header("location: ../../index.php?pfad=createArticle"); + exit(); + } + + if (!articleCategoryValidator($category)) { + $_SESSION["message"] = "invalid_category"; + header("location: ../../index.php?pfad=createArticle"); + exit(); + } + + if (!articleTagValidator($_POST["tags"])) { + $_SESSION["message"] = "invalid_tags"; header("location: ../../index.php?pfad=createArticle"); exit(); } else { - try { - $articleManager = ArticleManager::getInstance(); - $article = $articleManager->getArticle($id); - $article->setTitle($validatedTitle); - $article->setContent($validatedContent); - $article->setCategory($validatedCategory); - $article->setTags($validatedTags); - $articleManager->updateArticle($id ,$article, $validatedAuthor); - } catch (Exception $e){ - $_SESSION["message"] = "internal_error"; - header("location: ../../index.php?pfad=createArticle"); - exit(); + $cleanedTags = []; + $rawTags = explode(',', $tags); + foreach ($rawTags as $rawTag) { + // Leerzeichen am Anfang/Ende des einzelnen Tags entfernen: + $tag = trim($rawTag); + // Duplikate entfernen: + $cleanedTags = array_unique($cleanedTags); + $cleanedTags = implode(',', $cleanedTags); } } + + // ----------------- Übertragung der validierten Daten in ArticleManager: --------------------------- + try { + $articleManager = ArticleManager::getInstance(); + $article = $articleManager->getArticle($id); + $article->setTitle($title); + $article->setContent($content); + $article->setCategory($category); + $article->setTags($cleanedTags); + $articleManager->updateArticle($id ,$article, $author); + } catch (Exception $e){ + $_SESSION["message"] = "internal_error"; + header("location: ../../index.php?pfad=createArticle"); + exit(); + } $_SESSION["message"] = "article_updated"; // Weiterleitung zur Homepage header("location: ../../index.php?pfad=showArticle&id=$id"); - } - exit(); } ?> \ No newline at end of file diff --git a/php/validator/article-validator.php b/php/validator/article-validator.php new file mode 100644 index 0000000..2eb1dbc --- /dev/null +++ b/php/validator/article-validator.php @@ -0,0 +1,104 @@ + 10) { + return true; + }else{ + return false; + } +} + +/** + * 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; +} + +?> \ No newline at end of file