]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/twittersettings.php
TRUE
[quix0rs-gnu-social.git] / actions / twittersettings.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 require_once(INSTALLDIR.'/lib/settingsaction.php');
23
24 define('SUBSCRIPTIONS', 80);
25
26 class TwittersettingsAction extends SettingsAction {
27
28     function get_instructions()
29     {
30         return _('Add your Twitter account to automatically send your notices to Twitter, ' .
31             'and subscribe to Twitter friends already here.');
32     }
33
34     function show_form($msg=null, $success=false)
35     {
36         $user = common_current_user();
37         $profile = $user->getProfile();
38         $fuser = null;
39         $flink = Foreign_link::getByUserID($user->id, 1); // 1 == Twitter
40
41         if ($flink) {
42             $fuser = $flink->getForeignUser();
43         }
44
45         $this->form_header(_('Twitter settings'), $msg, $success);
46         common_element_start('form', array('method' => 'post',
47                                            'id' => 'twittersettings',
48                                            'action' =>
49                                            common_local_url('twittersettings')));
50         common_hidden('token', common_session_token());
51
52         common_element('h2', null, _('Twitter Account'));
53
54         if ($fuser) {
55             common_element_start('p');
56
57             common_element('span', 'twitter_user', $fuser->nickname);
58             common_element('a', array('href' => $fuser->uri),  $fuser->uri);
59             common_element('span', 'input_instructions',
60                            _('Current verified Twitter account.'));
61             common_hidden('flink_foreign_id', $flink->foreign_id);
62             common_element_end('p');
63             common_submit('remove', _('Remove'));
64         } else {
65             common_input('twitter_username', _('Twitter user name'),
66                          ($this->arg('twitter_username')) ? $this->arg('twitter_username') : $profile->nickname,
67                          _('No spaces, please.')); // hey, it's what Twitter says
68
69             common_password('twitter_password', _('Twitter password'));
70         }
71
72         common_element('h2', null, _('Preferences'));
73
74         common_checkbox('noticesync', _('Automatically send my notices to Twitter.'),
75                         ($flink) ? ($flink->noticesync & FOREIGN_NOTICE_SEND) : true);
76
77         common_checkbox('replysync', _('Send local "@" replies to Twitter.'),
78                         ($flink) ? ($flink->noticesync & FOREIGN_NOTICE_SEND_REPLY) : true);
79
80         common_checkbox('friendsync', _('Subscribe to my Twitter friends here.'),
81                         ($flink) ? ($flink->friendsync & FOREIGN_FRIEND_RECV) : false);
82
83         if ($flink) {
84             common_submit('save', _('Save'));
85         } else {
86             common_submit('add', _('Add'));
87         }
88
89         $this->show_twitter_subscriptions();
90
91         common_element_end('form');
92
93         common_show_footer();
94     }
95
96     function subscribed_twitter_users()
97     {
98
99         $current_user = common_current_user();
100
101         $qry = 'SELECT user.* ' .
102             'FROM subscription ' .
103             'JOIN user ON subscription.subscribed = user.id ' .
104             'JOIN foreign_link ON foreign_link.user_id = user.id ' .
105             'WHERE subscriber = %d ' .
106             'ORDER BY user.nickname';
107
108         $user = new User();
109
110         $user->query(sprintf($qry, $current_user->id));
111
112         $users = array();
113
114         while ($user->fetch()) {
115
116             // Don't include the user's own self-subscription
117             if ($user->id != $current_user->id) {
118                 $users[] = clone($user);
119             }
120         }
121
122         return $users;
123     }
124
125     function show_twitter_subscriptions()
126     {
127
128         $friends = $this->subscribed_twitter_users();
129         $friends_count = count($friends);
130
131         if ($friends_count > 0) {
132
133             common_element('h3', null, _('Twitter Friends'));
134             common_element_start('div', array('id' => 'subscriptions'));
135             common_element_start('ul', array('id' => 'subscriptions_avatars'));
136
137             for ($i = 0; $i < min($friends_count, SUBSCRIPTIONS); $i++) {
138
139                 $other = Profile::staticGet($friends[$i]->id);
140
141                 if (!$other) {
142                     common_log_db_error($subs, 'SELECT', __FILE__);
143                     continue;
144                 }
145
146                 common_element_start('li');
147                 common_element_start('a', array('title' => ($other->fullname) ?
148                                                 $other->fullname :
149                                                 $other->nickname,
150                                                 'href' => $other->profileurl,
151                                                 'rel' => 'contact',
152                                                 'class' => 'subscription'));
153                 $avatar = $other->getAvatar(AVATAR_MINI_SIZE);
154                 common_element('img', array('src' => (($avatar) ? common_avatar_display_url($avatar) :  common_default_avatar(AVATAR_MINI_SIZE)),
155                                             'width' => AVATAR_MINI_SIZE,
156                                             'height' => AVATAR_MINI_SIZE,
157                                             'class' => 'avatar mini',
158                                             'alt' =>  ($other->fullname) ?
159                                             $other->fullname :
160                                             $other->nickname));
161                 common_element_end('a');
162                 common_element_end('li');
163
164             }
165
166             common_element_end('ul');
167             common_element_end('div');
168
169         }
170
171         // XXX Figure out a way to show all Twitter friends... ?
172
173         /*
174         if ($subs_count > SUBSCRIPTIONS) {
175             common_element_start('p', array('id' => 'subscriptions_viewall'));
176
177             common_element('a', array('href' => common_local_url('subscriptions',
178                                                                  array('nickname' => $profile->nickname)),
179                                       'class' => 'moresubscriptions'),
180                            _('All subscriptions'));
181             common_element_end('p');
182         }
183         */
184
185     }
186
187     function handle_post()
188     {
189
190         # CSRF protection
191         $token = $this->trimmed('token');
192         if (!$token || $token != common_session_token()) {
193             $this->show_form(_('There was a problem with your session token. Try again, please.'));
194             return;
195         }
196
197         if ($this->arg('save')) {
198             $this->save_preferences();
199         } else if ($this->arg('add')) {
200             $this->add_twitter_acct();
201         } else if ($this->arg('remove')) {
202             $this->remove_twitter_acct();
203         } else {
204             $this->show_form(_('Unexpected form submission.'));
205         }
206     }
207
208     function add_twitter_acct()
209     {
210
211         $screen_name = $this->trimmed('twitter_username');
212         $password = $this->trimmed('twitter_password');
213         $noticesync = $this->boolean('noticesync');
214         $replysync = $this->boolean('replysync');
215         $friendsync = $this->boolean('friendsync');
216
217         if (!Validate::string($screen_name,
218                 array(    'min_length' => 1,
219                         'max_length' => 15,
220                          'format' => VALIDATE_NUM . VALIDATE_ALPHA . '_'))) {
221             $this->show_form(
222                 _('Username must have only numbers, upper- and lowercase letters, and underscore (_). 15 chars max.'));
223             return;
224         }
225
226         if (!$this->verify_credentials($screen_name, $password)) {
227             $this->show_form(_('Could not verify your Twitter credentials!'));
228             return;
229         }
230
231         $twit_user = twitter_user_info($screen_name, $password);
232
233         if (!$twit_user) {
234             $this->show_form(sprintf(_('Unable to retrieve account information for "%s" from Twitter.'),
235                 $screen_name));
236             return;
237         }
238
239         if (!save_twitter_user($twit_user->id, $screen_name)) {
240             $this->show_form(_('Unable to save your Twitter settings!'));
241             return;
242         }
243
244         $user = common_current_user();
245
246         $flink = DB_DataObject::factory('foreign_link');
247         $flink->user_id = $user->id;
248         $flink->foreign_id = $twit_user->id;
249         $flink->service = 1; // Twitter
250         $flink->credentials = $password;
251         $flink->created = common_sql_now();
252
253         $this->set_flags($flink, $noticesync, $replysync, $friendsync);
254
255         $flink_id = $flink->insert();
256
257         if (!$flink_id) {
258             common_log_db_error($flink, 'INSERT', __FILE__);
259             $this->show_form(_('Unable to save your Twitter settings!'));
260             return;
261         }
262
263         if ($friendsync) {
264             save_twitter_friends($user, $twit_user->id, $screen_name, $password);
265         }
266
267         $this->show_form(_('Twitter settings saved.'), true);
268     }
269
270     function remove_twitter_acct()
271     {
272
273         $user = common_current_user();
274         $flink = Foreign_link::getByUserID($user->id, 1);
275         $flink_foreign_id = $this->arg('flink_foreign_id');
276
277         # Maybe an old tab open...?
278         if ($flink->foreign_id != $flink_foreign_id) {
279             $this->show_form(_('That is not your Twitter account.'));
280             return;
281         }
282
283         $result = $flink->delete();
284
285         if (!$result) {
286             common_log_db_error($flink, 'DELETE', __FILE__);
287             common_server_error(_('Couldn\'t remove Twitter user.'));
288             return;
289         }
290
291         $this->show_form(_('Twitter account removed.'), true);
292     }
293
294     function save_preferences()
295     {
296
297         $noticesync = $this->boolean('noticesync');
298         $friendsync = $this->boolean('friendsync');
299         $replysync = $this->boolean('replysync');
300
301         $user = common_current_user();
302
303         $flink = Foreign_link::getByUserID($user->id, 1);
304
305         if (!$flink) {
306             common_log_db_error($flink, 'SELECT', __FILE__);
307             $this->show_form(_('Couldn\'t save Twitter preferences.'));
308             return;
309         }
310
311         $twitter_id = $flink->foreign_id;
312         $password = $flink->credentials;
313
314         $fuser = $flink->getForeignUser();
315
316         if (!$fuser) {
317             common_log_db_error($fuser, 'SELECT', __FILE__);
318             $this->show_form(_('Couldn\'t save Twitter preferences.'));
319             return;
320         }
321
322         $screen_name = $fuser->nickname;
323
324         $original = clone($flink);
325         $this->set_flags($flink, $noticesync, $replysync, $friendsync);
326         $result = $flink->update($original);
327
328         if ($result === false) {
329             common_log_db_error($flink, 'UPDATE', __FILE__);
330             $this->show_form(_('Couldn\'t save Twitter preferences.'));
331             return;
332         }
333
334         if ($friendsync) {
335             save_twitter_friends($user, $flink->foreign_id, $screen_name, $password);
336         }
337
338         $this->show_form(_('Twitter preferences saved.'));
339     }
340
341     function verify_credentials($screen_name, $password)
342     {
343         $uri = 'http://twitter.com/account/verify_credentials.json';
344         $data = get_twitter_data($uri, $screen_name, $password);
345
346         if (!$data) {
347             return false;
348         }
349
350         $user = json_decode($data);
351
352         if (!$user) {
353             return false;
354         }
355
356          $twitter_id = $user->status->id;
357
358         if ($twitter_id) {
359             return $twitter_id;
360         }
361
362         return false;
363     }
364
365     function set_flags(&$flink, $noticesync, $replysync, $friendsync)
366     {
367         if ($noticesync) {
368             $flink->noticesync |= FOREIGN_NOTICE_SEND;
369         } else {
370             $flink->noticesync &= ~FOREIGN_NOTICE_SEND;
371         }
372
373         if ($replysync) {
374             $flink->noticesync |= FOREIGN_NOTICE_SEND_REPLY;
375         } else {
376             $flink->noticesync &= ~FOREIGN_NOTICE_SEND_REPLY;
377         }
378
379         if ($friendsync) {
380             $flink->friendsync |= FOREIGN_FRIEND_RECV;
381         } else {
382             $flink->friendsync &= ~FOREIGN_FRIEND_RECV;
383         }
384
385         $flink->profilesync = 0;
386     }
387
388 }