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/settingsaction.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 SettingsAction
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' => 'twittersettings',
102 common_local_url('twittersettings')));
103 $this->hidden('token', common_session_token());
105 $this->element('h2', null, _('Twitter Account'));
108 $this->elementStart('p');
110 $this->element('span', 'twitter_user', $fuser->nickname);
111 $this->element('a', array('href' => $fuser->uri), $fuser->uri);
112 $this->element('span', 'input_instructions',
113 _('Current verified Twitter account.'));
114 $this->hidden('flink_foreign_id', $flink->foreign_id);
115 $this->elementEnd('p');
116 $this->submit('remove', _('Remove'));
118 $this->input('twitter_username', _('Twitter user name'),
119 ($this->arg('twitter_username')) ?
120 $this->arg('twitter_username') :
122 _('No spaces, please.')); // hey, it's what Twitter says
124 $this->password('twitter_password', _('Twitter password'));
127 $this->element('h2', null, _('Preferences'));
129 $this->checkbox('noticesync',
130 _('Automatically send my notices to Twitter.'),
132 ($flink->noticesync & FOREIGN_NOTICE_SEND) :
135 $this->checkbox('replysync',
136 _('Send local "@" replies to Twitter.'),
138 ($flink->noticesync & FOREIGN_NOTICE_SEND_REPLY) :
141 $this->checkbox('friendsync',
142 _('Subscribe to my Twitter friends here.'),
144 ($flink->friendsync & FOREIGN_FRIEND_RECV) :
148 $this->submit('save', _('Save'));
150 $this->submit('add', _('Add'));
153 $this->showTwitterSubscriptions();
155 $this->elementEnd('form');
159 * Gets some of the user's Twitter friends
161 * Gets the number of Twitter friends that are on this
162 * instance of Laconica.
164 * @return array array of User objects
167 function subscribedTwitterUsers()
170 $current_user = common_current_user();
172 $qry = 'SELECT user.* ' .
173 'FROM subscription ' .
174 'JOIN user ON subscription.subscribed = user.id ' .
175 'JOIN foreign_link ON foreign_link.user_id = user.id ' .
176 'WHERE subscriber = %d ' .
177 'ORDER BY user.nickname';
181 $user->query(sprintf($qry, $current_user->id));
185 while ($user->fetch()) {
187 // Don't include the user's own self-subscription
188 if ($user->id != $current_user->id) {
189 $users[] = clone($user);
197 * Show user's Twitter friends
199 * Gets the number of Twitter friends that are on this
200 * instance of Laconica, and shows their mini-avatars.
205 function showTwitterSubscriptions()
208 $friends = $this->subscribedTwitterUsers();
210 $friends_count = count($friends);
212 if ($friends_count > 0) {
214 $this->element('h3', null, _('Twitter Friends'));
215 $this->elementStart('div', array('id' => 'subscriptions'));
216 $this->elementStart('ul', array('id' => 'subscriptions_avatars'));
218 for ($i = 0; $i < min($friends_count, SUBSCRIPTIONS); $i++) {
220 $other = Profile::staticGet($friends[$i]->id);
223 common_log_db_error($subs, 'SELECT', __FILE__);
227 $this->elementStart('li');
228 $this->elementStart('a', array('title' => ($other->fullname) ?
231 'href' => $other->profileurl,
233 'class' => 'subscription'));
235 $avatar = $other->getAvatar(AVATAR_MINI_SIZE);
237 $avatar_url = ($avatar) ?
238 common_avatar_display_url($avatar) :
239 common_default_avatar(AVATAR_MINI_SIZE);
241 $this->element('img', array('src' => $avatar_url,
242 'width' => AVATAR_MINI_SIZE,
243 'height' => AVATAR_MINI_SIZE,
244 'class' => 'avatar mini',
245 'alt' => ($other->fullname) ?
248 $this->elementEnd('a');
249 $this->elementEnd('li');
253 $this->elementEnd('ul');
254 $this->elementEnd('div');
260 * Handle posts to this form
262 * Based on the button that was pressed, muxes out to other functions
263 * to do the actual task requested.
265 * All sub-functions reload the form with a message -- success or failure.
270 function handlePost()
274 $token = $this->trimmed('token');
275 if (!$token || $token != common_session_token()) {
276 $this->showForm(_('There was a problem with your session token. '.
277 'Try again, please.'));
281 if ($this->arg('save')) {
282 $this->savePreferences();
283 } else if ($this->arg('add')) {
284 $this->addTwitterAccount();
285 } else if ($this->arg('remove')) {
286 $this->removeTwitterAccount();
288 $this->showForm(_('Unexpected form submission.'));
293 * Associate a Twitter account with the user's account
295 * Validates post input; verifies it against Twitter; and if
296 * successful stores in the database.
301 function addTwitterAccount()
303 $screen_name = $this->trimmed('twitter_username');
304 $password = $this->trimmed('twitter_password');
305 $noticesync = $this->boolean('noticesync');
306 $replysync = $this->boolean('replysync');
307 $friendsync = $this->boolean('friendsync');
309 if (!Validate::string($screen_name,
310 array('min_length' => 1,
312 'format' => VALIDATE_NUM.VALIDATE_ALPHA.'_'))) {
313 $this->showForm(_('Username must have only numbers, '.
314 'upper- and lowercase letters, '.
315 'and underscore (_). 15 chars max.'));
319 if (!$this->verifyCredentials($screen_name, $password)) {
320 $this->showForm(_('Could not verify your Twitter credentials!'));
324 $twit_user = twitter_user_info($screen_name, $password);
327 $this->showForm(sprintf(_('Unable to retrieve account information '.
328 'For "%s" from Twitter.'),
333 if (!save_twitter_user($twit_user->id, $screen_name)) {
334 $this->showForm(_('Unable to save your Twitter settings!'));
338 $user = common_current_user();
340 $flink = new Foreign_link();
342 $flink->user_id = $user->id;
343 $flink->foreign_id = $twit_user->id;
344 $flink->service = 1; // Twitter
345 $flink->credentials = $password;
346 $flink->created = common_sql_now();
348 $flink->set_flags($noticesync, $replysync, $friendsync);
350 $flink_id = $flink->insert();
353 common_log_db_error($flink, 'INSERT', __FILE__);
354 $this->showForm(_('Unable to save your Twitter settings!'));
359 save_twitter_friends($user, $twit_user->id, $screen_name, $password);
362 $this->showForm(_('Twitter settings saved.'), true);
366 * Disassociate an existing Twitter account from this account
371 function removeTwitterAccount()
373 $user = common_current_user();
375 $flink = Foreign_link::getByUserID($user->id, 1);
377 $flink_foreign_id = $this->arg('flink_foreign_id');
379 // Maybe an old tab open...?
380 if ($flink->foreign_id != $flink_foreign_id) {
381 $this->showForm(_('That is not your Twitter account.'));
385 $result = $flink->delete();
388 common_log_db_error($flink, 'DELETE', __FILE__);
389 $this->serverError(_('Couldn\'t remove Twitter user.'));
393 $this->showForm(_('Twitter account removed.'), true);
397 * Save user's Twitter-bridging preferences
402 function savePreferences()
404 $noticesync = $this->boolean('noticesync');
405 $friendsync = $this->boolean('friendsync');
406 $replysync = $this->boolean('replysync');
408 $user = common_current_user();
410 $flink = Foreign_link::getByUserID($user->id, 1);
413 common_log_db_error($flink, 'SELECT', __FILE__);
414 $this->showForm(_('Couldn\'t save Twitter preferences.'));
418 $twitter_id = $flink->foreign_id;
419 $password = $flink->credentials;
421 $fuser = $flink->getForeignUser();
424 common_log_db_error($fuser, 'SELECT', __FILE__);
425 $this->showForm(_('Couldn\'t save Twitter preferences.'));
429 $screen_name = $fuser->nickname;
431 $original = clone($flink);
433 $flink->set_flags($noticesync, $replysync, $friendsync);
435 $result = $flink->update($original);
437 if ($result === false) {
438 common_log_db_error($flink, 'UPDATE', __FILE__);
439 $this->showForm(_('Couldn\'t save Twitter preferences.'));
444 save_twitter_friends($user, $flink->foreign_id, $screen_name, $password);
447 $this->showForm(_('Twitter preferences saved.'), true);
451 * Verifies a username and password against Twitter's API
453 * @param string $screen_name Twitter user name
454 * @param string $password Twitter password
456 * @return boolean success flag
459 function verifyCredentials($screen_name, $password)
461 $uri = 'http://twitter.com/account/verifyCredentials.json';
463 $data = get_twitter_data($uri, $screen_name, $password);
469 $user = json_decode($data);
475 $twitter_id = $user->id;