From 5f573a468f8738f19e29180fb4242007c113cffc Mon Sep 17 00:00:00 2001 From: Kilian Hofmann Date: Fri, 21 Jun 2024 12:47:08 +0200 Subject: [PATCH] Login/Register/confirm and new comment feature --- base/helpers.php | 16 ++++ base/icons.php | 7 ++ tasks/calc/index.php | 2 +- tasks/guestBook/components/newComment.php | 2 +- tasks/guestBookDB/actions/comment.php | 32 ++++---- tasks/guestBookDB/actions/login.php | 74 ++++++++++++++++++ tasks/guestBookDB/actions/logout.php | 24 ++++++ tasks/guestBookDB/actions/register.php | 87 +++++++++++++++++++++ tasks/guestBookDB/components/message.php | 7 ++ tasks/guestBookDB/components/newComment.php | 17 +--- tasks/guestBookDB/confirm.php | 44 +++++++++++ tasks/guestBookDB/index.php | 28 +++++-- tasks/guestBookDB/login.php | 86 ++++++++++++++++++++ tasks/guestBookDB/queries.php | 43 ++++++++++ tasks/guestBookDB/register.php | 87 +++++++++++++++++++++ 15 files changed, 516 insertions(+), 40 deletions(-) create mode 100644 base/helpers.php create mode 100644 tasks/guestBookDB/actions/login.php create mode 100644 tasks/guestBookDB/actions/logout.php create mode 100644 tasks/guestBookDB/actions/register.php create mode 100644 tasks/guestBookDB/components/message.php create mode 100644 tasks/guestBookDB/confirm.php create mode 100644 tasks/guestBookDB/login.php create mode 100644 tasks/guestBookDB/register.php diff --git a/base/helpers.php b/base/helpers.php new file mode 100644 index 0000000..a9b4d22 --- /dev/null +++ b/base/helpers.php @@ -0,0 +1,16 @@ + + + + + + + + \ No newline at end of file diff --git a/tasks/calc/index.php b/tasks/calc/index.php index fe42874..857c615 100644 --- a/tasks/calc/index.php +++ b/tasks/calc/index.php @@ -108,7 +108,7 @@

Result:

- +

diff --git a/tasks/guestBook/components/newComment.php b/tasks/guestBook/components/newComment.php index 2656c27..959e36c 100644 --- a/tasks/guestBook/components/newComment.php +++ b/tasks/guestBook/components/newComment.php @@ -1,4 +1,4 @@ -
+

Leave a Comment!

diff --git a/tasks/guestBookDB/actions/comment.php b/tasks/guestBookDB/actions/comment.php index d91f03f..acd167b 100644 --- a/tasks/guestBookDB/actions/comment.php +++ b/tasks/guestBookDB/actions/comment.php @@ -1,31 +1,25 @@ 0) { return; } -$string = file_get_contents("../data/data.json") ?? "[]"; -$json = json_decode($string); +$db = DB::openConnection(); -array_push($json, ["time" => $time, "title" => $title, "name" => $name, "comment" => $comment]); +$stmt = $db->prepare($insertCommentQuery); +$stmt->bindValue(":UID", $user["id"]); +$stmt->bindValue(":COM", $comment); +$stmt->execute(); -file_put_contents("../data/data.json", json_encode($json)); +DB::closeConnection($db); Headers::redirect("../"); diff --git a/tasks/guestBookDB/actions/login.php b/tasks/guestBookDB/actions/login.php new file mode 100644 index 0000000..cdf3f48 --- /dev/null +++ b/tasks/guestBookDB/actions/login.php @@ -0,0 +1,74 @@ + 0) { + Headers::redirect("../login"); + return; +} + +$db = DB::openConnection(); + +$stmt = $db->prepare($loginQuery); +$stmt->bindValue(":USR", $username); +$stmt->execute(); +$user = $stmt->fetch(); + +if ($user) { + if (password_verify($password, $user["passwort"])) { + $_SESSION["user"] = $user; + // REHASH for safety should it somehow change + if (password_needs_rehash($user["passwort"], PASSWORD_DEFAULT)) { + $newHash = password_hash($password, PASSWORD_DEFAULT); + $stmt = $db->prepare($updatePasswordQuery); + $stmt->bindValue(":PAS", $newHash); + $stmt->bindValue(":UID", $user["id"]); + $stmt->execute(); + } + unset($_SESSION["user"]["passwort"]); + unset($_SESSION["user"]["confirmationcode"]); + } else { + array_push($_SESSION["error"], "Username or Password incorrect."); + } +} else { + array_push($_SESSION["error"], "Username or Password incorrect."); +} + +DB::closeConnection($db); + +if (count($_SESSION["error"]) > 0) { + Headers::redirect("../login"); + return; +} + +Headers::redirect("../"); diff --git a/tasks/guestBookDB/actions/logout.php b/tasks/guestBookDB/actions/logout.php new file mode 100644 index 0000000..20b6810 --- /dev/null +++ b/tasks/guestBookDB/actions/logout.php @@ -0,0 +1,24 @@ + 0) { + Headers::redirect("../register"); + return; +} + +$db = DB::openConnection(); + +try { + $guid = guidv4(); + + $stmt = $db->prepare($insertUserQuery); + $stmt->bindValue(":USR", $username); + $stmt->bindValue(":PAS", password_hash($password, PASSWORD_DEFAULT)); + $stmt->bindValue(":EMA", $email); + $stmt->bindValue(":COD", $guid); + $stmt->execute(); + + mail( + $email, + "Account activation GuestBookDB", + "Hello $username. To activate your account, visit https://userpage.fu-berlin.de/khofmann/phpCourse/tasks/guestBookDB/confirm?c=$guid" + ); + + array_push($_SESSION["message"], "Please confirm your account using the mail we sent you."); +} catch (PDOException $e) { + if ($e->getCode() === "23000") { + array_push($_SESSION["error"], "A user with this username or email already exists"); + } else { + array_push($_SESSION["error"], "SQL Error: {$e->getMessage()}"); + } + Headers::redirect("../register"); + return; +} + +DB::closeConnection($db); + +Headers::redirect("../login"); diff --git a/tasks/guestBookDB/components/message.php b/tasks/guestBookDB/components/message.php new file mode 100644 index 0000000..7b4a5fc --- /dev/null +++ b/tasks/guestBookDB/components/message.php @@ -0,0 +1,7 @@ +
+ + + + + +
\ No newline at end of file diff --git a/tasks/guestBookDB/components/newComment.php b/tasks/guestBookDB/components/newComment.php index 18d3869..13904b2 100644 --- a/tasks/guestBookDB/components/newComment.php +++ b/tasks/guestBookDB/components/newComment.php @@ -1,20 +1,7 @@ - -

Under Construction

-
+ +

Leave a Comment!

-
- -
- Please enter a title. -
-
-
- -
- Please enter a name. -
-
diff --git a/tasks/guestBookDB/confirm.php b/tasks/guestBookDB/confirm.php new file mode 100644 index 0000000..d735721 --- /dev/null +++ b/tasks/guestBookDB/confirm.php @@ -0,0 +1,44 @@ +prepare($confirmFetchUserQuery); +$stmt->bindValue(":COD", $code); +$stmt->execute(); +$uid = $stmt->fetch(PDO::FETCH_COLUMN); + +if ($uid !== false) { + $stmt = $db->prepare($confirmUserQuery); + $stmt->bindValue(":UID", $uid); + $stmt->execute(); + + array_push($_SESSION["message"], "Account confirmed, you can now log in!"); +} else { + array_push($_SESSION["error"], "Account could not be confirmed"); +} + +DB::closeConnection($db); + +Headers::redirect("./login"); diff --git a/tasks/guestBookDB/index.php b/tasks/guestBookDB/index.php index a841dc7..d1afea3 100644 --- a/tasks/guestBookDB/index.php +++ b/tasks/guestBookDB/index.php @@ -27,6 +27,7 @@ session_name("PHP_SESSION_guestBook"); session_start(); $errors = $_SESSION["error"] ?? []; + $user = $_SESSION["user"] ?? null; $_SESSION["error"] = []; if (isset($_GET["p"]) && !is_numeric($_GET["p"])) { @@ -37,8 +38,6 @@ $db = DB::openConnection(); - - $stmt = $db->prepare($countQuery); $stmt->execute(); $maxPage = intdiv($stmt->fetch(PDO::FETCH_COLUMN), 9); @@ -55,13 +54,21 @@ ?>
-