| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- <?php
- /**
- * Implements hook_menu().
- */
- function twitter_signin_menu() {
- $items['twitter/redirect'] = array(
- 'title' => 'Twitter Redirect',
- 'page callback' => 'twitter_signin_redirect',
- 'access callback' => TRUE,
- 'type' => MENU_CALLBACK,
- );
- $items['admin/config/services/twitter/signin'] = array(
- 'title' => 'Sign-in',
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('twitter_signin_admin_settings'),
- 'access arguments' => array('administer site configuration'),
- 'file' => 'twitter_signin.pages.inc',
- 'type' => MENU_LOCAL_TASK,
- 'weight' => 5,
- );
- return $items;
- }
- /**
- * Implements hook_block_info().
- */
- function twitter_signin_block_info() {
- $block[0]['info'] = t('Sign in with Twitter');
- return $block;
- }
- /**
- * Implements hook_block_view().
- */
- function twitter_signin_block_view($delta) {
- global $user;
- if (!$user->uid && _twitter_use_oauth()) {
- $block['subject'] = t('Sign in with Twitter');
- $block['content'] = twitter_signin_button();
- return $block;
- }
- }
- /**
- * returns an image link for signing in with twitter
- */
- function twitter_signin_button() {
- $img = drupal_get_path('module', 'twitter_signin') . '/images/' . variable_get('twitter_signin_button', 'Sign-in-with-Twitter-lighter-small.png');
- return l(theme('image', array('path' => $img, 'alt' => t('Sign in with Twitter'))), 'twitter/redirect', array('html' => TRUE));
- }
- /**
- * Submit handler for twitter signin
- */
- function twitter_signin_redirect() {
- module_load_include('lib.php', 'oauth');
- module_load_include('lib.php', 'twitter');
- module_load_include('inc', 'twitter');
- $key = variable_get('twitter_consumer_key', '');
- $secret = variable_get('twitter_consumer_secret', '');
- $twitter = new TwitterOAuth($key, $secret);
- $token = $twitter->get_request_token();
- $_SESSION['twitter_oauth']['token'] = $token;
- $_SESSION['twitter_oauth']['destination'] = $_SERVER['HTTP_REFERER'];
- $_SESSION['twitter_oauth']['signin'] = TRUE;
- drupal_goto($twitter->get_authenticate_url($token));
- }
- /**
- * Implements hook_form_alter().
- */
- function twitter_signin_form_alter(&$form, $form_state, $form_id) {
- // This only applies if we've got OAuth / signin enabled.
- if (!_twitter_use_oauth()) {
- return;
- }
- if ($form_id == 'twitter_oauth_callback_form' && isset($_SESSION['twitter_oauth']['signin'])) {
- $form['#submit'] = array_merge(array('twitter_signin_oauth_callback_submit'), $form['#submit']);
- }
- if ($form_id == 'user_login' || $form_id == 'user_login_block') {
- $items = array();
- $items[] = twitter_signin_button();
- $form['twitter_signin'] = array(
- '#markup' => theme('item_list', array('items' => $items)),
- );
- }
- elseif ($form_id == 'user_register_form' && isset($_SESSION['twitter']['values'])) {
- $form['account']['name']['#default_value'] = $_SESSION['twitter']['values']['screen_name'];
- $form['auth_twitter'] = array(
- '#type' => 'hidden',
- '#value' => $_SESSION['twitter']['values']['user_id'],
- );
- }
- }
- /**
- * Form submit for the OAuth callback. Here we add in sign-in specific handling
- */
- function twitter_signin_oauth_callback_submit(&$form, &$form_state) {
- global $user;
- $success = FALSE;
- $key = variable_get('twitter_consumer_key', '');
- $secret = variable_get('twitter_consumer_secret', '');
- $response = $form_state['twitter_oauth']['response'];
- $account = user_external_load($response['user_id']);
- if (isset($account->uid)) {
- user_external_login($account, $response);
- $success = TRUE;
- }
- elseif ($uid = db_query("SELECT uid FROM {twitter_account} WHERE twitter_uid = :twitter_uid", array(':twitter_uid' => $response['user_id']))->fetchField()) {
- // We have an existing twitter account - set it up for login
- $account = user_load($uid);
- $edit["authname_twitter"] = $response['user_id'];
- user_save($account, $edit);
- $user = $account;
- $form_state['twitter_oauth']['account'] = $account;
- $success = TRUE;
- }
- else {
- // No existing user account, let's see if we can register.
- if (variable_get('twitter_signin_register', 0)) {
- // Check for a nickname collision
- $account = array_shift(user_load_multiple(array(), array('name' => $response['screen_name'])));
- if (empty($account->uid)) {
- $edit = array(
- 'name' => $response['screen_name'],
- 'pass' => user_password(),
- 'init' => $response['screen_name'],
- 'status' => 1,
- "authname_twitter" => $response['user_id'],
- 'access' => REQUEST_TIME,
- );
- $account = user_save('', $edit);
- $user = $account;
- $form_state['twitter_oauth']['account'] = $account;
- $success = TRUE;
- }
- else {
- 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');
- }
- }
- else {
- drupal_set_message(t('Please log in or register to relate your Twitter account with a user.'));
- }
- }
- if (!$success) {
- $_SESSION['twitter']['values'] = $response;
- drupal_goto('user/register');
- }
- }
- /**
- * Implements hook_user_insert()
- *
- * Relates a Twitter account with a just created user account if the user
- * signed in with Twitter but did not have an account in the site yet
- */
- function twitter_signin_user_insert(&$edit, $account, $category) {
- _twitter_signin_add_account($account);
- }
- /**
- * Implements hook_user_insert()
- *
- * Relates a Twitter account with an existing user account if the user
- * signed in with Twitter
- */
- function twitter_signin_user_login(&$edit, $account) {
- _twitter_signin_add_account($account);
- }
- /**
- * Relates a user account with a twitter account
- *
- * @param $account
- * The Drupal user account
- */
- function _twitter_signin_add_account($account) {
- if (isset($_SESSION['twitter']['values'])) {
- module_load_include('lib.php', 'twitter');
- module_load_include('inc', 'twitter');
- $key = variable_get('twitter_consumer_key', '');
- $secret = variable_get('twitter_consumer_secret', '');
- $response = $_SESSION['twitter']['values'];
- $twitter = new TwitterOAuth($key, $secret, $response['oauth_token'], $response['oauth_token_secret']);
- $twitter_account = $twitter->users_show($response['screen_name']);
- $twitter_account->set_auth($response);
- twitter_account_save($twitter_account, TRUE, $account);
- unset($_SESSION['twitter']);
- drupal_set_message(t('You have related a Twitter account with your user. Next time you can sign in with Twitter.'));
- }
- }
|