3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, 2009, Control Yourself, 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');
24 class FinishremotesubscribeAction extends Action
27 function handle($args)
30 parent::handle($args);
32 if (common_logged_in()) {
33 $this->clientError(_('You can use the local subscription!'));
37 $omb = $_SESSION['oauth_authorization_request'];
40 $this->clientError(_('Not expecting this response!'));
44 common_debug('stored request: '.print_r($omb,true), __FILE__);
46 common_remove_magic_from_request();
47 $req = OAuthRequest::from_request('POST', common_local_url('finishuserauthorization'));
49 $token = $req->get_parameter('oauth_token');
51 # I think this is the success metric
53 if ($token != $omb['token']) {
54 $this->clientError(_('Not authorized.'));
58 $version = $req->get_parameter('omb_version');
60 if ($version != OMB_VERSION_01) {
61 $this->clientError(_('Unknown version of OMB protocol.'));
65 $nickname = $req->get_parameter('omb_listener_nickname');
68 $this->clientError(_('No nickname provided by remote server.'));
72 $profile_url = $req->get_parameter('omb_listener_profile');
75 $this->clientError(_('No profile URL returned by server.'));
79 if (!Validate::uri($profile_url, array('allowed_schemes' => array('http', 'https')))) {
80 $this->clientError(_('Invalid profile URL returned by server.'));
84 if ($profile_url == common_local_url('showstream', array('nickname' => $nickname))) {
85 $this->clientError(_('You can use the local subscription!'));
89 common_debug('listenee: "'.$omb['listenee'].'"', __FILE__);
91 $user = User::staticGet('nickname', $omb['listenee']);
94 $this->clientError(_('User being listened to doesn\'t exist.'));
98 $other = User::staticGet('uri', $omb['listener']);
101 $this->clientError(_('You can use the local subscription!'));
105 $fullname = $req->get_parameter('omb_listener_fullname');
106 $homepage = $req->get_parameter('omb_listener_homepage');
107 $bio = $req->get_parameter('omb_listener_bio');
108 $location = $req->get_parameter('omb_listener_location');
109 $avatar_url = $req->get_parameter('omb_listener_avatar');
111 list($newtok, $newsecret) = $this->access_token($omb);
113 if (!$newtok || !$newsecret) {
114 $this->clientError(_('Couldn\'t convert request tokens to access tokens.'));
118 # XXX: possible attack point; subscribe and return someone else's profile URI
120 $remote = Remote_profile::staticGet('uri', $omb['listener']);
124 $profile = Profile::staticGet($remote->id);
125 $orig_remote = clone($remote);
126 $orig_profile = clone($profile);
127 # XXX: compare current postNotice and updateProfile URLs to the ones
128 # stored in the DB to avoid (possibly...) above attack
131 $remote = new Remote_profile();
132 $remote->uri = $omb['listener'];
133 $profile = new Profile();
136 $profile->nickname = $nickname;
137 $profile->profileurl = $profile_url;
139 if (!is_null($fullname)) {
140 $profile->fullname = $fullname;
142 if (!is_null($homepage)) {
143 $profile->homepage = $homepage;
145 if (!is_null($bio)) {
146 $profile->bio = $bio;
148 if (!is_null($location)) {
149 $profile->location = $location;
153 $profile->update($orig_profile);
155 $profile->created = DB_DataObject_Cast::dateTime(); # current time
156 $id = $profile->insert();
158 $this->serverError(_('Error inserting new profile'));
165 if (!$this->add_avatar($profile, $avatar_url)) {
166 $this->serverError(_('Error inserting avatar'));
171 $remote->postnoticeurl = $omb['post_notice_url'];
172 $remote->updateprofileurl = $omb['update_profile_url'];
175 if (!$remote->update($orig_remote)) {
176 $this->serverError(_('Error updating remote profile'));
180 $remote->created = DB_DataObject_Cast::dateTime(); # current time
181 if (!$remote->insert()) {
182 $this->serverError(_('Error inserting remote profile'));
187 if ($user->hasBlocked($profile)) {
188 $this->clientError(_('That user has blocked you from subscribing.'));
192 $sub = new Subscription();
194 $sub->subscriber = $remote->id;
195 $sub->subscribed = $user->id;
199 if ($sub->find(true)) {
201 $orig_sub = clone($sub);
204 $sub->created = DB_DataObject_Cast::dateTime(); # current time
207 $sub->token = $newtok;
208 $sub->secret = $newsecret;
211 $result = $sub->update($orig_sub);
213 $result = $sub->insert();
217 common_log_db_error($sub, ($sub_exists) ? 'UPDATE' : 'INSERT', __FILE__);
218 $this->clientError(_('Couldn\'t insert new subscription.'));
222 # Notify user, if necessary
224 mail_subscribe_notify_profile($user, $profile);
227 unset($_SESSION['oauth_authorization_request']);
229 # If we show subscriptions in reverse chron order, this should
230 # show up close to the top of the page
232 common_redirect(common_local_url('subscribers', array('nickname' =>
237 function add_avatar($profile, $url)
239 $temp_filename = tempnam(sys_get_temp_dir(), 'listener_avatar');
240 copy($url, $temp_filename);
241 $imagefile = new ImageFile($profile->id, $temp_filename);
242 $filename = Avatar::filename($profile->id,
243 image_type_to_extension($imagefile->type),
246 rename($temp_filename, Avatar::path($filename));
247 return $profile->setOriginal($filename);
250 function access_token($omb)
253 common_debug('starting request for access token', __FILE__);
255 $con = omb_oauth_consumer();
256 $tok = new OAuthToken($omb['token'], $omb['secret']);
258 common_debug('using request token "'.$tok.'"', __FILE__);
260 $url = $omb['access_token_url'];
262 common_debug('using access token url "'.$url.'"', __FILE__);
264 # XXX: Is this the right thing to do? Strip off GET params and make them
265 # POST params? Seems wrong to me.
267 $parsed = parse_url($url);
269 parse_str($parsed['query'], $params);
271 $req = OAuthRequest::from_consumer_and_token($con, $tok, "POST", $url, $params);
273 $req->set_parameter('omb_version', OMB_VERSION_01);
275 # XXX: test to see if endpoint accepts this signature method
277 $req->sign_request(omb_hmac_sha1(), $con, $tok);
279 # We re-use this tool's fetcher, since it's pretty good
281 common_debug('posting to access token url "'.$req->get_normalized_http_url().'"', __FILE__);
282 common_debug('posting request data "'.$req->to_postdata().'"', __FILE__);
284 $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
285 $result = $fetcher->post($req->get_normalized_http_url(),
287 array('User-Agent: Laconica/' . LACONICA_VERSION));
289 common_debug('got result: "'.print_r($result,true).'"', __FILE__);
291 if ($result->status != 200) {
295 parse_str($result->body, $return);
297 return array($return['oauth_token'], $return['oauth_token_secret']);