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