]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/twitter.php
ed12d359205e81aeaddcbf1e49a39d93bd3dbfda
[quix0rs-gnu-social.git] / lib / twitter.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2008, 2009, StatusNet, 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')) {
21     exit(1);
22 }
23
24 define('TWITTER_SERVICE', 1); // Twitter is foreign_service ID 1
25
26 function update_twitter_user($twitter_id, $screen_name)
27 {
28     $uri = 'http://twitter.com/' . $screen_name;
29     $fuser = new Foreign_user();
30
31     $fuser->query('BEGIN');
32
33     // Dropping down to SQL because regular DB_DataObject udpate stuff doesn't seem
34     // to work so good with tables that have multiple column primary keys
35
36     // Any time we update the uri for a forein user we have to make sure there
37     // are no dupe entries first -- unique constraint on the uri column
38
39     $qry = 'UPDATE foreign_user set uri = \'\' WHERE uri = ';
40     $qry .= '\'' . $uri . '\'' . ' AND service = ' . TWITTER_SERVICE;
41
42     $fuser->query($qry);
43
44     // Update the user
45
46     $qry = 'UPDATE foreign_user SET nickname = ';
47     $qry .= '\'' . $screen_name . '\'' . ', uri = \'' . $uri . '\' ';
48     $qry .= 'WHERE id = ' . $twitter_id . ' AND service = ' . TWITTER_SERVICE;
49
50     $fuser->query('COMMIT');
51
52     $fuser->free();
53     unset($fuser);
54
55     return true;
56 }
57
58 function add_twitter_user($twitter_id, $screen_name)
59 {
60
61     $new_uri = 'http://twitter.com/' . $screen_name;
62
63     // Clear out any bad old foreign_users with the new user's legit URL
64     // This can happen when users move around or fakester accounts get
65     // repoed, and things like that.
66
67     $luser = new Foreign_user();
68     $luser->uri = $new_uri;
69     $luser->service = TWITTER_SERVICE;
70     $result = $luser->delete();
71
72     if (empty($result)) {
73         common_log(LOG_WARNING,
74             "Twitter bridge - removed invalid Twitter user squatting on uri: $new_uri");
75     }
76
77     $luser->free();
78     unset($luser);
79
80     // Otherwise, create a new Twitter user
81
82     $fuser = new Foreign_user();
83
84     $fuser->nickname = $screen_name;
85     $fuser->uri = 'http://twitter.com/' . $screen_name;
86     $fuser->id = $twitter_id;
87     $fuser->service = TWITTER_SERVICE;
88     $fuser->created = common_sql_now();
89     $result = $fuser->insert();
90
91     if (empty($result)) {
92         common_log(LOG_WARNING,
93             "Twitter bridge - failed to add new Twitter user: $twitter_id - $screen_name.");
94         common_log_db_error($fuser, 'INSERT', __FILE__);
95     } else {
96         common_debug("Twitter bridge - Added new Twitter user: $screen_name ($twitter_id).");
97     }
98
99     return $result;
100 }
101
102 // Creates or Updates a Twitter user
103 function save_twitter_user($twitter_id, $screen_name)
104 {
105
106     // Check to see whether the Twitter user is already in the system,
107     // and update its screen name and uri if so.
108
109     $fuser = Foreign_user::getForeignUser($twitter_id, TWITTER_SERVICE);
110
111     if (!empty($fuser)) {
112
113         $result = true;
114
115         // Only update if Twitter screen name has changed
116
117         if ($fuser->nickname != $screen_name) {
118             $result = update_twitter_user($twitter_id, $screen_name);
119
120             common_debug('Twitter bridge - Updated nickname (and URI) for Twitter user ' .
121                 "$fuser->id to $screen_name, was $fuser->nickname");
122         }
123
124         return $result;
125
126     } else {
127         return add_twitter_user($twitter_id, $screen_name);
128     }
129
130     $fuser->free();
131     unset($fuser);
132
133     return true;
134 }
135
136 function is_twitter_bound($notice, $flink) {
137
138     // Check to see if notice should go to Twitter
139     if (!empty($flink) && ($flink->noticesync & FOREIGN_NOTICE_SEND)) {
140
141         // If it's not a Twitter-style reply, or if the user WANTS to send replies.
142         if (!preg_match('/^@[a-zA-Z0-9_]{1,15}\b/u', $notice->content) ||
143             ($flink->noticesync & FOREIGN_NOTICE_SEND_REPLY)) {
144             return true;
145         }
146     }
147
148     return false;
149 }
150
151 function broadcast_twitter($notice)
152 {
153     $flink = Foreign_link::getByUserID($notice->profile_id,
154                                        TWITTER_SERVICE);
155
156     if (is_twitter_bound($notice, $flink)) {
157
158         $user = $flink->getUser();
159
160         // XXX: Hack to get around PHP cURL's use of @ being a a meta character
161         $statustxt = preg_replace('/^@/', ' @', $notice->content);
162
163         $token = TwitterOAuthClient::unpackToken($flink->credentials);
164
165         $client = new TwitterOAuthClient($token->key, $token->secret);
166
167         $status = null;
168
169         try {
170             $status = $client->statusesUpdate($statustxt);
171         } catch (OAuthClientCurlException $e) {
172
173             if ($e->getMessage() == 'The requested URL returned error: 401') {
174
175                 $errmsg = sprintf('User %1$s (user id: %2$s) has an invalid ' .
176                                   'Twitter OAuth access token.',
177                                   $user->nickname, $user->id);
178                 common_log(LOG_WARNING, $errmsg);
179
180                 // Bad auth token! We need to delete the foreign_link
181                 // to Twitter and inform the user.
182
183                 remove_twitter_link($flink);
184                 return true;
185
186             } else {
187
188                 // Some other error happened, so we should probably
189                 // try to send again later.
190
191                 $errmsg = sprintf('cURL error trying to send notice to Twitter ' .
192                                   'for user %1$s (user id: %2$s) - ' .
193                                   'code: %3$s message: $4$s.',
194                                   $user->nickname, $user->id,
195                                   $e->getCode(), $e->getMessage());
196                 common_log(LOG_WARNING, $errmsg);
197
198                 return false;
199             }
200         }
201
202         if (empty($status)) {
203
204             // This could represent a failure posting,
205             // or the Twitter API might just be behaving flakey.
206
207             $errmsg = sprint('No data returned by Twitter API when ' .
208                              'trying to send update for %1$s (user id %2$s).',
209                              $user->nickname, $user->id);
210             common_log(LOG_WARNING, $errmsg);
211
212             return false;
213         }
214
215         // Notice crossed the great divide
216
217         $msg = sprintf('Twitter bridge posted notice %s to Twitter.',
218                        $notice->id);
219         common_log(LOG_INFO, $msg);
220     }
221
222     return true;
223 }
224
225 function remove_twitter_link($flink)
226 {
227     $user = $flink->getUser();
228
229     common_log(LOG_INFO, 'Removing Twitter bridge Foreign link for ' .
230         "user $user->nickname (user id: $user->id).");
231
232     $result = $flink->delete();
233
234     if (empty($result)) {
235         common_log(LOG_ERR, 'Could not remove Twitter bridge ' .
236                    "Foreign_link for $user->nickname (user id: $user->id)!");
237         common_log_db_error($flink, 'DELETE', __FILE__);
238     }
239
240     // Notify the user that her Twitter bridge is down
241
242     if (isset($user->email)) {
243
244         $result = mail_twitter_bridge_removed($user);
245
246         if (!$result) {
247
248             $msg = 'Unable to send email to notify ' .
249               "$user->nickname (user id: $user->id) " .
250               'that their Twitter bridge link was ' .
251               'removed!';
252
253             common_log(LOG_WARNING, $msg);
254         }
255     }
256
257 }