]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/block.php
Merge activity plugin into mainline
[quix0rs-gnu-social.git] / actions / block.php
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
31 if (!defined('STATUSNET') && !defined('LACONICA')) {
32     exit(1);
33 }
34
35 /**
36  * Block a user action class.
37  *
38  * @category Action
39  * @package  StatusNet
40  * @author   Evan Prodromou <evan@status.net>
41  * @author   Robin Millette <millette@status.net>
42  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
43  * @link     http://status.net/
44  */
45 class BlockAction extends ProfileFormAction
46 {
47     var $profile = null;
48
49     /**
50      * Take arguments for running
51      *
52      * @param array $args $_REQUEST args
53      *
54      * @return boolean success flag
55      */
56     function prepare($args)
57     {
58         if (!parent::prepare($args)) {
59             return false;
60         }
61
62         $cur = common_current_user();
63
64         assert(!empty($cur)); // checked by parent
65
66         if ($cur->hasBlocked($this->profile)) {
67             // TRANS: Client error displayed when blocking a user that has already been blocked.
68             $this->clientError(_('You already blocked that user.'));
69             return false;
70         }
71
72         return true;
73     }
74
75     /**
76      * Handle request
77      *
78      * Shows a page with list of favorite notices
79      *
80      * @param array $args $_REQUEST args; handled in prepare()
81      *
82      * @return void
83      */
84     function handle($args)
85     {
86         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
87             if ($this->arg('no')) {
88                 $this->returnToPrevious();
89             } elseif ($this->arg('yes')) {
90                 $this->handlePost();
91                 $this->returnToPrevious();
92             } else {
93                 $this->showPage();
94             }
95         } else {
96             $this->showPage();
97         }
98     }
99
100     function showContent() {
101         $this->areYouSureForm();
102     }
103
104     function title() {
105         // TRANS: Title for block user page.
106         return _('Block user');
107     }
108
109     function showNoticeForm() {
110         // nop
111     }
112
113     /**
114      * Confirm with user.
115      *
116      * Shows a confirmation form.
117      *
118      * @return void
119      */
120     function areYouSureForm()
121     {
122         // @fixme if we ajaxify the confirmation form, skip the preview on ajax hits
123         $profile = new ArrayWrapper(array($this->profile));
124         $preview = new ProfileList($profile, $this);
125         $preview->show();
126
127
128         $id = $this->profile->id;
129         $this->elementStart('form', array('id' => 'block-' . $id,
130                                            'method' => 'post',
131                                            'class' => 'form_settings form_entity_block',
132                                            'action' => common_local_url('block')));
133         $this->elementStart('fieldset');
134         $this->hidden('token', common_session_token());
135         // TRANS: Legend for block user form.
136         $this->element('legend', _('Block user'));
137         $this->element('p', null,
138                        // TRANS: Explanation of consequences when blocking a user on the block user page.
139                        _('Are you sure you want to block this user? '.
140                          'Afterwards, they will be unsubscribed from you, '.
141                          'unable to subscribe to you in the future, and '.
142                          'you will not be notified of any @-replies from them.'));
143         $this->element('input', array('id' => 'blockto-' . $id,
144                                       'name' => 'profileid',
145                                       'type' => 'hidden',
146                                       'value' => $id));
147         foreach ($this->args as $k => $v) {
148             if (substr($k, 0, 9) == 'returnto-') {
149                 $this->hidden($k, $v);
150             }
151         }
152         $this->submit('form_action-no',
153                       // TRANS: Button label on the user block form.
154                       _m('BUTTON','No'),
155                       'submit form_action-primary',
156                       'no',
157                       // TRANS: Submit button title for 'No' when blocking a user.
158                       _('Do not block this user.'));
159         $this->submit('form_action-yes',
160                       // TRANS: Button label on the user block form.
161                       _m('BUTTON','Yes'),
162                       'submit form_action-secondary',
163                       'yes',
164                       // TRANS: Submit button title for 'Yes' when blocking a user.
165                       _('Block this user.'));
166         $this->elementEnd('fieldset');
167         $this->elementEnd('form');
168     }
169
170     /**
171      * Actually block a user.
172      *
173      * @return void
174      */
175
176     function handlePost()
177     {
178         $cur = common_current_user();
179
180         if (Event::handle('StartBlockProfile', array($cur, $this->profile))) {
181             $result = $cur->block($this->profile);
182             if ($result) {
183                 Event::handle('EndBlockProfile', array($cur, $this->profile));
184             }
185         }
186
187         if (!$result) {
188             // TRANS: Server error displayed when blocking a user fails.
189             $this->serverError(_('Failed to save block information.'));
190             return;
191         }
192     }
193
194     function showScripts()
195     {
196         parent::showScripts();
197         $this->autofocus('form_action-yes');
198     }
199
200     /**
201      * Override for form session token checks; on our first hit we're just
202      * requesting confirmation, which doesn't need a token. We need to be
203      * able to take regular GET requests from email!
204      *
205      * @throws ClientException if token is bad on POST request or if we have
206      *         confirmation parameters which could trigger something.
207      */
208     function checkSessionToken()
209     {
210         if ($_SERVER['REQUEST_METHOD'] == 'POST' ||
211             $this->arg('yes') ||
212             $this->arg('no')) {
213
214             return parent::checkSessionToken();
215         }
216     }
217
218     /**
219      * If we reached this form without returnto arguments, return to the
220      * current user's subscription list.
221      *
222      * @return string URL
223      */
224     function defaultReturnTo()
225     {
226         $user = common_current_user();
227         if ($user) {
228             return common_local_url('subscribers',
229                                     array('nickname' => $user->nickname));
230         } else {
231             return common_local_url('public');
232         }
233     }
234 }