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);
82 $this->show_form(_t('Not a valid profile URL (no YADIS document).'));
88 $omb = $this->getOmb($yadis);
91 $this->show_form(_t('Not a valid profile URL (incorrect services).'));
95 list($token, $secret) = $this->request_token($omb);
97 if (!$token || !$secret) {
98 $this->show_form(_t('Couldn\'t get a request token.'));
102 $this->request_authorization($user, $omb, $token, $secret);
105 function get_user() {
107 $nickname = $this->trimmed('nickname');
109 $user = User::staticGet('nickname', $nickname);
114 function getOmb($yadis) {
115 static $endpoints = array(OMB_ENDPOINT_UPDATEPROFILE, OMB_ENDPOINT_POSTNOTICE,
116 OAUTH_ENDPOINT_REQUEST, OAUTH_ENDPOINT_AUTHORIZE,
117 OAUTH_ENDPOINT_ACCESS);
119 $services = $yadis->services(); # ordered by priority
124 foreach ($services as $service) {
125 $types = $service->matchTypes($endpoints);
126 foreach ($types as $type) {
127 # We take the first one, since it's the highest priority
128 if (!array_key_exists($type, $omb)) {
129 # URIs is an array, priority-ordered
130 $omb[$type] = $service->getURIs();
131 # Special handling for request
132 if ($type == OAUTH_ENDPOINT_REQUEST) {
133 $nodes = $service->getElements('LocalID');
138 $omb['listener'] = $service->parser->content($nodes[0]);
143 foreach ($endpoints as $ep) {
144 if (!array_key_exists($ep, $omb)) {
148 if (!array_key_exists('listener', $omb)) {
154 function request_token($omb) {
155 $con = omb_oauth_consumer();
157 $url = $omb[OAUTH_ENDPOINT_REQUEST][0];
159 # XXX: Is this the right thing to do? Strip off GET params and make them
160 # POST params? Seems wrong to me.
162 $parsed = parse_url($url);
164 parse_str($parsed['query'], $params);
166 $req = OAuthRequest::from_consumer_and_token($con, NULL, "POST", $url, $params);
168 $req->set_parameter('omb_listener', $omb['listener']);
169 $req->set_parameter('omb_version', OMB_VERSION_01);
171 # XXX: test to see if endpoint accepts this signature method
173 $req->sign_request(omb_hmac_sha1(), $con, NULL);
175 # We re-use this tool's fetcher, since it's pretty good
177 $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
178 $result = $fetcher->post($req->get_normalized_http_url(),
179 $req->to_postdata());
181 if ($result->status != 200) {
185 parse_str($result->body, $return);
187 return array($return['oauth_token'], $return['oauth_token_secret']);
190 function request_authorization($user, $omb, $token, $secret) {
191 global $config; # for license URL
193 $con = omb_oauth_consumer();
194 $tok = new OAuthToken($token, $secret);
196 $url = $omb[OAUTH_ENDPOINT_AUTHORIZE][0];
198 # XXX: Is this the right thing to do? Strip off GET params and make them
199 # POST params? Seems wrong to me.
201 $parsed = parse_url($url);
203 parse_str($parsed['query'], $params);
205 $req = OAuthRequest::from_consumer_and_token($con, $tok, 'GET', $url, $params);
207 # We send over a ton of information. This lets the other
208 # server store info about our user, and it lets the current
209 # user decide if they really want to authorize the subscription.
211 $req->set_parameter('omb_version', OMB_VERSION_01);
212 $req->set_parameter('omb_listener', $omb['listener']);
213 $req->set_parameter('omb_listenee', $user->uri);
214 $req->set_parameter('omb_listenee_profile', common_profile_url($user->nickname));
215 $req->set_parameter('omb_listenee_nickname', $user->nickname);
216 $req->set_parameter('omb_listenee_license', $config['license']['url']);
217 $profile = $user->getProfile();
218 if ($profile->fullname) {
219 $req->set_parameter('omb_listenee_fullname', $profile->fullname);
221 if ($profile->homepage) {
222 $req->set_parameter('omb_listenee_homepage', $profile->homepage);
225 $req->set_parameter('omb_listenee_bio', $profile->bio);
227 if ($profile->location) {
228 $req->set_parameter('omb_listenee_location', $profile->location);
230 $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
232 $req->set_parameter('omb_listenee_avatar', $avatar->url);
235 $nonce = $this->make_nonce();
237 $req->set_parameter('oauth_callback', common_local_url('finishremotesubscribe',
238 array('nonce' => $nonce)));
240 # XXX: test to see if endpoint accepts this signature method
242 $req->sign_request(omb_hmac_sha1(), $con, $tok);
244 # store all our info here
246 $omb['listenee'] = $user->nickname;
247 $omb['token'] = $token;
248 $omb['secret'] = $secret;
250 $_SESSION[$nonce] = $omb;
252 # Redirect to authorization service
254 common_redirect($req->to_url());