finishremotesubscribe.php 6.21 KB
Newer Older
1
<?php
2 3 4 5 6 7
/**
 * Handler for remote subscription finish callback
 *
 * PHP version 5
 *
 * @category Action
8 9 10
 * @package  StatusNet
 * @author   Evan Prodromou <evan@status.net>
 * @author   Robin Millette <millette@status.net>
11
 * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
12
 * @link     http://status.net/
13
 *
14
 * StatusNet - the distributed open-source microblogging tool
15
 * Copyright (C) 2008-2011, StatusNet, Inc.
16 17 18 19 20 21 22 23 24 25 26 27 28
 *
 * 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/>.
29
 **/
30

31
if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
32

33 34
require_once dirname(__FILE__) . '/../extlib/libomb/service_consumer.php';
require_once dirname(__FILE__) . '/../lib/omb.php';
35

36 37 38 39
/**
 * Handler for remote subscription finish callback
 *
 * When a remote user subscribes a local user, a redirect to this action is
40
 * issued after the remote user authorized their service to subscribe.
41 42 43
 *
 * @category Action
 * @package  Laconica
44
 * @author   Evan Prodromou <evan@status.net>
45 46 47 48
 * @author   Robin Millette <millette@controlyourself.ca>
 * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
 * @link     http://laconi.ca/
 */
49 50
class FinishremotesubscribeAction extends Action
{
51 52 53 54 55 56 57
    /**
     * Class handler.
     *
     * @param array $args query arguments
     *
     * @return nothing
     *
58
     */
59 60
    function handle($args)
    {
61
        parent::handle($args);
62

63 64 65
        /* Restore session data. RemotesubscribeAction should have stored
           this entry. */
        $service  = unserialize($_SESSION['oauth_authorization_request']);
66

67
        if (!$service) {
68
            // TRANS: Client error displayed when subscribing to a remote profile and an unexpected response is received.
69
            $this->clientError(_('Not expecting this response!'));
70 71 72
            return;
        }

73
        common_debug('stored request: '. print_r($service, true), __FILE__);
74

75 76
        /* Create user objects for both users. Do it early for request
           validation. */
77
        $user = User::staticGet('uri', $service->getListeneeURI());
78 79

        if (!$user) {
80
            // TRANS: Client error displayed when subscribing to a remote profile that does not exist.
81
            $this->clientError(_('User being listened to does not exist.'));
82 83 84
            return;
        }

85
        $other = User::staticGet('uri', $service->getListenerURI());
86 87

        if ($other) {
88
            // TRANS: Client error displayed when subscribing to a remote profile that is a local profile.
89
            $this->clientError(_('You can use the local subscription!'));
90 91 92
            return;
        }

93
        $remote = Remote_profile::staticGet('uri', $service->getListenerURI());
94 95 96
        if ($remote) {
            // Note remote profile may not have been saved yet.
            // @fixme not convinced this is correct at all!
97

98
            $profile = Profile::staticGet($remote->id);
99

100
            if ($user->hasBlocked($profile)) {
101
                // TRANS: Client error displayed when subscribing to a remote profile that is blocked form subscribing to.
102 103 104
                $this->clientError(_('That user has blocked you from subscribing.'));
                return;
            }
105 106
        }

107 108
        /* Perform the handling itself via libomb. */
        try {
109
            $service->finishAuthorization();
110 111 112
        } catch (OAuthException $e) {
            if ($e->getMessage() == 'The authorized token does not equal the ' .
                                    'submitted token.') {
113
                // TRANS: Client error displayed when subscribing to a remote profile without providing an authorised token.
114
                $this->clientError(_('You are not authorized.'));
115
                return;
116
            } else {
117
                // TRANS: Client error displayed when subscribing to a remote profile and conversion of the request token to access token fails.
118
                $this->clientError(_('Could not convert request token to ' .
119
                                     'access token.'));
120 121
                return;
            }
122
        } catch (OMB_RemoteServiceException $e) {
123
            // TRANS: Client error displayed when subscribing to a remote profile fails because of an unsupported version of the OMB protocol.
124 125
            $this->clientError(_('Remote service uses unknown version of ' .
                                 'OMB protocol.'));
126 127 128 129
            return;
        } catch (Exception $e) {
            common_debug('Got exception ' . print_r($e, true), __FILE__);
            $this->clientError($e->getMessage());
130 131 132
            return;
        }

133 134
        /* The service URLs are not accessible from datastore, so setting them
           after insertion of the profile. */
135
        $remote = Remote_profile::staticGet('uri', $service->getListenerURI());
136
        $orig_remote = clone($remote);
137

138 139 140 141
        $remote->postnoticeurl    =
                            $service->getServiceURI(OMB_ENDPOINT_POSTNOTICE);
        $remote->updateprofileurl =
                            $service->getServiceURI(OMB_ENDPOINT_UPDATEPROFILE);
142

143
        if (!$remote->update($orig_remote)) {
144
                // TRANS: Server error displayed when subscribing to a remote profile fails because the remote profile could not be updated.
145
                $this->serverError(_('Error updating remote profile.'));
146
                return;
147
        }
148

149
        /* Clear the session data. */
150
        unset($_SESSION['oauth_authorization_request']);
151

152 153
        /* If we show subscriptions in reverse chronological order, the new one
           should show up close to the top of the page. */
154
        common_redirect(common_local_url('subscribers', array('nickname' =>
155 156
                                                             $user->nickname)),
                        303);
157
    }
158
}