3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2009, StatusNet, Inc.
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.
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.
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/>.
21 * @package OStatusPlugin
22 * @maintainer Brion Vibber <brion@status.net>
25 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
30 * showInputForm() - form asking for a remote profile account or URL
31 * We end up back here on errors
33 * showPreviewForm() - surrounding form for preview-and-confirm
34 * previewUser() - display profile for a remote user
35 * previewGroup() - display profile for a remote group
37 * successUser() - redirects to subscriptions page on subscribe
38 * successGroup() - redirects to groups page on join
40 class OStatusSubAction extends Action
42 protected $profile_uri; // provided acct: or URI of remote entity
43 protected $oprofile; // Ostatus_profile of remote entity, if valid
46 * Show the initial form, when we haven't yet been given a valid
49 function showInputForm()
51 $user = common_current_user();
53 $profile = $user->getProfile();
55 $this->elementStart('form', array('method' => 'post',
56 'id' => 'form_ostatus_sub',
57 'class' => 'form_settings',
59 common_local_url('ostatussub')));
61 $this->hidden('token', common_session_token());
63 $this->elementStart('fieldset', array('id' => 'settings_feeds'));
65 $this->elementStart('ul', 'form_data');
66 $this->elementStart('li');
67 $this->input('profile',
68 _m('Address or profile URL'),
70 _m('Enter the profile URL of a PubSubHubbub-enabled feed'));
71 $this->elementEnd('li');
72 $this->elementEnd('ul');
74 $this->submit('validate', _m('Continue'));
76 $this->elementEnd('fieldset');
78 $this->elementEnd('form');
82 * Show the preview-and-confirm form. We've got a valid remote
83 * profile and are ready to poke it!
85 * This controls the wrapper form; actual profile display will
86 * be in previewUser() or previewGroup() depending on the type.
88 function showPreviewForm()
90 $this->elementStart('form', array('method' => 'post',
91 'id' => 'form_ostatus_sub',
92 'class' => 'form_settings',
94 common_local_url('ostatussub')));
96 $this->hidden('token', common_session_token());
97 $this->hidden('profile', $this->profile_uri);
99 $this->elementStart('fieldset', array('id' => 'settings_feeds'));
101 if ($this->oprofile->isGroup()) {
102 $this->previewGroup();
103 $this->submit('subscribe', _m('Join'));
105 $this->previewUser();
106 $this->submit('subscribe', _m('Subscribe'));
110 $this->elementEnd('fieldset');
112 $this->elementEnd('form');
116 * Show a preview for a remote user's profile
118 function previewUser()
120 $oprofile = $this->oprofile;
121 $profile = $oprofile->localProfile();
123 $this->text(sprintf(_m("Remote user %s"), $profile->nickname));
128 * Show a preview for a remote group's profile
130 function previewGroup()
132 $oprofile = $this->oprofile;
133 $group = $oprofile->localGroup();
135 $this->text(sprintf(_m("Remote group %s"), $group->nickname));
140 * Redirect on successful remote user subscription
142 function successUser()
144 $cur = common_current_user();
145 $url = common_local_url('subscriptions', array('nickname' => $cur->nickname));
146 common_redirect($url, 303);
150 * Redirect on successful remote group join
152 function successGroup()
154 $cur = common_current_user();
155 $url = common_local_url('usergroups', array('nickname' => $cur->nickname));
156 common_redirect($url, 303);
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.
166 function validateFeed()
168 $profile_uri = trim($this->arg('profile'));
170 if ($profile_uri == '') {
171 $this->showForm(_m('Empty remote profile URL!'));
174 $this->profile_uri = $profile_uri;
176 // @fixme validate, normalize bla bla
178 $oprofile = Ostatus_profile::ensureProfile($this->profile_uri);
179 $this->oprofile = $oprofile;
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());
202 * Attempt to finalize subscription.
203 * validateFeed must have been run first.
205 * Calls showForm on failure or successUser/successGroup on success.
209 // And subscribe the current user to the local profile
210 $user = common_current_user();
212 if (!$this->oprofile->subscribe()) {
213 $this->showForm(_m("Failed to set up server-to-server subscription."));
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();
224 $this->showForm(_m('Remote group join failed!'));
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();
233 $this->showForm(_m('Remote subscription failed!'));
238 function prepare($args)
240 parent::prepare($args);
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);
251 $this->profile_uri = $this->arg('profile');
257 * Handle the submission.
259 function handle($args)
261 parent::handle($args);
262 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
265 if ($this->arg('profile')) {
266 $this->validateFeed();
274 * Handle posts to this form
279 function handlePost()
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.'));
289 if ($this->validateFeed()) {
290 if ($this->arg('subscribe')) {
299 * Show the appropriate form based on our input state.
301 function showForm($err=null)
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');
325 * @return string Title of the page
330 return _m('Authorize subscription');
334 * Instructions for use
336 * @return instructions for use
339 function getInstructions()
341 return _m('You can subscribe to users from other supported sites. Paste their address or profile URI below:');
344 function showPageNotice()
347 $this->element('p', 'error', $this->error);
352 * Content area of the page
354 * Shows a form for associating a remote OStatus account with this
360 function showContent()
362 if ($this->oprofile) {
363 $this->showPreviewForm();
365 $this->showInputForm();
369 function showScripts()
371 parent::showScripts();
372 $this->autofocus('feedurl');