]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/TwitterBridge/twittersettings.php
Ask the user to set a password before disconnecting from Twitter
[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
49 class TwittersettingsAction extends ConnectSettingsAction
50 {
51     /**
52      * Title of the page
53      *
54      * @return string Title of the page
55      */
56
57     function title()
58     {
59         return _m('Twitter settings');
60     }
61
62     /**
63      * Instructions for use
64      *
65      * @return instructions for use
66      */
67
68     function getInstructions()
69     {
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
83     function showContent()
84     {
85
86         $user = common_current_user();
87
88         $profile = $user->getProfile();
89
90         $fuser = null;
91
92         $flink = Foreign_link::getByUserID($user->id, TWITTER_SERVICE);
93
94         if (!empty($flink)) {
95             $fuser = $flink->getForeignUser();
96         }
97
98         $this->elementStart('form', array('method' => 'post',
99                                           'id' => 'form_settings_twitter',
100                                           'class' => 'form_settings',
101                                           'action' =>
102                                           common_local_url('twittersettings')));
103
104         $this->hidden('token', common_session_token());
105
106         $this->elementStart('fieldset', array('id' => 'settings_twitter_account'));
107
108         if (empty($fuser)) {
109             $this->elementStart('ul', 'form_data');
110             $this->elementStart('li', array('id' => 'settings_twitter_login_button'));
111             $this->element('a', array('href' => common_local_url('twitterauthorization')),
112                            'Connect my Twitter account');
113             $this->elementEnd('li');
114             $this->elementEnd('ul');
115
116             $this->elementEnd('fieldset');
117         } else {
118             $this->element('legend', null, _m('Twitter account'));
119             $this->elementStart('p', array('id' => 'form_confirmed'));
120             $this->element('a', array('href' => $fuser->uri), $fuser->nickname);
121             $this->elementEnd('p');
122             $this->element('p', 'form_note',
123                            _m('Connected Twitter account'));
124             $this->elementEnd('fieldset');
125
126             $this->elementStart('fieldset');
127
128             $this->element('legend', null, _m('Disconnect my account from Twitter'));
129
130             if (!$user->password) {
131
132                 $this->elementStart('p', array('class' => 'form_guide'));
133                 $this->text(_m('Disconnecting your Twitter ' .
134                                'could make it impossible to log in! Please '));
135                 $this->element('a',
136                     array('href' => common_local_url('passwordsettings')),
137                         _m('set a password'));
138
139                 $this->text(_m(' first.'));
140                 $this->elementEnd('p');
141             } else {
142
143                 $note = _m('Keep your %1$s account but disconnect from Twitter. ' .
144                     'You can use your %1$s password to log in.');
145
146                 $site = common_config('site', 'name');
147
148                 $this->element('p', 'instructions',
149                     sprintf($note, $site));
150
151                 $this->submit('disconnect', _m('Disconnect'));
152             }
153
154             $this->elementEnd('fieldset');
155
156             $this->elementStart('fieldset', array('id' => 'settings_twitter_preferences'));
157
158             $this->element('legend', null, _m('Preferences'));
159             $this->elementStart('ul', 'form_data');
160             $this->elementStart('li');
161             $this->checkbox('noticesend',
162                             _m('Automatically send my notices to Twitter.'),
163                             ($flink) ?
164                             ($flink->noticesync & FOREIGN_NOTICE_SEND) :
165                             true);
166             $this->elementEnd('li');
167             $this->elementStart('li');
168             $this->checkbox('replysync',
169                             _m('Send local "@" replies to Twitter.'),
170                             ($flink) ?
171                             ($flink->noticesync & FOREIGN_NOTICE_SEND_REPLY) :
172                             true);
173             $this->elementEnd('li');
174             $this->elementStart('li');
175             $this->checkbox('friendsync',
176                             _m('Subscribe to my Twitter friends here.'),
177                             ($flink) ?
178                             ($flink->friendsync & FOREIGN_FRIEND_RECV) :
179                             false);
180             $this->elementEnd('li');
181
182             if (common_config('twitterimport','enabled')) {
183                 $this->elementStart('li');
184                 $this->checkbox('noticerecv',
185                                 _m('Import my Friends Timeline.'),
186                                 ($flink) ?
187                                 ($flink->noticesync & FOREIGN_NOTICE_RECV) :
188                                 false);
189                 $this->elementEnd('li');
190             } else {
191                 // preserve setting even if bidrection bridge toggled off
192
193                 if ($flink && ($flink->noticesync & FOREIGN_NOTICE_RECV)) {
194                     $this->hidden('noticerecv', true, 'noticerecv');
195                 }
196             }
197
198             $this->elementEnd('ul');
199
200             if ($flink) {
201                 $this->submit('save', _m('Save'));
202             } else {
203                 $this->submit('add', _m('Add'));
204             }
205
206             $this->elementEnd('fieldset');
207         }
208
209         $this->elementEnd('form');
210     }
211
212     /**
213      * Handle posts to this form
214      *
215      * Based on the button that was pressed, muxes out to other functions
216      * to do the actual task requested.
217      *
218      * All sub-functions reload the form with a message -- success or failure.
219      *
220      * @return void
221      */
222
223     function handlePost()
224     {
225         // CSRF protection
226         $token = $this->trimmed('token');
227         if (!$token || $token != common_session_token()) {
228             $this->showForm(_m('There was a problem with your session token. '.
229                                'Try again, please.'));
230             return;
231         }
232
233         if ($this->arg('save')) {
234             $this->savePreferences();
235         } else if ($this->arg('disconnect')) {
236             $this->removeTwitterAccount();
237         } else {
238             $this->showForm(_m('Unexpected form submission.'));
239         }
240     }
241
242     /**
243      * Disassociate an existing Twitter account from this account
244      *
245      * @return void
246      */
247
248     function removeTwitterAccount()
249     {
250         $user = common_current_user();
251         $flink = Foreign_link::getByUserID($user->id, TWITTER_SERVICE);
252
253         $result = $flink->delete();
254
255         if (empty($result)) {
256             common_log_db_error($flink, 'DELETE', __FILE__);
257             $this->serverError(_m('Couldn\'t remove Twitter user.'));
258             return;
259         }
260
261         $this->showForm(_m('Twitter account disconnected.'), true);
262     }
263
264     /**
265      * Save user's Twitter-bridging preferences
266      *
267      * @return void
268      */
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         $flink->set_flags($noticesend, $noticerecv, $replysync, $friendsync);
288         $result = $flink->update($original);
289
290         if ($result === false) {
291             common_log_db_error($flink, 'UPDATE', __FILE__);
292             $this->showForm(_m('Couldn\'t save Twitter preferences.'));
293             return;
294         }
295
296         $this->showForm(_m('Twitter preferences saved.'), true);
297     }
298
299 }