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

subscribe.php 4.46 KB
Newer Older
Evan Prodromou's avatar
Evan Prodromou committed
1
<?php
2
/**
3
 * StatusNet - the distributed open-source microblogging tool
4
 * Copyright (C) 2008-2011, StatusNet, Inc.
5 6
 *
 * Subscription action.
Evan Prodromou's avatar
Evan Prodromou committed
7
 *
Evan Prodromou's avatar
Evan Prodromou committed
8 9 10 11
 * 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.
Evan Prodromou's avatar
Evan Prodromou committed
12
 *
Evan Prodromou's avatar
Evan Prodromou committed
13 14 15 16
 * 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.
Evan Prodromou's avatar
Evan Prodromou committed
17
 *
Evan Prodromou's avatar
Evan Prodromou committed
18 19
 * 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/>.
20 21 22 23 24 25 26 27 28
 *
 * PHP version 5
 *
 * @category  Action
 * @package   StatusNet
 * @author    Evan Prodromou <evan@status.net>
 * @copyright 2008-2010 StatusNet, Inc.
 * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
 * @link      http://status.net/
Evan Prodromou's avatar
Evan Prodromou committed
29 30
 */

31 32 33 34 35 36 37
if (!defined('STATUSNET')) {
    exit(1);
}

/**
 * Subscription action
 *
38
 * Subscribing to a profile. Likely to work for OStatus profiles.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
 *
 * Takes parameters:
 *
 *    - subscribeto: a profile ID
 *    - token: session token to prevent CSRF attacks
 *    - ajax: boolean; whether to return Ajax or full-browser results
 *
 * Only works if the current user is logged in.
 *
 * @category  Action
 * @package   StatusNet
 * @author    Evan Prodromou <evan@status.net>
 * @copyright 2008-2010 StatusNet, Inc.
 * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
 * @link      http://status.net/
 */
55 56
class SubscribeAction extends Action
{
57 58
    var $user;
    var $other;
59

60 61 62 63 64 65 66 67 68 69
    /**
     * Check pre-requisites and instantiate attributes
     *
     * @param Array $args array of arguments (URL, GET, POST)
     *
     * @return boolean success flag
     */
    function prepare($args)
    {
        parent::prepare($args);
Evan Prodromou's avatar
Evan Prodromou committed
70

71
        // Only allow POST requests
72

73
        if ($_SERVER['REQUEST_METHOD'] != 'POST') {
74 75
            // TRANS: Client error displayed trying to perform any request method other than POST.
            // TRANS: Do not translate POST.
76
            $this->clientError(_('This action only accepts POST requests.'));
77
        }
78

79
        // CSRF protection
80

81
        $token = $this->trimmed('token');
82

83
        if (!$token || $token != common_session_token()) {
84
            // TRANS: Client error displayed when the session token is not okay.
85 86 87 88 89 90 91 92 93
            $this->clientError(_('There was a problem with your session token.'.
                                 ' Try again, please.'));
        }

        // Only for logged-in users

        $this->user = common_current_user();

        if (empty($this->user)) {
94
            // TRANS: Error message displayed when trying to perform an action that requires a logged in user.
95
            $this->clientError(_('Not logged in.'));
96
        }
97

98 99
        // Profile to subscribe to

100
        $other_id = $this->arg('subscribeto');
101

102
        $this->other = Profile::getKV('id', $other_id);
103

104
        if (empty($this->other)) {
105
            // TRANS: Client error displayed trying to subscribe to a non-existing profile.
106
            $this->clientError(_('No such profile.'));
107 108
        }

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
        return true;
    }

    /**
     * Handle request
     *
     * Does the subscription and returns results.
     *
     * @param Array $args unused.
     *
     * @return void
     */
    function handle($args)
    {
        // Throws exception on error

Brion Vibber's avatar
Brion Vibber committed
125 126
        $sub = Subscription::start($this->user->getProfile(),
                                   $this->other);
127

128
        if ($this->boolean('ajax')) {
129
            $this->startHTML('text/xml;charset=utf-8');
130
            $this->elementStart('head');
131
            // TRANS: Page title when subscription succeeded.
132 133 134
            $this->element('title', null, _('Subscribed'));
            $this->elementEnd('head');
            $this->elementStart('body');
Brion Vibber's avatar
Brion Vibber committed
135 136 137 138 139 140
            if ($sub instanceof Subscription) {
                $form = new UnsubscribeForm($this, $this->other);
            } else {
                $form = new CancelSubscriptionForm($this, $this->other);
            }
            $form->show();
141
            $this->elementEnd('body');
142
            $this->endHTML();
143
        } else {
144 145 146
            $url = common_local_url('subscriptions',
                                    array('nickname' => $this->user->nickname));
            common_redirect($url, 303);
millette's avatar
millette committed
147
        }
148
    }
millette's avatar
millette committed
149
}