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
36 * @return string Title of the page
41 return dgettext('FeedSubPlugin', 'Feed subscriptions');
45 * Instructions for use
47 * @return instructions for use
50 function getInstructions()
52 return dgettext('FeedSubPlugin',
53 'You can subscribe to feeds from other sites; ' .
54 'updates will appear in your personal timeline.');
58 * Content area of the page
60 * Shows a form for associating a Twitter account with this
61 * StatusNet account. Also lets the user set preferences.
66 function showContent()
68 $user = common_current_user();
70 $profile = $user->getProfile();
74 $flink = Foreign_link::getByUserID($user->id, FEEDSUB_SERVICE);
77 $fuser = $flink->getForeignUser();
80 $this->elementStart('form', array('method' => 'post',
81 'id' => 'form_settings_feedsub',
82 'class' => 'form_settings',
84 common_local_url('feedsubsettings')));
86 $this->hidden('token', common_session_token());
88 $this->elementStart('fieldset', array('id' => 'settings_feeds'));
90 $this->elementStart('ul', 'form_data');
91 $this->elementStart('li', array('id' => 'settings_twitter_login_button'));
92 $this->input('feedurl', _('Feed URL'), $this->feedurl, _('Enter the URL of a PubSubHubbub-enabled feed'));
93 $this->elementEnd('li');
94 $this->elementEnd('ul');
97 $this->submit('subscribe', dgettext('FeedSubPlugin', 'Subscribe'));
99 $this->submit('validate', dgettext('FeedSubPlugin', 'Continue'));
102 $this->elementEnd('fieldset');
104 $this->elementEnd('form');
106 if ($this->preview) {
107 $this->previewFeed();
112 * Handle posts to this form
114 * Based on the button that was pressed, muxes out to other functions
115 * to do the actual task requested.
117 * All sub-functions reload the form with a message -- success or failure.
122 function handlePost()
125 $token = $this->trimmed('token');
126 if (!$token || $token != common_session_token()) {
127 $this->showForm(_('There was a problem with your session token. '.
128 'Try again, please.'));
132 if ($this->arg('validate')) {
133 $this->validateAndPreview();
134 } else if ($this->arg('subscribe')) {
137 $this->showForm(_('Unexpected form submission.'));
142 * Set up and add a feed
144 * @return boolean true if feed successfully read
145 * Sends you back to input form if not.
147 function validateFeed()
149 $feedurl = trim($this->arg('feedurl'));
151 if ($feedurl == '') {
152 $this->showForm(dgettext('FeedSubPlugin',
156 $this->feedurl = $feedurl;
158 // Get the canonical feed URI and check it
160 $discover = new FeedDiscovery();
161 $uri = $discover->discoverFromURL($feedurl);
162 } catch (FeedSubBadURLException $e) {
163 $this->showForm(dgettext('FeedSubPlugin', 'Invalid URL or could not reach server.'));
165 } catch (FeedSubBadResponseException $e) {
166 $this->showForm(dgettext('FeedSubPlugin', 'Cannot read feed; server returned error.'));
168 } catch (FeedSubEmptyException $e) {
169 $this->showForm(dgettext('FeedSubPlugin', 'Cannot read feed; server returned an empty page.'));
171 } catch (FeedSubBadHTMLException $e) {
172 $this->showForm(dgettext('FeedSubPlugin', 'Bad HTML, could not find feed link.'));
174 } catch (FeedSubNoFeedException $e) {
175 $this->showForm(dgettext('FeedSubPlugin', 'Could not find a feed linked from this URL.'));
177 } catch (FeedSubUnrecognizedTypeException $e) {
178 $this->showForm(dgettext('FeedSubPlugin', 'Not a recognized feed type.'));
180 } catch (FeedSubException $e) {
181 // Any new ones we forgot about
182 $this->showForm(dgettext('FeedSubPlugin', 'Bad feed URL.'));
186 $this->munger = $discover->feedMunger();
187 $this->feedinfo = $this->munger->feedInfo();
189 if ($this->feedinfo->huburi == '') {
190 $this->showForm(dgettext('FeedSubPlugin', 'Feed is not PuSH-enabled; cannot subscribe.'));
199 if ($this->validateFeed()) {
200 $this->preview = true;
201 $this->feedinfo = Feedinfo::ensureProfile($this->munger);
203 // If not already in use, subscribe to updates via the hub
204 if ($this->feedinfo->sub_start) {
205 common_log(LOG_INFO, __METHOD__ . ": double the fun! new sub for {$this->feedinfo->feeduri} last subbed {$this->feedinfo->sub_start}");
207 $ok = $this->feedinfo->subscribe();
208 common_log(LOG_INFO, __METHOD__ . ": sub was $ok");
210 $this->showForm(dgettext('FeedSubPlugin', 'Feed subscription failed! Bad response from hub.'));
215 // And subscribe the current user to the local profile
216 $user = common_current_user();
217 $profile = $this->feedinfo->getProfile();
219 if ($user->isSubscribed($profile)) {
220 $this->showForm(dgettext('FeedSubPlugin', 'Already subscribed!'));
221 } elseif ($user->subscribeTo($profile)) {
222 $this->showForm(dgettext('FeedSubPlugin', 'Feed subscribed!'));
224 $this->showForm(dgettext('FeedSubPlugin', 'Feed subscription failed!'));
229 function validateAndPreview()
231 if ($this->validateFeed()) {
232 $this->preview = true;
233 $this->showForm(dgettext('FeedSubPlugin', 'Previewing feed:'));
237 function previewFeed()
239 $feedinfo = $this->munger->feedinfo();
240 $notice = $this->munger->notice(0, true); // preview
243 $this->element('b', null, 'Preview of latest post from this feed:');
245 $item = new NoticeList($notice, $this);
248 $this->element('b', null, 'No posts in this feed yet.');
252 function showScripts()
254 parent::showScripts();
255 $this->autofocus('feedurl');