]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/finishremotesubscribe.php
Merge branch '0.8.x' into 0.9.x
[quix0rs-gnu-social.git] / actions / finishremotesubscribe.php
1 <?php
2 /**
3  * Handler for remote subscription finish callback
4  *
5  * PHP version 5
6  *
7  * @category Action
8  * @package  Laconica
9  * @author   Evan Prodromou <evan@controlyourself.ca>
10  * @author   Robin Millette <millette@controlyourself.ca>
11  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
12  * @link     http://laconi.ca/
13  *
14  * Laconica - a distributed open-source microblogging tool
15  * Copyright (C) 2008, 2009, Control Yourself, Inc.
16  *
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.
21  *
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.
26  *
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/>.
29  **/
30
31 if (!defined('LACONICA')) {
32     exit(1);
33 }
34
35 require_once INSTALLDIR.'/extlib/libomb/service_consumer.php';
36 require_once INSTALLDIR.'/lib/omb.php';
37
38 /**
39  * Handler for remote subscription finish callback
40  *
41  * When a remote user subscribes a local user, a redirect to this action is
42  * issued after the remote user authorized his service to subscribe.
43  *
44  * @category Action
45  * @package  Laconica
46  * @author   Evan Prodromou <evan@controlyourself.ca>
47  * @author   Robin Millette <millette@controlyourself.ca>
48  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
49  * @link     http://laconi.ca/
50  */
51 class FinishremotesubscribeAction extends Action
52 {
53
54     /**
55      * Class handler.
56      *
57      * @param array $args query arguments
58      *
59      * @return nothing
60      *
61      **/
62     function handle($args)
63     {
64         parent::handle($args);
65
66         /* Restore session data. RemotesubscribeAction should have stored
67            this entry. */
68         $service  = unserialize($_SESSION['oauth_authorization_request']);
69
70         if (!$service) {
71             $this->clientError(_('Not expecting this response!'));
72             return;
73         }
74
75         common_debug('stored request: '. print_r($service, true), __FILE__);
76
77         /* Create user objects for both users. Do it early for request
78            validation. */
79         $user = User::staticGet('uri', $service->getListeneeURI());
80
81         if (!$user) {
82             $this->clientError(_('User being listened to does not exist.'));
83             return;
84         }
85
86         $other = User::staticGet('uri', $service->getListenerURI());
87
88         if ($other) {
89             $this->clientError(_('You can use the local subscription!'));
90             return;
91         }
92
93         $remote = Remote_profile::staticGet('uri', $service->getListenerURI());
94
95         $profile = Profile::staticGet($remote->id);
96
97         if ($user->hasBlocked($profile)) {
98             $this->clientError(_('That user has blocked you from subscribing.'));
99             return;
100         }
101
102         /* Perform the handling itself via libomb. */
103         try {
104             $service->finishAuthorization();
105         } catch (OAuthException $e) {
106             if ($e->getMessage() == 'The authorized token does not equal the ' .
107                                     'submitted token.') {
108                 $this->clientError(_('You are not authorized.'));
109                 return;
110             } else {
111                 $this->clientError(_('Could not convert request token to ' .
112                                      'access token.'));
113                 return;
114             }
115         } catch (OMB_RemoteServiceException $e) {
116             $this->clientError(_('Remote service uses unknown version of ' .
117                                  'OMB protocol.'));
118             return;
119         } catch (Exception $e) {
120             common_debug('Got exception ' . print_r($e, true), __FILE__);
121             $this->clientError($e->getMessage());
122             return;
123         }
124
125         /* The service URLs are not accessible from datastore, so setting them
126            after insertion of the profile. */
127         $orig_remote = clone($remote);
128
129         $remote->postnoticeurl    =
130                             $service->getServiceURI(OMB_ENDPOINT_POSTNOTICE);
131         $remote->updateprofileurl =
132                             $service->getServiceURI(OMB_ENDPOINT_UPDATEPROFILE);
133
134         if (!$remote->update($orig_remote)) {
135                 $this->serverError(_('Error updating remote profile'));
136                 return;
137         }
138
139         /* Clear the session data. */
140         unset($_SESSION['oauth_authorization_request']);
141
142         /* If we show subscriptions in reverse chronological order, the new one
143            should show up close to the top of the page. */
144         common_redirect(common_local_url('subscribers', array('nickname' =>
145                                                              $user->nickname)),
146                         303);
147     }
148 }