3 * Laconica, the distributed open-source microblogging tool
5 * Settings for Twitter integration
9 * LICENCE: This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * @author Evan Prodromou <evan@controlyourself.ca>
25 * @copyright 2008-2009 Control Yourself, Inc.
26 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27 * @link http://laconi.ca/
30 if (!defined('LACONICA')) {
34 require_once INSTALLDIR.'/lib/connectsettingsaction.php';
35 require_once INSTALLDIR.'/lib/twitter.php';
37 define('SUBSCRIPTIONS', 80);
40 * Settings for Twitter integration
44 * @author Evan Prodromou <evan@controlyourself.ca>
45 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
46 * @link http://laconi.ca/
51 class TwittersettingsAction extends ConnectSettingsAction
56 * @return string Title of the page
61 return _('Twitter settings');
65 * Instructions for use
67 * @return instructions for use
70 function getInstructions()
72 return _('Connect your Twitter account to share your updates ' .
73 'with your Twitter friends and vice-versa.');
77 * Content area of the page
79 * Shows a form for associating a Twitter account with this
80 * Laconica account. Also lets the user set preferences.
85 function showContent()
87 $user = common_current_user();
89 $profile = $user->getProfile();
93 $flink = Foreign_link::getByUserID($user->id, TWITTER_SERVICE);
96 $fuser = $flink->getForeignUser();
99 $this->elementStart('form', array('method' => 'post',
100 'id' => 'form_settings_twitter',
101 'class' => 'form_settings',
103 common_local_url('twittersettings')));
105 $this->hidden('token', common_session_token());
110 $this->elementStart('fieldset', array('id' => 'settings_twitter_account'));
111 $this->elementStart('ul', 'form_data');
112 $this->elementStart('li', array('id' => 'settings_twitter_login_button'));
113 $this->element('a', array('href' => common_local_url('twitterauthorization')),
114 'Connect my Twitter account');
115 $this->elementEnd('li');
116 $this->elementEnd('ul');
117 $this->elementEnd('fieldset');
121 $this->elementStart('fieldset',
122 array('id' => 'settings_twitter_preferences'));
123 $this->element('legend', null, _('Preferences'));
125 $this->elementStart('ul', 'form_data');
126 $this->elementStart('li');
127 $this->checkbox('noticesend',
128 _('Automatically send my notices to Twitter.'),
130 ($flink->noticesync & FOREIGN_NOTICE_SEND) :
132 $this->elementEnd('li');
133 $this->elementStart('li');
134 $this->checkbox('replysync',
135 _('Send local "@" replies to Twitter.'),
137 ($flink->noticesync & FOREIGN_NOTICE_SEND_REPLY) :
139 $this->elementEnd('li');
140 $this->elementStart('li');
141 $this->checkbox('friendsync',
142 _('Subscribe to my Twitter friends here.'),
144 ($flink->friendsync & FOREIGN_FRIEND_RECV) :
146 $this->elementEnd('li');
148 if (common_config('twitterbridge','enabled')) {
149 $this->elementStart('li');
150 $this->checkbox('noticerecv',
151 _('Import my Friends Timeline.'),
153 ($flink->noticesync & FOREIGN_NOTICE_RECV) :
155 $this->elementEnd('li');
157 // preserve setting even if bidrection bridge toggled off
159 if ($flink && ($flink->noticesync & FOREIGN_NOTICE_RECV)) {
160 $this->hidden('noticerecv', true, 'noticerecv');
164 $this->elementEnd('ul');
167 $this->submit('save', _('Save'));
169 $this->submit('add', _('Add'));
172 $this->elementEnd('fieldset');
175 $this->showTwitterSubscriptions();
177 $this->elementEnd('form');
181 * Gets some of the user's Twitter friends
183 * Gets the number of Twitter friends that are on this
184 * instance of Laconica.
186 * @return array array of User objects
189 function subscribedTwitterUsers()
192 $current_user = common_current_user();
194 $qry = 'SELECT "user".* ' .
195 'FROM subscription ' .
196 'JOIN "user" ON subscription.subscribed = "user".id ' .
197 'JOIN foreign_link ON foreign_link.user_id = "user".id ' .
198 'WHERE subscriber = %d ' .
199 'ORDER BY "user".nickname';
203 $user->query(sprintf($qry, $current_user->id));
207 while ($user->fetch()) {
209 // Don't include the user's own self-subscription
210 if ($user->id != $current_user->id) {
211 $users[] = clone($user);
219 * Show user's Twitter friends
221 * Gets the number of Twitter friends that are on this
222 * instance of Laconica, and shows their mini-avatars.
227 function showTwitterSubscriptions()
230 $friends = $this->subscribedTwitterUsers();
232 $friends_count = count($friends);
234 if ($friends_count > 0) {
235 $this->elementStart('div', array('id' => 'entity_subscriptions',
236 'class' => 'section'));
237 $this->element('h2', null, _('Twitter Friends'));
238 $this->elementStart('ul', 'entities users xoxo');
240 for ($i = 0; $i < min($friends_count, SUBSCRIPTIONS); $i++) {
242 $other = Profile::staticGet($friends[$i]->id);
245 common_log_db_error($subs, 'SELECT', __FILE__);
249 $this->elementStart('li', 'vcard');
250 $this->elementStart('a', array('title' => ($other->fullname) ?
253 'href' => $other->profileurl,
256 $avatar = $other->getAvatar(AVATAR_MINI_SIZE);
258 $avatar_url = ($avatar) ?
259 $avatar->displayUrl() :
260 Avatar::defaultImage(AVATAR_MINI_SIZE);
262 $this->element('img', array('src' => $avatar_url,
263 'width' => AVATAR_MINI_SIZE,
264 'height' => AVATAR_MINI_SIZE,
265 'class' => 'avatar photo',
266 'alt' => ($other->fullname) ?
270 $this->element('span', 'fn nickname', $other->nickname);
271 $this->elementEnd('a');
272 $this->elementEnd('li');
276 $this->elementEnd('ul');
277 $this->elementEnd('div');
283 * Handle posts to this form
285 * Based on the button that was pressed, muxes out to other functions
286 * to do the actual task requested.
288 * All sub-functions reload the form with a message -- success or failure.
293 function handlePost()
297 $token = $this->trimmed('token');
298 if (!$token || $token != common_session_token()) {
299 $this->showForm(_('There was a problem with your session token. '.
300 'Try again, please.'));
304 if ($this->arg('save')) {
305 $this->savePreferences();
306 } else if ($this->arg('add')) {
307 $this->addTwitterAccount();
308 } else if ($this->arg('remove')) {
309 $this->removeTwitterAccount();
311 $this->showForm(_('Unexpected form submission.'));
316 * Associate a Twitter account with the user's account
318 * Validates post input; verifies it against Twitter; and if
319 * successful stores in the database.
324 function addTwitterAccount()
326 $screen_name = $this->trimmed('twitter_username');
327 $password = $this->trimmed('twitter_password');
328 $noticesend = $this->boolean('noticesend');
329 $noticerecv = $this->boolean('noticerecv');
330 $replysync = $this->boolean('replysync');
331 $friendsync = $this->boolean('friendsync');
333 if (!Validate::string($screen_name,
334 array('min_length' => 1,
336 'format' => VALIDATE_NUM.VALIDATE_ALPHA.'_'))) {
337 $this->showForm(_('Username must have only numbers, '.
338 'upper- and lowercase letters, '.
339 'and underscore (_). 15 chars max.'));
343 if (!$this->verifyCredentials($screen_name, $password)) {
344 $this->showForm(_('Could not verify your Twitter credentials!'));
348 $twit_user = twitter_user_info($screen_name, $password);
351 $this->showForm(sprintf(_('Unable to retrieve account information '.
352 'For "%s" from Twitter.'),
357 if (!save_twitter_user($twit_user->id, $screen_name)) {
358 $this->showForm(_('Unable to save your Twitter settings!'));
362 $user = common_current_user();
364 $flink = new Foreign_link();
366 $flink->user_id = $user->id;
367 $flink->foreign_id = $twit_user->id;
368 $flink->service = TWITTER_SERVICE;
369 $flink->credentials = $password;
370 $flink->created = common_sql_now();
372 $flink->set_flags($noticesend, $noticerecv, $replysync, $friendsync);
374 $flink_id = $flink->insert();
377 common_log_db_error($flink, 'INSERT', __FILE__);
378 $this->showForm(_('Unable to save your Twitter settings!'));
383 save_twitter_friends($user, $twit_user->id, $screen_name, $password);
384 $flink->last_friendsync = common_sql_now();
388 $this->showForm(_('Twitter settings saved.'), true);
392 * Disassociate an existing Twitter account from this account
397 function removeTwitterAccount()
399 $user = common_current_user();
401 $flink = Foreign_link::getByUserID($user->id, 1);
403 $flink_foreign_id = $this->arg('flink_foreign_id');
405 // Maybe an old tab open...?
406 if ($flink->foreign_id != $flink_foreign_id) {
407 $this->showForm(_('That is not your Twitter account.'));
411 $result = $flink->delete();
414 common_log_db_error($flink, 'DELETE', __FILE__);
415 $this->serverError(_('Couldn\'t remove Twitter user.'));
419 $this->showForm(_('Twitter account removed.'), true);
423 * Save user's Twitter-bridging preferences
428 function savePreferences()
430 $noticesend = $this->boolean('noticesend');
431 $noticerecv = $this->boolean('noticerecv');
432 $friendsync = $this->boolean('friendsync');
433 $replysync = $this->boolean('replysync');
435 $user = common_current_user();
437 $flink = Foreign_link::getByUserID($user->id, 1);
440 common_log_db_error($flink, 'SELECT', __FILE__);
441 $this->showForm(_('Couldn\'t save Twitter preferences.'));
445 $twitter_id = $flink->foreign_id;
446 $password = $flink->credentials;
448 $fuser = $flink->getForeignUser();
451 common_log_db_error($fuser, 'SELECT', __FILE__);
452 $this->showForm(_('Couldn\'t save Twitter preferences.'));
456 $screen_name = $fuser->nickname;
458 $original = clone($flink);
460 $flink->set_flags($noticesend, $noticerecv, $replysync, $friendsync);
462 $result = $flink->update($original);
464 if ($result === false) {
465 common_log_db_error($flink, 'UPDATE', __FILE__);
466 $this->showForm(_('Couldn\'t save Twitter preferences.'));
471 save_twitter_friends($user, $flink->foreign_id, $screen_name, $password);
474 $this->showForm(_('Twitter preferences saved.'), true);
478 * Verifies a username and password against Twitter's API
480 * @param string $screen_name Twitter user name
481 * @param string $password Twitter password
483 * @return boolean success flag
486 function verifyCredentials($screen_name, $password)
488 $uri = 'http://twitter.com/account/verify_credentials.json';
490 $data = get_twitter_data($uri, $screen_name, $password);
496 $user = json_decode($data);
502 $twitter_id = $user->id;