]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/actions/ostatussub.php
OStatus: fix for avatars, submit button in updated remote profile preview
[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         $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
130         $avatarUrl = $avatar ? $avatar->displayUrl() : false;
131
132         $this->showEntity($profile,
133                           $profile->profileurl,
134                           $avatarUrl,
135                           $profile->bio);
136     }
137
138     /**
139      * Show a preview for a remote group's profile
140      */
141     function previewGroup()
142     {
143         $oprofile = $this->oprofile;
144         $group = $oprofile->localGroup();
145
146         $this->showEntity($group,
147                           $group->getProfileUrl(),
148                           $group->homepage_logo,
149                           $group->description);
150     }
151
152
153     function showEntity($entity, $profile, $avatar, $note)
154     {
155         $nickname = $entity->nickname;
156         $fullname = $entity->fullname;
157         $homepage = $entity->homepage;
158         $location = $entity->location;
159         
160         if (!$avatar) {
161             $avatar = Avatar::defaultImage(AVATAR_PROFILE_SIZE);
162         }
163
164         $this->elementStart('div', 'entity_profile vcard');
165         $this->elementStart('dl', 'entity_depiction');
166         $this->element('dt', null, _('Photo'));
167         $this->elementStart('dd');
168         $this->element('img', array('src' => $avatar,
169                                     'class' => 'photo avatar',
170                                     'width' => AVATAR_PROFILE_SIZE,
171                                     'height' => AVATAR_PROFILE_SIZE,
172                                     'alt' => $nickname));
173         $this->elementEnd('dd');
174         $this->elementEnd('dl');
175
176         $this->elementStart('dl', 'entity_nickname');
177         $this->element('dt', null, _('Nickname'));
178         $this->elementStart('dd');
179         $hasFN = ($fullname !== '') ? 'nickname' : 'fn nickname';
180         $this->elementStart('a', array('href' => $profile,
181                                        'class' => 'url '.$hasFN));
182         $this->raw($nickname);
183         $this->elementEnd('a');
184         $this->elementEnd('dd');
185         $this->elementEnd('dl');
186
187         if (!is_null($fullname)) {
188             $this->elementStart('dl', 'entity_fn');
189             $this->elementStart('dd');
190             $this->elementStart('span', 'fn');
191             $this->raw($fullname);
192             $this->elementEnd('span');
193             $this->elementEnd('dd');
194             $this->elementEnd('dl');
195         }
196         if (!is_null($location)) {
197             $this->elementStart('dl', 'entity_location');
198             $this->element('dt', null, _('Location'));
199             $this->elementStart('dd', 'label');
200             $this->raw($location);
201             $this->elementEnd('dd');
202             $this->elementEnd('dl');
203         }
204
205         if (!is_null($homepage)) {
206             $this->elementStart('dl', 'entity_url');
207             $this->element('dt', null, _('URL'));
208             $this->elementStart('dd');
209             $this->elementStart('a', array('href' => $homepage,
210                                                 'class' => 'url'));
211             $this->raw($homepage);
212             $this->elementEnd('a');
213             $this->elementEnd('dd');
214             $this->elementEnd('dl');
215         }
216
217         if (!is_null($note)) {
218             $this->elementStart('dl', 'entity_note');
219             $this->element('dt', null, _('Note'));
220             $this->elementStart('dd', 'note');
221             $this->raw($note);
222             $this->elementEnd('dd');
223             $this->elementEnd('dl');
224         }
225         $this->elementEnd('div');
226     }
227
228     /**
229      * Redirect on successful remote user subscription
230      */
231     function successUser()
232     {
233         $cur = common_current_user();
234         $url = common_local_url('subscriptions', array('nickname' => $cur->nickname));
235         common_redirect($url, 303);
236     }
237
238     /**
239      * Redirect on successful remote group join
240      */
241     function successGroup()
242     {
243         $cur = common_current_user();
244         $url = common_local_url('usergroups', array('nickname' => $cur->nickname));
245         common_redirect($url, 303);
246     }
247
248     /**
249      * Pull data for a remote profile and check if it's valid.
250      * Fills out error UI string in $this->error
251      * Fills out $this->oprofile on success.
252      *
253      * @return boolean
254      */
255     function validateFeed()
256     {
257         $profile_uri = trim($this->arg('profile'));
258
259         if ($profile_uri == '') {
260             $this->showForm(_m('Empty remote profile URL!'));
261             return;
262         }
263         $this->profile_uri = $profile_uri;
264
265         // @fixme validate, normalize bla bla
266         try {
267             $oprofile = Ostatus_profile::ensureProfile($this->profile_uri);
268             $this->oprofile = $oprofile;
269             return true;
270         } catch (FeedSubBadURLException $e) {
271             $this->error = _m('Invalid URL or could not reach server.');
272         } catch (FeedSubBadResponseException $e) {
273             $this->error = _m('Cannot read feed; server returned error.');
274         } catch (FeedSubEmptyException $e) {
275             $this->error = _m('Cannot read feed; server returned an empty page.');
276         } catch (FeedSubBadHTMLException $e) {
277             $this->error = _m('Bad HTML, could not find feed link.');
278         } catch (FeedSubNoFeedException $e) {
279             $this->error = _m('Could not find a feed linked from this URL.');
280         } catch (FeedSubUnrecognizedTypeException $e) {
281             $this->error = _m('Not a recognized feed type.');
282         } catch (FeedSubException $e) {
283             // Any new ones we forgot about
284             $this->error = sprintf(_m('Bad feed URL: %s %s'), get_class($e), $e->getMessage());
285         }
286
287         return false;
288     }
289
290     /**
291      * Attempt to finalize subscription.
292      * validateFeed must have been run first.
293      *
294      * Calls showForm on failure or successUser/successGroup on success.
295      */
296     function saveFeed()
297     {
298         // And subscribe the current user to the local profile
299         $user = common_current_user();
300
301         if (!$this->oprofile->subscribe()) {
302             $this->showForm(_m("Failed to set up server-to-server subscription."));
303             return;
304         }
305
306         if ($this->oprofile->isGroup()) {
307             $group = $this->oprofile->localGroup();
308             if ($user->isMember($group)) {
309                 $this->showForm(_m('Already a member!'));
310             } elseif (Group_member::join($this->oprofile->group_id, $user->id)) {
311                 $this->successGroup();
312             } else {
313                 $this->showForm(_m('Remote group join failed!'));
314             }
315         } else {
316             $local = $this->oprofile->localProfile();
317             if ($user->isSubscribed($local)) {
318                 $this->showForm(_m('Already subscribed!'));
319             } elseif ($this->oprofile->subscribeLocalToRemote($user)) {
320                 $this->successUser();
321             } else {
322                 $this->showForm(_m('Remote subscription failed!'));
323             }
324         }
325     }
326
327     function prepare($args)
328     {
329         parent::prepare($args);
330
331         if (!common_logged_in()) {
332             // XXX: selfURL() didn't work. :<
333             common_set_returnto($_SERVER['REQUEST_URI']);
334             if (Event::handle('RedirectToLogin', array($this, null))) {
335                 common_redirect(common_local_url('login'), 303);
336             }
337             return false;
338         }
339
340         $this->profile_uri = $this->arg('profile');
341
342         return true;
343     }
344
345     /**
346      * Handle the submission.
347      */
348     function handle($args)
349     {
350         parent::handle($args);
351         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
352             $this->handlePost();
353         } else {
354             if ($this->arg('profile')) {
355                 $this->validateFeed();
356             }
357             $this->showForm();
358         }
359     }
360
361
362     /**
363      * Handle posts to this form
364      *
365      * @return void
366      */
367
368     function handlePost()
369     {
370         // CSRF protection
371         $token = $this->trimmed('token');
372         if (!$token || $token != common_session_token()) {
373             $this->showForm(_('There was a problem with your session token. '.
374                               'Try again, please.'));
375             return;
376         }
377
378         if ($this->validateFeed()) {
379             if ($this->arg('submit')) {
380                 $this->saveFeed();
381                 return;
382             }
383         }
384         $this->showForm();
385     }
386
387     /**
388      * Show the appropriate form based on our input state.
389      */
390     function showForm($err=null)
391     {
392         if ($err) {
393             $this->error = $err;
394         }
395         if ($this->boolean('ajax')) {
396             header('Content-Type: text/xml;charset=utf-8');
397             $this->xw->startDocument('1.0', 'UTF-8');
398             $this->elementStart('html');
399             $this->elementStart('head');
400             $this->element('title', null, _m('Subscribe to user'));
401             $this->elementEnd('head');
402             $this->elementStart('body');
403             $this->showContent();
404             $this->elementEnd('body');
405             $this->elementEnd('html');
406         } else {
407             $this->showPage();
408         }
409     }
410
411     /**
412      * Title of the page
413      *
414      * @return string Title of the page
415      */
416
417     function title()
418     {
419         return _m('Authorize subscription');
420     }
421
422     /**
423      * Instructions for use
424      *
425      * @return instructions for use
426      */
427
428     function getInstructions()
429     {
430         return _m('You can subscribe to users from other supported sites. Paste their address or profile URI below:');
431     }
432
433     function showPageNotice()
434     {
435         if (!empty($this->error)) {
436             $this->element('p', 'error', $this->error);
437         }
438     }
439
440     /**
441      * Content area of the page
442      *
443      * Shows a form for associating a remote OStatus account with this
444      * StatusNet account.
445      *
446      * @return void
447      */
448
449     function showContent()
450     {
451         if ($this->oprofile) {
452             $this->showPreviewForm();
453         } else {
454             $this->showInputForm();
455         }
456     }
457
458     function showScripts()
459     {
460         parent::showScripts();
461         $this->autofocus('feedurl');
462     }
463 }