BanList.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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\Model;
  11. use InvalidArgumentException;
  12. use RuntimeException;
  13. class BanList extends Model
  14. {
  15. /**
  16. * Ключ модели для контейнера
  17. * @var string
  18. */
  19. protected $cKey = 'BanList';
  20. /**
  21. * Загружает список банов из кеша/БД
  22. * Создает кеш
  23. */
  24. public function init(): BanList
  25. {
  26. $list = $this->c->Cache->get('banlist');
  27. if (! isset($list['banList'], $list['userList'], $list['emailList'], $list['ipList'], $list['firstExpire'])) {
  28. $list = $this->load();
  29. if (true !== $this->c->Cache->set('banlist', $list)) {
  30. throw new RuntimeException('Unable to write value to cache - banlist');
  31. }
  32. }
  33. $this->banList = $list['banList'];
  34. $this->userList = $list['userList'];
  35. $this->emailList = $list['emailList'];
  36. $this->ipList = $list['ipList'];
  37. $this->firstExpire = $list['firstExpire'];
  38. return $this;
  39. }
  40. /**
  41. * Фильтрует значение
  42. */
  43. public function trimToNull(?string $val, bool $toLower = false): ?string
  44. {
  45. $val = \trim($val ?? '');
  46. if ('' == $val) {
  47. return null;
  48. } elseif ($toLower) {
  49. return \mb_strtolower($val, 'UTF-8');
  50. } else {
  51. return $val;
  52. }
  53. }
  54. /**
  55. * Переводит ip/ip-подсеть в hex представление
  56. */
  57. public function ip2hex(string $ip): string
  58. {
  59. $bin = \inet_pton($ip);
  60. if (false === $bin) {
  61. if (\preg_match('%[:a-fA-F]|\d{4}%', $ip)) {
  62. $result = '';
  63. // 0000-ffff
  64. foreach (\explode(':', $ip) as $cur) {
  65. $result .= \substr('0000' . \strtolower($cur), -4);
  66. }
  67. } else {
  68. $result = '-';
  69. // 00-ff
  70. foreach (\explode('.', $ip) as $cur) {
  71. $result .= \sprintf('%02x', $cur);
  72. }
  73. }
  74. return $result;
  75. } else {
  76. // The hyphen is needed for the joint sorting ipv4 and ipv6
  77. return (isset($bin[4]) ? '' : '-') . \bin2hex($bin);
  78. }
  79. }
  80. /**
  81. * Сбрасывает кеш банов
  82. */
  83. public function reset(): BanList
  84. {
  85. if (true !== $this->c->Cache->delete('banlist')) {
  86. throw new RuntimeException('Unable to remove key from cache - banlist');
  87. }
  88. return $this;
  89. }
  90. /**
  91. * Выдает номер бана по имени или 0
  92. */
  93. public function banFromName(?string $name): int
  94. {
  95. $name = $this->trimToNull($name, true);
  96. if (null === $name) {
  97. throw new InvalidArgumentException('Expected username, not empty string');
  98. }
  99. return $this->userList[$name] ?? 0;
  100. }
  101. }