]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/twittersettings.php
0aae7f7e4e1d79be2459039773e1c3b8e5f5c203
[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 credentials 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                 
33                 $user = common_current_user();
34                 $profile = $user->getProfile();
35                 $fuser = Foreign_user::getForeignUser($user->id, 0);
36                 
37                 $this->form_header(_('Twitter settings'), $msg, $success);
38
39                 common_element_start('form', array('method' => 'post',
40                                                                                    'id' => 'twittersettings',
41                                                                                    'action' =>
42                                                                                    common_local_url('twittersettings')));
43
44                 if ($fuser) {
45
46                         common_element_start('p');
47                         
48                         common_element('span', 'Twitter User', "http://www.twitter.com/$fuser->nickname");
49                         common_element('span', 'input_instructions',
50                                        _('Current verified Twitter User'));
51                         common_hidden('fuser_id', $fuser->id);
52                         
53                         common_element_end('p');
54                         common_submit('remove', _('Remove'));
55                         
56                 } else {
57
58                         // XXX: Should we make an educated guess as to the twitter accnt name? -- Zach
59                         common_input('twitter_username', _('Twitter Username'),
60                                                  ($this->arg('twitter_username')) ? $this->arg('twitter_username') : $profile->nickname,
61                                                  _('No spaces, please.')); // hey, it's what Twitter says
62
63                         common_password('twitter_password', _('Twitter Password'));             
64                         
65                         common_submit('add', _('Add'));
66                                 
67                 }
68         
69                 common_element('h2', NULL, _('Preferences'));
70                 
71                 // XXX: these checkboxes don't do anything yet
72                 common_checkbox('repost', _('Automatically send my notices to Twitter.'), true);
73                 common_checkbox('subscribe_friends', _('Subscribe to my Twitter friends here.'), true);
74
75                 common_submit('save', _('Save'));
76                 
77                 common_element_end('form');
78                 common_show_footer();
79         }
80
81         function handle_post() {
82                 
83                 if ($this->arg('save')) {
84                         $this->save_preferences();
85                 } else if ($this->arg('add')) {
86                         $this->add_twitter_acct();
87                 } else if ($this->arg('remove')) {
88                         $this->remove_twitter_acct();
89                 } else {
90                         $this->show_form(_('Unexpected form submission.'));
91                 }
92         }
93                 
94         function add_twitter_acct() {
95
96                 $user = common_current_user();          
97                 $fuser = Foreign_user::getForeignUser($user->id, 0);
98
99
100                 $twitter_username = $this->trimmed('twitter_username');
101                 $twitter_password = $this->trimmed('twitter_password');
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                 $user = common_current_user();
126                                 
127                 $fuser = Foreign_user::save(
128                                 array(
129                                         'id' => $twitter_id,
130                                         'service' => '0', // Twitter
131                                         'uri' => "http://www.twitter.com/$twitter_username",
132                                         'nickname' => $twitter_username, 
133                                         'user_id' => $user->id,
134                                         'credentials' => $twitter_password
135                                 ));
136                 
137                 if (!$fuser) {
138                         $this->show_form(_('Unable to save your Twitter credentials!'));
139                 }
140                 
141                 $this->show_form(_('Twitter settings saved.'), true);
142         }
143
144         function remove_twitter_acct() {
145                 
146                 $user = common_current_user();
147                 $fuser = Foreign_user::getForeignUser($user->id, 0);
148         
149                 $fuser_id = $this->arg('fuser_id');
150
151                 # Maybe an old tab open...?
152
153                 if ($fuser->id != $fuser_id) {
154                     $this->show_form(_('That is not your Twitter account.'));
155                     return;
156                 }
157
158                 $result = $fuser->delete();
159                 
160                 if (!$result) {
161                         common_log_db_error($user, 'UPDATE', __FILE__);
162                         common_server_error(_('Couldn\'t remove Twitter user.'));
163                         return;
164                 }
165
166                 $this->show_form(_('Twitter account removed.'), TRUE);
167         }
168         
169         function save_preferences() {
170
171                 $user = common_current_user();          
172                 $fuser = Foreign_user::getForeignUser($user->id, 0);
173                 
174                 $this->show_form(_('Save doesn\'t do anything yet.'));
175                 
176                 return;
177         }
178
179         function get_twitter_id($twitter_username) {
180                 
181                 $uri = "http://twitter.com/users/show/$twitter_username.json";                  
182                 $data = $this->get_twitter_data($uri);
183                 
184                 if (!$data) {
185                         return NULL;
186                 }
187                 
188                 $user = json_decode($data);
189
190                 if (!$user) {
191                         return NULL;
192                 }
193                 
194                 return $user->id;
195         }
196
197         function verify_credentials($user, $password) {
198                 
199                 $uri = 'http://twitter.com/account/verify_credentials.json';
200                 $data = $this->get_twitter_data($uri, $user, $password);
201                 
202                 if (!$data) {
203                         return false;
204                 }
205                 
206                 $creds = json_decode($data); 
207                 
208                 if (!$creds) {
209                         return false;
210                 }
211                 
212                 if ($creds->authorized == 1) { 
213                         return true;
214                 }
215                 
216                 return false;           
217         }
218         
219         // PHP's cURL the best thing to use here? -- Zach
220         function get_twitter_data($uri, $user=NULL, $password=NULL) {
221                 $options = array(
222                                 CURLOPT_USERPWD => "$user:$password",
223                                 CURLOPT_RETURNTRANSFER  => true,
224                                 CURLOPT_FAILONERROR             => true,
225                                 CURLOPT_HEADER                  => false,
226                                 CURLOPT_FOLLOWLOCATION  => true,
227                                 // CURLOPT_USERAGENT            => "identi.ca",
228                                 CURLOPT_CONNECTTIMEOUT  => 120,
229                                 CURLOPT_TIMEOUT                 => 120
230                 );
231                 
232                 $ch = curl_init($uri);
233             curl_setopt_array($ch, $options);
234             $data = curl_exec($ch);
235             $errmsg = curl_error($ch);
236
237                 if ($errmsg) {
238                         common_debug("cURL error: $errmsg - trying to load: $uri with user $user.", __FILE__);
239                 }
240
241                 curl_close($ch);
242                 return $data;
243         }
244         
245
246 }