diff --git a/content/createArticle.php b/content/createArticle.php index 381c85a..3e95141 100644 --- a/content/createArticle.php +++ b/content/createArticle.php @@ -18,6 +18,31 @@ session_start(); Jeder Beitrag muss einen Titel, Kategorie und Inhalt besitzen.
+ + + + + + + + + + + + + + + diff --git a/php/controller/createArticle-controller.php b/php/controller/createArticle-controller.php index 375d136..5896928 100644 --- a/php/controller/createArticle-controller.php +++ b/php/controller/createArticle-controller.php @@ -7,25 +7,101 @@ if ($_SERVER["REQUEST_METHOD"] === "POST") { if(!isset($_POST["title"]) ||!isset($_POST["content"]) || !isset($_POST["category"])){ $_SESSION["message"] = "missing_parameters"; header("location: ../../index.php?pfad=createArticle"); - } else { - - $title = $_POST["title"]; - $content = $_POST["content"]; - $category = $_POST["category"]; - $author = "max.mustermann"; // TODO: später aus Session den angemeldeten Nutzer beziehen. - $tags = $_POST["tags"]; - - try { - $articleManager = ArticleManager::getInstance(); - $articleManager->addArticle($title, $content, $author, $category, $tags); - } catch (Exception $e){ - $_SESSION["message"] = "internal_error"; - } - $_SESSION["message"] = "new_article"; - // Weiterleitung zur Homepage - header("location: ../../index.php"); exit(); + } else { + // ------------------------ Validierung des Authors: ---------------------------- + $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) { + $validatedContent = $content; + }else{ + $_SESSION["message"] = "invalid_content"; + header("location: ../../index.php?pfad=createArticle"); + exit(); + } + + // --------------------- -Eingabevalidierung der Kategorie: -------------------- + $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(); + } + + // -------------------------- Eingabevalidierung der tags: ---------------------- + if (isset($_POST['tags'])) { + $tags = trim($_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,20}$/u'; //Erlaubt: Buchstaben, Zahlen, Bindestriche, Leerzeichen; 2-20 Zeichen + if (preg_match($tagPattern, $tag)) { + $validatedTags[] = $tag; + } else { + $_SESSION["message"] = "invalid_tags"; + header("location: ../../index.php?pfad=createArticle"); + exit(); + } + } + // Duplikate entfernen: + $validatedTags = array_unique($validatedTags); + + // ----------------- Übertragung der validierten Daten in ArticleManager: --------------------------- + if (!isset($validatedTitle) || !isset($validatedContent) || !isset($validatedCategory) || !isset($validatedTags)) { + $_SESSION["message"] = "validation_missing"; + } else { + try { + $articleManager = ArticleManager::getInstance(); + $articleManager->addArticle($validatedTitle, $validatedContent, $validatedAuthor, $validatedCategory, $validatedTags); + } catch (Exception $e){ + $_SESSION["message"] = "internal_error"; + header("location: ../../index.php?pfad=createArticle"); + exit(); + } + $_SESSION["message"] = "new_article"; + // Weiterleitung zur Homepage + header("location: ../../index.php"); + exit(); + } } }