PMBlock.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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\Pages\PM;
  10. use ForkBB\Core\Validator;
  11. use ForkBB\Models\Page;
  12. use ForkBB\Models\Pages\PM\AbstractPM;
  13. use ForkBB\Models\PM\Cnst;
  14. use ForkBB\Models\PM\PPost;
  15. use ForkBB\Models\PM\PTopic;
  16. use ForkBB\Models\User\User;
  17. use InvalidArgumentException;
  18. use function \ForkBB\__;
  19. class PMBlock extends AbstractPM
  20. {
  21. const BLOCK = 'block';
  22. const UNBLOCK = 'unblock';
  23. /**
  24. * Подготовка данных для шаблона
  25. */
  26. protected function view(array $args, string $method): Page
  27. {
  28. $this->nameTpl = 'pm/block';
  29. $this->blockList = $this->c->pms->block->list;
  30. $this->pmCrumbs[] = [$this->c->Router->link('PMAction', $args), 'Blocked users'];
  31. return $this;
  32. }
  33. /**
  34. * (Раз)блокировка пользователя или отображение списка заблокированных
  35. */
  36. public function block(array $args, string $method): Page
  37. {
  38. $this->args = $args;
  39. $this->pmIndex = Cnst::ACTION_BLOCK;
  40. if (! isset($args['more1'])) {
  41. return $this->view($args, $method);
  42. }
  43. if (isset($args['more2'])) {
  44. if ('' !== \trim($args['more2'], '1234567890')) {
  45. return $this->c->Message->message('Bad request');
  46. }
  47. $post = $this->pms->load(Cnst::PPOST, (int) $args['more2']);
  48. if (! $post instanceof PPost
  49. || $args['more1'] !== $post->poster_id
  50. ) {
  51. return $this->c->Message->message('Bad request');
  52. }
  53. } else {
  54. $post = null;
  55. }
  56. $blockUser = $this->c->users->load($args['more1']);
  57. if (! $blockUser instanceof User) {
  58. return $this->c->Message->message('Bad request');
  59. }
  60. $blockStatus = $this->pms->block->isBlock($blockUser);
  61. if (
  62. ! $blockStatus
  63. && (
  64. ! $post
  65. || ! $this->pms->block->canBlock($blockUser)
  66. )
  67. ) {
  68. return $this->c->Message->message('Invalid action');
  69. }
  70. $this->c->Lang->load('validator');
  71. $this->linkPMBlk = $this->c->Router->link('PMAction', ['action' => Cnst::ACTION_BLOCK]);
  72. $this->linkBack = $post instanceof PPost ? $post->link : $this->linkPMBlk;
  73. if ('POST' === $method) {
  74. $v = $this->c->Validator->reset()
  75. ->addRules([
  76. 'token' => 'token:PMAction',
  77. 'confirm' => 'checkbox',
  78. self::BLOCK => 'string',
  79. self::UNBLOCK => 'string',
  80. ])->addAliases([
  81. ])->addArguments([
  82. 'token' => $args,
  83. ]);
  84. if (
  85. ! $v->validation($_POST)
  86. || '1' !== $v->confirm
  87. ) {
  88. return $this->c->Redirect->url($this->linkBack)->message('No confirm redirect');
  89. } elseif (
  90. (
  91. ! $v->{self::BLOCK}
  92. && ! $v->{self::UNBLOCK}
  93. )
  94. || (
  95. $v->{self::BLOCK}
  96. && $blockStatus
  97. )
  98. || (
  99. $v->{self::UNBLOCK}
  100. && ! $blockStatus
  101. )
  102. ) {
  103. return $this->c->Message->message('Invalid action');
  104. }
  105. if ($v->{self::BLOCK}) {
  106. $this->pms->block->add($blockUser);
  107. $message = 'User is blocked redirect';
  108. } else {
  109. $this->pms->block->remove($blockUser);
  110. $message = 'User is unblocked redirect';
  111. }
  112. return $this->c->Redirect->url($this->linkBack)->message($message);
  113. }
  114. $this->nameTpl = 'pm/form';
  115. $this->formTitle = $blockStatus ? 'Unblock user title' : 'Block user title';
  116. $this->formClass = 'block';
  117. $this->form = $this->formBlock($args, $blockStatus, $blockUser);
  118. $this->pmCrumbs[] = [
  119. $this->c->Router->link('PMAction', $args),
  120. [$blockStatus ? 'Unblock user %s crumb' : 'Block user %s crumb', $blockUser->username],
  121. ];
  122. $this->pmCrumbs[] = [$this->linkPMBlk, 'Blocked users'];
  123. return $this;
  124. }
  125. /**
  126. * Подготавливает массив данных для формы
  127. */
  128. protected function formBlock(array $args, bool $status, User $user): array
  129. {
  130. $btn = $status ? self::UNBLOCK : self::BLOCK;
  131. return [
  132. 'action' => $this->c->Router->link('PMAction', $args),
  133. 'hidden' => [
  134. 'token' => $this->c->Csrf->create('PMAction', $args),
  135. ],
  136. 'sets' => [
  137. 'info' => [
  138. 'info' => [
  139. [
  140. 'value' => __([$status ? 'Unblock user %s' : 'Block user %s', $user->username]),
  141. 'html' => true,
  142. ],
  143. ],
  144. ],
  145. 'confirm' => [
  146. 'fields' => [
  147. 'confirm' => [
  148. 'type' => 'checkbox',
  149. 'label' => 'Confirm action',
  150. 'checked' => false,
  151. ],
  152. ],
  153. ],
  154. ],
  155. 'btns' => [
  156. $btn => [
  157. 'type' => 'submit',
  158. 'value' => __($status ? 'Unblock' : 'Block'),
  159. ],
  160. 'cancel' => [
  161. 'type' => 'btn',
  162. 'value' => __('Cancel'),
  163. 'link' => $this->linkBack,
  164. ],
  165. ],
  166. ];
  167. }
  168. }