From e4bb8f10a3e2abbbbe3221465f30de2d6fbbfd7c Mon Sep 17 00:00:00 2001 From: bruno Date: Thu, 18 Jan 2024 11:49:38 +0100 Subject: [PATCH] big changes hehe --- endpoints/global/account.php | 22 ++++++++++ index.php | 18 ++++++-- lib/account.php | 61 +++++++++------------------- lib/config.php | 2 + lib/endpoint.php | 41 +++++++++++++++++++ lib/{navpages.php => navigation.php} | 0 lib/{routing.php => page.php} | 48 +--------------------- lib/router.php | 55 +++++++++++++++++++++++++ pages/global/account.php | 2 +- 9 files changed, 155 insertions(+), 94 deletions(-) create mode 100644 endpoints/global/account.php create mode 100644 lib/endpoint.php rename lib/{navpages.php => navigation.php} (100%) rename lib/{routing.php => page.php} (51%) create mode 100644 lib/router.php diff --git a/endpoints/global/account.php b/endpoints/global/account.php new file mode 100644 index 0000000..cb776cb --- /dev/null +++ b/endpoints/global/account.php @@ -0,0 +1,22 @@ + 0 && !empty($_SESSION["email"]); } -function doLogin(){ +function doLogin($email, $password){ global $mysqli; - if(!empty($_POST["email"]) && !empty($_POST["password"])){ - $email = $_POST["email"]; - $pass = $_POST["password"]; - /* prepare statement */ + if(!empty($email) && !empty($password)){ $stmt = $mysqli->prepare("SELECT ID, FirstName, LastName, Nickname, PasswordHash, MinecraftNick, isAdmin FROM Users WHERE EMAIL = ? AND isActive = 1"); $stmt->bind_param("s", $email); $stmt->execute(); + $idcko = 0; $fname = ""; $lname = ""; $nickname = ""; $pwdhash = ""; $mcnick = ""; - /* bind variables to prepared statement */ $stmt->bind_result($idcko, $fname, $lname, $nickname, $pwdhash, $mcnick, false); - /* fetch values */ $found = false; if($stmt->num_rows() > 0){ $stmt->fetch(); - if (password_verify($pass, $pwdhash)){ + if (password_verify($password, $pwdhash)){ $_SESSION["ID"] = $idcko; $_SESSION["first_name"] = $fname; $_SESSION["last_name"] = $lname; @@ -38,51 +34,32 @@ function doLogin(){ } } $stmt->close(); - if($found){ - $status = ["status" => "success"]; - } - else{ - $status = ["status" => "fail"]; - } - echo json_encode($status); + return $found ? ["status" => "success"] : ["status" => "fail"]; } } function doLogout(){ if(isLoggedIn()){ session_destroy(); - $status = ["status" => "success"]; + return ["status" => "success"]; + } else { + return ["status" => "fail"]; } - else{ - $status = ["status" => "fail"]; - } - echo json_encode($status); } -function doRegister(){ +function doRegister($firstname, $lastname, $nickname, $email, $password, $minecraftnick, $activationtoken){ + global $mysqli; $status = ["status" => "fail"]; - if (!empty($_POST["activationtoken"])){ - global $mysqli; - - $firstName = $_POST["firstname"]; - $lastName = $_POST["lastname"]; - $nickname = $_POST["nickname"]; - $email = $_POST["email"]; - $password = $_POST["password"]; - $minecraftNick = $_POST["minecraftnick"]; - $activationToken = $_POST["activationtoken"]; - if (!empty($firstName) && !empty($lastName) && !empty($nickname) && !empty($email) && !empty($password)) { - $passwordHash = password_hash($password, PASSWORD_DEFAULT); - - $stmt = $mysqli->prepare("UPDATE Users SET FirstName = ?, LastName = ?, Nickname = ?, Email = ?, PasswordHash = ?, MinecraftNick = ?, isAdmin = 0, isActivated = 1 WHERE isActivated = 0 AND ActivationToken = ?"); - $stmt->bind_param("ssssss", $firstName, $lastName, $nickname, $email, $passwordHash, $minecraftNick, $activationToken); - $stmt->execute(); - if ($stmt->affected_rows > 0) { - $status["status"] = "success"; - } - $stmt->close(); + if (!empty($activationtoken)){ + $passwordHash = password_hash($password, PASSWORD_DEFAULT); + $stmt = $mysqli->prepare("UPDATE Users SET FirstName = ?, LastName = ?, Nickname = ?, Email = ?, PasswordHash = ?, MinecraftNick = ?, isAdmin = 0, isActivated = 1 WHERE isActivated = 0 AND ActivationToken = ?"); + $stmt->bind_param("sssssss", $firstname, $lastname, $nickname, $email, $passwordHash, $minecraftnick, $activationtoken); + $stmt->execute(); + if ($stmt->affected_rows > 0) { + $status["status"] = "success"; } + $stmt->close(); } - echo json_encode($status); + return $status; } diff --git a/lib/config.php b/lib/config.php index 9d02198..62f4559 100644 --- a/lib/config.php +++ b/lib/config.php @@ -8,6 +8,8 @@ $routerConfig["template_dir"] = "templates/"; + $routerConfig["endpoint_dir"] = "endpoints/"; + $routerConfig["page_dir"] = "pages/"; $routerConfig["protocol"] = "https://"; diff --git a/lib/endpoint.php b/lib/endpoint.php new file mode 100644 index 0000000..8e50f6d --- /dev/null +++ b/lib/endpoint.php @@ -0,0 +1,41 @@ +