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 if (!$yadis || $yadis->failed) {
82 $this->show_form(_t('Not a valid profile URL (no YADIS document).'));
86 $xrds =& Auth_Yadis_XRDS::parseXRDS($yadis->response_text);
89 $this->show_form(_t('Not a valid profile URL (no XRDS defined).'));
93 $omb = $this->getOmb($xrds);
96 $this->show_form(_t('Not a valid profile URL (incorrect services).'));
100 list($token, $secret) = $this->request_token($omb);
102 if (!$token || !$secret) {
103 $this->show_form(_t('Couldn\'t get a request token.'));
107 $this->request_authorization($user, $omb, $token, $secret);
110 function get_user() {
112 $nickname = $this->trimmed('nickname');
114 $user = User::staticGet('nickname', $nickname);
119 function getOmb($xrds) {
121 static $omb_endpoints = array(OMB_ENDPOINT_UPDATEPROFILE, OMB_ENDPOINT_POSTNOTICE);
122 static $oauth_endpoints = array(OAUTH_ENDPOINT_REQUEST, OAUTH_ENDPOINT_AUTHORIZE,
123 OAUTH_ENDPOINT_ACCESS);
126 # XXX: the following code could probably be refactored to eliminate dupes
128 $oauth_services = omb_get_services($xrds, OAUTH_DISCOVERY);
130 if (!$oauth_services) {
134 $oauth_service = $oauth_services[0];
136 $oauth_xrd = $this->getXRD($oauth_service, $xrds);
142 if (!$this->addServices($oauth_xrd, $oauth_endpoints, $omb)) {
146 $omb_services = omb_get_services($xrds, OMB_NAMESPACE);
148 if (!$omb_services) {
152 $omb_service = $omb_services[0];
154 $omb_xrd = $this->getXRD($omb_service, $xrds);
160 if (!$this->addServices($omb_xrd, $omb_endpoints, $omb)) {
164 # XXX: check that we got all the services we needed
166 foreach (array_merge($omb_endpoints, $oauth_endpoints) as $type) {
167 if (!array_key_exists($type, $omb) || !$omb[$type]) {
172 if (!omb_local_id($omb[OAUTH_ENDPOINT_REQUEST])) {
179 function getXRD($main_service, $main_xrds) {
180 $uri = omb_service_uri($main_service);
181 if (strpos($uri, "#") !== 0) {
182 # FIXME: more rigorous handling of external service definitions
185 $id = substr($uri, 1);
186 $nodes = $main_xrds->allXrdNodes;
187 $parser = $main_xrds->parser;
188 foreach ($nodes as $node) {
189 $attrs = $parser->attributes($node);
190 if (array_key_exists('xml:id', $attrs) &&
191 $attrs['xml:id'] == $id) {
192 # XXX: trick the constructor into thinking this is the only node
193 $bogus_nodes = array($node);
194 return new Auth_Yadis_XRDS($parser, $bogus_nodes);
200 function addServices($xrd, $types, &$omb) {
201 foreach ($types as $type) {
202 $matches = omb_get_services($xrd, $type);
204 $omb[$type] = $matches[0];
213 function request_token($omb) {
214 $con = omb_oauth_consumer();
216 $url = omb_service_uri($omb[OAUTH_ENDPOINT_REQUEST]);
218 # XXX: Is this the right thing to do? Strip off GET params and make them
219 # POST params? Seems wrong to me.
221 $parsed = parse_url($url);
223 parse_str($parsed['query'], $params);
225 $req = OAuthRequest::from_consumer_and_token($con, NULL, "POST", $url, $params);
227 $listener = omb_local_id($omb[OAUTH_ENDPOINT_REQUEST]);
233 $req->set_parameter('omb_listener', $listener);
234 $req->set_parameter('omb_version', OMB_VERSION_01);
236 # XXX: test to see if endpoint accepts this signature method
238 $req->sign_request(omb_hmac_sha1(), $con, NULL);
240 # We re-use this tool's fetcher, since it's pretty good
242 $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
244 $result = $fetcher->post($req->get_normalized_http_url(),
245 $req->to_postdata());
247 if ($result->status != 200) {
251 parse_str($result->body, $return);
253 return array($return['oauth_token'], $return['oauth_token_secret']);
256 function request_authorization($user, $omb, $token, $secret) {
257 global $config; # for license URL
259 $con = omb_oauth_consumer();
260 $tok = new OAuthToken($token, $secret);
262 $url = omb_service_uri($omb[OAUTH_ENDPOINT_AUTHORIZE]);
264 # XXX: Is this the right thing to do? Strip off GET params and make them
265 # POST params? Seems wrong to me.
267 $parsed = parse_url($url);
269 parse_str($parsed['query'], $params);
271 $req = OAuthRequest::from_consumer_and_token($con, $tok, 'GET', $url, $params);
273 # We send over a ton of information. This lets the other
274 # server store info about our user, and it lets the current
275 # user decide if they really want to authorize the subscription.
277 $req->set_parameter('omb_version', OMB_VERSION_01);
278 $req->set_parameter('omb_listener', omb_local_id($omb[OAUTH_ENDPOINT_REQUEST]));
279 $req->set_parameter('omb_listenee', $user->uri);
280 $req->set_parameter('omb_listenee_profile', common_profile_url($user->nickname));
281 $req->set_parameter('omb_listenee_nickname', $user->nickname);
282 $req->set_parameter('omb_listenee_license', $config['license']['url']);
283 $profile = $user->getProfile();
284 if ($profile->fullname) {
285 $req->set_parameter('omb_listenee_fullname', $profile->fullname);
287 if ($profile->homepage) {
288 $req->set_parameter('omb_listenee_homepage', $profile->homepage);
291 $req->set_parameter('omb_listenee_bio', $profile->bio);
293 if ($profile->location) {
294 $req->set_parameter('omb_listenee_location', $profile->location);
296 $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
298 $req->set_parameter('omb_listenee_avatar', $avatar->url);
301 # XXX: add a nonce to prevent replay attacks
303 $req->set_parameter('oauth_callback', common_local_url('finishremotesubscribe'));
305 # XXX: test to see if endpoint accepts this signature method
307 $req->sign_request(omb_hmac_sha1(), $con, $tok);
309 # store all our info here
311 $omb['listenee'] = $user->nickname;
312 $omb['token'] = $token;
313 $omb['secret'] = $secret;
315 $_SESSION['oauth_authorization_request'] = $omb;
317 # Redirect to authorization service
319 common_redirect($req->to_url());
323 function make_nonce() {
324 return common_good_rand(16);