twitter.module 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. /**
  3. * @file
  4. * Provides API integration with the Twitter microblogging service.
  5. */
  6. /**
  7. * Implements hook_menu().
  8. */
  9. function twitter_menu() {
  10. $items['twitter/oauth'] = array(
  11. 'title' => 'Twitter',
  12. 'access callback' => TRUE,
  13. 'page callback' => 'drupal_get_form',
  14. 'page arguments' => array('twitter_oauth_callback'),
  15. 'type' => MENU_CALLBACK,
  16. 'file' => 'twitter.pages.inc',
  17. );
  18. $items['admin/config/services/twitter'] = array(
  19. 'title' => 'Twitter',
  20. 'description' => 'Configure integration with Twitter (and compatible) API services.',
  21. 'page callback' => 'drupal_get_form',
  22. 'page arguments' => array('twitter_admin_form'),
  23. 'access arguments' => array('administer site configuration'),
  24. 'file' => 'twitter.pages.inc',
  25. );
  26. $items['admin/config/services/twitter/default'] = array(
  27. 'title' => 'Twitter',
  28. 'type' => MENU_DEFAULT_LOCAL_TASK,
  29. );
  30. $items['user/%user_category/edit/twitter'] = array(
  31. 'title' => 'Twitter accounts',
  32. 'page callback' => 'twitter_user_settings',
  33. 'page arguments' => array(1),
  34. 'access callback' => 'twitter_edit_access',
  35. 'access arguments' => array(1),
  36. 'load arguments' => array('%map', '%index'),
  37. 'weight' => 10,
  38. 'file' => 'twitter.pages.inc',
  39. 'type' => MENU_LOCAL_TASK,
  40. );
  41. $items['user/%user/edit/twitter/global/%'] = array(
  42. 'title' => 'Twitter accounts',
  43. 'page callback' => 'drupal_get_form',
  44. 'page arguments' => array('twitter_user_make_global', 1, 5),
  45. 'access arguments' => array('make twitter accounts global'),
  46. 'file' => 'twitter.pages.inc',
  47. );
  48. return $items;
  49. }
  50. /**
  51. * Access callback for twitter account editing.
  52. */
  53. function twitter_edit_access($account) {
  54. return user_edit_access($account) && user_access('add twitter accounts');
  55. }
  56. /**
  57. * Implements hook_permission().
  58. */
  59. function twitter_permission() {
  60. return array(
  61. 'add twitter accounts' => array(
  62. 'title' => t('Add Twitter accounts'),
  63. ),
  64. 'use global twitter account' => array(
  65. 'title' => t('Use the site global Twitter account'),
  66. ),
  67. 'make twitter accounts global' => array(
  68. 'title' => t('Assign a Twitter account as the site global account.'),
  69. ),
  70. 'import own tweets' => array(
  71. 'title' => t('Import own tweets to the site.'),
  72. ),
  73. );
  74. }
  75. /**
  76. * Implements hook_user_categories().
  77. */
  78. function twitter_user_categories() {
  79. return array(
  80. array(
  81. 'name' => 'twitter',
  82. 'title' => t('Twitter accounts'),
  83. 'weight' => 3,
  84. ),
  85. );
  86. }
  87. /**
  88. * Implements hook_theme().
  89. */
  90. function twitter_theme() {
  91. return array(
  92. 'twitter_account_list_form' => array(
  93. 'render element' => 'form',
  94. ),
  95. );
  96. }
  97. /**
  98. * Very lightweight helper function to generate a TinyURL for a given post.
  99. */
  100. function twitter_shorten_url($url) {
  101. if (module_exists('shorten')) {
  102. return shorten_url($url);
  103. }
  104. else {
  105. $conf = TwitterConf::instance();
  106. $response = drupal_http_request("http://" . $conf->get('tiny_url') . "/api-create.php?url=" . $url);
  107. if ($response->code == 200) {
  108. return $response->data;
  109. }
  110. else {
  111. return $url;
  112. }
  113. }
  114. }
  115. /**
  116. * Implements hook_cron().
  117. *
  118. * Imports new Twitter statuses for site users, and deletes expired tweets.
  119. */
  120. function twitter_cron() {
  121. if (!variable_get('twitter_import', TRUE)) {
  122. return;
  123. }
  124. // Pull up a list of Twitter accounts that are flagged for updating,
  125. // sorted by how long it's been since we last updated them. This ensures
  126. // that the most out-of-date accounts get updated first.
  127. module_load_include('inc', 'twitter');
  128. $result = db_query_range("SELECT twitter_uid FROM {twitter_account} WHERE import = :import ORDER BY last_refresh ASC", 0, 20, array(':import' => 1));
  129. foreach ($result as $account) {
  130. twitter_fetch_user_timeline($account->twitter_uid);
  131. }
  132. // Nuke old statuses.
  133. if ($age = variable_get('twitter_expire', 0)) {
  134. db_delete('twitter')
  135. ->condition('created_time', REQUEST_TIME - $age, '<')
  136. ->execute();
  137. }
  138. }
  139. /**
  140. * Implements hook_filter_info().
  141. */
  142. function twitter_filter_info() {
  143. $filters['twitter_username'] = array(
  144. 'title' => t('Twitter @username converter'),
  145. 'description' => t('Converts Twitter-style @usernames into links to Twitter account pages.'),
  146. 'process callback' => '_twitter_filter_username',
  147. 'tips callback' => '_twitter_filter_tip_username',
  148. );
  149. $filters['twitter_hashtag'] = array(
  150. 'title' => t('Twitter #hashtag converter'),
  151. 'description' => t('Converts Twitter-style #hashtags into links to hashtags.org.'),
  152. 'process callback' => '_twitter_filter_hashtag',
  153. 'tips callback' => '_twitter_filter_tip_hashtag',
  154. );
  155. return $filters;
  156. }
  157. /**
  158. * Filter tips callback function for $filters[0] in hook_filter_info().
  159. */
  160. function _twitter_filter_tip_username($filter, $format, $long = FALSE) {
  161. return t('Twitter-style @usernames are linked to their Twitter account pages.');
  162. }
  163. /**
  164. * Filter tips callback function for $filters[1] in hook_filter_info().
  165. */
  166. function _twitter_filter_tip_hashtag($format, $long = FALSE) {
  167. return t('Twitter-style #hashtags are linked to !url.', array(
  168. '!url' => '<a href="http://search.twitter.com/">search.twitter.com</a>')
  169. );
  170. }
  171. /**
  172. * Callback for twitter @username converter
  173. */
  174. function _twitter_filter_username($text, $filter) {
  175. $prefix = '@';
  176. $conf = TwitterConf::instance();
  177. $destination = 'http://' . $conf->get('host') . '/';
  178. return _twitter_filter_text($text, $prefix, $destination);
  179. }
  180. /**
  181. * Callback for twitter #hashtag converter
  182. */
  183. function _twitter_filter_hashtag($text, $filter) {
  184. $prefix = '#';
  185. $conf = TwitterConf::instance();
  186. $destination = 'http://' . $conf->get('search') . '/search?q=%23';
  187. return _twitter_filter_text($text, $prefix, $destination);
  188. }
  189. /**
  190. * This helper function converts Twitter-style @usernames and #hashtags into
  191. * actual links.
  192. */
  193. function _twitter_filter_text($text, $prefix, $destination) {
  194. $matches = array(
  195. '/\>' . $prefix . '([a-z0-9_]+)/i',
  196. '/^' . $prefix . '([a-z0-9_]+)/i',
  197. '/(\s+)' . $prefix . '([a-z0-9_]+)/i',
  198. );
  199. $replacements = array(
  200. '><a href="' . $destination . '/${1}">' . $prefix . '${1}</a>',
  201. '<a href="' . $destination . '/${1}">' . $prefix . '${1}</a>',
  202. '${1}<a href="' . $destination . '/${2}">' . $prefix . '${2}</a>',
  203. );
  204. return preg_replace($matches, $replacements, $text);
  205. }
  206. /**
  207. * Implements hook_user_load().
  208. */
  209. function twitter_user_load($accounts) {
  210. foreach ($accounts as $uid => $account) {
  211. $accounts[$uid]->twitter_accounts = module_invoke_all('twitter_accounts', $account);
  212. }
  213. }
  214. /**
  215. * An implementation of hook_twitter_accounts. We want to move this into a
  216. * separate module eventually, but sticking the code here and using a hook
  217. * lets other modules solve the 'what accounts can a user post with' problem
  218. * in cleaner ways.
  219. *
  220. * @return
  221. * array with Twitter accounts
  222. */
  223. function twitter_twitter_accounts($account) {
  224. module_load_include('inc', 'twitter');
  225. $query = db_select('twitter_account', 'ta')
  226. ->fields('ta', array('twitter_uid'));
  227. if (user_access('use global twitter account', $account)) {
  228. $or = db_or();
  229. $or->condition('ta.uid', $account->uid);
  230. $or->condition('ta.is_global', 1);
  231. $query->condition($or);
  232. }
  233. else {
  234. $query->condition('ta.uid', $account->uid);
  235. }
  236. $twitter_accounts = array();
  237. foreach ($query->execute()->fetchCol() as $twitter_uid) {
  238. $twitter_accounts[] = twitter_account_load($twitter_uid);
  239. }
  240. return $twitter_accounts;
  241. }
  242. /**
  243. * Detect whether we should use oauth. This can probably just go now :)
  244. */
  245. function _twitter_use_oauth() {
  246. return module_exists('oauth_common') && variable_get('twitter_consumer_key', '') &&
  247. variable_get('twitter_consumer_secret', '');
  248. }
  249. /**
  250. * Implements hook_views_api().
  251. */
  252. function twitter_views_api() {
  253. return array('api' => 2);
  254. }
  255. /**
  256. * Implements hook_admin_paths_alter().
  257. *
  258. * OAuth callback disagrees with overlay.
  259. */
  260. function twitter_admin_paths_alter(&$paths) {
  261. $paths['user/*/edit/twitter'] = FALSE;
  262. }