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') {
36 $this->remote_subscription();
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.');
50 function show_top($err=NULL) {
52 common_element('div', 'error', $err);
54 $instructions = $this->get_instructions();
55 $output = common_markup_to_html($instructions);
56 common_element_start('div', 'instructions');
58 common_element_end('p');
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');
80 function remote_subscription() {
81 $user = $this->get_user();
84 $this->show_form(_('No such user.'));
88 $profile = $this->trimmed('profile_url');
91 $this->show_form(_('No such user.'));
95 if (!Validate::uri($profile, array('allowed_schemes' => array('http', 'https')))) {
96 $this->show_form(_('Invalid profile URL (bad format)'));
100 $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
101 $yadis = Auth_Yadis_Yadis::discover($profile, $fetcher);
103 if (!$yadis || $yadis->failed) {
104 $this->show_form(_('Not a valid profile URL (no YADIS document).'));
108 $xrds =& Auth_Yadis_XRDS::parseXRDS($yadis->response_text);
111 $this->show_form(_('Not a valid profile URL (no XRDS defined).'));
115 $omb = $this->getOmb($xrds);
118 $this->show_form(_('Not a valid profile URL (incorrect services).'));
122 list($token, $secret) = $this->request_token($omb);
124 if (!$token || !$secret) {
125 $this->show_form(_('Couldn\'t get a request token.'));
129 $this->request_authorization($user, $omb, $token, $secret);
132 function get_user() {
134 $nickname = $this->trimmed('nickname');
136 $user = User::staticGet('nickname', $nickname);
141 function getOmb($xrds) {
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);
148 # XXX: the following code could probably be refactored to eliminate dupes
150 $oauth_services = omb_get_services($xrds, OAUTH_DISCOVERY);
152 if (!$oauth_services) {
156 $oauth_service = $oauth_services[0];
158 $oauth_xrd = $this->getXRD($oauth_service, $xrds);
164 if (!$this->addServices($oauth_xrd, $oauth_endpoints, $omb)) {
168 $omb_services = omb_get_services($xrds, OMB_NAMESPACE);
170 if (!$omb_services) {
174 $omb_service = $omb_services[0];
176 $omb_xrd = $this->getXRD($omb_service, $xrds);
182 if (!$this->addServices($omb_xrd, $omb_endpoints, $omb)) {
186 # XXX: check that we got all the services we needed
188 foreach (array_merge($omb_endpoints, $oauth_endpoints) as $type) {
189 if (!array_key_exists($type, $omb) || !$omb[$type]) {
194 if (!omb_local_id($omb[OAUTH_ENDPOINT_REQUEST])) {
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
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);
222 function addServices($xrd, $types, &$omb) {
223 foreach ($types as $type) {
224 $matches = omb_get_services($xrd, $type);
226 $omb[$type] = $matches[0];
235 function request_token($omb) {
236 $con = omb_oauth_consumer();
238 $url = omb_service_uri($omb[OAUTH_ENDPOINT_REQUEST]);
240 # XXX: Is this the right thing to do? Strip off GET params and make them
241 # POST params? Seems wrong to me.
243 $parsed = parse_url($url);
245 parse_str($parsed['query'], $params);
247 $req = OAuthRequest::from_consumer_and_token($con, NULL, "POST", $url, $params);
249 $listener = omb_local_id($omb[OAUTH_ENDPOINT_REQUEST]);
255 $req->set_parameter('omb_listener', $listener);
256 $req->set_parameter('omb_version', OMB_VERSION_01);
258 # XXX: test to see if endpoint accepts this signature method
260 $req->sign_request(omb_hmac_sha1(), $con, NULL);
262 # We re-use this tool's fetcher, since it's pretty good
264 $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
266 $result = $fetcher->post($req->get_normalized_http_url(),
267 $req->to_postdata());
269 if ($result->status != 200) {
273 parse_str($result->body, $return);
275 return array($return['oauth_token'], $return['oauth_token_secret']);
278 function request_authorization($user, $omb, $token, $secret) {
279 global $config; # for license URL
281 $con = omb_oauth_consumer();
282 $tok = new OAuthToken($token, $secret);
284 $url = omb_service_uri($omb[OAUTH_ENDPOINT_AUTHORIZE]);
286 # XXX: Is this the right thing to do? Strip off GET params and make them
287 # POST params? Seems wrong to me.
289 $parsed = parse_url($url);
291 parse_str($parsed['query'], $params);
293 $req = OAuthRequest::from_consumer_and_token($con, $tok, 'GET', $url, $params);
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.
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);
309 if ($profile->homepage) {
310 $req->set_parameter('omb_listenee_homepage', $profile->homepage);
313 $req->set_parameter('omb_listenee_bio', $profile->bio);
315 if ($profile->location) {
316 $req->set_parameter('omb_listenee_location', $profile->location);
318 $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
320 $req->set_parameter('omb_listenee_avatar', $avatar->url);
323 # XXX: add a nonce to prevent replay attacks
325 $req->set_parameter('oauth_callback', common_local_url('finishremotesubscribe'));
327 # XXX: test to see if endpoint accepts this signature method
329 $req->sign_request(omb_hmac_sha1(), $con, $tok);
331 # store all our info here
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]);
342 common_ensure_session();
344 $_SESSION['oauth_authorization_request'] = $omb;
346 # Redirect to authorization service
348 common_redirect($req->to_url());
352 function make_nonce() {
353 return common_good_rand(16);