You are here

function twitter_signin_oauth_callback_submit in Twitter 7.5

Same name and namespace in other branches
  1. 6.5 twitter_signin/twitter_signin.module \twitter_signin_oauth_callback_submit()
  2. 6.3 twitter_signin/twitter_signin.module \twitter_signin_oauth_callback_submit()
  3. 6.4 twitter_signin/twitter_signin.module \twitter_signin_oauth_callback_submit()
  4. 7.6 twitter_signin/twitter_signin.module \twitter_signin_oauth_callback_submit()
  5. 7.3 twitter_signin/twitter_signin.module \twitter_signin_oauth_callback_submit()
  6. 7.4 twitter_signin/twitter_signin.module \twitter_signin_oauth_callback_submit()

Form submit for the OAuth callback. Here we add in sign-in specific handling.

1 string reference to 'twitter_signin_oauth_callback_submit'
twitter_signin_form_alter in twitter_signin/twitter_signin.module
Implements hook_form_alter().

File

twitter_signin/twitter_signin.module, line 166
Hook implementations for Twitter Signin module.

Code

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 (!empty($account)) {
    $account = array(
      'uid' => $account->uid,
    );
    user_login_submit(array(), $account);
    $success = TRUE;
  }
  else {
    $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.
    if (!empty($uid)) {
      $account = user_load($uid);
      $edit["authname_twitter"] = $response['user_id'];
      user_save($account, $edit);
      $success = TRUE;
    }
    elseif (variable_get('twitter_signin_register', 0)) {

      // Check for a nickname collision.
      $accounts = user_load_multiple(array(), array(
        'name' => $response['screen_name'],
      ));
      $account = array_shift($accounts);
      if (empty($account->uid)) {

        // Generate a random password.
        require_once DRUPAL_ROOT . '/' . variable_get('password_inc', 'includes/password.inc');
        $password = user_password();

        // Create a new user account object.
        $account = new stdClass();
        $account->name = $response['screen_name'];
        $account->pass = user_hash_password($password);
        $account->init = $response['screen_name'];
        $account->status = 1;
        $account->timezone = variable_get('date_default_timezone', '');
        $account->authname_twitter = $response['user_id'];
        $account->access = REQUEST_TIME;
        $account->is_new = TRUE;

        // Try saving the new user account.
        try {
          $account = user_save($account);
        } catch (Exception $e) {
          watchdog('oauth_login', "Exception saving new account: " . $e
            ->getMessage(), '', WATCHDOG_WARNING);
          return FALSE;
        }

        // Reload the user's account object.
        $account = user_load($account->uid);
        $args = array(
          '!password' => $password,
          '!url' => url('user/' . $account->uid . '/edit'),
        );
        if (variable_get('twitter_signin_show_password', TRUE)) {
          drupal_set_message(t('You have been automatically registered with the password !password. Copy it to <a href="!url">set your account settings</a>.', $args));
        }
        $success = TRUE;
      }
      else {
        $args = array(
          '%name' => $response['screen_name'],
          '@login' => url('user/login'),
        );
        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.', $args), 'warning');
      }
    }
    else {
      drupal_set_message(t('Please log in or register to relate your Twitter account with a user.'));
    }
  }

  // If there was a problem, return to the login screen.
  if (!$success) {
    $_SESSION['twitter']['values'] = $response;
    drupal_goto('user/login');
  }
  else {

    // Log the user into their account.
    $user = $account;

    // Connect the Drupal user account and the Twitter account together.
    $form_state['twitter_oauth']['account'] = $account;

    // Finish the login process.
    $form_state['uid'] = $user->uid;
    user_login_finalize($form_state);
  }
}