Update Ban\IsBanned

Now the isBanned() method will return an exception if email is empty.
The result returns the type of ban: solo ban for email or banned the entire domain.
This commit is contained in:
Visman 2021-02-16 18:05:19 +07:00
parent 48e6ea0b64
commit 8b12b5750e

View file

@ -12,55 +12,63 @@ namespace ForkBB\Models\BanList;
use ForkBB\Models\Method;
use ForkBB\Models\User\Model as User;
use InvalidArgumentException;
class IsBanned extends Method
{
/**
* Проверяет наличие бана пользователя на основании email
*
* результат: 0 - для этого email нет бана
* 1 - забанен именно этот email
* 2 - забанен домен
*/
public function isBanned(User $user): int
{
if (
$user->isGuest
&& ! empty($this->model->emailList)
&& $user->email_normal
) {
$email = $this->model->trimToNull($user->email_normal);
$stage = 0;
do {
if (isset($this->model->emailList[$email])) {
return $this->model->emailList[$email];
}
switch ($stage) { // "super@user"@example.com
case 0:
$pos = \strrpos($email, '@');
if (false !== $pos) {
$email = \substr($email, $pos + 1); // -> example.com
break;
}
++$stage;
case 1:
$email = '.' . $email; // -> .example.com
$pos = true;
break;
default:
$pos = \strpos($email, '.', 1);
if (false !== $pos) {
$email = \substr($email, $pos); // -> .com
}
break;
}
++$stage;
} while (false !== $pos);
if (empty($this->model->emailList)) {
return 0;
}
$email = $this->model->trimToNull($user->email_normal);
if (null === $email) {
throw new InvalidArgumentException('Expected email, not empty string');
}
$stage = 0;
do {
if (isset($this->model->emailList[$email])) {
return false === \strpos($email, '@') ? 2 : 1;
}
switch ($stage) { // "super@user"@example.com
case 0:
$pos = \strrpos($email, '@');
if (false !== $pos) {
$email = \substr($email, $pos + 1); // -> example.com
break;
}
++$stage;
case 1:
$email = '.' . $email; // -> .example.com
$pos = true;
break;
default:
$pos = \strpos($email, '.', 1);
if (false !== $pos) {
$email = \substr($email, $pos); // -> .com
}
break;
}
++$stage;
} while (false !== $pos);
return 0;
}
}