diff --git a/php/controller/updateArticle-controller.php b/php/controller/updateArticle-controller.php index fa5e232..6d1b875 100644 --- a/php/controller/updateArticle-controller.php +++ b/php/controller/updateArticle-controller.php @@ -5,6 +5,10 @@ require_once '../model/ArticleManager.php'; require_once '../model/Article.php'; if ($_SERVER["REQUEST_METHOD"] === "POST") { + $_SESSION["old_title"] = $_POST["title"] ?? ''; + $_SESSION["old_content"] = $_POST["content"] ?? ''; + $_SESSION["old_category"] = $_POST["category"] ?? ''; + $_SESSION["old_tags"] = $_POST["tags"] ?? ''; try { $id = $_GET["id"]; } catch (Exception $e){ @@ -12,28 +16,112 @@ if ($_SERVER["REQUEST_METHOD"] === "POST") { } if (!isset($_POST["title"]) ||!isset($_POST["content"]) || !isset($_POST["category"])){ $_SESSION["message"] = "missing_parameters"; - header("location: ../../index.php?pfad=updateArticle"); } elseif(!isset($id)) { $_SESSION["message"] = "missing_id"; - //header("location: ../../index.php?pfad=updateArticle"); }else{ - $newTitle = $_POST["title"]; - $newContent = $_POST["content"]; - $newCategory = $_POST["category"]; + // ------------------------ Validierung des Autors: ---------------------------- $author = "max.mustermann"; // TODO: später aus Session den angemeldeten Nutzer beziehen. - $newTags = $_POST["tags"]; + $validatedAuthor = $author; + echo "Autorvalidierung erfolgreich"; - try { - $articleManager = ArticleManager::getInstance(); // TODO: Später aus Session den Nutzer auslesen und Autorenrechte prüfen! - $article = $articleManager->getArticle($id); - $article->setTitle($newTitle); - $article->setContent($newContent); - $article->setCategory($newCategory); - $article->setTags($newTags); - $articleManager->updateArticle($id ,$article, $author); - } catch (Exception $e){ - $_SESSION["message"] = "internal_error"; + // --------------------- 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."; + } + echo "Titelvalidierung erfolgreich"; + + // --------------------- 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(); + } + echo "Contentvalidierung erfolgreich"; + + // --------------------- -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(); + } + $validatedCategory = $category; + echo "Kategorievalidierung erfolgreich"; + + // -------------------------- 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,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); + $validatedTags = implode(',', $validatedTags); + echo "Tagvalidierung erfolgreich"; + + // ----------------- Übertragung der validierten Daten in ArticleManager: --------------------------- + if (!isset($validatedTitle) || !isset($validatedContent) || !isset($validatedAuthor) || !isset($validatedCategory) || !isset($validatedTags)) { + $_SESSION["message"] = "validation_missing"; + 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(); + } } $_SESSION["message"] = "article_updated"; // Weiterleitung zur Homepage