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

subscribe.php 4.4 KB
Newer Older
Evan Prodromou's avatar
Evan Prodromou committed
1
<?php
2
/**
3
 * StatusNet - the distributed open-source microblogging tool
4 5 6
 * Copyright (C) 2008-2010, StatusNet, Inc.
 *
 * 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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
if (!defined('STATUSNET')) {
    exit(1);
}

/**
 * Subscription action
 *
 * Subscribing to a profile. Does not work for OMB 0.1 remote subscriptions,
 * but may work for other remote subscription protocols, like OStatus.
 *
 * 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/
 */
Evan Prodromou's avatar
Evan Prodromou committed
56

57 58
class SubscribeAction extends Action
{
59 60
    var $user;
    var $other;
61

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

70 71 72
    function prepare($args)
    {
        parent::prepare($args);
Evan Prodromou's avatar
Evan Prodromou committed
73

74
        // Only allow POST requests
75

76
        if ($_SERVER['REQUEST_METHOD'] != 'POST') {
77 78
            $this->clientError(_('This action only accepts POST requests.'));
            return false;
79
        }
80

81
        // CSRF protection
82

83
        $token = $this->trimmed('token');
84

85
        if (!$token || $token != common_session_token()) {
86 87 88 89 90 91 92 93 94 95 96 97
            $this->clientError(_('There was a problem with your session token.'.
                                 ' Try again, please.'));
            return false;
        }

        // Only for logged-in users

        $this->user = common_current_user();

        if (empty($this->user)) {
            $this->clientError(_('Not logged in.'));
            return false;
98
        }
99

100 101
        // Profile to subscribe to

102
        $other_id = $this->arg('subscribeto');
103

104
        $this->other = Profile::staticGet('id', $other_id);
105

106 107 108
        if (empty($this->other)) {
            $this->clientError(_('No such profile.'));
            return false;
109 110
        }

111 112 113 114
        // OMB 0.1 doesn't have a mechanism for local-server-
        // originated subscription.

        $omb01 = Remote_profile::staticGet('id', $other_id);
Evan Prodromou's avatar
Evan Prodromou committed
115

116 117 118 119
        if (!empty($omb01)) {
            $this->clientError(_('You cannot subscribe to an OMB 0.1'.
                                 ' remote profile with this action.'));
            return false;
120
        }
millette's avatar
millette committed
121

122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
        return true;
    }

    /**
     * Handle request
     *
     * Does the subscription and returns results.
     *
     * @param Array $args unused.
     *
     * @return void
     */

    function handle($args)
    {
        // Throws exception on error

        Subscription::start($this->user->getProfile(),
                            $this->other);

142
        if ($this->boolean('ajax')) {
143
            $this->startHTML('text/xml;charset=utf-8');
144 145 146 147
            $this->elementStart('head');
            $this->element('title', null, _('Subscribed'));
            $this->elementEnd('head');
            $this->elementStart('body');
148
            $unsubscribe = new UnsubscribeForm($this, $this->other->getProfile());
sarven's avatar
sarven committed
149
            $unsubscribe->show();
150 151
            $this->elementEnd('body');
            $this->elementEnd('html');
152
        } else {
153 154 155
            $url = common_local_url('subscriptions',
                                    array('nickname' => $this->user->nickname));
            common_redirect($url, 303);
millette's avatar
millette committed
156
        }
157
    }
millette's avatar
millette committed
158
}