Add PHPDocs generated by ChatGPT,
add additional clarification to some functions, add addNewsComment function and API, currently untested and not implemented in the client, fix a bunch of stuff that PHPStorm pointed out
This commit is contained in:
201
lib/account.php
201
lib/account.php
@@ -1,52 +1,95 @@
|
||||
<?php
|
||||
|
||||
use Random\RandomException;
|
||||
|
||||
/**
|
||||
* Checks if the current session represents a logged-in user.
|
||||
*
|
||||
* @global array $routerConfig Global configuration array containing permission thresholds.
|
||||
* @return bool Returns true if the user is logged in and meets the minimum privilege level; otherwise, false.
|
||||
*/
|
||||
function isLoggedIn(): bool
|
||||
{
|
||||
global $routerConfig;
|
||||
return $_SESSION["ID"] > 0 && !empty($_SESSION["email"]) && $_SESSION["privilege_level"] >= $routerConfig["permissions"]["logged_in_default"];
|
||||
}
|
||||
/**
|
||||
* Checks if the logged-in user is verified.
|
||||
*
|
||||
* @global array $routerConfig Global configuration array containing permission thresholds.
|
||||
* @return bool Returns true if the user is logged in and verified; otherwise, false.
|
||||
*/
|
||||
function isVerified(): bool
|
||||
{
|
||||
global $routerConfig;
|
||||
return isLoggedIn() && $_SESSION["privilege_level"] >= $routerConfig["permissions"]["verified"];
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the logged-in user is trustworthy.
|
||||
*
|
||||
* @global array $routerConfig Global configuration array containing permission thresholds.
|
||||
* @return bool Returns true if the user is logged in and considered trustworthy; otherwise, false.
|
||||
*/
|
||||
function isTrustWorthy(): bool
|
||||
{
|
||||
global $routerConfig;
|
||||
return isLoggedIn() && $_SESSION["privilege_level"] >= $routerConfig["permissions"]["trustworthy"];
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the logged-in user is a moderator.
|
||||
*
|
||||
* @global array $routerConfig Global configuration array containing permission thresholds.
|
||||
* @return bool Returns true if the user is logged in and a moderator; otherwise, false.
|
||||
*/
|
||||
function isModerator(): bool
|
||||
{
|
||||
global $routerConfig;
|
||||
return isLoggedIn() && $_SESSION["privilege_level"] >= $routerConfig["permissions"]["moderator"];
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the logged-in user is a user admin.
|
||||
*
|
||||
* @global array $routerConfig Global configuration array containing permission thresholds.
|
||||
* @return bool Returns true if the user is logged in and a user admin; otherwise, false.
|
||||
*/
|
||||
function isUserAdmin(): bool
|
||||
{
|
||||
global $routerConfig;
|
||||
return isLoggedIn() && $_SESSION["privilege_level"] >= $routerConfig["permissions"]["user_admin"];
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the logged-in user is an admin.
|
||||
*
|
||||
* @global array $routerConfig Global configuration array containing permission thresholds.
|
||||
* @return bool Returns true if the user is logged in and an admin; otherwise, false.
|
||||
*/
|
||||
function isAdmin(): bool
|
||||
{
|
||||
global $routerConfig;
|
||||
return isLoggedIn() && $_SESSION["privilege_level"] >= $routerConfig["permissions"]["admin"];
|
||||
}
|
||||
|
||||
|
||||
function generateActivationToken(): string
|
||||
/**
|
||||
* Generates a secure token for account activation or other purposes using cryptographic methods.
|
||||
*
|
||||
* @return string|null Returns a hexadecimal token or null in case of an error.
|
||||
*/
|
||||
function generateActivationToken(): ?string
|
||||
{
|
||||
try {
|
||||
return bin2hex(random_bytes(16));
|
||||
} catch (RandomException) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function isEmailAvailable($email): bool
|
||||
/**
|
||||
* Checks if an email address is available for registration.
|
||||
*
|
||||
* @param string $email The email address to check.
|
||||
* @return bool Returns true if the email is not already registered; otherwise, false.
|
||||
*@global mysqli $mysqli Global mysqli object for database access.
|
||||
*/
|
||||
function isEmailAvailable(string $email): bool
|
||||
{
|
||||
global $mysqli;
|
||||
$stmt = $mysqli->prepare("SELECT COUNT(*) FROM Users WHERE Email = ?");
|
||||
@@ -59,7 +102,12 @@ function isEmailAvailable($email): bool
|
||||
|
||||
return $count === 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets default session data typically used for a logged-out user(includes users that have just visited the page).
|
||||
*
|
||||
* @global array $routerConfig Global configuration array used for setting initial privilege levels.
|
||||
* @return void
|
||||
*/
|
||||
function setDefaultSessionData(): void
|
||||
{
|
||||
global $routerConfig;
|
||||
@@ -71,8 +119,15 @@ function setDefaultSessionData(): void
|
||||
$_SESSION["minecraft_nickname"] = "";
|
||||
$_SESSION["privilege_level"] = $routerConfig["permissions"]["logged_out"];
|
||||
}
|
||||
|
||||
function verifyPassword($userID, $password): bool
|
||||
/**
|
||||
* Verifies if the provided password matches the stored hash for the user.
|
||||
*
|
||||
* @param int $userID The user ID whose password is to be verified.
|
||||
* @param string $password The password to verify.
|
||||
* @return bool Returns true if the password matches the stored hash; otherwise, false.
|
||||
*@global mysqli $mysqli Global mysqli object for database access.
|
||||
*/
|
||||
function verifyPassword(int $userID, string $password): bool
|
||||
{
|
||||
global $mysqli;
|
||||
$stmt = $mysqli->prepare("SELECT PasswordHash FROM Users WHERE ID = ?");
|
||||
@@ -85,7 +140,12 @@ function verifyPassword($userID, $password): bool
|
||||
|
||||
return !empty($password_hash) && !empty($password) && password_verify($password, $password_hash);
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates session data from the database for the logged-in user.
|
||||
*
|
||||
* @global mysqli $mysqli Global mysqli object for database access.
|
||||
* @return void
|
||||
*/
|
||||
function UpdateSession(): void
|
||||
{
|
||||
global $mysqli;
|
||||
@@ -118,8 +178,15 @@ function UpdateSession(): void
|
||||
$_SESSION["favorite_color"] = $favorite_color;
|
||||
|
||||
}
|
||||
|
||||
function doLogin($email, $password): array
|
||||
/**
|
||||
* Attempts to log in a user with the given credentials.
|
||||
*
|
||||
* @param string $email The user's email address.
|
||||
* @param string $password The user's password.
|
||||
* @global mysqli $mysqli Global database connection object.
|
||||
* @return array An array containing the status of the login attempt ('Success' or 'Fail').
|
||||
*/
|
||||
function doLogin(string $email, string $password): array
|
||||
{
|
||||
global $mysqli;
|
||||
$found = false;
|
||||
@@ -149,7 +216,12 @@ function doLogin($email, $password): array
|
||||
}
|
||||
return $found ? ["Status" => "Success"] : ["Status" => "Fail"];
|
||||
}
|
||||
|
||||
/**
|
||||
* Logs out the current user by resetting session data.
|
||||
* Fails when the user wasn't logged in
|
||||
*
|
||||
* @return array An array with the logout status ('Success' if logged out, 'Fail' otherwise).
|
||||
*/
|
||||
function doLogout(): array
|
||||
{
|
||||
if(isLoggedIn()){
|
||||
@@ -159,8 +231,19 @@ function doLogout(): array
|
||||
return ["Status" => "Fail"];
|
||||
}
|
||||
}
|
||||
|
||||
function doRegister($firstname, $lastname, $email, $password, $activation_token): array
|
||||
/**
|
||||
* Registers a new user with provided personal details and activation token.
|
||||
*
|
||||
* @param string $firstname The user's first name.
|
||||
* @param string $lastname The user's last name.
|
||||
* @param string $email The user's email.
|
||||
* @param string $password The user's password.
|
||||
* @param string $activation_token The activation token to verify the registration.
|
||||
* @global mysqli $mysqli Global database connection object.
|
||||
* @global array $routerConfig Global configuration settings.
|
||||
* @return array An array with the registration status ('Success' or 'Fail').
|
||||
*/
|
||||
function doRegister(string $firstname, string $lastname, string $email, string $password, string $activation_token): array
|
||||
{
|
||||
global $mysqli, $routerConfig;
|
||||
$status = ["Status" => "Fail"];
|
||||
@@ -186,8 +269,15 @@ function doRegister($firstname, $lastname, $email, $password, $activation_token)
|
||||
return $status;
|
||||
}
|
||||
|
||||
|
||||
function changePassword($oldPassword, $newPassword): array
|
||||
/**
|
||||
* Changes the user's password after verifying the old password.
|
||||
*
|
||||
* @param string $oldPassword The current password for verification.
|
||||
* @param string $newPassword The new password to be set.
|
||||
* @return array An array indicating whether the password change was successful ('Success' or 'Fail').
|
||||
*@global mysqli $mysqli Global database connection object.
|
||||
*/
|
||||
function changePassword(string $oldPassword, string $newPassword): array
|
||||
{
|
||||
global $mysqli;
|
||||
$status = ["Status" => "Fail"];
|
||||
@@ -206,8 +296,17 @@ function changePassword($oldPassword, $newPassword): array
|
||||
}
|
||||
|
||||
|
||||
// Function to update user profile
|
||||
function updateUserProfile($firstName, $lastName, $nickname, $minecraft_nickname): array
|
||||
/**
|
||||
* Updates user profile information in the database.
|
||||
*
|
||||
* @param string $firstName The new first name.
|
||||
* @param string $lastName The new last name.
|
||||
* @param string $nickname The new nickname.
|
||||
* @param string $minecraft_nickname The new Minecraft nickname.
|
||||
* @return array Status of the profile update ('Success' or 'Fail').
|
||||
*@global mysqli $mysqli Global database connection object.
|
||||
*/
|
||||
function updateUserProfile(string $firstName, string $lastName, string $nickname, string $minecraft_nickname): array
|
||||
{
|
||||
global $mysqli;
|
||||
$status = ["Status" => "Fail"];
|
||||
@@ -230,8 +329,14 @@ function updateUserProfile($firstName, $lastName, $nickname, $minecraft_nickname
|
||||
return $status;
|
||||
}
|
||||
|
||||
// Function to update user email
|
||||
function updateUserEmail($email): array
|
||||
/**
|
||||
* Updates the email address of the logged-in user after validation.
|
||||
*
|
||||
* @param string $email The new email address to update.
|
||||
* @return array Status of the email update ('Success' or 'Fail').
|
||||
*@global mysqli $mysqli Global database connection object.
|
||||
*/
|
||||
function updateUserEmail(string $email): array
|
||||
{
|
||||
global $mysqli;
|
||||
$status = ["Status" => "Fail"];
|
||||
@@ -274,7 +379,11 @@ function updateUserEmail($email): array
|
||||
|
||||
return $status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves and updates the current session with user information from the database.
|
||||
*
|
||||
* @return array Contains user information and status if the user is logged in.
|
||||
*/
|
||||
function getUserInfo(): array
|
||||
{
|
||||
$output = ["Status" => "Fail"];
|
||||
@@ -310,8 +419,14 @@ function getUserInfo(): array
|
||||
return $output;
|
||||
}
|
||||
|
||||
|
||||
function addActivationCodes($count): array
|
||||
/**
|
||||
* Generates a specified number of activation codes for user registration and adds them to the database.
|
||||
*
|
||||
* @param int $count Number of activation codes to generate.
|
||||
* @return array An array containing the generated codes and status ('Success' or 'Fail').
|
||||
*@global mysqli $mysqli Global database connection object.
|
||||
*/
|
||||
function addActivationCodes(int $count): array
|
||||
{
|
||||
global $mysqli;
|
||||
$activationCodes = [];
|
||||
@@ -342,7 +457,12 @@ function addActivationCodes($count): array
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Lists all registered users, available only to user admins.
|
||||
*
|
||||
* @global mysqli $mysqli Global database connection object.
|
||||
* @return array An array containing user data and status.
|
||||
*/
|
||||
function listUsers(): array
|
||||
{
|
||||
global $mysqli;
|
||||
@@ -364,7 +484,12 @@ function listUsers(): array
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Lists activation codes available for assigning to new users, available only for user admins.
|
||||
*
|
||||
* @global mysqli $mysqli Global database connection object.
|
||||
* @return array An array containing activation codes and status.
|
||||
*/
|
||||
function listActivationCodes(): array
|
||||
{
|
||||
global $mysqli;
|
||||
@@ -409,8 +534,14 @@ function listActivationCodes(): array
|
||||
return $output;
|
||||
}
|
||||
|
||||
|
||||
function deleteUser($userID): array
|
||||
/**
|
||||
* Deletes a user by their ID, available only to user admins.
|
||||
*
|
||||
* @param int $userID The ID of the user to delete.
|
||||
* @return array Status of the delete operation ('Success' or 'Fail').
|
||||
*@global mysqli $mysqli Global database connection object.
|
||||
*/
|
||||
function deleteUser(int $userID): array
|
||||
{
|
||||
global $mysqli;
|
||||
$status = ["Status" => "Fail"];
|
||||
@@ -425,8 +556,14 @@ function deleteUser($userID): array
|
||||
}
|
||||
return $status;
|
||||
}
|
||||
|
||||
function deleteActivationCode($activationCode): array
|
||||
/**
|
||||
* Deletes an activation code, available only to user admins.
|
||||
*
|
||||
* @param string $activationCode The activation code to delete.
|
||||
* @return array Status of the delete operation ('Success' or 'Fail').
|
||||
*@global mysqli $mysqli Global database connection object.
|
||||
*/
|
||||
function deleteActivationCode(string $activationCode): array
|
||||
{
|
||||
global $mysqli;
|
||||
$status = ["Status" => "Fail"];
|
||||
|
Reference in New Issue
Block a user