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