Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 62a7b6773c | |||
| 625dd35b9e | |||
| f99b7bc8ab | |||
| e779162ec2 | |||
| 17345345b5 | |||
| 3df526d5b5 |
@@ -2,121 +2,85 @@
|
|||||||
session_start();
|
session_start();
|
||||||
require_once '../model/LocalArticleManager.php';
|
require_once '../model/LocalArticleManager.php';
|
||||||
require_once '../model/ArticleManager.php';
|
require_once '../model/ArticleManager.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"] ?? '';
|
||||||
$_SESSION["old_content"] = $_POST["content"] ?? '';
|
$_SESSION["old_content"] = $_POST["content"] ?? '';
|
||||||
$_SESSION["old_category"] = $_POST["category"] ?? '';
|
$_SESSION["old_category"] = $_POST["category"] ?? '';
|
||||||
$_SESSION["old_tags"] = $_POST["tags"] ?? '';
|
$_SESSION["old_tags"] = $_POST["tags"] ?? '';
|
||||||
|
|
||||||
if(!isset($_POST["title"]) ||!isset($_POST["content"]) || !isset($_POST["category"])){
|
if(!isset($_POST["title"]) ||!isset($_POST["content"]) || !isset($_POST["category"])){
|
||||||
$_SESSION["message"] = "missing_parameters";
|
$_SESSION["message"] = "missing_parameters";
|
||||||
header("location: ../../index.php?pfad=createArticle");
|
header("location: ../../index.php?pfad=createArticle");
|
||||||
exit();
|
exit();
|
||||||
} else {
|
} 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"];
|
$title = $_POST["title"];
|
||||||
// Mit Regex prüfen:
|
$content = $_POST["content"];
|
||||||
$titlePattern = '/^[a-zA-Z0-9äöüÄÖÜß\s.,!?:;()\'"„“«»_+-]{5,120}$/u'; //Erlaubt: Buchstaben, Zahlen, Standardsatzzeichen; 5-120 Zeichen
|
$author = "max.mustermann"; // TODO: später aus Session den angemeldeten Nutzer beziehen.
|
||||||
if (preg_match($titlePattern, $title)) {
|
$category = $_POST["category"];
|
||||||
$validatedTitle = $title;
|
$tags = $_POST['tags'] ?? '';
|
||||||
} else {
|
|
||||||
|
// -------------------------------- 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";
|
$_SESSION["message"] = "invalid_title";
|
||||||
header("location: ../../index.php?pfad=createArticle");
|
header("location: ../../index.php?pfad=createArticle");
|
||||||
exit();
|
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: -----------------------
|
if (!articleContentValidator($content)) {
|
||||||
$content = $_POST["content"];
|
|
||||||
$zeichenAnzahl = mb_strlen($content);
|
|
||||||
if ($zeichenAnzahl <= 7000 && $zeichenAnzahl > 10) {
|
|
||||||
$validatedContent = $content;
|
|
||||||
}else{
|
|
||||||
$_SESSION["message"] = "invalid_content";
|
$_SESSION["message"] = "invalid_content";
|
||||||
header("location: ../../index.php?pfad=createArticle");
|
header("location: ../../index.php?pfad=createArticle");
|
||||||
exit();
|
exit();
|
||||||
}
|
}
|
||||||
echo "Contentvalidierung erfolgreich";
|
|
||||||
|
|
||||||
// --------------------- -Eingabevalidierung der Kategorie: --------------------
|
if (!articleCategoryValidator($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";
|
$_SESSION["message"] = "invalid_category";
|
||||||
header("location: ../../index.php?pfad=createArticle");
|
header("location: ../../index.php?pfad=createArticle");
|
||||||
exit();
|
exit();
|
||||||
}
|
}
|
||||||
$validatedCategory = $category;
|
|
||||||
echo "Kategorievalidierung erfolgreich";
|
|
||||||
|
|
||||||
// -------------------------- Eingabevalidierung der tags: ----------------------
|
if (!articleTagValidator($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";
|
$_SESSION["message"] = "invalid_tags";
|
||||||
header("location: ../../index.php?pfad=createArticle");
|
header("location: ../../index.php?pfad=createArticle");
|
||||||
exit();
|
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:
|
// Duplikate entfernen:
|
||||||
$validatedTags = array_unique($validatedTags);
|
$cleanedTags = array_unique($cleanedTags);
|
||||||
$validatedTags = implode(',', $validatedTags);
|
$cleanedTags = implode(',', $cleanedTags);
|
||||||
echo "Tagvalidierung erfolgreich";
|
}
|
||||||
|
|
||||||
// ----------------- Übertragung der validierten Daten in ArticleManager: ---------------------------
|
// ----------------- Ü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 {
|
try {
|
||||||
$articleManager = ArticleManager::getInstance();
|
$articleManager = ArticleManager::getInstance();
|
||||||
$articleManager->addArticle($validatedTitle, $validatedContent, $validatedAuthor, $validatedCategory, $validatedTags);
|
$articleManager->addArticle($title, $content, $author, $category, $cleanedTags);
|
||||||
echo "Speichern erfolgreich";
|
|
||||||
|
// 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){
|
} catch (Exception $e){
|
||||||
$_SESSION["message"] = "internal_error";
|
$_SESSION["message"] = "internal_error";
|
||||||
header("location: ../../index.php?pfad=createArticle");
|
header("location: ../../index.php?pfad=createArticle");
|
||||||
exit();
|
exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
$_SESSION["message"] = "new_article";
|
$_SESSION["message"] = "new_article";
|
||||||
// Weiterleitung zur Homepage
|
// Weiterleitung zur Homepage
|
||||||
header("location: ../../index.php");
|
header("location: ../../index.php");
|
||||||
exit();
|
exit();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
?>
|
?>
|
||||||
@@ -10,54 +10,54 @@ if ($_SERVER["REQUEST_METHOD"] === "POST") {
|
|||||||
$_SESSION["old_content"] = $_POST["content"] ?? '';
|
$_SESSION["old_content"] = $_POST["content"] ?? '';
|
||||||
$_SESSION["old_category"] = $_POST["category"] ?? '';
|
$_SESSION["old_category"] = $_POST["category"] ?? '';
|
||||||
$_SESSION["old_tags"] = $_POST["tags"] ?? '';
|
$_SESSION["old_tags"] = $_POST["tags"] ?? '';
|
||||||
try {
|
|
||||||
|
if (isset($_GET["id"]) && !empty($_GET["id"])) {
|
||||||
$id = $_GET["id"];
|
$id = $_GET["id"];
|
||||||
} catch (Exception $e){
|
} else {
|
||||||
$_SESSION["message"] = "missing_id";
|
$_SESSION["message"] = "missing_id";
|
||||||
|
header("location: ../../index.php?pfad=updateArticle");
|
||||||
|
exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isset($_POST["title"]) ||!isset($_POST["content"]) || !isset($_POST["category"])){
|
if (!isset($_POST["title"]) ||!isset($_POST["content"]) || !isset($_POST["category"])){
|
||||||
$_SESSION["message"] = "missing_parameters";
|
$_SESSION["message"] = "missing_parameters";
|
||||||
} elseif(!isset($id)) {
|
header("location: ../../index.php?pfad=updateArticle");
|
||||||
$_SESSION["message"] = "missing_id";
|
exit();
|
||||||
}else{
|
}else{
|
||||||
$title = $_POST["title"];
|
$title = $_POST["title"];
|
||||||
$content = $_POST["content"];
|
$content = $_POST["content"];
|
||||||
$author = "max.mustermann"; // TODO: später aus Session den angemeldeten Nutzer beziehen.
|
$author = "max.mustermann"; // TODO: später aus Session den angemeldeten Nutzer beziehen.
|
||||||
$category = $_POST["category"];
|
$category = $_POST["category"];
|
||||||
if (isset($_POST['tags'])) {
|
$tags = $_POST['tags'] ?? '';
|
||||||
$tags = $_POST['tags'];
|
|
||||||
} else {
|
|
||||||
$tags = '';
|
|
||||||
}
|
|
||||||
|
|
||||||
// -------------------------------- Validierung der Daten: -------------------------
|
// -------------------------------- Validierung der Daten: -------------------------
|
||||||
if (!articleAuthorValidator($author)) {
|
if (!articleAuthorValidator($author)) {
|
||||||
$_SESSION["message"] = "author_not_valid";
|
$_SESSION["message"] = "author_not_valid";
|
||||||
header("location: ../../index.php?pfad=createArticle");
|
header("location: ../../index.php?pfad=updateArticle");
|
||||||
exit();
|
exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!articleTitleValidator($title)) {
|
if (!articleTitleValidator($title)) {
|
||||||
$_SESSION["message"] = "invalid_title";
|
$_SESSION["message"] = "invalid_title";
|
||||||
header("location: ../../index.php?pfad=createArticle");
|
header("location: ../../index.php?pfad=updateArticle");
|
||||||
exit();
|
exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!articleContentValidator($content)) {
|
if (!articleContentValidator($content)) {
|
||||||
$_SESSION["message"] = "invalid_content";
|
$_SESSION["message"] = "invalid_content";
|
||||||
header("location: ../../index.php?pfad=createArticle");
|
header("location: ../../index.php?pfad=updateArticle");
|
||||||
exit();
|
exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!articleCategoryValidator($category)) {
|
if (!articleCategoryValidator($category)) {
|
||||||
$_SESSION["message"] = "invalid_category";
|
$_SESSION["message"] = "invalid_category";
|
||||||
header("location: ../../index.php?pfad=createArticle");
|
header("location: ../../index.php?pfad=updateArticle");
|
||||||
exit();
|
exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!articleTagValidator($_POST["tags"])) {
|
if (!articleTagValidator($tags)) {
|
||||||
$_SESSION["message"] = "invalid_tags";
|
$_SESSION["message"] = "invalid_tags";
|
||||||
header("location: ../../index.php?pfad=createArticle");
|
header("location: ../../index.php?pfad=updateArticle");
|
||||||
exit();
|
exit();
|
||||||
} else {
|
} else {
|
||||||
$cleanedTags = [];
|
$cleanedTags = [];
|
||||||
@@ -65,11 +65,12 @@ if ($_SERVER["REQUEST_METHOD"] === "POST") {
|
|||||||
foreach ($rawTags as $rawTag) {
|
foreach ($rawTags as $rawTag) {
|
||||||
// Leerzeichen am Anfang/Ende des einzelnen Tags entfernen:
|
// Leerzeichen am Anfang/Ende des einzelnen Tags entfernen:
|
||||||
$tag = trim($rawTag);
|
$tag = trim($rawTag);
|
||||||
|
$cleanedTags[] = $tag;
|
||||||
|
}
|
||||||
// Duplikate entfernen:
|
// Duplikate entfernen:
|
||||||
$cleanedTags = array_unique($cleanedTags);
|
$cleanedTags = array_unique($cleanedTags);
|
||||||
$cleanedTags = implode(',', $cleanedTags);
|
$cleanedTags = implode(',', $cleanedTags);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// ----------------- Übertragung der validierten Daten in ArticleManager: ---------------------------
|
// ----------------- Übertragung der validierten Daten in ArticleManager: ---------------------------
|
||||||
try {
|
try {
|
||||||
@@ -82,7 +83,7 @@ if ($_SERVER["REQUEST_METHOD"] === "POST") {
|
|||||||
$articleManager->updateArticle($id ,$article, $author);
|
$articleManager->updateArticle($id ,$article, $author);
|
||||||
} catch (Exception $e){
|
} catch (Exception $e){
|
||||||
$_SESSION["message"] = "internal_error";
|
$_SESSION["message"] = "internal_error";
|
||||||
header("location: ../../index.php?pfad=createArticle");
|
header("location: ../../index.php?pfad=updateArticle");
|
||||||
exit();
|
exit();
|
||||||
}
|
}
|
||||||
$_SESSION["message"] = "article_updated";
|
$_SESSION["message"] = "article_updated";
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
* Prüft, ob der Autor auch der Eigentümer des Beitrags ist.
|
* Prüft, ob der Autor auch der Eigentümer des Beitrags ist.
|
||||||
* @param $author
|
* @param $author
|
||||||
* @return true
|
* @return true
|
||||||
|
* TODO: Implement this.
|
||||||
*/
|
*/
|
||||||
function articleAuthorValidator($author)
|
function articleAuthorValidator($author)
|
||||||
{
|
{
|
||||||
@@ -21,6 +22,7 @@ function articleAuthorValidator($author)
|
|||||||
*/
|
*/
|
||||||
function articleTitleValidator($title)
|
function articleTitleValidator($title)
|
||||||
{
|
{
|
||||||
|
$title = trim($title);
|
||||||
$titlePattern = '/^[a-zA-Z0-9äöüÄÖÜß\s.,!?:;()\'"„“«»_+-]{5,120}$/u';
|
$titlePattern = '/^[a-zA-Z0-9äöüÄÖÜß\s.,!?:;()\'"„“«»_+-]{5,120}$/u';
|
||||||
if (preg_match($titlePattern, $title)) {
|
if (preg_match($titlePattern, $title)) {
|
||||||
return true;
|
return true;
|
||||||
@@ -36,8 +38,9 @@ function articleTitleValidator($title)
|
|||||||
*/
|
*/
|
||||||
function articleContentValidator($content)
|
function articleContentValidator($content)
|
||||||
{
|
{
|
||||||
|
$content = trim($content);
|
||||||
$zeichenAnzahl = mb_strlen($content);
|
$zeichenAnzahl = mb_strlen($content);
|
||||||
if ($zeichenAnzahl <= 7000 && $zeichenAnzahl > 10) {
|
if ($zeichenAnzahl <= 7000 && $zeichenAnzahl >= 10) {
|
||||||
return true;
|
return true;
|
||||||
}else{
|
}else{
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
Reference in New Issue
Block a user