]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/block.php
Merge branch 'mgrdcm-review' into 0.7.x
[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  Laconica
9  * @author   Evan Prodromou <evan@controlyourself.ca>
10  * @author   Robin Millette <millette@controlyourself.ca>
11  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
12  * @link     http://laconi.ca/
13  *
14  * Laconica - a distributed open-source microblogging tool
15  * Copyright (C) 2008, Controlez-Vous, 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('LACONICA')) {
32     exit(1);
33 }
34
35 /**
36  * Block a user action class.
37  *
38  * @category Action
39  * @package  Laconica
40  * @author   Evan Prodromou <evan@controlyourself.ca>
41  * @author   Robin Millette <millette@controlyourself.ca>
42  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
43  * @link     http://laconi.ca/
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->element('p', null,
129                        _('Are you sure you want to block this user? '.
130                          'Afterwards, they will be unsubscribed from you, '.
131                          'unable to subscribe to you in the future, and '.
132                          'you will not be notified of any @-replies from them.'));
133         $this->elementStart('form', array('id' => 'block-' . $id,
134                                            'method' => 'post',
135                                            'class' => 'block',
136                                            'action' => common_local_url('block')));
137         $this->hidden('token', common_session_token());
138         $this->element('input', array('id' => 'blockto-' . $id,
139                                       'name' => 'blockto',
140                                       'type' => 'hidden',
141                                       'value' => $id));
142         foreach ($this->args as $k => $v) {
143             if (substr($k, 0, 9) == 'returnto-') {
144                 $this->hidden($k, $v);
145             }
146         }
147         $this->submit('no', _('No'));
148         $this->submit('yes', _('Yes'));
149         $this->elementEnd('form');
150     }
151
152     /**
153      * Actually block a user.
154      *
155      * @return void
156      */
157     function blockProfile()
158     {
159         $cur = common_current_user();
160
161         if ($cur->hasBlocked($this->profile)) {
162             $this->clientError(_('You have already blocked this user.'));
163             return;
164         }
165         $result = $cur->block($this->profile);
166         if (!$result) {
167             $this->serverError(_('Failed to save block information.'));
168             return;
169         }
170
171         // Now, gotta figure where we go back to
172         foreach ($this->args as $k => $v) {
173             if ($k == 'returnto-action') {
174                 $action = $v;
175             } elseif (substr($k, 0, 9) == 'returnto-') {
176                 $args[substr($k, 9)] = $v;
177             }
178         }
179
180         if ($action) {
181             common_redirect(common_local_url($action, $args), 303);
182         } else {
183             common_redirect(common_local_url('subscribers',
184                                              array('nickname' => $cur->nickname)),
185                             303);
186         }
187     }
188 }
189