]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/TwitterBridge/twittersettings.php
More info for a proper, fancy-url lighttpd setup
[quix0rs-gnu-social.git] / plugins / TwitterBridge / twittersettings.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Settings for Twitter integration
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Settings
23  * @package   StatusNet
24  * @author    Evan Prodromou <evan@status.net>
25  * @copyright 2008-2009 StatusNet, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://status.net/
28  */
29
30 if (!defined('STATUSNET') && !defined('LACONICA')) {
31     exit(1);
32 }
33
34 require_once INSTALLDIR . '/plugins/TwitterBridge/twitter.php';
35
36 /**
37  * Settings for Twitter integration
38  *
39  * @category Settings
40  * @package  StatusNet
41  * @author   Evan Prodromou <evan@status.net>
42  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
43  * @link     http://status.net/
44  *
45  * @see      SettingsAction
46  */
47 class TwittersettingsAction extends ProfileSettingsAction
48 {
49     /**
50      * Title of the page
51      *
52      * @return string Title of the page
53      */
54
55     function title()
56     {
57         // TRANS: Title for page with Twitter integration settings.
58         return _m('Twitter settings');
59     }
60
61     /**
62      * Instructions for use
63      *
64      * @return instructions for use
65      */
66
67     function getInstructions()
68     {
69         // TRANS: Instructions for page with Twitter integration settings.
70         return _m('Connect your Twitter account to share your updates ' .
71                   'with your Twitter friends and vice-versa.');
72     }
73
74     /**
75      * Content area of the page
76      *
77      * Shows a form for associating a Twitter account with this
78      * StatusNet account. Also lets the user set preferences.
79      *
80      * @return void
81      */
82     function showContent()
83     {
84
85         $user = common_current_user();
86
87         $profile = $user->getProfile();
88
89         $fuser = null;
90
91         $flink = Foreign_link::getByUserID($user->id, TWITTER_SERVICE);
92
93         if (!empty($flink)) {
94             $fuser = $flink->getForeignUser();
95         }
96
97         $this->elementStart('form', array('method' => 'post',
98                                           'id' => 'form_settings_twitter',
99                                           'class' => 'form_settings',
100                                           'action' =>
101                                           common_local_url('twittersettings')));
102
103         $this->hidden('token', common_session_token());
104
105         $this->elementStart('fieldset', array('id' => 'settings_twitter_account'));
106
107         if (empty($fuser)) {
108             $this->elementStart('ul', 'form_data');
109             $this->elementStart('li', array('id' => 'settings_twitter_login_button'));
110             $this->element('a', array('href' => common_local_url('twitterauthorization')),
111                            // TRANS: Link description to connect to a Twitter account.
112                            'Connect my Twitter account');
113             $this->elementEnd('li');
114             $this->elementEnd('ul');
115
116             $this->elementEnd('fieldset');
117         } else {
118             // TRANS: Fieldset legend.
119             $this->element('legend', null, _m('Twitter account'));
120             $this->elementStart('p', array('id' => 'form_confirmed'));
121             $this->element('a', array('href' => $fuser->uri), $fuser->nickname);
122             $this->elementEnd('p');
123             $this->element('p', 'form_note',
124                            // TRANS: Form note when a Twitter account has been connected.
125                            _m('Connected Twitter account'));
126             $this->elementEnd('fieldset');
127
128             $this->elementStart('fieldset');
129
130             // TRANS: Fieldset legend.
131             $this->element('legend', null, _m('Disconnect my account from Twitter'));
132
133             if (!$user->password) {
134                 $this->elementStart('p', array('class' => 'form_guide'));
135                 // TRANS: Form guide. %s is a URL to the password settings.
136                 // TRANS: This message contains a Markdown link in the form [description](link).
137                 $message = sprintf(_m('Disconnecting your Twitter account ' .
138                                       'could make it impossible to log in! Please ' .
139                                       '[set a password](%s) first.'),
140                                    common_local_url('passwordsettings'));
141                 $message = common_markup_to_html($message);
142                 $this->text($message);
143                 $this->elementEnd('p');
144             } else {
145                 // TRANS: Form instructions. %1$s is the StatusNet sitename.
146                 $note = _m('Keep your %1$s account but disconnect from Twitter. ' .
147                     'You can use your %1$s password to log in.');
148                 $site = common_config('site', 'name');
149
150                 $this->element('p', 'instructions',
151                     sprintf($note, $site));
152
153                 // TRANS: Button text for disconnecting a Twitter account.
154                 $this->submit('disconnect', _m('BUTTON','Disconnect'));
155             }
156
157             $this->elementEnd('fieldset');
158
159             $this->elementStart('fieldset', array('id' => 'settings_twitter_preferences'));
160
161             // TRANS: Fieldset legend.
162             $this->element('legend', null, _m('Preferences'));
163             $this->elementStart('ul', 'form_data');
164             $this->elementStart('li');
165             $this->checkbox('noticesend',
166                             // TRANS: Checkbox label.
167                             _m('Automatically send my notices to Twitter.'),
168                             ($flink) ?
169                             ($flink->noticesync & FOREIGN_NOTICE_SEND) :
170                             true);
171             $this->elementEnd('li');
172             $this->elementStart('li');
173             $this->checkbox('replysync',
174                             // TRANS: Checkbox label.
175                             _m('Send local "@" replies to Twitter.'),
176                             ($flink) ?
177                             ($flink->noticesync & FOREIGN_NOTICE_SEND_REPLY) :
178                             true);
179             $this->elementEnd('li');
180             $this->elementStart('li');
181             $this->checkbox('friendsync',
182                             // TRANS: Checkbox label.
183                             _m('Subscribe to my Twitter friends here.'),
184                             ($flink) ?
185                             ($flink->friendsync & FOREIGN_FRIEND_RECV) :
186                             false);
187             $this->elementEnd('li');
188
189             if (common_config('twitterimport','enabled')) {
190                 $this->elementStart('li');
191                 $this->checkbox('noticerecv',
192                                 // TRANS: Checkbox label.
193                                 _m('Import my friends timeline.'),
194                                 ($flink) ?
195                                 ($flink->noticesync & FOREIGN_NOTICE_RECV) :
196                                 false);
197                 $this->elementEnd('li');
198             } else {
199                 // preserve setting even if bidrection bridge toggled off
200
201                 if ($flink && ($flink->noticesync & FOREIGN_NOTICE_RECV)) {
202                     $this->hidden('noticerecv', true, 'noticerecv');
203                 }
204             }
205
206             $this->elementEnd('ul');
207
208             if ($flink) {
209                 // TRANS: Button text for saving Twitter integration settings.
210                 $this->submit('save', _m('BUTTON','Save'));
211             } else {
212                 // TRANS: Button text for adding Twitter integration.
213                 $this->submit('add', _m('BUTTON','Add'));
214             }
215
216             $this->elementEnd('fieldset');
217         }
218
219         $this->elementEnd('form');
220     }
221
222     /**
223      * Handle posts to this form
224      *
225      * Based on the button that was pressed, muxes out to other functions
226      * to do the actual task requested.
227      *
228      * All sub-functions reload the form with a message -- success or failure.
229      *
230      * @return void
231      */
232     function handlePost()
233     {
234         // CSRF protection
235         $token = $this->trimmed('token');
236         if (!$token || $token != common_session_token()) {
237             // TRANS: Client error displayed when the session token does not match or is not given.
238             $this->showForm(_m('There was a problem with your session token. '.
239                                'Try again, please.'));
240             return;
241         }
242
243         if ($this->arg('save')) {
244             $this->savePreferences();
245         } else if ($this->arg('disconnect')) {
246             $this->removeTwitterAccount();
247         } else {
248             // TRANS: Client error displayed when the submitted form contains unexpected data.
249             $this->showForm(_m('Unexpected form submission.'));
250         }
251     }
252
253     /**
254      * Disassociate an existing Twitter account from this account
255      *
256      * @return void
257      */
258     function removeTwitterAccount()
259     {
260         $user = common_current_user();
261         $flink = Foreign_link::getByUserID($user->id, TWITTER_SERVICE);
262
263         if (empty($flink)) {
264             // TRANS: Client error displayed when trying to remove a connected Twitter account when there isn't one connected.
265             $this->clientError(_m('No Twitter connection to remove.'));
266             return;
267         }
268
269         $result = $flink->safeDelete();
270
271         if (empty($result)) {
272             common_log_db_error($flink, 'DELETE', __FILE__);
273             // TRANS: Server error displayed when trying to remove a connected Twitter account fails.
274             $this->serverError(_m('Could not remove Twitter user.'));
275             return;
276         }
277
278         // TRANS: Success message displayed after disconnecting a Twitter account.
279         $this->showForm(_m('Twitter account disconnected.'), true);
280     }
281
282     /**
283      * Save user's Twitter-bridging preferences
284      *
285      * @return void
286      */
287     function savePreferences()
288     {
289         $noticesend = $this->boolean('noticesend');
290         $noticerecv = $this->boolean('noticerecv');
291         $friendsync = $this->boolean('friendsync');
292         $replysync  = $this->boolean('replysync');
293
294         $user = common_current_user();
295         $flink = Foreign_link::getByUserID($user->id, TWITTER_SERVICE);
296
297         if (empty($flink)) {
298             common_log_db_error($flink, 'SELECT', __FILE__);
299             // @todo FIXME: Shouldn't this be a serverError()?
300             // TRANS: Server error displayed when saving Twitter integration preferences fails.
301             $this->showForm(_m('Could not save Twitter preferences.'));
302             return;
303         }
304
305         $original = clone($flink);
306         $wasReceiving = (bool)($original->noticesync & FOREIGN_NOTICE_RECV);
307         $flink->set_flags($noticesend, $noticerecv, $replysync, $friendsync);
308         $result = $flink->update($original);
309
310         if ($result === false) {
311             common_log_db_error($flink, 'UPDATE', __FILE__);
312             // @todo FIXME: Shouldn't this be a serverError()?
313             // TRANS: Server error displayed when saving Twitter integration preferences fails.
314             $this->showForm(_m('Could not save Twitter preferences.'));
315             return;
316         }
317
318         if ($wasReceiving xor $noticerecv) {
319             $this->notifyDaemon($flink->foreign_id, $noticerecv);
320         }
321
322         // TRANS: Success message after saving Twitter integration preferences.
323         $this->showForm(_m('Twitter preferences saved.'), true);
324     }
325
326     /**
327      * Tell the import daemon that we've updated a user's receive status.
328      */
329     function notifyDaemon($twitterUserId, $receiving)
330     {
331         // @todo... should use control signals rather than queues
332     }
333 }