diff --git a/content/createArticle.php b/content/createArticle.php index 381c85a..41f1ca8 100644 --- a/content/createArticle.php +++ b/content/createArticle.php @@ -18,11 +18,40 @@ session_start(); Jeder Beitrag muss einen Titel, Kategorie und Inhalt besitzen.

+ +

+ Der Titel enthält ungültige Zeichen oder erfüllt die Länge von 5-120 Zeichen nicht. +

+ + +

+ Der Text erlaubt eine Länge von 10 bis maximal 7.000 Zeichen (ca. 1.000 Wörter). +

+ + +

+ Die ausgewählte Kategorie ist ungültig. +

+ + +

+ Ungültige Schlagworte gefunden. Erlaubt sind nur Buchstaben, Zahlen, Leerzeichen und Bindestriche (2-50 Zeichen). +

+ + +

+ Bei der Validierung deiner Daten ist ein Fehler aufgetreten. Bitte versuche es erneut. +

+ - - + + @@ -79,7 +108,9 @@ session_start(); diff --git a/content/showArticle.php b/content/showArticle.php index 77125dd..4457dcc 100644 --- a/content/showArticle.php +++ b/content/showArticle.php @@ -1,12 +1,11 @@ + - -
diff --git a/content/updateArticle.php b/content/updateArticle.php index 06d345e..ac86814 100644 --- a/content/updateArticle.php +++ b/content/updateArticle.php @@ -23,12 +23,52 @@ include_once 'php/controller/showArticle-controller.php'; Jeder Beitrag muss einen Titel, Kategorie und Inhalt besitzen.

+ +

+ Der Titel enthält ungültige Zeichen oder erfüllt die Länge von 5-120 Zeichen nicht. +

+ + +

+ Der Text erlaubt eine Länge von 10 bis maximal 7.000 Zeichen (ca. 1.000 Wörter). +

+ + +

+ Die ausgewählte Kategorie ist ungültig. +

+ + +

+ Ungültige Schlagworte gefunden. Erlaubt sind nur Buchstaben, Zahlen, Leerzeichen und Bindestriche (2-20 Zeichen). +

+ + +

+ Bei der Validierung deiner Daten ist ein Fehler aufgetreten. Bitte versuche es erneut. +

+ - +
@@ -86,7 +126,15 @@ include_once 'php/controller/showArticle-controller.php'; diff --git a/path.php b/path.php index 61ea409..d1d0a18 100644 --- a/path.php +++ b/path.php @@ -1,3 +1,4 @@ diff --git a/php/controller/createArticle-controller.php b/php/controller/createArticle-controller.php index 375d136..d4c1cec 100644 --- a/php/controller/createArticle-controller.php +++ b/php/controller/createArticle-controller.php @@ -2,30 +2,84 @@ session_start(); require_once '../model/LocalArticleManager.php'; require_once '../model/ArticleManager.php'; +require_once '../validator/article-validator.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"] ?? ''; + if(!isset($_POST["title"]) ||!isset($_POST["content"]) || !isset($_POST["category"])){ $_SESSION["message"] = "missing_parameters"; header("location: ../../index.php?pfad=createArticle"); + exit(); } 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"]; + $category = $_POST["category"]; + $tags = $_POST['tags'] ?? ''; + // -------------------------------- Validierung der Daten: ------------------------- + if (!articleAuthorValidator($author)) { + $_SESSION["message"] = "author_not_valid"; + header("location: ../../index.php?pfad=createArticle"); + exit(); + } + + 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($tags)) { + $_SESSION["message"] = "invalid_tags"; + header("location: ../../index.php?pfad=createArticle"); + exit(); + } else { + $cleanedTags = []; + $rawTags = explode(',', $tags); + foreach ($rawTags as $rawTag) { + // Leerzeichen am Anfang/Ende des einzelnen Tags entfernen: + $tag = trim($rawTag); + $cleanedTags[] = $tag; + } + // Duplikate entfernen: + $cleanedTags = array_unique($cleanedTags); + $cleanedTags = implode(',', $cleanedTags); + } + // ----------------- Übertragung der validierten Daten in ArticleManager: --------------------------- try { $articleManager = ArticleManager::getInstance(); - $articleManager->addArticle($title, $content, $author, $category, $tags); + $articleManager->addArticle($title, $content, $author, $category, $cleanedTags); + + // Formulardaten nach erfolgreichem Erstellen aus der Session löschen + unset($_SESSION["old_title"], $_SESSION["old_content"], $_SESSION["old_category"], $_SESSION["old_tags"]); + } 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(); - } } diff --git a/php/controller/showArticle-controller.php b/php/controller/showArticle-controller.php index 79b5e9e..38bb3f2 100644 --- a/php/controller/showArticle-controller.php +++ b/php/controller/showArticle-controller.php @@ -3,7 +3,7 @@ session_start(); require_once 'php/model/Article.php'; require_once 'php/model/ArticleManager.php'; -if (isset($_GET["id"])){ +if (isset($_GET["id"]) && !empty($_GET["id"])){ try { $id = $_GET["id"]; $articleManager = ArticleManager::getInstance(); @@ -15,12 +15,15 @@ if (isset($_GET["id"])){ $author = $article->getAuthor(); $tags = $article->getTags(); }else{ - $_SESSION["message"] = "article_not_found"; + //header("location: index.php?pfad=404"); + include_once "content/404.php"; + exit(); } } catch (Exception $e){ $_SESSION["message"] = "internal_error"; + exit(); } }else{ - $_SESSION["message"] = "article_not_found"; + $_SESSION["message"] = "missing_id"; } ?> \ No newline at end of file diff --git a/php/controller/updateArticle-controller.php b/php/controller/updateArticle-controller.php index fa5e232..6ff32ec 100644 --- a/php/controller/updateArticle-controller.php +++ b/php/controller/updateArticle-controller.php @@ -3,44 +3,93 @@ 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") { - try { + $_SESSION["old_title"] = $_POST["title"] ?? ''; + $_SESSION["old_content"] = $_POST["content"] ?? ''; + $_SESSION["old_category"] = $_POST["category"] ?? ''; + $_SESSION["old_tags"] = $_POST["tags"] ?? ''; + + if (isset($_GET["id"]) && !empty($_GET["id"])) { $id = $_GET["id"]; - } catch (Exception $e){ + } else { $_SESSION["message"] = "missing_id"; + header("location: ../../index.php?pfad=updateArticle"); + exit(); } + 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"); + exit(); }else{ - - $newTitle = $_POST["title"]; - $newContent = $_POST["content"]; - $newCategory = $_POST["category"]; + $title = $_POST["title"]; + $content = $_POST["content"]; $author = "max.mustermann"; // TODO: später aus Session den angemeldeten Nutzer beziehen. - $newTags = $_POST["tags"]; + $category = $_POST["category"]; + $tags = $_POST['tags'] ?? ''; + // -------------------------------- Validierung der Daten: ------------------------- + if (!articleAuthorValidator($author)) { + $_SESSION["message"] = "author_not_valid"; + header("location: ../../index.php?pfad=updateArticle"); + exit(); + } + + if (!articleTitleValidator($title)) { + $_SESSION["message"] = "invalid_title"; + header("location: ../../index.php?pfad=updateArticle"); + exit(); + } + + if (!articleContentValidator($content)) { + $_SESSION["message"] = "invalid_content"; + header("location: ../../index.php?pfad=updateArticle"); + exit(); + } + + if (!articleCategoryValidator($category)) { + $_SESSION["message"] = "invalid_category"; + header("location: ../../index.php?pfad=updateArticle"); + exit(); + } + + if (!articleTagValidator($tags)) { + $_SESSION["message"] = "invalid_tags"; + header("location: ../../index.php?pfad=updateArticle"); + exit(); + } else { + $cleanedTags = []; + $rawTags = explode(',', $tags); + foreach ($rawTags as $rawTag) { + // Leerzeichen am Anfang/Ende des einzelnen Tags entfernen: + $tag = trim($rawTag); + $cleanedTags[] = $tag; + } + // Duplikate entfernen: + $cleanedTags = array_unique($cleanedTags); + $cleanedTags = implode(',', $cleanedTags); + } + + // ----------------- Übertragung der validierten Daten in ArticleManager: --------------------------- try { - $articleManager = ArticleManager::getInstance(); // TODO: Später aus Session den Nutzer auslesen und Autorenrechte prüfen! + $articleManager = ArticleManager::getInstance(); $article = $articleManager->getArticle($id); - $article->setTitle($newTitle); - $article->setContent($newContent); - $article->setCategory($newCategory); - $article->setTags($newTags); + $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=updateArticle"); + 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..02c4f94 --- /dev/null +++ b/php/validator/article-validator.php @@ -0,0 +1,107 @@ += 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