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 $nonce = $this->trimmed('nonce');
39 common_user_error(_t('No nonce returned!'));
43 $omb = $_SESSION[$nonce];
46 common_user_error(_t('Not expecting this response!'));
50 $req = OAuthRequest::from_request();
52 $token = $req->get_parameter('oauth_token');
54 # I think this is the success metric
56 if ($token != $omb['token']) {
57 common_user_error(_t('Not authorized.'));
61 $version = $req->get_parameter('omb_version');
63 if ($version != OMB_VERSION_01) {
64 common_user_error(_t('Unknown version of OMB protocol.'));
68 $nickname = $req->get_parameter('omb_listener_nickname');
71 common_user_error(_t('No nickname provided by remote server.'));
75 $profile_url = $req->get_parameter('omb_listener_profile');
78 common_user_error(_t('No profile URL returned by server.'));
82 if (!Validate::uri($profile_url, array('allowed_schemes' => array('http', 'https')))) {
83 common_user_error(_t('Invalid profile URL returned by server.'));
87 $user = User::staticGet('uri', $omb['listenee']);
90 common_user_error(_t('User being listened to doesn\'t exist.'));
94 $fullname = $req->get_parameter('omb_listener_fullname');
95 $homepage = $req->get_parameter('omb_listener_homepage');
96 $bio = $req->get_parameter('omb_listener_bio');
97 $location = $req->get_parameter('omb_listener_location');
98 $avatar_url = $req->get_parameter('omb_listener_avatar');
100 list($newtok, $newsecret) = $this->access_token($omb);
102 if (!$newtok || !$newsecret) {
103 common_user_error(_t('Couldn\'t convert request tokens to access tokens.'));
107 # XXX: possible attack point; subscribe and return someone else's profile URI
109 $remote = Remote_profile::staticGet('uri', $omb['listener']);
113 $profile = Profile::staticGet($remote->id);
114 $orig_remote = clone($remote);
115 $orig_profile = clone($profile);
116 # XXX: compare current postNotice and updateProfile URLs to the ones
117 # stored in the DB to avoid (possibly...) above attack
120 $remote = new Remote_profile();
121 $remote->uri = $omb['listener'];
122 $profile = new Profile();
125 $profile->nickname = $nickname;
126 $profile->profileurl = $profile_url;
129 $profile->fullname = $fullname;
132 $profile->homepage = $homepage;
135 $profile->bio = $bio;
138 $profile->location = $location;
142 $profile->update($orig_profile);
144 $profile->created = DB_DataObject_Cast::dateTime(); # current time
145 $id = $profile->insert();
150 $this->add_avatar($avatar_url);
153 $remote->postnoticeurl = $omb[OMB_ENDPOINT_POSTNOTICE];
154 $remote->updateprofileurl = $omb[OMB_ENDPOINT_UPDATEPROFILE];
157 $remote->update($orig_remote);
159 $remote->created = DB_DataObject_Cast::dateTime(); # current time
163 $sub = new Subscription();
164 $sub->subscriber = $remote->id;
165 $sub->subscribed = $user->id;
166 $sub->token = $newtok;
167 $sub->secret = $newsecret;
168 $sub->created = DB_DataObject_Cast::dateTime(); # current time
170 if (!$sub->insert()) {
171 common_user_error(_t('Couldn\'t insert new subscription.'));
176 unset($_SESSION[$nonce]);
178 # If we show subscriptions in reverse chron order, this should
179 # show up close to the top of the page
181 common_redirect(common_local_url('subscribed', array('nickname' =>
185 function access_token($omb) {
187 $con = omb_oauth_consumer();
188 $tok = new OAuthToken($omb['token'], $omb['secret']);
190 $url = $omb[OAUTH_ENDPOINT_ACCESS][0];
192 # XXX: Is this the right thing to do? Strip off GET params and make them
193 # POST params? Seems wrong to me.
195 $parsed = parse_url($url);
197 parse_str($parsed['query'], $params);
199 $req = OAuthRequest::from_consumer_and_token($con, $tok, "POST", $url, $params);
201 $req->set_parameter('omb_version', OMB_VERSION_01);
203 # XXX: test to see if endpoint accepts this signature method
205 $req->sign_request(omb_hmac_sha1(), $con, NULL);
207 # We re-use this tool's fetcher, since it's pretty good
209 $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
210 $result = $fetcher->post($req->get_normalized_http_url(),
211 $req->to_postdata());
213 if ($result->status != 200) {
217 parse_str($result->body, $return);
219 return array($return['oauth_token'], $return['oauth_token_secret']);