Username.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. /**
  3. * This file is part of the ForkBB <https://github.com/forkbb>.
  4. *
  5. * @copyright (c) Visman <mio.visman@yandex.ru, https://github.com/MioVisman>
  6. * @license The MIT License (MIT)
  7. */
  8. declare(strict_types=1);
  9. namespace ForkBB\Models\Validators;
  10. use ForkBB\Core\RulesValidator;
  11. use ForkBB\Core\Validator;
  12. use ForkBB\Models\User\User;
  13. class Username extends RulesValidator
  14. {
  15. /**
  16. * Проверяет имя пользователя
  17. */
  18. public function username(Validator $v, string $username, /* mixed */ $attrs, /* mixed */ $originalUser): string
  19. {
  20. if ($originalUser instanceof User) {
  21. $id = $originalUser->id;
  22. $old = $originalUser->username;
  23. } else {
  24. $id = null;
  25. $old = null;
  26. }
  27. if ($old !== $username) {
  28. $user = $this->c->users->create(['id' => $id, 'username' => $username]);
  29. // 2-25 символов, буквы, цифры, пробел, подчеркивание, точка и тире
  30. if (! \preg_match($this->c->USERNAME_PATTERN, $username)) {
  31. $v->addError('Login format');
  32. // идущие подряд пробелы
  33. } elseif (\preg_match('%\s{2,}%u', $username)) {
  34. $v->addError('Username contains consecutive spaces');
  35. // цензура
  36. } elseif ($this->c->censorship->censor($username) !== $username) {
  37. $v->addError('Username censor');
  38. // username забанен
  39. } elseif ($this->c->bans->banFromName($username) > 0) {
  40. $v->addError('Banned username');
  41. // есть пользователь с похожим именем
  42. } elseif (
  43. empty($v->getErrors())
  44. && ! $this->c->users->isUniqueName($user)
  45. ) {
  46. $v->addError('Username not unique');
  47. }
  48. }
  49. return $username;
  50. }
  51. }