3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2010, 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 James Walker <james@status.net>
25 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
27 class OStatusSubAction extends Action
34 return _m("OStatus Subscribe");
37 function handle($args)
39 if ($this->validateFeed()) {
47 function showForm($err = null)
54 function showContent()
56 $user = common_current_user();
58 $profile = $user->getProfile();
62 $flink = Foreign_link::getByUserID($user->id, FEEDSUB_SERVICE);
65 $fuser = $flink->getForeignUser();
68 $this->elementStart('form', array('method' => 'post',
69 'id' => 'form_settings_feedsub',
70 'class' => 'form_settings',
72 common_local_url('feedsubsettings')));
74 $this->hidden('token', common_session_token());
76 $this->elementStart('fieldset', array('id' => 'settings_feeds'));
78 $this->elementStart('ul', 'form_data');
79 $this->elementStart('li', array('id' => 'settings_twitter_login_button'));
80 $this->input('feedurl', _('Feed URL'), $this->feedurl, _('Enter the URL of a PubSubHubbub-enabled feed'));
81 $this->elementEnd('li');
82 $this->elementEnd('ul');
84 $this->submit('subscribe', _m('Subscribe'));
86 $this->elementEnd('fieldset');
88 $this->elementEnd('form');
94 * Handle posts to this form
96 * Based on the button that was pressed, muxes out to other functions
97 * to do the actual task requested.
99 * All sub-functions reload the form with a message -- success or failure.
104 function handlePost()
107 $token = $this->trimmed('token');
108 if (!$token || $token != common_session_token()) {
109 $this->showForm(_('There was a problem with your session token. '.
110 'Try again, please.'));
114 if ($this->arg('subscribe')) {
117 $this->showForm(_('Unexpected form submission.'));
123 * Set up and add a feed
125 * @return boolean true if feed successfully read
126 * Sends you back to input form if not.
128 function validateFeed()
130 $feedurl = $this->trimmed('feed');
132 if ($feedurl == '') {
133 $this->showForm(_m('Empty feed URL!'));
136 $this->feedurl = $feedurl;
138 // Get the canonical feed URI and check it
140 $discover = new FeedDiscovery();
141 $uri = $discover->discoverFromURL($feedurl);
142 } catch (FeedSubBadURLException $e) {
143 $this->showForm(_m('Invalid URL or could not reach server.'));
145 } catch (FeedSubBadResponseException $e) {
146 $this->showForm(_m('Cannot read feed; server returned error.'));
148 } catch (FeedSubEmptyException $e) {
149 $this->showForm(_m('Cannot read feed; server returned an empty page.'));
151 } catch (FeedSubBadHTMLException $e) {
152 $this->showForm(_m('Bad HTML, could not find feed link.'));
154 } catch (FeedSubNoFeedException $e) {
155 $this->showForm(_m('Could not find a feed linked from this URL.'));
157 } catch (FeedSubUnrecognizedTypeException $e) {
158 $this->showForm(_m('Not a recognized feed type.'));
160 } catch (FeedSubException $e) {
161 // Any new ones we forgot about
162 $this->showForm(_m('Bad feed URL.'));
166 $this->munger = $discover->feedMunger();
167 $this->feedinfo = $this->munger->feedInfo();
169 if ($this->feedinfo->huburi == '') {
170 $this->showForm(_m('Feed is not PuSH-enabled; cannot subscribe.'));
179 if ($this->validateFeed()) {
180 $this->preview = true;
181 $this->feedinfo = Feedinfo::ensureProfile($this->munger);
183 // If not already in use, subscribe to updates via the hub
184 if ($this->feedinfo->sub_start) {
185 common_log(LOG_INFO, __METHOD__ . ": double the fun! new sub for {$this->feedinfo->feeduri} last subbed {$this->feedinfo->sub_start}");
187 $ok = $this->feedinfo->subscribe();
188 common_log(LOG_INFO, __METHOD__ . ": sub was $ok");
190 $this->showForm(_m('Feed subscription failed! Bad response from hub.'));
195 // And subscribe the current user to the local profile
196 $user = common_current_user();
197 $profile = $this->feedinfo->getProfile();
199 if ($user->isSubscribed($profile)) {
200 $this->showForm(_m('Already subscribed!'));
201 } elseif ($user->subscribeTo($profile)) {
202 $this->showForm(_m('Feed subscribed!'));
204 $this->showForm(_m('Feed subscription failed!'));
210 function previewFeed()
212 $feedinfo = $this->munger->feedinfo();
213 $notice = $this->munger->notice(0, true); // preview
216 $this->element('b', null, 'Preview of latest post from this feed:');
218 $item = new NoticeList($notice, $this);
221 $this->element('b', null, 'No posts in this feed yet.');