twitter.inc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * Twitter API functions
  4. */
  5. /**
  6. * Connect to the API using the 'proper' version (Oauth vs. standard)
  7. */
  8. function twitter_connect($account) {
  9. $auth = $account->get_auth();
  10. if (_twitter_use_oauth() && $auth['oauth_token'] && $auth['oauth_token_secret']) {
  11. module_load_include('lib.php', 'oauth_common');
  12. return new TwitterOAuth(variable_get('twitter_consumer_key', ''), variable_get('twitter_consumer_secret', ''),
  13. $auth['oauth_token'], $auth['oauth_token_secret']);
  14. }
  15. else if ($auth['password']) {
  16. return new Twitter($account->screen_name, $auth['password']);
  17. }
  18. else {
  19. return new Twitter;
  20. }
  21. }
  22. /**
  23. * Saves a TwitterUser object to {twitter_account}
  24. */
  25. function twitter_account_save($twitter_user, $save_auth = FALSE, $account = NULL) {
  26. $values = (array) $twitter_user;
  27. $values['twitter_uid'] = $values['id'];
  28. // bool => int for DB storage
  29. foreach (array('protected', 'verified', 'profile_background_tile') as $k) {
  30. if (isset($values[$k])) {
  31. $values[$k] = (int) $values[$k];
  32. }
  33. }
  34. if ($save_auth) {
  35. $values += $twitter_user->get_auth();
  36. if (empty($account)) {
  37. global $user;
  38. $account = $user;
  39. }
  40. $values['uid'] = $account->uid;
  41. }
  42. $schema = drupal_get_schema('twitter_account');
  43. foreach ($values as $k => $v) {
  44. if (!isset($schema['fields'][$k])) {
  45. unset($values[$k]);
  46. }
  47. }
  48. db_merge('twitter_account')
  49. ->key(array('twitter_uid' => $values['twitter_uid']))
  50. ->fields($values)
  51. ->execute();
  52. }
  53. /**
  54. * Load a Twitter account from {twitter_account}.
  55. *
  56. * @param $id
  57. * Twitter UID
  58. *
  59. * @return
  60. * TwitterUser object
  61. *
  62. */
  63. function twitter_account_load($id) {
  64. if ( $values = db_query("SELECT * FROM {twitter_account} WHERE twitter_uid = :twitter_uid", array(':twitter_uid' => $id))->fetchAssoc() ) {
  65. $values['id'] = $values['twitter_uid'];
  66. $account = new TwitterUser($values);
  67. $account->set_auth($values);
  68. $account->uid = $values['uid'];
  69. $account->import = $values['import'];
  70. $account->is_global = $values['is_global'];
  71. return $account;
  72. }
  73. }
  74. /**
  75. * Saves a TwitterStatus object to {twitter}
  76. */
  77. function twitter_status_save($status) {
  78. $status = array(
  79. 'twitter_id' => $status->id,
  80. 'screen_name' => $status->user->screen_name,
  81. 'created_time' => strtotime($status->created_at),
  82. 'text' => $status->text,
  83. 'source' => $status->source,
  84. 'in_reply_to_status_id' => ($status->in_reply_to_status_id > 0) ? (string) $status->in_reply_to_status_id : NULL,
  85. 'in_reply_to_user_id' => (int) $status->in_reply_to_user_id,
  86. 'in_reply_to_screen_name' => $status->in_reply_to_screen_name,
  87. 'truncated' => (int) $status->truncated,
  88. );
  89. db_merge('twitter')
  90. ->key(array('twitter_id' => $status['twitter_id']))
  91. ->fields($status)
  92. ->execute();
  93. }
  94. /**
  95. * Post a message to twitter
  96. */
  97. function twitter_set_status($twitter_account, $status) {
  98. $twitter = twitter_connect($twitter_account);
  99. $twitter->status_update($status);
  100. }
  101. /**
  102. * Fetches a user's timeline
  103. */
  104. function twitter_fetch_user_timeline($id) {
  105. $account = twitter_account_load($id);
  106. $since = db_query("SELECT MAX(twitter_id) FROM {twitter} WHERE screen_name = :screen_name", array(':screen_name' => $account->screen_name))->fetchField();
  107. $twitter = twitter_connect($account);
  108. $params = array();
  109. if ($since) {
  110. $params['since_id'] = $since;
  111. }
  112. $statuses = $twitter->user_timeline($account->id, $params, $account->protected);
  113. foreach ($statuses as $status) {
  114. twitter_status_save($status);
  115. }
  116. if (count($statuses) > 0) {
  117. twitter_account_save($statuses[0]->user);
  118. }
  119. db_update('twitter_account')
  120. ->fields(array(
  121. 'last_refresh' => REQUEST_TIME,
  122. ))
  123. ->condition('twitter_uid', $account->id)
  124. ->execute();
  125. }
  126. /**
  127. * Delete a twitter account and its statuses.
  128. *
  129. * @param $twitter_uid
  130. * An integer with the Twitter UID.
  131. *
  132. * @param $screen_name
  133. * Optional string with the user name.
  134. */
  135. function twitter_account_delete($twitter_uid) {
  136. $account = twitter_account_load($twitter_uid);
  137. // Delete from {twitter_account}.
  138. $query = db_delete('twitter_account');
  139. $query->conditions('twitter_uid', $twitter_uid);
  140. $query->execute();
  141. // Delete from {twitter}.
  142. $query = db_delete('twitter');
  143. $query->conditions('screen_name', $account->screen_name);
  144. $query->execute();
  145. // Delete from {twitter_account}.
  146. $query = db_delete('authmap');
  147. $query->conditions('authname', $twitter_uid);
  148. $query->conditions('module', 'twitter');
  149. $query->execute();
  150. }