]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/remotesubscribe.php
582e1d080da2e0dc0d707c0ad2b7324daa845905
[quix0rs-gnu-social.git] / actions / remotesubscribe.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 require_once(INSTALLDIR.'/lib/omb.php');
23 require_once('Auth/Yadis/Yadis.php');
24
25 class RemotesubscribeAction extends Action {
26
27         function handle($args) {
28
29                 parent::handle($args);
30
31                 if (common_logged_in()) {
32                         common_user_error(_t('You can use the local subscription!'));
33                     return;
34                 }
35
36                 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
37                         $this->remote_subscription();
38                 } else {
39                         $this->show_form();
40                 }
41         }
42
43         function show_form($err=NULL) {
44                 $nickname = $this->trimmed('nickname');
45                 common_show_header(_t('Remote subscribe'));
46                 if ($err) {
47                         common_element('div', 'error', $err);
48                 }
49                 common_element_start('form', array('id' => 'remotesubscribe', 'method' => 'POST',
50                                                                                    'action' => common_local_url('remotesubscribe')));
51                 common_input('nickname', _t('User nickname'), $nickname);
52                 common_input('profile', _t('Profile URL'));
53                 common_submit('submit', _t('Subscribe'));
54                 common_element_end('form');
55                 common_show_footer();
56         }
57
58         function remote_subscription() {
59                 $user = $this->get_user();
60
61                 if (!$user) {
62                         $this->show_form(_t('No such user!'));
63                         return;
64                 }
65
66                 $profile = $this->trimmed('profile');
67
68                 if (!$profile) {
69                         $this->show_form(_t('No such user!'));
70                         return;
71                 }
72
73                 if (!Validate::uri($profile, array('allowed_schemes' => array('http', 'https')))) {
74                         $this->show_form(_t('Invalid profile URL (bad format)'));
75                         return;
76                 }
77
78                 $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
79                 $yadis = Auth_Yadis_Yadis::discover($profile, $fetcher);
80
81                 common_debug('remotesubscribe.php: XRDS discovery failure? "'.$yadis->failed.'"');
82
83                 if (!$yadis || $yadis->failed) {
84                         $this->show_form(_t('Not a valid profile URL (no YADIS document).'));
85                         return;
86                 }
87
88         $xrds =& Auth_Yadis_XRDS::parseXRDS($yadis->response_text);
89
90                 if (!$xrds) {
91                         $this->show_form(_t('Not a valid profile URL (no XRDS defined).'));
92                         return;
93                 }
94
95                 common_debug('remotesubscribe.php: XRDS is "'.print_r($xrds,TRUE).'"');
96
97                 $omb = $this->getOmb($xrds);
98
99                 if (!$omb) {
100                         $this->show_form(_t('Not a valid profile URL (incorrect services).'));
101                         return;
102                 }
103
104                 list($token, $secret) = $this->request_token($omb);
105
106                 if (!$token || !$secret) {
107                         $this->show_form(_t('Couldn\'t get a request token.'));
108                         return;
109                 }
110
111                 $this->request_authorization($user, $omb, $token, $secret);
112         }
113
114         function get_user() {
115                 $user = NULL;
116                 $nickname = $this->trimmed('nickname');
117                 if ($nickname) {
118                         $user = User::staticGet('nickname', $nickname);
119                 }
120                 return $user;
121         }
122
123         function getOmb($xrds) {
124
125             static $omb_endpoints = array(OMB_ENDPOINT_UPDATEPROFILE, OMB_ENDPOINT_POSTNOTICE);
126                 static $oauth_endpoints = array(OAUTH_ENDPOINT_REQUEST, OAUTH_ENDPOINT_AUTHORIZE,
127                                                                                 OAUTH_ENDPOINT_ACCESS);
128                 $omb = array();
129
130                 # XXX: the following code could probably be refactored to eliminate dupes
131
132                 common_debug('remotesubscribe.php - looking for oauth discovery service');
133
134                 $oauth_services = $xrds->services(omb_service_filter(OAUTH_DISCOVERY));
135
136                 if (!$oauth_services) {
137                         common_debug('remotesubscribe.php - failed to find oauth discovery service');
138                         return NULL;
139                 }
140
141                 $oauth_service = $oauth_services[0];
142
143                 common_debug('remotesubscribe.php - looking for oauth discovery XRD');
144
145                 $oauth_xrd = $this->getXRD($oauth_service, $xrds);
146
147                 if (!$oauth_xrd) {
148                         common_debug('remotesubscribe.php - failed to find oauth discovery XRD');
149                         return NULL;
150                 }
151
152                 common_debug('remotesubscribe.php - adding OAuth services from XRD');
153
154                 if (!$this->addServices($oauth_xrd, $oauth_endpoints, $omb)) {
155                         common_debug('remotesubscribe.php - failed to add OAuth services');
156                         return NULL;
157                 }
158
159                 common_debug('remotesubscribe.php - looking for OMB discovery service');
160
161                 $omb_services = $xrds->services(omb_service_filter(OMB_NAMESPACE));
162
163                 if (!$omb_services) {
164                         common_debug('remotesubscribe.php - failed to find OMB discovery service');
165                         return NULL;
166                 }
167
168                 $omb_service = $omb_services[0];
169
170                 common_debug('remotesubscribe.php - looking for OMB discovery XRD');
171
172                 $omb_xrd = $this->getXRD($omb_service, $xrds);
173
174                 if (!$omb_xrd) {
175                         common_debug('remotesubscribe.php - failed to find OMB discovery XRD');
176                         return NULL;
177                 }
178
179                 common_debug('remotesubscribe.php - adding OMB services from XRD');
180
181                 if (!$this->addServices($omb_xrd, $omb_endpoints, $omb)) {
182                         common_debug('remotesubscribe.php - failed to add OMB services');
183                         return NULL;
184                 }
185
186                 # XXX: check that we got all the services we needed
187
188                 foreach (array_merge($omb_endpoints, $oauth_endpoints) as $type) {
189                         if (!array_key_exists($type, $omb) || !$omb[$type]) {
190                                 common_debug('remotesubscribe.php - could not find type "'.$type.'"');
191                                 return NULL;
192                         }
193                         common_debug('remotesubscribe.php - key ="'.$type.'" and URI ="'.omb_service_uri($omb[$type]).'"');
194                 }
195
196                 if (!omb_local_id($omb[OAUTH_ENDPOINT_REQUEST])) {
197                         common_debug('remotesubscribe.php - request token service has no LocalID.');
198                         return NULL;
199                 }
200
201                 return $omb;
202         }
203
204         function getXRD($main_service, $main_xrds) {
205                 $uri = omb_service_uri($main_service);
206                 if (strpos($uri, "#") !== 0) {
207                         # FIXME: more rigorous handling of external service definitions
208                         return NULL;
209                 }
210                 $id = substr($uri, 1);
211                 $nodes = $main_xrds->allXrdNodes;
212                 $parser = $main_xrds->parser;
213                 foreach ($nodes as $node) {
214                         $attrs = $parser->attributes($node);
215                         if (array_key_exists('xml:id', $attrs) &&
216                                 $attrs['xml:id'] == $id) {
217                                 # XXX: trick the constructor into thinking this is the only node
218                                 $bogus_nodes = array($node);
219                                 return new Auth_Yadis_XRDS($parser, $bogus_nodes);
220                         }
221                 }
222                 return NULL;
223         }
224
225         function addServices($xrd, $types, &$omb) {
226                 foreach ($types as $type) {
227                         $matches = $xrd->services(omb_service_filter($type));
228                         common_debug('remotesubscribe.php - ' . count($matches) . ' matches for "'.$type.'"');
229                         if ($matches) {
230                                 foreach ($matches as $match) {
231                                         common_debug('remotesubscribe.php - "' . get_service_uri($match) . '" matches "'.$type.'"');
232                                 }
233                                 $omb[$type] = $matches[0];
234                         } else {
235                                 # no match for type
236                                 return false;
237                         }
238                 }
239                 return true;
240         }
241
242         function request_token($omb) {
243                 $con = omb_oauth_consumer();
244
245                 $url = omb_service_uri($omb[OAUTH_ENDPOINT_REQUEST]);
246
247                 # XXX: Is this the right thing to do? Strip off GET params and make them
248                 # POST params? Seems wrong to me.
249
250                 $parsed = parse_url($url);
251                 $params = array();
252                 parse_str($parsed['query'], $params);
253
254                 common_debug('remotesubscribe.php - building a POST message for request token call');
255                 
256                 $req = OAuthRequest::from_consumer_and_token($con, NULL, "POST", $url, $params);
257
258                 $listener = omb_local_id($omb[OAUTH_ENDPOINT_REQUEST]);
259
260                 if (!$listener) {
261                         return NULL;
262                 }
263
264                 common_debug('remotesubscribe.php - request token listener = "' . $listener . '"');
265                 
266                 $req->set_parameter('omb_listener', $listener);
267                 $req->set_parameter('omb_version', OMB_VERSION_01);
268
269                 # XXX: test to see if endpoint accepts this signature method
270
271                 $req->sign_request(omb_hmac_sha1(), $con, NULL);
272
273                 # We re-use this tool's fetcher, since it's pretty good
274
275                 $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
276                 
277                 common_debug('remotesubscribe.php - request token URL = "'.$req->get_normalized_http_url().'"');
278                 common_debug('remotesubscribe.php - request token data = "'.$req->to_postdata().'"');
279                 
280                 $result = $fetcher->post($req->get_normalized_http_url(),
281                                                                  $req->to_postdata());
282
283                 if ($result->status != 200) {
284                         common_debug('remotesubscribe.php - request token status = "' . $result->status . '"');
285                         common_debug('remotesubscribe.php - request token body = "' . $result->body . '"');
286                         return NULL;
287                 }
288
289                 common_debug('remotesubscribe.php - request token body = "' . $result->body . '"');
290                 
291                 parse_str($result->body, $return);
292
293                 common_debug('remotesubscribe.php - request token token = "' . $return['oauth_token'] . '"');
294                 common_debug('remotesubscribe.php - request token secret = "' . $return['oauth_token_secret'] . '"');
295                 
296                 return array($return['oauth_token'], $return['oauth_token_secret']);
297         }
298
299         function request_authorization($user, $omb, $token, $secret) {
300                 global $config; # for license URL
301
302                 $con = omb_oauth_consumer();
303                 $tok = new OAuthToken($token, $secret);
304
305                 $url = omb_service_uri($omb[OAUTH_ENDPOINT_AUTHORIZE]);
306
307                 common_debug('remotesubscribe.php - user authorization URI = "' . $url . '"');
308                 
309                 # XXX: Is this the right thing to do? Strip off GET params and make them
310                 # POST params? Seems wrong to me.
311
312                 $parsed = parse_url($url);
313                 $params = array();
314                 parse_str($parsed['query'], $params);
315
316                 $req = OAuthRequest::from_consumer_and_token($con, $tok, 'GET', $url, $params);
317
318                 # We send over a ton of information. This lets the other
319                 # server store info about our user, and it lets the current
320                 # user decide if they really want to authorize the subscription.
321
322                 $req->set_parameter('omb_version', OMB_VERSION_01);
323                 $req->set_parameter('omb_listener', omb_local_id($omb[OAUTH_ENDPOINT_REQUEST]));
324                 $req->set_parameter('omb_listenee', $user->uri);
325                 $req->set_parameter('omb_listenee_profile', common_profile_url($user->nickname));
326                 $req->set_parameter('omb_listenee_nickname', $user->nickname);
327                 $req->set_parameter('omb_listenee_license', $config['license']['url']);
328                 $profile = $user->getProfile();
329                 if ($profile->fullname) {
330                         $req->set_parameter('omb_listenee_fullname', $profile->fullname);
331                 }
332                 if ($profile->homepage) {
333                         $req->set_parameter('omb_listenee_homepage', $profile->homepage);
334                 }
335                 if ($profile->bio) {
336                         $req->set_parameter('omb_listenee_bio', $profile->bio);
337                 }
338                 if ($profile->location) {
339                         $req->set_parameter('omb_listenee_location', $profile->location);
340                 }
341                 $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
342                 if ($avatar) {
343                         $req->set_parameter('omb_listenee_avatar', $avatar->url);
344                 }
345
346                 # XXX: add a nonce to prevent replay attacks
347                 
348                 $req->set_parameter('oauth_callback', common_local_url('finishremotesubscribe'));
349
350                 # XXX: test to see if endpoint accepts this signature method
351
352                 $req->sign_request(omb_hmac_sha1(), $con, $tok);
353
354                 # store all our info here
355
356                 $omb['listenee'] = $user->nickname;
357                 $omb['token'] = $token;
358                 $omb['secret'] = $secret;
359
360                 $_SESSION['oauth_authorization_request'] = $omb;
361
362                 # Redirect to authorization service
363
364                 common_redirect($req->to_url());
365                 return;
366         }
367         
368         function make_nonce() {
369                 return common_good_rand(16);
370         }
371 }