Compare commits
29 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b5fc79a2e7 | |||
| 24a3176f43 | |||
| a5650f2720 | |||
| 4758366063 | |||
| e7a5c2b352 | |||
| 36ec2a539c | |||
| 339e51cca7 | |||
| 0726ba214c | |||
| 0e032ae029 | |||
| b9dead1c8b | |||
| 9010b68a39 | |||
| 252c14a377 | |||
| 4abfe801ec | |||
| c23a10eb6e | |||
| 11b55008ea | |||
| 46dcc16c59 | |||
| d42dff1165 | |||
| 11da418f60 | |||
| 981e78ff7e | |||
| c798bbd230 | |||
| e22d97e15a | |||
| c662369259 | |||
| 11b7be4ca0 | |||
| a029314bc9 | |||
| 8dd2d4f4ea | |||
| a4efbd4f0b | |||
| f63b8c6506 | |||
| caa9a9ff8f | |||
| 524dc2a399 |
@@ -1,19 +0,0 @@
|
|||||||
<?php
|
|
||||||
require_once "php/model/LocalUserManager.php";
|
|
||||||
|
|
||||||
$dao = new LocalUserManager();
|
|
||||||
|
|
||||||
/*
|
|
||||||
Deregistrierung
|
|
||||||
Funktion: Entfernt User aus Dummy-Daten und beendet Session
|
|
||||||
*/
|
|
||||||
|
|
||||||
if (isset($_SESSION["user_email"])) {
|
|
||||||
$dao->deleteUser($_SESSION["user_email"]);
|
|
||||||
}
|
|
||||||
|
|
||||||
$_SESSION = [];
|
|
||||||
session_destroy();
|
|
||||||
|
|
||||||
header("Location: index.php");
|
|
||||||
exit();
|
|
||||||
@@ -58,11 +58,6 @@ include_once 'php/controller/showArticle-controller.php';
|
|||||||
Sie sind nicht berechtigt, diesen Beitrag zu bearbeiten.
|
Sie sind nicht berechtigt, diesen Beitrag zu bearbeiten.
|
||||||
</p>
|
</p>
|
||||||
<?php endif; ?>
|
<?php endif; ?>
|
||||||
<?php if (isset($_SESSION["message"]) && $_SESSION["message"] == "not_found_article"): ?>
|
|
||||||
<p class="alert-message is-error">
|
|
||||||
Der zu bearbeitende Artikel wurde nicht gefunden. Bitte versuche es erneut.
|
|
||||||
</p>
|
|
||||||
<?php endif; ?>
|
|
||||||
<?php
|
<?php
|
||||||
unset($_SESSION["message"]);
|
unset($_SESSION["message"]);
|
||||||
?>
|
?>
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
<FilesMatch "\.db">
|
||||||
|
deny from all
|
||||||
|
</FilesMatch>
|
||||||
@@ -17,12 +17,12 @@ if ($pfad === "register") {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ($pfad === "logout") {
|
if ($pfad === "logout") {
|
||||||
include_once "content/logout.php";
|
include_once "content/logout-controller.php";
|
||||||
exit();
|
exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($pfad === "deleteAccount") {
|
if ($pfad === "deleteAccount") {
|
||||||
include_once "content/deleteAccount.php";
|
include_once "content/deleteAccount-controller.php";
|
||||||
exit();
|
exit();
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ 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';
|
require_once '../validator/article-validator.php';
|
||||||
require_once "../model/LocalUserManager.php";
|
|
||||||
|
|
||||||
if ($_SERVER["REQUEST_METHOD"] === "POST") {
|
if ($_SERVER["REQUEST_METHOD"] === "POST") {
|
||||||
$_SESSION["old_title"] = $_POST["title"] ?? '';
|
$_SESSION["old_title"] = $_POST["title"] ?? '';
|
||||||
|
|||||||
@@ -0,0 +1,30 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
require_once "php/model/UserManager.php";
|
||||||
|
|
||||||
|
/*
|
||||||
|
Deregistrierung
|
||||||
|
Funktion: Entfernt User aus der Datenbank und beendet die Session
|
||||||
|
*/
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
$dao = UserManager::getInstance();
|
||||||
|
|
||||||
|
if (isset($_SESSION["user_email"])) {
|
||||||
|
$dao->deleteUser($_SESSION["user_email"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
$_SESSION = [];
|
||||||
|
session_destroy();
|
||||||
|
|
||||||
|
header("Location: index.php");
|
||||||
|
exit();
|
||||||
|
|
||||||
|
} catch (Exception $e) {
|
||||||
|
|
||||||
|
$_SESSION["message"] = "internal_error";
|
||||||
|
|
||||||
|
header("Location: index.php?pfad=profile");
|
||||||
|
exit();
|
||||||
|
}
|
||||||
@@ -1,8 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
require_once "php/model/LocalUserManager.php";
|
require_once "php/model/UserManager.php";
|
||||||
|
|
||||||
$dao = new LocalUserManager();
|
|
||||||
$error = null;
|
$error = null;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -11,6 +10,10 @@ $error = null;
|
|||||||
*/
|
*/
|
||||||
if ($_SERVER["REQUEST_METHOD"] === "POST") {
|
if ($_SERVER["REQUEST_METHOD"] === "POST") {
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
$dao = UserManager::getInstance();
|
||||||
|
|
||||||
$email = $_POST["email"] ?? "";
|
$email = $_POST["email"] ?? "";
|
||||||
$password = $_POST["password"] ?? "";
|
$password = $_POST["password"] ?? "";
|
||||||
|
|
||||||
@@ -18,12 +21,7 @@ if ($_SERVER["REQUEST_METHOD"] === "POST") {
|
|||||||
|
|
||||||
if ($user && password_verify($password, $user["password"])) {
|
if ($user && password_verify($password, $user["password"])) {
|
||||||
|
|
||||||
if (isset($user["vorname"]) && isset($user["nachname"])) {
|
|
||||||
$_SESSION["user"] = $user["vorname"] . " " . $user["nachname"];
|
$_SESSION["user"] = $user["vorname"] . " " . $user["nachname"];
|
||||||
} else {
|
|
||||||
$_SESSION["user"] = $user["username"] ?? "";
|
|
||||||
}
|
|
||||||
|
|
||||||
$_SESSION["user_email"] = $user["email"];
|
$_SESSION["user_email"] = $user["email"];
|
||||||
|
|
||||||
header("Location: index.php");
|
header("Location: index.php");
|
||||||
@@ -32,4 +30,8 @@ if ($_SERVER["REQUEST_METHOD"] === "POST") {
|
|||||||
} else {
|
} else {
|
||||||
$error = "Login fehlgeschlagen. Bitte überprüfe deine Eingaben.";
|
$error = "Login fehlgeschlagen. Bitte überprüfe deine Eingaben.";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} catch (Exception $e) {
|
||||||
|
$error = "Es ist ein interner Fehler aufgetreten. Bitte versuche es erneut.";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
require_once "php/model/LocalUserManager.php";
|
require_once "php/model/UserManager.php";
|
||||||
require_once "php/model/Article.php";
|
require_once "php/model/Article.php";
|
||||||
require_once "php/model/ArticleManager.php";
|
require_once "php/model/ArticleManager.php";
|
||||||
require_once "php/validator/user-validator.php";
|
require_once "php/validator/user-validator.php";
|
||||||
@@ -13,7 +13,7 @@ if (!isset($_SESSION["user"])) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$dao = new LocalUserManager();
|
$dao = UserManager::getInstance();
|
||||||
|
|
||||||
$user = $dao->findUser($_SESSION["user_email"] ?? "");
|
$user = $dao->findUser($_SESSION["user_email"] ?? "");
|
||||||
|
|
||||||
@@ -38,15 +38,15 @@ try {
|
|||||||
$_GET["edit"] = "1";
|
$_GET["edit"] = "1";
|
||||||
|
|
||||||
} elseif (!userNameValidator($vorname)) {
|
} elseif (!userNameValidator($vorname)) {
|
||||||
$error = "Der Vorname muss 2 bis 50 Zeichen lang sein und darf nur Buchstaben, Leerzeichen und Bindestriche enthalten.";
|
$error = "Der Vorname muss 2 bis 20 Zeichen lang sein und darf nur Buchstaben, Leerzeichen und Bindestriche enthalten.";
|
||||||
$_GET["edit"] = "1";
|
$_GET["edit"] = "1";
|
||||||
|
|
||||||
} elseif (!userNameValidator($nachname)) {
|
} elseif (!userNameValidator($nachname)) {
|
||||||
$error = "Der Nachname muss 2 bis 50 Zeichen lang sein und darf nur Buchstaben, Leerzeichen und Bindestriche enthalten.";
|
$error = "Der Nachname muss 2 bis 20 Zeichen lang sein und darf nur Buchstaben, Leerzeichen und Bindestriche enthalten.";
|
||||||
$_GET["edit"] = "1";
|
$_GET["edit"] = "1";
|
||||||
|
|
||||||
} elseif (!userOptionalPasswordValidator($password)) {
|
} elseif (!userOptionalPasswordValidator($password)) {
|
||||||
$error = "Das Passwort muss 8 bis 72 Zeichen lang sein.";
|
$error = "Das Passwort muss 5 bis 12 Zeichen lang sein.";
|
||||||
$_GET["edit"] = "1";
|
$_GET["edit"] = "1";
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -5,10 +5,10 @@
|
|||||||
|
|
||||||
require_once 'php/model/Article.php';
|
require_once 'php/model/Article.php';
|
||||||
require_once 'php/model/ArticleManager.php';
|
require_once 'php/model/ArticleManager.php';
|
||||||
require_once "../model/LocalUserManager.php";
|
require_once "php/model/UserManager.php";
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$dao = new LocalUserManager();
|
$dao = UserManager::getInstance();
|
||||||
$user = $dao->findUser($_SESSION["user_email"] ?? "");
|
$user = $dao->findUser($_SESSION["user_email"] ?? "");
|
||||||
|
|
||||||
$author = $user["email"];
|
$author = $user["email"];
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
require_once "php/model/LocalUserManager.php";
|
require_once "php/model/UserManager.php";
|
||||||
require_once "php/validator/user-validator.php";
|
require_once "php/validator/user-validator.php";
|
||||||
|
|
||||||
$error = null;
|
$error = null;
|
||||||
@@ -15,14 +15,14 @@ if ($_SERVER["REQUEST_METHOD"] === "POST") {
|
|||||||
if (!userEmailValidator($email)) {
|
if (!userEmailValidator($email)) {
|
||||||
$error = "Bitte gib eine gültige E-Mail-Adresse ein.";
|
$error = "Bitte gib eine gültige E-Mail-Adresse ein.";
|
||||||
} elseif (!userNameValidator($vorname)) {
|
} elseif (!userNameValidator($vorname)) {
|
||||||
$error = "Der Vorname muss 2 bis 50 Zeichen lang sein und darf nur Buchstaben, Umlaute, Leerzeichen und Bindestriche enthalten.";
|
$error = "Der Vorname muss 2 bis 20 Zeichen lang sein und darf nur Buchstaben, Umlaute, Leerzeichen und Bindestriche enthalten.";
|
||||||
} elseif (!userNameValidator($nachname)) {
|
} elseif (!userNameValidator($nachname)) {
|
||||||
$error = "Der Nachname muss 2 bis 50 Zeichen lang sein und darf nur Buchstaben, Umlaute, Leerzeichen und Bindestriche enthalten.";
|
$error = "Der Nachname muss 2 bis 20 Zeichen lang sein und darf nur Buchstaben, Umlaute, Leerzeichen und Bindestriche enthalten.";
|
||||||
} elseif (!userPasswordValidator($plainPassword)) {
|
} elseif (!userPasswordValidator($plainPassword)) {
|
||||||
$error = "Das Passwort muss 8 bis 72 Zeichen lang sein.";
|
$error = "Das Passwort muss 5 bis 12 Zeichen lang sein.";
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
$dao = new LocalUserManager();
|
$dao = UserManager::getInstance();
|
||||||
|
|
||||||
$password = password_hash($plainPassword, PASSWORD_DEFAULT);
|
$password = password_hash($plainPassword, PASSWORD_DEFAULT);
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
require_once 'LocalArticleManager.php';
|
require_once 'LocalArticleManager.php';
|
||||||
|
require_once 'DatabaseArticleManager.php';
|
||||||
require_once 'Article.php';
|
require_once 'Article.php';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -7,11 +8,11 @@ require_once 'Article.php';
|
|||||||
*
|
*
|
||||||
* @author Niklas Ortmann
|
* @author Niklas Ortmann
|
||||||
*/
|
*/
|
||||||
class ArticleManager extends LocalArticleManager
|
class ArticleManager
|
||||||
{
|
{
|
||||||
public static function getInstance()
|
public static function getInstance()
|
||||||
{
|
{
|
||||||
$articleManager = LocalArticleManager::getInstance(); // TODO: später durch DataBaseArticleManager ersetzen.
|
$articleManager = DatabaseArticleManager::getInstance(); // Hier kann zwischen dem lokalen und datenbankbasiertem ArticleManager gewechselt werden.
|
||||||
|
|
||||||
// Erstellen von Dummy-Beiträgen:
|
// Erstellen von Dummy-Beiträgen:
|
||||||
if($articleManager->getArticle(1) == null ){
|
if($articleManager->getArticle(1) == null ){
|
||||||
|
|||||||
@@ -0,0 +1,315 @@
|
|||||||
|
<?php
|
||||||
|
require_once 'ArticleManagerDAO.php';
|
||||||
|
require_once 'Article.php';
|
||||||
|
/**
|
||||||
|
* Klasse: Eine SQLLite3 Lösung des ArticleManagerDAO.
|
||||||
|
*
|
||||||
|
* @author Niklas Ortmann
|
||||||
|
*/
|
||||||
|
class DatabaseArticleManager implements ArticleManagerDAO {
|
||||||
|
|
||||||
|
private static $instance = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Konstruktor
|
||||||
|
*
|
||||||
|
* Erstellt die Datenbankverbindung und die Tabelle articles.
|
||||||
|
* @throws InternalServerErrorException
|
||||||
|
*/
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
if (!file_exists(__DIR__ . '/../../db/articles.db')) {
|
||||||
|
try {
|
||||||
|
$user = 'root';
|
||||||
|
$pw = null;
|
||||||
|
$dsn = 'sqlite:' . __DIR__ . '/../../db/articles.db';
|
||||||
|
$db = new PDO($dsn, $user, $pw);
|
||||||
|
|
||||||
|
$db->exec("
|
||||||
|
CREATE TABLE articles (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
title TEXT,
|
||||||
|
content TEXT,
|
||||||
|
author TEXT,
|
||||||
|
category TEXT,
|
||||||
|
tags TEXT,
|
||||||
|
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||||
|
);");
|
||||||
|
unset($db);
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
throw new InternalServerErrorException($e->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Baut die Verbindung zur Datenbank auf.
|
||||||
|
* @throws InternalServerErrorException
|
||||||
|
*/
|
||||||
|
private function getConnection()
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$user = 'root';
|
||||||
|
$pw = null;
|
||||||
|
$dsn = 'sqlite:' . __DIR__ . '/../../db/articles.db';
|
||||||
|
return new PDO($dsn, $user, $pw);
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
throw new InternalServerErrorException($e->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gibt die DatabaseArticleManager-Instanz zurück.
|
||||||
|
* @return DatabaseArticleManager
|
||||||
|
*/
|
||||||
|
public static function getInstance()
|
||||||
|
{
|
||||||
|
if (self::$instance == null) {
|
||||||
|
self::$instance = new DatabaseArticleManager();
|
||||||
|
}
|
||||||
|
return self::$instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function addArticle($title, $content, $author, $category, $tags)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$db = $this->getConnection();
|
||||||
|
|
||||||
|
$sql = "INSERT INTO articles (title, content, author, category, tags)
|
||||||
|
VALUES (:title, :content, :author, :category, :tags);";
|
||||||
|
|
||||||
|
$command = $db->prepare($sql);
|
||||||
|
if (!$command) {
|
||||||
|
throw new InternalServerErrorException("internal_error");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verknüpft die übergebenen Parameter exakt mit den SQL-Platzhaltern
|
||||||
|
$success = $command->execute([
|
||||||
|
":title" => $title,
|
||||||
|
":content" => $content,
|
||||||
|
":author" => $author,
|
||||||
|
":category" => $category,
|
||||||
|
":tags" => $tags
|
||||||
|
]);
|
||||||
|
|
||||||
|
if (!$success) {
|
||||||
|
throw new InternalServerErrorException("internal_error");
|
||||||
|
}
|
||||||
|
|
||||||
|
return intval($db->lastInsertId());
|
||||||
|
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
throw new InternalServerErrorException($e->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function updateArticle($id, $article, $author)
|
||||||
|
{
|
||||||
|
if (empty($article)) {
|
||||||
|
throw new InternalServerErrorException("internal_error");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Berechtigungsprüfung analog zur lokalen Implementierung:
|
||||||
|
if ($article->getAuthor() !== $author) {
|
||||||
|
throw new UnauthorizedAccessException("unauthorized_access");
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$db = $this->getConnection();
|
||||||
|
|
||||||
|
$sql = "UPDATE articles
|
||||||
|
SET title = :title, content = :content, author = :author, category = :category, tags = :tags
|
||||||
|
WHERE id = :id;";
|
||||||
|
|
||||||
|
$command = $db->prepare($sql);
|
||||||
|
if (!$command) {
|
||||||
|
throw new InternalServerErrorException("internal_error");
|
||||||
|
}
|
||||||
|
|
||||||
|
$success = $command->execute([
|
||||||
|
":id" => $id,
|
||||||
|
":title" => $article->getTitle(),
|
||||||
|
":content" => $article->getContent(),
|
||||||
|
":author" => $author,
|
||||||
|
":category" => $article->getCategory(),
|
||||||
|
":tags" => $article->getTags()
|
||||||
|
]);
|
||||||
|
|
||||||
|
// rowCount() prüft, ob eine Zeile mit dieser ID existierte und geändert werden konnte
|
||||||
|
if (!$success || $command->rowCount() === 0) {
|
||||||
|
// Falls die ID nicht existiert, prüfen wir, ob sie überhaupt da ist
|
||||||
|
if (!$this->getArticle($id)) {
|
||||||
|
throw new NotFoundException("missing_id");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
throw new InternalServerErrorException("internal_error");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function deleteArticle($id, $author)
|
||||||
|
{
|
||||||
|
$article = getArticle($id);
|
||||||
|
if (empty($article)) {
|
||||||
|
throw new NotFoundException("not_found_article");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Berechtigungsprüfung:
|
||||||
|
if ($article->getAuthor() !== $author) {
|
||||||
|
throw new UnauthorizedAccessException("unauthorized_access");
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$db = $this->getConnection();
|
||||||
|
$sql = "DELETE FROM articles WHERE id = :id;";
|
||||||
|
|
||||||
|
$command = $db->prepare($sql);
|
||||||
|
if (!$command) {
|
||||||
|
throw new InternalServerErrorException("internal_error");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$command->execute([":id" => $id])) {
|
||||||
|
throw new InternalServerErrorException("internal_error");
|
||||||
|
}
|
||||||
|
} catch (PDOException $exc) {
|
||||||
|
throw new InternalServerErrorException("internal_error");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getArticle($id)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$db = $this->getConnection();
|
||||||
|
$sql = "SELECT * FROM articles WHERE id = :id;";
|
||||||
|
|
||||||
|
$command = $db->prepare($sql);
|
||||||
|
if (!$command) {
|
||||||
|
throw new InternalServerErrorException("internal_error");
|
||||||
|
}
|
||||||
|
|
||||||
|
$command->execute([":id" => $id]);
|
||||||
|
$row = $command->fetch(PDO::FETCH_ASSOC);
|
||||||
|
|
||||||
|
if ($row) {
|
||||||
|
return new Article(
|
||||||
|
intval($row['id']),
|
||||||
|
$row['title'],
|
||||||
|
$row['content'],
|
||||||
|
$row['author'],
|
||||||
|
$row['category'],
|
||||||
|
$row['tags'],
|
||||||
|
$row['created']
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
throw new InternalServerErrorException("internal_error");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getAllArticles()
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$db = $this->getConnection();
|
||||||
|
$sql = "SELECT * FROM articles;";
|
||||||
|
|
||||||
|
$command = $db->query($sql);
|
||||||
|
if (!$command) {
|
||||||
|
throw new InternalServerErrorException("internal_error");
|
||||||
|
}
|
||||||
|
|
||||||
|
$rows = $command->fetchAll(PDO::FETCH_ASSOC);
|
||||||
|
$articles = [];
|
||||||
|
|
||||||
|
foreach ($rows as $row) {
|
||||||
|
$articles[] = new Article(
|
||||||
|
intval($row['id']),
|
||||||
|
$row['title'],
|
||||||
|
$row['content'],
|
||||||
|
$row['author'],
|
||||||
|
$row['category'],
|
||||||
|
$row['tags'],
|
||||||
|
$row['created']
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $articles;
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
throw new InternalServerErrorException("internal_error");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getArticlesByAuthor($author)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$db = $this->getConnection();
|
||||||
|
$sql = "SELECT * FROM articles WHERE author = :author;";
|
||||||
|
|
||||||
|
$command = $db->prepare($sql);
|
||||||
|
if (!$command) {
|
||||||
|
throw new InternalServerErrorException("internal_error");
|
||||||
|
}
|
||||||
|
|
||||||
|
$command->execute([":author" => $author]);
|
||||||
|
$rows = $command->fetchAll(PDO::FETCH_ASSOC);
|
||||||
|
$filteredArticles = [];
|
||||||
|
|
||||||
|
foreach ($rows as $row) {
|
||||||
|
$filteredArticles[] = new Article(
|
||||||
|
intval($row['id']),
|
||||||
|
$row['title'],
|
||||||
|
$row['content'],
|
||||||
|
$row['author'],
|
||||||
|
$row['category'],
|
||||||
|
$row['tags'],
|
||||||
|
$row['created']
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $filteredArticles;
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
throw new InternalServerErrorException("internal_error");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getArticlesByCategory($category)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$db = $this->getConnection();
|
||||||
|
$sql = "SELECT * FROM articles WHERE category = :category;";
|
||||||
|
|
||||||
|
$command = $db->prepare($sql);
|
||||||
|
if (!$command) {
|
||||||
|
throw new InternalServerErrorException("internal_error");
|
||||||
|
}
|
||||||
|
|
||||||
|
$command->execute([":category" => $category]);
|
||||||
|
$rows = $command->fetchAll(PDO::FETCH_ASSOC);
|
||||||
|
$filteredArticles = [];
|
||||||
|
|
||||||
|
foreach ($rows as $row) {
|
||||||
|
$filteredArticles[] = new Article(
|
||||||
|
intval($row['id']),
|
||||||
|
$row['title'],
|
||||||
|
$row['content'],
|
||||||
|
$row['author'],
|
||||||
|
$row['category'],
|
||||||
|
$row['tags'],
|
||||||
|
$row['created']
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $filteredArticles;
|
||||||
|
} catch (PDOException $exc) {
|
||||||
|
throw new InternalServerErrorException("internal_error");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function search(string $keyword): array
|
||||||
|
{
|
||||||
|
// TODO: implement search()
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,261 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
require_once "UserManagerDAO.php";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Klasse für den Zugriff auf Benutzerdaten über eine SQLite-Datenbank.
|
||||||
|
*
|
||||||
|
* Diese Klasse verwendet PDO, Prepared Statements und speichert
|
||||||
|
* Benutzerdaten in der Datei db/users.db.
|
||||||
|
*/
|
||||||
|
class DatabaseUserManager implements UserManagerDAO {
|
||||||
|
|
||||||
|
private static $instance = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Konstruktor.
|
||||||
|
*
|
||||||
|
* Erstellt die Benutzerdatenbank und die Tabelle users,
|
||||||
|
* falls diese noch nicht existieren.
|
||||||
|
*
|
||||||
|
* @throws RuntimeException wenn die Datenbank nicht erstellt werden kann
|
||||||
|
*/
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$db = $this->getConnection();
|
||||||
|
|
||||||
|
$db->exec("
|
||||||
|
CREATE TABLE IF NOT EXISTS users (
|
||||||
|
email TEXT PRIMARY KEY,
|
||||||
|
vorname TEXT NOT NULL,
|
||||||
|
nachname TEXT NOT NULL,
|
||||||
|
password TEXT NOT NULL
|
||||||
|
);
|
||||||
|
");
|
||||||
|
|
||||||
|
unset($db);
|
||||||
|
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
throw new RuntimeException("Benutzerdatenbank konnte nicht erstellt werden.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Baut eine Verbindung zur SQLite-Datenbank auf.
|
||||||
|
*
|
||||||
|
* @return PDO Datenbankverbindung
|
||||||
|
*
|
||||||
|
* @throws RuntimeException wenn keine Verbindung hergestellt werden kann
|
||||||
|
*/
|
||||||
|
private function getConnection()
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$dsn = 'sqlite:' . __DIR__ . '/../../db/users.db';
|
||||||
|
|
||||||
|
$db = new PDO($dsn, null, null);
|
||||||
|
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||||
|
|
||||||
|
return $db;
|
||||||
|
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
throw new RuntimeException("Verbindung zur Benutzerdatenbank fehlgeschlagen.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gibt die Singleton-Instanz des DatabaseUserManagers zurück.
|
||||||
|
*
|
||||||
|
* @return DatabaseUserManager Instanz des DatabaseUserManagers
|
||||||
|
*/
|
||||||
|
public static function getInstance()
|
||||||
|
{
|
||||||
|
if (self::$instance == null) {
|
||||||
|
self::$instance = new DatabaseUserManager();
|
||||||
|
}
|
||||||
|
|
||||||
|
return self::$instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sucht einen Benutzer anhand seiner E-Mail-Adresse.
|
||||||
|
*
|
||||||
|
* @param string $email E-Mail-Adresse des Benutzers
|
||||||
|
*
|
||||||
|
* @return array|null Benutzerdaten als Array oder null,
|
||||||
|
* wenn kein Benutzer gefunden wurde
|
||||||
|
*
|
||||||
|
* @throws RuntimeException wenn der Benutzer nicht geladen werden kann
|
||||||
|
*/
|
||||||
|
public function findUser($email)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$db = $this->getConnection();
|
||||||
|
|
||||||
|
$sql = "SELECT * FROM users WHERE email = :email";
|
||||||
|
$command = $db->prepare($sql);
|
||||||
|
|
||||||
|
$command->execute([
|
||||||
|
":email" => $email
|
||||||
|
]);
|
||||||
|
|
||||||
|
$user = $command->fetch(PDO::FETCH_ASSOC);
|
||||||
|
|
||||||
|
return $user ?: null;
|
||||||
|
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
throw new RuntimeException("Benutzer konnte nicht geladen werden.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fügt einen neuen Benutzer in die Datenbank ein.
|
||||||
|
*
|
||||||
|
* Die Speicherung erfolgt innerhalb einer Transaktion.
|
||||||
|
* Doppelte E-Mail-Adressen werden durch den Primary Key verhindert.
|
||||||
|
*
|
||||||
|
* @param string $email E-Mail-Adresse des Benutzers
|
||||||
|
* @param string $vorname Vorname des Benutzers
|
||||||
|
* @param string $nachname Nachname des Benutzers
|
||||||
|
* @param string $password Passwort-Hash des Benutzers
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*
|
||||||
|
* @throws InvalidArgumentException wenn die E-Mail-Adresse bereits verwendet wird
|
||||||
|
* @throws RuntimeException wenn der Benutzer nicht gespeichert werden kann
|
||||||
|
*/
|
||||||
|
public function addUser($email, $vorname, $nachname, $password)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$db = $this->getConnection();
|
||||||
|
$db->beginTransaction();
|
||||||
|
|
||||||
|
$sql = "
|
||||||
|
INSERT INTO users (email, vorname, nachname, password)
|
||||||
|
VALUES (:email, :vorname, :nachname, :password)
|
||||||
|
";
|
||||||
|
|
||||||
|
$command = $db->prepare($sql);
|
||||||
|
|
||||||
|
$command->execute([
|
||||||
|
":email" => $email,
|
||||||
|
":vorname" => $vorname,
|
||||||
|
":nachname" => $nachname,
|
||||||
|
":password" => $password
|
||||||
|
]);
|
||||||
|
|
||||||
|
$db->commit();
|
||||||
|
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
if (isset($db) && $db->inTransaction()) {
|
||||||
|
$db->rollBack();
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($e->getCode() === "23000") {
|
||||||
|
throw new InvalidArgumentException("Diese E-Mail-Adresse wird bereits verwendet.");
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new RuntimeException("Benutzer konnte nicht gespeichert werden.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Aktualisiert die Daten eines bestehenden Benutzers.
|
||||||
|
*
|
||||||
|
* Optional kann zusätzlich das Passwort geändert werden.
|
||||||
|
* Wenn kein neues Passwort übergeben wird, bleibt das alte Passwort erhalten.
|
||||||
|
*
|
||||||
|
* @param string $oldEmail Aktuelle E-Mail-Adresse des Benutzers
|
||||||
|
* @param string $newEmail Neue E-Mail-Adresse des Benutzers
|
||||||
|
* @param string $vorname Neuer Vorname des Benutzers
|
||||||
|
* @param string $nachname Neuer Nachname des Benutzers
|
||||||
|
* @param string|null $password Neues Passwort oder null
|
||||||
|
*
|
||||||
|
* @return bool true, wenn der Benutzer aktualisiert wurde,
|
||||||
|
* sonst false
|
||||||
|
*
|
||||||
|
* @throws InvalidArgumentException wenn die neue E-Mail-Adresse bereits verwendet wird
|
||||||
|
* @throws RuntimeException wenn der Benutzer nicht aktualisiert werden kann
|
||||||
|
*/
|
||||||
|
public function updateUser($oldEmail, $newEmail, $vorname, $nachname, $password = null)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$db = $this->getConnection();
|
||||||
|
|
||||||
|
if (!empty($password)) {
|
||||||
|
$sql = "
|
||||||
|
UPDATE users
|
||||||
|
SET email = :newEmail,
|
||||||
|
vorname = :vorname,
|
||||||
|
nachname = :nachname,
|
||||||
|
password = :password
|
||||||
|
WHERE email = :oldEmail
|
||||||
|
";
|
||||||
|
|
||||||
|
$params = [
|
||||||
|
":newEmail" => $newEmail,
|
||||||
|
":vorname" => $vorname,
|
||||||
|
":nachname" => $nachname,
|
||||||
|
":password" => password_hash($password, PASSWORD_DEFAULT),
|
||||||
|
":oldEmail" => $oldEmail
|
||||||
|
];
|
||||||
|
} else {
|
||||||
|
$sql = "
|
||||||
|
UPDATE users
|
||||||
|
SET email = :newEmail,
|
||||||
|
vorname = :vorname,
|
||||||
|
nachname = :nachname
|
||||||
|
WHERE email = :oldEmail
|
||||||
|
";
|
||||||
|
|
||||||
|
$params = [
|
||||||
|
":newEmail" => $newEmail,
|
||||||
|
":vorname" => $vorname,
|
||||||
|
":nachname" => $nachname,
|
||||||
|
":oldEmail" => $oldEmail
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
$command = $db->prepare($sql);
|
||||||
|
$command->execute($params);
|
||||||
|
|
||||||
|
return $command->rowCount() > 0;
|
||||||
|
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
if ($e->getCode() === "23000") {
|
||||||
|
throw new InvalidArgumentException("Diese E-Mail-Adresse wird bereits verwendet.");
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new RuntimeException("Benutzer konnte nicht aktualisiert werden.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Löscht einen Benutzer anhand seiner E-Mail-Adresse.
|
||||||
|
*
|
||||||
|
* @param string $email E-Mail-Adresse des zu löschenden Benutzers
|
||||||
|
*
|
||||||
|
* @return bool true, wenn der Benutzer gelöscht wurde,
|
||||||
|
* sonst false
|
||||||
|
*
|
||||||
|
* @throws RuntimeException wenn der Benutzer nicht gelöscht werden kann
|
||||||
|
*/
|
||||||
|
public function deleteUser($email)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$db = $this->getConnection();
|
||||||
|
|
||||||
|
$sql = "DELETE FROM users WHERE email = :email";
|
||||||
|
$command = $db->prepare($sql);
|
||||||
|
|
||||||
|
$command->execute([
|
||||||
|
":email" => $email
|
||||||
|
]);
|
||||||
|
|
||||||
|
return $command->rowCount() > 0;
|
||||||
|
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
throw new RuntimeException("Benutzer konnte nicht gelöscht werden.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -66,7 +66,7 @@ class LocalArticleManager implements ArticleManagerDAO {
|
|||||||
public function updateArticle($id, $article, $author)
|
public function updateArticle($id, $article, $author)
|
||||||
{
|
{
|
||||||
if (empty($article)) {
|
if (empty($article)) {
|
||||||
throw new NotFoundException("not_found_article");
|
throw new InternalServerErrorException("internal_error");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Berechtigungsprüfung:
|
// Berechtigungsprüfung:
|
||||||
|
|||||||
@@ -6,6 +6,10 @@ class LocalUserManager implements UserManagerDAO {
|
|||||||
|
|
||||||
private string $file = "data/users.json";
|
private string $file = "data/users.json";
|
||||||
|
|
||||||
|
public static function getInstance(){
|
||||||
|
// TODO: implement the getIsntance method.
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Lädt alle Benutzer aus der JSON-Datei.
|
* Lädt alle Benutzer aus der JSON-Datei.
|
||||||
*
|
*
|
||||||
@@ -158,8 +162,6 @@ class LocalUserManager implements UserManagerDAO {
|
|||||||
/**
|
/**
|
||||||
* Löscht einen Benutzer anhand seiner E-Mail-Adresse.
|
* Löscht einen Benutzer anhand seiner E-Mail-Adresse.
|
||||||
*
|
*
|
||||||
* TODO: wenn ein Benutzer gelöscht wird, sollten dann auch seine Beiträge gelöscht werden?
|
|
||||||
*
|
|
||||||
* @param string $email E-Mail-Adresse des zu löschenden Benutzers
|
* @param string $email E-Mail-Adresse des zu löschenden Benutzers
|
||||||
* @return bool true, wenn der Benutzer gelöscht wurde, sonst false
|
* @return bool true, wenn der Benutzer gelöscht wurde, sonst false
|
||||||
* @throws RuntimeException wenn die Benutzerdaten nicht gelesen oder gespeichert werden können
|
* @throws RuntimeException wenn die Benutzerdaten nicht gelesen oder gespeichert werden können
|
||||||
|
|||||||
@@ -1,3 +1,32 @@
|
|||||||
<?php
|
<?php
|
||||||
// TODO: implement this.
|
|
||||||
// dummy-user anlegen: - `max.mustermann, test123, mustermann@web.de`
|
require_once "LocalUserManager.php";
|
||||||
|
require_once "DatabaseUserManager.php";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Zentrale Klasse für den Zugriff auf Benutzerdaten.
|
||||||
|
*
|
||||||
|
* Hier kann zwischen lokaler Speicherung und Datenbankspeicherung
|
||||||
|
* gewechselt werden.
|
||||||
|
*/
|
||||||
|
class UserManager {
|
||||||
|
|
||||||
|
public static function getInstance() {
|
||||||
|
$userManager = DatabaseUserManager::getInstance();
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Dummy-User anlegen, falls er noch nicht existiert.
|
||||||
|
* Passwort: test123
|
||||||
|
*/
|
||||||
|
if ($userManager->findUser("mustermann@web.de") == null) {
|
||||||
|
$userManager->addUser(
|
||||||
|
"mustermann@web.de",
|
||||||
|
"Max",
|
||||||
|
"Mustermann",
|
||||||
|
password_hash("test12345", PASSWORD_DEFAULT)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $userManager;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,13 +1,33 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prüft, ob ein Vor- oder Nachname gültig ist.
|
||||||
|
*
|
||||||
|
* Erlaubt werden Buchstaben, Umlaute, Leerzeichen und Bindestriche.
|
||||||
|
* Die Länge muss zwischen 2 und 20 Zeichen liegen.
|
||||||
|
*
|
||||||
|
* @param string $name Zu prüfender Name
|
||||||
|
*
|
||||||
|
* @return bool true wenn der Name gültig ist, sonst false
|
||||||
|
*/
|
||||||
function userNameValidator($name)
|
function userNameValidator($name)
|
||||||
{
|
{
|
||||||
$name = trim($name);
|
$name = trim($name);
|
||||||
$namePattern = '/^[a-zA-ZäöüÄÖÜß\s-]{2,50}$/u';
|
$namePattern = '/^[a-zA-ZäöüÄÖÜß\s-]{2,20}$/u';
|
||||||
|
|
||||||
return preg_match($namePattern, $name) === 1;
|
return preg_match($namePattern, $name) === 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prüft, ob eine E-Mail-Adresse gültig ist.
|
||||||
|
*
|
||||||
|
* Die E-Mail-Adresse muss dem Standardformat entsprechen
|
||||||
|
* und darf höchstens 100 Zeichen lang sein.
|
||||||
|
*
|
||||||
|
* @param string $email Zu prüfende E-Mail-Adresse
|
||||||
|
*
|
||||||
|
* @return bool true wenn die E-Mail-Adresse gültig ist, sonst false
|
||||||
|
*/
|
||||||
function userEmailValidator($email)
|
function userEmailValidator($email)
|
||||||
{
|
{
|
||||||
$email = trim($email);
|
$email = trim($email);
|
||||||
@@ -16,13 +36,35 @@ function userEmailValidator($email)
|
|||||||
&& mb_strlen($email) <= 100;
|
&& mb_strlen($email) <= 100;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prüft, ob ein Passwort die Längenanforderungen erfüllt.
|
||||||
|
*
|
||||||
|
* Das Passwort muss zwischen 5 und 12 Zeichen lang sein.
|
||||||
|
*
|
||||||
|
* @param string $password Zu prüfendes Passwort
|
||||||
|
*
|
||||||
|
* @return bool true wenn das Passwort gültig ist, sonst false
|
||||||
|
*/
|
||||||
function userPasswordValidator($password)
|
function userPasswordValidator($password)
|
||||||
{
|
{
|
||||||
$zeichenAnzahl = mb_strlen($password);
|
$zeichenAnzahl = mb_strlen($password);
|
||||||
|
|
||||||
return $zeichenAnzahl >= 8 && $zeichenAnzahl <= 72;
|
return $zeichenAnzahl >= 5 && $zeichenAnzahl <= 12;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prüft ein optionales Passwort.
|
||||||
|
*
|
||||||
|
* Ein leeres Passwort ist erlaubt und bedeutet,
|
||||||
|
* dass das bestehende Passwort unverändert bleibt.
|
||||||
|
* Falls ein Passwort angegeben wurde, wird es
|
||||||
|
* mit den normalen Passwortregeln geprüft.
|
||||||
|
*
|
||||||
|
* @param string|null $password Zu prüfendes Passwort
|
||||||
|
*
|
||||||
|
* @return bool true wenn das Passwort gültig oder leer ist,
|
||||||
|
* sonst false
|
||||||
|
*/
|
||||||
function userOptionalPasswordValidator($password)
|
function userOptionalPasswordValidator($password)
|
||||||
{
|
{
|
||||||
if (!isset($password) || $password === '') {
|
if (!isset($password) || $password === '') {
|
||||||
|
|||||||
Reference in New Issue
Block a user