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 FeedSubPlugin
22 * @maintainer Brion Vibber <brion@status.net>
25 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
27 class FeedSubSettingsAction extends ConnectSettingsAction
29 protected $profile_uri;
36 * @return string Title of the page
41 return _m('Feed subscriptions');
45 * Instructions for use
47 * @return instructions for use
50 function getInstructions()
52 return _m('You can subscribe to feeds from other sites; ' .
53 'updates will appear in your personal timeline.');
57 * Content area of the page
59 * Shows a form for associating a Twitter account with this
60 * StatusNet account. Also lets the user set preferences.
65 function showContent()
67 $user = common_current_user();
69 $profile = $user->getProfile();
71 $this->elementStart('form', array('method' => 'post',
72 'id' => 'form_settings_feedsub',
73 'class' => 'form_settings',
75 common_local_url('feedsubsettings')));
77 $this->hidden('token', common_session_token());
79 $this->elementStart('fieldset', array('id' => 'settings_feeds'));
81 $this->elementStart('ul', 'form_data');
82 $this->elementStart('li', array('id' => 'settings_twitter_login_button'));
83 $this->input('profile_uri',
86 _m('Enter the profile URL of a PubSubHubbub-enabled feed'));
87 $this->elementEnd('li');
88 $this->elementEnd('ul');
91 $this->submit('subscribe', _m('Subscribe'));
93 $this->submit('validate', _m('Continue'));
96 $this->elementEnd('fieldset');
98 $this->elementEnd('form');
100 if ($this->preview) {
101 $this->previewFeed();
106 * Handle posts to this form
108 * Based on the button that was pressed, muxes out to other functions
109 * to do the actual task requested.
111 * All sub-functions reload the form with a message -- success or failure.
116 function handlePost()
119 $token = $this->trimmed('token');
120 if (!$token || $token != common_session_token()) {
121 $this->showForm(_('There was a problem with your session token. '.
122 'Try again, please.'));
126 if ($this->arg('validate')) {
127 $this->validateAndPreview();
128 } else if ($this->arg('subscribe')) {
131 $this->showForm(_('Unexpected form submission.'));
136 * Set up and add a feed
138 * @return boolean true if feed successfully read
139 * Sends you back to input form if not.
141 function validateFeed()
143 $profile_uri = trim($this->arg('profile_uri'));
145 if ($profile_uri == '') {
146 $this->showForm(_m('Empty remote profile URL!'));
149 $this->profile_uri = $profile_uri;
151 // @fixme validate, normalize bla bla
153 $oprofile = Ostatus_profile::ensureProfile($this->profile_uri);
154 $this->oprofile = $oprofile;
156 } catch (FeedSubBadURLException $e) {
157 $err = _m('Invalid URL or could not reach server.');
158 } catch (FeedSubBadResponseException $e) {
159 $err = _m('Cannot read feed; server returned error.');
160 } catch (FeedSubEmptyException $e) {
161 $err = _m('Cannot read feed; server returned an empty page.');
162 } catch (FeedSubBadHTMLException $e) {
163 $err = _m('Bad HTML, could not find feed link.');
164 } catch (FeedSubNoFeedException $e) {
165 $err = _m('Could not find a feed linked from this URL.');
166 } catch (FeedSubUnrecognizedTypeException $e) {
167 $err = _m('Not a recognized feed type.');
168 } catch (FeedSubException $e) {
169 // Any new ones we forgot about
170 $err = sprintf(_m('Bad feed URL: %s %s'), get_class($e), $e->getMessage());
173 $this->showForm($err);
179 if ($this->validateFeed()) {
180 $this->preview = true;
182 // And subscribe the current user to the local profile
183 $user = common_current_user();
185 if (!$this->oprofile->subscribe()) {
186 $this->showForm(_m("Failed to set up server-to-server subscription."));
190 if ($this->oprofile->isGroup()) {
191 $group = $this->oprofile->localGroup();
192 if ($user->isMember($group)) {
193 $this->showForm(_m('Already a member!'));
194 } elseif (Group_member::join($this->profile->group_id, $user->id)) {
195 $this->showForm(_m('Joined remote group!'));
197 $this->showForm(_m('Remote group join failed!'));
200 $local = $this->oprofile->localProfile();
201 if ($user->isSubscribed($local)) {
202 $this->showForm(_m('Already subscribed!'));
203 } elseif ($this->oprofile->subscribeLocalToRemote($user)) {
204 $this->showForm(_m('Remote user subscribed!'));
206 $this->showForm(_m('Remote subscription failed!'));
212 function validateAndPreview()
214 if ($this->validateFeed()) {
215 $this->preview = true;
216 $this->showForm(_m('Previewing feed:'));
220 function previewFeed()
222 $this->text('Profile preview should go here');
225 function showScripts()
227 parent::showScripts();
228 $this->autofocus('feedurl');