do("INSERT INTO 'players' ($keys) VALUES ($placeholders);", array_values($data)); return App::$db->lastInsertID(); } public static function validateCredentials(string $identifier, string $password, bool $fetch = false): int|false { // get the player from their username or email $player = App::$db->do("SELECT " . ($fetch ? '*' : 'id, password') . " FROM players WHERE LOWER(username) = :i OR LOWER(email) = :i LIMIT 1;", ['i' => strtolower($identifier)]); if ($player == false) return false; $player = $player->fetch(); // check password, return the player data if good if (password_verify($password, $player['password'])) { unset($player['password']); return $fetch ? $player : $player['id']; } return false; } public static function goodUsername(string $username): bool { // username must be alphanumeric and between 2 and 20 characters, allow single spaces return preg_match('/^(?!.* )[a-zA-Z0-9 ]{2,20}$/', $username); } public static function uniqueUsername(string $username): bool { $player = App::$db->do("SELECT id FROM players WHERE LOWER(username) = :i LIMIT 1;", ['i' => strtolower($username)]); return $player->fetch() == false; } public static function uniqueEmail(string $email): bool { $player = App::$db->do("SELECT id FROM players WHERE LOWER(email) = :i LIMIT 1;", ['i' => strtolower($email)]); return $player->fetch() == false; } }