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 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('profile', _t('Profile URL'));
51 common_submit('submit', _t('Subscribe'));
52 common_element_end('form');
55 function remote_subscription() {
56 $user = $this->get_user();
59 $this->show_form(_t('No such user!'));
63 $profile = $this->trimmed('profile');
66 $this->show_form(_t('No such user!'));
70 if (!Validate::uri($profile, array('allowed_schemes' => array('http', 'https')))) {
71 $this->show_form(_t('Invalid profile URL (bad format)'));
75 $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
76 $yadis = Auth_Yadis_Yadis::discover($profile, $fetcher);
79 $this->show_form(_t('Not a valid profile URL (no YADIS document).'));
83 $omb = $this->getOmb($yadis);
86 $this->show_form(_t('Not a valid profile URL (incorrect services).'));
90 list($token, $secret) = $this->request_token($omb);
92 if (!$token || !$secret) {
93 $this->show_form(_t('Couldn\'t get a request token.'));
97 $this->request_authorization($user, $omb, $token, $secret);
100 function get_user() {
102 $nickname = $this->trimmed('nickname');
104 $user = User::staticGet('nickname', $nickname);
109 function getOmb($yadis) {
110 static $endpoints = array(OMB_ENDPOINT_UPDATEPROFILE, OMB_ENDPOINT_POSTNOTICE,
111 OAUTH_ENDPOINT_REQUEST, OAUTH_ENDPOINT_AUTHORIZE,
112 OAUTH_ENDPOINT_ACCESS);
114 $services = $yadis->services(); # ordered by priority
115 foreach ($services as $service) {
116 $types = $service->matchTypes($endpoints);
117 foreach ($types as $type) {
118 # We take the first one, since it's the highest priority
119 if (!array_key_exists($type, $omb)) {
120 # URIs is an array, priority-ordered
121 $omb[$type] = $service->getURIs();
122 # Special handling for request
123 if ($type == OAUTH_ENDPOINT_REQUEST) {
124 $nodes = $service->getElements('LocalID');
129 $omb['listener'] = $service->parser->content($nodes[0]);
134 foreach ($endpoints as $ep) {
135 if (!array_key_exists($ep, $omb)) {
139 if (!array_key_exists('listener', $omb)) {
145 function request_token($omb) {
146 $con = omb_oauth_consumer();
148 $url = $omb[OAUTH_ENDPOINT_REQUEST][0];
150 # XXX: Is this the right thing to do? Strip off GET params and make them
151 # POST params? Seems wrong to me.
153 $parsed = parse_url($url);
155 parse_str($parsed['query'], $params);
157 $req = OAuthRequest::from_consumer_and_token($con, NULL, "POST", $url, $params);
159 $req->set_parameter('omb_listener', $omb['listener']);
160 $req->set_parameter('omb_version', OMB_VERSION_01);
162 # XXX: test to see if endpoint accepts this signature method
164 $req->sign_request(omb_hmac_sha1(), $con, NULL);
166 # We re-use this tool's fetcher, since it's pretty good
168 $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
169 $result = $fetcher->post($req->get_normalized_http_url(),
170 $req->to_postdata());
172 if ($result->status != 200) {
176 parse_str($result->body, $return);
178 return array($return['oauth_token'], $return['oauth_token_secret']);
181 function request_authorization($user, $omb, $token, $secret) {
182 global $config; # for license URL
184 $con = omb_oauth_consumer();
185 $tok = new OAuthToken($token, $secret);
187 $url = $omb[OAUTH_ENDPOINT_AUTHORIZE][0];
189 # XXX: Is this the right thing to do? Strip off GET params and make them
190 # POST params? Seems wrong to me.
192 $parsed = parse_url($url);
194 parse_str($parsed['query'], $params);
196 $req = OAuthRequest::from_consumer_and_token($con, $tok, 'GET', $url, $params);
198 # We send over a ton of information. This lets the other
199 # server store info about our user, and it lets the current
200 # user decide if they really want to authorize the subscription.
202 $req->set_parameter('omb_version', OMB_VERSION_01);
203 $req->set_parameter('omb_listener', $omb['listener']);
204 $req->set_parameter('omb_listenee', $user->uri);
205 $req->set_parameter('omb_listenee_profile', common_profile_url($user->nickname));
206 $req->set_parameter('omb_listenee_nickname', $user->nickname);
207 $req->set_parameter('omb_listenee_license', $config['license']['url']);
208 $profile = $user->getProfile();
209 if ($profile->fullname) {
210 $req->set_parameter('omb_listenee_fullname', $profile->fullname);
212 if ($profile->homepage) {
213 $req->set_parameter('omb_listenee_homepage', $profile->homepage);
216 $req->set_parameter('omb_listenee_bio', $profile->bio);
218 if ($profile->location) {
219 $req->set_parameter('omb_listenee_location', $profile->location);
221 $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
223 $req->set_parameter('omb_listenee_avatar', $avatar->url);
226 $nonce = $this->make_nonce();
228 $req->set_parameter('oauth_callback', common_local_url('finishremotesubscribe',
229 array('nonce' => $nonce)));
231 # XXX: test to see if endpoint accepts this signature method
233 $req->sign_request(omb_hmac_sha1(), $con, $tok);
235 # store all our info here
237 $omb['listenee'] = $user->nickname;
238 $omb['token'] = $token;
239 $omb['secret'] = $secret;
241 $_SESSION[$nonce] = $omb;
243 # Redirect to authorization service
245 common_redirect($req->to_url());