Compare commits

...

5 Commits

3 changed files with 69 additions and 101 deletions
+45 -81
View File
@@ -2,120 +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 {
// ------------------------ Validierung des Autors: ----------------------------
$author = "max.mustermann"; // TODO: später aus Session den angemeldeten Nutzer beziehen.
$validatedAuthor = $author;
echo "Autorvalidierung erfolgreich";
// --------------------- 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 {
$content = $_POST["content"];
$author = "max.mustermann"; // TODO: später aus Session den angemeldeten Nutzer beziehen.
$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 (!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{
if (!articleContentValidator($content)) {
$_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)) {
if (!articleCategoryValidator($category)) {
$_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,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();
}
}
// 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";
if (!articleTagValidator($tags)) {
$_SESSION["message"] = "invalid_tags";
header("location: ../../index.php?pfad=createArticle");
exit();
} else {
try {
$articleManager = ArticleManager::getInstance();
$articleManager->addArticle($validatedTitle, $validatedContent, $validatedAuthor, $validatedCategory, $validatedTags);
echo "Speichern erfolgreich";
} 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);
$cleanedTags[] = $tag;
}
$_SESSION["message"] = "new_article";
// Weiterleitung zur Homepage
header("location: ../../index.php");
// 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, $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();
}
}
+20 -19
View File
@@ -10,54 +10,54 @@ if ($_SERVER["REQUEST_METHOD"] === "POST") {
$_SESSION["old_content"] = $_POST["content"] ?? '';
$_SESSION["old_category"] = $_POST["category"] ?? '';
$_SESSION["old_tags"] = $_POST["tags"] ?? '';
try {
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";
} elseif(!isset($id)) {
$_SESSION["message"] = "missing_id";
header("location: ../../index.php?pfad=updateArticle");
exit();
}else{
$title = $_POST["title"];
$content = $_POST["content"];
$author = "max.mustermann"; // TODO: später aus Session den angemeldeten Nutzer beziehen.
$category = $_POST["category"];
if (isset($_POST['tags'])) {
$tags = $_POST['tags'];
} else {
$tags = '';
}
$tags = $_POST['tags'] ?? '';
// -------------------------------- Validierung der Daten: -------------------------
if (!articleAuthorValidator($author)) {
$_SESSION["message"] = "author_not_valid";
header("location: ../../index.php?pfad=createArticle");
header("location: ../../index.php?pfad=updateArticle");
exit();
}
if (!articleTitleValidator($title)) {
$_SESSION["message"] = "invalid_title";
header("location: ../../index.php?pfad=createArticle");
header("location: ../../index.php?pfad=updateArticle");
exit();
}
if (!articleContentValidator($content)) {
$_SESSION["message"] = "invalid_content";
header("location: ../../index.php?pfad=createArticle");
header("location: ../../index.php?pfad=updateArticle");
exit();
}
if (!articleCategoryValidator($category)) {
$_SESSION["message"] = "invalid_category";
header("location: ../../index.php?pfad=createArticle");
header("location: ../../index.php?pfad=updateArticle");
exit();
}
if (!articleTagValidator($_POST["tags"])) {
if (!articleTagValidator($tags)) {
$_SESSION["message"] = "invalid_tags";
header("location: ../../index.php?pfad=createArticle");
header("location: ../../index.php?pfad=updateArticle");
exit();
} else {
$cleanedTags = [];
@@ -65,10 +65,11 @@ if ($_SERVER["REQUEST_METHOD"] === "POST") {
foreach ($rawTags as $rawTag) {
// Leerzeichen am Anfang/Ende des einzelnen Tags entfernen:
$tag = trim($rawTag);
// Duplikate entfernen:
$cleanedTags = array_unique($cleanedTags);
$cleanedTags = implode(',', $cleanedTags);
$cleanedTags[] = $tag;
}
// Duplikate entfernen:
$cleanedTags = array_unique($cleanedTags);
$cleanedTags = implode(',', $cleanedTags);
}
// ----------------- Übertragung der validierten Daten in ArticleManager: ---------------------------
@@ -82,7 +83,7 @@ if ($_SERVER["REQUEST_METHOD"] === "POST") {
$articleManager->updateArticle($id ,$article, $author);
} catch (Exception $e){
$_SESSION["message"] = "internal_error";
header("location: ../../index.php?pfad=createArticle");
header("location: ../../index.php?pfad=updateArticle");
exit();
}
$_SESSION["message"] = "article_updated";
+4 -1
View File
@@ -3,6 +3,7 @@
* Prüft, ob der Autor auch der Eigentümer des Beitrags ist.
* @param $author
* @return true
* TODO: Implement this.
*/
function articleAuthorValidator($author)
{
@@ -21,6 +22,7 @@ function articleAuthorValidator($author)
*/
function articleTitleValidator($title)
{
$title = trim($title);
$titlePattern = '/^[a-zA-Z0-9äöüÄÖÜß\s.,!?:;()\'"„“«»_+-]{5,120}$/u';
if (preg_match($titlePattern, $title)) {
return true;
@@ -36,8 +38,9 @@ function articleTitleValidator($title)
*/
function articleContentValidator($content)
{
$content = trim($content);
$zeichenAnzahl = mb_strlen($content);
if ($zeichenAnzahl <= 7000 && $zeichenAnzahl > 10) {
if ($zeichenAnzahl <= 7000 && $zeichenAnzahl >= 10) {
return true;
}else{
return false;