3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, Controlez-Vous, Inc.
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.
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.
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/>.
20 if (!defined('LACONICA')) { exit(1); }
22 require_once(INSTALLDIR.'/lib/settingsaction.php');
24 define('SUBSCRIPTIONS', 80);
26 class TwittersettingsAction extends SettingsAction {
28 function get_instructions()
30 return _('Add your Twitter account to automatically send your notices to Twitter, ' .
31 'and subscribe to Twitter friends already here.');
34 function show_form($msg=null, $success=false)
36 $user = common_current_user();
37 $profile = $user->getProfile();
39 $flink = Foreign_link::getByUserID($user->id, 1); // 1 == Twitter
42 $fuser = $flink->getForeignUser();
45 $this->form_header(_('Twitter settings'), $msg, $success);
46 common_element_start('form', array('method' => 'post',
47 'id' => 'twittersettings',
49 common_local_url('twittersettings')));
50 common_hidden('token', common_session_token());
52 common_element('h2', null, _('Twitter Account'));
55 common_element_start('p');
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'));
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
69 common_password('twitter_password', _('Twitter password'));
72 common_element('h2', null, _('Preferences'));
74 common_checkbox('noticesync', _('Automatically send my notices to Twitter.'),
75 ($flink) ? ($flink->noticesync & FOREIGN_NOTICE_SEND) : true);
77 common_checkbox('replysync', _('Send local "@" replies to Twitter.'),
78 ($flink) ? ($flink->noticesync & FOREIGN_NOTICE_SEND_REPLY) : true);
80 common_checkbox('friendsync', _('Subscribe to my Twitter friends here.'),
81 ($flink) ? ($flink->friendsync & FOREIGN_FRIEND_RECV) : false);
84 common_submit('save', _('Save'));
86 common_submit('add', _('Add'));
89 $this->show_twitter_subscriptions();
91 common_element_end('form');
96 function subscribed_twitter_users()
99 $current_user = common_current_user();
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';
110 $user->query(sprintf($qry, $current_user->id));
114 while ($user->fetch()) {
116 // Don't include the user's own self-subscription
117 if ($user->id != $current_user->id) {
118 $users[] = clone($user);
125 function show_twitter_subscriptions()
128 $friends = $this->subscribed_twitter_users();
129 $friends_count = count($friends);
131 if ($friends_count > 0) {
133 common_element('h3', null, _('Twitter Friends'));
134 common_element_start('div', array('id' => 'subscriptions'));
135 common_element_start('ul', array('id' => 'subscriptions_avatars'));
137 for ($i = 0; $i < min($friends_count, SUBSCRIPTIONS); $i++) {
139 $other = Profile::staticGet($friends[$i]->id);
142 common_log_db_error($subs, 'SELECT', __FILE__);
146 common_element_start('li');
147 common_element_start('a', array('title' => ($other->fullname) ?
150 'href' => $other->profileurl,
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) ?
161 common_element_end('a');
162 common_element_end('li');
166 common_element_end('ul');
167 common_element_end('div');
171 // XXX Figure out a way to show all Twitter friends... ?
174 if ($subs_count > SUBSCRIPTIONS) {
175 common_element_start('p', array('id' => 'subscriptions_viewall'));
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');
187 function handle_post()
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.'));
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();
204 $this->show_form(_('Unexpected form submission.'));
208 function add_twitter_acct()
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');
217 if (!Validate::string($screen_name,
218 array( 'min_length' => 1,
220 'format' => VALIDATE_NUM . VALIDATE_ALPHA . '_'))) {
222 _('Username must have only numbers, upper- and lowercase letters, and underscore (_). 15 chars max.'));
226 if (!$this->verify_credentials($screen_name, $password)) {
227 $this->show_form(_('Could not verify your Twitter credentials!'));
231 $twit_user = twitter_user_info($screen_name, $password);
234 $this->show_form(sprintf(_('Unable to retrieve account information for "%s" from Twitter.'),
239 if (!save_twitter_user($twit_user->id, $screen_name)) {
240 $this->show_form(_('Unable to save your Twitter settings!'));
244 $user = common_current_user();
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();
253 $this->set_flags($flink, $noticesync, $replysync, $friendsync);
255 $flink_id = $flink->insert();
258 common_log_db_error($flink, 'INSERT', __FILE__);
259 $this->show_form(_('Unable to save your Twitter settings!'));
264 save_twitter_friends($user, $twit_user->id, $screen_name, $password);
267 $this->show_form(_('Twitter settings saved.'), true);
270 function remove_twitter_acct()
273 $user = common_current_user();
274 $flink = Foreign_link::getByUserID($user->id, 1);
275 $flink_foreign_id = $this->arg('flink_foreign_id');
277 # Maybe an old tab open...?
278 if ($flink->foreign_id != $flink_foreign_id) {
279 $this->show_form(_('That is not your Twitter account.'));
283 $result = $flink->delete();
286 common_log_db_error($flink, 'DELETE', __FILE__);
287 common_server_error(_('Couldn\'t remove Twitter user.'));
291 $this->show_form(_('Twitter account removed.'), true);
294 function save_preferences()
297 $noticesync = $this->boolean('noticesync');
298 $friendsync = $this->boolean('friendsync');
299 $replysync = $this->boolean('replysync');
301 $user = common_current_user();
303 $flink = Foreign_link::getByUserID($user->id, 1);
306 common_log_db_error($flink, 'SELECT', __FILE__);
307 $this->show_form(_('Couldn\'t save Twitter preferences.'));
311 $twitter_id = $flink->foreign_id;
312 $password = $flink->credentials;
314 $fuser = $flink->getForeignUser();
317 common_log_db_error($fuser, 'SELECT', __FILE__);
318 $this->show_form(_('Couldn\'t save Twitter preferences.'));
322 $screen_name = $fuser->nickname;
324 $original = clone($flink);
325 $this->set_flags($flink, $noticesync, $replysync, $friendsync);
326 $result = $flink->update($original);
328 if ($result === false) {
329 common_log_db_error($flink, 'UPDATE', __FILE__);
330 $this->show_form(_('Couldn\'t save Twitter preferences.'));
335 save_twitter_friends($user, $flink->foreign_id, $screen_name, $password);
338 $this->show_form(_('Twitter preferences saved.'));
341 function verify_credentials($screen_name, $password)
343 $uri = 'http://twitter.com/account/verify_credentials.json';
344 $data = get_twitter_data($uri, $screen_name, $password);
350 $user = json_decode($data);
356 $twitter_id = $user->status->id;
365 function set_flags(&$flink, $noticesync, $replysync, $friendsync)
368 $flink->noticesync |= FOREIGN_NOTICE_SEND;
370 $flink->noticesync &= ~FOREIGN_NOTICE_SEND;
374 $flink->noticesync |= FOREIGN_NOTICE_SEND_REPLY;
376 $flink->noticesync &= ~FOREIGN_NOTICE_SEND_REPLY;
380 $flink->friendsync |= FOREIGN_FRIEND_RECV;
382 $flink->friendsync &= ~FOREIGN_FRIEND_RECV;
385 $flink->profilesync = 0;