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) {
28 parent::handle($args);
30 if (common_logged_in()) {
31 common_user_error(_('You can use the local subscription!'));
35 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
38 $token = $this->trimmed('token');
39 if (!$token || $token != common_session_token()) {
40 $this->show_form(_('There was a problem with your session token. Try again, please.'));
44 $this->remote_subscription();
50 function get_instructions() {
51 return _('To subscribe, you can [login](%%action.login%%),' .
52 ' or [register](%%action.register%%) a new ' .
53 ' account. If you already have an account ' .
54 ' on a [compatible microblogging site](%%doc.openmublog%%), ' .
55 ' enter your profile URL below.');
58 function show_top($err=NULL) {
60 common_element('div', 'error', $err);
62 $instructions = $this->get_instructions();
63 $output = common_markup_to_html($instructions);
64 common_element_start('div', 'instructions');
66 common_element_end('p');
70 function show_form($err=NULL) {
71 $nickname = $this->trimmed('nickname');
72 $profile = $this->trimmed('profile_url');
73 common_show_header(_('Remote subscribe'), NULL, $err,
74 array($this, 'show_top'));
75 # id = remotesubscribe conflicts with the
76 # button on profile page
77 common_element_start('form', array('id' => 'remsub', 'method' => 'post',
78 'action' => common_local_url('remotesubscribe')));
79 common_hidden('token', common_session_token());
80 common_input('nickname', _('User nickname'), $nickname,
81 _('Nickname of the user you want to follow'));
82 common_input('profile_url', _('Profile URL'), $profile,
83 _('URL of your profile on another compatible microblogging service'));
84 common_submit('submit', _('Subscribe'));
85 common_element_end('form');
89 function remote_subscription() {
90 $user = $this->get_user();
93 $this->show_form(_('No such user.'));
97 $profile = $this->trimmed('profile_url');
100 $this->show_form(_('No such user.'));
104 if (!Validate::uri($profile, array('allowed_schemes' => array('http', 'https')))) {
105 $this->show_form(_('Invalid profile URL (bad format)'));
109 $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
110 $yadis = Auth_Yadis_Yadis::discover($profile, $fetcher);
112 if (!$yadis || $yadis->failed) {
113 $this->show_form(_('Not a valid profile URL (no YADIS document).'));
117 # XXX: a little liberal for sites that accidentally put whitespace before the xml declaration
119 $xrds =& Auth_Yadis_XRDS::parseXRDS(trim($yadis->response_text));
122 $this->show_form(_('Not a valid profile URL (no XRDS defined).'));
126 $omb = $this->getOmb($xrds);
129 $this->show_form(_('Not a valid profile URL (incorrect services).'));
133 if (omb_service_uri($omb[OAUTH_ENDPOINT_REQUEST]) ==
134 common_local_url('requesttoken'))
136 $this->show_form(_('That\'s a local profile! Login to subscribe.'));
140 if (User::staticGet('uri', omb_local_id($omb[OAUTH_ENDPOINT_REQUEST]))) {
141 $this->show_form(_('That\'s a local profile! Login to subscribe.'));
145 list($token, $secret) = $this->request_token($omb);
147 if (!$token || !$secret) {
148 $this->show_form(_('Couldn\'t get a request token.'));
152 $this->request_authorization($user, $omb, $token, $secret);
155 function get_user() {
157 $nickname = $this->trimmed('nickname');
159 $user = User::staticGet('nickname', $nickname);
164 function getOmb($xrds) {
166 static $omb_endpoints = array(OMB_ENDPOINT_UPDATEPROFILE, OMB_ENDPOINT_POSTNOTICE);
167 static $oauth_endpoints = array(OAUTH_ENDPOINT_REQUEST, OAUTH_ENDPOINT_AUTHORIZE,
168 OAUTH_ENDPOINT_ACCESS);
171 # XXX: the following code could probably be refactored to eliminate dupes
173 $oauth_services = omb_get_services($xrds, OAUTH_DISCOVERY);
175 if (!$oauth_services) {
179 $oauth_service = $oauth_services[0];
181 $oauth_xrd = $this->getXRD($oauth_service, $xrds);
187 if (!$this->addServices($oauth_xrd, $oauth_endpoints, $omb)) {
191 $omb_services = omb_get_services($xrds, OMB_NAMESPACE);
193 if (!$omb_services) {
197 $omb_service = $omb_services[0];
199 $omb_xrd = $this->getXRD($omb_service, $xrds);
205 if (!$this->addServices($omb_xrd, $omb_endpoints, $omb)) {
209 # XXX: check that we got all the services we needed
211 foreach (array_merge($omb_endpoints, $oauth_endpoints) as $type) {
212 if (!array_key_exists($type, $omb) || !$omb[$type]) {
217 if (!omb_local_id($omb[OAUTH_ENDPOINT_REQUEST])) {
224 function getXRD($main_service, $main_xrds) {
225 $uri = omb_service_uri($main_service);
226 if (strpos($uri, "#") !== 0) {
227 # FIXME: more rigorous handling of external service definitions
230 $id = substr($uri, 1);
231 $nodes = $main_xrds->allXrdNodes;
232 $parser = $main_xrds->parser;
233 foreach ($nodes as $node) {
234 $attrs = $parser->attributes($node);
235 if (array_key_exists('xml:id', $attrs) &&
236 $attrs['xml:id'] == $id) {
237 # XXX: trick the constructor into thinking this is the only node
238 $bogus_nodes = array($node);
239 return new Auth_Yadis_XRDS($parser, $bogus_nodes);
245 function addServices($xrd, $types, &$omb) {
246 foreach ($types as $type) {
247 $matches = omb_get_services($xrd, $type);
249 $omb[$type] = $matches[0];
258 function request_token($omb) {
259 $con = omb_oauth_consumer();
261 $url = omb_service_uri($omb[OAUTH_ENDPOINT_REQUEST]);
263 # XXX: Is this the right thing to do? Strip off GET params and make them
264 # POST params? Seems wrong to me.
266 $parsed = parse_url($url);
268 parse_str($parsed['query'], $params);
270 $req = OAuthRequest::from_consumer_and_token($con, NULL, "POST", $url, $params);
272 $listener = omb_local_id($omb[OAUTH_ENDPOINT_REQUEST]);
278 $req->set_parameter('omb_listener', $listener);
279 $req->set_parameter('omb_version', OMB_VERSION_01);
281 # XXX: test to see if endpoint accepts this signature method
283 $req->sign_request(omb_hmac_sha1(), $con, NULL);
285 # We re-use this tool's fetcher, since it's pretty good
287 $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
289 $result = $fetcher->post($req->get_normalized_http_url(),
290 $req->to_postdata());
292 if ($result->status != 200) {
296 parse_str($result->body, $return);
298 return array($return['oauth_token'], $return['oauth_token_secret']);
301 function request_authorization($user, $omb, $token, $secret) {
302 global $config; # for license URL
304 $con = omb_oauth_consumer();
305 $tok = new OAuthToken($token, $secret);
307 $url = omb_service_uri($omb[OAUTH_ENDPOINT_AUTHORIZE]);
309 # XXX: Is this the right thing to do? Strip off GET params and make them
310 # POST params? Seems wrong to me.
312 $parsed = parse_url($url);
314 parse_str($parsed['query'], $params);
316 $req = OAuthRequest::from_consumer_and_token($con, $tok, 'GET', $url, $params);
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.
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']);
329 $profile = $user->getProfile();
331 common_log_db_error($user, 'SELECT', __FILE__);
332 $this->server_error(_('User without matching profile'));
336 if ($profile->fullname) {
337 $req->set_parameter('omb_listenee_fullname', $profile->fullname);
339 if ($profile->homepage) {
340 $req->set_parameter('omb_listenee_homepage', $profile->homepage);
343 $req->set_parameter('omb_listenee_bio', $profile->bio);
345 if ($profile->location) {
346 $req->set_parameter('omb_listenee_location', $profile->location);
348 $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
350 $req->set_parameter('omb_listenee_avatar', $avatar->url);
353 # XXX: add a nonce to prevent replay attacks
355 $req->set_parameter('oauth_callback', common_local_url('finishremotesubscribe'));
357 # XXX: test to see if endpoint accepts this signature method
359 $req->sign_request(omb_hmac_sha1(), $con, $tok);
361 # store all our info here
363 $omb['listenee'] = $user->nickname;
364 $omb['listener'] = omb_local_id($omb[OAUTH_ENDPOINT_REQUEST]);
365 $omb['token'] = $token;
366 $omb['secret'] = $secret;
367 # call doesn't work after bounce back so we cache; maybe serialization issue...?
368 $omb['access_token_url'] = omb_service_uri($omb[OAUTH_ENDPOINT_ACCESS]);
369 $omb['post_notice_url'] = omb_service_uri($omb[OMB_ENDPOINT_POSTNOTICE]);
370 $omb['update_profile_url'] = omb_service_uri($omb[OMB_ENDPOINT_UPDATEPROFILE]);
372 common_ensure_session();
374 $_SESSION['oauth_authorization_request'] = $omb;
376 # Redirect to authorization service
378 common_redirect($req->to_url());
382 function make_nonce() {
383 return common_good_rand(16);