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