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 class TwittersettingsAction extends SettingsAction {
26 function get_instructions() {
27 return _('Add your Twitter account to automatically send your notices to Twitter, ' .
28 'and subscribe to Twitter friends already here.');
31 function show_form($msg=NULL, $success=false) {
32 $user = common_current_user();
33 $profile = $user->getProfile();
35 $flink = Foreign_link::getForeignLink($user->id, 1); // 1 == Twitter
38 $fuser = Foreign_user::getForeignUser($flink->foreign_id, 1);
41 $this->form_header(_('Twitter settings'), $msg, $success);
42 common_element_start('form', array('method' => 'post',
43 'id' => 'twittersettings',
45 common_local_url('twittersettings')));
46 common_hidden('token', common_session_token());
48 common_element('h2', NULL, _('Twitter Account'));
51 common_element_start('p');
53 common_element('span', 'twitter_user', $fuser->nickname);
54 common_element('a', array('href' => $fuser->uri), $fuser->uri);
55 common_element('span', 'input_instructions',
56 _('Current verified Twitter account.'));
57 common_hidden('flink_foreign_id', $flink->foreign_id);
58 common_element_end('p');
59 common_submit('remove', _('Remove'));
62 // XXX: Should we make an educated guess as to the twitter acct name? -- Zach
63 common_input('twitter_username', _('Twitter Username'),
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,
83 common_submit('save', _('Save'));
85 common_submit('add', _('Add'));
88 common_element_end('form');
92 function handle_post() {
95 $token = $this->trimmed('token');
96 if (!$token || $token != common_session_token()) {
97 $this->show_form(_('There was a problem with your session token. Try again, please.'));
101 if ($this->arg('save')) {
102 $this->save_preferences();
103 } else if ($this->arg('add')) {
104 $this->add_twitter_acct();
105 } else if ($this->arg('remove')) {
106 $this->remove_twitter_acct();
108 $this->show_form(_('Unexpected form submission.'));
112 function add_twitter_acct() {
113 $twitter_username = $this->trimmed('twitter_username');
114 $twitter_password = $this->trimmed('twitter_password');
115 $noticesync = $this->boolean('noticesync');
116 $replysync = $this->boolean('replysync');
117 $friendsync = $this->boolean('friendsync');
119 if (!Validate::string($twitter_username, array('min_length' => 1,
121 'format' => VALIDATE_NUM . VALIDATE_ALPHA . '_'))) {
122 $this->show_form(_('Username must have only numbers, upper- and lowercase letters, and underscore (_). 15 chars max.'));
126 // Verify this is a real Twitter user.
127 if (!$this->verify_credentials($twitter_username, $twitter_password)) {
128 $this->show_form(_('Could not verify your Twitter credentials!'));
132 // Now that we have a valid Twitter user, we have to make another api call to
133 // find its Twitter ID. Dumb, but true.
134 $twitter_id = $this->get_twitter_id($twitter_username, $twitter_password);
137 $this->show_form(sprintf(_('Unable to retrieve account information for "%s" from Twitter.'), $twitter_username));
141 $fuser = DB_DataObject::factory('foreign_user');
142 $fuser->id = $twitter_id;
143 $fuser->service = 1; // Twitter
144 $fuser->uri = "http://www.twitter.com/$twitter_username";
145 $fuser->nickname = $twitter_username;
146 $fuser->created = common_sql_now();
147 $result = $fuser->insert();
150 common_log_db_error($fuser, 'INSERT', __FILE__);
151 $this->show_form(_('Unable to save your Twitter settings!'));
155 $user = common_current_user();
157 $flink = DB_DataObject::factory('foreign_link');
158 $flink->user_id = $user->id;
159 $flink->foreign_id = $fuser->id;
160 $flink->service = 1; // Twitter
161 $flink->credentials = $twitter_password;
162 $flink->created = common_sql_now();
164 $this->set_flags($flink, $noticesync, $replysync, $friendsync);
166 $flink_id = $flink->insert();
169 common_log_db_error($flink, 'INSERT', __FILE__);
170 $this->show_form(_('Unable to save your Twitter settings!'));
174 $this->show_form(_('Twitter settings saved.'), true);
177 function remove_twitter_acct() {
178 $user = common_current_user();
180 // For now we assume one Twitter acct per Laconica acct
181 $flink = Foreign_link::getForeignLink($user->id, 1);
182 $fuser = Foreign_user::getForeignUser($flink->foreign_id, 1);
183 $flink_foreign_id = $this->arg('flink_foreign_id');
186 common_debug("couldn't get flink");
189 # Maybe an old tab open...?
190 if ($flink->foreign_id != $flink_foreign_id) {
191 common_debug("flink user_id = " . $flink->user_id);
192 $this->show_form(_('That is not your Twitter account.'));
196 $result = $fuser->delete();
199 common_log_db_error($fuser, 'DELETE', __FILE__);
200 $this->show_form(_('Couldn\'t remove Twitter user.'));
204 $result = $flink->delete();
207 common_log_db_error($flink, 'DELETE', __FILE__);
208 common_server_error(_('Couldn\'t remove Twitter user.'));
212 $this->show_form(_('Twitter account removed.'), TRUE);
215 function save_preferences() {
216 $noticesync = $this->boolean('noticesync');
217 $friendsync = $this->boolean('friendsync');
218 $replysync = $this->boolean('replysync');
220 $user = common_current_user();
221 $flink = Foreign_link::getForeignLink($user->id, 1);
224 common_log_db_error($flink, 'SELECT', __FILE__);
225 $this->show_form(_('Couldn\'t save Twitter preferences.'));
229 $flink->query('BEGIN');
231 $original = clone($flink);
233 $this->set_flags($flink, $noticesync, $replysync, $friendsync);
235 $result = $flink->update($original);
237 if ($result === FALSE) {
238 common_log_db_error($flink, 'UPDATE', __FILE__);
239 $this->show_form(_('Couldn\'t save Twitter preferences.'));
243 $flink->query('COMMIT');
245 $this->show_form(_('Twitter preferences saved.'));
250 function get_twitter_id($user, $password) {
251 $uri = "http://twitter.com/users/show/$user.json";
252 $data = $this->get_twitter_data($uri, $user, $password);
258 $user = json_decode($data);
267 function verify_credentials($user, $password) {
268 $uri = 'http://twitter.com/account/verify_credentials.json';
269 $data = $this->get_twitter_data($uri, $user, $password);
275 $creds = json_decode($data);
281 if ($creds->authorized == 1) {
288 // PHP's cURL the best thing to use here? -- Zach
289 function get_twitter_data($uri, $user=NULL, $password=NULL) {
291 CURLOPT_USERPWD => "$user:$password",
292 CURLOPT_RETURNTRANSFER => true,
293 CURLOPT_FAILONERROR => true,
294 CURLOPT_HEADER => false,
295 CURLOPT_FOLLOWLOCATION => true,
296 // CURLOPT_USERAGENT => "identi.ca",
297 CURLOPT_CONNECTTIMEOUT => 120,
298 CURLOPT_TIMEOUT => 120
301 $ch = curl_init($uri);
302 curl_setopt_array($ch, $options);
303 $data = curl_exec($ch);
304 $errmsg = curl_error($ch);
307 common_debug("cURL error: $errmsg - trying to load: $uri with user $user.", __FILE__);
315 function set_flags(&$flink, $noticesync, $replysync, $friendsync) {
317 $flink->noticesync |= FOREIGN_NOTICE_SEND;
319 $flink->noticesync &= ~FOREIGN_NOTICE_SEND;
323 $flink->noticesync |= FOREIGN_NOTICE_SEND_REPLY;
325 $flink->noticesync &= ~FOREIGN_NOTICE_SEND_REPLY;
329 $flink->friendsync |= FOREIGN_FRIEND_RECV;
331 $flink->friendsync &= ~FOREIGN_FRIEND_RECV;
334 $flink->profilesync = 0; // XXX: leave as default?