3 * Handler for remote subscription finish callback
9 * @author Evan Prodromou <evan@status.net>
10 * @author Robin Millette <millette@status.net>
11 * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
12 * @link http://status.net/
14 * StatusNet - the distributed open-source microblogging tool
15 * Copyright (C) 2008, 2009, StatusNet, Inc.
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.
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.
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/>.
31 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
33 require_once INSTALLDIR.'/extlib/libomb/service_consumer.php';
34 require_once INSTALLDIR.'/lib/omb.php';
37 * Handler for remote subscription finish callback
39 * 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.
44 * @author Evan Prodromou <evan@status.net>
45 * @author Robin Millette <millette@controlyourself.ca>
46 * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
47 * @link http://laconi.ca/
49 class FinishremotesubscribeAction extends Action
54 * @param array $args query arguments
59 function handle($args)
61 parent::handle($args);
63 /* Restore session data. RemotesubscribeAction should have stored
65 $service = unserialize($_SESSION['oauth_authorization_request']);
68 // TRANS: Client error displayed when subscribing to a remote profile and an unexpected response is received.
69 $this->clientError(_('Not expecting this response!'));
73 common_debug('stored request: '. print_r($service, true), __FILE__);
75 /* Create user objects for both users. Do it early for request
77 $user = User::staticGet('uri', $service->getListeneeURI());
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.'));
85 $other = User::staticGet('uri', $service->getListenerURI());
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!'));
93 $remote = Remote_profile::staticGet('uri', $service->getListenerURI());
95 // Note remote profile may not have been saved yet.
96 // @fixme not convinced this is correct at all!
98 $profile = Profile::staticGet($remote->id);
100 if ($user->hasBlocked($profile)) {
101 // TRANS: Client error displayed when subscribing to a remote profile that is blocked form subscribing to.
102 $this->clientError(_('That user has blocked you from subscribing.'));
107 /* Perform the handling itself via libomb. */
109 $service->finishAuthorization();
110 } catch (OAuthException $e) {
111 if ($e->getMessage() == 'The authorized token does not equal the ' .
112 '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.'));
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 ' .
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 $this->clientError(_('Remote service uses unknown version of ' .
127 } catch (Exception $e) {
128 common_debug('Got exception ' . print_r($e, true), __FILE__);
129 $this->clientError($e->getMessage());
133 /* The service URLs are not accessible from datastore, so setting them
134 after insertion of the profile. */
135 $remote = Remote_profile::staticGet('uri', $service->getListenerURI());
136 $orig_remote = clone($remote);
138 $remote->postnoticeurl =
139 $service->getServiceURI(OMB_ENDPOINT_POSTNOTICE);
140 $remote->updateprofileurl =
141 $service->getServiceURI(OMB_ENDPOINT_UPDATEPROFILE);
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.'));
149 /* Clear the session data. */
150 unset($_SESSION['oauth_authorization_request']);
152 /* If we show subscriptions in reverse chronological order, the new one
153 should show up close to the top of the page. */
154 common_redirect(common_local_url('subscribers', array('nickname' =>