128 lines
4.7 KiB
PHP
128 lines
4.7 KiB
PHP
<?php
|
|
namespace Seria\Users;
|
|
|
|
use InvalidArgumentException;
|
|
use RuntimeException;
|
|
use Index\DateTime;
|
|
use Index\XString;
|
|
use Index\Colour\Colour;
|
|
use Index\Data\DbStatementCache;
|
|
use Index\Data\DbTools;
|
|
use Index\Data\IDbConnection;
|
|
use Index\Net\IPAddress;
|
|
|
|
class Users {
|
|
private DbStatementCache $cache;
|
|
|
|
public function __construct(IDbConnection $dbConn) {
|
|
$this->cache = new DbStatementCache($dbConn);
|
|
}
|
|
|
|
public static function generatePassKey(): string {
|
|
return XString::random(48);
|
|
}
|
|
|
|
public function syncChatUser(object $authInfo): void {
|
|
if(!$authInfo->success)
|
|
return;
|
|
|
|
$userColourFixed = /*($authInfo->colour_raw & 0x40000000) ? null :*/ $authInfo->colour_raw;
|
|
$stmt = $this->cache->get('INSERT INTO ser_users (user_id, user_name, user_colour, user_rank, user_permissions) VALUES (?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE user_name = ?, user_colour = ?, user_rank = ?, user_permissions = ?');
|
|
$stmt->addParameter(1, $authInfo->user_id);
|
|
$stmt->addParameter(2, $authInfo->username);
|
|
$stmt->addParameter(3, $userColourFixed);
|
|
$stmt->addParameter(4, $authInfo->rank);
|
|
$stmt->addParameter(5, $authInfo->perms);
|
|
$stmt->addParameter(6, $authInfo->username);
|
|
$stmt->addParameter(7, $userColourFixed);
|
|
$stmt->addParameter(8, $authInfo->rank);
|
|
$stmt->addParameter(9, $authInfo->perms);
|
|
$stmt->execute();
|
|
}
|
|
|
|
public const GET_USER_ID = 0x01;
|
|
public const GET_USER_NAME = 0x02;
|
|
public const GET_USER_PASSKEY = 0x04;
|
|
|
|
private const GET_USER_SELECT_ALIASES = [
|
|
'id' => self::GET_USER_ID,
|
|
'name' => self::GET_USER_NAME,
|
|
'passkey' => self::GET_USER_PASSKEY,
|
|
];
|
|
|
|
public static function resolveGetUserSelectAlias(int|string $select): int {
|
|
if(is_string($select)) {
|
|
if(!array_key_exists($select, self::GET_USER_SELECT_ALIASES))
|
|
throw new InvalidArgumentException('Invalid $select alias.');
|
|
$select = self::GET_USER_SELECT_ALIASES[$select];
|
|
} elseif($select === 0)
|
|
throw new InvalidArgumentException('$select may not be zero.');
|
|
|
|
return $select;
|
|
}
|
|
|
|
public function getUser(string $value, int|string $select = self::GET_USER_ID): UserInfo {
|
|
if($value === '')
|
|
throw new InvalidArgumentException('$value may not be empty.');
|
|
|
|
$select = self::resolveGetUserSelectAlias($select);
|
|
$selectId = ($select & self::GET_USER_ID) > 0;
|
|
$selectName = ($select & self::GET_USER_NAME) > 0;
|
|
$selectPassKey = ($select & self::GET_USER_PASSKEY) > 0;
|
|
|
|
if(!$selectId && !$selectName && !$selectPassKey)
|
|
throw new InvalidArgumentException('$select flagset is invalid.');
|
|
|
|
$args = 0;
|
|
$query = 'SELECT user_id, user_name, user_colour, user_rank, user_permissions, user_pass_key, user_bytes_downloaded, user_bytes_uploaded FROM ser_users';
|
|
if($selectId) {
|
|
++$args;
|
|
$query .= ' WHERE user_id = ?';
|
|
}
|
|
if($selectName)
|
|
$query .= sprintf(' %s user_name = ?', ++$args > 1 ? 'OR' : 'WHERE');
|
|
if($selectPassKey)
|
|
$query .= sprintf(' %s user_pass_key = ?', ++$args > 1 ? 'OR' : 'WHERE');
|
|
|
|
$args = 0;
|
|
$stmt = $this->cache->get($query);
|
|
if($selectId)
|
|
$stmt->addParameter(++$args, $value);
|
|
if($selectName)
|
|
$stmt->addParameter(++$args, $value);
|
|
if($selectPassKey)
|
|
$stmt->addParameter(++$args, $value);
|
|
$stmt->execute();
|
|
|
|
$result = $stmt->getResult();
|
|
if(!$result->next())
|
|
throw new RuntimeException('User not found.');
|
|
|
|
return new UserInfo($result);
|
|
}
|
|
|
|
public function updatePassKey(UserInfo|string $userInfo): string {
|
|
if($userInfo instanceof UserInfo)
|
|
$userInfo = $userInfo->getId();
|
|
|
|
$passKey = self::generatePassKey();
|
|
$stmt = $this->cache->get('UPDATE ser_users SET user_pass_key = ? WHERE user_id = ?');
|
|
$stmt->addParameter(1, $passKey);
|
|
$stmt->addParameter(2, $userInfo);
|
|
$stmt->execute();
|
|
|
|
return $passKey;
|
|
}
|
|
|
|
public function incrementTransferStats(UserInfo|string $userInfo, int $bytesDownloaded, int $bytesUploaded): void {
|
|
if($userInfo instanceof UserInfo)
|
|
$userInfo = $userInfo->getId();
|
|
|
|
$stmt = $this->cache->get('UPDATE ser_users SET user_bytes_downloaded = user_bytes_downloaded + ?, user_bytes_uploaded = user_bytes_uploaded + ? WHERE user_id = ?');
|
|
$stmt->addParameter(1, $bytesDownloaded);
|
|
$stmt->addParameter(2, $bytesUploaded);
|
|
$stmt->addParameter(3, $userInfo);
|
|
$stmt->execute();
|
|
}
|
|
}
|