]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/twittersettings.php
Twitter-integration - Twitter settings tab now saves Twitter credentials
[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 _('Enter your Twitter 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                 $user = common_current_user();
33                 $profile = $user->getProfile();
34                 
35                 $this->form_header(_('Twitter settings'), $msg, $success);
36
37                 common_element_start('form', array('method' => 'post',
38                                                                                    'id' => 'twittersettings',
39                                                                                    'action' =>
40                                                                                    common_local_url('twittersettings')));
41         
42                 common_input('twitter_username', _('Twitter Username'),
43                                          ($this->arg('twitter_username')) ? $this->arg('twitter_username') : $profile->nickname,
44                                          _('No spaces, please.')); // hey, it's what Twitter says
45                                         
46                 common_password('twitter_password', _('Twitter Password'));                     
47         
48                 // these checkboxes don't do anything yet
49                 
50                 common_checkbox('repost', _('Automatically send my notices to Twitter.'), true);
51                 common_checkbox('subscribe_friends', _('Subscribe to my Twitter friends here.'), true);
52                 
53                 common_submit('submit', _('Save'));
54                 common_element_end('form');
55                 common_show_footer();
56         }
57
58         function handle_post() {
59
60                 $twitter_username = $this->trimmed('twitter_username');
61                 $twitter_password = $this->trimmed('twitter_password');
62                 
63                 if (!Validate::string($twitter_username, array('min_length' => 1,
64                                                                                            'max_length' => 64,
65                                                                                            'format' => VALIDATE_NUM . VALIDATE_ALPHA_LOWER))) {
66                         $this->show_form(_('Username must have only lowercase letters and numbers and no spaces.'));
67                         return;
68                 }
69                 
70                 // Verify this is a real Twitter user.
71                 if (!$this->verify_credentials($twitter_username, $twitter_password)) {
72                         $this->show_form(_('Could not verify your Twitter credentials!'));
73                         return;
74                 }
75                 
76                 // Now that we have a valid Twitter user, we have to make another api call to 
77                 // find its Twitter ID.
78                 $twitter_id = $this->get_twitter_id($twitter_username);
79                 
80                 if (!$twitter_id) {
81                         $this->show_form(sprintf(_('Unable to retrieve account information for "%s" from Twitter.'), $twitter_username));
82                         return;
83                 }
84                         
85                 $user = common_current_user();
86                                 
87                 $fuser = Foreign_user::save(
88                                 array(
89                                         'id' => $twitter_id,
90                                         'service' => '0', // Twitter
91                                         'uri' => "http://www.twitter.com/$twitter_username",
92                                         'nickname' => $twitter_username, 
93                                         'user_id' => $user->id,
94                                         'credentials' => $twitter_password
95                                 ));
96                 
97                 if (!$fuser) {
98                         $this->show_form(_('Unable to save your Twitter credentials!'));
99                 }
100                 
101                 $this->show_form(_('Twitter settings saved.'), true);
102         }
103
104         function get_twitter_id($twitter_username) {
105                 
106                 $uri = "http://twitter.com/users/show/$twitter_username.json";
107                 
108                 common_debug("uri; $uri");
109                         
110                 $data = $this->get_twitter_data($uri);
111                 
112                 if (!$data) {
113                         return NULL;
114                 }
115                 
116                 $user = json_decode($data);
117
118                 if (!$user) {
119                         return NULL;
120                 }
121                 
122                 return $user->id;
123         }
124
125         function verify_credentials($user, $password) {
126                 
127                 $uri = 'http://twitter.com/account/verify_credentials.json';
128                 $data = $this->get_twitter_data($uri, $user, $password);
129                 
130                 if (!$data) {
131                         return false;
132                 }
133                 
134                 $creds = json_decode($data); 
135                 
136                 if (!$creds) {
137                         return false;
138                 }
139                 
140                 if ($creds->authorized == 1) { 
141                         return true;
142                 }
143                 
144                 return false;           
145         }
146         
147         // PHP's cURL the best thing to use here? -- Zach
148         function get_twitter_data($uri, $user=NULL, $password=NULL) {
149                 $options = array(
150                                 CURLOPT_USERPWD => "$user:$password",
151                                 CURLOPT_RETURNTRANSFER  => true,
152                                 CURLOPT_FAILONERROR             => true,
153                                 CURLOPT_HEADER                  => false,
154                                 CURLOPT_FOLLOWLOCATION  => true,
155                                 // CURLOPT_USERAGENT            => "identi.ca",
156                                 CURLOPT_CONNECTTIMEOUT  => 120,
157                                 CURLOPT_TIMEOUT                 => 120
158                 );
159                 
160                 $ch = curl_init($uri);
161             curl_setopt_array($ch, $options);
162             $data = curl_exec($ch);
163             $errmsg = curl_error($ch);
164
165                 if ($errmsg) {
166                         common_debug("cURL error: $errmsg - trying to load: $uri with user $user.", __FILE__);
167                 }
168
169                 curl_close($ch);
170                 return $data;
171         }
172         
173
174 }