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