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