68 lines
2.1 KiB
PHP
68 lines
2.1 KiB
PHP
<?php
|
|
namespace Mince;
|
|
|
|
use Index\Data\IDbConnection;
|
|
use Index\Data\DbType;
|
|
|
|
final class Whitelist {
|
|
public static function add(IDbConnection $db, 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 = $db->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 = self::remove($db, $userInfo);
|
|
if($resp !== '')
|
|
return $resp;
|
|
}
|
|
|
|
$resp = Remote::call('wl:add', $userName);
|
|
|
|
if($resp !== 'success')
|
|
return $resp;
|
|
|
|
$insert = $db->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 static function remove(IDbConnection $db, object $userInfo): string {
|
|
if(empty($userInfo->mc_whitelisted) || empty($userInfo->mc_username))
|
|
return 'not-listed';
|
|
|
|
$resp = Remote::call('wl:remove', $userInfo->mc_username);
|
|
|
|
if($resp !== 'success')
|
|
return $resp;
|
|
|
|
$delete = $db->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 '';
|
|
}
|
|
}
|