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 FinishremotesubscribeAction 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 $omb = $_SESSION['oauth_authorization_request'];
39 common_user_error(_t('Not expecting this response!'));
43 $req = OAuthRequest::from_request();
45 $token = $req->get_parameter('oauth_token');
47 # I think this is the success metric
49 if ($token != $omb['token']) {
50 common_user_error(_t('Not authorized.'));
54 $version = $req->get_parameter('omb_version');
56 if ($version != OMB_VERSION_01) {
57 common_user_error(_t('Unknown version of OMB protocol.'));
61 $nickname = $req->get_parameter('omb_listener_nickname');
64 common_user_error(_t('No nickname provided by remote server.'));
68 $profile_url = $req->get_parameter('omb_listener_profile');
71 common_user_error(_t('No profile URL returned by server.'));
75 if (!Validate::uri($profile_url, array('allowed_schemes' => array('http', 'https')))) {
76 common_user_error(_t('Invalid profile URL returned by server.'));
80 $user = User::staticGet('uri', $omb['listenee']);
83 common_user_error(_t('User being listened to doesn\'t exist.'));
87 $fullname = $req->get_parameter('omb_listener_fullname');
88 $homepage = $req->get_parameter('omb_listener_homepage');
89 $bio = $req->get_parameter('omb_listener_bio');
90 $location = $req->get_parameter('omb_listener_location');
91 $avatar_url = $req->get_parameter('omb_listener_avatar');
93 list($newtok, $newsecret) = $this->access_token($omb);
95 if (!$newtok || !$newsecret) {
96 common_user_error(_t('Couldn\'t convert request tokens to access tokens.'));
100 # XXX: possible attack point; subscribe and return someone else's profile URI
102 $remote = Remote_profile::staticGet('uri', $omb['listener']);
106 $profile = Profile::staticGet($remote->id);
107 $orig_remote = clone($remote);
108 $orig_profile = clone($profile);
109 # XXX: compare current postNotice and updateProfile URLs to the ones
110 # stored in the DB to avoid (possibly...) above attack
113 $remote = new Remote_profile();
114 $remote->uri = $omb['listener'];
115 $profile = new Profile();
118 $profile->nickname = $nickname;
119 $profile->profileurl = $profile_url;
122 $profile->fullname = $fullname;
125 $profile->homepage = $homepage;
128 $profile->bio = $bio;
131 $profile->location = $location;
135 $profile->update($orig_profile);
137 $profile->created = DB_DataObject_Cast::dateTime(); # current time
138 $id = $profile->insert();
143 $this->add_avatar($avatar_url);
146 $remote->postnoticeurl = $omb[OMB_ENDPOINT_POSTNOTICE];
147 $remote->updateprofileurl = $omb[OMB_ENDPOINT_UPDATEPROFILE];
150 $remote->update($orig_remote);
152 $remote->created = DB_DataObject_Cast::dateTime(); # current time
156 $sub = new Subscription();
157 $sub->subscriber = $remote->id;
158 $sub->subscribed = $user->id;
159 $sub->token = $newtok;
160 $sub->secret = $newsecret;
161 $sub->created = DB_DataObject_Cast::dateTime(); # current time
163 if (!$sub->insert()) {
164 common_user_error(_t('Couldn\'t insert new subscription.'));
169 unset($_SESSION['oauth_authorization_request']);
171 # If we show subscriptions in reverse chron order, this should
172 # show up close to the top of the page
174 common_redirect(common_local_url('subscribed', array('nickname' =>
178 function access_token($omb) {
180 $con = omb_oauth_consumer();
181 $tok = new OAuthToken($omb['token'], $omb['secret']);
183 $url = omb_service_uri($omb[OAUTH_ENDPOINT_ACCESS]);
185 # XXX: Is this the right thing to do? Strip off GET params and make them
186 # POST params? Seems wrong to me.
188 $parsed = parse_url($url);
190 parse_str($parsed['query'], $params);
192 $req = OAuthRequest::from_consumer_and_token($con, $tok, "POST", $url, $params);
194 $req->set_parameter('omb_version', OMB_VERSION_01);
196 # XXX: test to see if endpoint accepts this signature method
198 $req->sign_request(omb_hmac_sha1(), $con, NULL);
200 # We re-use this tool's fetcher, since it's pretty good
202 $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
203 $result = $fetcher->post($req->get_normalized_http_url(),
204 $req->to_postdata());
206 if ($result->status != 200) {
210 parse_str($result->body, $return);
212 return array($return['oauth_token'], $return['oauth_token_secret']);