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