]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/remotesubscribe.php
be a little more liberal for sites that accidentally put whitespace before the xml...
[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
24 class RemotesubscribeAction extends Action {
25
26         function handle($args) {
27
28                 parent::handle($args);
29
30                 if (common_logged_in()) {
31                         common_user_error(_('You can use the local subscription!'));
32                     return;
33                 }
34
35                 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
36                         $this->remote_subscription();
37                 } else {
38                         $this->show_form();
39                 }
40         }
41
42         function get_instructions() {
43                 return _('To subscribe, you can [login](%%action.login%%),' .
44                           ' or [register](%%action.register%%) a new ' .
45                           ' account. If you already have an account ' .
46                           ' on a [compatible microblogging site](%%doc.openmublog%%), ' .
47                           ' enter your profile URL below.');
48         }
49
50         function show_top($err=NULL) {
51                 if ($err) {
52                         common_element('div', 'error', $err);
53                 } else {
54                         $instructions = $this->get_instructions();
55                         $output = common_markup_to_html($instructions);
56                         common_element_start('div', 'instructions');
57                         common_raw($output);
58                         common_element_end('p');
59                 }
60         }
61
62         function show_form($err=NULL) {
63                 $nickname = $this->trimmed('nickname');
64                 $profile = $this->trimmed('profile_url');
65                 common_show_header(_('Remote subscribe'), NULL, $err,
66                                                    array($this, 'show_top'));
67                 # id = remotesubscribe conflicts with the
68                 # button on profile page
69                 common_element_start('form', array('id' => 'remsub', 'method' => 'post',
70                                                                                    'action' => common_local_url('remotesubscribe')));
71                 common_input('nickname', _('User nickname'), $nickname,
72                                          _('Nickname of the user you want to follow'));
73                 common_input('profile_url', _('Profile URL'), $profile,
74                                          _('URL of your profile on another compatible microblogging service'));
75                 common_submit('submit', _('Subscribe'));
76                 common_element_end('form');
77                 common_show_footer();
78         }
79
80         function remote_subscription() {
81                 $user = $this->get_user();
82
83                 if (!$user) {
84                         $this->show_form(_('No such user.'));
85                         return;
86                 }
87
88                 $profile = $this->trimmed('profile_url');
89
90                 if (!$profile) {
91                         $this->show_form(_('No such user.'));
92                         return;
93                 }
94
95                 if (!Validate::uri($profile, array('allowed_schemes' => array('http', 'https')))) {
96                         $this->show_form(_('Invalid profile URL (bad format)'));
97                         return;
98                 }
99
100                 $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
101                 $yadis = Auth_Yadis_Yadis::discover($profile, $fetcher);
102
103                 if (!$yadis || $yadis->failed) {
104                         $this->show_form(_('Not a valid profile URL (no YADIS document).'));
105                         return;
106                 }
107
108                 # XXX: a little liberal for sites that accidentally put whitespace before the xml declaration
109                 
110         $xrds =& Auth_Yadis_XRDS::parseXRDS(trim($yadis->response_text));
111
112                 if (!$xrds) {
113                         $this->show_form(_('Not a valid profile URL (no XRDS defined).'));
114                         return;
115                 }
116
117                 $omb = $this->getOmb($xrds);
118
119                 if (!$omb) {
120                         $this->show_form(_('Not a valid profile URL (incorrect services).'));
121                         return;
122                 }
123
124                 list($token, $secret) = $this->request_token($omb);
125
126                 if (!$token || !$secret) {
127                         $this->show_form(_('Couldn\'t get a request token.'));
128                         return;
129                 }
130
131                 $this->request_authorization($user, $omb, $token, $secret);
132         }
133
134         function get_user() {
135                 $user = NULL;
136                 $nickname = $this->trimmed('nickname');
137                 if ($nickname) {
138                         $user = User::staticGet('nickname', $nickname);
139                 }
140                 return $user;
141         }
142
143         function getOmb($xrds) {
144
145             static $omb_endpoints = array(OMB_ENDPOINT_UPDATEPROFILE, OMB_ENDPOINT_POSTNOTICE);
146                 static $oauth_endpoints = array(OAUTH_ENDPOINT_REQUEST, OAUTH_ENDPOINT_AUTHORIZE,
147                                                                                 OAUTH_ENDPOINT_ACCESS);
148                 $omb = array();
149
150                 # XXX: the following code could probably be refactored to eliminate dupes
151
152                 $oauth_services = omb_get_services($xrds, OAUTH_DISCOVERY);
153
154                 if (!$oauth_services) {
155                         return NULL;
156                 }
157
158                 $oauth_service = $oauth_services[0];
159
160                 $oauth_xrd = $this->getXRD($oauth_service, $xrds);
161
162                 if (!$oauth_xrd) {
163                         return NULL;
164                 }
165
166                 if (!$this->addServices($oauth_xrd, $oauth_endpoints, $omb)) {
167                         return NULL;
168                 }
169
170                 $omb_services = omb_get_services($xrds, OMB_NAMESPACE);
171
172                 if (!$omb_services) {
173                         return NULL;
174                 }
175
176                 $omb_service = $omb_services[0];
177
178                 $omb_xrd = $this->getXRD($omb_service, $xrds);
179
180                 if (!$omb_xrd) {
181                         return NULL;
182                 }
183
184                 if (!$this->addServices($omb_xrd, $omb_endpoints, $omb)) {
185                         return NULL;
186                 }
187
188                 # XXX: check that we got all the services we needed
189
190                 foreach (array_merge($omb_endpoints, $oauth_endpoints) as $type) {
191                         if (!array_key_exists($type, $omb) || !$omb[$type]) {
192                                 return NULL;
193                         }
194                 }
195
196                 if (!omb_local_id($omb[OAUTH_ENDPOINT_REQUEST])) {
197                         return NULL;
198                 }
199
200                 return $omb;
201         }
202
203         function getXRD($main_service, $main_xrds) {
204                 $uri = omb_service_uri($main_service);
205                 if (strpos($uri, "#") !== 0) {
206                         # FIXME: more rigorous handling of external service definitions
207                         return NULL;
208                 }
209                 $id = substr($uri, 1);
210                 $nodes = $main_xrds->allXrdNodes;
211                 $parser = $main_xrds->parser;
212                 foreach ($nodes as $node) {
213                         $attrs = $parser->attributes($node);
214                         if (array_key_exists('xml:id', $attrs) &&
215                                 $attrs['xml:id'] == $id) {
216                                 # XXX: trick the constructor into thinking this is the only node
217                                 $bogus_nodes = array($node);
218                                 return new Auth_Yadis_XRDS($parser, $bogus_nodes);
219                         }
220                 }
221                 return NULL;
222         }
223
224         function addServices($xrd, $types, &$omb) {
225                 foreach ($types as $type) {
226                         $matches = omb_get_services($xrd, $type);
227                         if ($matches) {
228                                 $omb[$type] = $matches[0];
229                         } else {
230                                 # no match for type
231                                 return false;
232                         }
233                 }
234                 return true;
235         }
236
237         function request_token($omb) {
238                 $con = omb_oauth_consumer();
239
240                 $url = omb_service_uri($omb[OAUTH_ENDPOINT_REQUEST]);
241
242                 # XXX: Is this the right thing to do? Strip off GET params and make them
243                 # POST params? Seems wrong to me.
244
245                 $parsed = parse_url($url);
246                 $params = array();
247                 parse_str($parsed['query'], $params);
248
249                 $req = OAuthRequest::from_consumer_and_token($con, NULL, "POST", $url, $params);
250
251                 $listener = omb_local_id($omb[OAUTH_ENDPOINT_REQUEST]);
252
253                 if (!$listener) {
254                         return NULL;
255                 }
256
257                 $req->set_parameter('omb_listener', $listener);
258                 $req->set_parameter('omb_version', OMB_VERSION_01);
259
260                 # XXX: test to see if endpoint accepts this signature method
261
262                 $req->sign_request(omb_hmac_sha1(), $con, NULL);
263
264                 # We re-use this tool's fetcher, since it's pretty good
265
266                 $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
267
268                 $result = $fetcher->post($req->get_normalized_http_url(),
269                                                                  $req->to_postdata());
270
271                 if ($result->status != 200) {
272                         return NULL;
273                 }
274
275                 parse_str($result->body, $return);
276
277                 return array($return['oauth_token'], $return['oauth_token_secret']);
278         }
279
280         function request_authorization($user, $omb, $token, $secret) {
281                 global $config; # for license URL
282
283                 $con = omb_oauth_consumer();
284                 $tok = new OAuthToken($token, $secret);
285
286                 $url = omb_service_uri($omb[OAUTH_ENDPOINT_AUTHORIZE]);
287
288                 # XXX: Is this the right thing to do? Strip off GET params and make them
289                 # POST params? Seems wrong to me.
290
291                 $parsed = parse_url($url);
292                 $params = array();
293                 parse_str($parsed['query'], $params);
294
295                 $req = OAuthRequest::from_consumer_and_token($con, $tok, 'GET', $url, $params);
296
297                 # We send over a ton of information. This lets the other
298                 # server store info about our user, and it lets the current
299                 # user decide if they really want to authorize the subscription.
300
301                 $req->set_parameter('omb_version', OMB_VERSION_01);
302                 $req->set_parameter('omb_listener', omb_local_id($omb[OAUTH_ENDPOINT_REQUEST]));
303                 $req->set_parameter('omb_listenee', $user->uri);
304                 $req->set_parameter('omb_listenee_profile', common_profile_url($user->nickname));
305                 $req->set_parameter('omb_listenee_nickname', $user->nickname);
306                 $req->set_parameter('omb_listenee_license', $config['license']['url']);
307                 $profile = $user->getProfile();
308                 if ($profile->fullname) {
309                         $req->set_parameter('omb_listenee_fullname', $profile->fullname);
310                 }
311                 if ($profile->homepage) {
312                         $req->set_parameter('omb_listenee_homepage', $profile->homepage);
313                 }
314                 if ($profile->bio) {
315                         $req->set_parameter('omb_listenee_bio', $profile->bio);
316                 }
317                 if ($profile->location) {
318                         $req->set_parameter('omb_listenee_location', $profile->location);
319                 }
320                 $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
321                 if ($avatar) {
322                         $req->set_parameter('omb_listenee_avatar', $avatar->url);
323                 }
324
325                 # XXX: add a nonce to prevent replay attacks
326
327                 $req->set_parameter('oauth_callback', common_local_url('finishremotesubscribe'));
328
329                 # XXX: test to see if endpoint accepts this signature method
330
331                 $req->sign_request(omb_hmac_sha1(), $con, $tok);
332
333                 # store all our info here
334
335                 $omb['listenee'] = $user->nickname;
336                 $omb['listener'] = omb_local_id($omb[OAUTH_ENDPOINT_REQUEST]);
337                 $omb['token'] = $token;
338                 $omb['secret'] = $secret;
339                 # call doesn't work after bounce back so we cache; maybe serialization issue...?
340                 $omb['access_token_url'] = omb_service_uri($omb[OAUTH_ENDPOINT_ACCESS]);
341                 $omb['post_notice_url'] = omb_service_uri($omb[OMB_ENDPOINT_POSTNOTICE]);
342                 $omb['update_profile_url'] = omb_service_uri($omb[OMB_ENDPOINT_UPDATEPROFILE]);
343
344                 common_ensure_session();
345                 
346                 $_SESSION['oauth_authorization_request'] = $omb;
347
348                 # Redirect to authorization service
349
350                 common_redirect($req->to_url());
351                 return;
352         }
353
354         function make_nonce() {
355                 return common_good_rand(16);
356         }
357 }