article-validator.php
This commit is contained in:
@@ -3,6 +3,7 @@ session_start();
|
|||||||
require_once '../model/LocalArticleManager.php';
|
require_once '../model/LocalArticleManager.php';
|
||||||
require_once '../model/ArticleManager.php';
|
require_once '../model/ArticleManager.php';
|
||||||
require_once '../model/Article.php';
|
require_once '../model/Article.php';
|
||||||
|
require_once '../validator/article-validator.php';
|
||||||
|
|
||||||
if ($_SERVER["REQUEST_METHOD"] === "POST") {
|
if ($_SERVER["REQUEST_METHOD"] === "POST") {
|
||||||
$_SESSION["old_title"] = $_POST["title"] ?? '';
|
$_SESSION["old_title"] = $_POST["title"] ?? '';
|
||||||
@@ -19,111 +20,75 @@ if ($_SERVER["REQUEST_METHOD"] === "POST") {
|
|||||||
} elseif(!isset($id)) {
|
} elseif(!isset($id)) {
|
||||||
$_SESSION["message"] = "missing_id";
|
$_SESSION["message"] = "missing_id";
|
||||||
}else{
|
}else{
|
||||||
|
|
||||||
// ------------------------ Validierung des Autors: ----------------------------
|
|
||||||
$author = "max.mustermann"; // TODO: später aus Session den angemeldeten Nutzer beziehen.
|
|
||||||
$validatedAuthor = $author;
|
|
||||||
|
|
||||||
// --------------------- Eingabevalidierung des Titels: -------------------------
|
|
||||||
$title = $_POST["title"];
|
$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"];
|
$content = $_POST["content"];
|
||||||
$zeichenAnzahl = mb_strlen($content);
|
$author = "max.mustermann"; // TODO: später aus Session den angemeldeten Nutzer beziehen.
|
||||||
if ($zeichenAnzahl <= 7000 && $zeichenAnzahl > 10) {
|
|
||||||
$validatedContent = $content;
|
|
||||||
}else{
|
|
||||||
$_SESSION["message"] = "invalid_content";
|
|
||||||
header("location: ../../index.php?pfad=createArticle");
|
|
||||||
exit();
|
|
||||||
}
|
|
||||||
|
|
||||||
// --------------------- -Eingabevalidierung der Kategorie: --------------------
|
|
||||||
$category = $_POST["category"];
|
$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'])) {
|
if (isset($_POST['tags'])) {
|
||||||
$tags = $_POST['tags'];
|
$tags = $_POST['tags'];
|
||||||
} else {
|
} else {
|
||||||
$tags = '';
|
$tags = '';
|
||||||
}
|
}
|
||||||
$validatedTags = [];
|
|
||||||
$rawTags = explode(',', $tags); // String mit Kommas in array...
|
|
||||||
|
|
||||||
foreach ($rawTags as $rawTag) {
|
// -------------------------------- Validierung der Daten: -------------------------
|
||||||
// Leerzeichen am Anfang/Ende des einzelnen Tags entfernen:
|
if (!articleAuthorValidator($author)) {
|
||||||
$tag = trim($rawTag);
|
$_SESSION["message"] = "author_not_valid";
|
||||||
|
header("location: ../../index.php?pfad=createArticle");
|
||||||
// leere Elemente überspringen:
|
exit();
|
||||||
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);
|
|
||||||
|
|
||||||
// ----------------- Übertragung der validierten Daten in ArticleManager: ---------------------------
|
if (!articleTitleValidator($title)) {
|
||||||
if (!isset($validatedTitle) || !isset($validatedContent) || !isset($validatedAuthor) || !isset($validatedCategory) || !isset($validatedTags)) {
|
$_SESSION["message"] = "invalid_title";
|
||||||
$_SESSION["message"] = "validation_missing";
|
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");
|
header("location: ../../index.php?pfad=createArticle");
|
||||||
exit();
|
exit();
|
||||||
} else {
|
} else {
|
||||||
try {
|
$cleanedTags = [];
|
||||||
$articleManager = ArticleManager::getInstance();
|
$rawTags = explode(',', $tags);
|
||||||
$article = $articleManager->getArticle($id);
|
foreach ($rawTags as $rawTag) {
|
||||||
$article->setTitle($validatedTitle);
|
// Leerzeichen am Anfang/Ende des einzelnen Tags entfernen:
|
||||||
$article->setContent($validatedContent);
|
$tag = trim($rawTag);
|
||||||
$article->setCategory($validatedCategory);
|
// Duplikate entfernen:
|
||||||
$article->setTags($validatedTags);
|
$cleanedTags = array_unique($cleanedTags);
|
||||||
$articleManager->updateArticle($id ,$article, $validatedAuthor);
|
$cleanedTags = implode(',', $cleanedTags);
|
||||||
} catch (Exception $e){
|
|
||||||
$_SESSION["message"] = "internal_error";
|
|
||||||
header("location: ../../index.php?pfad=createArticle");
|
|
||||||
exit();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ----------------- Ü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";
|
$_SESSION["message"] = "article_updated";
|
||||||
// Weiterleitung zur Homepage
|
// Weiterleitung zur Homepage
|
||||||
header("location: ../../index.php?pfad=showArticle&id=$id");
|
header("location: ../../index.php?pfad=showArticle&id=$id");
|
||||||
|
|
||||||
}
|
}
|
||||||
exit();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
?>
|
?>
|
||||||
@@ -0,0 +1,104 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Prüft, ob der Autor auch der Eigentümer des Beitrags ist.
|
||||||
|
* @param $author
|
||||||
|
* @return true
|
||||||
|
*/
|
||||||
|
function articleAuthorValidator($author)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prüft, ob der Titel die folgenden Bedingungen erfüllt:
|
||||||
|
* Buchstaben von a-z; A-Z
|
||||||
|
* Zahlen von 0-9
|
||||||
|
* Umlaute äöüÄÖÜß
|
||||||
|
* Satzeichen .,!?:;()"„“«»_+-
|
||||||
|
* 5-120 Zeichen
|
||||||
|
* @param $title
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
function articleTitleValidator($title)
|
||||||
|
{
|
||||||
|
$titlePattern = '/^[a-zA-Z0-9äöüÄÖÜß\s.,!?:;()\'"„“«»_+-]{5,120}$/u';
|
||||||
|
if (preg_match($titlePattern, $title)) {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prüft, ob der Contenttext 10-7000 Zeichen enthält.
|
||||||
|
* @param $content
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
function articleContentValidator($content)
|
||||||
|
{
|
||||||
|
$zeichenAnzahl = mb_strlen($content);
|
||||||
|
if ($zeichenAnzahl <= 7000 && $zeichenAnzahl > 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
||||||
Reference in New Issue
Block a user