]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/remotesubscribe.php
try to clean up user-without-profile errors
[quix0rs-gnu-social.git] / actions / remotesubscribe.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 RemotesubscribeAction 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                 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
36                         
37                         # CSRF protection
38                         $token = $this->trimmed('token');
39                         if (!$token || $token != common_session_token()) {
40                                 $this->show_form(_('There was a problem with your session token. Try again, please.'));
41                                 return;
42                         }
43                         
44                         $this->remote_subscription();
45                 } else {
46                         $this->show_form();
47                 }
48         }
49
50         function get_instructions() {
51                 return _('To subscribe, you can [login](%%action.login%%),' .
52                           ' or [register](%%action.register%%) a new ' .
53                           ' account. If you already have an account ' .
54                           ' on a [compatible microblogging site](%%doc.openmublog%%), ' .
55                           ' enter your profile URL below.');
56         }
57
58         function show_top($err=NULL) {
59                 if ($err) {
60                         common_element('div', 'error', $err);
61                 } else {
62                         $instructions = $this->get_instructions();
63                         $output = common_markup_to_html($instructions);
64                         common_element_start('div', 'instructions');
65                         common_raw($output);
66                         common_element_end('p');
67                 }
68         }
69
70         function show_form($err=NULL) {
71                 $nickname = $this->trimmed('nickname');
72                 $profile = $this->trimmed('profile_url');
73                 common_show_header(_('Remote subscribe'), NULL, $err,
74                                                    array($this, 'show_top'));
75                 # id = remotesubscribe conflicts with the
76                 # button on profile page
77                 common_element_start('form', array('id' => 'remsub', 'method' => 'post',
78                                                                                    'action' => common_local_url('remotesubscribe')));
79                 common_hidden('token', common_session_token());
80                 common_input('nickname', _('User nickname'), $nickname,
81                                          _('Nickname of the user you want to follow'));
82                 common_input('profile_url', _('Profile URL'), $profile,
83                                          _('URL of your profile on another compatible microblogging service'));
84                 common_submit('submit', _('Subscribe'));
85                 common_element_end('form');
86                 common_show_footer();
87         }
88
89         function remote_subscription() {
90                 $user = $this->get_user();
91
92                 if (!$user) {
93                         $this->show_form(_('No such user.'));
94                         return;
95                 }
96
97                 $profile = $this->trimmed('profile_url');
98
99                 if (!$profile) {
100                         $this->show_form(_('No such user.'));
101                         return;
102                 }
103
104                 if (!Validate::uri($profile, array('allowed_schemes' => array('http', 'https')))) {
105                         $this->show_form(_('Invalid profile URL (bad format)'));
106                         return;
107                 }
108
109                 $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
110                 $yadis = Auth_Yadis_Yadis::discover($profile, $fetcher);
111
112                 if (!$yadis || $yadis->failed) {
113                         $this->show_form(_('Not a valid profile URL (no YADIS document).'));
114                         return;
115                 }
116
117                 # XXX: a little liberal for sites that accidentally put whitespace before the xml declaration
118                 
119         $xrds =& Auth_Yadis_XRDS::parseXRDS(trim($yadis->response_text));
120
121                 if (!$xrds) {
122                         $this->show_form(_('Not a valid profile URL (no XRDS defined).'));
123                         return;
124                 }
125
126                 $omb = $this->getOmb($xrds);
127
128                 if (!$omb) {
129                         $this->show_form(_('Not a valid profile URL (incorrect services).'));
130                         return;
131                 }
132
133                 list($token, $secret) = $this->request_token($omb);
134
135                 if (!$token || !$secret) {
136                         $this->show_form(_('Couldn\'t get a request token.'));
137                         return;
138                 }
139
140                 $this->request_authorization($user, $omb, $token, $secret);
141         }
142
143         function get_user() {
144                 $user = NULL;
145                 $nickname = $this->trimmed('nickname');
146                 if ($nickname) {
147                         $user = User::staticGet('nickname', $nickname);
148                 }
149                 return $user;
150         }
151
152         function getOmb($xrds) {
153
154             static $omb_endpoints = array(OMB_ENDPOINT_UPDATEPROFILE, OMB_ENDPOINT_POSTNOTICE);
155                 static $oauth_endpoints = array(OAUTH_ENDPOINT_REQUEST, OAUTH_ENDPOINT_AUTHORIZE,
156                                                                                 OAUTH_ENDPOINT_ACCESS);
157                 $omb = array();
158
159                 # XXX: the following code could probably be refactored to eliminate dupes
160
161                 $oauth_services = omb_get_services($xrds, OAUTH_DISCOVERY);
162
163                 if (!$oauth_services) {
164                         return NULL;
165                 }
166
167                 $oauth_service = $oauth_services[0];
168
169                 $oauth_xrd = $this->getXRD($oauth_service, $xrds);
170
171                 if (!$oauth_xrd) {
172                         return NULL;
173                 }
174
175                 if (!$this->addServices($oauth_xrd, $oauth_endpoints, $omb)) {
176                         return NULL;
177                 }
178
179                 $omb_services = omb_get_services($xrds, OMB_NAMESPACE);
180
181                 if (!$omb_services) {
182                         return NULL;
183                 }
184
185                 $omb_service = $omb_services[0];
186
187                 $omb_xrd = $this->getXRD($omb_service, $xrds);
188
189                 if (!$omb_xrd) {
190                         return NULL;
191                 }
192
193                 if (!$this->addServices($omb_xrd, $omb_endpoints, $omb)) {
194                         return NULL;
195                 }
196
197                 # XXX: check that we got all the services we needed
198
199                 foreach (array_merge($omb_endpoints, $oauth_endpoints) as $type) {
200                         if (!array_key_exists($type, $omb) || !$omb[$type]) {
201                                 return NULL;
202                         }
203                 }
204
205                 if (!omb_local_id($omb[OAUTH_ENDPOINT_REQUEST])) {
206                         return NULL;
207                 }
208
209                 return $omb;
210         }
211
212         function getXRD($main_service, $main_xrds) {
213                 $uri = omb_service_uri($main_service);
214                 if (strpos($uri, "#") !== 0) {
215                         # FIXME: more rigorous handling of external service definitions
216                         return NULL;
217                 }
218                 $id = substr($uri, 1);
219                 $nodes = $main_xrds->allXrdNodes;
220                 $parser = $main_xrds->parser;
221                 foreach ($nodes as $node) {
222                         $attrs = $parser->attributes($node);
223                         if (array_key_exists('xml:id', $attrs) &&
224                                 $attrs['xml:id'] == $id) {
225                                 # XXX: trick the constructor into thinking this is the only node
226                                 $bogus_nodes = array($node);
227                                 return new Auth_Yadis_XRDS($parser, $bogus_nodes);
228                         }
229                 }
230                 return NULL;
231         }
232
233         function addServices($xrd, $types, &$omb) {
234                 foreach ($types as $type) {
235                         $matches = omb_get_services($xrd, $type);
236                         if ($matches) {
237                                 $omb[$type] = $matches[0];
238                         } else {
239                                 # no match for type
240                                 return false;
241                         }
242                 }
243                 return true;
244         }
245
246         function request_token($omb) {
247                 $con = omb_oauth_consumer();
248
249                 $url = omb_service_uri($omb[OAUTH_ENDPOINT_REQUEST]);
250
251                 # XXX: Is this the right thing to do? Strip off GET params and make them
252                 # POST params? Seems wrong to me.
253
254                 $parsed = parse_url($url);
255                 $params = array();
256                 parse_str($parsed['query'], $params);
257
258                 $req = OAuthRequest::from_consumer_and_token($con, NULL, "POST", $url, $params);
259
260                 $listener = omb_local_id($omb[OAUTH_ENDPOINT_REQUEST]);
261
262                 if (!$listener) {
263                         return NULL;
264                 }
265
266                 $req->set_parameter('omb_listener', $listener);
267                 $req->set_parameter('omb_version', OMB_VERSION_01);
268
269                 # XXX: test to see if endpoint accepts this signature method
270
271                 $req->sign_request(omb_hmac_sha1(), $con, NULL);
272
273                 # We re-use this tool's fetcher, since it's pretty good
274
275                 $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
276
277                 $result = $fetcher->post($req->get_normalized_http_url(),
278                                                                  $req->to_postdata());
279
280                 if ($result->status != 200) {
281                         return NULL;
282                 }
283
284                 parse_str($result->body, $return);
285
286                 return array($return['oauth_token'], $return['oauth_token_secret']);
287         }
288
289         function request_authorization($user, $omb, $token, $secret) {
290                 global $config; # for license URL
291
292                 $con = omb_oauth_consumer();
293                 $tok = new OAuthToken($token, $secret);
294
295                 $url = omb_service_uri($omb[OAUTH_ENDPOINT_AUTHORIZE]);
296
297                 # XXX: Is this the right thing to do? Strip off GET params and make them
298                 # POST params? Seems wrong to me.
299
300                 $parsed = parse_url($url);
301                 $params = array();
302                 parse_str($parsed['query'], $params);
303
304                 $req = OAuthRequest::from_consumer_and_token($con, $tok, 'GET', $url, $params);
305
306                 # We send over a ton of information. This lets the other
307                 # server store info about our user, and it lets the current
308                 # user decide if they really want to authorize the subscription.
309
310                 $req->set_parameter('omb_version', OMB_VERSION_01);
311                 $req->set_parameter('omb_listener', omb_local_id($omb[OAUTH_ENDPOINT_REQUEST]));
312                 $req->set_parameter('omb_listenee', $user->uri);
313                 $req->set_parameter('omb_listenee_profile', common_profile_url($user->nickname));
314                 $req->set_parameter('omb_listenee_nickname', $user->nickname);
315                 $req->set_parameter('omb_listenee_license', $config['license']['url']);
316
317                 $profile = $user->getProfile();
318                 if (!$profile) {
319                         common_log_db_error($user, 'SELECT', __FILE__);
320                         $this->server_error(_('User without matching profile'));
321                         return;
322                 }
323                 
324                 if ($profile->fullname) {
325                         $req->set_parameter('omb_listenee_fullname', $profile->fullname);
326                 }
327                 if ($profile->homepage) {
328                         $req->set_parameter('omb_listenee_homepage', $profile->homepage);
329                 }
330                 if ($profile->bio) {
331                         $req->set_parameter('omb_listenee_bio', $profile->bio);
332                 }
333                 if ($profile->location) {
334                         $req->set_parameter('omb_listenee_location', $profile->location);
335                 }
336                 $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
337                 if ($avatar) {
338                         $req->set_parameter('omb_listenee_avatar', $avatar->url);
339                 }
340
341                 # XXX: add a nonce to prevent replay attacks
342
343                 $req->set_parameter('oauth_callback', common_local_url('finishremotesubscribe'));
344
345                 # XXX: test to see if endpoint accepts this signature method
346
347                 $req->sign_request(omb_hmac_sha1(), $con, $tok);
348
349                 # store all our info here
350
351                 $omb['listenee'] = $user->nickname;
352                 $omb['listener'] = omb_local_id($omb[OAUTH_ENDPOINT_REQUEST]);
353                 $omb['token'] = $token;
354                 $omb['secret'] = $secret;
355                 # call doesn't work after bounce back so we cache; maybe serialization issue...?
356                 $omb['access_token_url'] = omb_service_uri($omb[OAUTH_ENDPOINT_ACCESS]);
357                 $omb['post_notice_url'] = omb_service_uri($omb[OMB_ENDPOINT_POSTNOTICE]);
358                 $omb['update_profile_url'] = omb_service_uri($omb[OMB_ENDPOINT_UPDATEPROFILE]);
359
360                 common_ensure_session();
361                 
362                 $_SESSION['oauth_authorization_request'] = $omb;
363
364                 # Redirect to authorization service
365
366                 common_redirect($req->to_url());
367                 return;
368         }
369
370         function make_nonce() {
371                 return common_good_rand(16);
372         }
373 }