]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/actions/ostatussub.php
Fix for ticket 2756 - Calls to OAuth endpoints are redirected to the
[quix0rs-gnu-social.git] / plugins / OStatus / actions / ostatussub.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2009-2010, 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 /**
21  * @package OStatusPlugin
22  * @maintainer Brion Vibber <brion@status.net>
23  */
24
25 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
26
27 /**
28  * Key UI methods:
29  *
30  *  showInputForm() - form asking for a remote profile account or URL
31  *                    We end up back here on errors
32  *
33  *  showPreviewForm() - surrounding form for preview-and-confirm
34  *    preview() - display profile for a remote user
35  *
36  *  success() - redirects to subscriptions page on subscribe
37  */
38 class OStatusSubAction extends Action
39 {
40     protected $profile_uri; // provided acct: or URI of remote entity
41     protected $oprofile; // Ostatus_profile of remote entity, if valid
42
43     /**
44      * Show the initial form, when we haven't yet been given a valid
45      * remote profile.
46      */
47     function showInputForm()
48     {
49         $user = common_current_user();
50
51         $profile = $user->getProfile();
52
53         $this->elementStart('form', array('method' => 'post',
54                                           'id' => 'form_ostatus_sub',
55                                           'class' => 'form_settings',
56                                           'action' => $this->selfLink()));
57
58         $this->hidden('token', common_session_token());
59
60         $this->elementStart('fieldset', array('id' => 'settings_feeds'));
61
62         $this->elementStart('ul', 'form_data');
63         $this->elementStart('li');
64         $this->input('profile',
65                      _m('Subscribe to'),
66                      $this->profile_uri,
67                      _m("OStatus user's address, like nickname@example.com or http://example.net/nickname"));  // @todo i18n FIXME: needs context/translator hint.
68         $this->elementEnd('li');
69         $this->elementEnd('ul');
70
71         $this->submit('validate', _m('Continue')); // @todo i18n FIXME: needs context/translator hint.
72
73         $this->elementEnd('fieldset');
74
75         $this->elementEnd('form');
76     }
77
78     /**
79      * Show the preview-and-confirm form. We've got a valid remote
80      * profile and are ready to poke it!
81      *
82      * This controls the wrapper form; actual profile display will
83      * be in previewUser() or previewGroup() depending on the type.
84      */
85     function showPreviewForm()
86     {
87         $ok = $this->preview();
88         if (!$ok) {
89             // @fixme maybe provide a cancel button or link back?
90             return;
91         }
92
93         $this->elementStart('div', 'entity_actions');
94         $this->elementStart('ul');
95         $this->elementStart('li', 'entity_subscribe');
96         $this->elementStart('form', array('method' => 'post',
97                                           'id' => 'form_ostatus_sub',
98                                           'class' => 'form_remote_authorize',
99                                           'action' =>
100                                           $this->selfLink()));
101         $this->elementStart('fieldset');
102         $this->hidden('token', common_session_token());
103         $this->hidden('profile', $this->profile_uri);
104         if ($this->oprofile->isGroup()) {
105             $this->submit('submit', _m('Join'), 'submit', null,
106                          _m('Join this group')); // @todo i18n FIXME: needs context/translator hint.
107         } else {
108             $this->submit('submit', _m('Confirm'), 'submit', null,
109                          _m('Subscribe to this user')); // @todo i18n FIXME: needs context/translator hint.
110         }
111         $this->elementEnd('fieldset');
112         $this->elementEnd('form');
113         $this->elementEnd('li');
114         $this->elementEnd('ul');
115         $this->elementEnd('div');
116     }
117
118     /**
119      * Show a preview for a remote user's profile
120      * @return boolean true if we're ok to try subscribing
121      */
122     function preview()
123     {
124         $oprofile = $this->oprofile;
125         $profile = $oprofile->localProfile();
126
127         $cur = common_current_user();
128         if ($cur->isSubscribed($profile)) {
129             $this->element('div', array('class' => 'error'),
130                            _m("You are already subscribed to this user."));
131             $ok = false;
132         } else {
133             $ok = true;
134         }
135
136         $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
137         $avatarUrl = $avatar ? $avatar->displayUrl() : false;
138
139         $this->showEntity($profile,
140                           $profile->profileurl,
141                           $avatarUrl,
142                           $profile->bio);
143         return $ok;
144     }
145
146     function showEntity($entity, $profile, $avatar, $note)
147     {
148         $nickname = $entity->nickname;
149         $fullname = $entity->fullname;
150         $homepage = $entity->homepage;
151         $location = $entity->location;
152
153         if (!$avatar) {
154             $avatar = Avatar::defaultImage(AVATAR_PROFILE_SIZE);
155         }
156
157         $this->elementStart('div', 'entity_profile vcard');
158         $this->elementStart('dl', 'entity_depiction');
159         $this->element('dt', null, _('Photo'));
160         $this->elementStart('dd');
161         $this->element('img', array('src' => $avatar,
162                                     'class' => 'photo avatar',
163                                     'width' => AVATAR_PROFILE_SIZE,
164                                     'height' => AVATAR_PROFILE_SIZE,
165                                     'alt' => $nickname));
166         $this->elementEnd('dd');
167         $this->elementEnd('dl');
168
169         $this->elementStart('dl', 'entity_nickname');
170         $this->element('dt', null, _('Nickname'));
171         $this->elementStart('dd');
172         $hasFN = ($fullname !== '') ? 'nickname' : 'fn nickname';
173         $this->elementStart('a', array('href' => $profile,
174                                        'class' => 'url '.$hasFN));
175         $this->raw($nickname);
176         $this->elementEnd('a');
177         $this->elementEnd('dd');
178         $this->elementEnd('dl');
179
180         if (!is_null($fullname)) {
181             $this->elementStart('dl', 'entity_fn');
182             $this->elementStart('dd');
183             $this->elementStart('span', 'fn');
184             $this->raw($fullname);
185             $this->elementEnd('span');
186             $this->elementEnd('dd');
187             $this->elementEnd('dl');
188         }
189         if (!is_null($location)) {
190             $this->elementStart('dl', 'entity_location');
191             $this->element('dt', null, _('Location'));
192             $this->elementStart('dd', 'label');
193             $this->raw($location);
194             $this->elementEnd('dd');
195             $this->elementEnd('dl');
196         }
197
198         if (!is_null($homepage)) {
199             $this->elementStart('dl', 'entity_url');
200             $this->element('dt', null, _('URL'));
201             $this->elementStart('dd');
202             $this->elementStart('a', array('href' => $homepage,
203                                                 'class' => 'url'));
204             $this->raw($homepage);
205             $this->elementEnd('a');
206             $this->elementEnd('dd');
207             $this->elementEnd('dl');
208         }
209
210         if (!is_null($note)) {
211             $this->elementStart('dl', 'entity_note');
212             $this->element('dt', null, _('Note'));
213             $this->elementStart('dd', 'note');
214             $this->raw($note);
215             $this->elementEnd('dd');
216             $this->elementEnd('dl');
217         }
218         $this->elementEnd('div');
219     }
220
221     /**
222      * Redirect on successful remote user subscription
223      */
224     function success()
225     {
226         $cur = common_current_user();
227         $url = common_local_url('subscriptions', array('nickname' => $cur->nickname));
228         common_redirect($url, 303);
229     }
230
231     /**
232      * Pull data for a remote profile and check if it's valid.
233      * Fills out error UI string in $this->error
234      * Fills out $this->oprofile on success.
235      *
236      * @return boolean
237      */
238     function pullRemoteProfile()
239     {
240         $this->profile_uri = $this->trimmed('profile');
241         try {
242             if (Validate::email($this->profile_uri)) {
243                 $this->oprofile = Ostatus_profile::ensureWebfinger($this->profile_uri);
244             } else if (Validate::uri($this->profile_uri)) {
245                 $this->oprofile = Ostatus_profile::ensureProfileURL($this->profile_uri);
246             } else {
247                 $this->error = _m("Sorry, we could not reach that address. Please make sure that the OStatus address is like nickname@example.com or http://example.net/nickname.");
248                 common_debug('Invalid address format.', __FILE__);
249                 return false;
250             }
251             return true;
252         } catch (FeedSubBadURLException $e) {
253             $this->error = _m("Sorry, we could not reach that address. Please make sure that the OStatus address is like nickname@example.com or http://example.net/nickname.");
254             common_debug('Invalid URL or could not reach server.', __FILE__);
255         } catch (FeedSubBadResponseException $e) {
256             $this->error = _m("Sorry, we could not reach that feed. Please try that OStatus address again later.");
257             common_debug('Cannot read feed; server returned error.', __FILE__);
258         } catch (FeedSubEmptyException $e) {
259             $this->error = _m("Sorry, we could not reach that feed. Please try that OStatus address again later.");
260             common_debug('Cannot read feed; server returned an empty page.', __FILE__);
261         } catch (FeedSubBadHTMLException $e) {
262             $this->error = _m("Sorry, we could not reach that feed. Please try that OStatus address again later.");
263             common_debug('Bad HTML, could not find feed link.', __FILE__);
264         } catch (FeedSubNoFeedException $e) {
265             $this->error = _m("Sorry, we could not reach that feed. Please try that OStatus address again later.");
266             common_debug('Could not find a feed linked from this URL.', __FILE__);
267         } catch (FeedSubUnrecognizedTypeException $e) {
268             $this->error = _m("Sorry, we could not reach that feed. Please try that OStatus address again later.");
269             common_debug('Not a recognized feed type.', __FILE__);
270         } catch (Exception $e) {
271             // Any new ones we forgot about
272             $this->error = _m("Sorry, we could not reach that address. Please make sure that the OStatus address is like nickname@example.com or http://example.net/nickname.");
273             common_debug(sprintf('Bad feed URL: %s %s', get_class($e), $e->getMessage()), __FILE__);
274         }
275
276         return false;
277     }
278
279     function validateRemoteProfile()
280     {
281         if ($this->oprofile->isGroup()) {
282             // Send us to the group subscription form for conf
283             $target = common_local_url('ostatusgroup', array(), array('profile' => $this->profile_uri));
284             common_redirect($target, 303);
285         }
286     }
287
288     /**
289      * Attempt to finalize subscription.
290      * validateFeed must have been run first.
291      *
292      * Calls showForm on failure or success on success.
293      */
294     function saveFeed()
295     {
296         // And subscribe the current user to the local profile
297         $user = common_current_user();
298         $local = $this->oprofile->localProfile();
299         if ($user->isSubscribed($local)) {
300             // TRANS: OStatus remote subscription dialog error.
301             $this->showForm(_m('Already subscribed!'));
302         } elseif (Subscription::start($user, $local)) {
303             $this->success();
304         } else {
305             // TRANS: OStatus remote subscription dialog error.
306             $this->showForm(_m('Remote subscription failed!'));
307         }
308     }
309
310     function prepare($args)
311     {
312         parent::prepare($args);
313
314         if (!common_logged_in()) {
315             // XXX: selfURL() didn't work. :<
316             common_set_returnto($_SERVER['REQUEST_URI']);
317             if (Event::handle('RedirectToLogin', array($this, null))) {
318                 common_redirect(common_local_url('login'), 303);
319             }
320             return false;
321         }
322
323         if ($this->pullRemoteProfile()) {
324             $this->validateRemoteProfile();
325         }
326         return true;
327     }
328
329     /**
330      * Handle the submission.
331      */
332     function handle($args)
333     {
334         parent::handle($args);
335         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
336             $this->handlePost();
337         } else {
338             $this->showForm();
339         }
340     }
341
342     /**
343      * Handle posts to this form
344      *
345      * @return void
346      */
347
348     function handlePost()
349     {
350         // CSRF protection
351         $token = $this->trimmed('token');
352         if (!$token || $token != common_session_token()) {
353             $this->showForm(_('There was a problem with your session token. '.
354                               'Try again, please.'));
355             return;
356         }
357
358         if ($this->oprofile) {
359             if ($this->arg('submit')) {
360                 $this->saveFeed();
361                 return;
362             }
363         }
364         $this->showForm();
365     }
366
367     /**
368      * Show the appropriate form based on our input state.
369      */
370     function showForm($err=null)
371     {
372         if ($err) {
373             $this->error = $err;
374         }
375         if ($this->boolean('ajax')) {
376             header('Content-Type: text/xml;charset=utf-8');
377             $this->xw->startDocument('1.0', 'UTF-8');
378             $this->elementStart('html');
379             $this->elementStart('head');
380             $this->element('title', null, _m('Subscribe to user'));
381             $this->elementEnd('head');
382             $this->elementStart('body');
383             $this->showContent();
384             $this->elementEnd('body');
385             $this->elementEnd('html');
386         } else {
387             $this->showPage();
388         }
389     }
390
391     /**
392      * Title of the page
393      *
394      * @return string Title of the page
395      */
396
397     function title()
398     {
399         // TRANS: Page title for OStatus remote subscription form
400         return _m('Confirm');
401     }
402
403     /**
404      * Instructions for use
405      *
406      * @return instructions for use
407      */
408
409     function getInstructions()
410     {
411         return _m('You can subscribe to users from other supported sites. Paste their address or profile URI below:');
412     }
413
414     function showPageNotice()
415     {
416         if (!empty($this->error)) {
417             $this->element('p', 'error', $this->error);
418         }
419     }
420
421     /**
422      * Content area of the page
423      *
424      * Shows a form for associating a remote OStatus account with this
425      * StatusNet account.
426      *
427      * @return void
428      */
429
430     function showContent()
431     {
432         if ($this->oprofile) {
433             $this->showPreviewForm();
434         } else {
435             $this->showInputForm();
436         }
437     }
438
439     function showScripts()
440     {
441         parent::showScripts();
442         $this->autofocus('feedurl');
443     }
444
445     function selfLink()
446     {
447         return common_local_url('ostatussub');
448     }
449
450     /**
451      * Disable the send-notice form at the top of the page.
452      * This is really just a hack for the broken CSS in the Cloudy theme,
453      * I think; copying from other non-notice-navigation pages that do this
454      * as well. There will be plenty of others also broken.
455      *
456      * @fixme fix the cloudy theme
457      * @fixme do this in a more general way
458      */
459     function showNoticeForm() {
460         // nop
461     }
462
463 }