]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/actions/ostatussub.php
Merge branch 'testing' of git@gitorious.org:statusnet/mainline into testing
[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         $this->elementStart('form', array('method' => 'post',
91                                           'id' => 'form_ostatus_sub',
92                                           'class' => 'form_settings',
93                                           'action' =>
94                                           common_local_url('ostatussub')));
95
96         $this->hidden('token', common_session_token());
97         $this->hidden('profile', $this->profile_uri);
98
99         $this->elementStart('fieldset', array('id' => 'settings_feeds'));
100
101         if ($this->oprofile->isGroup()) {
102             $this->previewGroup();
103             $this->submit('subscribe', _m('Join'));
104         } else {
105             $this->previewUser();
106             $this->submit('subscribe', _m('Subscribe'));
107         }
108
109
110         $this->elementEnd('fieldset');
111
112         $this->elementEnd('form');
113     }
114
115     /**
116      * Show a preview for a remote user's profile
117      */
118     function previewUser()
119     {
120         $oprofile = $this->oprofile;
121         $profile = $oprofile->localProfile();
122
123         $this->text(sprintf(_m("Remote user %s"), $profile->nickname));
124         // ...
125     }
126
127     /**
128      * Show a preview for a remote group's profile
129      */
130     function previewGroup()
131     {
132         $oprofile = $this->oprofile;
133         $group = $oprofile->localGroup();
134
135         $this->text(sprintf(_m("Remote group %s"), $group->nickname));
136         // ..
137     }
138
139     /**
140      * Redirect on successful remote user subscription
141      */
142     function successUser()
143     {
144         $cur = common_current_user();
145         $url = common_local_url('subscriptions', array('nickname' => $cur->nickname));
146         common_redirect($url, 303);
147     }
148
149     /**
150      * Redirect on successful remote group join
151      */
152     function successGroup()
153     {
154         $cur = common_current_user();
155         $url = common_local_url('usergroups', array('nickname' => $cur->nickname));
156         common_redirect($url, 303);
157     }
158
159     /**
160      * Pull data for a remote profile and check if it's valid.
161      * Fills out error UI string in $this->error
162      * Fills out $this->oprofile on success.
163      *
164      * @return boolean
165      */
166     function validateFeed()
167     {
168         $profile_uri = trim($this->arg('profile'));
169
170         if ($profile_uri == '') {
171             $this->showForm(_m('Empty remote profile URL!'));
172             return;
173         }
174         $this->profile_uri = $profile_uri;
175
176         // @fixme validate, normalize bla bla
177         try {
178             $oprofile = Ostatus_profile::ensureProfile($this->profile_uri);
179             $this->oprofile = $oprofile;
180             return true;
181         } catch (FeedSubBadURLException $e) {
182             $this->error = _m('Invalid URL or could not reach server.');
183         } catch (FeedSubBadResponseException $e) {
184             $this->error = _m('Cannot read feed; server returned error.');
185         } catch (FeedSubEmptyException $e) {
186             $this->error = _m('Cannot read feed; server returned an empty page.');
187         } catch (FeedSubBadHTMLException $e) {
188             $this->error = _m('Bad HTML, could not find feed link.');
189         } catch (FeedSubNoFeedException $e) {
190             $this->error = _m('Could not find a feed linked from this URL.');
191         } catch (FeedSubUnrecognizedTypeException $e) {
192             $this->error = _m('Not a recognized feed type.');
193         } catch (FeedSubException $e) {
194             // Any new ones we forgot about
195             $this->error = sprintf(_m('Bad feed URL: %s %s'), get_class($e), $e->getMessage());
196         }
197
198         return false;
199     }
200
201     /**
202      * Attempt to finalize subscription.
203      * validateFeed must have been run first.
204      *
205      * Calls showForm on failure or successUser/successGroup on success.
206      */
207     function saveFeed()
208     {
209         // And subscribe the current user to the local profile
210         $user = common_current_user();
211
212         if (!$this->oprofile->subscribe()) {
213             $this->showForm(_m("Failed to set up server-to-server subscription."));
214             return;
215         }
216
217         if ($this->oprofile->isGroup()) {
218             $group = $this->oprofile->localGroup();
219             if ($user->isMember($group)) {
220                 $this->showForm(_m('Already a member!'));
221             } elseif (Group_member::join($this->oprofile->group_id, $user->id)) {
222                 $this->successGroup();
223             } else {
224                 $this->showForm(_m('Remote group join failed!'));
225             }
226         } else {
227             $local = $this->oprofile->localProfile();
228             if ($user->isSubscribed($local)) {
229                 $this->showForm(_m('Already subscribed!'));
230             } elseif ($this->oprofile->subscribeLocalToRemote($user)) {
231                 $this->successUser();
232             } else {
233                 $this->showForm(_m('Remote subscription failed!'));
234             }
235         }
236     }
237
238     function prepare($args)
239     {
240         parent::prepare($args);
241
242         if (!common_logged_in()) {
243             // XXX: selfURL() didn't work. :<
244             common_set_returnto($_SERVER['REQUEST_URI']);
245             if (Event::handle('RedirectToLogin', array($this, null))) {
246                 common_redirect(common_local_url('login'), 303);
247             }
248             return false;
249         }
250
251         $this->profile_uri = $this->arg('profile');
252
253         return true;
254     }
255
256     /**
257      * Handle the submission.
258      */
259     function handle($args)
260     {
261         parent::handle($args);
262         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
263             $this->handlePost();
264         } else {
265             if ($this->arg('profile')) {
266                 $this->validateFeed();
267             }
268             $this->showForm();
269         }
270     }
271
272
273     /**
274      * Handle posts to this form
275      *
276      * @return void
277      */
278
279     function handlePost()
280     {
281         // CSRF protection
282         $token = $this->trimmed('token');
283         if (!$token || $token != common_session_token()) {
284             $this->showForm(_('There was a problem with your session token. '.
285                               'Try again, please.'));
286             return;
287         }
288
289         if ($this->validateFeed()) {
290             if ($this->arg('subscribe')) {
291                 $this->saveFeed();
292                 return;
293             }
294         }
295         $this->showForm();
296     }
297
298     /**
299      * Show the appropriate form based on our input state.
300      */
301     function showForm($err=null)
302     {
303         if ($err) {
304             $this->error = $err;
305         }
306         if ($this->boolean('ajax')) {
307             header('Content-Type: text/xml;charset=utf-8');
308             $this->xw->startDocument('1.0', 'UTF-8');
309             $this->elementStart('html');
310             $this->elementStart('head');
311             $this->element('title', null, _m('Subscribe to user'));
312             $this->elementEnd('head');
313             $this->elementStart('body');
314             $this->showContent();
315             $this->elementEnd('body');
316             $this->elementEnd('html');
317         } else {
318             $this->showPage();
319         }
320     }
321
322     /**
323      * Title of the page
324      *
325      * @return string Title of the page
326      */
327
328     function title()
329     {
330         return _m('Authorize subscription');
331     }
332
333     /**
334      * Instructions for use
335      *
336      * @return instructions for use
337      */
338
339     function getInstructions()
340     {
341         return _m('You can subscribe to users from other supported sites. Paste their address or profile URI below:');
342     }
343
344     function showPageNotice()
345     {
346         if ($this->error) {
347             $this->element('p', 'error', $this->error);
348         }
349     }
350
351     /**
352      * Content area of the page
353      *
354      * Shows a form for associating a remote OStatus account with this
355      * StatusNet account.
356      *
357      * @return void
358      */
359
360     function showContent()
361     {
362         if ($this->oprofile) {
363             $this->showPreviewForm();
364         } else {
365             $this->showInputForm();
366         }
367     }
368
369     function showScripts()
370     {
371         parent::showScripts();
372         $this->autofocus('feedurl');
373     }
374 }