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');
23 require_once('Auth/Yadis/Yadis.php');
25 class RemotesubscribeAction extends Action {
27 function handle($args) {
29 parent::handle($args);
31 if (common_logged_in()) {
32 common_user_error(_t('You can use the local subscription!'));
36 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
37 $this->remote_subscription();
43 function show_form($err=NULL) {
44 $nickname = $this->trimmed('nickname');
45 common_show_header(_t('Remote subscribe'));
47 common_element('div', 'error', $err);
49 common_element_start('form', array('id' => 'remotesubscribe', 'method' => 'POST',
50 'action' => common_local_url('remotesubscribe')));
51 common_input('nickname', _t('User nickname'), $nickname);
52 common_input('profile', _t('Profile URL'));
53 common_submit('submit', _t('Subscribe'));
54 common_element_end('form');
58 function remote_subscription() {
59 $user = $this->get_user();
62 $this->show_form(_t('No such user!'));
66 $profile = $this->trimmed('profile');
69 $this->show_form(_t('No such user!'));
73 if (!Validate::uri($profile, array('allowed_schemes' => array('http', 'https')))) {
74 $this->show_form(_t('Invalid profile URL (bad format)'));
78 $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
79 $yadis = Auth_Yadis_Yadis::discover($profile, $fetcher);
81 common_debug('remotesubscribe.php: XRDS discovery failure? "'.$yadis->failed.'"');
83 if (!$yadis || $yadis->failed) {
84 $this->show_form(_t('Not a valid profile URL (no YADIS document).'));
88 $xrds =& Auth_Yadis_XRDS::parseXRDS($yadis->response_text);
91 $this->show_form(_t('Not a valid profile URL (no XRDS defined).'));
95 common_debug('remotesubscribe.php: XRDS is "'.print_r($xrds,TRUE).'"');
97 $omb = $this->getOmb($xrds);
100 $this->show_form(_t('Not a valid profile URL (incorrect services).'));
104 list($token, $secret) = $this->request_token($omb);
106 if (!$token || !$secret) {
107 $this->show_form(_t('Couldn\'t get a request token.'));
111 $this->request_authorization($user, $omb, $token, $secret);
114 function get_user() {
116 $nickname = $this->trimmed('nickname');
118 $user = User::staticGet('nickname', $nickname);
123 function getOmb($xrds) {
125 static $omb_endpoints = array(OMB_ENDPOINT_UPDATEPROFILE, OMB_ENDPOINT_POSTNOTICE);
126 static $oauth_endpoints = array(OAUTH_ENDPOINT_REQUEST, OAUTH_ENDPOINT_AUTHORIZE,
127 OAUTH_ENDPOINT_ACCESS);
130 # XXX: the following code could probably be refactored to eliminate dupes
132 $oauth_service = $xrds->services(omb_match_service(OAUTH_DISCOVERY));
134 if (!$oauth_service) {
138 $xrd = $this->getXRD($oauth_service, $xrds);
139 $this->addServices($xrd, $oauth_endpoints, $omb);
141 $omb_service = $xrds->services(omb_match_service(OMB_NAMESPACE));
147 $xrd = $this->getXRD($omb_service, $xrds);
148 $this->addServices($xrd, $omb_endpoints, $omb);
150 # XXX: check that we got all the services we needed
152 foreach (array_merge($omb_endpoints, $oauth_endpoints) as $type) {
153 if (!array_key_exists($type, $omb)) {
158 if (!omb_local_id($omb[OAUTH_ENDPOINT_REQUEST])) {
165 function getXRD($main_service, $main_xrds) {
166 $uri = omb_service_uri($main_service);
167 if (strpos($uri, "#") !== 0) {
168 # FIXME: more rigorous handling of external service definitions
171 $id = substr($uri, 1);
172 $nodes = $main_xrds->allXrdNodes;
173 $parser = $main_xrds->parser;
174 foreach ($nodes as $node) {
175 $attrs = $parser->attributes($node);
176 if (array_key_exists('xml:id', $attrs) &&
177 $attrs['xml:id'] == $id) {
178 return new Auth_Yadis_XRDS($parser, array($node));
184 function addServices($xrd, $types, &$omb) {
185 foreach ($types as $type) {
186 $matches = $xrd->services(omb_match_service($type));
188 $omb[$type] = $services[0];
197 function request_token($omb) {
198 $con = omb_oauth_consumer();
200 $url = omb_service_uri($omb[OAUTH_ENDPOINT_REQUEST]);
202 # XXX: Is this the right thing to do? Strip off GET params and make them
203 # POST params? Seems wrong to me.
205 $parsed = parse_url($url);
207 parse_str($parsed['query'], $params);
209 $req = OAuthRequest::from_consumer_and_token($con, NULL, "POST", $url, $params);
211 $listener = omb_local_id($omb[OAUTH_ENDPOINT_REQUEST]);
217 $req->set_parameter('omb_listener', $listener);
218 $req->set_parameter('omb_version', OMB_VERSION_01);
220 # XXX: test to see if endpoint accepts this signature method
222 $req->sign_request(omb_hmac_sha1(), $con, NULL);
224 # We re-use this tool's fetcher, since it's pretty good
226 $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
227 $result = $fetcher->post($req->get_normalized_http_url(),
228 $req->to_postdata());
230 if ($result->status != 200) {
234 parse_str($result->body, $return);
236 return array($return['oauth_token'], $return['oauth_token_secret']);
239 function request_authorization($user, $omb, $token, $secret) {
240 global $config; # for license URL
242 $con = omb_oauth_consumer();
243 $tok = new OAuthToken($token, $secret);
245 $url = omb_service_uri($omb[OAUTH_ENDPOINT_AUTHORIZE]);
247 # XXX: Is this the right thing to do? Strip off GET params and make them
248 # POST params? Seems wrong to me.
250 $parsed = parse_url($url);
252 parse_str($parsed['query'], $params);
254 $req = OAuthRequest::from_consumer_and_token($con, $tok, 'GET', $url, $params);
256 # We send over a ton of information. This lets the other
257 # server store info about our user, and it lets the current
258 # user decide if they really want to authorize the subscription.
260 $req->set_parameter('omb_version', OMB_VERSION_01);
261 $req->set_parameter('omb_listener', omb_local_id($omb[OAUTH_ENDPOINT_REQUEST]));
262 $req->set_parameter('omb_listenee', $user->uri);
263 $req->set_parameter('omb_listenee_profile', common_profile_url($user->nickname));
264 $req->set_parameter('omb_listenee_nickname', $user->nickname);
265 $req->set_parameter('omb_listenee_license', $config['license']['url']);
266 $profile = $user->getProfile();
267 if ($profile->fullname) {
268 $req->set_parameter('omb_listenee_fullname', $profile->fullname);
270 if ($profile->homepage) {
271 $req->set_parameter('omb_listenee_homepage', $profile->homepage);
274 $req->set_parameter('omb_listenee_bio', $profile->bio);
276 if ($profile->location) {
277 $req->set_parameter('omb_listenee_location', $profile->location);
279 $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
281 $req->set_parameter('omb_listenee_avatar', $avatar->url);
284 $nonce = $this->make_nonce();
286 $req->set_parameter('oauth_callback', common_local_url('finishremotesubscribe',
287 array('nonce' => $nonce)));
289 # XXX: test to see if endpoint accepts this signature method
291 $req->sign_request(omb_hmac_sha1(), $con, $tok);
293 # store all our info here
295 $omb['listenee'] = $user->nickname;
296 $omb['token'] = $token;
297 $omb['secret'] = $secret;
299 $_SESSION[$nonce] = $omb;
301 # Redirect to authorization service
303 common_redirect($req->to_url());