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');
24 class FinishremotesubscribeAction extends Action {
26 function handle($args) {
28 parent::handle($args);
30 if (common_logged_in()) {
31 common_user_error(_('You can use the local subscription!'));
35 $omb = $_SESSION['oauth_authorization_request'];
38 common_user_error(_('Not expecting this response!'));
42 common_debug('stored request: '.print_r($omb,true), __FILE__);
45 common_remove_magic_from_request();
46 $req = OAuthRequest::from_request();
48 $token = $req->get_parameter('oauth_token');
50 # I think this is the success metric
52 if ($token != $omb['token']) {
53 common_user_error(_('Not authorized.'));
57 $version = $req->get_parameter('omb_version');
59 if ($version != OMB_VERSION_01) {
60 common_user_error(_('Unknown version of OMB protocol.'));
64 $nickname = $req->get_parameter('omb_listener_nickname');
67 common_user_error(_('No nickname provided by remote server.'));
71 $profile_url = $req->get_parameter('omb_listener_profile');
74 common_user_error(_('No profile URL returned by server.'));
78 if (!Validate::uri($profile_url, array('allowed_schemes' => array('http', 'https')))) {
79 common_user_error(_('Invalid profile URL returned by server.'));
83 if ($profile_url == common_local_url('showstream', array('nickname' => $nickname))) {
84 common_user_error(_('You can use the local subscription!'));
88 common_debug('listenee: "'.$omb['listenee'].'"', __FILE__);
90 $user = User::staticGet('nickname', $omb['listenee']);
93 common_user_error(_('User being listened to doesn\'t exist.'));
97 $other = User::staticGet('uri', $omb['listener']);
100 common_user_error(_('You can use the local subscription!'));
104 $fullname = $req->get_parameter('omb_listener_fullname');
105 $homepage = $req->get_parameter('omb_listener_homepage');
106 $bio = $req->get_parameter('omb_listener_bio');
107 $location = $req->get_parameter('omb_listener_location');
108 $avatar_url = $req->get_parameter('omb_listener_avatar');
110 list($newtok, $newsecret) = $this->access_token($omb);
112 if (!$newtok || !$newsecret) {
113 common_user_error(_('Couldn\'t convert request tokens to access tokens.'));
117 # XXX: possible attack point; subscribe and return someone else's profile URI
119 $remote = Remote_profile::staticGet('uri', $omb['listener']);
123 $profile = Profile::staticGet($remote->id);
124 $orig_remote = clone($remote);
125 $orig_profile = clone($profile);
126 # XXX: compare current postNotice and updateProfile URLs to the ones
127 # stored in the DB to avoid (possibly...) above attack
130 $remote = new Remote_profile();
131 $remote->uri = $omb['listener'];
132 $profile = new Profile();
135 $profile->nickname = $nickname;
136 $profile->profileurl = $profile_url;
139 $profile->fullname = $fullname;
142 $profile->homepage = $homepage;
145 $profile->bio = $bio;
148 $profile->location = $location;
152 $profile->update($orig_profile);
154 $profile->created = DB_DataObject_Cast::dateTime(); # current time
155 $id = $profile->insert();
157 common_server_error(_('Error inserting new profile'));
164 if (!$this->add_avatar($profile, $avatar_url)) {
165 common_server_error(_('Error inserting avatar'));
170 $remote->postnoticeurl = $omb['post_notice_url'];
171 $remote->updateprofileurl = $omb['update_profile_url'];
174 if (!$remote->update($orig_remote)) {
175 common_server_error(_('Error updating remote profile'));
179 $remote->created = DB_DataObject_Cast::dateTime(); # current time
180 if (!$remote->insert()) {
181 common_server_error(_('Error inserting remote profile'));
186 $sub = new Subscription();
187 $sub->subscriber = $remote->id;
188 $sub->subscribed = $user->id;
189 $sub->token = $newtok;
190 $sub->secret = $newsecret;
191 $sub->created = DB_DataObject_Cast::dateTime(); # current time
193 if (!$sub->insert()) {
194 common_user_error(_('Couldn\'t insert new subscription.'));
198 # Notify user, if necessary
200 mail_subscribe_notify_profile($user, $profile);
203 unset($_SESSION['oauth_authorization_request']);
205 # If we show subscriptions in reverse chron order, this should
206 # show up close to the top of the page
208 common_redirect(common_local_url('subscribers', array('nickname' =>
212 function add_avatar($profile, $url) {
213 $temp_filename = tempnam(sys_get_temp_dir(), 'listener_avatar');
214 copy($url, $temp_filename);
215 return $profile->setOriginal($temp_filename);
218 function access_token($omb) {
220 common_debug('starting request for access token', __FILE__);
222 $con = omb_oauth_consumer();
223 $tok = new OAuthToken($omb['token'], $omb['secret']);
225 common_debug('using request token "'.$tok.'"', __FILE__);
227 $url = $omb['access_token_url'];
229 common_debug('using access token url "'.$url.'"', __FILE__);
231 # XXX: Is this the right thing to do? Strip off GET params and make them
232 # POST params? Seems wrong to me.
234 $parsed = parse_url($url);
236 parse_str($parsed['query'], $params);
238 $req = OAuthRequest::from_consumer_and_token($con, $tok, "POST", $url, $params);
240 $req->set_parameter('omb_version', OMB_VERSION_01);
242 # XXX: test to see if endpoint accepts this signature method
244 $req->sign_request(omb_hmac_sha1(), $con, $tok);
246 # We re-use this tool's fetcher, since it's pretty good
248 common_debug('posting to access token url "'.$req->get_normalized_http_url().'"', __FILE__);
249 common_debug('posting request data "'.$req->to_postdata().'"', __FILE__);
251 $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
252 $result = $fetcher->post($req->get_normalized_http_url(),
253 $req->to_postdata());
255 common_debug('got result: "'.print_r($result,TRUE).'"', __FILE__);
257 if ($result->status != 200) {
261 parse_str($result->body, $return);
263 return array($return['oauth_token'], $return['oauth_token_secret']);