You are here

function ulogin_callback in uLogin (advanced version) 7

Same name and namespace in other branches
  1. 6 ulogin.pages.inc \ulogin_callback()
1 string reference to 'ulogin_callback'
ulogin_menu in ./ulogin.module
Implements hook_menu().

File

./ulogin.pages.inc, line 2

Code

function ulogin_callback() {
  if (!empty($_POST['token']) || !empty($_GET['token'])) {
    $token = !empty($_POST['token']) ? $_POST['token'] : $_GET['token'];
    $data_raw = drupal_http_request('http://ulogin.ru/token.php?token=' . $token . '&host=' . $_SERVER['HTTP_HOST']);
    if (!empty($data_raw->error)) {
      watchdog('ulogin', print_r($data_raw, 1), array(), WATCHDOG_WARNING);
      drupal_set_message($data_raw->error, 'error');
      return MENU_ACCESS_DENIED;
    }
    $data = json_decode($data_raw->data, TRUE);

    //check for error
    if (!empty($data['error'])) {
      watchdog('ulogin', print_r($data, 1), array(), WATCHDOG_WARNING);
      drupal_set_message($data['error'], 'error');
      return MENU_ACCESS_DENIED;
    }

    //validate that returned data contains 'network' and 'uid' keys
    if (empty($data['network']) || empty($data['uid'])) {
      watchdog('ulogin', print_r($data, 1), array(), WATCHDOG_WARNING);
      drupal_set_message('something is wrong, try again later', 'error');
      return MENU_ACCESS_DENIED;
    }

    //remove 'access_token' property
    unset($data['access_token']);
  }
  else {
    drupal_set_message('no token given', 'error');
    return MENU_ACCESS_DENIED;
  }
  global $user;

  //user is already logged in, tries to add new identity
  if (user_is_logged_in()) {

    //identity is already registered
    if ($identity = _ulogin_identity_load($data)) {

      //registered to this user
      if ($user->uid == $identity['uid']) {
        drupal_set_message(t('You have already registered this identity.'));
        $destination = drupal_get_destination();
        drupal_goto($destination['destination']);
      }
      else {
        drupal_set_message(t('This identity is registered to another user.'), 'error');
        $destination = drupal_get_destination();
        drupal_goto($destination['destination']);
      }
    }
    else {
      _ulogin_identity_save($data);
      drupal_set_message(t('New identity added.'));

      //invoke ulogin_identity_added rules event
      if (module_exists('rules')) {
        rules_invoke_event('ulogin_identity_added', $user, $data);
      }
      $destination = drupal_get_destination();
      drupal_goto($destination['destination']);
    }
  }
  if ($identity = _ulogin_identity_load($data)) {

    //check if user is blocked
    if (_ulogin_user_is_blocked_by_uid($identity['uid'])) {
      drupal_set_message(t('Your account has not been activated or is blocked.'), 'error');
    }
    else {
      $form_state['uid'] = $identity['uid'];
      user_login_submit(array(), $form_state);
    }
  }
  elseif (variable_get('ulogin_duplicate_emails', 1) && !empty($data['email']) && ($account = user_load_by_mail($data['email']))) {
    drupal_set_message(t('You are trying to login with email address of another user.'), 'error');
    if (!empty($account->data['ulogin'])) {
      $providers = _ulogin_providers_list();
      drupal_set_message(t('If you are completely sure it is your email address, try to login through %network.', array(
        '%network' => $providers[$account->data['ulogin']['network']],
      )), 'status');
    }
    else {
      drupal_set_message(t('If you are completely sure it is your email address, try to login using your username and password on this site. If you don\'t remember your password - <a href="@password">request new password</a>.', array(
        '@password' => url('user/password'),
      )));
    }
  }
  else {
    global $ulogin_data;
    $ulogin_data = $data;
    user_external_login_register(_ulogin_make_username($data), 'ulogin');
    _ulogin_user_save($data);
  }
  $destination = drupal_get_destination();
  drupal_goto($destination['destination']);
}