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;
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');
67 $this->elementEnd('body');
68 $this->elementEnd('html');
74 function showPageNotice()
77 $this->element('p', 'error', $this->error);
82 * Content area of the page
84 * Shows a form for associating a remote OStatus account with this
90 function showContent()
92 $user = common_current_user();
94 $profile = $user->getProfile();
96 $this->elementStart('form', array('method' => 'post',
97 'id' => 'form_ostatus_sub',
98 'class' => 'form_settings',
100 common_local_url('ostatussub')));
102 $this->hidden('token', common_session_token());
104 $this->elementStart('fieldset', array('id' => 'settings_feeds'));
106 $this->elementStart('ul', 'form_data');
107 $this->elementStart('li');
108 $this->input('profile',
109 _m('Address or profile URL'),
111 _m('Enter the profile URL of a PubSubHubbub-enabled feed'));
112 $this->elementEnd('li');
113 $this->elementEnd('ul');
115 if ($this->preview) {
116 $this->submit('subscribe', _m('Subscribe'));
118 $this->submit('validate', _m('Continue'));
121 $this->elementEnd('fieldset');
123 $this->elementEnd('form');
125 if ($this->preview) {
126 $this->previewFeed();
130 function prepare($args)
132 parent::prepare($args);
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);
143 $this->profile_uri = $this->arg('profile');
148 function handle($args)
150 parent::handle($args);
151 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
154 if ($this->profile_uri) {
155 $this->validateAndPreview();
163 * Handle posts to this form
165 * Based on the button that was pressed, muxes out to other functions
166 * to do the actual task requested.
168 * All sub-functions reload the form with a message -- success or failure.
173 function handlePost()
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.'));
183 if ($this->arg('validate')) {
184 $this->validateAndPreview();
185 } else if ($this->arg('subscribe')) {
188 $this->showForm(_('Unexpected form submission.'));
193 * Set up and add a feed
195 * @return boolean true if feed successfully read
196 * Sends you back to input form if not.
198 function validateFeed()
200 $profile_uri = trim($this->arg('profile'));
202 if ($profile_uri == '') {
203 $this->showForm(_m('Empty remote profile URL!'));
206 $this->profile_uri = $profile_uri;
208 // @fixme validate, normalize bla bla
210 $oprofile = Ostatus_profile::ensureProfile($this->profile_uri);
211 $this->oprofile = $oprofile;
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());
230 $this->showForm($err);
236 if ($this->validateFeed()) {
237 $this->preview = true;
239 // And subscribe the current user to the local profile
240 $user = common_current_user();
242 if (!$this->oprofile->subscribe()) {
243 $this->showForm(_m("Failed to set up server-to-server subscription."));
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->profile->group_id, $user->id)) {
252 $this->showForm(_m('Joined remote group!'));
254 $this->showForm(_m('Remote group join failed!'));
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!'));
263 $this->showForm(_m('Remote subscription failed!'));
269 function validateAndPreview()
271 if ($this->validateFeed()) {
272 $this->preview = true;
277 function previewFeed()
279 $this->text('Profile preview should go here');
282 function showScripts()
284 parent::showScripts();
285 $this->autofocus('feedurl');