3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, Controlez-Vous, Inc.
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.
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.
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/>.
20 if (!defined('LACONICA')) { exit(1); }
22 require_once(INSTALLDIR.'/lib/omb.php');
24 class RemotesubscribeAction extends Action
27 function handle($args)
30 parent::handle($args);
32 if (common_logged_in()) {
33 common_user_error(_('You can use the local subscription!'));
37 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
40 $token = $this->trimmed('token');
41 if (!$token || $token != common_session_token()) {
42 $this->show_form(_('There was a problem with your session token. Try again, please.'));
46 $this->remote_subscription();
52 function get_instructions()
54 return _('To subscribe, you can [login](%%action.login%%),' .
55 ' or [register](%%action.register%%) a new ' .
56 ' account. If you already have an account ' .
57 ' on a [compatible microblogging site](%%doc.openmublog%%), ' .
58 ' enter your profile URL below.');
61 function show_top($err=null)
64 $this->element('div', 'error', $err);
66 $instructions = $this->get_instructions();
67 $output = common_markup_to_html($instructions);
68 $this->elementStart('div', 'instructions');
70 $this->elementEnd('p');
74 function show_form($err=null)
76 $nickname = $this->trimmed('nickname');
77 $profile = $this->trimmed('profile_url');
78 common_show_header(_('Remote subscribe'), null, $err,
79 array($this, 'show_top'));
80 # id = remotesubscribe conflicts with the
81 # button on profile page
82 $this->elementStart('form', array('id' => 'remsub', 'method' => 'post',
83 'action' => common_local_url('remotesubscribe')));
84 $this->hidden('token', common_session_token());
85 $this->input('nickname', _('User nickname'), $nickname,
86 _('Nickname of the user you want to follow'));
87 $this->input('profile_url', _('Profile URL'), $profile,
88 _('URL of your profile on another compatible microblogging service'));
89 $this->submit('submit', _('Subscribe'));
90 $this->elementEnd('form');
94 function remote_subscription()
96 $user = $this->get_user();
99 $this->show_form(_('No such user.'));
103 $profile = $this->trimmed('profile_url');
106 $this->show_form(_('No such user.'));
110 if (!Validate::uri($profile, array('allowed_schemes' => array('http', 'https')))) {
111 $this->show_form(_('Invalid profile URL (bad format)'));
115 $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
116 $yadis = Auth_Yadis_Yadis::discover($profile, $fetcher);
118 if (!$yadis || $yadis->failed) {
119 $this->show_form(_('Not a valid profile URL (no YADIS document).'));
123 # XXX: a little liberal for sites that accidentally put whitespace before the xml declaration
125 $xrds =& Auth_Yadis_XRDS::parseXRDS(trim($yadis->response_text));
128 $this->show_form(_('Not a valid profile URL (no XRDS defined).'));
132 $omb = $this->getOmb($xrds);
135 $this->show_form(_('Not a valid profile URL (incorrect services).'));
139 if (omb_service_uri($omb[OAUTH_ENDPOINT_REQUEST]) ==
140 common_local_url('requesttoken'))
142 $this->show_form(_('That\'s a local profile! Login to subscribe.'));
146 if (User::staticGet('uri', omb_local_id($omb[OAUTH_ENDPOINT_REQUEST]))) {
147 $this->show_form(_('That\'s a local profile! Login to subscribe.'));
151 list($token, $secret) = $this->request_token($omb);
153 if (!$token || !$secret) {
154 $this->show_form(_('Couldn\'t get a request token.'));
158 $this->request_authorization($user, $omb, $token, $secret);
164 $nickname = $this->trimmed('nickname');
166 $user = User::staticGet('nickname', $nickname);
171 function getOmb($xrds)
174 static $omb_endpoints = array(OMB_ENDPOINT_UPDATEPROFILE, OMB_ENDPOINT_POSTNOTICE);
175 static $oauth_endpoints = array(OAUTH_ENDPOINT_REQUEST, OAUTH_ENDPOINT_AUTHORIZE,
176 OAUTH_ENDPOINT_ACCESS);
179 # XXX: the following code could probably be refactored to eliminate dupes
181 $oauth_services = omb_get_services($xrds, OAUTH_DISCOVERY);
183 if (!$oauth_services) {
187 $oauth_service = $oauth_services[0];
189 $oauth_xrd = $this->getXRD($oauth_service, $xrds);
195 if (!$this->addServices($oauth_xrd, $oauth_endpoints, $omb)) {
199 $omb_services = omb_get_services($xrds, OMB_NAMESPACE);
201 if (!$omb_services) {
205 $omb_service = $omb_services[0];
207 $omb_xrd = $this->getXRD($omb_service, $xrds);
213 if (!$this->addServices($omb_xrd, $omb_endpoints, $omb)) {
217 # XXX: check that we got all the services we needed
219 foreach (array_merge($omb_endpoints, $oauth_endpoints) as $type) {
220 if (!array_key_exists($type, $omb) || !$omb[$type]) {
225 if (!omb_local_id($omb[OAUTH_ENDPOINT_REQUEST])) {
232 function getXRD($main_service, $main_xrds)
234 $uri = omb_service_uri($main_service);
235 if (strpos($uri, "#") !== 0) {
236 # FIXME: more rigorous handling of external service definitions
239 $id = substr($uri, 1);
240 $nodes = $main_xrds->allXrdNodes;
241 $parser = $main_xrds->parser;
242 foreach ($nodes as $node) {
243 $attrs = $parser->attributes($node);
244 if (array_key_exists('xml:id', $attrs) &&
245 $attrs['xml:id'] == $id) {
246 # XXX: trick the constructor into thinking this is the only node
247 $bogus_nodes = array($node);
248 return new Auth_Yadis_XRDS($parser, $bogus_nodes);
254 function addServices($xrd, $types, &$omb)
256 foreach ($types as $type) {
257 $matches = omb_get_services($xrd, $type);
259 $omb[$type] = $matches[0];
268 function request_token($omb)
270 $con = omb_oauth_consumer();
272 $url = omb_service_uri($omb[OAUTH_ENDPOINT_REQUEST]);
274 # XXX: Is this the right thing to do? Strip off GET params and make them
275 # POST params? Seems wrong to me.
277 $parsed = parse_url($url);
279 parse_str($parsed['query'], $params);
281 $req = OAuthRequest::from_consumer_and_token($con, null, "POST", $url, $params);
283 $listener = omb_local_id($omb[OAUTH_ENDPOINT_REQUEST]);
289 $req->set_parameter('omb_listener', $listener);
290 $req->set_parameter('omb_version', OMB_VERSION_01);
292 # XXX: test to see if endpoint accepts this signature method
294 $req->sign_request(omb_hmac_sha1(), $con, null);
296 # We re-use this tool's fetcher, since it's pretty good
298 $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
300 $result = $fetcher->post($req->get_normalized_http_url(),
302 array('User-Agent' => 'Laconica/' . LACONICA_VERSION));
304 if ($result->status != 200) {
308 parse_str($result->body, $return);
310 return array($return['oauth_token'], $return['oauth_token_secret']);
313 function request_authorization($user, $omb, $token, $secret)
315 global $config; # for license URL
317 $con = omb_oauth_consumer();
318 $tok = new OAuthToken($token, $secret);
320 $url = omb_service_uri($omb[OAUTH_ENDPOINT_AUTHORIZE]);
322 # XXX: Is this the right thing to do? Strip off GET params and make them
323 # POST params? Seems wrong to me.
325 $parsed = parse_url($url);
327 parse_str($parsed['query'], $params);
329 $req = OAuthRequest::from_consumer_and_token($con, $tok, 'GET', $url, $params);
331 # We send over a ton of information. This lets the other
332 # server store info about our user, and it lets the current
333 # user decide if they really want to authorize the subscription.
335 $req->set_parameter('omb_version', OMB_VERSION_01);
336 $req->set_parameter('omb_listener', omb_local_id($omb[OAUTH_ENDPOINT_REQUEST]));
337 $req->set_parameter('omb_listenee', $user->uri);
338 $req->set_parameter('omb_listenee_profile', common_profile_url($user->nickname));
339 $req->set_parameter('omb_listenee_nickname', $user->nickname);
340 $req->set_parameter('omb_listenee_license', $config['license']['url']);
342 $profile = $user->getProfile();
344 common_log_db_error($user, 'SELECT', __FILE__);
345 $this->server_error(_('User without matching profile'));
349 if ($profile->fullname) {
350 $req->set_parameter('omb_listenee_fullname', $profile->fullname);
352 if ($profile->homepage) {
353 $req->set_parameter('omb_listenee_homepage', $profile->homepage);
356 $req->set_parameter('omb_listenee_bio', $profile->bio);
358 if ($profile->location) {
359 $req->set_parameter('omb_listenee_location', $profile->location);
361 $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
363 $req->set_parameter('omb_listenee_avatar', $avatar->url);
366 # XXX: add a nonce to prevent replay attacks
368 $req->set_parameter('oauth_callback', common_local_url('finishremotesubscribe'));
370 # XXX: test to see if endpoint accepts this signature method
372 $req->sign_request(omb_hmac_sha1(), $con, $tok);
374 # store all our info here
376 $omb['listenee'] = $user->nickname;
377 $omb['listener'] = omb_local_id($omb[OAUTH_ENDPOINT_REQUEST]);
378 $omb['token'] = $token;
379 $omb['secret'] = $secret;
380 # call doesn't work after bounce back so we cache; maybe serialization issue...?
381 $omb['access_token_url'] = omb_service_uri($omb[OAUTH_ENDPOINT_ACCESS]);
382 $omb['post_notice_url'] = omb_service_uri($omb[OMB_ENDPOINT_POSTNOTICE]);
383 $omb['update_profile_url'] = omb_service_uri($omb[OMB_ENDPOINT_UPDATEPROFILE]);
385 common_ensure_session();
387 $_SESSION['oauth_authorization_request'] = $omb;
389 # Redirect to authorization service
391 common_redirect($req->to_url());
395 function make_nonce()
397 return common_good_rand(16);