You are here

function auto_login_url_create in Auto Login URL 7

Same name and namespace in other branches
  1. 8 auto_login_url.module \auto_login_url_create()
  2. 2.x auto_login_url.module \auto_login_url_create()

Create an auto login hash on demand.

Parameters

int $uid: User id.

string $destination: Destination URL.

bool $absolute: Absolute or relative link.

Return value

string Autologin URL.

5 calls to auto_login_url_create()
AutoLoginUrlConvertTextClass::replace in ./auto_login_url.module
Replace each link in the text.
AutoLoginUrlTestCase::testAluFloodCheck in tests/auto_login_url.test
Test flood.
AutoLoginUrlTestCase::testAluSettingsCheck in tests/auto_login_url.test
Test token generation with different settings.
AutoLoginUrlTestCase::testAluTokenGenerationCheck in tests/auto_login_url.test
Test token generation.
auto_login_url_tokens in ./auto_login_url.module
Implements hook_tokens().

File

./auto_login_url.module, line 362
Main file for auto_login_url module.

Code

function auto_login_url_create($uid = NULL, $destination = '', $absolute = FALSE) {
  if ($uid != NULL && is_numeric($uid) && $uid != 0) {

    // Get ALU secret.
    $auto_login_url_secret = _auto_login_url_get_secret();

    // Get user password.
    $password = db_query("SELECT pass FROM {users} WHERE uid = :uid", array(
      ':uid' => $uid,
    ))
      ->fetchField();

    // Create key.
    $key = drupal_get_hash_salt() . $auto_login_url_secret . $password;

    // Repeat until the hash that is saved in DB is unique.
    $hash_helper = 0;
    do {
      $data = $uid . microtime(TRUE) . $destination . $hash_helper;

      // Generate hash.
      $hash = drupal_hmac_base64($data, $key);

      // Get substring.
      $hash = substr($hash, 0, variable_get('auto_login_url_token_length', 64));

      // Generate hash to save to DB.
      $hash_db = drupal_hmac_base64($hash, $key);

      // Check hash is unique.
      $result = db_select('auto_login_url', 'alu')
        ->fields('alu', array(
        'hash',
      ))
        ->condition('alu.hash', $hash_db)
        ->execute()
        ->rowCount();

      // Increment value in case there will be a next iteration.
      $hash_helper++;
    } while ($result != 0);

    // Insert a new hash.
    db_insert('auto_login_url')
      ->fields(array(
      'uid',
      'hash',
      'destination',
      'timestamp',
    ))
      ->values(array(
      'uid' => $uid,
      'hash' => $hash_db,
      'destination' => $destination,
      'timestamp' => time(),
    ))
      ->execute();

    // Check if link is absolute.
    $absolute_path = '';
    if ($absolute) {
      global $base_url;
      $absolute_path = $base_url . '/';
    }
    return $absolute_path . variable_get('auto_login_url_short_url', 'autologinurl') . '/' . $uid . '/' . $hash;
  }

  // Return empty string.
  return '';
}