]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/block.php
define LACONICA and accept LACONICA for backwards compatibility
[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 Action
46 {
47     var $profile = null;
48     /**
49      * Take arguments for running
50      *
51      * @param array $args $_REQUEST args
52      *
53      * @return boolean success flag
54      */
55     function prepare($args)
56     {
57         parent::prepare($args);
58         if (!common_logged_in()) {
59             $this->clientError(_('Not logged in.'));
60             return false;
61         }
62         $token = $this->trimmed('token');
63         if (!$token || $token != common_session_token()) {
64             $this->clientError(_('There was a problem with your session token. Try again, please.'));
65             return;
66         }
67         $id = $this->trimmed('blockto');
68         if (!$id) {
69             $this->clientError(_('No profile specified.'));
70             return false;
71         }
72         $this->profile = Profile::staticGet('id', $id);
73         if (!$this->profile) {
74             $this->clientError(_('No profile with that ID.'));
75             return false;
76         }
77         return true;
78     }
79
80     /**
81      * Handle request
82      *
83      * Shows a page with list of favorite notices
84      *
85      * @param array $args $_REQUEST args; handled in prepare()
86      *
87      * @return void
88      */
89     function handle($args)
90     {
91         parent::handle($args);
92         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
93             if ($this->arg('no')) {
94                 $cur = common_current_user();
95                 $other = Profile::staticGet('id', $this->arg('blockto'));
96                 common_redirect(common_local_url('showstream', array('nickname' => $other->nickname)),
97                                 303);
98             } elseif ($this->arg('yes')) {
99                 $this->blockProfile();
100             } elseif ($this->arg('blockto')) {
101                 $this->showPage();
102             }
103         }
104     }
105
106     function showContent() {
107         $this->areYouSureForm();
108     }
109
110     function title() {
111         return _('Block user');
112     }
113
114     function showNoticeForm() {
115         // nop
116     }
117
118     /**
119      * Confirm with user.
120      *
121      * Shows a confirmation form.
122      *
123      * @return void
124      */
125     function areYouSureForm()
126     {
127         $id = $this->profile->id;
128         $this->elementStart('form', array('id' => 'block-' . $id,
129                                            'method' => 'post',
130                                            'class' => 'form_settings form_entity_block',
131                                            'action' => common_local_url('block')));
132         $this->elementStart('fieldset');
133         $this->hidden('token', common_session_token());
134         $this->element('legend', _('Block user'));
135         $this->element('p', null,
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' => 'blockto',
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', _('No'), 'submit form_action-primary', 'no', _("Do not block this user from this group"));
150         $this->submit('form_action-yes', _('Yes'), 'submit form_action-secondary', 'yes', _('Block this user from this group'));
151         $this->elementEnd('fieldset');
152         $this->elementEnd('form');
153     }
154
155     /**
156      * Actually block a user.
157      *
158      * @return void
159      */
160     function blockProfile()
161     {
162         $cur = common_current_user();
163
164         if ($cur->hasBlocked($this->profile)) {
165             $this->clientError(_('You have already blocked this user.'));
166             return;
167         }
168         $result = $cur->block($this->profile);
169         if (!$result) {
170             $this->serverError(_('Failed to save block information.'));
171             return;
172         }
173
174         // Now, gotta figure where we go back to
175         foreach ($this->args as $k => $v) {
176             if ($k == 'returnto-action') {
177                 $action = $v;
178             } elseif (substr($k, 0, 9) == 'returnto-') {
179                 $args[substr($k, 9)] = $v;
180             }
181         }
182
183         if ($action) {
184             common_redirect(common_local_url($action, $args), 303);
185         } else {
186             common_redirect(common_local_url('subscribers',
187                                              array('nickname' => $cur->nickname)),
188                             303);
189         }
190     }
191 }
192