IsUniqueName.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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\User;
  10. use ForkBB\Models\Action;
  11. use ForkBB\Models\User\User;
  12. class IsUniqueName extends Action
  13. {
  14. /**
  15. * Добавляет экранированный символ в конец каждого элемента $input дописывая их в $output
  16. */
  17. protected function addSymbol(array $input, string $symbol, array $output = []): array
  18. {
  19. if ('#%' === $symbol) {
  20. $symbol = '%';
  21. } else {
  22. $symbol = \str_replace(['#', '_', '%'], ['##', '#_', '#%'], $symbol);
  23. }
  24. if (empty($input)) {
  25. $input = [''];
  26. }
  27. foreach ($input as $str) {
  28. $output[] = $str . $symbol;
  29. }
  30. return $output;
  31. }
  32. /**
  33. * Строит массив вариантов для сранения LIKE
  34. */
  35. protected function variants(string $str): array
  36. {
  37. \preg_match_all('%.%us', $str, $matches);
  38. $result = [];
  39. foreach ($matches[0] as $i => $symbol) {
  40. $tmp = [];
  41. if (isset($symbol[1])) {
  42. if ($i > 1) {
  43. return $this->addSymbol($result, '#%'); // добавить % (без #) в конец каждого элемента
  44. }
  45. $symbolL = \mb_strtolower($symbol, 'UTF-8');
  46. $symbolU = \mb_strtoupper($symbol, 'UTF-8');
  47. if ($symbolL !== $symbol) {
  48. $tmp = $this->addSymbol($result, $symbolL, $tmp);
  49. }
  50. if ($symbolU !== $symbol) {
  51. $tmp = $this->addSymbol($result, $symbolU, $tmp);
  52. }
  53. }
  54. $result = $this->addSymbol($result, $symbol, $tmp);
  55. }
  56. return $result;
  57. }
  58. /**
  59. * Проверка на уникальность имени пользователя
  60. */
  61. public function isUniqueName(User $user): bool
  62. {
  63. $name7bit = 0 === \preg_match('%[\x80-\xFF]%', $user->username);
  64. $norm7bit = 0 === \preg_match('%[\x80-\xFF]%', $user->username_normal);
  65. $like = 'LIKE';
  66. switch ($this->c->DB->getType()) {
  67. // case 'mysql':
  68. // break;
  69. case 'pgsql':
  70. $like = 'ILIKE';
  71. // case 'sqlite':
  72. default:
  73. // UTF-8 не нужен
  74. if ($name7bit && $norm7bit) {
  75. break;
  76. }
  77. // бд поддерживает UTF-8 сравнение без учета регистра
  78. if ($this->c->config->insensitive()) {
  79. break;
  80. }
  81. $vars = [(int) $user->id];
  82. $query = 'SELECT u.username, u.username_normal
  83. FROM ::users AS u
  84. WHERE u.id!=?i
  85. AND (';
  86. $sptr = '';
  87. $arr = $this->variants($user->username);
  88. foreach ($arr as $value) {
  89. $vars[] = $value;
  90. $query .= "{$sptr}u.username {$like} ?s ESCAPE '#'";
  91. $sptr = ' OR ';
  92. }
  93. $arr = $this->variants($user->username_normal);
  94. foreach ($arr as $value) {
  95. $vars[] = $value;
  96. $query .= "{$sptr}u.username_normal {$like} ?s ESCAPE '#'";
  97. $sptr = ' OR ';
  98. }
  99. $query .= ')';
  100. $nameL = \mb_strtolower($user->username, 'UTF-8');
  101. $nameU = \mb_strtoupper($user->username, 'UTF-8');
  102. $normL = \mb_strtolower($user->username_normal, 'UTF-8');
  103. $normU = \mb_strtoupper($user->username_normal, 'UTF-8');
  104. $stmt = $this->c->DB->query($query, $vars);
  105. while (false !== ($row = $stmt->fetch())) {
  106. if (
  107. \mb_strtolower($row['username'], 'UTF-8') === $nameL
  108. || \mb_strtoupper($row['username'], 'UTF-8') === $nameU
  109. || \mb_strtolower($row['username_normal'], 'UTF-8') === $normL
  110. || \mb_strtoupper($row['username_normal'], 'UTF-8') === $normU
  111. ) {
  112. $stmt->closeCursor();
  113. return false;
  114. }
  115. }
  116. return true;
  117. }
  118. $vars = [
  119. ':id' => (int) $user->id,
  120. ':name' => \str_replace(['#', '_', '%'], ['##', '#_', '#%'], $user->username),
  121. ':norm' => \str_replace(['#', '_', '%'], ['##', '#_', '#%'], $user->username_normal),
  122. ];
  123. $query = "SELECT 1
  124. FROM ::users AS u
  125. WHERE u.id!=?i:id
  126. AND (
  127. u.username {$like} ?s:name ESCAPE '#'
  128. OR u.username_normal {$like} ?s:norm ESCAPE '#'
  129. )";
  130. $result = $this->c->DB->query($query, $vars)->fetchAll();
  131. return ! \count($result);
  132. }
  133. }