]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/TwitterBridge/twittersettings.php
Merge branch '1.0.x' into testing
[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. %s is a URL to the password settings.
146                 // TRANS: %1$s is the StatusNet sitename.
147                 $note = _m('Keep your %1$s account but disconnect from Twitter. ' .
148                     'You can use your %1$s password to log in.');
149                 $site = common_config('site', 'name');
150
151                 $this->element('p', 'instructions',
152                     sprintf($note, $site));
153
154                 // TRANS: Button text for disconnecting a Twitter account.
155                 $this->submit('disconnect', _m('BUTTON','Disconnect'));
156             }
157
158             $this->elementEnd('fieldset');
159
160             $this->elementStart('fieldset', array('id' => 'settings_twitter_preferences'));
161
162             // TRANS: Fieldset legend.
163             $this->element('legend', null, _m('Preferences'));
164             $this->elementStart('ul', 'form_data');
165             $this->elementStart('li');
166             $this->checkbox('noticesend',
167                             // TRANS: Checkbox label.
168                             _m('Automatically send my notices to Twitter.'),
169                             ($flink) ?
170                             ($flink->noticesync & FOREIGN_NOTICE_SEND) :
171                             true);
172             $this->elementEnd('li');
173             $this->elementStart('li');
174             $this->checkbox('replysync',
175                             // TRANS: Checkbox label.
176                             _m('Send local "@" replies to Twitter.'),
177                             ($flink) ?
178                             ($flink->noticesync & FOREIGN_NOTICE_SEND_REPLY) :
179                             true);
180             $this->elementEnd('li');
181             $this->elementStart('li');
182             $this->checkbox('friendsync',
183                             // TRANS: Checkbox label.
184                             _m('Subscribe to my Twitter friends here.'),
185                             ($flink) ?
186                             ($flink->friendsync & FOREIGN_FRIEND_RECV) :
187                             false);
188             $this->elementEnd('li');
189
190             if (common_config('twitterimport','enabled')) {
191                 $this->elementStart('li');
192                 $this->checkbox('noticerecv',
193                                 // TRANS: Checkbox label.
194                                 _m('Import my friends timeline.'),
195                                 ($flink) ?
196                                 ($flink->noticesync & FOREIGN_NOTICE_RECV) :
197                                 false);
198                 $this->elementEnd('li');
199             } else {
200                 // preserve setting even if bidrection bridge toggled off
201
202                 if ($flink && ($flink->noticesync & FOREIGN_NOTICE_RECV)) {
203                     $this->hidden('noticerecv', true, 'noticerecv');
204                 }
205             }
206
207             $this->elementEnd('ul');
208
209             if ($flink) {
210                 // TRANS: Button text for saving Twitter integration settings.
211                 $this->submit('save', _m('BUTTON','Save'));
212             } else {
213                 // TRANS: Button text for adding Twitter integration.
214                 $this->submit('add', _m('BUTTON','Add'));
215             }
216
217             $this->elementEnd('fieldset');
218         }
219
220         $this->elementEnd('form');
221     }
222
223     /**
224      * Handle posts to this form
225      *
226      * Based on the button that was pressed, muxes out to other functions
227      * to do the actual task requested.
228      *
229      * All sub-functions reload the form with a message -- success or failure.
230      *
231      * @return void
232      */
233     function handlePost()
234     {
235         // CSRF protection
236         $token = $this->trimmed('token');
237         if (!$token || $token != common_session_token()) {
238             // TRANS: Client error displayed when the session token does not match or is not given.
239             $this->showForm(_m('There was a problem with your session token. '.
240                                'Try again, please.'));
241             return;
242         }
243
244         if ($this->arg('save')) {
245             $this->savePreferences();
246         } else if ($this->arg('disconnect')) {
247             $this->removeTwitterAccount();
248         } else {
249             // TRANS: Client error displayed when the submitted form contains unexpected data.
250             $this->showForm(_m('Unexpected form submission.'));
251         }
252     }
253
254     /**
255      * Disassociate an existing Twitter account from this account
256      *
257      * @return void
258      */
259     function removeTwitterAccount()
260     {
261         $user = common_current_user();
262         $flink = Foreign_link::getByUserID($user->id, TWITTER_SERVICE);
263
264         if (empty($flink)) {
265             // TRANS: Client error displayed when trying to remove a connected Twitter account when there isn't one connected.
266             $this->clientError(_m('No Twitter connection to remove.'));
267             return;
268         }
269
270         $result = $flink->safeDelete();
271
272         if (empty($result)) {
273             common_log_db_error($flink, 'DELETE', __FILE__);
274             // TRANS: Server error displayed when trying to remove a connected Twitter account fails.
275             $this->serverError(_m('Could not remove Twitter user.'));
276             return;
277         }
278
279         // TRANS: Success message displayed after disconnecting a Twitter account.
280         $this->showForm(_m('Twitter account disconnected.'), true);
281     }
282
283     /**
284      * Save user's Twitter-bridging preferences
285      *
286      * @return void
287      */
288     function savePreferences()
289     {
290         $noticesend = $this->boolean('noticesend');
291         $noticerecv = $this->boolean('noticerecv');
292         $friendsync = $this->boolean('friendsync');
293         $replysync  = $this->boolean('replysync');
294
295         $user = common_current_user();
296         $flink = Foreign_link::getByUserID($user->id, TWITTER_SERVICE);
297
298         if (empty($flink)) {
299             common_log_db_error($flink, 'SELECT', __FILE__);
300             // @todo FIXME: Shouldn't this be a serverError()?
301             // TRANS: Server error displayed when saving Twitter integration preferences fails.
302             $this->showForm(_m('Could not save Twitter preferences.'));
303             return;
304         }
305
306         $original = clone($flink);
307         $wasReceiving = (bool)($original->noticesync & FOREIGN_NOTICE_RECV);
308         $flink->set_flags($noticesend, $noticerecv, $replysync, $friendsync);
309         $result = $flink->update($original);
310
311         if ($result === false) {
312             common_log_db_error($flink, 'UPDATE', __FILE__);
313             // @todo FIXME: Shouldn't this be a serverError()?
314             // TRANS: Server error displayed when saving Twitter integration preferences fails.
315             $this->showForm(_m('Could not save Twitter preferences.'));
316             return;
317         }
318
319         if ($wasReceiving xor $noticerecv) {
320             $this->notifyDaemon($flink->foreign_id, $noticerecv);
321         }
322
323         // TRANS: Success message after saving Twitter integration preferences.
324         $this->showForm(_m('Twitter preferences saved.'), true);
325     }
326
327     /**
328      * Tell the import daemon that we've updated a user's receive status.
329      */
330     function notifyDaemon($twitterUserId, $receiving)
331     {
332         // @todo... should use control signals rather than queues
333     }
334 }