]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/twitter.php
Merge branch '0.7.x' into 0.8.x
[quix0rs-gnu-social.git] / lib / twitter.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 define('TWITTER_SERVICE', 1); // Twitter is foreign_service ID 1
23
24 function get_twitter_data($uri, $screen_name, $password)
25 {
26
27     $options = array(
28             CURLOPT_USERPWD => sprintf("%s:%s", $screen_name, $password),
29             CURLOPT_RETURNTRANSFER    => true,
30             CURLOPT_FAILONERROR        => true,
31             CURLOPT_HEADER            => false,
32             CURLOPT_FOLLOWLOCATION    => true,
33             CURLOPT_USERAGENT      => "Laconica",
34             CURLOPT_CONNECTTIMEOUT    => 120,
35             CURLOPT_TIMEOUT            => 120,
36             # Twitter is strict about accepting invalid "Expect" headers
37             CURLOPT_HTTPHEADER => array('Expect:')
38     );
39
40     $ch = curl_init($uri);
41     curl_setopt_array($ch, $options);
42     $data = curl_exec($ch);
43     $errmsg = curl_error($ch);
44
45     if ($errmsg) {
46         common_debug("Twitter bridge - cURL error: $errmsg - trying to load: $uri with user $screen_name.",
47             __FILE__);
48
49         if (defined('SCRIPT_DEBUG')) {
50             print "cURL error: $errmsg - trying to load: $uri with user $screen_name.\n";
51         }
52     }
53
54     curl_close($ch);
55
56     return $data;
57 }
58
59 function twitter_json_data($uri, $screen_name, $password)
60 {
61     $json_data = get_twitter_data($uri, $screen_name, $password);
62
63     if (!$json_data) {
64         return false;
65     }
66
67     $data = json_decode($json_data);
68
69     if (!$data) {
70         return false;
71     }
72
73     return $data;
74 }
75
76 function twitter_user_info($screen_name, $password)
77 {
78     $uri = "http://twitter.com/users/show/$screen_name.json";
79     return twitter_json_data($uri, $screen_name, $password);
80 }
81
82 function twitter_friends_ids($screen_name, $password)
83 {
84     $uri = "http://twitter.com/friends/ids/$screen_name.json";
85     return twitter_json_data($uri, $screen_name, $password);
86 }
87
88 function update_twitter_user($twitter_id, $screen_name)
89 {
90     $uri = 'http://twitter.com/' . $screen_name;
91
92     $fuser = new Foreign_user();
93
94     $fuser->query('BEGIN');
95
96     // Dropping down to SQL because regular db_object udpate stuff doesn't seem
97     // to work so good with tables that have multiple column primary keys
98
99     // Any time we update the uri for a forein user we have to make sure there
100     // are no dupe entries first -- unique constraint on the uri column
101
102     $qry = 'UPDATE foreign_user set uri = \'\' WHERE uri = ';
103     $qry .= '\'' . $uri . '\'' . ' AND service = ' . TWITTER_SERVICE;
104
105     $result = $fuser->query($qry);
106
107     if ($result) {
108         common_debug("Removed uri ($uri) from another foreign_user who was squatting on it.");
109         if (defined('SCRIPT_DEBUG')) {
110             print("Removed uri ($uri) from another Twitter user who was squatting on it.\n");
111         }
112     }
113
114     // Update the user
115     $qry = 'UPDATE foreign_user SET nickname = ';
116     $qry .= '\'' . $screen_name . '\'' . ', uri = \'' . $uri . '\' ';
117     $qry .= 'WHERE id = ' . $twitter_id . ' AND service = ' . TWITTER_SERVICE;
118
119     $result = $fuser->query($qry);
120
121     if (!$result) {
122         common_log(LOG_WARNING,
123             "Couldn't update foreign_user data for Twitter user: $screen_name");
124         common_log_db_error($fuser, 'UPDATE', __FILE__);
125         if (defined('SCRIPT_DEBUG')) {
126             print "UPDATE failed: for Twitter user:  $twitter_id - $screen_name. - ";
127             print common_log_objstring($fuser) . "\n";
128             $error = &PEAR::getStaticProperty('DB_DataObject','lastError');
129             print "DB_DataObject Error: " . $error->getMessage() . "\n";
130         }
131         return false;
132     }
133
134     $fuser->query('COMMIT');
135
136     $fuser->free();
137     unset($fuser);
138
139     return true;
140 }
141
142 function add_twitter_user($twitter_id, $screen_name)
143 {
144
145     $new_uri = 'http://twitter.com/' . $screen_name;
146
147     // Clear out any bad old foreign_users with the new user's legit URL
148     // This can happen when users move around or fakester accounts get
149     // repoed, and things like that.
150     $luser = new Foreign_user();
151     $luser->uri = $new_uri;
152     $luser->service = TWITTER_SERVICE;
153     $result = $luser->delete();
154
155     if ($result) {
156         common_log(LOG_WARNING,
157             "Twitter bridge - removed invalid Twitter user squatting on uri: $new_uri");
158         if (defined('SCRIPT_DEBUG')) {
159             print "Removed invalid Twitter user squatting on uri: $new_uri\n";
160         }
161     }
162
163     $luser->free();
164     unset($luser);
165
166     // Otherwise, create a new Twitter user
167     $fuser = new Foreign_user();
168
169     $fuser->nickname = $screen_name;
170     $fuser->uri = 'http://twitter.com/' . $screen_name;
171     $fuser->id = $twitter_id;
172     $fuser->service = TWITTER_SERVICE;
173     $fuser->created = common_sql_now();
174     $result = $fuser->insert();
175
176     if (!$result) {
177         common_log(LOG_WARNING,
178             "Twitter bridge - failed to add new Twitter user: $twitter_id - $screen_name.");
179         common_log_db_error($fuser, 'INSERT', __FILE__);
180         if (defined('SCRIPT_DEBUG')) {
181             print "INSERT failed: could not add new Twitter user: $twitter_id - $screen_name. - ";
182             print common_log_objstring($fuser) . "\n";
183             $error = &PEAR::getStaticProperty('DB_DataObject','lastError');
184             print "DB_DataObject Error: " . $error->getMessage() . "\n";
185         }
186     } else {
187         common_debug("Twitter bridge - Added new Twitter user: $screen_name ($twitter_id).");
188         if (defined('SCRIPT_DEBUG')) {
189             print "Added new Twitter user: $screen_name ($twitter_id).\n";
190         }
191     }
192
193     return $result;
194 }
195
196 // Creates or Updates a Twitter user
197 function save_twitter_user($twitter_id, $screen_name)
198 {
199
200     // Check to see whether the Twitter user is already in the system,
201     // and update its screen name and uri if so.
202     $fuser = Foreign_user::getForeignUser($twitter_id, TWITTER_SERVICE);
203
204     if ($fuser) {
205
206         $result = true;
207
208         // Only update if Twitter screen name has changed
209         if ($fuser->nickname != $screen_name) {
210             $result = update_twitter_user($twitter_id, $screen_name);
211
212             common_debug('Twitter bridge - Updated nickname (and URI) for Twitter user ' .
213                 "$fuser->id to $screen_name, was $fuser->nickname");
214
215             if (defined('SCRIPT_DEBUG')) {
216                 print 'Updated nickname (and URI) for Twitter user ' .
217                     "$fuser->id to $screen_name, was $fuser->nickname\n";
218             }
219         }
220
221         return $result;
222
223     } else {
224         return add_twitter_user($twitter_id, $screen_name);
225     }
226
227     $fuser->free();
228     unset($fuser);
229
230     return true;
231 }
232
233 function retreive_twitter_friends($twitter_id, $screen_name, $password)
234 {
235     $friends = array();
236
237     $uri = "http://twitter.com/statuses/friends/$twitter_id.json?page=";
238     $friends_ids = twitter_friends_ids($screen_name, $password);
239
240     if (!$friends_ids) {
241         return $friends;
242     }
243
244     if (defined('SCRIPT_DEBUG')) {
245         print "Twitter 'social graph' ids method says $screen_name has " .
246             count($friends_ids) . " friends.\n";
247     }
248
249     // Calculate how many pages to get...
250     $pages = ceil(count($friends_ids) / 100);
251
252     if ($pages == 0) {
253         common_log(LOG_WARNING,
254             "Twitter bridge - $screen_name seems to have no friends.");
255         if (defined('SCRIPT_DEBUG')) {
256             print "$screen_name seems to have no friends.\n";
257         }
258     }
259
260     for ($i = 1; $i <= $pages; $i++) {
261
262         $data = get_twitter_data($uri . $i, $screen_name, $password);
263
264         if (!$data) {
265             common_log(LOG_WARNING,
266                 "Twitter bridge - Couldn't retrieve page $i of $screen_name's friends.");
267             if (defined('SCRIPT_DEBUG')) {
268                 print "Couldn't retrieve page $i of $screen_name's friends.\n";
269             }
270             continue;
271         }
272
273         $more_friends = json_decode($data);
274
275         if (!$more_friends) {
276
277             common_log(LOG_WARNING,
278                 "Twitter bridge - No data for page $i of $screen_name's friends.");
279             if (defined('SCRIPT_DEBUG')) {
280                 print "No data for page $i of $screen_name's friends.\n";
281             }
282             continue;
283         }
284
285          $friends = array_merge($friends, $more_friends);
286     }
287
288     return $friends;
289 }
290
291 function save_twitter_friends($user, $twitter_id, $screen_name, $password)
292 {
293
294     $friends = retreive_twitter_friends($twitter_id, $screen_name, $password);
295
296     if (empty($friends)) {
297         common_debug("Twitter bridge - Couldn't get friends data from Twitter for $screen_name.");
298         if (defined('SCRIPT_DEBUG')) {
299             print "Couldn't get friends data from Twitter for $screen_name.\n";
300         }
301         return false;
302     }
303
304     foreach ($friends as $friend) {
305
306         $friend_name = $friend->screen_name;
307         $friend_id = (int) $friend->id;
308
309         // Update or create the Foreign_user record
310         if (!save_twitter_user($friend_id, $friend_name)) {
311             common_log(LOG_WARNING,
312                 "Twitter bridge - couldn't save $screen_name's friend, $friend_name.");
313             if (defined('SCRIPT_DEBUG')) {
314                 print "Couldn't save $screen_name's friend, $friend_name.\n";
315             }
316             continue;
317         }
318
319         // Check to see if there's a related local user
320         $flink = Foreign_link::getByForeignID($friend_id, 1);
321
322         if ($flink) {
323
324             // Get associated user and subscribe her
325             $friend_user = User::staticGet('id', $flink->user_id);
326             if (!empty($friend_user)) {
327                 $result = subs_subscribe_to($user, $friend_user);
328
329                 if ($result === true) {
330                     common_debug("Twitter bridge - subscribed $friend_user->nickname to $user->nickname.");
331                     if (defined('SCRIPT_DEBUG')) {
332                         print("Subscribed $friend_user->nickname to $user->nickname.\n");
333                     }
334                 } else {
335                     if (defined('SCRIPT_DEBUG')) {
336                         print "$result ($friend_user->nickname to $user->nickname)\n";
337                     }
338                 }
339             }
340         }
341     }
342
343     return true;
344 }
345
346 function is_twitter_bound($notice, $flink) {
347
348     // Check to see if notice should go to Twitter
349     if (!empty($flink) && ($flink->noticesync & FOREIGN_NOTICE_SEND)) {
350
351         // If it's not a Twitter-style reply, or if the user WANTS to send replies.
352         if (!preg_match('/^@[a-zA-Z0-9_]{1,15}\b/u', $notice->content) ||
353             ($flink->noticesync & FOREIGN_NOTICE_SEND_REPLY)) {
354                 return true;
355         }
356     }
357
358     return false;
359 }
360
361 function broadcast_twitter($notice)
362 {
363     $success = true;
364
365     $flink = Foreign_link::getByUserID($notice->profile_id,
366         TWITTER_SERVICE);
367
368     // XXX: Not sure WHERE to check whether a notice should go to
369     // Twitter. Should we even put in the queue if it shouldn't? --Zach
370     if (!is_null($flink) && is_twitter_bound($notice, $flink)) {
371
372         $fuser = $flink->getForeignUser();
373         $twitter_user = $fuser->nickname;
374         $twitter_password = $flink->credentials;
375         $uri = 'http://www.twitter.com/statuses/update.json';
376
377         // XXX: Hack to get around PHP cURL's use of @ being a a meta character
378         $statustxt = preg_replace('/^@/', ' @', $notice->content);
379
380         $options = array(
381             CURLOPT_USERPWD        => "$twitter_user:$twitter_password",
382             CURLOPT_POST           => true,
383             CURLOPT_POSTFIELDS     =>
384                 array(
385                         'status' => $statustxt,
386                         'source' => common_config('integration', 'source')
387                      ),
388             CURLOPT_RETURNTRANSFER => true,
389             CURLOPT_FAILONERROR    => true,
390             CURLOPT_HEADER         => false,
391             CURLOPT_FOLLOWLOCATION => true,
392             CURLOPT_USERAGENT      => "Laconica",
393             CURLOPT_CONNECTTIMEOUT => 120,  // XXX: How long should this be?
394             CURLOPT_TIMEOUT        => 120,
395
396             # Twitter is strict about accepting invalid "Expect" headers
397             CURLOPT_HTTPHEADER => array('Expect:')
398             );
399
400         $ch = curl_init($uri);
401         curl_setopt_array($ch, $options);
402         $data = curl_exec($ch);
403         $errmsg = curl_error($ch);
404
405         if ($errmsg) {
406             common_debug("cURL error: $errmsg - " .
407                 "trying to send notice for $twitter_user.",
408                          __FILE__);
409             $success = false;
410         }
411
412         curl_close($ch);
413
414         if (!$data) {
415             common_debug("No data returned by Twitter's " .
416                 "API trying to send update for $twitter_user",
417                          __FILE__);
418             $success = false;
419         }
420
421         // Twitter should return a status
422         $status = json_decode($data);
423
424         if (!$status->id) {
425             common_debug("Unexpected data returned by Twitter " .
426                 " API trying to send update for $twitter_user",
427                          __FILE__);
428             $success = false;
429         }
430     }
431
432     return $success;
433 }