]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/actions/feedsubsettings.php
Merge branch '0.9.x' of git@gitorious.org:statusnet/mainline into 0.9.x
[quix0rs-gnu-social.git] / plugins / OStatus / actions / feedsubsettings.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2009, StatusNet, Inc.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 /**
21  * @package FeedSubPlugin
22  * @maintainer Brion Vibber <brion@status.net>
23  */
24
25 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
26
27 class FeedSubSettingsAction extends ConnectSettingsAction
28 {
29     protected $feedurl;
30     protected $preview;
31     protected $munger;
32
33     /**
34      * Title of the page
35      *
36      * @return string Title of the page
37      */
38
39     function title()
40     {
41         return _m('Feed subscriptions');
42     }
43
44     /**
45      * Instructions for use
46      *
47      * @return instructions for use
48      */
49
50     function getInstructions()
51     {
52         return _m('You can subscribe to feeds from other sites; ' .
53                   'updates will appear in your personal timeline.');
54     }
55
56     /**
57      * Content area of the page
58      *
59      * Shows a form for associating a Twitter account with this
60      * StatusNet account. Also lets the user set preferences.
61      *
62      * @return void
63      */
64
65     function showContent()
66     {
67         $user = common_current_user();
68
69         $profile = $user->getProfile();
70
71         $fuser = null;
72
73         $flink = Foreign_link::getByUserID($user->id, FEEDSUB_SERVICE);
74
75         if (!empty($flink)) {
76             $fuser = $flink->getForeignUser();
77         }
78
79         $this->elementStart('form', array('method' => 'post',
80                                           'id' => 'form_settings_feedsub',
81                                           'class' => 'form_settings',
82                                           'action' =>
83                                           common_local_url('feedsubsettings')));
84
85         $this->hidden('token', common_session_token());
86
87         $this->elementStart('fieldset', array('id' => 'settings_feeds'));
88
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');
94
95         if ($this->preview) {
96             $this->submit('subscribe', _m('Subscribe'));
97         } else {
98             $this->submit('validate', _m('Continue'));
99         }
100
101         $this->elementEnd('fieldset');
102
103         $this->elementEnd('form');
104
105         if ($this->preview) {
106             $this->previewFeed();
107         }
108     }
109
110     /**
111      * Handle posts to this form
112      *
113      * Based on the button that was pressed, muxes out to other functions
114      * to do the actual task requested.
115      *
116      * All sub-functions reload the form with a message -- success or failure.
117      *
118      * @return void
119      */
120
121     function handlePost()
122     {
123         // CSRF protection
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.'));
128             return;
129         }
130
131         if ($this->arg('validate')) {
132             $this->validateAndPreview();
133         } else if ($this->arg('subscribe')) {
134             $this->saveFeed();
135         } else {
136             $this->showForm(_('Unexpected form submission.'));
137         }
138     }
139
140     /**
141      * Set up and add a feed
142      *
143      * @return boolean true if feed successfully read
144      * Sends you back to input form if not.
145      */
146     function validateFeed()
147     {
148         $feedurl = trim($this->arg('feedurl'));
149         
150         if ($feedurl == '') {
151             $this->showForm(_m('Empty feed URL!'));
152             return;
153         }
154         $this->feedurl = $feedurl;
155         
156         // Get the canonical feed URI and check it
157         try {
158             $discover = new FeedDiscovery();
159             $uri = $discover->discoverFromURL($feedurl);
160         } catch (FeedSubBadURLException $e) {
161             $this->showForm(_m('Invalid URL or could not reach server.'));
162             return false;
163         } catch (FeedSubBadResponseException $e) {
164             $this->showForm(_m('Cannot read feed; server returned error.'));
165             return false;
166         } catch (FeedSubEmptyException $e) {
167             $this->showForm(_m('Cannot read feed; server returned an empty page.'));
168             return false;
169         } catch (FeedSubBadHTMLException $e) {
170             $this->showForm(_m('Bad HTML, could not find feed link.'));
171             return false;
172         } catch (FeedSubNoFeedException $e) {
173             $this->showForm(_m('Could not find a feed linked from this URL.'));
174             return false;
175         } catch (FeedSubUnrecognizedTypeException $e) {
176             $this->showForm(_m('Not a recognized feed type.'));
177             return false;
178         } catch (FeedSubException $e) {
179             // Any new ones we forgot about
180             $this->showForm(_m('Bad feed URL.'));
181             return false;
182         }
183         
184         $this->munger = $discover->feedMunger();
185         $this->feedinfo = $this->munger->feedInfo();
186
187         if ($this->feedinfo->huburi == '' && !common_config('feedsub', 'nohub')) {
188             $this->showForm(_m('Feed is not PuSH-enabled; cannot subscribe.'));
189             return false;
190         }
191         
192         return true;
193     }
194
195     function saveFeed()
196     {
197         if ($this->validateFeed()) {
198             $this->preview = true;
199             $this->feedinfo = Feedinfo::ensureProfile($this->munger);
200
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}");
204             } else {
205                 $ok = $this->feedinfo->subscribe();
206                 common_log(LOG_INFO, __METHOD__ . ": sub was $ok");
207                 if (!$ok) {
208                     $this->showForm(_m('Feed subscription failed! Bad response from hub.'));
209                     return;
210                 }
211             }
212             
213             // And subscribe the current user to the local profile
214             $user = common_current_user();
215             $profile = $this->feedinfo->getProfile();
216             if (!$profile) {
217                 throw new ServerException("Feed profile was not saved properly.");
218             }
219
220             if ($user->isSubscribed($profile)) {
221                 $this->showForm(_m('Already subscribed!'));
222             } elseif ($user->subscribeTo($profile)) {
223                 $this->showForm(_m('Feed subscribed!'));
224             } else {
225                 $this->showForm(_m('Feed subscription failed!'));
226             }
227         }
228     }
229
230     function validateAndPreview()
231     {
232         if ($this->validateFeed()) {
233             $this->preview = true;
234             $this->showForm(_m('Previewing feed:'));
235         }
236     }
237
238     function previewFeed()
239     {
240         $feedinfo = $this->munger->feedinfo();
241         $notice = $this->munger->notice(0, true); // preview
242
243         if ($notice) {
244             $this->element('b', null, 'Preview of latest post from this feed:');
245
246             $item = new NoticeList($notice, $this);
247             $item->show();
248         } else {
249             $this->element('b', null, 'No posts in this feed yet.');
250         }
251     }
252
253     function showScripts()
254     {
255         parent::showScripts();
256         $this->autofocus('feedurl');
257     }
258 }