You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.8 KiB

<?php
use App\Enumerators\SessionElement;
use App\Wrappers\CAPTCHAWrapper;
use App\Wrappers\DatabaseInteractions;
use App\Wrappers\SQLQueryBuilderWrapper;
use Ramsey\Uuid\Uuid;
require_once __DIR__ . "/../../vendor/autoload.php";
$captchaResponse = $_POST['g-recaptcha-response'];
CAPTCHAWrapper::HandleCaptchaResponse($captchaResponse);
if(!isset($_POST['Username'])) die('No username');
if(!isset($_POST['Password1'])) die('No password');
if(!isset($_POST['Password2'])) die('No confirm password');
if($_POST['Password1'] != $_POST['Password2']) die('Passwords do not match');
$sha512Hash = hash(algo: 'sha512', data: $_POST['Password1'], binary: false);
$hashedPassword = password_hash(password: $sha512Hash, algo: PASSWORD_BCRYPT);
$db = new DatabaseInteractions();
$existingUser = $db->RunSelect(
queryBuilder: SQLQueryBuilderWrapper::SELECT(
table: 'Users'
)
->where(cond: 'T.Username LIKE :__username__')
->bindValue(name: '__username__', value: $_POST['Username'])
->limit(limit: 1)
);
if (!empty($existingUser)) {
echo "Username already exists. Please choose a different username.";
die();
}
$userID = Uuid::uuid4()->toString();
$db->RunInsert(
queryBuilder: SQLQueryBuilderWrapper::INSERT(
table: 'Users',
)
->set(col: 'ID', value: ':__user_id__')
->set(col: 'Username', value: ':__username__')
->set(col: 'PasswordHash', value: ':__password_hash__')
->bindValue(name: '__user_id__', value: $userID)
->bindValue(name: '__username__', value: $_POST['Username'])
->bindValue(name: '__password_hash__', value: $hashedPassword)
);
$_SESSION[SessionElement::IS_LOGGED_IN->value] = true;
$_SESSION[SessionElement::USER_ID->value] = $userID;
$_SESSION[SessionElement::USERNAME->value] = $_POST['Username'];
header("Location: /profile");
die();