The official, canonical postActiv repository. http://www.postactiv.com

block.php 7.8KB

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