3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2008, 2009, StatusNet, 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('STATUSNET') && !defined('LACONICA')) { exit(1); }
22 require_once(INSTALLDIR.'/lib/omb.php');
24 class RemotesubscribeAction extends Action
30 function prepare($args)
32 parent::prepare($args);
34 if (common_logged_in()) {
35 $this->clientError(_('You can use the local subscription!'));
39 $this->nickname = $this->trimmed('nickname');
40 $this->profile_url = $this->trimmed('profile_url');
45 function handle($args)
47 parent::handle($args);
49 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
51 $token = $this->trimmed('token');
52 if (!$token || $token != common_session_token()) {
53 $this->showForm(_('There was a problem with your session token. '.
54 'Try again, please.'));
57 $this->remoteSubscription();
63 function showForm($err=null)
69 function showPageNotice()
72 $this->element('div', 'error', $this->err);
74 $inst = sprintf(_('To subscribe, you can [login](%%%%action.%s%%%%),' .
75 ' or [register](%%%%action.%s%%%%) a new ' .
76 ' account. If you already have an account ' .
77 ' on a [compatible microblogging site](%%doc.openmublog%%), ' .
78 ' enter your profile URL below.'),
79 (!common_config('site','openidonly')) ? 'login' : 'openidlogin',
80 (!common_config('site','openidonly')) ? 'register' : 'openidlogin');
81 $output = common_markup_to_html($inst);
82 $this->elementStart('div', 'instructions');
84 $this->elementEnd('div');
90 return _('Remote subscribe');
93 function showContent()
95 # id = remotesubscribe conflicts with the
96 # button on profile page
97 $this->elementStart('form', array('id' => 'form_remote_subscribe',
99 'class' => 'form_settings',
100 'action' => common_local_url('remotesubscribe')));
101 $this->elementStart('fieldset');
102 $this->element('legend', _('Subscribe to a remote user'));
103 $this->hidden('token', common_session_token());
105 $this->elementStart('ul', 'form_data');
106 $this->elementStart('li');
107 $this->input('nickname', _('User nickname'), $this->nickname,
108 _('Nickname of the user you want to follow'));
109 $this->elementEnd('li');
110 $this->elementStart('li');
111 $this->input('profile_url', _('Profile URL'), $this->profile_url,
112 _('URL of your profile on another compatible microblogging service'));
113 $this->elementEnd('li');
114 $this->elementEnd('ul');
115 $this->submit('submit', _('Subscribe'));
116 $this->elementEnd('fieldset');
117 $this->elementEnd('form');
120 function remoteSubscription()
122 $user = $this->getUser();
125 $this->showForm(_('No such user.'));
129 $this->profile_url = $this->trimmed('profile_url');
131 if (!$this->profile_url) {
132 $this->showForm(_('No such user.'));
136 if (!Validate::uri($this->profile_url, array('allowed_schemes' => array('http', 'https')))) {
137 $this->showForm(_('Invalid profile URL (bad format)'));
141 $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
142 $yadis = Auth_Yadis_Yadis::discover($this->profile_url, $fetcher);
144 if (!$yadis || $yadis->failed) {
145 $this->showForm(_('Not a valid profile URL (no YADIS document).'));
149 # XXX: a little liberal for sites that accidentally put whitespace before the xml declaration
151 $xrds =& Auth_Yadis_XRDS::parseXRDS(trim($yadis->response_text));
154 $this->showForm(_('Not a valid profile URL (no XRDS defined).'));
158 $omb = $this->getOmb($xrds);
161 $this->showForm(_('Not a valid profile URL (incorrect services).'));
165 if (omb_service_uri($omb[OAUTH_ENDPOINT_REQUEST]) ==
166 common_local_url('requesttoken'))
168 $this->showForm(_('That\'s a local profile! Login to subscribe.'));
172 if (User::staticGet('uri', omb_local_id($omb[OAUTH_ENDPOINT_REQUEST]))) {
173 $this->showForm(_('That\'s a local profile! Login to subscribe.'));
177 list($token, $secret) = $this->requestToken($omb);
179 if (!$token || !$secret) {
180 $this->showForm(_('Couldn\'t get a request token.'));
184 $this->requestAuthorization($user, $omb, $token, $secret);
190 if ($this->nickname) {
191 $user = User::staticGet('nickname', $this->nickname);
196 function getOmb($xrds)
198 static $omb_endpoints = array(OMB_ENDPOINT_UPDATEPROFILE, OMB_ENDPOINT_POSTNOTICE);
199 static $oauth_endpoints = array(OAUTH_ENDPOINT_REQUEST, OAUTH_ENDPOINT_AUTHORIZE,
200 OAUTH_ENDPOINT_ACCESS);
203 # XXX: the following code could probably be refactored to eliminate dupes
205 $oauth_services = omb_get_services($xrds, OAUTH_DISCOVERY);
207 if (!$oauth_services) {
211 $oauth_service = $oauth_services[0];
213 $oauth_xrd = $this->getXRD($oauth_service, $xrds);
219 if (!$this->addServices($oauth_xrd, $oauth_endpoints, $omb)) {
223 $omb_services = omb_get_services($xrds, OMB_NAMESPACE);
225 if (!$omb_services) {
229 $omb_service = $omb_services[0];
231 $omb_xrd = $this->getXRD($omb_service, $xrds);
237 if (!$this->addServices($omb_xrd, $omb_endpoints, $omb)) {
241 # XXX: check that we got all the services we needed
243 foreach (array_merge($omb_endpoints, $oauth_endpoints) as $type) {
244 if (!array_key_exists($type, $omb) || !$omb[$type]) {
249 if (!omb_local_id($omb[OAUTH_ENDPOINT_REQUEST])) {
256 function getXRD($main_service, $main_xrds)
258 $uri = omb_service_uri($main_service);
259 if (strpos($uri, "#") !== 0) {
260 # FIXME: more rigorous handling of external service definitions
263 $id = substr($uri, 1);
264 $nodes = $main_xrds->allXrdNodes;
265 $parser = $main_xrds->parser;
266 foreach ($nodes as $node) {
267 $attrs = $parser->attributes($node);
268 if (array_key_exists('xml:id', $attrs) &&
269 $attrs['xml:id'] == $id) {
270 # XXX: trick the constructor into thinking this is the only node
271 $bogus_nodes = array($node);
272 return new Auth_Yadis_XRDS($parser, $bogus_nodes);
278 function addServices($xrd, $types, &$omb)
280 foreach ($types as $type) {
281 $matches = omb_get_services($xrd, $type);
283 $omb[$type] = $matches[0];
292 function requestToken($omb)
294 $con = omb_oauth_consumer();
296 $url = omb_service_uri($omb[OAUTH_ENDPOINT_REQUEST]);
298 # XXX: Is this the right thing to do? Strip off GET params and make them
299 # POST params? Seems wrong to me.
301 $parsed = parse_url($url);
303 parse_str($parsed['query'], $params);
305 $req = OAuthRequest::from_consumer_and_token($con, null, "POST", $url, $params);
307 $listener = omb_local_id($omb[OAUTH_ENDPOINT_REQUEST]);
313 $req->set_parameter('omb_listener', $listener);
314 $req->set_parameter('omb_version', OMB_VERSION_01);
316 # XXX: test to see if endpoint accepts this signature method
318 $req->sign_request(omb_hmac_sha1(), $con, null);
320 # We re-use this tool's fetcher, since it's pretty good
322 $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
324 $result = $fetcher->post($req->get_normalized_http_url(),
326 array('User-Agent: StatusNet/' . STATUSNET_VERSION));
327 if ($result->status != 200) {
331 parse_str($result->body, $return);
333 return array($return['oauth_token'], $return['oauth_token_secret']);
336 function requestAuthorization($user, $omb, $token, $secret)
338 $con = omb_oauth_consumer();
339 $tok = new OAuthToken($token, $secret);
341 $url = omb_service_uri($omb[OAUTH_ENDPOINT_AUTHORIZE]);
343 # XXX: Is this the right thing to do? Strip off GET params and make them
344 # POST params? Seems wrong to me.
346 $parsed = parse_url($url);
348 parse_str($parsed['query'], $params);
350 $req = OAuthRequest::from_consumer_and_token($con, $tok, 'GET', $url, $params);
352 # We send over a ton of information. This lets the other
353 # server store info about our user, and it lets the current
354 # user decide if they really want to authorize the subscription.
356 $req->set_parameter('omb_version', OMB_VERSION_01);
357 $req->set_parameter('omb_listener', omb_local_id($omb[OAUTH_ENDPOINT_REQUEST]));
358 $req->set_parameter('omb_listenee', $user->uri);
359 $req->set_parameter('omb_listenee_profile', common_profile_url($user->nickname));
360 $req->set_parameter('omb_listenee_nickname', $user->nickname);
361 $req->set_parameter('omb_listenee_license', common_config('license', 'url'));
363 $profile = $user->getProfile();
365 common_log_db_error($user, 'SELECT', __FILE__);
366 $this->serverError(_('User without matching profile'));
370 if (!is_null($profile->fullname)) {
371 $req->set_parameter('omb_listenee_fullname', $profile->fullname);
373 if (!is_null($profile->homepage)) {
374 $req->set_parameter('omb_listenee_homepage', $profile->homepage);
376 if (!is_null($profile->bio)) {
377 $req->set_parameter('omb_listenee_bio', $profile->bio);
379 if (!is_null($profile->location)) {
380 $req->set_parameter('omb_listenee_location', $profile->location);
382 $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
384 $req->set_parameter('omb_listenee_avatar', $avatar->url);
387 # XXX: add a nonce to prevent replay attacks
389 $req->set_parameter('oauth_callback', common_local_url('finishremotesubscribe'));
391 # XXX: test to see if endpoint accepts this signature method
393 $req->sign_request(omb_hmac_sha1(), $con, $tok);
395 # store all our info here
397 $omb['listenee'] = $user->nickname;
398 $omb['listener'] = omb_local_id($omb[OAUTH_ENDPOINT_REQUEST]);
399 $omb['token'] = $token;
400 $omb['secret'] = $secret;
401 # call doesn't work after bounce back so we cache; maybe serialization issue...?
402 $omb['access_token_url'] = omb_service_uri($omb[OAUTH_ENDPOINT_ACCESS]);
403 $omb['post_notice_url'] = omb_service_uri($omb[OMB_ENDPOINT_POSTNOTICE]);
404 $omb['update_profile_url'] = omb_service_uri($omb[OMB_ENDPOINT_UPDATEPROFILE]);
406 common_ensure_session();
408 $_SESSION['oauth_authorization_request'] = $omb;
410 # Redirect to authorization service
412 common_redirect($req->to_url(), 303);