Compare commits

...

37 Commits

Author SHA1 Message Date
niklas.ortmann 62a7b6773c Merge pull request 'Eingabevalidierung' (#16) from Beitrag-Eingabevalidierung into Beitrag-bearbeiten
Reviewed-on: #16
2026-06-01 14:05:28 +02:00
niklas.ortmann 625dd35b9e Update createArticle-controller.php 2026-06-01 14:02:31 +02:00
niklas.ortmann f99b7bc8ab Update updateArticle-controller.php 2026-06-01 14:02:11 +02:00
niklas.ortmann e779162ec2 Update createArticle-controller.php 2026-06-01 13:57:53 +02:00
niklas.ortmann 17345345b5 Update updateArticle-controller.php 2026-06-01 13:54:04 +02:00
niklas.ortmann 3df526d5b5 Update article-validator.php 2026-06-01 13:54:02 +02:00
niklas.ortmann d5f30176c4 article-validator.php 2026-06-01 13:41:22 +02:00
niklas.ortmann 308e5af6ff tags-> bis 50 Zeichen 2026-06-01 12:51:17 +02:00
niklas.ortmann 268fe6a6b9 Update updateArticle.php 2026-06-01 12:49:45 +02:00
niklas.ortmann a24914126f Update updateArticle-controller.php 2026-06-01 12:46:51 +02:00
niklas.ortmann 1ca27bc072 Update updateArticle.php 2026-06-01 12:43:11 +02:00
niklas.ortmann c96d93c2ce Update updateArticle-controller.php 2026-06-01 12:42:00 +02:00
niklas.ortmann cd38af22db Update updateArticle.php 2026-06-01 12:33:13 +02:00
niklas.ortmann c440c25e41 Update updateArticle.php 2026-06-01 12:30:50 +02:00
niklas.ortmann e105ab61b7 Update showArticle-controller.php 2026-06-01 12:24:59 +02:00
niklas.ortmann 10b058afa0 Update path.php 2026-06-01 12:22:14 +02:00
niklas.ortmann d18f33a87a Update showArticle-controller.php 2026-06-01 12:17:11 +02:00
niklas.ortmann 62190fa821 Update showArticle.php 2026-06-01 12:17:10 +02:00
niklas.ortmann a6210c9d6e Update showArticle-controller.php 2026-06-01 12:13:03 +02:00
niklas.ortmann fdece531af Update showArticle-controller.php 2026-06-01 12:09:44 +02:00
niklas.ortmann 8073235466 Update showArticle-controller.php 2026-06-01 12:07:26 +02:00
niklas.ortmann 7dc3c3b988 Update showArticle-controller.php 2026-06-01 12:06:32 +02:00
niklas.ortmann 21793dffd7 Update showArticle-controller.php 2026-06-01 12:03:40 +02:00
niklas.ortmann ca337c4fac debugging 2026-06-01 12:03:22 +02:00
niklas.ortmann 76beb8d62e Update showArticle-controller.php 2026-06-01 12:02:40 +02:00
niklas.ortmann c5bfd02f09 Update showArticle-controller.php 2026-06-01 12:02:21 +02:00
niklas.ortmann 9f4ca058bd Update showArticle-controller.php 2026-06-01 11:52:16 +02:00
niklas.ortmann 603c208ae4 Update showArticle-controller.php 2026-06-01 11:50:59 +02:00
niklas.ortmann 754777d69e Update showArticle-controller.php 2026-06-01 11:49:36 +02:00
niklas.ortmann 829a0e49d8 Content: zwischen 10 und 7000 Zeichen 2026-06-01 11:42:51 +02:00
niklas.ortmann f12babf2b1 Update createArticle.php 2026-06-01 11:35:25 +02:00
niklas.ortmann d6249169c6 Update createArticle.php 2026-06-01 11:31:25 +02:00
niklas.ortmann 6aa1cd9a63 Update createArticle-controller.php 2026-06-01 11:30:07 +02:00
niklas.ortmann 09795c30dd debugging 2026-06-01 11:19:01 +02:00
niklas.ortmann 59268dc102 Eingaben gehen bei Fehlermeldungen nicht verloren 2026-06-01 11:14:59 +02:00
niklas.ortmann 979378d142 Update createArticle-controller.php 2026-06-01 11:06:33 +02:00
niklas.ortmann 6ce7f0ef22 createArticle-Eingabevalidierung 2026-06-01 11:02:20 +02:00
8 changed files with 327 additions and 35 deletions
+34 -3
View File
@@ -18,11 +18,40 @@ session_start();
Jeder Beitrag muss einen Titel, Kategorie und Inhalt besitzen.
</p>
<?php endif; ?>
<?php if (isset($_SESSION["message"]) && $_SESSION["message"] == "invalid_title"): ?>
<p class="alert-message is-error">
Der Titel enthält ungültige Zeichen oder erfüllt die Länge von 5-120 Zeichen nicht.
</p>
<?php endif; ?>
<?php if (isset($_SESSION["message"]) && $_SESSION["message"] == "invalid_content"): ?>
<p class="alert-message is-error">
Der Text erlaubt eine Länge von 10 bis maximal 7.000 Zeichen (ca. 1.000 Wörter).
</p>
<?php endif; ?>
<?php if (isset($_SESSION["message"]) && $_SESSION["message"] == "invalid_category"): ?>
<p class="alert-message is-error">
Die ausgewählte Kategorie ist ungültig.
</p>
<?php endif; ?>
<?php if (isset($_SESSION["message"]) && $_SESSION["message"] == "invalid_tags"): ?>
<p class="alert-message is-error">
Ungültige Schlagworte gefunden. Erlaubt sind nur Buchstaben, Zahlen, Leerzeichen und Bindestriche (2-50 Zeichen).
</p>
<?php endif; ?>
<?php if (isset($_SESSION["message"]) && $_SESSION["message"] == "validation_missing"): ?>
<p class="alert-message is-error">
Bei der Validierung deiner Daten ist ein Fehler aufgetreten. Bitte versuche es erneut.
</p>
<?php endif; ?>
<?php
unset($_SESSION["message"]);
?>
<input type="text" id="title" name="title" placeholder="Titel hier eingeben" required>
<textarea id="content" name="content" placeholder="Schreibe deinen Beitrag..."></textarea>
<input type="text" id="title" name="title"
value="<?php echo htmlspecialchars($_SESSION['old_title'] ?? ''); unset($_SESSION['old_title']); ?>"
placeholder="Titel hier eingeben" required>
<textarea id="content" name="content" placeholder="Schreibe deinen Beitrag...">
<?php echo htmlspecialchars($_SESSION['old_content'] ?? ''); unset($_SESSION['old_content']); ?>
</textarea>
</main>
<!-- Seitenleiste -->
@@ -79,7 +108,9 @@ session_start();
<div class="sidebar-block">
<label for="tags">Schlagwörter</label>
<input type="text" id="tags" name="tags" placeholder="z.B. Technik, IT (mit Komma trennen)">
<input type="text" id="tags" name="tags"
value="<?php echo htmlspecialchars($_SESSION['old_tags'] ?? ''); unset($_SESSION['old_tags']); ?>"
placeholder="z.B. Technik, IT (mit Komma trennen)">
</div>
</aside>
+3 -4
View File
@@ -1,12 +1,11 @@
<?php
include_once 'php/controller/showArticle-controller.php';
?>
<!--
Seite: Anzeige für Beiträge
Funktion: Stellt einen übergebenen Beitrag dar.
-->
<?php
include_once 'php/controller/showArticle-controller.php';
?>
<!-- Hauptcontainer für die Beitragsansicht (Ausschließlich der Content-Bereich) -->
<main class="article-view-container">
<?php if (isset($_SESSION["message"]) && $_SESSION["message"] == "internal_error"): ?>
+51 -3
View File
@@ -23,12 +23,52 @@ include_once 'php/controller/showArticle-controller.php';
Jeder Beitrag muss einen Titel, Kategorie und Inhalt besitzen.
</p>
<?php endif; ?>
<?php if (isset($_SESSION["message"]) && $_SESSION["message"] == "invalid_title"): ?>
<p class="alert-message is-error">
Der Titel enthält ungültige Zeichen oder erfüllt die Länge von 5-120 Zeichen nicht.
</p>
<?php endif; ?>
<?php if (isset($_SESSION["message"]) && $_SESSION["message"] == "invalid_content"): ?>
<p class="alert-message is-error">
Der Text erlaubt eine Länge von 10 bis maximal 7.000 Zeichen (ca. 1.000 Wörter).
</p>
<?php endif; ?>
<?php if (isset($_SESSION["message"]) && $_SESSION["message"] == "invalid_category"): ?>
<p class="alert-message is-error">
Die ausgewählte Kategorie ist ungültig.
</p>
<?php endif; ?>
<?php if (isset($_SESSION["message"]) && $_SESSION["message"] == "invalid_tags"): ?>
<p class="alert-message is-error">
Ungültige Schlagworte gefunden. Erlaubt sind nur Buchstaben, Zahlen, Leerzeichen und Bindestriche (2-20 Zeichen).
</p>
<?php endif; ?>
<?php if (isset($_SESSION["message"]) && $_SESSION["message"] == "validation_missing"): ?>
<p class="alert-message is-error">
Bei der Validierung deiner Daten ist ein Fehler aufgetreten. Bitte versuche es erneut.
</p>
<?php endif; ?>
<?php
unset($_SESSION["message"]);
?>
<input type="text" id="title" name="title" value="<?php if (isset($title) && !empty($title)){echo htmlspecialchars($title);} ?>" placeholder="Titel hier eingeben" required>
<input type="text" id="title" name="title"
value="<?php
if (isset($title) && !empty($title)){echo htmlspecialchars($title);
}elseif (isset($_SESSION['old_title']) && !empty($_SESSION['old_title'])){
echo htmlspecialchars($_SESSION['old_title']);
unset($_SESSION['old_title']);
}
?>"
placeholder="Titel hier eingeben" required>
<textarea id="content" name="content" placeholder="Schreibe deinen Beitrag...">
<?php if (isset($content) && !empty($content)){echo htmlspecialchars($content);} ?>
<?php
if (isset($content) && !empty($content)){echo htmlspecialchars($content);
}elseif (isset($_SESSION['old_content']) && !empty($_SESSION['old_content'])){
echo htmlspecialchars($_SESSION['old_content']);
unset($_SESSION['old_content']);
}
?>
</textarea>
</main>
@@ -86,7 +126,15 @@ include_once 'php/controller/showArticle-controller.php';
<div class="sidebar-block">
<label for="tags">Schlagwörter</label>
<input type="text" id="tags" name="tags" value="<?php if (isset($tags) && !empty($tags)){echo htmlspecialchars($tags);} ?>" placeholder="z.B. Technik, IT (mit Komma trennen)">
<input type="text" id="tags" name="tags"
value="<?php
if (isset($tags) && !empty($tags)){echo htmlspecialchars($tags);
} elseif (isset($_SESSION['old_tags']) && !empty($_SESSION['old_tags'])){
echo htmlspecialchars($_SESSION['old_tags']);
unset($_SESSION['old_tags']);
}
?>"
placeholder="z.B. Technik, IT (mit Komma trennen)">
</div>
</aside>
+1
View File
@@ -1,3 +1,4 @@
<?php
// mit $_SERVER['DOCUMENT_ROOT'] später umbauen?
$abs_path = __DIR__;
?>
+59 -5
View File
@@ -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();
}
}
+6 -3
View File
@@ -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";
}
?>
+66 -17
View File
@@ -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();
}
?>
+107
View File
@@ -0,0 +1,107 @@
<?php
/**
* Prüft, ob der Autor auch der Eigentümer des Beitrags ist.
* @param $author
* @return true
* TODO: Implement this.
*/
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)
{
$title = trim($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)
{
$content = trim($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;
}
?>