e949/api/user/index.php

153 lines
3.2 KiB
PHP
Raw Normal View History

2023-08-19 21:15:47 +03:00
<?php // Viewing account data
2023-08-12 01:39:17 +03:00
// Includes
2023-08-12 01:39:17 +03:00
require_once("../_auth.php");
2023-08-19 21:15:47 +03:00
require_once("../_utils.php");
require_once("../_errorslist.php");
require_once("../_types.php");
2023-08-12 01:39:17 +03:00
// Functions
/*
* FUNCTION
* Check if user with supplied login exists
*/
2023-08-19 21:15:47 +03:00
function User_LoginExist ($login): bool {
global $db;
$s = $db->prepare("SELECT * FROM users WHERE login = ?");
$s->bind_param("s", $login);
$s->execute();
return (bool)$s->get_result()->fetch_assoc();
}
/*
* FUNCTION
* Check if user with supplied ID exists
*/
function User_IDExist ($id): bool {
global $db;
$s = $db->prepare("SELECT * FROM users WHERE id = ?");
$s->bind_param("s", $id);
$s->execute();
return (bool)$s->get_result()->fetch_assoc();
}
/*
* FUNCTION
* Check if user has specified role
*/
function User_HasRole ($id, $role): ReturnT {
global $db;
$s = $db->prepare("SELECT * FROM users WHERE id = ?");
$s->bind_param("s", $id);
$s->execute();
$d = $s->get_result()->fetch_assoc();
if (!(bool)$d)
return new ReturnT(err_code: E_UIN_WRONGID, err_desc: "user not found in database");
2023-08-30 04:41:13 +03:00
return new ReturnT(data: $d["role"] === $role);
}
/*
* FUNCTION
* Check if user is moderator (or higher)
*/
function User_IsMod ($id): ReturnT {
global $db;
$s = $db->prepare("SELECT * FROM users WHERE id = ?");
$s->bind_param("s", $id);
$s->execute();
$d = $s->get_result()->fetch_assoc();
if (!(bool)$d)
return new ReturnT(err_code: E_UIN_WRONGID, err_desc: "user not found in database");
return new ReturnT(data: in_array($d["role"], array("mod", "admin")));
}
/*
* FUNCTION
* Get user information from DB
*/
function User_GetInfoByID (int $id): ReturnT {
global $THIS_USER;
$result = array();
$s = $db->prepare("SELECT * FROM users WHERE id = ?");
$s->bind_param("s", $id);
$s->execute();
$d = $s->get_result()->fetch_assoc();
if (!(bool)$d)
return new ReturnT(err_code: E_UIN_WRONGID, err_desc: "user not found in database");
$result["id"] = $d["id"];
$result["created_at"] = $d["created_at"];
$result["login"] = $d["login"];
$result["avatar_path"] = $d["avatar_path"];
$result["role"] = $d["role"];
$result["banned"] = $d["banned"];
if (($id === $THIS_USER) || User_IsMod($THIS_USER).GetData()) { // User himself and mods can see additional info
$result["email"] = $d["email"];
$result["invite_id"] = $d["invite_id"];
}
return new ReturnT(data: $result);
}
// Methods
/*
* METHOD
* Get user information from DB
*/
function User_GetInfoByID_Method (array $req): ReturnT {
global $db, $THIS_USER, $LOGGED_IN;
// Input sanity checks
$UserID = null;
if (isset($req["id"])) {
if (!ctype_digit($req["id"]))
return new ReturnT(err_code: E_UIN_BADARGS, err_desc: "id must be numeric");
$UserID = intval($req["id"]);
} else {
if ($LOGGED_IN)
$UserID = $THIS_USER;
else
return new ReturnT(err_code: E_UIN_BADARGS, err_desc: "id must be specified or valid session must be provided");
}
// Actions
return User_GetInfoByID($UserID);
}
if (Utils_ThisFileIsRequested(__FILE__)) {
2023-08-19 21:15:47 +03:00
require_once("../_json.php");
$result = User_GetInfoByID_Method($_REQUEST);
if ($result->IsError())
$result->ThrowJSONError();
else
JSON_ReturnData($result->GetData());
2023-08-12 01:39:17 +03:00
}
2023-08-12 01:39:17 +03:00
?>