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;
63 * Content area of the page
65 * Shows a form for associating a remote OStatus account with this
71 function showContent()
73 // @fixme is this right place?
75 $this->text($this->error);
78 $user = common_current_user();
80 $profile = $user->getProfile();
82 $this->elementStart('form', array('method' => 'post',
83 'id' => 'ostatus_sub',
84 'class' => 'form_settings',
86 common_local_url('ostatussub')));
88 $this->hidden('token', common_session_token());
90 $this->elementStart('fieldset', array('id' => 'settings_feeds'));
92 $this->elementStart('ul', 'form_data');
93 $this->elementStart('li');
94 $this->input('profile',
95 _m('Address or profile URL'),
97 _m('Enter the profile URL of a PubSubHubbub-enabled feed'));
98 $this->elementEnd('li');
99 $this->elementEnd('ul');
101 if ($this->preview) {
102 $this->submit('subscribe', _m('Subscribe'));
104 $this->submit('validate', _m('Continue'));
107 $this->elementEnd('fieldset');
109 $this->elementEnd('form');
111 if ($this->preview) {
112 $this->previewFeed();
116 function prepare($args)
118 parent::prepare($args);
119 $this->profile_uri = $this->arg('profile');
123 function handle($args)
125 parent::handle($args);
126 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
129 if ($this->profile_uri) {
130 $this->validateAndPreview();
138 * Handle posts to this form
140 * Based on the button that was pressed, muxes out to other functions
141 * to do the actual task requested.
143 * All sub-functions reload the form with a message -- success or failure.
148 function handlePost()
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.'));
158 if ($this->arg('validate')) {
159 $this->validateAndPreview();
160 } else if ($this->arg('subscribe')) {
163 $this->showForm(_('Unexpected form submission.'));
168 * Set up and add a feed
170 * @return boolean true if feed successfully read
171 * Sends you back to input form if not.
173 function validateFeed()
175 $profile_uri = trim($this->arg('profile'));
177 if ($profile_uri == '') {
178 $this->showForm(_m('Empty remote profile URL!'));
181 $this->profile_uri = $profile_uri;
183 // @fixme validate, normalize bla bla
185 $oprofile = Ostatus_profile::ensureProfile($this->profile_uri);
186 $this->oprofile = $oprofile;
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());
205 $this->showForm($err);
211 if ($this->validateFeed()) {
212 $this->preview = true;
214 // And subscribe the current user to the local profile
215 $user = common_current_user();
217 if (!$this->oprofile->subscribe()) {
218 $this->showForm(_m("Failed to set up server-to-server subscription."));
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!'));
229 $this->showForm(_m('Remote group join failed!'));
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!'));
238 $this->showForm(_m('Remote subscription failed!'));
244 function validateAndPreview()
246 if ($this->validateFeed()) {
247 $this->preview = true;
248 $this->showForm(_m('Previewing feed:'));
252 function previewFeed()
254 $this->text('Profile preview should go here');
257 function showScripts()
259 parent::showScripts();
260 $this->autofocus('feedurl');