twitter_signin.module 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. /**
  3. * Implements hook_menu().
  4. */
  5. function twitter_signin_menu() {
  6. $items['twitter/redirect'] = array(
  7. 'title' => 'Twitter Redirect',
  8. 'page callback' => 'twitter_signin_redirect',
  9. 'access callback' => TRUE,
  10. 'type' => MENU_CALLBACK,
  11. );
  12. $items['admin/config/services/twitter/signin'] = array(
  13. 'title' => 'Sign-in',
  14. 'page callback' => 'drupal_get_form',
  15. 'page arguments' => array('twitter_signin_admin_settings'),
  16. 'access arguments' => array('administer site configuration'),
  17. 'file' => 'twitter_signin.pages.inc',
  18. 'type' => MENU_LOCAL_TASK,
  19. 'weight' => 5,
  20. );
  21. return $items;
  22. }
  23. /**
  24. * Implements hook_block_info().
  25. */
  26. function twitter_signin_block_info() {
  27. $block[0]['info'] = t('Sign in with Twitter');
  28. return $block;
  29. }
  30. /**
  31. * Implements hook_block_view().
  32. */
  33. function twitter_signin_block_view($delta) {
  34. global $user;
  35. if (!$user->uid && _twitter_use_oauth()) {
  36. $block['subject'] = t('Sign in with Twitter');
  37. $block['content'] = twitter_signin_button();
  38. return $block;
  39. }
  40. }
  41. /**
  42. * returns an image link for signing in with twitter
  43. */
  44. function twitter_signin_button() {
  45. $img = drupal_get_path('module', 'twitter_signin') . '/images/' . variable_get('twitter_signin_button', 'Sign-in-with-Twitter-lighter-small.png');
  46. return l(theme('image', array('path' => $img, 'alt' => t('Sign in with Twitter'))), 'twitter/redirect', array('html' => TRUE));
  47. }
  48. /**
  49. * Submit handler for twitter signin
  50. */
  51. function twitter_signin_redirect() {
  52. module_load_include('lib.php', 'oauth');
  53. module_load_include('lib.php', 'twitter');
  54. module_load_include('inc', 'twitter');
  55. $key = variable_get('twitter_consumer_key', '');
  56. $secret = variable_get('twitter_consumer_secret', '');
  57. $twitter = new TwitterOAuth($key, $secret);
  58. $token = $twitter->get_request_token();
  59. $_SESSION['twitter_oauth']['token'] = $token;
  60. $_SESSION['twitter_oauth']['destination'] = $_SERVER['HTTP_REFERER'];
  61. $_SESSION['twitter_oauth']['signin'] = TRUE;
  62. drupal_goto($twitter->get_authenticate_url($token));
  63. }
  64. /**
  65. * Implements hook_form_alter().
  66. */
  67. function twitter_signin_form_alter(&$form, $form_state, $form_id) {
  68. // This only applies if we've got OAuth / signin enabled.
  69. if (!_twitter_use_oauth()) {
  70. return;
  71. }
  72. if ($form_id == 'twitter_oauth_callback_form' && isset($_SESSION['twitter_oauth']['signin'])) {
  73. $form['#submit'] = array_merge(array('twitter_signin_oauth_callback_submit'), $form['#submit']);
  74. }
  75. if ($form_id == 'user_login' || $form_id == 'user_login_block') {
  76. $items = array();
  77. $items[] = twitter_signin_button();
  78. $form['twitter_signin'] = array(
  79. '#markup' => theme('item_list', array('items' => $items)),
  80. );
  81. }
  82. elseif ($form_id == 'user_register_form' && isset($_SESSION['twitter']['values'])) {
  83. $form['account']['name']['#default_value'] = $_SESSION['twitter']['values']['screen_name'];
  84. $form['auth_twitter'] = array(
  85. '#type' => 'hidden',
  86. '#value' => $_SESSION['twitter']['values']['user_id'],
  87. );
  88. }
  89. }
  90. /**
  91. * Form submit for the OAuth callback. Here we add in sign-in specific handling
  92. */
  93. function twitter_signin_oauth_callback_submit(&$form, &$form_state) {
  94. global $user;
  95. $success = FALSE;
  96. $key = variable_get('twitter_consumer_key', '');
  97. $secret = variable_get('twitter_consumer_secret', '');
  98. $response = $form_state['twitter_oauth']['response'];
  99. $account = user_external_load($response['user_id']);
  100. if (isset($account->uid)) {
  101. user_external_login($account, $response);
  102. $success = TRUE;
  103. }
  104. elseif ($uid = db_query("SELECT uid FROM {twitter_account} WHERE twitter_uid = :twitter_uid", array(':twitter_uid' => $response['user_id']))->fetchField()) {
  105. // We have an existing twitter account - set it up for login
  106. $account = user_load($uid);
  107. $edit["authname_twitter"] = $response['user_id'];
  108. user_save($account, $edit);
  109. $user = $account;
  110. $form_state['twitter_oauth']['account'] = $account;
  111. $success = TRUE;
  112. }
  113. else {
  114. // No existing user account, let's see if we can register.
  115. if (variable_get('twitter_signin_register', 0)) {
  116. // Check for a nickname collision
  117. $account = array_shift(user_load_multiple(array(), array('name' => $response['screen_name'])));
  118. if (empty($account->uid)) {
  119. $edit = array(
  120. 'name' => $response['screen_name'],
  121. 'pass' => user_password(),
  122. 'init' => $response['screen_name'],
  123. 'status' => 1,
  124. "authname_twitter" => $response['user_id'],
  125. 'access' => REQUEST_TIME,
  126. );
  127. $account = user_save('', $edit);
  128. $user = $account;
  129. $form_state['twitter_oauth']['account'] = $account;
  130. $success = TRUE;
  131. }
  132. else {
  133. drupal_set_message(t('The nickname %name is already in use on this site, please register below with a new nick name, or @login to continue.', array('%name' => $response['screen_name'], '@login' => url('user/login'))), 'warning');
  134. }
  135. }
  136. else {
  137. drupal_set_message(t('Please log in or register to relate your Twitter account with a user.'));
  138. }
  139. }
  140. if (!$success) {
  141. $_SESSION['twitter']['values'] = $response;
  142. drupal_goto('user/register');
  143. }
  144. }
  145. /**
  146. * Implements hook_user_insert()
  147. *
  148. * Relates a Twitter account with a just created user account if the user
  149. * signed in with Twitter but did not have an account in the site yet
  150. */
  151. function twitter_signin_user_insert(&$edit, $account, $category) {
  152. _twitter_signin_add_account($account);
  153. }
  154. /**
  155. * Implements hook_user_insert()
  156. *
  157. * Relates a Twitter account with an existing user account if the user
  158. * signed in with Twitter
  159. */
  160. function twitter_signin_user_login(&$edit, $account) {
  161. _twitter_signin_add_account($account);
  162. }
  163. /**
  164. * Relates a user account with a twitter account
  165. *
  166. * @param $account
  167. * The Drupal user account
  168. */
  169. function _twitter_signin_add_account($account) {
  170. if (isset($_SESSION['twitter']['values'])) {
  171. module_load_include('lib.php', 'twitter');
  172. module_load_include('inc', 'twitter');
  173. $key = variable_get('twitter_consumer_key', '');
  174. $secret = variable_get('twitter_consumer_secret', '');
  175. $response = $_SESSION['twitter']['values'];
  176. $twitter = new TwitterOAuth($key, $secret, $response['oauth_token'], $response['oauth_token_secret']);
  177. $twitter_account = $twitter->users_show($response['screen_name']);
  178. $twitter_account->set_auth($response);
  179. twitter_account_save($twitter_account, TRUE, $account);
  180. unset($_SESSION['twitter']);
  181. drupal_set_message(t('You have related a Twitter account with your user. Next time you can sign in with Twitter.'));
  182. }
  183. }