adlerka.top/lib/account.php
2024-01-18 11:53:39 +01:00

66 lines
2.2 KiB
PHP

<?php
function isLoggedIn(){
return $_SESSION["ID"] > 0 && !empty($_SESSION["email"]);
}
function doLogin($email, $password){
global $mysqli;
$found = false;
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 = "";
$stmt->bind_result($idcko, $fname, $lname, $nickname, $pwdhash, $mcnick, false);
if($stmt->num_rows() > 0){
$stmt->fetch();
if (password_verify($password, $pwdhash)){
$_SESSION["ID"] = $idcko;
$_SESSION["first_name"] = $fname;
$_SESSION["last_name"] = $lname;
$_SESSION["nickname"] = $nickname;
$_SESSION["email"] = $email;
$_SESSION["mcnick"] = $mcnick;
$_SESSION["isadmin"] = false;
$found = true;
}
}
$stmt->close();
}
return $found ? ["status" => "success"] : ["status" => "fail"];
}
function doLogout(){
if(isLoggedIn()){
session_destroy();
return ["status" => "success"];
} else {
return ["status" => "fail"];
}
}
function doRegister($firstname, $lastname, $nickname, $email, $password, $minecraftnick, $activationtoken){
global $mysqli;
$status = ["status" => "fail"];
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();
}
return $status;
}