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 _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();
73 $flink = Foreign_link::getByUserID($user->id, FEEDSUB_SERVICE);
76 $fuser = $flink->getForeignUser();
79 $this->elementStart('form', array('method' => 'post',
80 'id' => 'form_settings_feedsub',
81 'class' => 'form_settings',
83 common_local_url('feedsubsettings')));
85 $this->hidden('token', common_session_token());
87 $this->elementStart('fieldset', array('id' => 'settings_feeds'));
89 $this->elementStart('ul', 'form_data');
90 $this->elementStart('li', array('id' => 'settings_twitter_login_button'));
91 $this->input('feedurl', _('Feed URL'), $this->feedurl, _('Enter the URL of a PubSubHubbub-enabled feed'));
92 $this->elementEnd('li');
93 $this->elementEnd('ul');
96 $this->submit('subscribe', _m('Subscribe'));
98 $this->submit('validate', _m('Continue'));
101 $this->elementEnd('fieldset');
103 $this->elementEnd('form');
105 if ($this->preview) {
106 $this->previewFeed();
111 * Handle posts to this form
113 * Based on the button that was pressed, muxes out to other functions
114 * to do the actual task requested.
116 * All sub-functions reload the form with a message -- success or failure.
121 function handlePost()
124 $token = $this->trimmed('token');
125 if (!$token || $token != common_session_token()) {
126 $this->showForm(_('There was a problem with your session token. '.
127 'Try again, please.'));
131 if ($this->arg('validate')) {
132 $this->validateAndPreview();
133 } else if ($this->arg('subscribe')) {
136 $this->showForm(_('Unexpected form submission.'));
141 * Set up and add a feed
143 * @return boolean true if feed successfully read
144 * Sends you back to input form if not.
146 function validateFeed()
148 $feedurl = trim($this->arg('feedurl'));
150 if ($feedurl == '') {
151 $this->showForm(_m('Empty feed URL!'));
154 $this->feedurl = $feedurl;
156 // Get the canonical feed URI and check it
158 $discover = new FeedDiscovery();
159 $uri = $discover->discoverFromURL($feedurl);
160 } catch (FeedSubBadURLException $e) {
161 $this->showForm(_m('Invalid URL or could not reach server.'));
163 } catch (FeedSubBadResponseException $e) {
164 $this->showForm(_m('Cannot read feed; server returned error.'));
166 } catch (FeedSubEmptyException $e) {
167 $this->showForm(_m('Cannot read feed; server returned an empty page.'));
169 } catch (FeedSubBadHTMLException $e) {
170 $this->showForm(_m('Bad HTML, could not find feed link.'));
172 } catch (FeedSubNoFeedException $e) {
173 $this->showForm(_m('Could not find a feed linked from this URL.'));
175 } catch (FeedSubUnrecognizedTypeException $e) {
176 $this->showForm(_m('Not a recognized feed type.'));
178 } catch (FeedSubException $e) {
179 // Any new ones we forgot about
180 $this->showForm(_m('Bad feed URL.'));
184 $this->munger = $discover->feedMunger();
185 $this->feedinfo = $this->munger->feedInfo();
187 if ($this->feedinfo->huburi == '' && !common_config('feedsub', 'nohub')) {
188 $this->showForm(_m('Feed is not PuSH-enabled; cannot subscribe.'));
197 if ($this->validateFeed()) {
198 $this->preview = true;
199 $this->feedinfo = Feedinfo::ensureProfile($this->munger);
201 // If not already in use, subscribe to updates via the hub
202 if ($this->feedinfo->sub_start) {
203 common_log(LOG_INFO, __METHOD__ . ": double the fun! new sub for {$this->feedinfo->feeduri} last subbed {$this->feedinfo->sub_start}");
205 $ok = $this->feedinfo->subscribe();
206 common_log(LOG_INFO, __METHOD__ . ": sub was $ok");
208 $this->showForm(_m('Feed subscription failed! Bad response from hub.'));
213 // And subscribe the current user to the local profile
214 $user = common_current_user();
215 $profile = $this->feedinfo->getProfile();
217 throw new ServerException("Feed profile was not saved properly.");
220 if ($user->isSubscribed($profile)) {
221 $this->showForm(_m('Already subscribed!'));
222 } elseif ($user->subscribeTo($profile)) {
223 $this->showForm(_m('Feed subscribed!'));
225 $this->showForm(_m('Feed subscription failed!'));
230 function validateAndPreview()
232 if ($this->validateFeed()) {
233 $this->preview = true;
234 $this->showForm(_m('Previewing feed:'));
238 function previewFeed()
240 $feedinfo = $this->munger->feedinfo();
241 $notice = $this->munger->notice(0, true); // preview
244 $this->element('b', null, 'Preview of latest post from this feed:');
246 $item = new NoticeList($notice, $this);
249 $this->element('b', null, 'No posts in this feed yet.');
253 function showScripts()
255 parent::showScripts();
256 $this->autofocus('feedurl');