]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/remotesubscribe.php
d1baa4152f6c1a82db8c9ffbbd5c122473d0cea8
[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         $xrds =& Auth_Yadis_XRDS::parseXRDS($yadis->response_text);
109
110                 if (!$xrds) {
111                         $this->show_form(_('Not a valid profile URL (no XRDS defined).'));
112                         return;
113                 }
114
115                 $omb = $this->getOmb($xrds);
116
117                 if (!$omb) {
118                         $this->show_form(_('Not a valid profile URL (incorrect services).'));
119                         return;
120                 }
121
122                 list($token, $secret) = $this->request_token($omb);
123
124                 if (!$token || !$secret) {
125                         $this->show_form(_('Couldn\'t get a request token.'));
126                         return;
127                 }
128
129                 $this->request_authorization($user, $omb, $token, $secret);
130         }
131
132         function get_user() {
133                 $user = NULL;
134                 $nickname = $this->trimmed('nickname');
135                 if ($nickname) {
136                         $user = User::staticGet('nickname', $nickname);
137                 }
138                 return $user;
139         }
140
141         function getOmb($xrds) {
142
143             static $omb_endpoints = array(OMB_ENDPOINT_UPDATEPROFILE, OMB_ENDPOINT_POSTNOTICE);
144                 static $oauth_endpoints = array(OAUTH_ENDPOINT_REQUEST, OAUTH_ENDPOINT_AUTHORIZE,
145                                                                                 OAUTH_ENDPOINT_ACCESS);
146                 $omb = array();
147
148                 # XXX: the following code could probably be refactored to eliminate dupes
149
150                 $oauth_services = omb_get_services($xrds, OAUTH_DISCOVERY);
151
152                 if (!$oauth_services) {
153                         return NULL;
154                 }
155
156                 $oauth_service = $oauth_services[0];
157
158                 $oauth_xrd = $this->getXRD($oauth_service, $xrds);
159
160                 if (!$oauth_xrd) {
161                         return NULL;
162                 }
163
164                 if (!$this->addServices($oauth_xrd, $oauth_endpoints, $omb)) {
165                         return NULL;
166                 }
167
168                 $omb_services = omb_get_services($xrds, OMB_NAMESPACE);
169
170                 if (!$omb_services) {
171                         return NULL;
172                 }
173
174                 $omb_service = $omb_services[0];
175
176                 $omb_xrd = $this->getXRD($omb_service, $xrds);
177
178                 if (!$omb_xrd) {
179                         return NULL;
180                 }
181
182                 if (!$this->addServices($omb_xrd, $omb_endpoints, $omb)) {
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                                 return NULL;
191                         }
192                 }
193
194                 if (!omb_local_id($omb[OAUTH_ENDPOINT_REQUEST])) {
195                         return NULL;
196                 }
197
198                 return $omb;
199         }
200
201         function getXRD($main_service, $main_xrds) {
202                 $uri = omb_service_uri($main_service);
203                 if (strpos($uri, "#") !== 0) {
204                         # FIXME: more rigorous handling of external service definitions
205                         return NULL;
206                 }
207                 $id = substr($uri, 1);
208                 $nodes = $main_xrds->allXrdNodes;
209                 $parser = $main_xrds->parser;
210                 foreach ($nodes as $node) {
211                         $attrs = $parser->attributes($node);
212                         if (array_key_exists('xml:id', $attrs) &&
213                                 $attrs['xml:id'] == $id) {
214                                 # XXX: trick the constructor into thinking this is the only node
215                                 $bogus_nodes = array($node);
216                                 return new Auth_Yadis_XRDS($parser, $bogus_nodes);
217                         }
218                 }
219                 return NULL;
220         }
221
222         function addServices($xrd, $types, &$omb) {
223                 foreach ($types as $type) {
224                         $matches = omb_get_services($xrd, $type);
225                         if ($matches) {
226                                 $omb[$type] = $matches[0];
227                         } else {
228                                 # no match for type
229                                 return false;
230                         }
231                 }
232                 return true;
233         }
234
235         function request_token($omb) {
236                 $con = omb_oauth_consumer();
237
238                 $url = omb_service_uri($omb[OAUTH_ENDPOINT_REQUEST]);
239
240                 # XXX: Is this the right thing to do? Strip off GET params and make them
241                 # POST params? Seems wrong to me.
242
243                 $parsed = parse_url($url);
244                 $params = array();
245                 parse_str($parsed['query'], $params);
246
247                 $req = OAuthRequest::from_consumer_and_token($con, NULL, "POST", $url, $params);
248
249                 $listener = omb_local_id($omb[OAUTH_ENDPOINT_REQUEST]);
250
251                 if (!$listener) {
252                         return NULL;
253                 }
254
255                 $req->set_parameter('omb_listener', $listener);
256                 $req->set_parameter('omb_version', OMB_VERSION_01);
257
258                 # XXX: test to see if endpoint accepts this signature method
259
260                 $req->sign_request(omb_hmac_sha1(), $con, NULL);
261
262                 # We re-use this tool's fetcher, since it's pretty good
263
264                 $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
265
266                 $result = $fetcher->post($req->get_normalized_http_url(),
267                                                                  $req->to_postdata());
268
269                 if ($result->status != 200) {
270                         return NULL;
271                 }
272
273                 parse_str($result->body, $return);
274
275                 return array($return['oauth_token'], $return['oauth_token_secret']);
276         }
277
278         function request_authorization($user, $omb, $token, $secret) {
279                 global $config; # for license URL
280
281                 $con = omb_oauth_consumer();
282                 $tok = new OAuthToken($token, $secret);
283
284                 $url = omb_service_uri($omb[OAUTH_ENDPOINT_AUTHORIZE]);
285
286                 # XXX: Is this the right thing to do? Strip off GET params and make them
287                 # POST params? Seems wrong to me.
288
289                 $parsed = parse_url($url);
290                 $params = array();
291                 parse_str($parsed['query'], $params);
292
293                 $req = OAuthRequest::from_consumer_and_token($con, $tok, 'GET', $url, $params);
294
295                 # We send over a ton of information. This lets the other
296                 # server store info about our user, and it lets the current
297                 # user decide if they really want to authorize the subscription.
298
299                 $req->set_parameter('omb_version', OMB_VERSION_01);
300                 $req->set_parameter('omb_listener', omb_local_id($omb[OAUTH_ENDPOINT_REQUEST]));
301                 $req->set_parameter('omb_listenee', $user->uri);
302                 $req->set_parameter('omb_listenee_profile', common_profile_url($user->nickname));
303                 $req->set_parameter('omb_listenee_nickname', $user->nickname);
304                 $req->set_parameter('omb_listenee_license', $config['license']['url']);
305                 $profile = $user->getProfile();
306                 if ($profile->fullname) {
307                         $req->set_parameter('omb_listenee_fullname', $profile->fullname);
308                 }
309                 if ($profile->homepage) {
310                         $req->set_parameter('omb_listenee_homepage', $profile->homepage);
311                 }
312                 if ($profile->bio) {
313                         $req->set_parameter('omb_listenee_bio', $profile->bio);
314                 }
315                 if ($profile->location) {
316                         $req->set_parameter('omb_listenee_location', $profile->location);
317                 }
318                 $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
319                 if ($avatar) {
320                         $req->set_parameter('omb_listenee_avatar', $avatar->url);
321                 }
322
323                 # XXX: add a nonce to prevent replay attacks
324
325                 $req->set_parameter('oauth_callback', common_local_url('finishremotesubscribe'));
326
327                 # XXX: test to see if endpoint accepts this signature method
328
329                 $req->sign_request(omb_hmac_sha1(), $con, $tok);
330
331                 # store all our info here
332
333                 $omb['listenee'] = $user->nickname;
334                 $omb['listener'] = omb_local_id($omb[OAUTH_ENDPOINT_REQUEST]);
335                 $omb['token'] = $token;
336                 $omb['secret'] = $secret;
337                 # call doesn't work after bounce back so we cache; maybe serialization issue...?
338                 $omb['access_token_url'] = omb_service_uri($omb[OAUTH_ENDPOINT_ACCESS]);
339                 $omb['post_notice_url'] = omb_service_uri($omb[OMB_ENDPOINT_POSTNOTICE]);
340                 $omb['update_profile_url'] = omb_service_uri($omb[OMB_ENDPOINT_UPDATEPROFILE]);
341
342                 common_ensure_session();
343                 
344                 $_SESSION['oauth_authorization_request'] = $omb;
345
346                 # Redirect to authorization service
347
348                 common_redirect($req->to_url());
349                 return;
350         }
351
352         function make_nonce() {
353                 return common_good_rand(16);
354         }
355 }