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); }
27 class OStatusSubAction extends Action
29 protected $profile_uri;
36 * @return string Title of the page
41 return _m('Authorize subscription');
45 * Instructions for use
47 * @return instructions for use
50 function getInstructions()
52 return _m('You can subscribe to users from other supported sites. Paste their address or profile URI below:');
55 function showForm($error=null)
57 $this->error = $error;
62 * Content area of the page
64 * Shows a form for associating a remote OStatus account with this
70 function showContent()
72 // @fixme is this right place?
74 $this->text($this->error);
77 $user = common_current_user();
79 $profile = $user->getProfile();
81 $this->elementStart('form', array('method' => 'post',
82 'id' => 'ostatus_sub',
83 'class' => 'form_settings',
85 common_local_url('ostatussub')));
87 $this->hidden('token', common_session_token());
89 $this->elementStart('fieldset', array('id' => 'settings_feeds'));
91 $this->elementStart('ul', 'form_data');
92 $this->elementStart('li');
93 $this->input('profile',
94 _m('Address or profile URL'),
96 _m('Enter the profile URL of a PubSubHubbub-enabled feed'));
97 $this->elementEnd('li');
98 $this->elementEnd('ul');
100 if ($this->preview) {
101 $this->submit('subscribe', _m('Subscribe'));
103 $this->submit('validate', _m('Continue'));
106 $this->elementEnd('fieldset');
108 $this->elementEnd('form');
110 if ($this->preview) {
111 $this->previewFeed();
115 function prepare($args)
117 parent::prepare($args);
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);
128 $this->profile_uri = $this->arg('profile');
133 function handle($args)
135 parent::handle($args);
136 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
139 if ($this->profile_uri) {
140 $this->validateAndPreview();
148 * Handle posts to this form
150 * Based on the button that was pressed, muxes out to other functions
151 * to do the actual task requested.
153 * All sub-functions reload the form with a message -- success or failure.
158 function handlePost()
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.'));
168 if ($this->arg('validate')) {
169 $this->validateAndPreview();
170 } else if ($this->arg('subscribe')) {
173 $this->showForm(_('Unexpected form submission.'));
178 * Set up and add a feed
180 * @return boolean true if feed successfully read
181 * Sends you back to input form if not.
183 function validateFeed()
185 $profile_uri = trim($this->arg('profile'));
187 if ($profile_uri == '') {
188 $this->showForm(_m('Empty remote profile URL!'));
191 $this->profile_uri = $profile_uri;
193 // @fixme validate, normalize bla bla
195 $oprofile = Ostatus_profile::ensureProfile($this->profile_uri);
196 $this->oprofile = $oprofile;
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());
215 $this->showForm($err);
221 if ($this->validateFeed()) {
222 $this->preview = true;
224 // And subscribe the current user to the local profile
225 $user = common_current_user();
227 if (!$this->oprofile->subscribe()) {
228 $this->showForm(_m("Failed to set up server-to-server subscription."));
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!'));
239 $this->showForm(_m('Remote group join failed!'));
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!'));
248 $this->showForm(_m('Remote subscription failed!'));
254 function validateAndPreview()
256 if ($this->validateFeed()) {
257 $this->preview = true;
258 $this->showForm(_m('Previewing feed:'));
262 function previewFeed()
264 $this->text('Profile preview should go here');
267 function showScripts()
269 parent::showScripts();
270 $this->autofocus('feedurl');