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(_t('You can use the local subscription!'));
35 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
36 $this->remote_subscription();
42 function get_instructions() {
43 return _t('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('p', '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(_t('Remote subscribe'), NULL, $err,
66 array($this, 'show_top'));
67 common_element_start('form', array('id' => 'remotesubscribe', 'method' => 'post',
68 'action' => common_local_url('remotesubscribe')));
69 common_input('nickname', _t('User nickname'), $nickname,
70 _t('Nickname of the user you want to follow'));
71 common_input('profile_url', _t('Profile URL'), $profile,
72 _t('URL of your profile on another compatible microblogging service'));
73 common_submit('submit', _t('Subscribe'));
74 common_element_end('form');
78 function remote_subscription() {
79 $user = $this->get_user();
82 $this->show_form(_t('No such user!'));
86 $profile = $this->trimmed('profile_url');
89 $this->show_form(_t('No such user!'));
93 if (!Validate::uri($profile, array('allowed_schemes' => array('http', 'https')))) {
94 $this->show_form(_t('Invalid profile URL (bad format)'));
98 $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
99 $yadis = Auth_Yadis_Yadis::discover($profile, $fetcher);
101 if (!$yadis || $yadis->failed) {
102 $this->show_form(_t('Not a valid profile URL (no YADIS document).'));
106 $xrds =& Auth_Yadis_XRDS::parseXRDS($yadis->response_text);
109 $this->show_form(_t('Not a valid profile URL (no XRDS defined).'));
113 $omb = $this->getOmb($xrds);
116 $this->show_form(_t('Not a valid profile URL (incorrect services).'));
120 list($token, $secret) = $this->request_token($omb);
122 if (!$token || !$secret) {
123 $this->show_form(_t('Couldn\'t get a request token.'));
127 $this->request_authorization($user, $omb, $token, $secret);
130 function get_user() {
132 $nickname = $this->trimmed('nickname');
134 $user = User::staticGet('nickname', $nickname);
139 function getOmb($xrds) {
141 static $omb_endpoints = array(OMB_ENDPOINT_UPDATEPROFILE, OMB_ENDPOINT_POSTNOTICE);
142 static $oauth_endpoints = array(OAUTH_ENDPOINT_REQUEST, OAUTH_ENDPOINT_AUTHORIZE,
143 OAUTH_ENDPOINT_ACCESS);
146 # XXX: the following code could probably be refactored to eliminate dupes
148 $oauth_services = omb_get_services($xrds, OAUTH_DISCOVERY);
150 if (!$oauth_services) {
154 $oauth_service = $oauth_services[0];
156 $oauth_xrd = $this->getXRD($oauth_service, $xrds);
162 if (!$this->addServices($oauth_xrd, $oauth_endpoints, $omb)) {
166 $omb_services = omb_get_services($xrds, OMB_NAMESPACE);
168 if (!$omb_services) {
172 $omb_service = $omb_services[0];
174 $omb_xrd = $this->getXRD($omb_service, $xrds);
180 if (!$this->addServices($omb_xrd, $omb_endpoints, $omb)) {
184 # XXX: check that we got all the services we needed
186 foreach (array_merge($omb_endpoints, $oauth_endpoints) as $type) {
187 if (!array_key_exists($type, $omb) || !$omb[$type]) {
192 if (!omb_local_id($omb[OAUTH_ENDPOINT_REQUEST])) {
199 function getXRD($main_service, $main_xrds) {
200 $uri = omb_service_uri($main_service);
201 if (strpos($uri, "#") !== 0) {
202 # FIXME: more rigorous handling of external service definitions
205 $id = substr($uri, 1);
206 $nodes = $main_xrds->allXrdNodes;
207 $parser = $main_xrds->parser;
208 foreach ($nodes as $node) {
209 $attrs = $parser->attributes($node);
210 if (array_key_exists('xml:id', $attrs) &&
211 $attrs['xml:id'] == $id) {
212 # XXX: trick the constructor into thinking this is the only node
213 $bogus_nodes = array($node);
214 return new Auth_Yadis_XRDS($parser, $bogus_nodes);
220 function addServices($xrd, $types, &$omb) {
221 foreach ($types as $type) {
222 $matches = omb_get_services($xrd, $type);
224 $omb[$type] = $matches[0];
233 function request_token($omb) {
234 $con = omb_oauth_consumer();
236 $url = omb_service_uri($omb[OAUTH_ENDPOINT_REQUEST]);
238 # XXX: Is this the right thing to do? Strip off GET params and make them
239 # POST params? Seems wrong to me.
241 $parsed = parse_url($url);
243 parse_str($parsed['query'], $params);
245 $req = OAuthRequest::from_consumer_and_token($con, NULL, "POST", $url, $params);
247 $listener = omb_local_id($omb[OAUTH_ENDPOINT_REQUEST]);
253 $req->set_parameter('omb_listener', $listener);
254 $req->set_parameter('omb_version', OMB_VERSION_01);
256 # XXX: test to see if endpoint accepts this signature method
258 $req->sign_request(omb_hmac_sha1(), $con, NULL);
260 # We re-use this tool's fetcher, since it's pretty good
262 $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
264 $result = $fetcher->post($req->get_normalized_http_url(),
265 $req->to_postdata());
267 if ($result->status != 200) {
271 parse_str($result->body, $return);
273 return array($return['oauth_token'], $return['oauth_token_secret']);
276 function request_authorization($user, $omb, $token, $secret) {
277 global $config; # for license URL
279 $con = omb_oauth_consumer();
280 $tok = new OAuthToken($token, $secret);
282 $url = omb_service_uri($omb[OAUTH_ENDPOINT_AUTHORIZE]);
284 # XXX: Is this the right thing to do? Strip off GET params and make them
285 # POST params? Seems wrong to me.
287 $parsed = parse_url($url);
289 parse_str($parsed['query'], $params);
291 $req = OAuthRequest::from_consumer_and_token($con, $tok, 'GET', $url, $params);
293 # We send over a ton of information. This lets the other
294 # server store info about our user, and it lets the current
295 # user decide if they really want to authorize the subscription.
297 $req->set_parameter('omb_version', OMB_VERSION_01);
298 $req->set_parameter('omb_listener', omb_local_id($omb[OAUTH_ENDPOINT_REQUEST]));
299 $req->set_parameter('omb_listenee', $user->uri);
300 $req->set_parameter('omb_listenee_profile', common_profile_url($user->nickname));
301 $req->set_parameter('omb_listenee_nickname', $user->nickname);
302 $req->set_parameter('omb_listenee_license', $config['license']['url']);
303 $profile = $user->getProfile();
304 if ($profile->fullname) {
305 $req->set_parameter('omb_listenee_fullname', $profile->fullname);
307 if ($profile->homepage) {
308 $req->set_parameter('omb_listenee_homepage', $profile->homepage);
311 $req->set_parameter('omb_listenee_bio', $profile->bio);
313 if ($profile->location) {
314 $req->set_parameter('omb_listenee_location', $profile->location);
316 $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
318 $req->set_parameter('omb_listenee_avatar', $avatar->url);
321 # XXX: add a nonce to prevent replay attacks
323 $req->set_parameter('oauth_callback', common_local_url('finishremotesubscribe'));
325 # XXX: test to see if endpoint accepts this signature method
327 $req->sign_request(omb_hmac_sha1(), $con, $tok);
329 # store all our info here
331 $omb['listenee'] = $user->nickname;
332 $omb['listener'] = omb_local_id($omb[OAUTH_ENDPOINT_REQUEST]);
333 $omb['token'] = $token;
334 $omb['secret'] = $secret;
335 # call doesn't work after bounce back so we cache; maybe serialization issue...?
336 $omb['access_token_url'] = omb_service_uri($omb[OAUTH_ENDPOINT_ACCESS]);
337 $omb['post_notice_url'] = omb_service_uri($omb[OMB_ENDPOINT_POSTNOTICE]);
338 $omb['update_profile_url'] = omb_service_uri($omb[OMB_ENDPOINT_UPDATEPROFILE]);
340 $_SESSION['oauth_authorization_request'] = $omb;
342 # Redirect to authorization service
344 common_redirect($req->to_url());
348 function make_nonce() {
349 return common_good_rand(16);