]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/block.php
XSS vulnerability when remote-subscribing
[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         }
70
71         return true;
72     }
73
74     /**
75      * Handle request
76      *
77      * @param array $args $_REQUEST args; handled in prepare()
78      *
79      * @return void
80      */
81     function handle($args)
82     {
83         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
84             if ($this->arg('no')) {
85                 $this->returnToPrevious();
86             } elseif ($this->arg('yes')) {
87                 $this->handlePost();
88                 $this->returnToPrevious();
89             } else {
90                 $this->showPage();
91             }
92         } else {
93             $this->showPage();
94         }
95     }
96
97     function showContent() {
98         $this->areYouSureForm();
99     }
100
101     function title() {
102         // TRANS: Title for block user page.
103         return _('Block user');
104     }
105
106     function showNoticeForm() {
107         // nop
108     }
109
110     /**
111      * Confirm with user.
112      *
113      * Shows a confirmation form.
114      *
115      * @return void
116      */
117     function areYouSureForm()
118     {
119         // @fixme if we ajaxify the confirmation form, skip the preview on ajax hits
120         $profile = new ArrayWrapper(array($this->profile));
121         $preview = new ProfileList($profile, $this);
122         $preview->show();
123
124
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     /**
168      * Actually block a user.
169      *
170      * @return void
171      */
172
173     function handlePost()
174     {
175         $cur = common_current_user();
176
177         if (Event::handle('StartBlockProfile', array($cur, $this->profile))) {
178             $result = $cur->block($this->profile);
179             if ($result) {
180                 Event::handle('EndBlockProfile', array($cur, $this->profile));
181             }
182         }
183
184         if (!$result) {
185             // TRANS: Server error displayed when blocking a user fails.
186             $this->serverError(_('Failed to save block information.'));
187         }
188     }
189
190     function showScripts()
191     {
192         parent::showScripts();
193         $this->autofocus('form_action-yes');
194     }
195
196     /**
197      * Override for form session token checks; on our first hit we're just
198      * requesting confirmation, which doesn't need a token. We need to be
199      * able to take regular GET requests from email!
200      *
201      * @throws ClientException if token is bad on POST request or if we have
202      *         confirmation parameters which could trigger something.
203      */
204     function checkSessionToken()
205     {
206         if ($_SERVER['REQUEST_METHOD'] == 'POST' ||
207             $this->arg('yes') ||
208             $this->arg('no')) {
209
210             return parent::checkSessionToken();
211         }
212     }
213
214     /**
215      * If we reached this form without returnto arguments, return to the
216      * current user's subscription list.
217      *
218      * @return string URL
219      */
220     function defaultReturnTo()
221     {
222         $user = common_current_user();
223         if ($user) {
224             return common_local_url('subscribers',
225                                     array('nickname' => $user->nickname));
226         } else {
227             return common_local_url('public');
228         }
229     }
230 }