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 {
26 function handle($args)
29 parent::handle($args);
31 if (common_logged_in()) {
32 common_user_error(_('You can use the local subscription!'));
36 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
39 $token = $this->trimmed('token');
40 if (!$token || $token != common_session_token()) {
41 $this->show_form(_('There was a problem with your session token. Try again, please.'));
45 $this->remote_subscription();
51 function get_instructions()
53 return _('To subscribe, you can [login](%%action.login%%),' .
54 ' or [register](%%action.register%%) a new ' .
55 ' account. If you already have an account ' .
56 ' on a [compatible microblogging site](%%doc.openmublog%%), ' .
57 ' enter your profile URL below.');
60 function show_top($err=null)
63 common_element('div', 'error', $err);
65 $instructions = $this->get_instructions();
66 $output = common_markup_to_html($instructions);
67 common_element_start('div', 'instructions');
69 common_element_end('p');
73 function show_form($err=null)
75 $nickname = $this->trimmed('nickname');
76 $profile = $this->trimmed('profile_url');
77 common_show_header(_('Remote subscribe'), null, $err,
78 array($this, 'show_top'));
79 # id = remotesubscribe conflicts with the
80 # button on profile page
81 common_element_start('form', array('id' => 'remsub', 'method' => 'post',
82 'action' => common_local_url('remotesubscribe')));
83 common_hidden('token', common_session_token());
84 common_input('nickname', _('User nickname'), $nickname,
85 _('Nickname of the user you want to follow'));
86 common_input('profile_url', _('Profile URL'), $profile,
87 _('URL of your profile on another compatible microblogging service'));
88 common_submit('submit', _('Subscribe'));
89 common_element_end('form');
93 function remote_subscription()
95 $user = $this->get_user();
98 $this->show_form(_('No such user.'));
102 $profile = $this->trimmed('profile_url');
105 $this->show_form(_('No such user.'));
109 if (!Validate::uri($profile, array('allowed_schemes' => array('http', 'https')))) {
110 $this->show_form(_('Invalid profile URL (bad format)'));
114 $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
115 $yadis = Auth_Yadis_Yadis::discover($profile, $fetcher);
117 if (!$yadis || $yadis->failed) {
118 $this->show_form(_('Not a valid profile URL (no YADIS document).'));
122 # XXX: a little liberal for sites that accidentally put whitespace before the xml declaration
124 $xrds =& Auth_Yadis_XRDS::parseXRDS(trim($yadis->response_text));
127 $this->show_form(_('Not a valid profile URL (no XRDS defined).'));
131 $omb = $this->getOmb($xrds);
134 $this->show_form(_('Not a valid profile URL (incorrect services).'));
138 if (omb_service_uri($omb[OAUTH_ENDPOINT_REQUEST]) ==
139 common_local_url('requesttoken'))
141 $this->show_form(_('That\'s a local profile! Login to subscribe.'));
145 if (User::staticGet('uri', omb_local_id($omb[OAUTH_ENDPOINT_REQUEST]))) {
146 $this->show_form(_('That\'s a local profile! Login to subscribe.'));
150 list($token, $secret) = $this->request_token($omb);
152 if (!$token || !$secret) {
153 $this->show_form(_('Couldn\'t get a request token.'));
157 $this->request_authorization($user, $omb, $token, $secret);
163 $nickname = $this->trimmed('nickname');
165 $user = User::staticGet('nickname', $nickname);
170 function getOmb($xrds)
173 static $omb_endpoints = array(OMB_ENDPOINT_UPDATEPROFILE, OMB_ENDPOINT_POSTNOTICE);
174 static $oauth_endpoints = array(OAUTH_ENDPOINT_REQUEST, OAUTH_ENDPOINT_AUTHORIZE,
175 OAUTH_ENDPOINT_ACCESS);
178 # XXX: the following code could probably be refactored to eliminate dupes
180 $oauth_services = omb_get_services($xrds, OAUTH_DISCOVERY);
182 if (!$oauth_services) {
186 $oauth_service = $oauth_services[0];
188 $oauth_xrd = $this->getXRD($oauth_service, $xrds);
194 if (!$this->addServices($oauth_xrd, $oauth_endpoints, $omb)) {
198 $omb_services = omb_get_services($xrds, OMB_NAMESPACE);
200 if (!$omb_services) {
204 $omb_service = $omb_services[0];
206 $omb_xrd = $this->getXRD($omb_service, $xrds);
212 if (!$this->addServices($omb_xrd, $omb_endpoints, $omb)) {
216 # XXX: check that we got all the services we needed
218 foreach (array_merge($omb_endpoints, $oauth_endpoints) as $type) {
219 if (!array_key_exists($type, $omb) || !$omb[$type]) {
224 if (!omb_local_id($omb[OAUTH_ENDPOINT_REQUEST])) {
231 function getXRD($main_service, $main_xrds)
233 $uri = omb_service_uri($main_service);
234 if (strpos($uri, "#") !== 0) {
235 # FIXME: more rigorous handling of external service definitions
238 $id = substr($uri, 1);
239 $nodes = $main_xrds->allXrdNodes;
240 $parser = $main_xrds->parser;
241 foreach ($nodes as $node) {
242 $attrs = $parser->attributes($node);
243 if (array_key_exists('xml:id', $attrs) &&
244 $attrs['xml:id'] == $id) {
245 # XXX: trick the constructor into thinking this is the only node
246 $bogus_nodes = array($node);
247 return new Auth_Yadis_XRDS($parser, $bogus_nodes);
253 function addServices($xrd, $types, &$omb)
255 foreach ($types as $type) {
256 $matches = omb_get_services($xrd, $type);
258 $omb[$type] = $matches[0];
267 function request_token($omb)
269 $con = omb_oauth_consumer();
271 $url = omb_service_uri($omb[OAUTH_ENDPOINT_REQUEST]);
273 # XXX: Is this the right thing to do? Strip off GET params and make them
274 # POST params? Seems wrong to me.
276 $parsed = parse_url($url);
278 parse_str($parsed['query'], $params);
280 $req = OAuthRequest::from_consumer_and_token($con, null, "POST", $url, $params);
282 $listener = omb_local_id($omb[OAUTH_ENDPOINT_REQUEST]);
288 $req->set_parameter('omb_listener', $listener);
289 $req->set_parameter('omb_version', OMB_VERSION_01);
291 # XXX: test to see if endpoint accepts this signature method
293 $req->sign_request(omb_hmac_sha1(), $con, null);
295 # We re-use this tool's fetcher, since it's pretty good
297 $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
299 $result = $fetcher->post($req->get_normalized_http_url(),
301 array('User-Agent' => 'Laconica/' . LACONICA_VERSION));
303 if ($result->status != 200) {
307 parse_str($result->body, $return);
309 return array($return['oauth_token'], $return['oauth_token_secret']);
312 function request_authorization($user, $omb, $token, $secret)
314 global $config; # for license URL
316 $con = omb_oauth_consumer();
317 $tok = new OAuthToken($token, $secret);
319 $url = omb_service_uri($omb[OAUTH_ENDPOINT_AUTHORIZE]);
321 # XXX: Is this the right thing to do? Strip off GET params and make them
322 # POST params? Seems wrong to me.
324 $parsed = parse_url($url);
326 parse_str($parsed['query'], $params);
328 $req = OAuthRequest::from_consumer_and_token($con, $tok, 'GET', $url, $params);
330 # We send over a ton of information. This lets the other
331 # server store info about our user, and it lets the current
332 # user decide if they really want to authorize the subscription.
334 $req->set_parameter('omb_version', OMB_VERSION_01);
335 $req->set_parameter('omb_listener', omb_local_id($omb[OAUTH_ENDPOINT_REQUEST]));
336 $req->set_parameter('omb_listenee', $user->uri);
337 $req->set_parameter('omb_listenee_profile', common_profile_url($user->nickname));
338 $req->set_parameter('omb_listenee_nickname', $user->nickname);
339 $req->set_parameter('omb_listenee_license', $config['license']['url']);
341 $profile = $user->getProfile();
343 common_log_db_error($user, 'SELECT', __FILE__);
344 $this->server_error(_('User without matching profile'));
348 if ($profile->fullname) {
349 $req->set_parameter('omb_listenee_fullname', $profile->fullname);
351 if ($profile->homepage) {
352 $req->set_parameter('omb_listenee_homepage', $profile->homepage);
355 $req->set_parameter('omb_listenee_bio', $profile->bio);
357 if ($profile->location) {
358 $req->set_parameter('omb_listenee_location', $profile->location);
360 $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
362 $req->set_parameter('omb_listenee_avatar', $avatar->url);
365 # XXX: add a nonce to prevent replay attacks
367 $req->set_parameter('oauth_callback', common_local_url('finishremotesubscribe'));
369 # XXX: test to see if endpoint accepts this signature method
371 $req->sign_request(omb_hmac_sha1(), $con, $tok);
373 # store all our info here
375 $omb['listenee'] = $user->nickname;
376 $omb['listener'] = omb_local_id($omb[OAUTH_ENDPOINT_REQUEST]);
377 $omb['token'] = $token;
378 $omb['secret'] = $secret;
379 # call doesn't work after bounce back so we cache; maybe serialization issue...?
380 $omb['access_token_url'] = omb_service_uri($omb[OAUTH_ENDPOINT_ACCESS]);
381 $omb['post_notice_url'] = omb_service_uri($omb[OMB_ENDPOINT_POSTNOTICE]);
382 $omb['update_profile_url'] = omb_service_uri($omb[OMB_ENDPOINT_UPDATEPROFILE]);
384 common_ensure_session();
386 $_SESSION['oauth_authorization_request'] = $omb;
388 # Redirect to authorization service
390 common_redirect($req->to_url());
394 function make_nonce()
396 return common_good_rand(16);