]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/twittersettings.php
Merge branch '0.8.x' into twitter-oauth
[quix0rs-gnu-social.git] / actions / twittersettings.php
1 <?php
2 /**
3  * Laconica, 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   Laconica
24  * @author    Evan Prodromou <evan@controlyourself.ca>
25  * @copyright 2008-2009 Control Yourself, 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  Laconica
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      * Laconica account. Also lets the user set preferences.
79      *
80      * @return void
81      */
82
83     function showContent()
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                            'Connect my Twitter account');
112             $this->elementEnd('li');
113             $this->elementEnd('ul');
114
115             $this->elementEnd('fieldset');
116         } else {
117             $this->element('legend', null, _('Twitter account'));
118             $this->elementStart('p', array('id' => 'form_confirmed'));
119             $this->element('a', array('href' => $fuser->uri), $fuser->nickname);
120             $this->elementEnd('p');
121             $this->element('p', 'form_note',
122                            _('Connected Twitter account'));
123
124             $this->submit('remove', _('Remove'));
125
126             $this->elementEnd('fieldset');
127
128             $this->elementStart('fieldset', array('id' => 'settings_twitter_preferences'));
129
130             $this->element('legend', null, _('Preferences'));
131             $this->elementStart('ul', 'form_data');
132             $this->elementStart('li');
133             $this->checkbox('noticesend',
134                             _('Automatically send my notices to Twitter.'),
135                             ($flink) ?
136                             ($flink->noticesync & FOREIGN_NOTICE_SEND) :
137                             true);
138             $this->elementEnd('li');
139             $this->elementStart('li');
140             $this->checkbox('replysync',
141                             _('Send local "@" replies to Twitter.'),
142                             ($flink) ?
143                             ($flink->noticesync & FOREIGN_NOTICE_SEND_REPLY) :
144                             true);
145             $this->elementEnd('li');
146             $this->elementStart('li');
147             $this->checkbox('friendsync',
148                             _('Subscribe to my Twitter friends here.'),
149                             ($flink) ?
150                             ($flink->friendsync & FOREIGN_FRIEND_RECV) :
151                             false);
152             $this->elementEnd('li');
153
154             if (common_config('twitterbridge','enabled')) {
155                 $this->elementStart('li');
156                 $this->checkbox('noticerecv',
157                                 _('Import my Friends Timeline.'),
158                                 ($flink) ?
159                                 ($flink->noticesync & FOREIGN_NOTICE_RECV) :
160                                 false);
161                 $this->elementEnd('li');
162
163                 // preserve setting even if bidrection bridge toggled off
164
165                 if ($flink && ($flink->noticesync & FOREIGN_NOTICE_RECV)) {
166                     $this->hidden('noticerecv', true, 'noticerecv');
167                 }
168             }
169
170             $this->elementEnd('ul');
171
172             if ($flink) {
173                 $this->submit('save', _('Save'));
174             } else {
175                 $this->submit('add', _('Add'));
176             }
177
178             $this->elementEnd('fieldset');
179         }
180
181         $this->elementEnd('form');
182     }
183
184     /**
185      * Handle posts to this form
186      *
187      * Based on the button that was pressed, muxes out to other functions
188      * to do the actual task requested.
189      *
190      * All sub-functions reload the form with a message -- success or failure.
191      *
192      * @return void
193      */
194
195     function handlePost()
196     {
197         // CSRF protection
198         $token = $this->trimmed('token');
199         if (!$token || $token != common_session_token()) {
200             $this->showForm(_('There was a problem with your session token. '.
201                               'Try again, please.'));
202             return;
203         }
204
205         if ($this->arg('save')) {
206             $this->savePreferences();
207         } else if ($this->arg('remove')) {
208             $this->removeTwitterAccount();
209         } else {
210             $this->showForm(_('Unexpected form submission.'));
211         }
212     }
213
214     /**
215      * Disassociate an existing Twitter account from this account
216      *
217      * @return void
218      */
219
220     function removeTwitterAccount()
221     {
222         $user = common_current_user();
223         $flink = Foreign_link::getByUserID($user->id, TWITTER_SERVICE);
224
225         $result = $flink->delete();
226
227         if (empty($result)) {
228             common_log_db_error($flink, 'DELETE', __FILE__);
229             $this->serverError(_('Couldn\'t remove Twitter user.'));
230             return;
231         }
232
233         $this->showForm(_('Twitter account removed.'), true);
234     }
235
236     /**
237      * Save user's Twitter-bridging preferences
238      *
239      * @return void
240      */
241
242     function savePreferences()
243     {
244         $noticesend = $this->boolean('noticesend');
245         $noticerecv = $this->boolean('noticerecv');
246         $friendsync = $this->boolean('friendsync');
247         $replysync  = $this->boolean('replysync');
248
249         $user = common_current_user();
250         $flink = Foreign_link::getByUserID($user->id, TWITTER_SERVICE);
251
252         if (empty($flink)) {
253             common_log_db_error($flink, 'SELECT', __FILE__);
254             $this->showForm(_('Couldn\'t save Twitter preferences.'));
255             return;
256         }
257
258         $original = clone($flink);
259         $flink->set_flags($noticesend, $noticerecv, $replysync, $friendsync);
260         $result = $flink->update($original);
261
262         if ($result === false) {
263             common_log_db_error($flink, 'UPDATE', __FILE__);
264             $this->showForm(_('Couldn\'t save Twitter preferences.'));
265             return;
266         }
267
268         $this->showForm(_('Twitter preferences saved.'), true);
269     }
270
271 }