]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/block.php
Merge branch 'master' of ../trunk
[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('block')) {
94                 $this->areYouSureForm();
95             } else if ($this->arg('no')) {
96                 $cur = common_current_user();
97                 common_redirect(common_local_url('subscribers', array('nickname' => $cur->nickname)));
98             } else if ($this->arg('yes')) {
99                 $this->blockProfile();
100             }
101         }
102     }
103
104     /**
105      * Confirm with user.
106      *
107      * Shows a confirmation form.
108      *
109      * @return void
110      */
111     function areYouSureForm()
112     {
113         $id = $this->profile->id;
114         common_show_header(_('Block user'));
115         $this->element('p', null,
116                        _('Are you sure you want to block this user? '.
117                          'Afterwards, they will be unsubscribed from you, '.
118                          'unable to subscribe to you in the future, and '.
119                          'you will not be notified of any @-replies from them.'));
120         $this->elementStart('form', array('id' => 'block-' . $id,
121                                            'method' => 'post',
122                                            'class' => 'block',
123                                            'action' => common_local_url('block')));
124         $this->hidden('token', common_session_token());
125         $this->element('input', array('id' => 'blockto-' . $id,
126                                       'name' => 'blockto',
127                                       'type' => 'hidden',
128                                       'value' => $id));
129         foreach ($this->args as $k => $v) {
130             if (substr($k, 0, 9) == 'returnto-') {
131                 $this->hidden($k, $v);
132             }
133         }
134         $this->submit('no', _('No'));
135         $this->submit('yes', _('Yes'));
136         $this->elementEnd('form');
137         common_show_footer();
138     }
139
140     /**
141      * Actually block a user.
142      *
143      * @return void
144      */
145     function blockProfile()
146     {
147         $cur = common_current_user();
148
149         if ($cur->hasBlocked($this->profile)) {
150             $this->clientError(_('You have already blocked this user.'));
151             return;
152         }
153         $result = $cur->block($this->profile);
154         if (!$result) {
155             $this->serverError(_('Failed to save block information.'));
156             return;
157         }
158
159         // Now, gotta figure where we go back to
160         foreach ($this->args as $k => $v) {
161             if ($k == 'returnto-action') {
162                 $action = $v;
163             } else if (substr($k, 0, 9) == 'returnto-') {
164                 $args[substr($k, 9)] = $v;
165             }
166         }
167
168         if ($action) {
169             common_redirect(common_local_url($action, $args));
170         } else {
171             common_redirect(common_local_url('subscriptions',
172                                              array('nickname' => $cur->nickname)));
173         }
174     }
175 }
176