]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/twittersettings.php
058bec1b0feb2b7c4a0df21b204c5916bd0afcf5
[quix0rs-gnu-social.git] / actions / twittersettings.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 require_once(INSTALLDIR.'/lib/settingsaction.php');
23
24 class TwittersettingsAction extends SettingsAction {
25
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.');
29         }
30
31         function show_form($msg=NULL, $success=false) {
32                 $user = common_current_user();
33                 $profile = $user->getProfile();
34                 $fuser = NULL;
35                 $flink = Foreign_link::getForeignLink($user->id, 1); // 1 == Twitter
36
37                 if ($flink) {
38                         $fuser = Foreign_user::getForeignUser($flink->foreign_id, 1);
39                 }
40
41                 $this->form_header(_('Twitter settings'), $msg, $success);
42                 common_element_start('form', array('method' => 'post',
43                                                                                    'id' => 'twittersettings',
44                                                                                    'action' =>
45                                                                                    common_local_url('twittersettings')));
46                 common_hidden('token', common_session_token());
47
48                 common_element('h2', NULL, _('Twitter Account'));
49
50                 if ($fuser) {
51                         common_element_start('p');
52
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'));
60                 } else {
61
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
66
67                         common_password('twitter_password', _('Twitter Password'));
68                 }
69
70                 common_element('h2', NULL, _('Preferences'));
71
72                 if ($flink) {
73                         common_checkbox('noticesync', _('Automatically send my notices to Twitter.'),
74                                 ($flink->noticesync) ? true : false);
75                         common_checkbox('friendsync', _('Subscribe to my Twitter friends here.'),
76                                 ($flink->friendsync) ? true : false);
77                         common_submit('save', _('Save'));
78                 } else {
79                         common_checkbox('noticesync', _('Automatically send my notices to Twitter.'), true);
80                         common_checkbox('friendsync', _('Subscribe to my Twitter friends here.'), true);
81                         common_submit('add', _('Add'));
82                 }
83
84                 common_element_end('form');
85                 common_show_footer();
86         }
87
88         function handle_post() {
89
90                 # CSRF protection
91                 $token = $this->trimmed('token');
92                 if (!$token || $token != common_session_token()) {
93                         $this->show_form(_('There was a problem with your session token. Try again, please.'));
94                         return;
95                 }
96
97                 if ($this->arg('save')) {
98                         $this->save_preferences();
99                 } else if ($this->arg('add')) {
100                         $this->add_twitter_acct();
101                 } else if ($this->arg('remove')) {
102                         $this->remove_twitter_acct();
103                 } else {
104                         $this->show_form(_('Unexpected form submission.'));
105                 }
106         }
107
108         function add_twitter_acct() {
109                 $twitter_username = $this->trimmed('twitter_username');
110                 $twitter_password = $this->trimmed('twitter_password');
111                 $noticesync = $this->boolean('noticesync');
112                 $friendsync = $this->boolean('friendsync');
113
114                 if (!Validate::string($twitter_username, array('min_length' => 1,
115                                                                                                            'max_length' => 15,
116                                                                                                            'format' => VALIDATE_NUM . VALIDATE_ALPHA . '_'))) {
117                         $this->show_form(_('Username must have only numbers, upper- and lowercase letters, and underscore (_).'));
118                         return;
119                 }
120
121                 // Verify this is a real Twitter user.
122                 if (!$this->verify_credentials($twitter_username, $twitter_password)) {
123                         $this->show_form(_('Could not verify your Twitter credentials!'));
124                         return;
125                 }
126
127                 // Now that we have a valid Twitter user, we have to make another api call to
128                 // find its Twitter ID.  Dumb, but true.
129                 $twitter_id = $this->get_twitter_id($twitter_username, $twitter_password);
130
131                 if (!$twitter_id) {
132                         $this->show_form(sprintf(_('Unable to retrieve account information for "%s" from Twitter.'), $twitter_username));
133                         return;
134                 }
135
136                 $fuser = DB_DataObject::factory('foreign_user');
137                 $fuser->id = $twitter_id;
138                 $fuser->service = 1; // Twitter
139                 $fuser->uri = "http://www.twitter.com/$twitter_username";
140                 $fuser->nickname = $twitter_username;
141                 $fuser->created = common_sql_now();
142                 $result = $fuser->insert();
143
144                 if (!$result) {
145                         common_log_db_error($fuser, 'INSERT', __FILE__);
146                         $this->show_form(_('Unable to save your Twitter settings!'));
147                         return;
148                 }
149
150                 $user = common_current_user();
151
152                 $flink = DB_DataObject::factory('foreign_link');
153                 $flink->user_id = $user->id;
154                 $flink->foreign_id = $fuser->id;
155                 $flink->service = 1; // Twitter
156                 $flink->credentials = $twitter_password;
157                 $flink->created = common_sql_now();
158                 $flink->noticesync = ($noticesync) ? 1 : 0;
159                 $flink->friendsync = ($friendsync) ? 2 : 0;
160                 $flink->profilesync = 0; // XXX: leave as default?
161                 $flink_id = $flink->insert();
162
163                 if (!$flink_id) {
164                         common_log_db_error($flink, 'INSERT', __FILE__);
165                         $this->show_form(_('Unable to save your Twitter settings!'));
166                         return;
167                 }
168
169                 $this->show_form(_('Twitter settings saved.'), true);
170         }
171
172         function remove_twitter_acct() {
173                 $user = common_current_user();
174
175                 // For now we assume one Twitter acct per Laconica acct
176                 $flink = Foreign_link::getForeignLink($user->id, 1);
177                 $fuser = Foreign_user::getForeignUser($flink->foreign_id, 1);
178                 $flink_foreign_id = $this->arg('flink_foreign_id');
179
180                 if (!$flink) {
181                         common_debug("couldn't get flink");
182                 }
183
184                 # Maybe an old tab open...?
185                 if ($flink->foreign_id != $flink_foreign_id) {
186                         common_debug("flink user_id = " . $flink->user_id);
187                     $this->show_form(_('That is not your Twitter account.'));
188                     return;
189                 }
190
191                 $result = $fuser->delete();
192
193                 if (!$result) {
194                         common_log_db_error($fuser, 'DELETE', __FILE__);
195                         $this->show_form(_('Couldn\'t remove Twitter user.'));
196                         return;
197                 }
198
199                 $result = $flink->delete();
200
201                 if (!$result) {
202                         common_log_db_error($flink, 'DELETE', __FILE__);
203                         common_server_error(_('Couldn\'t remove Twitter user.'));
204                         return;
205                 }
206
207                 $this->show_form(_('Twitter account removed.'), TRUE);
208         }
209
210         function save_preferences() {
211                 $noticesync = $this->boolean('noticesync');
212                 $friendsync = $this->boolean('friendsync');
213                 $user = common_current_user();
214                 $flink = Foreign_link::getForeignLink($user->id, 1);
215
216                 if (!$flink) {
217                         common_log_db_error($flink, 'SELECT', __FILE__);
218                         $this->show_form(_('Couldn\'t save Twitter preferences.'));
219                         return;
220                 }
221
222                 $flink->noticesync = ($noticesync) ? 1 : 0;
223                 $flink->friendsync = ($friendsync) ? 2 : 0;
224                 // $flink->profilesync = 0; // XXX: leave as default?
225                 $result = $flink->update();
226
227                 if (!$result) {
228                         common_log_db_error($flink, 'UPDATE', __FILE__);
229                         $this->show_form(_('Couldn\'t save Twitter preferences.'));
230                         return;
231                 }
232
233                 $this->show_form(_('Twitter preferences saved.'));
234
235                 return;
236         }
237
238         function get_twitter_id($user, $password) {
239                 $uri = "http://twitter.com/users/show/$user.json";
240                 $data = $this->get_twitter_data($uri, $user, $password);
241
242                 if (!$data) {
243                         return NULL;
244                 }
245
246                 $user = json_decode($data);
247
248                 if (!$user) {
249                         return NULL;
250                 }
251
252                 return $user->id;
253         }
254
255         function verify_credentials($user, $password) {
256                 $uri = 'http://twitter.com/account/verify_credentials.json';
257                 $data = $this->get_twitter_data($uri, $user, $password);
258
259                 if (!$data) {
260                         return false;
261                 }
262
263                 $creds = json_decode($data);
264
265                 if (!$creds) {
266                         return false;
267                 }
268
269                 if ($creds->authorized == 1) {
270                         return true;
271                 }
272
273                 return false;
274         }
275
276         // PHP's cURL the best thing to use here? -- Zach
277         function get_twitter_data($uri, $user=NULL, $password=NULL) {
278                 $options = array(
279                                 CURLOPT_USERPWD => "$user:$password",
280                                 CURLOPT_RETURNTRANSFER  => true,
281                                 CURLOPT_FAILONERROR             => true,
282                                 CURLOPT_HEADER                  => false,
283                                 CURLOPT_FOLLOWLOCATION  => true,
284                                 // CURLOPT_USERAGENT            => "identi.ca",
285                                 CURLOPT_CONNECTTIMEOUT  => 120,
286                                 CURLOPT_TIMEOUT                 => 120
287                 );
288
289                 $ch = curl_init($uri);
290             curl_setopt_array($ch, $options);
291             $data = curl_exec($ch);
292             $errmsg = curl_error($ch);
293
294                 if ($errmsg) {
295                         common_debug("cURL error: $errmsg - trying to load: $uri with user $user.", __FILE__);
296                 }
297
298                 curl_close($ch);
299
300                 return $data;
301         }
302
303 }