e949/api/user/create.php

60 lines
2.0 KiB
PHP
Raw Normal View History

2023-08-19 23:45:47 +05:30
<?php // Creating account
2023-08-12 04:09:17 +05:30
2023-08-19 23:45:47 +05:30
require_once("../_auth.php");
require_once("../_utils.php");
// Create new user account
function User_Create ($login, $password, $email = null, $invite_id = null, $avatar_path = null): bool {
global $db;
$salt = GenerateRandomString(8);
$pwd_hash = hash("sha256", $password . $salt, true);
$s = $db->prepare("INSERT INTO users (login,email,password_hash,salt,avatar_path,role,invite_id) VALUES (?,?,?,?,?,?,?)");
$s->bind_param("sssssss", $login, $email, $pwd_hash, $salt, $avatar_path, "newbie", $invite_id);
return $s->execute() !== false;
}
if (ThisFileIsRequested(__FILE__)) {
require_once("../_json.php");
// If registration turned off
if (!$Config["registration"]["active"]) {
ReturnJSONError($Err_DP_RegClosed, "registrations are closed");
}
// If user is logged in, then we should not allow creation of account
if ($LOGGED_IN)
ReturnJSONError($Err_DP_AlreadyLoggedIn, "you are already logged in");
// If we have some POST data
if (isset($_POST["login"]) && isset($_POST["password"])) {
// If we need email but it isnt supplied
if ($Config["registration"]["need_email"] && !isset($_POST["email"]))
ReturnJSONError($Err_RDP_InvalidArgs, "email is necessary");
elseif (isset($_POST["email"])) {
// Validation of email
if (!filter_var($_POST["email"], FILTER_VALIDATE_EMAIL))
ReturnJSONError($Err_RDP_InvalidArgs, "email is invalid");
}
// If we need invite but it isnt supplied
if ($Config["registration"]["need_invite"] && !isset($_POST["invite_id"]))
ReturnJSONError($Err_RDP_InvalidArgs, "registrations are invite-only");
// Check login and password for pattern match
$preg_str = "/[^" . $Config["registration"]["allowed_syms"] . "]/";
if (preg_match($preg_str, $_POST["login"]) || preg_match($preg_str, $_POST["password"])) {
ReturnJSONError($Err_RDP_InvalidArgs, "only allowed symbols are: " . $Config["registration"]["allowed_syms"]);
}
// TODO
} else { // Not enough arguments
ReturnJSONError($Err_RDP_InvalidArgs, "not enough or no arguments were supplied");
}
}
2023-08-12 04:09:17 +05:30
?>