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