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 $profile = $this->trimmed('profile_url');
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 _t('Nickname of the user you want to follow'));
53 common_input('profile_url', _t('Profile URL'), $profile,
54 _t('URL of your profile on another compatible microblogging service'));
55 common_submit('submit', _t('Subscribe'));
56 common_element_end('form');
60 function remote_subscription() {
61 $user = $this->get_user();
64 $this->show_form(_t('No such user!'));
68 $profile = $this->trimmed('profile_url');
71 $this->show_form(_t('No such user!'));
75 if (!Validate::uri($profile, array('allowed_schemes' => array('http', 'https')))) {
76 $this->show_form(_t('Invalid profile URL (bad format)'));
80 $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
81 $yadis = Auth_Yadis_Yadis::discover($profile, $fetcher);
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 $omb = $this->getOmb($xrds);
98 $this->show_form(_t('Not a valid profile URL (incorrect services).'));
102 list($token, $secret) = $this->request_token($omb);
104 if (!$token || !$secret) {
105 $this->show_form(_t('Couldn\'t get a request token.'));
109 $this->request_authorization($user, $omb, $token, $secret);
112 function get_user() {
114 $nickname = $this->trimmed('nickname');
116 $user = User::staticGet('nickname', $nickname);
121 function getOmb($xrds) {
123 static $omb_endpoints = array(OMB_ENDPOINT_UPDATEPROFILE, OMB_ENDPOINT_POSTNOTICE);
124 static $oauth_endpoints = array(OAUTH_ENDPOINT_REQUEST, OAUTH_ENDPOINT_AUTHORIZE,
125 OAUTH_ENDPOINT_ACCESS);
128 # XXX: the following code could probably be refactored to eliminate dupes
130 $oauth_services = omb_get_services($xrds, OAUTH_DISCOVERY);
132 if (!$oauth_services) {
136 $oauth_service = $oauth_services[0];
138 $oauth_xrd = $this->getXRD($oauth_service, $xrds);
144 if (!$this->addServices($oauth_xrd, $oauth_endpoints, $omb)) {
148 $omb_services = omb_get_services($xrds, OMB_NAMESPACE);
150 if (!$omb_services) {
154 $omb_service = $omb_services[0];
156 $omb_xrd = $this->getXRD($omb_service, $xrds);
162 if (!$this->addServices($omb_xrd, $omb_endpoints, $omb)) {
166 # XXX: check that we got all the services we needed
168 foreach (array_merge($omb_endpoints, $oauth_endpoints) as $type) {
169 if (!array_key_exists($type, $omb) || !$omb[$type]) {
174 if (!omb_local_id($omb[OAUTH_ENDPOINT_REQUEST])) {
181 function getXRD($main_service, $main_xrds) {
182 $uri = omb_service_uri($main_service);
183 if (strpos($uri, "#") !== 0) {
184 # FIXME: more rigorous handling of external service definitions
187 $id = substr($uri, 1);
188 $nodes = $main_xrds->allXrdNodes;
189 $parser = $main_xrds->parser;
190 foreach ($nodes as $node) {
191 $attrs = $parser->attributes($node);
192 if (array_key_exists('xml:id', $attrs) &&
193 $attrs['xml:id'] == $id) {
194 # XXX: trick the constructor into thinking this is the only node
195 $bogus_nodes = array($node);
196 return new Auth_Yadis_XRDS($parser, $bogus_nodes);
202 function addServices($xrd, $types, &$omb) {
203 foreach ($types as $type) {
204 $matches = omb_get_services($xrd, $type);
206 $omb[$type] = $matches[0];
215 function request_token($omb) {
216 $con = omb_oauth_consumer();
218 $url = omb_service_uri($omb[OAUTH_ENDPOINT_REQUEST]);
220 # XXX: Is this the right thing to do? Strip off GET params and make them
221 # POST params? Seems wrong to me.
223 $parsed = parse_url($url);
225 parse_str($parsed['query'], $params);
227 $req = OAuthRequest::from_consumer_and_token($con, NULL, "POST", $url, $params);
229 $listener = omb_local_id($omb[OAUTH_ENDPOINT_REQUEST]);
235 $req->set_parameter('omb_listener', $listener);
236 $req->set_parameter('omb_version', OMB_VERSION_01);
238 # XXX: test to see if endpoint accepts this signature method
240 $req->sign_request(omb_hmac_sha1(), $con, NULL);
242 # We re-use this tool's fetcher, since it's pretty good
244 $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
246 $result = $fetcher->post($req->get_normalized_http_url(),
247 $req->to_postdata());
249 if ($result->status != 200) {
253 parse_str($result->body, $return);
255 return array($return['oauth_token'], $return['oauth_token_secret']);
258 function request_authorization($user, $omb, $token, $secret) {
259 global $config; # for license URL
261 $con = omb_oauth_consumer();
262 $tok = new OAuthToken($token, $secret);
264 $url = omb_service_uri($omb[OAUTH_ENDPOINT_AUTHORIZE]);
266 # XXX: Is this the right thing to do? Strip off GET params and make them
267 # POST params? Seems wrong to me.
269 $parsed = parse_url($url);
271 parse_str($parsed['query'], $params);
273 $req = OAuthRequest::from_consumer_and_token($con, $tok, 'GET', $url, $params);
275 # We send over a ton of information. This lets the other
276 # server store info about our user, and it lets the current
277 # user decide if they really want to authorize the subscription.
279 $req->set_parameter('omb_version', OMB_VERSION_01);
280 $req->set_parameter('omb_listener', omb_local_id($omb[OAUTH_ENDPOINT_REQUEST]));
281 $req->set_parameter('omb_listenee', $user->uri);
282 $req->set_parameter('omb_listenee_profile', common_profile_url($user->nickname));
283 $req->set_parameter('omb_listenee_nickname', $user->nickname);
284 $req->set_parameter('omb_listenee_license', $config['license']['url']);
285 $profile = $user->getProfile();
286 if ($profile->fullname) {
287 $req->set_parameter('omb_listenee_fullname', $profile->fullname);
289 if ($profile->homepage) {
290 $req->set_parameter('omb_listenee_homepage', $profile->homepage);
293 $req->set_parameter('omb_listenee_bio', $profile->bio);
295 if ($profile->location) {
296 $req->set_parameter('omb_listenee_location', $profile->location);
298 $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
300 $req->set_parameter('omb_listenee_avatar', $avatar->url);
303 # XXX: add a nonce to prevent replay attacks
305 $req->set_parameter('oauth_callback', common_local_url('finishremotesubscribe'));
307 # XXX: test to see if endpoint accepts this signature method
309 $req->sign_request(omb_hmac_sha1(), $con, $tok);
311 # store all our info here
313 $omb['listenee'] = $user->nickname;
314 $omb['listener'] = omb_local_id($omb[OAUTH_ENDPOINT_REQUEST]);
315 $omb['token'] = $token;
316 $omb['secret'] = $secret;
317 # call doesn't work after bounce back so we cache; maybe serialization issue...?
318 $omb['access_token_url'] = omb_service_uri($omb[OAUTH_ENDPOINT_ACCESS]);
319 $omb['post_notice_url'] = omb_service_uri($omb[OMB_ENDPOINT_POSTNOTICE]);
320 $omb['update_profile_url'] = omb_service_uri($omb[OMB_ENDPOINT_UPDATEPROFILE]);
322 $_SESSION['oauth_authorization_request'] = $omb;
324 # Redirect to authorization service
326 common_redirect($req->to_url());
330 function make_nonce() {
331 return common_good_rand(16);