AutoLoginUrlCreate.php in Auto Login URL 8
File
src/AutoLoginUrlCreate.php
View source
<?php
namespace Drupal\auto_login_url;
use Drupal\Core\Database\Connection;
use Drupal\Component\Utility\Crypt;
use Drupal\Core\Site\Settings;
class AutoLoginUrlCreate {
protected $connection;
public function __construct(Connection $connection) {
$this->connection = $connection;
}
function create($uid, $destination, $absolute = FALSE) {
$config = \Drupal::config('auto_login_url.settings');
$auto_login_url_secret = \Drupal::service('auto_login_url.general')
->getSecret();
$password = \Drupal::service('auto_login_url.general')
->getUserHash($uid);
$key = Settings::getHashSalt() . $auto_login_url_secret . $password;
$hash_helper = 0;
do {
$data = $uid . microtime(TRUE) . $destination . $hash_helper;
$hash = Crypt::hmacBase64($data, $key);
$hash = substr($hash, 0, $config
->get('token_length'));
$hash_db = Crypt::hmacBase64($hash, $key);
$result = $this->connection
->select('auto_login_url', 'alu')
->fields('alu', [
'hash',
])
->condition('alu.hash', $hash_db)
->execute()
->fetchAssoc();
$hash_helper++;
} while (isset($result['hash']));
$this->connection
->insert('auto_login_url')
->fields([
'uid',
'hash',
'destination',
'timestamp',
])
->values([
'uid' => $uid,
'hash' => $hash_db,
'destination' => $destination,
'timestamp' => time(),
])
->execute();
$absolute_path = '';
if ($absolute) {
global $base_url;
$absolute_path = $base_url . '/';
}
return $absolute_path . 'autologinurl/' . $uid . '/' . $hash;
}
function convertText($uid, $text) {
global $base_root;
$pattern = '/' . str_replace('/', '\\/', $base_root) . '\\/[^\\s^"^\']*/';
$current_conversion = new AutoLoginUrlConvertTextClass($uid);
$text = preg_replace_callback($pattern, [
&$current_conversion,
'replace',
], $text);
return $text;
}
}