summaryrefslogtreecommitdiff
path: root/includes/functions_user.php
blob: 4ea1ad1d70998b0c7beb1778bf4d37d25a65d686 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php

function username_exists(string $username): bool
{
	$sql = "SELECT * FROM users WHERE user_name = ?;";
	$result = Database::get()->query($sql, "s", $username);

	return !empty($result);
}

function register_user(string $username, string $pass_hash)
{
	$sql = "INSERT INTO users(user_name, user_pass, user_date) VALUES(?, ?, NOW());";
	Database::get()->query($sql, "ss", $username, $pass_hash);
}

function change_password(User $user, string $pass_hash)
{
	if (!Session::get()->is_signed_in()) {
		trigger_error('You are not signed in.');
		return;
	}

	if (Session::get()->get_current_user()->id != $user->id) {
		trigger_error("You can't change another user's password.");
		return;
	}

	$sql = "UPDATE users SET user_pass = ? WHERE user_id = ?;";
	Database::get()->query($sql, "si", $pass_hash, $user->id);
}