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