You are here

function ulogin_callback in uLogin (advanced version) 6

Same name and namespace in other branches
  1. 7 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_substr(drupal_get_destination(), 12);
        drupal_goto($destination);
      }
      else {
        drupal_set_message(t('This identity is registered to another user.'), 'error');
        $destination = drupal_substr(drupal_get_destination(), 12);
        drupal_goto($destination);
      }
    }
    else {
      _ulogin_identity_save($data);
      drupal_set_message(t('New identity added.'));
      $destination = drupal_substr(drupal_get_destination(), 12);
      drupal_goto($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 {
      user_external_login(user_load($identity['uid']));
    }
  }
  elseif (variable_get('ulogin_duplicate_emails', 1) && !empty($data['email']) && ($account = user_load(array(
    'mail' => $data['email'],
  )))) {
    drupal_set_message(t('You are trying to login with email address of another user.'), 'error');
    if (!empty($account->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->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_identity_save($data);
    $user_save_trigger = FALSE;
    $edit = array();

    //save user picture
    if (variable_get('user_pictures', 0) && variable_get('ulogin_pictures', 1)) {
      $photo_url = '';
      if (!empty($data['photo_big']) && $data['photo_big'] != 'http://ulogin.ru/img/photo_big.png') {
        $photo_url = $data['photo_big'];
      }
      elseif (!empty($data['photo']) && $data['photo'] != 'http://ulogin.ru/img/photo.png') {
        $photo_url = $data['photo'];
      }
      if ($photo_url) {
        $photo = drupal_http_request($photo_url);
        $file = file_save_data($photo->data, file_directory_temp() . '/' . md5($photo_url), FILE_EXISTS_REPLACE);
        $info = image_get_info($file);
        $destination = variable_get('user_picture_path', 'pictures') . '/picture-' . $user->uid . '.' . $info['extension'];
        if (file_copy($file, $destination, FILE_EXISTS_REPLACE)) {
          $edit['picture'] = $file;
          $user_save_trigger = TRUE;
        }
      }
    }

    //set email address
    if (!empty($data['email'])) {
      $edit['mail'] = $data['email'];
      $user_save_trigger = TRUE;
    }
    if ($user_save_trigger) {
      user_save($user, $edit);
    }
  }
  $destination = drupal_substr(drupal_get_destination(), 12);
  drupal_goto($destination);
}