View source
<?php
function prlp_menu() {
$items = array();
$items['admin/config/people/accounts/prlp'] = array(
'title' => 'PRLP settings',
'description' => 'Configure - Password Reset Landing Page - what happens when users use their one-time login links for resetting password.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'prlp_admin_settings',
),
'access arguments' => array(
'administer users',
),
'file' => 'prlp.admin.inc',
'type' => MENU_LOCAL_TASK,
);
return $items;
}
function prlp_drupal_goto_alter(&$path, &$options, &$http_response_code) {
if (isset($_SESSION['pass_reset_hash'])) {
$parts = explode('/', $path);
if (isset($parts[4]) && $parts[4] === 'confirm' && $parts[0] === 'user' && $parts[1] === 'reset') {
$_SESSION['prlp_reset_hash'] = $_SESSION['pass_reset_hash'];
}
}
}
function prlp_form_user_pass_reset_alter(&$form, &$form_state) {
@(list($uid, $timestamp, $hashed_pass, $action) = $form_state['build_info']['args']);
if ($uid) {
$form['#user'] = user_load($uid);
if (empty($action)) {
user_account_form($form, $form_state);
$user_account_form_id = 'user_account_form';
drupal_alter(array(
'form',
'form_user_account_form',
), $form, $form_state, $user_account_form_id);
$form['#user_category'] = 'account';
foreach ($form as $key => &$element) {
if (in_array($key, prlp_get_hidden_fields())) {
$element['#access'] = FALSE;
}
}
foreach ($form['account'] as $key => &$element) {
if (in_array($key, prlp_get_hidden_account_fields())) {
$element['#access'] = FALSE;
}
}
unset($form['#action']);
if (isset($_SESSION['prlp_reset_hash']) && empty($form_state['input'])) {
$_SESSION['pass_reset_hash'] = $_SESSION['prlp_reset_hash'];
}
if (!empty($form['message']['#markup'])) {
$form['message']['#markup'] = str_replace('<p>Click on this button to log in to the site and change your password.</p>', '', $form['message']['#markup']);
}
if (is_array($form['account']['pass']) && variable_get('prlp_password_required', 1)) {
$form['account']['pass']['#required'] = TRUE;
}
$form['#submit'][] = 'prlp_user_pass_reset_submit';
}
}
}
define('PRLP_DESTINATION_DEFAULT', 'user/%uid/edit');
function prlp_user_pass_reset_submit($form, &$form_state) {
global $user;
@(list($uid, $timestamp, $hashed_pass, $action) = $form_state['build_info']['args']);
if ($hashed_pass === 'confirm') {
if (isset($_SESSION['prlp_reset_hash'])) {
$hashed_pass = $_SESSION['prlp_reset_hash'];
unset($_SESSION['prlp_reset_hash']);
}
else {
drupal_set_message(t('Could not find password hash; login will likely fail.'), 'warning');
}
}
if ($user->uid) {
if ($user->uid == $uid) {
drupal_set_message(t('You are logged in as %user. <a href="!user_edit">Change your password.</a>', array(
'%user' => $user->name,
'!user_edit' => url("user/{$user->uid}/edit"),
)));
}
else {
$reset_link_account = user_load($uid);
if (!empty($reset_link_account)) {
drupal_set_message(t('Another user (%other_user) is already logged into the site on this computer, but you tried to use a one-time link for user %resetting_user. Please <a href="!logout">logout</a> and try using the link again.', array(
'%other_user' => $user->name,
'%resetting_user' => $reset_link_account->name,
'!logout' => url('user/logout'),
)));
}
else {
drupal_set_message(t('The one-time login link you clicked is invalid.'));
}
}
$form_state['redirect'] = '';
}
else {
$timeout = variable_get('user_password_reset_timeout', 86400);
$current = REQUEST_TIME;
$form_state['values']['roles'] = array_filter($form_state['values']['roles']);
$users = user_load_multiple(array(
$uid,
), array(
'status' => '1',
));
if ($timestamp <= $current && ($account = reset($users))) {
if ($account->login && $current - $timestamp > $timeout) {
drupal_set_message(t('You have tried to use a one-time login link that has expired. Please request a new one using the form below.'));
$form_state['redirect'] = 'user/password';
}
elseif ($account->uid && $timestamp >= $account->login && $timestamp <= $current && $hashed_pass == user_pass_rehash($account->pass, $timestamp, $account->login, $account->uid)) {
$form_state['user'] = $account;
if (!isset($_SESSION)) {
$_SESSION = array();
}
user_profile_form_submit($form, $form_state);
$message_index = array_search(t('The changes have been saved.'), $_SESSION['messages']['status']);
$_SESSION['messages']['status'][$message_index] = variable_get($account->access ? 'prlp_confirmation_message_existing_users' : 'prlp_confirmation_message_new_users');
$_SESSION['messages']['status'] = array_filter($_SESSION['messages']['status']);
if (empty($_SESSION['messages']['status'])) {
unset($_SESSION['messages']['status']);
}
watchdog('user', 'User %name used one-time login link at time %timestamp.', array(
'%name' => $account->name,
'%timestamp' => $timestamp,
));
$user = user_load($account->uid);
user_login_finalize();
$token = drupal_hash_base64(drupal_random_bytes(55));
$_SESSION['pass_reset_' . $user->uid] = $token;
$destination = variable_get('prlp_destination', PRLP_DESTINATION_DEFAULT);
if (empty($destination) || $destination == PRLP_DESTINATION_DEFAULT) {
$form_state['redirect'] = array(
'user/' . $user->uid . '/edit',
array(
'query' => array(
'pass-reset-token' => $token,
),
),
);
}
else {
$destination = str_replace('%uid', $user->uid, $destination);
$destination = drupal_parse_url($destination);
$form_state['redirect'] = array(
$destination['path'],
$destination,
);
}
}
else {
drupal_set_message(t('You have tried to use a one-time login link that has either been used or is no longer valid. Please request a new one using the form below.'));
$form_state['redirect'] = 'user/password';
}
}
else {
drupal_access_denied();
}
}
}
define('PRLP_HIDDEN_FIELDS_DEFAULT', "signature_settings\npicture");
function prlp_get_hidden_fields() {
$fields =& drupal_static(__FUNCTION__);
if (!isset($fields)) {
$fields = explode("\n", variable_get('prlp_hidden_fields', PRLP_HIDDEN_FIELDS_DEFAULT));
foreach ($fields as &$field) {
$field = trim($field);
}
}
return $fields;
}
define('PRLP_HIDDEN_ACCOUNT_FIELDS_DEFAULT', "mail");
function prlp_get_hidden_account_fields() {
$fields =& drupal_static(__FUNCTION__);
if (!isset($fields)) {
$fields = explode("\n", variable_get('prlp_hidden_account_fields', PRLP_HIDDEN_ACCOUNT_FIELDS_DEFAULT));
}
return $fields;
}
function prlp_module_implements_alter(&$implementations, $hook) {
if (module_exists("password_policy") && $hook == 'form_alter') {
$group = $implementations['password_policy'];
unset($implementations['password_policy']);
$implementations['password_policy'] = $group;
}
}
function prlp_variable_info($options) {
$variables['prlp_confirmation_message_existing_users'] = array(
'type' => 'string',
'title' => t('Confirmation message for existing users', array(), $options),
'default' => 'Your new password has been saved.',
'description' => t('Confirmation message that will be shown to existing users after they changed their password.', array(), $options),
'group' => 'prlp_settings',
'required' => FALSE,
);
$variables['prlp_confirmation_message_new_users'] = array(
'type' => 'string',
'title' => t('Confirmation message for new users', array(), $options),
'default' => 'Thank you for setting your password, the registration process has completed.',
'description' => t('Confirmation message that will be shown to new users after they set their password.', array(), $options),
'group' => 'prlp_settings',
'required' => FALSE,
);
return $variables;
}
function prlp_variable_group_info() {
$groups['prlp_settings'] = array(
'title' => t('Password reset settings'),
'description' => t('Settings of the prlp module.'),
);
return $groups;
}