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
29 function get_instructions()
31 return _('Add your Twitter account to automatically send your notices to Twitter, ' .
32 'and subscribe to Twitter friends already here.');
35 function show_form($msg=null, $success=false)
37 $user = common_current_user();
38 $profile = $user->getProfile();
40 $flink = Foreign_link::getByUserID($user->id, 1); // 1 == Twitter
43 $fuser = $flink->getForeignUser();
46 $this->form_header(_('Twitter settings'), $msg, $success);
47 $this->elementStart('form', array('method' => 'post',
48 'id' => 'twittersettings',
50 common_local_url('twittersettings')));
51 $this->hidden('token', common_session_token());
53 $this->element('h2', null, _('Twitter Account'));
56 $this->elementStart('p');
58 $this->element('span', 'twitter_user', $fuser->nickname);
59 $this->element('a', array('href' => $fuser->uri), $fuser->uri);
60 $this->element('span', 'input_instructions',
61 _('Current verified Twitter account.'));
62 $this->hidden('flink_foreign_id', $flink->foreign_id);
63 $this->elementEnd('p');
64 $this->submit('remove', _('Remove'));
66 $this->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
70 $this->password('twitter_password', _('Twitter password'));
73 $this->element('h2', null, _('Preferences'));
75 $this->checkbox('noticesync', _('Automatically send my notices to Twitter.'),
76 ($flink) ? ($flink->noticesync & FOREIGN_NOTICE_SEND) : true);
78 $this->checkbox('replysync', _('Send local "@" replies to Twitter.'),
79 ($flink) ? ($flink->noticesync & FOREIGN_NOTICE_SEND_REPLY) : true);
81 $this->checkbox('friendsync', _('Subscribe to my Twitter friends here.'),
82 ($flink) ? ($flink->friendsync & FOREIGN_FRIEND_RECV) : false);
85 $this->submit('save', _('Save'));
87 $this->submit('add', _('Add'));
90 $this->show_twitter_subscriptions();
92 $this->elementEnd('form');
97 function subscribed_twitter_users()
100 $current_user = common_current_user();
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';
111 $user->query(sprintf($qry, $current_user->id));
115 while ($user->fetch()) {
117 // Don't include the user's own self-subscription
118 if ($user->id != $current_user->id) {
119 $users[] = clone($user);
126 function show_twitter_subscriptions()
129 $friends = $this->subscribed_twitter_users();
130 $friends_count = count($friends);
132 if ($friends_count > 0) {
134 $this->element('h3', null, _('Twitter Friends'));
135 $this->elementStart('div', array('id' => 'subscriptions'));
136 $this->elementStart('ul', array('id' => 'subscriptions_avatars'));
138 for ($i = 0; $i < min($friends_count, SUBSCRIPTIONS); $i++) {
140 $other = Profile::staticGet($friends[$i]->id);
143 common_log_db_error($subs, 'SELECT', __FILE__);
147 $this->elementStart('li');
148 $this->elementStart('a', array('title' => ($other->fullname) ?
151 'href' => $other->profileurl,
153 'class' => 'subscription'));
154 $avatar = $other->getAvatar(AVATAR_MINI_SIZE);
155 $this->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) ?
162 $this->elementEnd('a');
163 $this->elementEnd('li');
167 $this->elementEnd('ul');
168 $this->elementEnd('div');
172 // XXX Figure out a way to show all Twitter friends... ?
175 if ($subs_count > SUBSCRIPTIONS) {
176 $this->elementStart('p', array('id' => 'subscriptions_viewall'));
178 $this->element('a', array('href' => common_local_url('subscriptions',
179 array('nickname' => $profile->nickname)),
180 'class' => 'moresubscriptions'),
181 _('All subscriptions'));
182 $this->elementEnd('p');
188 function handle_post()
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.'));
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();
205 $this->show_form(_('Unexpected form submission.'));
209 function add_twitter_acct()
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');
218 if (!Validate::string($screen_name,
219 array( 'min_length' => 1,
221 'format' => VALIDATE_NUM . VALIDATE_ALPHA . '_'))) {
223 _('Username must have only numbers, upper- and lowercase letters, and underscore (_). 15 chars max.'));
227 if (!$this->verify_credentials($screen_name, $password)) {
228 $this->show_form(_('Could not verify your Twitter credentials!'));
232 $twit_user = twitter_user_info($screen_name, $password);
235 $this->show_form(sprintf(_('Unable to retrieve account information for "%s" from Twitter.'),
240 if (!save_twitter_user($twit_user->id, $screen_name)) {
241 $this->show_form(_('Unable to save your Twitter settings!'));
245 $user = common_current_user();
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();
254 $flink->set_flags($noticesync, $replysync, $friendsync);
256 $flink_id = $flink->insert();
259 common_log_db_error($flink, 'INSERT', __FILE__);
260 $this->show_form(_('Unable to save your Twitter settings!'));
265 save_twitter_friends($user, $twit_user->id, $screen_name, $password);
268 $this->show_form(_('Twitter settings saved.'), true);
271 function remove_twitter_acct()
274 $user = common_current_user();
275 $flink = Foreign_link::getByUserID($user->id, 1);
276 $flink_foreign_id = $this->arg('flink_foreign_id');
278 # Maybe an old tab open...?
279 if ($flink->foreign_id != $flink_foreign_id) {
280 $this->show_form(_('That is not your Twitter account.'));
284 $result = $flink->delete();
287 common_log_db_error($flink, 'DELETE', __FILE__);
288 common_server_error(_('Couldn\'t remove Twitter user.'));
292 $this->show_form(_('Twitter account removed.'), true);
295 function save_preferences()
298 $noticesync = $this->boolean('noticesync');
299 $friendsync = $this->boolean('friendsync');
300 $replysync = $this->boolean('replysync');
302 $user = common_current_user();
304 $flink = Foreign_link::getByUserID($user->id, 1);
307 common_log_db_error($flink, 'SELECT', __FILE__);
308 $this->show_form(_('Couldn\'t save Twitter preferences.'));
312 $twitter_id = $flink->foreign_id;
313 $password = $flink->credentials;
315 $fuser = $flink->getForeignUser();
318 common_log_db_error($fuser, 'SELECT', __FILE__);
319 $this->show_form(_('Couldn\'t save Twitter preferences.'));
323 $screen_name = $fuser->nickname;
325 $original = clone($flink);
326 $flink->set_flags($noticesync, $replysync, $friendsync);
327 $result = $flink->update($original);
329 if ($result === false) {
330 common_log_db_error($flink, 'UPDATE', __FILE__);
331 $this->show_form(_('Couldn\'t save Twitter preferences.'));
336 save_twitter_friends($user, $flink->foreign_id, $screen_name, $password);
339 $this->show_form(_('Twitter preferences saved.'), true);
342 function verify_credentials($screen_name, $password)
344 $uri = 'http://twitter.com/account/verify_credentials.json';
345 $data = get_twitter_data($uri, $screen_name, $password);
351 $user = json_decode($data);
357 $twitter_id = $user->status->id;