Check.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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\BanList;
  10. use ForkBB\Models\Method;
  11. use ForkBB\Models\User\User;
  12. class Check extends Method
  13. {
  14. /**
  15. * Проверяет наличие бана (для текущего пользователя) на основании имени пользователя/ip
  16. * Удаляет просроченные баны
  17. */
  18. public function check(User $user): bool
  19. {
  20. $now = \time();
  21. // удаление просроченных банов
  22. if (
  23. $this->model->firstExpire > 0
  24. && $this->model->firstExpire < $now
  25. ) {
  26. $ids = [];
  27. foreach ($this->model->banList as $id => $row) {
  28. if (
  29. null !== $row['expire']
  30. && $row['expire'] < $now
  31. ) {
  32. $ids[] = $id;
  33. }
  34. }
  35. if (! empty($ids)) {
  36. $this->model->delete(...$ids);
  37. }
  38. }
  39. // проверка гостя
  40. if ($user->isGuest) {
  41. if (! empty($this->model->ipList)) {
  42. $ip = $this->model->trimToNull($user->ip);
  43. if (null !== $ip) {
  44. $list = $this->model->ipList;
  45. $letters = \str_split($this->model->ip2hex($ip));
  46. foreach ($letters as $letter) {
  47. if (! isset($list[$letter])) {
  48. break;
  49. } elseif (\is_array($list[$letter])) {
  50. $list = $list[$letter];
  51. } else {
  52. $id = $list[$letter];
  53. if (isset($this->model->banList[$id])) {
  54. $user->__banInfo = $this->model->banList[$id];
  55. }
  56. return true;
  57. }
  58. }
  59. }
  60. }
  61. // проверка пользователя
  62. } elseif (! $user->isAdmin) {
  63. $id = $this->model->banFromName($user->username);
  64. if ($id > 0) {
  65. if (isset($this->model->banList[$id])) {
  66. $user->__banInfo = $this->model->banList[$id];
  67. }
  68. return true;
  69. }
  70. }
  71. return false;
  72. }
  73. }