Model.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace ForkBB\Models\BanList;
  3. use ForkBB\Models\Model as ParentModel;
  4. class Model extends ParentModel
  5. {
  6. /**
  7. * Загружает список банов из кеша/БД
  8. */
  9. public function init(): Model
  10. {
  11. $list = $this->c->Cache->get('banlist');
  12. if (isset($list['banList'], $list['userList'], $list['emailList'], $list['ipList'])) {
  13. $this->banList = $list['banList'];
  14. $this->userList = $list['userList'];
  15. $this->emailList = $list['emailList'];
  16. $this->ipList = $list['ipList'];
  17. } else {
  18. $this->load();
  19. }
  20. return $this;
  21. }
  22. /**
  23. * Фильтрует значение
  24. */
  25. public function trimToNull(/* mixed */ $val, bool $toLower = false): ?string
  26. {
  27. $val = \trim($val);
  28. if ('' == $val) {
  29. return null;
  30. } elseif ($toLower) {
  31. return \mb_strtolower($val, 'UTF-8');
  32. } else {
  33. return $val;
  34. }
  35. }
  36. /**
  37. * Переводит ip/ip-подсеть в hex представление
  38. */
  39. public function ip2hex(string $ip): string
  40. {
  41. $bin = \inet_pton($ip);
  42. if (false === $bin) {
  43. if (\preg_match('%[:a-fA-F]|\d{4}%', $ip)) {
  44. $result = '';
  45. // 0000-ffff
  46. foreach (\explode(':', $ip) as $cur) {
  47. $result .= \substr('0000' . \strtolower($cur), -4);
  48. }
  49. } else {
  50. $result = '-';
  51. // 00-ff
  52. foreach (\explode('.', $ip) as $cur) {
  53. $result .= \sprintf('%02x', $cur);
  54. }
  55. }
  56. return $result;
  57. } else {
  58. // The hyphen is needed for the joint sorting ipv4 and ipv6
  59. return (isset($bin[4]) ? '' : '-') . \bin2hex($bin);
  60. }
  61. }
  62. }