]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/finishremotesubscribe.php
bd6dbf2a6585cac6ea2b59310dd745e9aea81020
[quix0rs-gnu-social.git] / actions / finishremotesubscribe.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2008, 2009, StatusNet, Inc.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 require_once(INSTALLDIR.'/lib/omb.php');
23
24 class FinishremotesubscribeAction extends Action
25 {
26
27     function handle($args)
28     {
29
30         parent::handle($args);
31
32         if (common_logged_in()) {
33             $this->clientError(_('You can use the local subscription!'));
34             return;
35         }
36
37         $omb = $_SESSION['oauth_authorization_request'];
38
39         if (!$omb) {
40             $this->clientError(_('Not expecting this response!'));
41             return;
42         }
43
44         common_debug('stored request: '.print_r($omb,true), __FILE__);
45
46         common_remove_magic_from_request();
47         $req = OAuthRequest::from_request('POST', common_local_url('finishuserauthorization'));
48
49         $token = $req->get_parameter('oauth_token');
50
51         # I think this is the success metric
52
53         if ($token != $omb['token']) {
54             $this->clientError(_('Not authorized.'));
55             return;
56         }
57
58         $version = $req->get_parameter('omb_version');
59
60         if ($version != OMB_VERSION_01) {
61             $this->clientError(_('Unknown version of OMB protocol.'));
62             return;
63         }
64
65         $nickname = $req->get_parameter('omb_listener_nickname');
66
67         if (!$nickname) {
68             $this->clientError(_('No nickname provided by remote server.'));
69             return;
70         }
71
72         $profile_url = $req->get_parameter('omb_listener_profile');
73
74         if (!$profile_url) {
75             $this->clientError(_('No profile URL returned by server.'));
76             return;
77         }
78
79         if (!Validate::uri($profile_url, array('allowed_schemes' => array('http', 'https')))) {
80             $this->clientError(_('Invalid profile URL returned by server.'));
81             return;
82         }
83
84         if ($profile_url == common_local_url('showstream', array('nickname' => $nickname))) {
85             $this->clientError(_('You can use the local subscription!'));
86             return;
87         }
88
89         common_debug('listenee: "'.$omb['listenee'].'"', __FILE__);
90
91         $user = User::staticGet('nickname', $omb['listenee']);
92
93         if (!$user) {
94             $this->clientError(_('User being listened to doesn\'t exist.'));
95             return;
96         }
97
98         $other = User::staticGet('uri', $omb['listener']);
99
100         if ($other) {
101             $this->clientError(_('You can use the local subscription!'));
102             return;
103         }
104
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');
110
111         list($newtok, $newsecret) = $this->access_token($omb);
112
113         if (!$newtok || !$newsecret) {
114             $this->clientError(_('Couldn\'t convert request tokens to access tokens.'));
115             return;
116         }
117
118         # XXX: possible attack point; subscribe and return someone else's profile URI
119
120         $remote = Remote_profile::staticGet('uri', $omb['listener']);
121
122         if ($remote) {
123             $exists = true;
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
129         } else {
130             $exists = false;
131             $remote = new Remote_profile();
132             $remote->uri = $omb['listener'];
133             $profile = new Profile();
134         }
135
136         $profile->nickname = $nickname;
137         $profile->profileurl = $profile_url;
138
139         if (!is_null($fullname)) {
140             $profile->fullname = $fullname;
141         }
142         if (!is_null($homepage)) {
143             $profile->homepage = $homepage;
144         }
145         if (!is_null($bio)) {
146             $profile->bio = $bio;
147         }
148         if (!is_null($location)) {
149             $profile->location = $location;
150         }
151
152         if ($exists) {
153             $profile->update($orig_profile);
154         } else {
155             $profile->created = DB_DataObject_Cast::dateTime(); # current time
156             $id = $profile->insert();
157             if (!$id) {
158                 $this->serverError(_('Error inserting new profile'));
159                 return;
160             }
161             $remote->id = $id;
162         }
163
164         if ($avatar_url) {
165             if (!$this->add_avatar($profile, $avatar_url)) {
166                 $this->serverError(_('Error inserting avatar'));
167                 return;
168             }
169         }
170
171         $remote->postnoticeurl = $omb['post_notice_url'];
172         $remote->updateprofileurl = $omb['update_profile_url'];
173
174         if ($exists) {
175             if (!$remote->update($orig_remote)) {
176                 $this->serverError(_('Error updating remote profile'));
177                 return;
178             }
179         } else {
180             $remote->created = DB_DataObject_Cast::dateTime(); # current time
181             if (!$remote->insert()) {
182                 $this->serverError(_('Error inserting remote profile'));
183                 return;
184             }
185         }
186
187         if ($user->hasBlocked($profile)) {
188             $this->clientError(_('That user has blocked you from subscribing.'));
189             return;
190         }
191
192         $sub = new Subscription();
193
194         $sub->subscriber = $remote->id;
195         $sub->subscribed = $user->id;
196
197         $sub_exists = false;
198
199         if ($sub->find(true)) {
200             $sub_exists = true;
201             $orig_sub = clone($sub);
202         } else {
203             $sub_exists = false;
204             $sub->created = DB_DataObject_Cast::dateTime(); # current time
205         }
206
207         $sub->token = $newtok;
208         $sub->secret = $newsecret;
209
210         if ($sub_exists) {
211             $result = $sub->update($orig_sub);
212         } else {
213             $result = $sub->insert();
214         }
215
216         if (!$result) {
217             common_log_db_error($sub, ($sub_exists) ? 'UPDATE' : 'INSERT', __FILE__);
218             $this->clientError(_('Couldn\'t insert new subscription.'));
219             return;
220         }
221
222         # Notify user, if necessary
223
224         mail_subscribe_notify_profile($user, $profile);
225
226         # Clear the data
227         unset($_SESSION['oauth_authorization_request']);
228
229         # If we show subscriptions in reverse chron order, this should
230         # show up close to the top of the page
231
232         common_redirect(common_local_url('subscribers', array('nickname' =>
233                                                              $user->nickname)),
234                         303);
235     }
236
237     function add_avatar($profile, $url)
238     {
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),
244                                      null,
245                                      common_timestamp());
246         rename($temp_filename, Avatar::path($filename));
247         return $profile->setOriginal($filename);
248     }
249
250     function access_token($omb)
251     {
252
253         common_debug('starting request for access token', __FILE__);
254
255         $con = omb_oauth_consumer();
256         $tok = new OAuthToken($omb['token'], $omb['secret']);
257
258         common_debug('using request token "'.$tok.'"', __FILE__);
259
260         $url = $omb['access_token_url'];
261
262         common_debug('using access token url "'.$url.'"', __FILE__);
263
264         # XXX: Is this the right thing to do? Strip off GET params and make them
265         # POST params? Seems wrong to me.
266
267         $parsed = parse_url($url);
268         $params = array();
269         parse_str($parsed['query'], $params);
270
271         $req = OAuthRequest::from_consumer_and_token($con, $tok, "POST", $url, $params);
272
273         $req->set_parameter('omb_version', OMB_VERSION_01);
274
275         # XXX: test to see if endpoint accepts this signature method
276
277         $req->sign_request(omb_hmac_sha1(), $con, $tok);
278
279         # We re-use this tool's fetcher, since it's pretty good
280
281         common_debug('posting to access token url "'.$req->get_normalized_http_url().'"', __FILE__);
282         common_debug('posting request data "'.$req->to_postdata().'"', __FILE__);
283
284         $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
285         $result = $fetcher->post($req->get_normalized_http_url(),
286                                  $req->to_postdata(),
287                                  array('User-Agent: StatusNet/' . LACONICA_VERSION));
288
289         common_debug('got result: "'.print_r($result,true).'"', __FILE__);
290
291         if ($result->status != 200) {
292             return null;
293         }
294
295         parse_str($result->body, $return);
296
297         return array($return['oauth_token'], $return['oauth_token_secret']);
298     }
299 }