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 show_form($err=NULL) {
43 $nickname = $this->trimmed('nickname');
44 common_show_header(_t('Remote subscribe'));
46 common_element('div', 'error', $err);
48 common_element_start('form', array('id' => 'remotesubscribe', 'method' => 'POST',
49 'action' => common_local_url('remotesubscribe')));
50 common_input('nickname', _t('User nickname'), $nickname);
51 common_input('profile', _t('Profile URL'));
52 common_submit('submit', _t('Subscribe'));
53 common_element_end('form');
57 function remote_subscription() {
58 $user = $this->get_user();
61 $this->show_form(_t('No such user!'));
65 $profile = $this->trimmed('profile');
68 $this->show_form(_t('No such user!'));
72 if (!Validate::uri($profile, array('allowed_schemes' => array('http', 'https')))) {
73 $this->show_form(_t('Invalid profile URL (bad format)'));
77 $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
78 $yadis = Auth_Yadis_Yadis::discover($profile, $fetcher);
80 if (!$yadis || $yadis->failed) {
81 $this->show_form(_t('Not a valid profile URL (no YADIS document).'));
85 $xrds =& Auth_Yadis_XRDS::parseXRDS($yadis->response_text);
88 $this->show_form(_t('Not a valid profile URL (no XRDS defined).'));
92 $omb = $this->getOmb($xrds);
95 $this->show_form(_t('Not a valid profile URL (incorrect services).'));
99 list($token, $secret) = $this->request_token($omb);
101 if (!$token || !$secret) {
102 $this->show_form(_t('Couldn\'t get a request token.'));
106 $this->request_authorization($user, $omb, $token, $secret);
109 function get_user() {
111 $nickname = $this->trimmed('nickname');
113 $user = User::staticGet('nickname', $nickname);
118 function getOmb($xrds) {
120 static $omb_endpoints = array(OMB_ENDPOINT_UPDATEPROFILE, OMB_ENDPOINT_POSTNOTICE);
121 static $oauth_endpoints = array(OAUTH_ENDPOINT_REQUEST, OAUTH_ENDPOINT_AUTHORIZE,
122 OAUTH_ENDPOINT_ACCESS);
125 # XXX: the following code could probably be refactored to eliminate dupes
127 $oauth_services = omb_get_services($xrds, OAUTH_DISCOVERY);
129 if (!$oauth_services) {
133 $oauth_service = $oauth_services[0];
135 $oauth_xrd = $this->getXRD($oauth_service, $xrds);
141 if (!$this->addServices($oauth_xrd, $oauth_endpoints, $omb)) {
145 $omb_services = omb_get_services($xrds, OMB_NAMESPACE);
147 if (!$omb_services) {
151 $omb_service = $omb_services[0];
153 $omb_xrd = $this->getXRD($omb_service, $xrds);
159 if (!$this->addServices($omb_xrd, $omb_endpoints, $omb)) {
163 # XXX: check that we got all the services we needed
165 foreach (array_merge($omb_endpoints, $oauth_endpoints) as $type) {
166 if (!array_key_exists($type, $omb) || !$omb[$type]) {
171 if (!omb_local_id($omb[OAUTH_ENDPOINT_REQUEST])) {
178 function getXRD($main_service, $main_xrds) {
179 $uri = omb_service_uri($main_service);
180 if (strpos($uri, "#") !== 0) {
181 # FIXME: more rigorous handling of external service definitions
184 $id = substr($uri, 1);
185 $nodes = $main_xrds->allXrdNodes;
186 $parser = $main_xrds->parser;
187 foreach ($nodes as $node) {
188 $attrs = $parser->attributes($node);
189 if (array_key_exists('xml:id', $attrs) &&
190 $attrs['xml:id'] == $id) {
191 # XXX: trick the constructor into thinking this is the only node
192 $bogus_nodes = array($node);
193 return new Auth_Yadis_XRDS($parser, $bogus_nodes);
199 function addServices($xrd, $types, &$omb) {
200 foreach ($types as $type) {
201 $matches = omb_get_services($xrd, $type);
203 $omb[$type] = $matches[0];
212 function request_token($omb) {
213 $con = omb_oauth_consumer();
215 $url = omb_service_uri($omb[OAUTH_ENDPOINT_REQUEST]);
217 # XXX: Is this the right thing to do? Strip off GET params and make them
218 # POST params? Seems wrong to me.
220 $parsed = parse_url($url);
222 parse_str($parsed['query'], $params);
224 $req = OAuthRequest::from_consumer_and_token($con, NULL, "POST", $url, $params);
226 $listener = omb_local_id($omb[OAUTH_ENDPOINT_REQUEST]);
232 $req->set_parameter('omb_listener', $listener);
233 $req->set_parameter('omb_version', OMB_VERSION_01);
235 # XXX: test to see if endpoint accepts this signature method
237 $req->sign_request(omb_hmac_sha1(), $con, NULL);
239 # We re-use this tool's fetcher, since it's pretty good
241 $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
243 $result = $fetcher->post($req->get_normalized_http_url(),
244 $req->to_postdata());
246 if ($result->status != 200) {
250 parse_str($result->body, $return);
252 return array($return['oauth_token'], $return['oauth_token_secret']);
255 function request_authorization($user, $omb, $token, $secret) {
256 global $config; # for license URL
258 $con = omb_oauth_consumer();
259 $tok = new OAuthToken($token, $secret);
261 $url = omb_service_uri($omb[OAUTH_ENDPOINT_AUTHORIZE]);
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, $tok, 'GET', $url, $params);
272 # We send over a ton of information. This lets the other
273 # server store info about our user, and it lets the current
274 # user decide if they really want to authorize the subscription.
276 $req->set_parameter('omb_version', OMB_VERSION_01);
277 $req->set_parameter('omb_listener', omb_local_id($omb[OAUTH_ENDPOINT_REQUEST]));
278 $req->set_parameter('omb_listenee', $user->uri);
279 $req->set_parameter('omb_listenee_profile', common_profile_url($user->nickname));
280 $req->set_parameter('omb_listenee_nickname', $user->nickname);
281 $req->set_parameter('omb_listenee_license', $config['license']['url']);
282 $profile = $user->getProfile();
283 if ($profile->fullname) {
284 $req->set_parameter('omb_listenee_fullname', $profile->fullname);
286 if ($profile->homepage) {
287 $req->set_parameter('omb_listenee_homepage', $profile->homepage);
290 $req->set_parameter('omb_listenee_bio', $profile->bio);
292 if ($profile->location) {
293 $req->set_parameter('omb_listenee_location', $profile->location);
295 $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
297 $req->set_parameter('omb_listenee_avatar', $avatar->url);
300 # XXX: add a nonce to prevent replay attacks
302 $req->set_parameter('oauth_callback', common_local_url('finishremotesubscribe'));
304 # XXX: test to see if endpoint accepts this signature method
306 $req->sign_request(omb_hmac_sha1(), $con, $tok);
308 # store all our info here
310 $omb['listenee'] = $user->nickname;
311 $omb['token'] = $token;
312 $omb['secret'] = $secret;
313 # call doesn't work after bounce back so we cache; maybe serialization issue...?
314 $omb['access_token_url'] = omb_service_uri($omb[OAUTH_ENDPOINT_ACCESS]);
315 $omb['post_notice_url'] = omb_service_uri($omb[OMB_ENDPOINT_POSTNOTICE]);
316 $omb['update_profile_url'] = omb_service_uri($omb[OMB_ENDPOINT_UPDATEPROFILE]);
318 $_SESSION['oauth_authorization_request'] = $omb;
320 # Redirect to authorization service
322 common_redirect($req->to_url());
326 function make_nonce() {
327 return common_good_rand(16);