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