You are here

function urllogin_link_page in urllogin 6

Same name and namespace in other branches
  1. 7 urllogin.inc \urllogin_link_page()

This is the function that actually performs the login.

Parameters

$urlstring: login string from URL

The function first validates the URL login string. If good, then the user is logged in and transferred to the destination page. Otherwise they are taken to the front page. Results, good or bad, are logged with watchdog. If the intended user is already logged in, then redirect will occur even if link is outdated.

1 string reference to 'urllogin_link_page'
urllogin_menu in ./urllogin.module
Implements hook_menu().

File

./urllogin.inc, line 239

Code

function urllogin_link_page($urlstring = 'none') {
  $urlstr = check_plain($urlstring);

  // sanitize
  $resultmsg = "";
  global $user;
  $uid = urllogin_decode($urlstr, variable_get('urllogin_codekey', 20110531), variable_get('urllogin_codemin', 20110531), urllogin_passphrase(), $resultmsg, $user->uid);
  if ($uid > -1) {
    $account = _urllogin_testuid($uid, $resultmsg);
  }
  else {
    $account = NULL;
  }

  //watchdog('urllogin', $resultmsg, array(), WATCHDOG_DEBUG);
  if ($account != NULL) {

    // find where to go: get rid of first two arguments and use the rest of the URL as the destination
    $args = arg();
    unset($args[0]);
    unset($args[1]);
    $goto = implode('/', $args);

    // maintain the original query string
    $query = $_GET;
    unset($query['q']);
    if (count($query) > 0) {
      $goto .= '?' . implode('&', $query);
    }

    // Check in case this user is already logged in
    $logged_in = $user->uid == $account->uid;
    if ($logged_in) {
      $resultmsg = t('User %username (%uid) was already logged in. Redirected to: %goto', array(
        '%username' => $account->name,
        '%uid' => $uid,
        '%goto' => $goto,
      ));
      watchdog('urllogin', $resultmsg, array(), WATCHDOG_NOTICE);
    }
    else {
      $logged_in = user_external_login($account);
      if ($logged_in) {
        $resultmsg = t('Logging in as %username (%uid). Redirected to: %goto', array(
          '%username' => $account->name,
          '%uid' => $uid,
          '%goto' => $goto,
        ));
        watchdog('urllogin', $resultmsg, array(), WATCHDOG_INFO);

        // if persistent_login is installed, then set "remember me"
        if (module_exists('persistent_login')) {
          _persistent_login_create_cookie($account);
        }
      }
      else {
        $resultmsg = t('Failed login as %username (%uid)', array(
          '%username' => $account->name,
          '%uid' => $uid,
        ));
      }
    }
    if ($logged_in) {
      drupal_goto(implode('/', $args), $query);
    }
  }
  watchdog('urllogin', $resultmsg, array(), WATCHDOG_WARNING);
  if ($uid == -2) {
    return '<h1>' . t('The link you used to access this page has expired.') . '</h1>' . '<p>' . t('If you have created a password, you can log on') . ' ' . l(t('here'), 'user') . '.</p>';
  }
  else {
    drupal_goto('');
  }
}