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 _('Enter your Twitter credentials to automatically send your notices to Twitter, ' .
28 'and subscribe to Twitter friends already here.');
31 function show_form($msg=NULL, $success=false) {
33 $user = common_current_user();
34 $fuser = Foreign_user::staticGet('user_id', $user->id);
36 $this->form_header(_('Twitter settings'), $msg, $success);
38 common_element_start('form', array('method' => 'post',
39 'id' => 'twittersettings',
41 common_local_url('twittersettings')));
45 common_element_start('p');
47 common_element('span', 'Twitter User', "http://www.twitter.com/$fuser->nickname");
48 common_element('span', 'input_instructions',
49 _('Current verified Twitter User'));
50 common_hidden('fuser_id', $fuser->id);
52 common_element_end('p');
53 common_submit('remove', _('Remove'));
57 common_input('twitter_username', _('Twitter Username'),
58 ($this->arg('twitter_username')) ? $this->arg('twitter_username') : $profile->nickname,
59 _('No spaces, please.')); // hey, it's what Twitter says
61 common_password('twitter_password', _('Twitter Password'));
63 common_submit('add', _('Add'));
67 common_element('h2', NULL, _('Preferences'));
69 // these checkboxes don't do anything yet
71 common_checkbox('repost', _('Automatically send my notices to Twitter.'), true);
72 common_checkbox('subscribe_friends', _('Subscribe to my Twitter friends here.'), true);
74 common_submit('save', _('Save'));
77 common_element_end('form');
81 function handle_post() {
83 if ($this->arg('save')) {
84 $this->save_preferences();
85 } else if ($this->arg('add')) {
86 $this->add_twitter_user();
87 } else if ($this->arg('remove')) {
88 $this->remove_twitter_user();
90 $this->show_form(_('Unexpected form submission.'));
95 function remove_twitter_user() {
97 $user = common_current_user();
98 $fuser = Foreign_user::staticGet('user_id', $user->id);
100 $fuser_id = $this->arg('fuser_id');
102 # Maybe an old tab open...?
104 if ($fuser->id != $fuser_id) {
105 $this->show_form(_('That is not your Twitter account.'));
109 $result = $fuser->delete();
112 common_log_db_error($user, 'UPDATE', __FILE__);
113 common_server_error(_('Couldn\'t remove Twitter user.'));
117 $this->show_form(_('Twitter account removed.'), TRUE);
121 function save_preferences() {
123 $user = common_current_user();
124 $fuser = Foreign_user::staticGet('user_id', $user->id);
126 $this->show_form(_('Save doesn\'t do anything yet.'));
131 function add_twitter_user() {
133 $user = common_current_user();
134 $fuser = Foreign_user::staticGet('user_id', $user->id);
137 $twitter_username = $this->trimmed('twitter_username');
138 $twitter_password = $this->trimmed('twitter_password');
140 if (!Validate::string($twitter_username, array('min_length' => 1,
142 'format' => VALIDATE_NUM . VALIDATE_ALPHA_LOWER))) {
143 $this->show_form(_('Username must have only lowercase letters and numbers and no spaces.'));
147 // Verify this is a real Twitter user.
148 if (!$this->verify_credentials($twitter_username, $twitter_password)) {
149 $this->show_form(_('Could not verify your Twitter credentials!'));
153 // Now that we have a valid Twitter user, we have to make another api call to
154 // find its Twitter ID.
155 $twitter_id = $this->get_twitter_id($twitter_username);
158 $this->show_form(sprintf(_('Unable to retrieve account information for "%s" from Twitter.'), $twitter_username));
162 $user = common_current_user();
164 $fuser = Foreign_user::save(
167 'service' => '0', // Twitter
168 'uri' => "http://www.twitter.com/$twitter_username",
169 'nickname' => $twitter_username,
170 'user_id' => $user->id,
171 'credentials' => $twitter_password
175 $this->show_form(_('Unable to save your Twitter credentials!'));
178 $this->show_form(_('Twitter settings saved.'), true);
181 function get_twitter_id($twitter_username) {
183 $uri = "http://twitter.com/users/show/$twitter_username.json";
184 $data = $this->get_twitter_data($uri);
190 $user = json_decode($data);
199 function verify_credentials($user, $password) {
201 $uri = 'http://twitter.com/account/verify_credentials.json';
202 $data = $this->get_twitter_data($uri, $user, $password);
208 $creds = json_decode($data);
214 if ($creds->authorized == 1) {
221 // PHP's cURL the best thing to use here? -- Zach
222 function get_twitter_data($uri, $user=NULL, $password=NULL) {
224 CURLOPT_USERPWD => "$user:$password",
225 CURLOPT_RETURNTRANSFER => true,
226 CURLOPT_FAILONERROR => true,
227 CURLOPT_HEADER => false,
228 CURLOPT_FOLLOWLOCATION => true,
229 // CURLOPT_USERAGENT => "identi.ca",
230 CURLOPT_CONNECTTIMEOUT => 120,
231 CURLOPT_TIMEOUT => 120
234 $ch = curl_init($uri);
235 curl_setopt_array($ch, $options);
236 $data = curl_exec($ch);
237 $errmsg = curl_error($ch);
240 common_debug("cURL error: $errmsg - trying to load: $uri with user $user.", __FILE__);