]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/actions/feedsubsettings.php
aee4cee9af0942bb77e3f99bfa0e651aea2353e3
[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 $profile_uri;
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         $this->elementStart('form', array('method' => 'post',
72                                           'id' => 'form_settings_feedsub',
73                                           'class' => 'form_settings',
74                                           'action' =>
75                                           common_local_url('feedsubsettings')));
76
77         $this->hidden('token', common_session_token());
78
79         $this->elementStart('fieldset', array('id' => 'settings_feeds'));
80
81         $this->elementStart('ul', 'form_data');
82         $this->elementStart('li', array('id' => 'settings_twitter_login_button'));
83         $this->input('profile_uri',
84                      _m('Feed URL'),
85                      $this->profile_uri,
86                      _m('Enter the profile URL of a PubSubHubbub-enabled feed'));
87         $this->elementEnd('li');
88         $this->elementEnd('ul');
89
90         if ($this->preview) {
91             $this->submit('subscribe', _m('Subscribe'));
92         } else {
93             $this->submit('validate', _m('Continue'));
94         }
95
96         $this->elementEnd('fieldset');
97
98         $this->elementEnd('form');
99
100         if ($this->preview) {
101             $this->previewFeed();
102         }
103     }
104
105     /**
106      * Handle posts to this form
107      *
108      * Based on the button that was pressed, muxes out to other functions
109      * to do the actual task requested.
110      *
111      * All sub-functions reload the form with a message -- success or failure.
112      *
113      * @return void
114      */
115
116     function handlePost()
117     {
118         // CSRF protection
119         $token = $this->trimmed('token');
120         if (!$token || $token != common_session_token()) {
121             $this->showForm(_('There was a problem with your session token. '.
122                               'Try again, please.'));
123             return;
124         }
125
126         if ($this->arg('validate')) {
127             $this->validateAndPreview();
128         } else if ($this->arg('subscribe')) {
129             $this->saveFeed();
130         } else {
131             $this->showForm(_('Unexpected form submission.'));
132         }
133     }
134
135     /**
136      * Set up and add a feed
137      *
138      * @return boolean true if feed successfully read
139      * Sends you back to input form if not.
140      */
141     function validateFeed()
142     {
143         $profile_uri = trim($this->arg('profile_uri'));
144         
145         if ($profile_uri == '') {
146             $this->showForm(_m('Empty remote profile URL!'));
147             return;
148         }
149         $this->profile_uri = $profile_uri;
150         
151         // @fixme validate, normalize bla bla
152         try {
153             $oprofile = Ostatus_profile::ensureProfile($this->profile_uri);
154             $this->oprofile = $oprofile;
155             return true;
156         } catch (FeedSubBadURLException $e) {
157             $err = _m('Invalid URL or could not reach server.');
158         } catch (FeedSubBadResponseException $e) {
159             $err = _m('Cannot read feed; server returned error.');
160         } catch (FeedSubEmptyException $e) {
161             $err = _m('Cannot read feed; server returned an empty page.');
162         } catch (FeedSubBadHTMLException $e) {
163             $err = _m('Bad HTML, could not find feed link.');
164         } catch (FeedSubNoFeedException $e) {
165             $err = _m('Could not find a feed linked from this URL.');
166         } catch (FeedSubUnrecognizedTypeException $e) {
167             $err = _m('Not a recognized feed type.');
168         } catch (FeedSubException $e) {
169             // Any new ones we forgot about
170             $err = sprintf(_m('Bad feed URL: %s %s'), get_class($e), $e->getMessage());
171         }
172
173         $this->showForm($err);
174         return false;
175     }
176
177     function saveFeed()
178     {
179         if ($this->validateFeed()) {
180             $this->preview = true;
181
182             // And subscribe the current user to the local profile
183             $user = common_current_user();
184
185             if (!$this->oprofile->subscribe()) {
186                 $this->showForm(_m("Failed to set up server-to-server subscription."));
187                 return;
188             }
189
190             if ($this->oprofile->isGroup()) {
191                 $group = $this->oprofile->localGroup();
192                 if ($user->isMember($group)) {
193                     $this->showForm(_m('Already a member!'));
194                 } elseif (Group_member::join($this->profile->group_id, $user->id)) {
195                     $this->showForm(_m('Joined remote group!'));
196                 } else {
197                     $this->showForm(_m('Remote group join failed!'));
198                 }
199             } else {
200                 $local = $this->oprofile->localProfile();
201                 if ($user->isSubscribed($local)) {
202                     $this->showForm(_m('Already subscribed!'));
203                 } elseif ($this->oprofile->subscribeLocalToRemote($user)) {
204                     $this->showForm(_m('Remote user subscribed!'));
205                 } else {
206                     $this->showForm(_m('Remote subscription failed!'));
207                 }
208             }
209         }
210     }
211
212     function validateAndPreview()
213     {
214         if ($this->validateFeed()) {
215             $this->preview = true;
216             $this->showForm(_m('Previewing feed:'));
217         }
218     }
219
220     function previewFeed()
221     {
222         $this->text('Profile preview should go here');
223     }
224
225     function showScripts()
226     {
227         parent::showScripts();
228         $this->autofocus('feedurl');
229     }
230 }