block.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Block a user action class.
  18. *
  19. * @category Action
  20. * @package GNUsocial
  21. * @author Evan Prodromou <evan@status.net>
  22. * @author Robin Millette <millette@status.net>
  23. * @copyright 2008, 2009 StatusNet, Inc.
  24. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  25. */
  26. defined('GNUSOCIAL') || die();
  27. /**
  28. * Block a user action class.
  29. *
  30. * @category Action
  31. * @package GNUsocial
  32. * @author Evan Prodromou <evan@status.net>
  33. * @author Robin Millette <millette@status.net>
  34. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  35. */
  36. class BlockAction extends ProfileFormAction
  37. {
  38. public $profile = null;
  39. /**
  40. * Take arguments for running
  41. *
  42. * @param array $args $_REQUEST args
  43. *
  44. * @return boolean success flag
  45. */
  46. public function prepare(array $args = []): bool
  47. {
  48. if (!parent::prepare($args)) {
  49. return false;
  50. }
  51. $cur = common_current_user();
  52. assert(!empty($cur)); // checked by parent
  53. if ($cur->hasBlocked($this->profile)) {
  54. // TRANS: Client error displayed when blocking a user that has already been blocked.
  55. $this->clientError(_('You already blocked that user.'));
  56. }
  57. return true;
  58. }
  59. /**
  60. * Handle request
  61. *
  62. * @param array $args $_REQUEST args; handled in prepare()
  63. *
  64. * @return void
  65. */
  66. public function handle(): void
  67. {
  68. if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  69. if ($this->arg('no')) {
  70. $this->returnToPrevious();
  71. } elseif ($this->arg('yes')) {
  72. $this->handlePost();
  73. $this->returnToPrevious();
  74. } else {
  75. $this->showPage();
  76. }
  77. } else {
  78. $this->showPage();
  79. }
  80. }
  81. public function showContent(): void
  82. {
  83. $this->areYouSureForm();
  84. }
  85. public function title(): string
  86. {
  87. // TRANS: Title for block user page.
  88. return _('Block user');
  89. }
  90. public function showNoticeForm(): void
  91. {
  92. // nop
  93. }
  94. /**
  95. * Confirm with user.
  96. *
  97. * Shows a confirmation form.
  98. *
  99. * @return void
  100. */
  101. public function areYouSureForm()
  102. {
  103. // @fixme if we ajaxify the confirmation form, skip the preview on ajax hits
  104. $profile = new ArrayWrapper(array($this->profile));
  105. $preview = new ProfileList($profile, $this);
  106. $preview->show();
  107. $id = $this->profile->id;
  108. $this->elementStart('form', array('id' => 'block-' . $id,
  109. 'method' => 'post',
  110. 'class' => 'form_settings form_entity_block',
  111. 'action' => common_local_url('block')));
  112. $this->elementStart('fieldset');
  113. $this->hidden('token', common_session_token());
  114. // TRANS: Legend for block user form.
  115. $this->element('legend', _('Block user'));
  116. $this->element(
  117. 'p',
  118. null,
  119. // TRANS: Explanation of consequences when blocking a user on the block user page.
  120. _('Are you sure you want to block this user? '
  121. . 'Afterwards, they will be unsubscribed from you, '
  122. . 'unable to subscribe to you in the future, and '
  123. . 'you will not be notified of any @-replies from them.')
  124. );
  125. $this->element('input', [
  126. 'id' => 'blockto-' . $id,
  127. 'name' => 'profileid',
  128. 'type' => 'hidden',
  129. 'value' => $id
  130. ]);
  131. foreach ($this->args as $k => $v) {
  132. if (substr($k, 0, 9) == 'returnto-') {
  133. $this->hidden($k, $v);
  134. }
  135. }
  136. $this->submit(
  137. 'form_action-no',
  138. // TRANS: Button label on the user block form.
  139. _m('BUTTON', 'No'),
  140. 'submit form_action-primary',
  141. 'no',
  142. // TRANS: Submit button title for 'No' when blocking a user.
  143. _('Do not block this user.')
  144. );
  145. $this->submit(
  146. 'form_action-yes',
  147. // TRANS: Button label on the user block form.
  148. _m('BUTTON', 'Yes'),
  149. 'submit form_action-secondary',
  150. 'yes',
  151. // TRANS: Submit button title for 'Yes' when blocking a user.
  152. _('Block this user.')
  153. );
  154. $this->elementEnd('fieldset');
  155. $this->elementEnd('form');
  156. }
  157. /**
  158. * Actually block a user.
  159. *
  160. * @return void
  161. */
  162. public function handlePost(): void
  163. {
  164. $cur = common_current_user();
  165. if (Event::handle('StartBlockProfile', array($cur, $this->profile))) {
  166. $result = $cur->block($this->profile);
  167. if ($result) {
  168. Event::handle('EndBlockProfile', array($cur, $this->profile));
  169. }
  170. }
  171. if (!$result) {
  172. // TRANS: Server error displayed when blocking a user fails.
  173. $this->serverError(_('Failed to save block information.'));
  174. }
  175. }
  176. public function showScripts(): void
  177. {
  178. parent::showScripts();
  179. $this->autofocus('form_action-yes');
  180. }
  181. /**
  182. * Override for form session token checks; on our first hit we're just
  183. * requesting confirmation, which doesn't need a token. We need to be
  184. * able to take regular GET requests from email!
  185. *
  186. * @throws ClientException if token is bad on POST request or if we have
  187. * confirmation parameters which could trigger something.
  188. */
  189. public function checkSessionToken(): void
  190. {
  191. if (
  192. $_SERVER['REQUEST_METHOD'] === 'POST'
  193. || $this->arg('yes')
  194. || $this->arg('no')
  195. ) {
  196. parent::checkSessionToken();
  197. }
  198. }
  199. /**
  200. * If we reached this form without returnto arguments, return to the
  201. * current user's subscription list.
  202. *
  203. * @return string URL
  204. */
  205. public function defaultReturnTo()
  206. {
  207. $user = common_current_user();
  208. if ($user) {
  209. return common_local_url(
  210. 'subscribers',
  211. ['nickname' => $user->nickname]
  212. );
  213. } else {
  214. return common_local_url('public');
  215. }
  216. }
  217. }