]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/actions/ostatussub.php
redirect ostatussub if not logged in
[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 class OStatusSubAction extends Action
28 {
29     protected $profile_uri;
30     protected $preview;
31     protected $munger;
32
33     /**
34      * Title of the page
35      *
36      * @return string Title of the page
37      */
38
39     function title()
40     {
41         return _m('Authorize subscription');
42     }
43
44     /**
45      * Instructions for use
46      *
47      * @return instructions for use
48      */
49
50     function getInstructions()
51     {
52         return _m('You can subscribe to users from other supported sites. Paste their address or profile URI below:');
53     }
54
55     function showForm($error=null)
56     {
57         $this->error = $error;
58         $this->showPage();
59     }
60
61     /**
62      * Content area of the page
63      *
64      * Shows a form for associating a remote OStatus account with this
65      * StatusNet account.
66      *
67      * @return void
68      */
69
70     function showContent()
71     {
72         // @fixme is this right place?
73         if ($this->error) {
74             $this->text($this->error);
75         }
76
77         $user = common_current_user();
78
79         $profile = $user->getProfile();
80
81         $this->elementStart('form', array('method' => 'post',
82                                           'id' => 'ostatus_sub',
83                                           'class' => 'form_settings',
84                                           'action' =>
85                                           common_local_url('ostatussub')));
86
87         $this->hidden('token', common_session_token());
88
89         $this->elementStart('fieldset', array('id' => 'settings_feeds'));
90
91         $this->elementStart('ul', 'form_data');
92         $this->elementStart('li');
93         $this->input('profile',
94                      _m('Address or profile URL'),
95                      $this->profile_uri,
96                      _m('Enter the profile URL of a PubSubHubbub-enabled feed'));
97         $this->elementEnd('li');
98         $this->elementEnd('ul');
99
100         if ($this->preview) {
101             $this->submit('subscribe', _m('Subscribe'));
102         } else {
103             $this->submit('validate', _m('Continue'));
104         }
105
106         $this->elementEnd('fieldset');
107
108         $this->elementEnd('form');
109
110         if ($this->preview) {
111             $this->previewFeed();
112         }
113     }
114
115     function prepare($args)
116     {
117         parent::prepare($args);
118
119         if (!common_logged_in()) {
120             // XXX: selfURL() didn't work. :<
121             common_set_returnto($_SERVER['REQUEST_URI']);
122             if (Event::handle('RedirectToLogin', array($this, null))) {
123                 common_redirect(common_local_url('login'), 303);
124             }
125             return false;
126         }
127
128         $this->profile_uri = $this->arg('profile');
129
130         return true;
131     }
132
133     function handle($args)
134     {
135         parent::handle($args);
136         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
137             $this->handlePost();
138         } else {
139             if ($this->profile_uri) {
140                 $this->validateAndPreview();
141             } else {
142                 $this->showPage();
143             }
144         }
145     }
146
147     /**
148      * Handle posts to this form
149      *
150      * Based on the button that was pressed, muxes out to other functions
151      * to do the actual task requested.
152      *
153      * All sub-functions reload the form with a message -- success or failure.
154      *
155      * @return void
156      */
157
158     function handlePost()
159     {
160         // CSRF protection
161         $token = $this->trimmed('token');
162         if (!$token || $token != common_session_token()) {
163             $this->showForm(_('There was a problem with your session token. '.
164                               'Try again, please.'));
165             return;
166         }
167
168         if ($this->arg('validate')) {
169             $this->validateAndPreview();
170         } else if ($this->arg('subscribe')) {
171             $this->saveFeed();
172         } else {
173             $this->showForm(_('Unexpected form submission.'));
174         }
175     }
176
177     /**
178      * Set up and add a feed
179      *
180      * @return boolean true if feed successfully read
181      * Sends you back to input form if not.
182      */
183     function validateFeed()
184     {
185         $profile_uri = trim($this->arg('profile'));
186
187         if ($profile_uri == '') {
188             $this->showForm(_m('Empty remote profile URL!'));
189             return;
190         }
191         $this->profile_uri = $profile_uri;
192
193         // @fixme validate, normalize bla bla
194         try {
195             $oprofile = Ostatus_profile::ensureProfile($this->profile_uri);
196             $this->oprofile = $oprofile;
197             return true;
198         } catch (FeedSubBadURLException $e) {
199             $err = _m('Invalid URL or could not reach server.');
200         } catch (FeedSubBadResponseException $e) {
201             $err = _m('Cannot read feed; server returned error.');
202         } catch (FeedSubEmptyException $e) {
203             $err = _m('Cannot read feed; server returned an empty page.');
204         } catch (FeedSubBadHTMLException $e) {
205             $err = _m('Bad HTML, could not find feed link.');
206         } catch (FeedSubNoFeedException $e) {
207             $err = _m('Could not find a feed linked from this URL.');
208         } catch (FeedSubUnrecognizedTypeException $e) {
209             $err = _m('Not a recognized feed type.');
210         } catch (FeedSubException $e) {
211             // Any new ones we forgot about
212             $err = sprintf(_m('Bad feed URL: %s %s'), get_class($e), $e->getMessage());
213         }
214
215         $this->showForm($err);
216         return false;
217     }
218
219     function saveFeed()
220     {
221         if ($this->validateFeed()) {
222             $this->preview = true;
223
224             // And subscribe the current user to the local profile
225             $user = common_current_user();
226
227             if (!$this->oprofile->subscribe()) {
228                 $this->showForm(_m("Failed to set up server-to-server subscription."));
229                 return;
230             }
231
232             if ($this->oprofile->isGroup()) {
233                 $group = $this->oprofile->localGroup();
234                 if ($user->isMember($group)) {
235                     $this->showForm(_m('Already a member!'));
236                 } elseif (Group_member::join($this->profile->group_id, $user->id)) {
237                     $this->showForm(_m('Joined remote group!'));
238                 } else {
239                     $this->showForm(_m('Remote group join failed!'));
240                 }
241             } else {
242                 $local = $this->oprofile->localProfile();
243                 if ($user->isSubscribed($local)) {
244                     $this->showForm(_m('Already subscribed!'));
245                 } elseif ($this->oprofile->subscribeLocalToRemote($user)) {
246                     $this->showForm(_m('Remote user subscribed!'));
247                 } else {
248                     $this->showForm(_m('Remote subscription failed!'));
249                 }
250             }
251         }
252     }
253
254     function validateAndPreview()
255     {
256         if ($this->validateFeed()) {
257             $this->preview = true;
258             $this->showForm(_m('Previewing feed:'));
259         }
260     }
261
262     function previewFeed()
263     {
264         $this->text('Profile preview should go here');
265     }
266
267     function showScripts()
268     {
269         parent::showScripts();
270         $this->autofocus('feedurl');
271     }
272 }