stylehub/lib/account.php

89 lines
2.9 KiB
PHP
Raw Normal View History

2024-01-16 19:24:40 +01:00
<?php
function isLoggedIn(){
return $_SESSION["ID"] > 0 && !empty($_SESSION["email"]);
}
function doLogin(){
global $mysqli;
if(!empty($_POST["email"]) && !empty($_POST["password"])){
$email = $_POST["email"];
$pass = $_POST["password"];
/* prepare statement */
$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 */
2024-01-16 20:43:57 +01:00
$stmt->bind_result($idcko, $fname, $lname, $nickname, $pwdhash, $mcnick, false);
2024-01-16 19:24:40 +01:00
/* fetch values */
2024-01-16 20:43:57 +01:00
$found = false;
if($stmt->num_rows() > 0){
$stmt->fetch();
2024-01-16 19:24:40 +01:00
if (password_verify($pass, $pwdhash)){
$_SESSION["ID"] = $idcko;
$_SESSION["first_name"] = $fname;
$_SESSION["last_name"] = $lname;
$_SESSION["nickname"] = $nickname;
$_SESSION["email"] = $email;
$_SESSION["mcnick"] = $mcnick;
2024-01-16 20:43:57 +01:00
$_SESSION["isadmin"] = false;
$found = true;
2024-01-16 19:24:40 +01:00
}
}
$stmt->close();
if($found){
$status = ["status" => "success"];
}
else{
$status = ["status" => "fail"];
}
echo json_encode($status);
}
}
function doLogout(){
if(isLoggedIn()){
session_destroy();
$status = ["status" => "success"];
}
else{
$status = ["status" => "fail"];
}
echo json_encode($status);
}
function doRegister(){
$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"];
2024-01-16 20:43:57 +01:00
if (!empty($firstName) && !empty($lastName) && !empty($nickname) && !empty($email) && !empty($password)) {
2024-01-16 19:24:40 +01:00
$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();
}
}
2024-01-16 20:43:57 +01:00
echo json_encode($status);
2024-01-16 19:24:40 +01:00
}