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