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() {
29 return _('Add your Twitter account to automatically send your notices to Twitter, ' .
30 'and subscribe to Twitter friends already here.');
33 function show_form($msg=NULL, $success=false) {
34 $user = common_current_user();
35 $profile = $user->getProfile();
37 $flink = Foreign_link::getByUserID($user->id, 1); // 1 == Twitter
40 $fuser = $flink->getForeignUser();
43 $this->form_header(_('Twitter settings'), $msg, $success);
44 common_element_start('form', array('method' => 'post',
45 'id' => 'twittersettings',
47 common_local_url('twittersettings')));
48 common_hidden('token', common_session_token());
50 common_element('h2', NULL, _('Twitter Account'));
53 common_element_start('p');
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'));
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
67 common_password('twitter_password', _('Twitter password'));
70 common_element('h2', NULL, _('Preferences'));
72 common_checkbox('noticesync', _('Automatically send my notices to Twitter.'),
73 ($flink) ? ($flink->noticesync & FOREIGN_NOTICE_SEND) : true);
75 common_checkbox('replysync', _('Send local "@" replies to Twitter.'),
76 ($flink) ? ($flink->noticesync & FOREIGN_NOTICE_SEND_REPLY) : true);
78 common_checkbox('friendsync', _('Subscribe to my Twitter friends here.'),
79 ($flink) ? ($flink->friendsync & FOREIGN_FRIEND_RECV) : false);
82 common_submit('save', _('Save'));
84 common_submit('add', _('Add'));
87 $this->show_twitter_subscriptions();
89 common_element_end('form');
94 function subscribed_twitter_users() {
96 $current_user = common_current_user();
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';
107 $user->query(sprintf($qry, $current_user->id));
111 while ($user->fetch()) {
113 // Don't include the user's own self-subscription
114 if ($user->id != $current_user->id) {
115 $users[] = clone($user);
122 function show_twitter_subscriptions() {
124 $friends = $this->subscribed_twitter_users();
125 $friends_count = count($friends);
127 if ($friends_count > 0) {
129 common_element('h3', NULL, _('Twitter Friends'));
130 common_element_start('div', array('id' => 'subscriptions'));
131 common_element_start('ul', array('id' => 'subscriptions_avatars'));
133 for ($i = 0; $i < min($friends_count, SUBSCRIPTIONS); $i++) {
135 $other = Profile::staticGet($friends[$i]->id);
138 common_log_db_error($subs, 'SELECT', __FILE__);
142 common_element_start('li');
143 common_element_start('a', array('title' => ($other->fullname) ?
146 'href' => $other->profileurl,
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) ?
157 common_element_end('a');
158 common_element_end('li');
162 common_element_end('ul');
163 common_element_end('div');
167 // XXX Figure out a way to show all Twitter friends... ?
170 if ($subs_count > SUBSCRIPTIONS) {
171 common_element_start('p', array('id' => 'subscriptions_viewall'));
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');
183 function handle_post() {
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.'));
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();
199 $this->show_form(_('Unexpected form submission.'));
203 function add_twitter_acct() {
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');
211 if (!Validate::string($screen_name,
212 array( 'min_length' => 1,
214 'format' => VALIDATE_NUM . VALIDATE_ALPHA . '_'))) {
216 _('Username must have only numbers, upper- and lowercase letters, and underscore (_). 15 chars max.'));
220 if (!$this->verify_credentials($screen_name, $password)) {
221 $this->show_form(_('Could not verify your Twitter credentials!'));
225 $twit_user = twitter_user_info($screen_name, $password);
228 $this->show_form(sprintf(_('Unable to retrieve account information for "%s" from Twitter.'),
233 if (!save_twitter_user($twit_user->id, $screen_name)) {
234 $this->show_form(_('Unable to save your Twitter settings!'));
238 $user = common_current_user();
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();
247 $this->set_flags($flink, $noticesync, $replysync, $friendsync);
249 $flink_id = $flink->insert();
252 common_log_db_error($flink, 'INSERT', __FILE__);
253 $this->show_form(_('Unable to save your Twitter settings!'));
258 save_twitter_friends($user, $twit_user->id, $screen_name, $password);
261 $this->show_form(_('Twitter settings saved.'), true);
264 function remove_twitter_acct() {
266 $user = common_current_user();
267 $flink = Foreign_link::getByUserID($user->id, 1);
268 $flink_foreign_id = $this->arg('flink_foreign_id');
270 # Maybe an old tab open...?
271 if ($flink->foreign_id != $flink_foreign_id) {
272 $this->show_form(_('That is not your Twitter account.'));
276 $result = $flink->delete();
279 common_log_db_error($flink, 'DELETE', __FILE__);
280 common_server_error(_('Couldn\'t remove Twitter user.'));
284 $this->show_form(_('Twitter account removed.'), TRUE);
287 function save_preferences() {
289 $noticesync = $this->boolean('noticesync');
290 $friendsync = $this->boolean('friendsync');
291 $replysync = $this->boolean('replysync');
293 $user = common_current_user();
295 $flink = Foreign_link::getByUserID($user->id, 1);
298 common_log_db_error($flink, 'SELECT', __FILE__);
299 $this->show_form(_('Couldn\'t save Twitter preferences.'));
303 $twitter_id = $flink->foreign_id;
304 $password = $flink->credentials;
306 $fuser = $flink->getForeignUser();
309 common_log_db_error($fuser, 'SELECT', __FILE__);
310 $this->show_form(_('Couldn\'t save Twitter preferences.'));
314 $screen_name = $fuser->nickname;
316 $original = clone($flink);
317 $this->set_flags($flink, $noticesync, $replysync, $friendsync);
318 $result = $flink->update($original);
320 if ($result === FALSE) {
321 common_log_db_error($flink, 'UPDATE', __FILE__);
322 $this->show_form(_('Couldn\'t save Twitter preferences.'));
327 save_twitter_friends($user, $flink->foreign_id, $screen_name, $password);
330 $this->show_form(_('Twitter preferences saved.'));
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);
341 $creds = json_decode($data);
347 if ($creds->authorized == 1) {
354 function set_flags(&$flink, $noticesync, $replysync, $friendsync) {
356 $flink->noticesync |= FOREIGN_NOTICE_SEND;
358 $flink->noticesync &= ~FOREIGN_NOTICE_SEND;
362 $flink->noticesync |= FOREIGN_NOTICE_SEND_REPLY;
364 $flink->noticesync &= ~FOREIGN_NOTICE_SEND_REPLY;
368 $flink->friendsync |= FOREIGN_FRIEND_RECV;
370 $flink->friendsync &= ~FOREIGN_FRIEND_RECV;
373 $flink->profilesync = 0;