]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/actions/ostatussub.php
OStatus: renamed feedinfo table to ostatus_profile -- will cover remote ostatus peopl...
[quix0rs-gnu-social.git] / plugins / OStatus / actions / ostatussub.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, 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 OStatusPlugin
22  * @maintainer James Walker <james@status.net>
23  */
24
25 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
26
27 class OStatusSubAction extends Action
28 {
29
30     protected $feedurl;
31     
32     function title()
33     {
34         return _m("OStatus Subscribe");
35     }
36
37     function handle($args)
38     {
39         if ($this->validateFeed()) {
40             $this->showForm();
41         }
42
43         return true;
44
45     }
46
47     function showForm($err = null)
48     {
49         $this->err = $err;
50         $this->showPage();
51     }
52
53
54     function showContent()
55     {
56         $user = common_current_user();
57
58         $profile = $user->getProfile();
59
60         $fuser = null;
61
62         $flink = Foreign_link::getByUserID($user->id, FEEDSUB_SERVICE);
63
64         if (!empty($flink)) {
65             $fuser = $flink->getForeignUser();
66         }
67
68         $this->elementStart('form', array('method' => 'post',
69                                           'id' => 'form_settings_feedsub',
70                                           'class' => 'form_settings',
71                                           'action' =>
72                                           common_local_url('feedsubsettings')));
73
74         $this->hidden('token', common_session_token());
75
76         $this->elementStart('fieldset', array('id' => 'settings_feeds'));
77
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');
83
84         $this->submit('subscribe', _m('Subscribe'));
85
86         $this->elementEnd('fieldset');
87
88         $this->elementEnd('form');
89
90         $this->previewFeed();
91     }
92
93     /**
94      * Handle posts to this form
95      *
96      * Based on the button that was pressed, muxes out to other functions
97      * to do the actual task requested.
98      *
99      * All sub-functions reload the form with a message -- success or failure.
100      *
101      * @return void
102      */
103
104     function handlePost()
105     {
106         // CSRF protection
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.'));
111             return;
112         }
113
114         if ($this->arg('subscribe')) {
115             $this->saveFeed();
116         } else {
117             $this->showForm(_('Unexpected form submission.'));
118         }
119     }
120
121     
122     /**
123      * Set up and add a feed
124      *
125      * @return boolean true if feed successfully read
126      * Sends you back to input form if not.
127      */
128     function validateFeed()
129     {
130         $feedurl = $this->trimmed('feed');
131         
132         if ($feedurl == '') {
133             $this->showForm(_m('Empty feed URL!'));
134             return;
135         }
136         $this->feedurl = $feedurl;
137         
138         // Get the canonical feed URI and check it
139         try {
140             $discover = new FeedDiscovery();
141             $uri = $discover->discoverFromURL($feedurl);
142         } catch (FeedSubBadURLException $e) {
143             $this->showForm(_m('Invalid URL or could not reach server.'));
144             return false;
145         } catch (FeedSubBadResponseException $e) {
146             $this->showForm(_m('Cannot read feed; server returned error.'));
147             return false;
148         } catch (FeedSubEmptyException $e) {
149             $this->showForm(_m('Cannot read feed; server returned an empty page.'));
150             return false;
151         } catch (FeedSubBadHTMLException $e) {
152             $this->showForm(_m('Bad HTML, could not find feed link.'));
153             return false;
154         } catch (FeedSubNoFeedException $e) {
155             $this->showForm(_m('Could not find a feed linked from this URL.'));
156             return false;
157         } catch (FeedSubUnrecognizedTypeException $e) {
158             $this->showForm(_m('Not a recognized feed type.'));
159             return false;
160         } catch (FeedSubException $e) {
161             // Any new ones we forgot about
162             $this->showForm(_m('Bad feed URL.'));
163             return false;
164         }
165         
166         $this->munger = $discover->feedMunger();
167         $this->profile = $this->munger->ostatusProfile();
168
169         if ($this->profile->huburi == '') {
170             $this->showForm(_m('Feed is not PuSH-enabled; cannot subscribe.'));
171             return false;
172         }
173         
174         return true;
175     }
176
177     function saveFeed()
178     {
179         if ($this->validateFeed()) {
180             $this->preview = true;
181             $this->profile = Ostatus_profile::ensureProfile($this->munger);
182
183             // If not already in use, subscribe to updates via the hub
184             if ($this->profile->sub_start) {
185                 common_log(LOG_INFO, __METHOD__ . ": double the fun! new sub for {$this->profile->feeduri} last subbed {$this->profile->sub_start}");
186             } else {
187                 $ok = $this->profile->subscribe();
188                 common_log(LOG_INFO, __METHOD__ . ": sub was $ok");
189                 if (!$ok) {
190                     $this->showForm(_m('Feed subscription failed! Bad response from hub.'));
191                     return;
192                 }
193             }
194             
195             // And subscribe the current user to the local profile
196             $user = common_current_user();
197             $profile = $this->profile->getProfile();
198             
199             if ($user->isSubscribed($profile)) {
200                 $this->showForm(_m('Already subscribed!'));
201             } elseif ($user->subscribeTo($profile)) {
202                 $this->showForm(_m('Feed subscribed!'));
203             } else {
204                 $this->showForm(_m('Feed subscription failed!'));
205             }
206         }
207     }
208
209     
210     function previewFeed()
211     {
212         $profile = $this->munger->ostatusProfile();
213         $notice = $this->munger->notice(0, true); // preview
214
215         if ($notice) {
216             $this->element('b', null, 'Preview of latest post from this feed:');
217
218             $item = new NoticeList($notice, $this);
219             $item->show();
220         } else {
221             $this->element('b', null, 'No posts in this feed yet.');
222         }
223     }
224
225
226 }