72 lines
2.1 KiB
PHP
72 lines
2.1 KiB
PHP
<?php
|
|
namespace Mince;
|
|
|
|
use Index\Data\IDbConnection;
|
|
use Index\Data\DbType;
|
|
|
|
class Whitelist {
|
|
public function __construct(
|
|
private IDbConnection $dbConn
|
|
) {}
|
|
|
|
public function getNames(): array {
|
|
$names = [];
|
|
|
|
$getNames = $this->dbConn->query('SELECT minecraft_username FROM whitelist_2022');
|
|
while($getNames->next())
|
|
$names[] = $getNames->getString(0);
|
|
|
|
return $names;
|
|
}
|
|
|
|
public function add(object $userInfo, string $userName): string {
|
|
$length = strlen($userName);
|
|
if($length < 3)
|
|
return 'short';
|
|
if($length > 16)
|
|
return 'long';
|
|
if(!preg_match('#^([a-zA-Z0-9_]{3,16})$#', $userName))
|
|
return 'invalid';
|
|
|
|
$dupeCheck = $this->dbConn->prepare('SELECT COUNT(`flashii_id`) > 0 FROM `whitelist_2022` WHERE `minecraft_username` = ?');
|
|
$dupeCheck->addParameter(1, $userName, DbType::STRING);
|
|
$dupeCheck->execute();
|
|
$dupeResult = $dupeCheck->getResult();
|
|
|
|
if(!$dupeResult->next())
|
|
return 'dupefail';
|
|
if($dupeResult->getInteger(0))
|
|
return 'conflict';
|
|
|
|
if(!empty($userInfo->mc_whitelisted) || !empty($userInfo->mc_username)) {
|
|
$resp = $this->remove($userInfo);
|
|
if($resp !== '')
|
|
return $resp;
|
|
}
|
|
|
|
$insert = $this->dbConn->prepare('INSERT INTO `whitelist_2022` (`flashii_id`, `minecraft_username`) VALUES (?, ?)');
|
|
$insert->addParameter(1, $userInfo->user_id);
|
|
$insert->addParameter(2, $userName, DbType::STRING);
|
|
$insert->execute();
|
|
|
|
$userInfo->mc_username = $userName;
|
|
$userInfo->mc_whitelisted = time();
|
|
|
|
return '';
|
|
}
|
|
|
|
public function remove(object $userInfo): string {
|
|
if(empty($userInfo->mc_whitelisted) || empty($userInfo->mc_username))
|
|
return 'not-listed';
|
|
|
|
$delete = $this->dbConn->prepare('DELETE FROM `whitelist_2022` WHERE `flashii_id` = ?');
|
|
$delete->addParameter(1, $userInfo->user_id);
|
|
$delete->execute();
|
|
|
|
$userInfo->mc_username = null;
|
|
$userInfo->mc_whitelisted = 0;
|
|
|
|
return '';
|
|
}
|
|
}
|