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 _('Add your Twitter account to automatically send '.
73 ' your notices to Twitter, ' .
74 'and subscribe to Twitter friends already here.');
78 * Content area of the page
80 * Shows a form for associating a Twitter account with this
81 * Laconica account. Also lets the user set preferences.
86 function showContent()
88 $user = common_current_user();
90 $profile = $user->getProfile();
94 $flink = Foreign_link::getByUserID($user->id, TWITTER_SERVICE);
97 $fuser = $flink->getForeignUser();
100 $this->elementStart('form', array('method' => 'post',
101 'id' => 'form_settings_twitter',
102 'class' => 'form_settings',
104 common_local_url('twittersettings')));
105 $this->elementStart('fieldset', array('id' => 'settings_twitter_account'));
106 $this->element('legend', null, _('Twitter Account'));
107 $this->hidden('token', common_session_token());
109 $this->elementStart('ul', 'form_data');
110 $this->elementStart('li', array('id' => 'settings_twitter_remove'));
111 $this->element('span', 'twitter_user', $fuser->nickname);
112 $this->element('a', array('href' => $fuser->uri), $fuser->uri);
113 $this->element('p', 'form_note',
114 _('Current verified Twitter account.'));
115 $this->hidden('flink_foreign_id', $flink->foreign_id);
116 $this->elementEnd('li');
117 $this->elementEnd('ul');
118 $this->submit('remove', _('Remove'));
120 $this->elementStart('ul', 'form_data');
121 $this->elementStart('li', array('id' => 'settings_twitter_login'));
122 $this->input('twitter_username', _('Twitter user name'),
123 ($this->arg('twitter_username')) ?
124 $this->arg('twitter_username') :
126 _('No spaces, please.')); // hey, it's what Twitter says
127 $this->elementEnd('li');
128 $this->elementStart('li');
129 $this->password('twitter_password', _('Twitter password'));
130 $this->elementend('li');
131 $this->elementEnd('ul');
133 $this->elementEnd('fieldset');
135 $this->elementStart('fieldset',
136 array('id' => 'settings_twitter_preferences'));
137 $this->element('legend', null, _('Preferences'));
139 $this->elementStart('ul', 'form_data');
140 $this->elementStart('li');
141 $this->checkbox('noticesend',
142 _('Automatically send my notices to Twitter.'),
144 ($flink->noticesync & FOREIGN_NOTICE_SEND) :
146 $this->elementEnd('li');
147 $this->elementStart('li');
148 $this->checkbox('replysync',
149 _('Send local "@" replies to Twitter.'),
151 ($flink->noticesync & FOREIGN_NOTICE_SEND_REPLY) :
153 $this->elementEnd('li');
154 $this->elementStart('li');
155 $this->checkbox('friendsync',
156 _('Subscribe to my Twitter friends here.'),
158 ($flink->friendsync & FOREIGN_FRIEND_RECV) :
160 $this->elementEnd('li');
162 if (common_config('twitterbridge','enabled')) {
163 $this->elementStart('li');
164 $this->checkbox('noticerecv',
165 _('Import my Friends Timeline.'),
167 ($flink->noticesync & FOREIGN_NOTICE_RECV) :
169 $this->elementEnd('li');
171 // preserve setting even if bidrection bridge toggled off
172 if ($flink && ($flink->noticesync & FOREIGN_NOTICE_RECV)) {
173 $this->hidden('noticerecv', true, 'noticerecv');
177 $this->elementEnd('ul');
180 $this->submit('save', _('Save'));
182 $this->submit('add', _('Add'));
184 $this->elementEnd('fieldset');
186 $this->showTwitterSubscriptions();
188 $this->elementEnd('form');
192 * Gets some of the user's Twitter friends
194 * Gets the number of Twitter friends that are on this
195 * instance of Laconica.
197 * @return array array of User objects
200 function subscribedTwitterUsers()
203 $current_user = common_current_user();
205 $qry = 'SELECT "user".* ' .
206 'FROM subscription ' .
207 'JOIN "user" ON subscription.subscribed = "user".id ' .
208 'JOIN foreign_link ON foreign_link.user_id = "user".id ' .
209 'WHERE subscriber = %d ' .
210 'ORDER BY "user".nickname';
214 $user->query(sprintf($qry, $current_user->id));
218 while ($user->fetch()) {
220 // Don't include the user's own self-subscription
221 if ($user->id != $current_user->id) {
222 $users[] = clone($user);
230 * Show user's Twitter friends
232 * Gets the number of Twitter friends that are on this
233 * instance of Laconica, and shows their mini-avatars.
238 function showTwitterSubscriptions()
241 $friends = $this->subscribedTwitterUsers();
243 $friends_count = count($friends);
245 if ($friends_count > 0) {
246 $this->elementStart('div', array('id' => 'entity_subscriptions',
247 'class' => 'section'));
248 $this->element('h2', null, _('Twitter Friends'));
249 $this->elementStart('ul', 'entities users xoxo');
251 for ($i = 0; $i < min($friends_count, SUBSCRIPTIONS); $i++) {
253 $other = Profile::staticGet($friends[$i]->id);
256 common_log_db_error($subs, 'SELECT', __FILE__);
260 $this->elementStart('li', 'vcard');
261 $this->elementStart('a', array('title' => ($other->fullname) ?
264 'href' => $other->profileurl,
267 $avatar = $other->getAvatar(AVATAR_MINI_SIZE);
269 $avatar_url = ($avatar) ?
270 $avatar->displayUrl() :
271 Avatar::defaultImage(AVATAR_MINI_SIZE);
273 $this->element('img', array('src' => $avatar_url,
274 'width' => AVATAR_MINI_SIZE,
275 'height' => AVATAR_MINI_SIZE,
276 'class' => 'avatar photo',
277 'alt' => ($other->fullname) ?
281 $this->element('span', 'fn nickname', $other->nickname);
282 $this->elementEnd('a');
283 $this->elementEnd('li');
287 $this->elementEnd('ul');
288 $this->elementEnd('div');
294 * Handle posts to this form
296 * Based on the button that was pressed, muxes out to other functions
297 * to do the actual task requested.
299 * All sub-functions reload the form with a message -- success or failure.
304 function handlePost()
308 $token = $this->trimmed('token');
309 if (!$token || $token != common_session_token()) {
310 $this->showForm(_('There was a problem with your session token. '.
311 'Try again, please.'));
315 if ($this->arg('save')) {
316 $this->savePreferences();
317 } else if ($this->arg('add')) {
318 $this->addTwitterAccount();
319 } else if ($this->arg('remove')) {
320 $this->removeTwitterAccount();
322 $this->showForm(_('Unexpected form submission.'));
327 * Associate a Twitter account with the user's account
329 * Validates post input; verifies it against Twitter; and if
330 * successful stores in the database.
335 function addTwitterAccount()
337 $screen_name = $this->trimmed('twitter_username');
338 $password = $this->trimmed('twitter_password');
339 $noticesend = $this->boolean('noticesend');
340 $noticerecv = $this->boolean('noticerecv');
341 $replysync = $this->boolean('replysync');
342 $friendsync = $this->boolean('friendsync');
344 if (!Validate::string($screen_name,
345 array('min_length' => 1,
347 'format' => VALIDATE_NUM.VALIDATE_ALPHA.'_'))) {
348 $this->showForm(_('Username must have only numbers, '.
349 'upper- and lowercase letters, '.
350 'and underscore (_). 15 chars max.'));
354 if (!$this->verifyCredentials($screen_name, $password)) {
355 $this->showForm(_('Could not verify your Twitter credentials!'));
359 $twit_user = twitter_user_info($screen_name, $password);
362 $this->showForm(sprintf(_('Unable to retrieve account information '.
363 'For "%s" from Twitter.'),
368 if (!save_twitter_user($twit_user->id, $screen_name)) {
369 $this->showForm(_('Unable to save your Twitter settings!'));
373 $user = common_current_user();
375 $flink = new Foreign_link();
377 $flink->user_id = $user->id;
378 $flink->foreign_id = $twit_user->id;
379 $flink->service = TWITTER_SERVICE;
380 $flink->credentials = $password;
381 $flink->created = common_sql_now();
383 $flink->set_flags($noticesend, $noticerecv, $replysync, $friendsync);
385 $flink_id = $flink->insert();
388 common_log_db_error($flink, 'INSERT', __FILE__);
389 $this->showForm(_('Unable to save your Twitter settings!'));
394 save_twitter_friends($user, $twit_user->id, $screen_name, $password);
395 $flink->last_friendsync = common_sql_now();
399 $this->showForm(_('Twitter settings saved.'), true);
403 * Disassociate an existing Twitter account from this account
408 function removeTwitterAccount()
410 $user = common_current_user();
412 $flink = Foreign_link::getByUserID($user->id, 1);
414 $flink_foreign_id = $this->arg('flink_foreign_id');
416 // Maybe an old tab open...?
417 if ($flink->foreign_id != $flink_foreign_id) {
418 $this->showForm(_('That is not your Twitter account.'));
422 $result = $flink->delete();
425 common_log_db_error($flink, 'DELETE', __FILE__);
426 $this->serverError(_('Couldn\'t remove Twitter user.'));
430 $this->showForm(_('Twitter account removed.'), true);
434 * Save user's Twitter-bridging preferences
439 function savePreferences()
441 $noticesend = $this->boolean('noticesend');
442 $noticerecv = $this->boolean('noticerecv');
443 $friendsync = $this->boolean('friendsync');
444 $replysync = $this->boolean('replysync');
446 $user = common_current_user();
448 $flink = Foreign_link::getByUserID($user->id, 1);
451 common_log_db_error($flink, 'SELECT', __FILE__);
452 $this->showForm(_('Couldn\'t save Twitter preferences.'));
456 $twitter_id = $flink->foreign_id;
457 $password = $flink->credentials;
459 $fuser = $flink->getForeignUser();
462 common_log_db_error($fuser, 'SELECT', __FILE__);
463 $this->showForm(_('Couldn\'t save Twitter preferences.'));
467 $screen_name = $fuser->nickname;
469 $original = clone($flink);
471 $flink->set_flags($noticesend, $noticerecv, $replysync, $friendsync);
473 $result = $flink->update($original);
475 if ($result === false) {
476 common_log_db_error($flink, 'UPDATE', __FILE__);
477 $this->showForm(_('Couldn\'t save Twitter preferences.'));
482 save_twitter_friends($user, $flink->foreign_id, $screen_name, $password);
485 $this->showForm(_('Twitter preferences saved.'), true);
489 * Verifies a username and password against Twitter's API
491 * @param string $screen_name Twitter user name
492 * @param string $password Twitter password
494 * @return boolean success flag
497 function verifyCredentials($screen_name, $password)
499 $uri = 'http://twitter.com/account/verify_credentials.json';
501 $data = get_twitter_data($uri, $screen_name, $password);
507 $user = json_decode($data);
513 $twitter_id = $user->id;