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