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';
36 define('SUBSCRIPTIONS', 80);
39 * Settings for Twitter integration
43 * @author Evan Prodromou <evan@controlyourself.ca>
44 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
45 * @link http://laconi.ca/
50 class TwittersettingsAction extends ConnectSettingsAction
55 * @return string Title of the page
60 _('Twitter settings');
64 * Instructions for use
66 * @return instructions for use
69 function getInstructions()
71 return _('Add your Twitter account to automatically send '.
72 ' your notices to Twitter, ' .
73 'and subscribe to Twitter friends already here.');
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, 1); // 1 == Twitter
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')));
104 $this->elementStart('fieldset', array('id' => 'settings_twitter_account'));
105 $this->element('legend', null, _('Twitter Account'));
106 $this->hidden('token', common_session_token());
107 $this->elementStart('ul', 'form_datas');
109 $this->elementStart('li');
110 $this->element('span', 'twitter_user', $fuser->nickname);
111 $this->element('a', array('href' => $fuser->uri), $fuser->uri);
112 $this->element('p', 'form_guide',
113 _('Current verified Twitter account.'));
114 $this->hidden('flink_foreign_id', $flink->foreign_id);
115 $this->submit('remove', _('Remove'));
116 $this->elementEnd('li');
118 $this->elementStart('li');
119 $this->input('twitter_username', _('Twitter user name'),
120 ($this->arg('twitter_username')) ?
121 $this->arg('twitter_username') :
123 _('No spaces, please.')); // hey, it's what Twitter says
124 $this->elementEnd('li');
125 $this->elementStart('li');
126 $this->password('twitter_password', _('Twitter password'));
127 $this->elementend('li');
129 $this->elementEnd('ul');
130 $this->elementEnd('fieldset');
132 $this->elementStart('fieldset',
133 array('id' => 'settings_twitter_preferences'));
134 $this->element('legend', null, _('Preferences'));
136 $this->elementStart('ul');
137 $this->elementStart('li');
138 $this->checkbox('noticesync',
139 _('Automatically send my notices to Twitter.'),
141 ($flink->noticesync & FOREIGN_NOTICE_SEND) :
143 $this->elementEnd('li');
144 $this->elementStart('li');
145 $this->checkbox('replysync',
146 _('Send local "@" replies to Twitter.'),
148 ($flink->noticesync & FOREIGN_NOTICE_SEND_REPLY) :
150 $this->elementEnd('li');
151 $this->elementStart('li');
152 $this->checkbox('friendsync',
153 _('Subscribe to my Twitter friends here.'),
155 ($flink->friendsync & FOREIGN_FRIEND_RECV) :
157 $this->elementEnd('li');
158 $this->elementEnd('ul');
161 $this->submit('save', _('Save'));
163 $this->submit('add', _('Add'));
165 $this->elementEnd('fieldset');
167 $this->showTwitterSubscriptions();
169 $this->elementEnd('form');
173 * Gets some of the user's Twitter friends
175 * Gets the number of Twitter friends that are on this
176 * instance of Laconica.
178 * @return array array of User objects
181 function subscribedTwitterUsers()
184 $current_user = common_current_user();
186 $qry = 'SELECT user.* ' .
187 'FROM subscription ' .
188 'JOIN user ON subscription.subscribed = user.id ' .
189 'JOIN foreign_link ON foreign_link.user_id = user.id ' .
190 'WHERE subscriber = %d ' .
191 'ORDER BY user.nickname';
195 $user->query(sprintf($qry, $current_user->id));
199 while ($user->fetch()) {
201 // Don't include the user's own self-subscription
202 if ($user->id != $current_user->id) {
203 $users[] = clone($user);
211 * Show user's Twitter friends
213 * Gets the number of Twitter friends that are on this
214 * instance of Laconica, and shows their mini-avatars.
219 function showTwitterSubscriptions()
222 $friends = $this->subscribedTwitterUsers();
224 $friends_count = count($friends);
226 if ($friends_count > 0) {
228 $this->element('h3', null, _('Twitter Friends'));
229 $this->elementStart('div', array('id' => 'subscriptions'));
230 $this->elementStart('ul', array('id' => 'subscriptions_avatars'));
232 for ($i = 0; $i < min($friends_count, SUBSCRIPTIONS); $i++) {
234 $other = Profile::staticGet($friends[$i]->id);
237 common_log_db_error($subs, 'SELECT', __FILE__);
241 $this->elementStart('li');
242 $this->elementStart('a', array('title' => ($other->fullname) ?
245 'href' => $other->profileurl,
247 'class' => 'subscription'));
249 $avatar = $other->getAvatar(AVATAR_MINI_SIZE);
251 $avatar_url = ($avatar) ?
252 common_avatar_display_url($avatar) :
253 common_default_avatar(AVATAR_MINI_SIZE);
255 $this->element('img', array('src' => $avatar_url,
256 'width' => AVATAR_MINI_SIZE,
257 'height' => AVATAR_MINI_SIZE,
258 'class' => 'avatar mini',
259 'alt' => ($other->fullname) ?
262 $this->elementEnd('a');
263 $this->elementEnd('li');
267 $this->elementEnd('ul');
268 $this->elementEnd('div');
274 * Handle posts to this form
276 * Based on the button that was pressed, muxes out to other functions
277 * to do the actual task requested.
279 * All sub-functions reload the form with a message -- success or failure.
284 function handlePost()
288 $token = $this->trimmed('token');
289 if (!$token || $token != common_session_token()) {
290 $this->showForm(_('There was a problem with your session token. '.
291 'Try again, please.'));
295 if ($this->arg('save')) {
296 $this->savePreferences();
297 } else if ($this->arg('add')) {
298 $this->addTwitterAccount();
299 } else if ($this->arg('remove')) {
300 $this->removeTwitterAccount();
302 $this->showForm(_('Unexpected form submission.'));
307 * Associate a Twitter account with the user's account
309 * Validates post input; verifies it against Twitter; and if
310 * successful stores in the database.
315 function addTwitterAccount()
317 $screen_name = $this->trimmed('twitter_username');
318 $password = $this->trimmed('twitter_password');
319 $noticesync = $this->boolean('noticesync');
320 $replysync = $this->boolean('replysync');
321 $friendsync = $this->boolean('friendsync');
323 if (!Validate::string($screen_name,
324 array('min_length' => 1,
326 'format' => VALIDATE_NUM.VALIDATE_ALPHA.'_'))) {
327 $this->showForm(_('Username must have only numbers, '.
328 'upper- and lowercase letters, '.
329 'and underscore (_). 15 chars max.'));
333 if (!$this->verifyCredentials($screen_name, $password)) {
334 $this->showForm(_('Could not verify your Twitter credentials!'));
338 $twit_user = twitter_user_info($screen_name, $password);
341 $this->showForm(sprintf(_('Unable to retrieve account information '.
342 'For "%s" from Twitter.'),
347 if (!save_twitter_user($twit_user->id, $screen_name)) {
348 $this->showForm(_('Unable to save your Twitter settings!'));
352 $user = common_current_user();
354 $flink = new Foreign_link();
356 $flink->user_id = $user->id;
357 $flink->foreign_id = $twit_user->id;
358 $flink->service = 1; // Twitter
359 $flink->credentials = $password;
360 $flink->created = common_sql_now();
362 $flink->set_flags($noticesync, $replysync, $friendsync);
364 $flink_id = $flink->insert();
367 common_log_db_error($flink, 'INSERT', __FILE__);
368 $this->showForm(_('Unable to save your Twitter settings!'));
373 save_twitter_friends($user, $twit_user->id, $screen_name, $password);
376 $this->showForm(_('Twitter settings saved.'), true);
380 * Disassociate an existing Twitter account from this account
385 function removeTwitterAccount()
387 $user = common_current_user();
389 $flink = Foreign_link::getByUserID($user->id, 1);
391 $flink_foreign_id = $this->arg('flink_foreign_id');
393 // Maybe an old tab open...?
394 if ($flink->foreign_id != $flink_foreign_id) {
395 $this->showForm(_('That is not your Twitter account.'));
399 $result = $flink->delete();
402 common_log_db_error($flink, 'DELETE', __FILE__);
403 $this->serverError(_('Couldn\'t remove Twitter user.'));
407 $this->showForm(_('Twitter account removed.'), true);
411 * Save user's Twitter-bridging preferences
416 function savePreferences()
418 $noticesync = $this->boolean('noticesync');
419 $friendsync = $this->boolean('friendsync');
420 $replysync = $this->boolean('replysync');
422 $user = common_current_user();
424 $flink = Foreign_link::getByUserID($user->id, 1);
427 common_log_db_error($flink, 'SELECT', __FILE__);
428 $this->showForm(_('Couldn\'t save Twitter preferences.'));
432 $twitter_id = $flink->foreign_id;
433 $password = $flink->credentials;
435 $fuser = $flink->getForeignUser();
438 common_log_db_error($fuser, 'SELECT', __FILE__);
439 $this->showForm(_('Couldn\'t save Twitter preferences.'));
443 $screen_name = $fuser->nickname;
445 $original = clone($flink);
447 $flink->set_flags($noticesync, $replysync, $friendsync);
449 $result = $flink->update($original);
451 if ($result === false) {
452 common_log_db_error($flink, 'UPDATE', __FILE__);
453 $this->showForm(_('Couldn\'t save Twitter preferences.'));
458 save_twitter_friends($user, $flink->foreign_id, $screen_name, $password);
461 $this->showForm(_('Twitter preferences saved.'), true);
465 * Verifies a username and password against Twitter's API
467 * @param string $screen_name Twitter user name
468 * @param string $password Twitter password
470 * @return boolean success flag
473 function verifyCredentials($screen_name, $password)
475 $uri = 'http://twitter.com/account/verifyCredentials.json';
477 $data = get_twitter_data($uri, $screen_name, $password);
483 $user = json_decode($data);
489 $twitter_id = $user->id;