2023-08-19 21:15:47 +03:00
|
|
|
<?php // Creating account
|
2023-08-12 01:39:17 +03:00
|
|
|
|
2023-10-31 21:57:17 +03:00
|
|
|
// Includes
|
2023-08-19 21:15:47 +03:00
|
|
|
require_once("../_auth.php");
|
|
|
|
require_once("../_utils.php");
|
2023-10-31 21:57:17 +03:00
|
|
|
require_once("../_errorslist.php");
|
|
|
|
require_once("../_types.php");
|
|
|
|
require_once("index.php");
|
2023-08-19 21:15:47 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
2023-10-31 21:57:17 +03:00
|
|
|
// Methods
|
2023-08-19 21:15:47 +03:00
|
|
|
|
2023-10-31 21:57:17 +03:00
|
|
|
/*
|
|
|
|
* METHOD
|
|
|
|
* Create new user account
|
|
|
|
*/
|
|
|
|
function User_Create_Method (array $req): ReturnT {
|
|
|
|
global $db, $Config, $LOGGED_IN;
|
2023-08-19 21:15:47 +03:00
|
|
|
|
2023-10-31 21:57:17 +03:00
|
|
|
$login = null;
|
|
|
|
$password = null;
|
|
|
|
$email = null;
|
|
|
|
$invite_id = null;
|
|
|
|
$avatar_path = null;
|
2023-08-19 21:15:47 +03:00
|
|
|
|
2023-10-31 21:57:17 +03:00
|
|
|
// Input sanity checks
|
2023-08-30 05:16:15 +03:00
|
|
|
|
2023-08-19 21:15:47 +03:00
|
|
|
// If registration turned off
|
2023-10-31 21:57:17 +03:00
|
|
|
if (!$Config["registration"]["active"])
|
|
|
|
return new ReturnT(err_code: E_AUT_REGCLOSED);
|
2023-08-19 21:15:47 +03:00
|
|
|
|
|
|
|
// If user is logged in, then we should not allow creation of account
|
|
|
|
if ($LOGGED_IN)
|
2023-10-31 21:57:17 +03:00
|
|
|
return new ReturnT(err_code: E_AUT_ALRLOGIN);
|
2023-08-19 21:15:47 +03:00
|
|
|
|
2023-10-31 21:57:17 +03:00
|
|
|
// If we have some base data
|
|
|
|
if (isset($req["login"]) && isset($req["password"])) {
|
|
|
|
$login = $req["login"];
|
|
|
|
$password = $req["password"];
|
2023-08-23 03:44:40 +03:00
|
|
|
|
|
|
|
// If password is too weak
|
|
|
|
if (strlen($password) < 8)
|
2023-10-31 21:57:17 +03:00
|
|
|
return new ReturnT(err_code: E_AUT_PWD2WEAK);
|
2023-08-23 03:44:40 +03:00
|
|
|
|
2023-08-19 21:15:47 +03:00
|
|
|
// If we need email but it isnt supplied
|
2023-10-31 21:57:17 +03:00
|
|
|
if ($Config["registration"]["need_email"] && !isset($req["email"])) {
|
|
|
|
return new ReturnT(err_code: E_UIN_INSUFARGS, err_desc: "email is necessary");
|
|
|
|
} elseif (isset($req["email"])) {
|
2023-08-19 21:15:47 +03:00
|
|
|
// Validation of email
|
2023-10-31 21:57:17 +03:00
|
|
|
if (!filter_var($req["email"], FILTER_VALIDATE_EMAIL))
|
|
|
|
return new ReturnT(err_code: E_UIN_BADARGS, err_desc: "email is invalid");
|
|
|
|
$email = $req["email"];
|
2023-08-19 21:15:47 +03:00
|
|
|
}
|
|
|
|
// If we need invite but it isnt supplied
|
2023-10-31 21:57:17 +03:00
|
|
|
if ($Config["registration"]["need_invite"] && !isset($req["invite_id"])) {
|
|
|
|
return new ReturnT(err_code: E_UIN_INSUFARGS, err_desc: "registrations are invite-only, you need to specify invite ID");
|
|
|
|
} elseif (isset($req["invite_id"])) {
|
2023-08-23 03:44:40 +03:00
|
|
|
// TODO: check invite and reject if it invalid
|
2023-10-31 21:57:17 +03:00
|
|
|
//$invite_id = $req["invite_id"];
|
|
|
|
return new ReturnT(err_code: E_UNS_NOTIMPL, err_desc: "invitations are not implemented");
|
2023-08-23 03:44:40 +03:00
|
|
|
}
|
2023-08-19 21:15:47 +03:00
|
|
|
|
|
|
|
// Check login and password for pattern match
|
|
|
|
$preg_str = "/[^" . $Config["registration"]["allowed_syms"] . "]/";
|
2023-10-31 21:57:17 +03:00
|
|
|
if (preg_match($preg_str, $login) || preg_match($preg_str, $password))
|
|
|
|
return new ReturnT(err_code: E_UIN_BADARGS, err_desc: "only allowed symbols are: " . $Config["registration"]["allowed_syms"]);
|
2023-08-19 21:15:47 +03:00
|
|
|
|
2023-08-30 04:41:13 +03:00
|
|
|
// Check if login already exists
|
2023-08-23 03:44:40 +03:00
|
|
|
if (User_LoginExist($login))
|
2023-10-31 21:57:17 +03:00
|
|
|
return new ReturnT(err_code: E_UIN_BADARGS, err_desc: "login already exists");
|
2023-08-23 03:44:40 +03:00
|
|
|
|
2023-10-31 21:57:17 +03:00
|
|
|
// TODO: check $avatar_path
|
2023-08-19 21:15:47 +03:00
|
|
|
} else { // Not enough arguments
|
2023-10-31 21:57:17 +03:00
|
|
|
return new ReturnT(err_code: E_UIN_INSUFARGS, err_desc: "not enough or no arguments were supplied");
|
2023-08-19 21:15:47 +03:00
|
|
|
}
|
2023-10-31 21:57:17 +03:00
|
|
|
|
|
|
|
// Actions
|
|
|
|
|
|
|
|
$result = null;
|
|
|
|
|
|
|
|
$salt = Utils_GenerateRandomString(8);
|
|
|
|
$pwd_hash = hash("sha256", $password . $salt, true);
|
|
|
|
|
|
|
|
// TODO: process invite
|
|
|
|
|
|
|
|
$s = $db->prepare("INSERT INTO users (login,email,password_hash,salt,avatar_path,role,invite_id) VALUES (?,?,?,?,?,?,?)");
|
|
|
|
$role = "newbie";
|
|
|
|
$s->bind_param("sssssss", $login, $email, $pwd_hash, $salt, $avatar_path, $role, $invite_id);
|
|
|
|
$result = ($s->execute() !== false);
|
|
|
|
|
|
|
|
if (!$result)
|
|
|
|
return new ReturnT(err_code: E_DBE_INSERTFAIL, err_desc: "cant insert record to users DB");
|
|
|
|
|
|
|
|
return new ReturnT(data: $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (Utils_ThisFileIsRequested(__FILE__)) {
|
|
|
|
require_once("../_json.php");
|
|
|
|
|
|
|
|
// HACK: for debugging purposes. Will be removed later
|
|
|
|
if ($Config["debug"])
|
|
|
|
$_POST = $_REQUEST;
|
|
|
|
|
|
|
|
// Create account
|
|
|
|
$result = User_Create_Method($_POST);
|
|
|
|
|
|
|
|
// Checking result
|
|
|
|
if ($result->IsError())
|
|
|
|
$result->ThrowJSONError();
|
|
|
|
else
|
|
|
|
JSON_ReturnData(["success" => $result->GetData()]);
|
2023-08-19 21:15:47 +03:00
|
|
|
}
|
2023-08-12 01:39:17 +03:00
|
|
|
|
|
|
|
?>
|