]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/TwitterBridge/twittersettings.php
Switch Twitter bridge settings page to be a ProfileSettingsAction, as ConnectSettings...
[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         return _m('Twitter settings');
58     }
59
60     /**
61      * Instructions for use
62      *
63      * @return instructions for use
64      */
65
66     function getInstructions()
67     {
68         return _m('Connect your Twitter account to share your updates ' .
69                   'with your Twitter friends and vice-versa.');
70     }
71
72     /**
73      * Content area of the page
74      *
75      * Shows a form for associating a Twitter account with this
76      * StatusNet account. Also lets the user set preferences.
77      *
78      * @return void
79      */
80     function showContent()
81     {
82
83         $user = common_current_user();
84
85         $profile = $user->getProfile();
86
87         $fuser = null;
88
89         $flink = Foreign_link::getByUserID($user->id, TWITTER_SERVICE);
90
91         if (!empty($flink)) {
92             $fuser = $flink->getForeignUser();
93         }
94
95         $this->elementStart('form', array('method' => 'post',
96                                           'id' => 'form_settings_twitter',
97                                           'class' => 'form_settings',
98                                           'action' =>
99                                           common_local_url('twittersettings')));
100
101         $this->hidden('token', common_session_token());
102
103         $this->elementStart('fieldset', array('id' => 'settings_twitter_account'));
104
105         if (empty($fuser)) {
106             $this->elementStart('ul', 'form_data');
107             $this->elementStart('li', array('id' => 'settings_twitter_login_button'));
108             $this->element('a', array('href' => common_local_url('twitterauthorization')),
109                            'Connect my Twitter account');
110             $this->elementEnd('li');
111             $this->elementEnd('ul');
112
113             $this->elementEnd('fieldset');
114         } else {
115             $this->element('legend', null, _m('Twitter account'));
116             $this->elementStart('p', array('id' => 'form_confirmed'));
117             $this->element('a', array('href' => $fuser->uri), $fuser->nickname);
118             $this->elementEnd('p');
119             $this->element('p', 'form_note',
120                            _m('Connected Twitter account'));
121             $this->elementEnd('fieldset');
122
123             $this->elementStart('fieldset');
124
125             $this->element('legend', null, _m('Disconnect my account from Twitter'));
126
127             if (!$user->password) {
128
129                 $this->elementStart('p', array('class' => 'form_guide'));
130                 // @todo FIXME: Bad i18n (patchwork in three parts).
131                 $this->text(_m('Disconnecting your Twitter ' .
132                                'could make it impossible to log in! Please '));
133                 $this->element('a',
134                     array('href' => common_local_url('passwordsettings')),
135                         _m('set a password'));
136
137                 $this->text(_m(' first.'));
138                 $this->elementEnd('p');
139             } else {
140                 // TRANS: %1$s is the current website name.
141                 $note = _m('Keep your %1$s account but disconnect from Twitter. ' .
142                     'You can use your %1$s password to log in.');
143
144                 $site = common_config('site', 'name');
145
146                 $this->element('p', 'instructions',
147                     sprintf($note, $site));
148
149                 $this->submit('disconnect', _m('Disconnect'));
150             }
151
152             $this->elementEnd('fieldset');
153
154             $this->elementStart('fieldset', array('id' => 'settings_twitter_preferences'));
155
156             $this->element('legend', null, _m('Preferences'));
157             $this->elementStart('ul', 'form_data');
158             $this->elementStart('li');
159             $this->checkbox('noticesend',
160                             _m('Automatically send my notices to Twitter.'),
161                             ($flink) ?
162                             ($flink->noticesync & FOREIGN_NOTICE_SEND) :
163                             true);
164             $this->elementEnd('li');
165             $this->elementStart('li');
166             $this->checkbox('replysync',
167                             _m('Send local "@" replies to Twitter.'),
168                             ($flink) ?
169                             ($flink->noticesync & FOREIGN_NOTICE_SEND_REPLY) :
170                             true);
171             $this->elementEnd('li');
172             $this->elementStart('li');
173             $this->checkbox('friendsync',
174                             _m('Subscribe to my Twitter friends here.'),
175                             ($flink) ?
176                             ($flink->friendsync & FOREIGN_FRIEND_RECV) :
177                             false);
178             $this->elementEnd('li');
179
180             if (common_config('twitterimport','enabled')) {
181                 $this->elementStart('li');
182                 $this->checkbox('noticerecv',
183                                 _m('Import my friends timeline.'),
184                                 ($flink) ?
185                                 ($flink->noticesync & FOREIGN_NOTICE_RECV) :
186                                 false);
187                 $this->elementEnd('li');
188             } else {
189                 // preserve setting even if bidrection bridge toggled off
190
191                 if ($flink && ($flink->noticesync & FOREIGN_NOTICE_RECV)) {
192                     $this->hidden('noticerecv', true, 'noticerecv');
193                 }
194             }
195
196             $this->elementEnd('ul');
197
198             if ($flink) {
199                 $this->submit('save', _m('Save'));
200             } else {
201                 $this->submit('add', _m('Add'));
202             }
203
204             $this->elementEnd('fieldset');
205         }
206
207         $this->elementEnd('form');
208     }
209
210     /**
211      * Handle posts to this form
212      *
213      * Based on the button that was pressed, muxes out to other functions
214      * to do the actual task requested.
215      *
216      * All sub-functions reload the form with a message -- success or failure.
217      *
218      * @return void
219      */
220     function handlePost()
221     {
222         // CSRF protection
223         $token = $this->trimmed('token');
224         if (!$token || $token != common_session_token()) {
225             $this->showForm(_m('There was a problem with your session token. '.
226                                'Try again, please.'));
227             return;
228         }
229
230         if ($this->arg('save')) {
231             $this->savePreferences();
232         } else if ($this->arg('disconnect')) {
233             $this->removeTwitterAccount();
234         } else {
235             $this->showForm(_m('Unexpected form submission.'));
236         }
237     }
238
239     /**
240      * Disassociate an existing Twitter account from this account
241      *
242      * @return void
243      */
244     function removeTwitterAccount()
245     {
246         $user = common_current_user();
247         $flink = Foreign_link::getByUserID($user->id, TWITTER_SERVICE);
248
249         if (empty($flink)) {
250             $this->clientError(_m('No Twitter connection to remove.'));
251             return;
252         }
253
254         $result = $flink->safeDelete();
255
256         if (empty($result)) {
257             common_log_db_error($flink, 'DELETE', __FILE__);
258             $this->serverError(_m('Couldn\'t remove Twitter user.'));
259             return;
260         }
261
262         $this->showForm(_m('Twitter account disconnected.'), true);
263     }
264
265     /**
266      * Save user's Twitter-bridging preferences
267      *
268      * @return void
269      */
270     function savePreferences()
271     {
272         $noticesend = $this->boolean('noticesend');
273         $noticerecv = $this->boolean('noticerecv');
274         $friendsync = $this->boolean('friendsync');
275         $replysync  = $this->boolean('replysync');
276
277         $user = common_current_user();
278         $flink = Foreign_link::getByUserID($user->id, TWITTER_SERVICE);
279
280         if (empty($flink)) {
281             common_log_db_error($flink, 'SELECT', __FILE__);
282             $this->showForm(_m('Couldn\'t save Twitter preferences.'));
283             return;
284         }
285
286         $original = clone($flink);
287         $wasReceiving = (bool)($original->noticesync & FOREIGN_NOTICE_RECV);
288         $flink->set_flags($noticesend, $noticerecv, $replysync, $friendsync);
289         $result = $flink->update($original);
290
291         if ($result === false) {
292             common_log_db_error($flink, 'UPDATE', __FILE__);
293             $this->showForm(_m('Couldn\'t save Twitter preferences.'));
294             return;
295         }
296
297         if ($wasReceiving xor $noticerecv) {
298             $this->notifyDaemon($flink->foreign_id, $noticerecv);
299         }
300
301         $this->showForm(_m('Twitter preferences saved.'), true);
302     }
303
304     /**
305      * Tell the import daemon that we've updated a user's receive status.
306      */
307     function notifyDaemon($twitterUserId, $receiving)
308     {
309         // todo... should use control signals rather than queues
310     }
311
312 }