twitter.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. <?php
  2. /*
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2008-2011 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. if (!defined('STATUSNET') && !defined('LACONICA')) {
  20. exit(1);
  21. }
  22. define('TWITTER_SERVICE', 1); // Twitter is foreign_service ID 1
  23. function add_twitter_user($twitter_id, $screen_name)
  24. {
  25. // Clear out any bad old foreign_users with the new user's legit URL
  26. // This can happen when users move around or fakester accounts get
  27. // repoed, and things like that.
  28. try {
  29. $fuser = Foreign_user::getForeignUser($twitter_id, TWITTER_SERVICE);
  30. $result = $fuser->delete();
  31. if ($result != false) {
  32. common_log(
  33. LOG_INFO,
  34. "Twitter bridge - removed old Twitter user: $screen_name ($twitter_id)."
  35. );
  36. }
  37. } catch (NoResultException $e) {
  38. // no old foreign users exist for this id
  39. }
  40. $fuser = new Foreign_user();
  41. $fuser->nickname = $screen_name;
  42. $fuser->uri = 'http://twitter.com/' . $screen_name;
  43. $fuser->id = $twitter_id;
  44. $fuser->service = TWITTER_SERVICE;
  45. $fuser->created = common_sql_now();
  46. $result = $fuser->insert();
  47. if ($result === false) {
  48. common_log(LOG_WARNING, "Twitter bridge - failed to add new Twitter user: $twitter_id - $screen_name.");
  49. common_log_db_error($fuser, 'INSERT', __FILE__);
  50. } else {
  51. common_log(LOG_INFO,
  52. "Twitter bridge - Added new Twitter user: $screen_name ($twitter_id).");
  53. }
  54. return $result;
  55. }
  56. // Creates or Updates a Twitter user
  57. function save_twitter_user($twitter_id, $screen_name)
  58. {
  59. // Check to see whether the Twitter user is already in the system,
  60. // and update its screen name and uri if so.
  61. try {
  62. $fuser = Foreign_user::getForeignUser($twitter_id, TWITTER_SERVICE);
  63. // Delete old record if Twitter user changed screen name
  64. if ($fuser->nickname != $screen_name) {
  65. $oldname = $fuser->nickname;
  66. $fuser->delete();
  67. common_log(LOG_INFO, sprintf('Twitter bridge - Updated nickname (and URI) ' .
  68. 'for Twitter user %1$d - %2$s, was %3$s.',
  69. $fuser->id,
  70. $screen_name,
  71. $oldname));
  72. }
  73. } catch (NoResultException $e) {
  74. // No old users exist for this id
  75. // Kill any old, invalid records for this screen name
  76. // XXX: Is this really only supposed to be run if the above getForeignUser fails?
  77. try {
  78. $fuser = Foreign_user::getByNickname($screen_name, TWITTER_SERVICE);
  79. $fuser->delete();
  80. common_log(
  81. LOG_INFO,
  82. sprintf(
  83. 'Twitter bridge - deteted old record for Twitter ' .
  84. 'screen name "%s" belonging to Twitter ID %d.',
  85. $screen_name,
  86. $fuser->id
  87. )
  88. );
  89. } catch (NoResultException $e) {
  90. // No old users exist for this screen_name
  91. }
  92. }
  93. return add_twitter_user($twitter_id, $screen_name);
  94. }
  95. function is_twitter_bound($notice, $flink) {
  96. // Don't send activity activities (at least for now)
  97. if ($notice->object_type == ActivityObject::ACTIVITY) {
  98. return false;
  99. }
  100. $allowedVerbs = array(ActivityVerb::POST);
  101. // Default behavior: always send repeats
  102. if (empty($flink))
  103. array_push($allowedVerbs, ActivityVerb::SHARE);
  104. // Otherwise, check to see if repeats are allowed
  105. else if (($flink->noticesync & FOREIGN_NOTICE_SEND_REPEAT) == FOREIGN_NOTICE_SEND_REPEAT)
  106. array_push($allowedVerbs, ActivityVerb::SHARE);
  107. // Don't send things that aren't posts or repeats (at least for now)
  108. if (!in_array($notice->verb, $allowedVerbs)) {
  109. return false;
  110. }
  111. // Check to see if notice should go to Twitter
  112. if (!empty($flink) && (($flink->noticesync & FOREIGN_NOTICE_SEND) == FOREIGN_NOTICE_SEND)) {
  113. // If it's not a Twitter-style reply, or if the user WANTS to send replies,
  114. // or if it's in reply to a twitter notice
  115. if ( (($flink->noticesync & FOREIGN_NOTICE_SEND_REPLY) == FOREIGN_NOTICE_SEND_REPLY) ||
  116. is_twitter_notice($notice->reply_to) || is_twitter_notice($notice->repeat_of) ||
  117. (empty($notice->reply_to) && !preg_match('/^@[a-zA-Z0-9_]{1,15}\b/u', $notice->content)) ){
  118. return true;
  119. }
  120. }
  121. return false;
  122. }
  123. function is_twitter_notice($id)
  124. {
  125. $n2s = Notice_to_status::getKV('notice_id', $id);
  126. return (!empty($n2s));
  127. }
  128. /**
  129. * Pull the formatted status ID number from a Twitter status object
  130. * returned via JSON from Twitter API.
  131. *
  132. * Encapsulates checking for the id_str attribute, which is required
  133. * to read 64-bit "Snowflake" ID numbers on a 32-bit system -- the
  134. * integer id attribute gets corrupted into a double-precision float,
  135. * losing a few digits of precision.
  136. *
  137. * Warning: avoid performing arithmetic or direct comparisons with
  138. * this number, as it may get coerced back to a double on 32-bit.
  139. *
  140. * @param object $status
  141. * @param string $field base field name if not 'id'
  142. * @return mixed id number as int or string
  143. */
  144. function twitter_id($status, $field='id')
  145. {
  146. $field_str = "{$field}_str";
  147. if (isset($status->$field_str)) {
  148. // String version of the id -- required on 32-bit systems
  149. // since the 64-bit numbers get corrupted as ints.
  150. return $status->$field_str;
  151. } else {
  152. return $status->$field;
  153. }
  154. }
  155. /**
  156. * Check if we need to broadcast a notice over the Twitter bridge, and
  157. * do so if necessary. Will determine whether to do a straight post or
  158. * a repeat/retweet
  159. *
  160. * This function is meant to be called directly from TwitterQueueHandler.
  161. *
  162. * @param Notice $notice
  163. * @return boolean true if complete or successful, false if we should retry
  164. */
  165. function broadcast_twitter($notice)
  166. {
  167. try {
  168. $flink = Foreign_link::getByUserID($notice->profile_id, TWITTER_SERVICE);
  169. } catch (NoResultException $e) {
  170. // Alright so don't broadcast it then! (since there's no foreign link)
  171. return true;
  172. }
  173. // Don't bother with basic auth, since it's no longer allowed
  174. if (TwitterOAuthClient::isPackedToken($flink->credentials)) {
  175. if (is_twitter_bound($notice, $flink)) {
  176. if (!empty($notice->repeat_of) && is_twitter_notice($notice->repeat_of)) {
  177. $retweet = retweet_notice($flink, Notice::getKV('id', $notice->repeat_of));
  178. if (is_object($retweet)) {
  179. Notice_to_status::saveNew($notice->id, twitter_id($retweet));
  180. return true;
  181. } else {
  182. // Our error processing will have decided if we need to requeue
  183. // this or can discard safely.
  184. return $retweet;
  185. }
  186. } else {
  187. return broadcast_oauth($notice, $flink);
  188. }
  189. }
  190. }
  191. return true;
  192. }
  193. /**
  194. * Send a retweet to Twitter for a notice that has been previously bridged
  195. * in or out.
  196. *
  197. * Warning: the return value is not guaranteed to be an object; some error
  198. * conditions will return a 'true' which should be passed on to a calling
  199. * queue handler.
  200. *
  201. * No local information about the resulting retweet is saved: it's up to
  202. * caller to save new mappings etc if appropriate.
  203. *
  204. * @param Foreign_link $flink
  205. * @param Notice $notice
  206. * @return mixed object with resulting Twitter status data on success, or true/false/null on error conditions.
  207. */
  208. function retweet_notice($flink, $notice)
  209. {
  210. $token = TwitterOAuthClient::unpackToken($flink->credentials);
  211. $client = new TwitterOAuthClient($token->key, $token->secret);
  212. $id = twitter_status_id($notice);
  213. if (empty($id)) {
  214. common_log(LOG_WARNING, "Trying to retweet notice {$notice->id} with no known status id.");
  215. return null;
  216. }
  217. try {
  218. $status = $client->statusesRetweet($id);
  219. return $status;
  220. } catch (OAuthClientException $e) {
  221. return process_error($e, $flink, $notice);
  222. }
  223. }
  224. function twitter_status_id($notice)
  225. {
  226. $n2s = Notice_to_status::getKV('notice_id', $notice->id);
  227. if (empty($n2s)) {
  228. return null;
  229. } else {
  230. return $n2s->status_id;
  231. }
  232. }
  233. /**
  234. * Pull any extra information from a notice that we should transfer over
  235. * to Twitter beyond the notice text itself.
  236. *
  237. * @param Notice $notice
  238. * @return array of key-value pairs for Twitter update submission
  239. * @access private
  240. */
  241. function twitter_update_params($notice)
  242. {
  243. $params = array();
  244. if ($notice->lat || $notice->lon) {
  245. $params['lat'] = $notice->lat;
  246. $params['long'] = $notice->lon;
  247. }
  248. if (!empty($notice->reply_to) && is_twitter_notice($notice->reply_to)) {
  249. $reply = Notice::getKV('id', $notice->reply_to);
  250. $params['in_reply_to_status_id'] = twitter_status_id($reply);
  251. }
  252. return $params;
  253. }
  254. function broadcast_oauth($notice, Foreign_link $flink) {
  255. try {
  256. $user = $flink->getUser();
  257. } catch (ServerException $e) {
  258. common_log(LOG_WARNING, 'Discarding broadcast_oauth for notice '.$notice->id.' because of exception: '.$e->getMessage());
  259. return true;
  260. }
  261. $statustxt = format_status($notice);
  262. $params = twitter_update_params($notice);
  263. $token = TwitterOAuthClient::unpackToken($flink->credentials);
  264. $client = new TwitterOAuthClient($token->key, $token->secret);
  265. $status = null;
  266. try {
  267. $status = $client->statusesUpdate($statustxt, $params);
  268. if (!empty($status)) {
  269. Notice_to_status::saveNew($notice->id, twitter_id($status));
  270. }
  271. } catch (OAuthClientException $e) {
  272. return process_error($e, $flink, $notice);
  273. }
  274. if (empty($status)) {
  275. // This could represent a failure posting,
  276. // or the Twitter API might just be behaving flakey.
  277. $errmsg = sprintf('Twitter bridge - No data returned by Twitter API when ' .
  278. 'trying to post notice %d for User %s (user id %d).',
  279. $notice->id,
  280. $user->nickname,
  281. $user->id);
  282. common_log(LOG_WARNING, $errmsg);
  283. return false;
  284. }
  285. // Notice crossed the great divide
  286. $msg = sprintf('Twitter bridge - posted notice %d to Twitter using ' .
  287. 'OAuth for User %s (user id %d).',
  288. $notice->id,
  289. $user->nickname,
  290. $user->id);
  291. common_log(LOG_INFO, $msg);
  292. return true;
  293. }
  294. function process_error($e, $flink, $notice)
  295. {
  296. $user = $flink->getUser();
  297. $code = $e->getCode();
  298. $logmsg = sprintf('Twitter bridge - %d posting notice %d for ' .
  299. 'User %s (user id: %d): %s.',
  300. $code,
  301. $notice->id,
  302. $user->nickname,
  303. $user->id,
  304. $e->getMessage());
  305. common_log(LOG_WARNING, $logmsg);
  306. // http://dev.twitter.com/pages/responses_errors
  307. switch($code) {
  308. case 400:
  309. // Probably invalid data (bad Unicode chars or coords) that
  310. // cannot be resolved by just sending again.
  311. //
  312. // It could also be rate limiting, but retrying immediately
  313. // won't help much with that, so we'll discard for now.
  314. // If a facility for retrying things later comes up in future,
  315. // we can detect the rate-limiting headers and use that.
  316. //
  317. // Discard the message permanently.
  318. return true;
  319. break;
  320. case 401:
  321. // Probably a revoked or otherwise bad access token - nuke!
  322. remove_twitter_link($flink);
  323. return true;
  324. break;
  325. case 403:
  326. // User has exceeder her rate limit -- toss the notice
  327. return true;
  328. break;
  329. case 404:
  330. // Resource not found. Shouldn't happen much on posting,
  331. // but just in case!
  332. //
  333. // Consider it a matter for tossing the notice.
  334. return true;
  335. break;
  336. default:
  337. // For every other case, it's probably some flakiness so try
  338. // sending the notice again later (requeue).
  339. return false;
  340. break;
  341. }
  342. }
  343. function format_status($notice)
  344. {
  345. // Start with the plaintext source of this notice...
  346. $statustxt = $notice->content;
  347. // Convert !groups to #hashes
  348. // XXX: Make this an optional setting?
  349. $statustxt = preg_replace('/(^|\s)!([A-Za-z0-9]{1,64})/', "\\1#\\2", $statustxt);
  350. // detect links, each link uses 23 characters on twitter
  351. $numberOfLinks = preg_match_all('`((http|https|ftp)://[^\s<]+[^\s<\.)])`i', $statustxt);
  352. $statusWithoutLinks = preg_replace('`((http|https|ftp)://[^\s<]+[^\s<\.)])`i', '', $statustxt);
  353. $statusLength = mb_strlen($statusWithoutLinks) + $numberOfLinks * 23;
  354. // Twitter still has a 140-char hardcoded max.
  355. if ($statusLength > 140) {
  356. $noticeUrl = common_shorten_url($notice->getUrl());
  357. // each link uses 23 chars on twitter + 3 for the ' … ' => 26
  358. $statustxt = mb_substr($statustxt, 0, 140 - 26) . ' … ' . $noticeUrl;
  359. }
  360. return $statustxt;
  361. }
  362. function remove_twitter_link($flink)
  363. {
  364. $user = $flink->getUser();
  365. common_log(LOG_INFO, 'Removing Twitter bridge Foreign link for ' .
  366. "user $user->nickname (user id: $user->id).");
  367. $result = $flink->safeDelete();
  368. if (empty($result)) {
  369. common_log(LOG_ERR, 'Could not remove Twitter bridge ' .
  370. "Foreign_link for $user->nickname (user id: $user->id)!");
  371. common_log_db_error($flink, 'DELETE', __FILE__);
  372. }
  373. // Notify the user that her Twitter bridge is down
  374. if (isset($user->email)) {
  375. $result = mail_twitter_bridge_removed($user);
  376. if (!$result) {
  377. $msg = 'Unable to send email to notify ' .
  378. "$user->nickname (user id: $user->id) " .
  379. 'that their Twitter bridge link was ' .
  380. 'removed!';
  381. common_log(LOG_WARNING, $msg);
  382. }
  383. }
  384. }
  385. /**
  386. * Send a mail message to notify a user that her Twitter bridge link
  387. * has stopped working, and therefore has been removed. This can
  388. * happen when the user changes her Twitter password, or otherwise
  389. * revokes access.
  390. *
  391. * @param User $user user whose Twitter bridge link has been removed
  392. *
  393. * @return boolean success flag
  394. */
  395. function mail_twitter_bridge_removed($user)
  396. {
  397. $profile = $user->getProfile();
  398. common_switch_locale($user->language);
  399. // TRANS: Mail subject after forwarding notices to Twitter has stopped working.
  400. $subject = sprintf(_m('Your Twitter bridge has been disabled'));
  401. $site_name = common_config('site', 'name');
  402. // TRANS: Mail body after forwarding notices to Twitter has stopped working.
  403. // TRANS: %1$ is the name of the user the mail is sent to, %2$s is a URL to the
  404. // TRANS: Twitter settings, %3$s is the StatusNet sitename.
  405. $body = sprintf(_m('Hi, %1$s. We\'re sorry to inform you that your ' .
  406. 'link to Twitter has been disabled. We no longer seem to have ' .
  407. 'permission to update your Twitter status. Did you maybe revoke ' .
  408. '%3$s\'s access?' . "\n\n" .
  409. 'You can re-enable your Twitter bridge by visiting your ' .
  410. "Twitter settings page:\n\n\t%2\$s\n\n" .
  411. "Regards,\n%3\$s"),
  412. $profile->getBestName(),
  413. common_local_url('twittersettings'),
  414. common_config('site', 'name'));
  415. common_switch_locale();
  416. return mail_to_user($user, $subject, $body);
  417. }