]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/twittersettings.php
592c9347f6d6994ceb50e28ddaf3249735319fe3
[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::staticGet('user_id', $flink->user_id);
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
47                 if ($fuser) {
48                         common_element_start('p');
49                          
50                         common_element('span', 'twitter_user', $fuser->nickname);
51                         common_element('a', array('href' => $fuser->uri),  $fuser->uri);
52                         common_element('span', 'input_instructions',
53                                        _('Current verified Twitter User'));
54                         common_hidden('flink_user_id', $flink->user_id);
55                         common_element_end('p');
56                         common_submit('remove', _('Remove'));
57                 } else {
58
59                         // XXX: Should we make an educated guess as to the twitter acct name? -- Zach
60                         common_input('twitter_username', _('Twitter Username'),
61                                                  ($this->arg('twitter_username')) ? $this->arg('twitter_username') : $profile->nickname,
62                                                  _('No spaces, please.')); // hey, it's what Twitter says
63
64                         common_password('twitter_password', _('Twitter Password'));             
65                 }
66         
67                 common_element('h2', NULL, _('Preferences'));
68                 
69                 if ($flink) { 
70                         common_checkbox('noticesync', _('Automatically send my notices to Twitter.'), 
71                                 ($flink->noticesync) ? true : false);
72                         common_checkbox('friendsync', _('Subscribe to my Twitter friends here.'),
73                                 ($flink->friendsync) ? true : false);                   
74                         common_submit('save', _('Save'));       
75                 } else {
76                         common_checkbox('noticesync', _('Automatically send my notices to Twitter.'), true);
77                         common_checkbox('friendsync', _('Subscribe to my Twitter friends here.'), true);
78                         common_submit('add', _('Add'));
79                 }
80                 
81                 common_element_end('form');
82                 common_show_footer();
83         }
84
85         function handle_post() {
86                 if ($this->arg('save')) {
87                         $this->save_preferences();
88                 } else if ($this->arg('add')) {
89                         $this->add_twitter_acct();
90                 } else if ($this->arg('remove')) {
91                         $this->remove_twitter_acct();
92                 } else {
93                         $this->show_form(_('Unexpected form submission.'));
94                 }
95         }
96                 
97         function add_twitter_acct() {
98                 $twitter_username = $this->trimmed('twitter_username');
99                 $twitter_password = $this->trimmed('twitter_password');
100                 $noticesync = $this->boolean('noticesync');
101                 $friendsync = $this->boolean('friendsync');
102                 
103                 if (!Validate::string($twitter_username, array('min_length' => 1,
104                                                                                            'max_length' => 64,
105                                                                                            'format' => VALIDATE_NUM . VALIDATE_ALPHA_LOWER))) {
106                         $this->show_form(_('Username must have only lowercase letters and numbers and no spaces.'));
107                         return;
108                 }
109                 
110                 // Verify this is a real Twitter user.
111                 if (!$this->verify_credentials($twitter_username, $twitter_password)) {
112                         $this->show_form(_('Could not verify your Twitter credentials!'));
113                         return;
114                 }
115                 
116                 // Now that we have a valid Twitter user, we have to make another api call to 
117                 // find its Twitter ID.  Dumb, but true.
118                 $twitter_id = $this->get_twitter_id($twitter_username);
119                 
120                 if (!$twitter_id) {
121                         $this->show_form(sprintf(_('Unable to retrieve account information for "%s" from Twitter.'), $twitter_username));
122                         return;
123                 }
124                                 
125                 $fuser = DB_DataObject::factory('foreign_user');
126                 $fuser->id = $twitter_id;
127                 $fuser->service = 1; // Twitter         
128                 $fuser->uri = "http://www.twitter.com/$twitter_username";
129                 $fuser->nickname = $twitter_username;
130                 $fuser->created = common_sql_now();
131                 $result = $fuser->insert();
132                 
133                 if (!$result) {
134                         common_log_db_error($fuser, 'INSERT', __FILE__);
135                         $this->show_form(_('Unable to save your Twitter settings!'));
136                         return;
137                 }
138
139                 $user = common_current_user();
140                 
141                 $flink = DB_DataObject::factory('foreign_link');
142                 $flink->user_id = $user->id;
143                 $flink->foreign_id = $fuser->id;
144                 $flink->service = 1; // Twitter
145                 $flink->credentials = $twitter_password;
146                 $flink->created = common_sql_now();
147                 $flink->noticesync = ($noticesync) ? 1 : 0;
148                 $flink->friendsync = ($friendsync) ? 2 : 0;
149                 $flink->profilesync = 0; // XXX: leave as default?
150                 $flink_id = $flink->insert();
151                 
152                 if (!$flink_id) {
153                         common_log_db_error($flink, 'INSERT', __FILE__);
154                         $this->show_form(_('Unable to save your Twitter settings!'));
155                         return;
156                 }
157                 
158                 $this->show_form(_('Twitter settings saved.'), true);
159         }
160
161         function remove_twitter_acct() {
162                 $user = common_current_user();
163                 
164                 // For now we assume one Twitter acct per Laconica acct
165                 $flink = Foreign_link::getForeignLink($user->id, 1);
166                 $fuser = Foreign_user::getForeignUser($flink->foreign_id, 1);
167                 $flink_user_id = $this->arg('flink_user_id');
168
169                 if (!$flink) {
170                         common_debug("couldn't get flink");
171                 }
172
173                 # Maybe an old tab open...?
174                 if ($flink->user_id != $flink_user_id) {
175                         common_debug("flink user_id = " . $flink->user_id);
176                     $this->show_form(_('That is not your Twitter account.'));
177                     return;
178                 }
179
180                 $result = $fuser->delete();
181                 
182                 if (!$result) {
183                         common_log_db_error($flink, 'DELETE', __FILE__);
184                         $this->show_form(_('Couldn\'t remove Twitter user.'));
185                         return;
186                 }
187                 
188                 $result = $flink->delete();
189
190                 if (!$result) {
191                         common_log_db_error($flink, 'DELETE', __FILE__);
192                         common_server_error(_('Couldn\'t remove Twitter user.'));
193                         return;
194                 }
195
196                 $this->show_form(_('Twitter account removed.'), TRUE);
197         }
198         
199         function save_preferences() {
200                 $noticesync = $this->boolean('noticesync');
201                 $friendsync = $this->boolean('friendsync');
202                 $user = common_current_user();
203                 $flink = Foreign_link::getForeignLink($user->id, 1);
204                 
205                 if (!$flink) {
206                         common_log_db_error($flink, 'SELECT', __FILE__);
207                         $this->show_form(_('Couldn\'t save Twitter preferences.'));
208                         return;
209                 } 
210                 
211                 $flink->noticesync = ($noticesync) ? 1 : 0;
212                 $flink->friendsync = ($friendsync) ? 2 : 0;
213                 // $flink->profilesync = 0; // XXX: leave as default?
214                 $result = $flink->update();
215         
216                 if (!$result) {
217                         common_log_db_error($flink, 'UPDATE', __FILE__);
218                         $this->show_form(_('Couldn\'t save Twitter preferences.'));
219                         return;
220                 }
221         
222                 $this->show_form(_('Twitter preferences saved.'));
223                 
224                 return;
225         }
226
227         function get_twitter_id($twitter_username) {    
228                 $uri = "http://twitter.com/users/show/$twitter_username.json";                  
229                 $data = $this->get_twitter_data($uri);
230                 
231                 if (!$data) {
232                         return NULL;
233                 }
234                 
235                 $user = json_decode($data);
236
237                 if (!$user) {
238                         return NULL;
239                 }
240                 
241                 return $user->id;
242         }
243
244         function verify_credentials($user, $password) {         
245                 $uri = 'http://twitter.com/account/verify_credentials.json';
246                 $data = $this->get_twitter_data($uri, $user, $password);
247                 
248                 if (!$data) {
249                         return false;
250                 }
251                 
252                 $creds = json_decode($data); 
253                 
254                 if (!$creds) {
255                         return false;
256                 }
257                 
258                 if ($creds->authorized == 1) { 
259                         return true;
260                 }
261                 
262                 return false;           
263         }
264         
265         // PHP's cURL the best thing to use here? -- Zach
266         function get_twitter_data($uri, $user=NULL, $password=NULL) {
267                 $options = array(
268                                 CURLOPT_USERPWD => "$user:$password",
269                                 CURLOPT_RETURNTRANSFER  => true,
270                                 CURLOPT_FAILONERROR             => true,
271                                 CURLOPT_HEADER                  => false,
272                                 CURLOPT_FOLLOWLOCATION  => true,
273                                 // CURLOPT_USERAGENT            => "identi.ca",
274                                 CURLOPT_CONNECTTIMEOUT  => 120,
275                                 CURLOPT_TIMEOUT                 => 120
276                 );
277                 
278                 $ch = curl_init($uri);
279             curl_setopt_array($ch, $options);
280             $data = curl_exec($ch);
281             $errmsg = curl_error($ch);
282
283                 if ($errmsg) {
284                         common_debug("cURL error: $errmsg - trying to load: $uri with user $user.", __FILE__);
285                 }
286
287                 curl_close($ch);
288                 
289                 return $data;
290         }
291
292 }