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 common_debug('stored request: '.print_r($omb,true), __FILE__);
45 $req = OAuthRequest::from_request();
47 $token = $req->get_parameter('oauth_token');
49 # I think this is the success metric
51 if ($token != $omb['token']) {
52 common_user_error(_t('Not authorized.'));
56 $version = $req->get_parameter('omb_version');
58 if ($version != OMB_VERSION_01) {
59 common_user_error(_t('Unknown version of OMB protocol.'));
63 $nickname = $req->get_parameter('omb_listener_nickname');
66 common_user_error(_t('No nickname provided by remote server.'));
70 $profile_url = $req->get_parameter('omb_listener_profile');
73 common_user_error(_t('No profile URL returned by server.'));
77 if (!Validate::uri($profile_url, array('allowed_schemes' => array('http', 'https')))) {
78 common_user_error(_t('Invalid profile URL returned by server.'));
82 common_debug('listenee: "'.$omb['listenee'].'"', __FILE__);
84 $user = User::staticGet('nickname', $omb['listenee']);
87 common_user_error(_t('User being listened to doesn\'t exist.'));
91 $fullname = $req->get_parameter('omb_listener_fullname');
92 $homepage = $req->get_parameter('omb_listener_homepage');
93 $bio = $req->get_parameter('omb_listener_bio');
94 $location = $req->get_parameter('omb_listener_location');
95 $avatar_url = $req->get_parameter('omb_listener_avatar');
97 list($newtok, $newsecret) = $this->access_token($omb);
99 if (!$newtok || !$newsecret) {
100 common_user_error(_t('Couldn\'t convert request tokens to access tokens.'));
104 # XXX: possible attack point; subscribe and return someone else's profile URI
106 $remote = Remote_profile::staticGet('uri', $omb['listener']);
110 $profile = Profile::staticGet($remote->id);
111 $orig_remote = clone($remote);
112 $orig_profile = clone($profile);
113 # XXX: compare current postNotice and updateProfile URLs to the ones
114 # stored in the DB to avoid (possibly...) above attack
117 $remote = new Remote_profile();
118 $remote->uri = $omb['listener'];
119 $profile = new Profile();
122 $profile->nickname = $nickname;
123 $profile->profileurl = $profile_url;
126 $profile->fullname = $fullname;
129 $profile->homepage = $homepage;
132 $profile->bio = $bio;
135 $profile->location = $location;
139 $profile->update($orig_profile);
141 $profile->created = DB_DataObject_Cast::dateTime(); # current time
142 $id = $profile->insert();
147 $this->add_avatar($profile, $avatar_url);
150 $remote->postnoticeurl = $omb[OMB_ENDPOINT_POSTNOTICE];
151 $remote->updateprofileurl = $omb[OMB_ENDPOINT_UPDATEPROFILE];
154 $remote->update($orig_remote);
156 $remote->created = DB_DataObject_Cast::dateTime(); # current time
160 $sub = new Subscription();
161 $sub->subscriber = $remote->id;
162 $sub->subscribed = $user->id;
163 $sub->token = $newtok;
164 $sub->secret = $newsecret;
165 $sub->created = DB_DataObject_Cast::dateTime(); # current time
167 if (!$sub->insert()) {
168 common_user_error(_t('Couldn\'t insert new subscription.'));
173 unset($_SESSION['oauth_authorization_request']);
175 # If we show subscriptions in reverse chron order, this should
176 # show up close to the top of the page
178 common_redirect(common_local_url('subscribed', array('nickname' =>
182 function add_avatar($profile, $url) {
183 $temp_filename = tempnam(sys_get_temp_dir(), 'listener_avatar');
184 copy($url, $temp_filename);
185 return $profile->setOriginal($temp_filename);
188 function access_token($omb) {
190 $con = omb_oauth_consumer();
191 $tok = new OAuthToken($omb['token'], $omb['secret']);
193 $url = omb_service_uri($omb[OAUTH_ENDPOINT_ACCESS]);
195 # XXX: Is this the right thing to do? Strip off GET params and make them
196 # POST params? Seems wrong to me.
198 $parsed = parse_url($url);
200 parse_str($parsed['query'], $params);
202 $req = OAuthRequest::from_consumer_and_token($con, $tok, "POST", $url, $params);
204 $req->set_parameter('omb_version', OMB_VERSION_01);
206 # XXX: test to see if endpoint accepts this signature method
208 $req->sign_request(omb_hmac_sha1(), $con, NULL);
210 # We re-use this tool's fetcher, since it's pretty good
212 $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
213 $result = $fetcher->post($req->get_normalized_http_url(),
214 $req->to_postdata());
216 if ($result->status != 200) {
220 parse_str($result->body, $return);
222 return array($return['oauth_token'], $return['oauth_token_secret']);