You are here

function _persistent_login_setcookie in Persistent Login 5

Same name and namespace in other branches
  1. 6 persistent_login.module \_persistent_login_setcookie()
  2. 7 persistent_login.module \_persistent_login_setcookie()
1 call to _persistent_login_setcookie()
persistent_login_user in ./persistent_login.module
Implementation of hook_user().

File

./persistent_login.module, line 361

Code

function _persistent_login_setcookie($user, $edit = array()) {
  global $cookie_domain;

  // We're about to set a new PL cookie.  If the user already has a PL
  // but $edit['pl_series'] does not exist, they got here because they
  // tried to access a protected page and had to reauthenticate.
  // Clean up the old PL series to avoid junk in the db.
  //
  if (isset($_COOKIE[PERSISTENT_LOGIN_COOKIE]) && !isset($edit['pl_series'])) {
    list($uid, $series, $token) = explode(':', $_COOKIE[PERSISTENT_LOGIN_COOKIE]);
    _persistent_login_invalidate('cleanup', 'uid=%d AND series=\'%s\'', $uid, $series);
  }
  $tok = drupal_get_token(uniqid(mt_rand(), true));
  $days = variable_get('persistent_login_maxlife', PERSISTENT_LOGIN_MAXLIFE);
  $expires = isset($edit['pl_expires']) ? $edit['pl_expires'] : ($days > 0 ? time() + $days * 86400 : 0);
  $series = isset($edit['pl_series']) ? $edit['pl_series'] : drupal_get_token(uniqid(mt_rand(), true));
  setcookie(PERSISTENT_LOGIN_COOKIE, $user->uid . ':' . $series . ':' . $tok, $expires > 0 ? $expires : 2147483647, '/', $cookie_domain);
  db_query("INSERT INTO {persistent_login} (uid, series, token, expires) VALUES (%d, '%s', '%s', %d)", $user->uid, $series, $tok, $expires);
  if (db_affected_rows() != 1) {
    watchdog('security', t('Persistent Login FAILURE: could not insert (%user, %series, %tok, %expires)', array(
      '%user' => $user->name,
      '%series' => $series,
      '%tok' => $tok,
      '%expires' => $expires,
    )));
  }
}