stylehub/lib/account.php

66 lines
2.2 KiB
PHP
Raw Normal View History

2024-01-16 19:24:40 +01:00
<?php
function isLoggedIn(){
return $_SESSION["ID"] > 0 && !empty($_SESSION["email"]);
}
2024-01-18 11:49:38 +01:00
function doLogin($email, $password){
2024-01-16 19:24:40 +01:00
global $mysqli;
2024-01-18 11:49:38 +01:00
if(!empty($email) && !empty($password)){
2024-01-16 19:24:40 +01:00
$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();
2024-01-18 11:49:38 +01:00
2024-01-16 19:24:40 +01:00
$idcko = 0;
$fname = "";
$lname = "";
$nickname = "";
$pwdhash = "";
$mcnick = "";
2024-01-16 20:43:57 +01:00
$stmt->bind_result($idcko, $fname, $lname, $nickname, $pwdhash, $mcnick, false);
$found = false;
if($stmt->num_rows() > 0){
$stmt->fetch();
2024-01-18 11:49:38 +01:00
if (password_verify($password, $pwdhash)){
2024-01-16 19:24:40 +01:00
$_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();
2024-01-18 11:49:38 +01:00
return $found ? ["status" => "success"] : ["status" => "fail"];
2024-01-16 19:24:40 +01:00
}
}
function doLogout(){
if(isLoggedIn()){
session_destroy();
2024-01-18 11:49:38 +01:00
return ["status" => "success"];
} else {
return ["status" => "fail"];
2024-01-16 19:24:40 +01:00
}
}
2024-01-18 11:49:38 +01:00
function doRegister($firstname, $lastname, $nickname, $email, $password, $minecraftnick, $activationtoken){
global $mysqli;
2024-01-16 19:24:40 +01:00
$status = ["status" => "fail"];
2024-01-18 11:49:38 +01:00
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";
2024-01-16 19:24:40 +01:00
}
2024-01-18 11:49:38 +01:00
$stmt->close();
2024-01-16 19:24:40 +01:00
}
2024-01-18 11:49:38 +01:00
return $status;
2024-01-16 19:24:40 +01:00
}