]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/finishremotesubscribe.php
44abbfceb793c31d9a74119cce11987643246948
[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  StatusNet
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/
13  *
14  * StatusNet - the distributed open-source microblogging tool
15  * Copyright (C) 2008, 2009, StatusNet, 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('STATUSNET') && !defined('LACONICA')) { exit(1); }
32
33 require_once INSTALLDIR.'/extlib/libomb/service_consumer.php';
34 require_once INSTALLDIR.'/lib/omb.php';
35
36 /**
37  * Handler for remote subscription finish callback
38  *
39  * When a remote user subscribes a local user, a redirect to this action is
40  * issued after the remote user authorized his service to subscribe.
41  *
42  * @category Action
43  * @package  Laconica
44  * @author   Evan Prodromou <evan@controlyourself.ca>
45  * @author   Robin Millette <millette@controlyourself.ca>
46  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
47  * @link     http://laconi.ca/
48  */
49 class FinishremotesubscribeAction extends Action
50 {
51
52     /**
53      * Class handler.
54      *
55      * @param array $args query arguments
56      *
57      * @return nothing
58      *
59      **/
60     function handle($args)
61     {
62         parent::handle($args);
63
64         /* Restore session data. RemotesubscribeAction should have stored
65            this entry. */
66         $service  = unserialize($_SESSION['oauth_authorization_request']);
67
68         if (!$service) {
69             $this->clientError(_('Not expecting this response!'));
70             return;
71         }
72
73         common_debug('stored request: '. print_r($service, true), __FILE__);
74
75         /* Create user objects for both users. Do it early for request
76            validation. */
77         $user = User::staticGet('uri', $service->getListeneeURI());
78
79         if (!$user) {
80             $this->clientError(_('User being listened to does not exist.'));
81             return;
82         }
83
84         $other = User::staticGet('uri', $service->getListenerURI());
85
86         if ($other) {
87             $this->clientError(_('You can use the local subscription!'));
88             return;
89         }
90
91         $remote = Remote_profile::staticGet('uri', $service->getListenerURI());
92
93         $profile = Profile::staticGet($remote->id);
94
95         if ($user->hasBlocked($profile)) {
96             $this->clientError(_('That user has blocked you from subscribing.'));
97             return;
98         }
99
100         /* Perform the handling itself via libomb. */
101         try {
102             $service->finishAuthorization();
103         } catch (OAuthException $e) {
104             if ($e->getMessage() == 'The authorized token does not equal the ' .
105                                     'submitted token.') {
106                 $this->clientError(_('You are not authorized.'));
107                 return;
108             } else {
109                 $this->clientError(_('Could not convert request token to ' .
110                                      'access token.'));
111                 return;
112             }
113         } catch (OMB_RemoteServiceException $e) {
114             $this->clientError(_('Remote service uses unknown version of ' .
115                                  'OMB protocol.'));
116             return;
117         } catch (Exception $e) {
118             common_debug('Got exception ' . print_r($e, true), __FILE__);
119             $this->clientError($e->getMessage());
120             return;
121         }
122
123         /* The service URLs are not accessible from datastore, so setting them
124            after insertion of the profile. */
125         $orig_remote = clone($remote);
126
127         $remote->postnoticeurl    =
128                             $service->getServiceURI(OMB_ENDPOINT_POSTNOTICE);
129         $remote->updateprofileurl =
130                             $service->getServiceURI(OMB_ENDPOINT_UPDATEPROFILE);
131
132         if (!$remote->update($orig_remote)) {
133                 $this->serverError(_('Error updating remote profile'));
134                 return;
135         }
136
137         /* Clear the session data. */
138         unset($_SESSION['oauth_authorization_request']);
139
140         /* If we show subscriptions in reverse chronological order, the new one
141            should show up close to the top of the page. */
142         common_redirect(common_local_url('subscribers', array('nickname' =>
143                                                              $user->nickname)),
144                         303);
145     }
146
147     function add_avatar($profile, $url)
148     {
149         $temp_filename = tempnam(sys_get_temp_dir(), 'listener_avatar');
150         copy($url, $temp_filename);
151         $imagefile = new ImageFile($profile->id, $temp_filename);
152         $filename = Avatar::filename($profile->id,
153                                      image_type_to_extension($imagefile->type),
154                                      null,
155                                      common_timestamp());
156         rename($temp_filename, Avatar::path($filename));
157         return $profile->setOriginal($filename);
158     }
159
160     function access_token($omb)
161     {
162
163         common_debug('starting request for access token', __FILE__);
164
165         $con = omb_oauth_consumer();
166         $tok = new OAuthToken($omb['token'], $omb['secret']);
167
168         common_debug('using request token "'.$tok.'"', __FILE__);
169
170         $url = $omb['access_token_url'];
171
172         common_debug('using access token url "'.$url.'"', __FILE__);
173
174         # XXX: Is this the right thing to do? Strip off GET params and make them
175         # POST params? Seems wrong to me.
176
177         $parsed = parse_url($url);
178         $params = array();
179         parse_str($parsed['query'], $params);
180
181         $req = OAuthRequest::from_consumer_and_token($con, $tok, "POST", $url, $params);
182
183         $req->set_parameter('omb_version', OMB_VERSION_01);
184
185         # XXX: test to see if endpoint accepts this signature method
186
187         $req->sign_request(omb_hmac_sha1(), $con, $tok);
188
189         # We re-use this tool's fetcher, since it's pretty good
190
191         common_debug('posting to access token url "'.$req->get_normalized_http_url().'"', __FILE__);
192         common_debug('posting request data "'.$req->to_postdata().'"', __FILE__);
193
194         $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
195         $result = $fetcher->post($req->get_normalized_http_url(),
196                                  $req->to_postdata(),
197                                  array('User-Agent: StatusNet/' . STATUSNET_VERSION));
198
199         common_debug('got result: "'.print_r($result,true).'"', __FILE__);
200
201         if ($result->status != 200) {
202             return null;
203         }
204
205         parse_str($result->body, $return);
206
207         return array($return['oauth_token'], $return['oauth_token_secret']);
208     }
209 }