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