start.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /**
  3. * Elgg Twitter Service
  4. * This service plugin allows users to authenticate their Elgg account with Twitter.
  5. *
  6. * @package TwitterAPI
  7. */
  8. elgg_register_event_handler('init', 'system', 'twitter_api_init');
  9. function twitter_api_init() {
  10. // require libraries
  11. $base = elgg_get_plugins_path() . 'twitter_api';
  12. elgg_register_class('TwitterOAuth', "$base/vendors/twitteroauth/twitterOAuth.php");
  13. elgg_register_library('twitter_api', "$base/lib/twitter_api.php");
  14. elgg_load_library('twitter_api');
  15. // extend site views
  16. //elgg_extend_view('metatags', 'twitter_api/metatags');
  17. elgg_extend_view('css/elgg', 'twitter_api/css');
  18. elgg_extend_view('css/admin', 'twitter_api/css');
  19. elgg_extend_view('js/elgg', 'twitter_api/js');
  20. // sign on with twitter
  21. if (twitter_api_allow_sign_on_with_twitter()) {
  22. elgg_extend_view('login/extend', 'twitter_api/login');
  23. }
  24. // register page handler
  25. elgg_register_page_handler('twitter_api', 'twitter_api_pagehandler');
  26. // backward compatibility
  27. elgg_register_page_handler('twitterservice', 'twitter_api_pagehandler_deprecated');
  28. // register Walled Garden public pages
  29. elgg_register_plugin_hook_handler('public_pages', 'walled_garden', 'twitter_api_public_pages');
  30. // push wire post messages to twitter
  31. if (elgg_get_plugin_setting('wire_posts', 'twitter_api') == 'yes') {
  32. elgg_register_plugin_hook_handler('status', 'user', 'twitter_api_tweet');
  33. }
  34. $actions = dirname(__FILE__) . '/actions/twitter_api';
  35. elgg_register_action('twitter_api/interstitial_settings', "$actions/interstitial_settings.php", 'logged_in');
  36. }
  37. /**
  38. * Handles old pg/twitterservice/ handler
  39. *
  40. * @param array $page
  41. * @return bool
  42. */
  43. function twitter_api_pagehandler_deprecated($page) {
  44. $url = elgg_get_site_url() . 'pg/twitter_api/authorize';
  45. $msg = elgg_echo('twitter_api:deprecated_callback_url', array($url));
  46. register_error($msg);
  47. return twitter_api_pagehandler($page);
  48. }
  49. /**
  50. * Serves pages for twitter.
  51. *
  52. * @param array $page
  53. * @return bool
  54. */
  55. function twitter_api_pagehandler($page) {
  56. if (!isset($page[0])) {
  57. return false;
  58. }
  59. switch ($page[0]) {
  60. case 'authorize':
  61. twitter_api_authorize();
  62. break;
  63. case 'revoke':
  64. twitter_api_revoke();
  65. break;
  66. case 'forward':
  67. twitter_api_forward();
  68. break;
  69. case 'login':
  70. twitter_api_login();
  71. break;
  72. case 'interstitial':
  73. elgg_gatekeeper();
  74. // only let twitter users do this.
  75. $guid = elgg_get_logged_in_user_guid();
  76. $twitter_name = elgg_get_plugin_user_setting('twitter_name', $guid, 'twitter_api');
  77. if (!$twitter_name) {
  78. register_error(elgg_echo('twitter_api:invalid_page'));
  79. forward();
  80. }
  81. $pages = dirname(__FILE__) . '/pages/twitter_api';
  82. include "$pages/interstitial.php";
  83. break;
  84. default:
  85. return false;
  86. }
  87. return true;
  88. }
  89. /**
  90. * Push a status update to twitter.
  91. *
  92. * @param string $hook
  93. * @param string $type
  94. * @param null $returnvalue
  95. * @param array $params
  96. */
  97. function twitter_api_tweet($hook, $type, $returnvalue, $params) {
  98. if (!$params['user'] instanceof ElggUser) {
  99. return;
  100. }
  101. // @todo - allow admin to select origins?
  102. // check user settings
  103. $user_guid = $params['user']->getGUID();
  104. $access_key = elgg_get_plugin_user_setting('access_key', $user_guid, 'twitter_api');
  105. $access_secret = elgg_get_plugin_user_setting('access_secret', $user_guid, 'twitter_api');
  106. if (!($access_key && $access_secret)) {
  107. return;
  108. }
  109. $api = twitter_api_get_api_object($access_key, $access_secret);
  110. if (!$api) {
  111. return;
  112. }
  113. $api->post('statuses/update', array('status' => $params['message']));
  114. }
  115. /**
  116. * Get tweets for a user.
  117. *
  118. * @param int $user_guid The Elgg user GUID
  119. * @param array $options
  120. * @return array
  121. */
  122. function twitter_api_fetch_tweets($user_guid, $options = array()) {
  123. // check user settings
  124. $access_key = elgg_get_plugin_user_setting('access_key', $user_guid, 'twitter_api');
  125. $access_secret = elgg_get_plugin_user_setting('access_secret', $user_guid, 'twitter_api');
  126. if (!($access_key && $access_secret)) {
  127. return FALSE;
  128. }
  129. $api = twitter_api_get_api_object($access_key, $access_secret);
  130. if (!$api) {
  131. return FALSE;
  132. }
  133. return $api->get('statuses/user_timeline', $options);
  134. }
  135. /**
  136. * Register as public pages for walled garden.
  137. *
  138. * @param string $hook
  139. * @param string $type
  140. * @param array $return_value
  141. * @param array $params
  142. * @return array
  143. */
  144. function twitter_api_public_pages($hook, $type, $return_value, $params) {
  145. $return_value[] = 'twitter_api/forward';
  146. $return_value[] = 'twitter_api/login';
  147. return $return_value;
  148. }