block.php 7.1 KB

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