twitter.php 16 KB

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