We are no longer offering accounts on this server. Consider https://gitlab.freedesktop.org/ as a place to host projects.

block.php 4.45 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
<?php
/*
 * Laconica - a distributed open-source microblogging tool
 * Copyright (C) 2008, Controlez-Vous, Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

if (!defined('LACONICA')) { exit(1); }

22 23
class BlockAction extends Action
{
24

Evan Prodromou's avatar
Evan Prodromou committed
25
    var $profile = null;
26

27 28
    function prepare($args)
    {
29 30 31 32

        parent::prepare($args);

        if (!common_logged_in()) {
33
            $this->clientError(_('Not logged in.'));
34 35 36
            return false;
        }

37
        $token = $this->trimmed('token');
38

39
        if (!$token || $token != common_session_token()) {
40
            $this->clientError(_('There was a problem with your session token. Try again, please.'));
41 42
            return;
        }
43 44 45 46

        $id = $this->trimmed('blockto');

        if (!$id) {
47
            $this->clientError(_('No profile specified.'));
48 49 50 51 52 53
            return false;
        }

        $this->profile = Profile::staticGet('id', $id);

        if (!$this->profile) {
54
            $this->clientError(_('No profile with that ID.'));
55 56
            return false;
        }
Evan Prodromou's avatar
Evan Prodromou committed
57 58

        return true;
59 60
    }

61 62
    function handle($args)
    {
63
        parent::handle($args);
64 65 66 67 68 69 70 71 72 73 74 75 76
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            if ($this->arg('block')) {
                $this->are_you_sure_form();
            } else if ($this->arg('no')) {
                $cur = common_current_user();
                common_redirect(common_local_url('subscribers',
                                                 array('nickname' => $cur->nickname)));
            } else if ($this->arg('yes')) {
                $this->block_profile();
            }
        }
    }

77 78
    function are_you_sure_form()
    {
79 80 81

        $id = $this->profile->id;

82
        common_show_header(_('Block user'));
Evan Prodromou's avatar
Evan Prodromou committed
83

84
        $this->element('p', null,
85 86 87 88
                       _('Are you sure you want to block this user? '.
                         'Afterwards, they will be unsubscribed from you, '.
                         'unable to subscribe to you in the future, and '.
                         'you will not be notified of any @-replies from them.'));
89

90
        $this->elementStart('form', array('id' => 'block-' . $id,
91 92 93 94
                                           'method' => 'post',
                                           'class' => 'block',
                                           'action' => common_local_url('block')));

95
        $this->hidden('token', common_session_token());
96

97
        $this->element('input', array('id' => 'blockto-' . $id,
98 99 100 101
                                      'name' => 'blockto',
                                      'type' => 'hidden',
                                      'value' => $id));

102 103
        foreach ($this->args as $k => $v) {
            if (substr($k, 0, 9) == 'returnto-') {
104
                $this->hidden($k, $v);
105 106 107
            }
        }

108 109
        $this->submit('no', _('No'));
        $this->submit('yes', _('Yes'));
110

111
        $this->elementEnd('form');
Evan Prodromou's avatar
Evan Prodromou committed
112 113

        common_show_footer();
114 115
    }

116 117
    function block_profile()
    {
118 119 120 121

        $cur = common_current_user();

        if ($cur->hasBlocked($this->profile)) {
122
            $this->clientError(_('You have already blocked this user.'));
123 124 125
            return;
        }

Evan Prodromou's avatar
Evan Prodromou committed
126
        $result = $cur->block($this->profile);
127 128

        if (!$result) {
129
            $this->serverError(_('Failed to save block information.'));
130 131 132
            return;
        }

133 134 135 136 137 138
        # Now, gotta figure where we go back to

        foreach ($this->args as $k => $v) {
            if ($k == 'returnto-action') {
                $action = $v;
            } else if (substr($k, 0, 9) == 'returnto-') {
139
                $args[substr($k, 9)] = $v;
140 141 142 143 144 145 146 147 148
            }
        }

        if ($action) {
            common_redirect(common_local_url($action, $args));
        } else {
            common_redirect(common_local_url('subscriptions',
                                             array('nickname' => $cur->nickname)));
        }
149 150
    }
}