]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/twittersettings.php
change Laconica and Control Yourself to StatusNet in PHP files
[quix0rs-gnu-social.git] / actions / 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@controlyourself.ca>
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://laconi.ca/
28  */
29
30 if (!defined('LACONICA')) {
31     exit(1);
32 }
33
34 require_once INSTALLDIR.'/lib/connectsettingsaction.php';
35 require_once INSTALLDIR.'/lib/twitter.php';
36
37 /**
38  * Settings for Twitter integration
39  *
40  * @category Settings
41  * @package  StatusNet
42  * @author   Evan Prodromou <evan@controlyourself.ca>
43  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
44  * @link     http://laconi.ca/
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 _('Twitter settings');
60     }
61
62     /**
63      * Instructions for use
64      *
65      * @return instructions for use
66      */
67
68     function getInstructions()
69     {
70         return _('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         if (!common_config('twitter', 'enabled')) {
86             $this->element('div', array('class' => 'error'),
87                            _('Twitter is not available.'));
88             return;
89         }
90
91         $user = common_current_user();
92
93         $profile = $user->getProfile();
94
95         $fuser = null;
96
97         $flink = Foreign_link::getByUserID($user->id, TWITTER_SERVICE);
98
99         if (!empty($flink)) {
100             $fuser = $flink->getForeignUser();
101         }
102
103         $this->elementStart('form', array('method' => 'post',
104                                           'id' => 'form_settings_twitter',
105                                           'class' => 'form_settings',
106                                           'action' =>
107                                           common_local_url('twittersettings')));
108
109         $this->hidden('token', common_session_token());
110
111         $this->elementStart('fieldset', array('id' => 'settings_twitter_account'));
112
113         if (empty($fuser)) {
114             $this->elementStart('ul', 'form_data');
115             $this->elementStart('li', array('id' => 'settings_twitter_login_button'));
116             $this->element('a', array('href' => common_local_url('twitterauthorization')),
117                            'Connect my Twitter account');
118             $this->elementEnd('li');
119             $this->elementEnd('ul');
120
121             $this->elementEnd('fieldset');
122         } else {
123             $this->element('legend', null, _('Twitter account'));
124             $this->elementStart('p', array('id' => 'form_confirmed'));
125             $this->element('a', array('href' => $fuser->uri), $fuser->nickname);
126             $this->elementEnd('p');
127             $this->element('p', 'form_note',
128                            _('Connected Twitter account'));
129
130             $this->submit('remove', _('Remove'));
131
132             $this->elementEnd('fieldset');
133
134             $this->elementStart('fieldset', array('id' => 'settings_twitter_preferences'));
135
136             $this->element('legend', null, _('Preferences'));
137             $this->elementStart('ul', 'form_data');
138             $this->elementStart('li');
139             $this->checkbox('noticesend',
140                             _('Automatically send my notices to Twitter.'),
141                             ($flink) ?
142                             ($flink->noticesync & FOREIGN_NOTICE_SEND) :
143                             true);
144             $this->elementEnd('li');
145             $this->elementStart('li');
146             $this->checkbox('replysync',
147                             _('Send local "@" replies to Twitter.'),
148                             ($flink) ?
149                             ($flink->noticesync & FOREIGN_NOTICE_SEND_REPLY) :
150                             true);
151             $this->elementEnd('li');
152             $this->elementStart('li');
153             $this->checkbox('friendsync',
154                             _('Subscribe to my Twitter friends here.'),
155                             ($flink) ?
156                             ($flink->friendsync & FOREIGN_FRIEND_RECV) :
157                             false);
158             $this->elementEnd('li');
159
160             if (common_config('twitterbridge','enabled')) {
161                 $this->elementStart('li');
162                 $this->checkbox('noticerecv',
163                                 _('Import my Friends Timeline.'),
164                                 ($flink) ?
165                                 ($flink->noticesync & FOREIGN_NOTICE_RECV) :
166                                 false);
167                 $this->elementEnd('li');
168
169                 // preserve setting even if bidrection bridge toggled off
170
171                 if ($flink && ($flink->noticesync & FOREIGN_NOTICE_RECV)) {
172                     $this->hidden('noticerecv', true, 'noticerecv');
173                 }
174             }
175
176             $this->elementEnd('ul');
177
178             if ($flink) {
179                 $this->submit('save', _('Save'));
180             } else {
181                 $this->submit('add', _('Add'));
182             }
183
184             $this->elementEnd('fieldset');
185         }
186
187         $this->elementEnd('form');
188     }
189
190     /**
191      * Handle posts to this form
192      *
193      * Based on the button that was pressed, muxes out to other functions
194      * to do the actual task requested.
195      *
196      * All sub-functions reload the form with a message -- success or failure.
197      *
198      * @return void
199      */
200
201     function handlePost()
202     {
203         // CSRF protection
204         $token = $this->trimmed('token');
205         if (!$token || $token != common_session_token()) {
206             $this->showForm(_('There was a problem with your session token. '.
207                               'Try again, please.'));
208             return;
209         }
210
211         if ($this->arg('save')) {
212             $this->savePreferences();
213         } else if ($this->arg('remove')) {
214             $this->removeTwitterAccount();
215         } else {
216             $this->showForm(_('Unexpected form submission.'));
217         }
218     }
219
220     /**
221      * Disassociate an existing Twitter account from this account
222      *
223      * @return void
224      */
225
226     function removeTwitterAccount()
227     {
228         $user = common_current_user();
229         $flink = Foreign_link::getByUserID($user->id, TWITTER_SERVICE);
230
231         $result = $flink->delete();
232
233         if (empty($result)) {
234             common_log_db_error($flink, 'DELETE', __FILE__);
235             $this->serverError(_('Couldn\'t remove Twitter user.'));
236             return;
237         }
238
239         $this->showForm(_('Twitter account removed.'), true);
240     }
241
242     /**
243      * Save user's Twitter-bridging preferences
244      *
245      * @return void
246      */
247
248     function savePreferences()
249     {
250         $noticesend = $this->boolean('noticesend');
251         $noticerecv = $this->boolean('noticerecv');
252         $friendsync = $this->boolean('friendsync');
253         $replysync  = $this->boolean('replysync');
254
255         $user = common_current_user();
256         $flink = Foreign_link::getByUserID($user->id, TWITTER_SERVICE);
257
258         if (empty($flink)) {
259             common_log_db_error($flink, 'SELECT', __FILE__);
260             $this->showForm(_('Couldn\'t save Twitter preferences.'));
261             return;
262         }
263
264         $original = clone($flink);
265         $flink->set_flags($noticesend, $noticerecv, $replysync, $friendsync);
266         $result = $flink->update($original);
267
268         if ($result === false) {
269             common_log_db_error($flink, 'UPDATE', __FILE__);
270             $this->showForm(_('Couldn\'t save Twitter preferences.'));
271             return;
272         }
273
274         $this->showForm(_('Twitter preferences saved.'), true);
275     }
276
277 }