<?php
function user_autocomplete($string = '') {
$matches = array();
if ($string) {
$result = db_select('users')
->fields('users', array(
'name',
))
->condition('name', db_like($string) . '%', 'LIKE')
->range(0, 10)
->execute();
foreach ($result as $user) {
$matches[$user->name] = check_plain($user->name);
}
}
drupal_json_output($matches);
}
function user_pass() {
global $user;
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Username or e-mail address'),
'#size' => 60,
'#maxlength' => max(USERNAME_MAX_LENGTH, EMAIL_MAX_LENGTH),
'#required' => TRUE,
'#default_value' => isset($_GET['name']) ? $_GET['name'] : '',
);
if ($user->uid > 0) {
$form['name']['#type'] = 'value';
$form['name']['#value'] = $user->mail;
$form['mail'] = array(
'#prefix' => '<p>',
'#markup' => t('Password reset instructions will be mailed to %email. You must log out to use the password reset link in the e-mail.', array(
'%email' => $user->mail,
)),
'#suffix' => '</p>',
);
}
$form['actions'] = array(
'#type' => 'actions',
);
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('E-mail new password'),
);
return $form;
}
function user_pass_validate($form, &$form_state) {
$name = trim($form_state['values']['name']);
$users = user_load_multiple(array(), array(
'mail' => $name,
'status' => '1',
));
$account = reset($users);
if (!$account) {
$users = user_load_multiple(array(), array(
'name' => $name,
'status' => '1',
));
$account = reset($users);
}
if (isset($account->uid)) {
form_set_value(array(
'#parents' => array(
'account',
),
), $account, $form_state);
}
else {
form_set_error('name', t('Sorry, %name is not recognized as a user name or an e-mail address.', array(
'%name' => $name,
)));
}
}
function user_pass_submit($form, &$form_state) {
global $language;
$account = $form_state['values']['account'];
$mail = _user_mail_notify('password_reset', $account, $language);
if (!empty($mail)) {
watchdog('user', 'Password reset instructions mailed to %name at %email.', array(
'%name' => $account->name,
'%email' => $account->mail,
));
drupal_set_message(t('Further instructions have been sent to your e-mail address.'));
}
$form_state['redirect'] = 'user';
return;
}
function user_pass_reset($form, &$form_state, $uid, $timestamp, $hashed_pass, $action = NULL) {
global $user;
if ($user->uid) {
if ($user->uid == $uid) {
$destination = array();
if (isset($_GET['destination'])) {
$destination = drupal_get_destination();
}
user_logout_current_user();
unset($_GET['destination']);
drupal_goto(current_path(), array(
'query' => drupal_get_query_parameters() + $destination,
));
}
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'),
)), 'warning');
}
else {
drupal_set_message(t('The one-time login link you clicked is invalid.'), 'error');
}
drupal_goto();
}
}
else {
$timeout = variable_get('user_password_reset_timeout', 86400);
$current = REQUEST_TIME;
$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.'), 'error');
drupal_goto('user/password');
}
elseif ($account->uid && $timestamp >= $account->login && $timestamp <= $current && $hashed_pass == user_pass_rehash($account->pass, $timestamp, $account->login, $account->uid)) {
if ($action == 'login') {
$user = $account;
user_login_finalize();
watchdog('user', 'User %name used one-time login link at time %timestamp.', array(
'%name' => $account->name,
'%timestamp' => $timestamp,
));
drupal_set_message(t('You have just used your one-time login link. It is no longer necessary to use this link to log in. Please change your password.'));
$token = drupal_random_key();
$_SESSION['pass_reset_' . $user->uid] = $token;
drupal_goto('user/' . $user->uid . '/edit', array(
'query' => array(
'pass-reset-token' => $token,
),
));
}
else {
$form['message'] = array(
'#markup' => t('<p>This is a one-time login for %user_name and will expire on %expiration_date.</p><p>Click on this button to log in to the site and change your password.</p>', array(
'%user_name' => $account->name,
'%expiration_date' => format_date($timestamp + $timeout),
)),
);
$form['help'] = array(
'#markup' => '<p>' . t('This login can be used only once.') . '</p>',
);
$form['actions'] = array(
'#type' => 'actions',
);
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Log in'),
);
$form['#action'] = url("user/reset/{$uid}/{$timestamp}/{$hashed_pass}/login");
return $form;
}
}
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.'), 'error');
drupal_goto('user/password');
}
}
else {
drupal_access_denied();
drupal_exit();
}
}
}
function user_logout() {
user_logout_current_user();
drupal_goto();
}
function user_logout_current_user() {
global $user;
watchdog('user', 'Session closed for %name.', array(
'%name' => $user->name,
));
module_invoke_all('user_logout', $user);
session_destroy();
}
function template_preprocess_user_profile(&$variables) {
$account = $variables['elements']['#account'];
foreach (element_children($variables['elements']) as $key) {
$variables['user_profile'][$key] = $variables['elements'][$key];
}
field_attach_preprocess('user', $account, $variables['elements'], $variables);
}
function template_preprocess_user_profile_item(&$variables) {
$variables['title'] = $variables['element']['#title'];
$variables['value'] = $variables['element']['#markup'];
$variables['attributes'] = '';
if (isset($variables['element']['#attributes'])) {
$variables['attributes'] = drupal_attributes($variables['element']['#attributes']);
}
}
function template_preprocess_user_profile_category(&$variables) {
$variables['title'] = check_plain($variables['element']['#title']);
$variables['profile_items'] = $variables['element']['#children'];
$variables['attributes'] = '';
if (isset($variables['element']['#attributes'])) {
$variables['attributes'] = drupal_attributes($variables['element']['#attributes']);
}
}
function user_profile_form($form, &$form_state, $account, $category = 'account') {
global $user;
if (!isset($form_state['user'])) {
$form_state['user'] = $account;
}
else {
$account = $form_state['user'];
}
$form['#user'] = $account;
$form['#user_category'] = $category;
if ($category == 'account') {
user_account_form($form, $form_state);
$langcode = entity_language('user', $account);
field_attach_form('user', $account, $form, $form_state, $langcode);
}
$form['actions'] = array(
'#type' => 'actions',
);
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
if ($category == 'account') {
$form['actions']['cancel'] = array(
'#type' => 'submit',
'#value' => t('Cancel account'),
'#submit' => array(
'user_edit_cancel_submit',
),
'#access' => $account->uid > 1 && ($account->uid == $user->uid && user_access('cancel account') || user_access('administer users')),
);
}
$form['#validate'][] = 'user_profile_form_validate';
$form['#submit'][] = 'user_profile_form_submit';
return $form;
}
function user_profile_form_validate($form, &$form_state) {
entity_form_field_validate('user', $form, $form_state);
}
function user_profile_form_submit($form, &$form_state) {
$account = $form_state['user'];
$category = $form['#user_category'];
form_state_values_clean($form_state);
$account_unchanged = clone $account;
entity_form_submit_build_entity('user', $account, $form, $form_state);
$edit = array_intersect_key((array) $account, $form_state['values']);
user_save($account_unchanged, $edit, $category);
$form_state['values']['uid'] = $account->uid;
if ($category == 'account' && !empty($edit['pass'])) {
unset($_SESSION['pass_reset_' . $account->uid]);
}
cache_clear_all();
drupal_set_message(t('The changes have been saved.'));
}
function user_edit_cancel_submit($form, &$form_state) {
$destination = array();
if (isset($_GET['destination'])) {
$destination = drupal_get_destination();
unset($_GET['destination']);
}
$form_state['redirect'] = array(
"user/" . $form['#user']->uid . "/cancel",
array(
'query' => $destination,
),
);
}
function user_cancel_confirm_form($form, &$form_state, $account) {
global $user;
$form['_account'] = array(
'#type' => 'value',
'#value' => $account,
);
$admin_access = user_access('administer users');
$can_select_method = $admin_access || user_access('select account cancellation method');
$form['user_cancel_method'] = array(
'#type' => 'item',
'#title' => $account->uid == $user->uid ? t('When cancelling your account') : t('When cancelling the account'),
'#access' => $can_select_method,
);
$form['user_cancel_method'] += user_cancel_methods();
$override_access = $admin_access && $account->uid != $user->uid;
$form['user_cancel_confirm'] = array(
'#type' => 'checkbox',
'#title' => t('Require e-mail confirmation to cancel account.'),
'#default_value' => $override_access ? FALSE : TRUE,
'#access' => $override_access,
'#description' => t('When enabled, the user must confirm the account cancellation via e-mail.'),
);
$default_notify = variable_get('user_mail_status_canceled_notify', FALSE);
$form['user_cancel_notify'] = array(
'#type' => 'checkbox',
'#title' => t('Notify user when account is canceled.'),
'#default_value' => $override_access ? FALSE : $default_notify,
'#access' => $override_access && $default_notify,
'#description' => t('When enabled, the user will receive an e-mail notification after the account has been cancelled.'),
);
if ($account->uid == $user->uid) {
$question = t('Are you sure you want to cancel your account?');
}
else {
$question = t('Are you sure you want to cancel the account %name?', array(
'%name' => $account->name,
));
}
$description = '';
if ($can_select_method) {
$description = t('Select the method to cancel the account above.');
foreach (element_children($form['user_cancel_method']) as $element) {
unset($form['user_cancel_method'][$element]['#description']);
}
}
else {
foreach (element_children($form['user_cancel_method']) as $element) {
if ($form['user_cancel_method'][$element]['#default_value'] == $form['user_cancel_method'][$element]['#return_value']) {
$description = $form['user_cancel_method'][$element]['#description'];
}
unset($form['user_cancel_method'][$element]['#description']);
}
}
$form['uid'] = array(
'#type' => 'value',
'#value' => $account->uid,
);
return confirm_form($form, $question, 'user/' . $account->uid, $description . ' ' . t('This action cannot be undone.'), t('Cancel account'), t('Cancel'));
}
function user_cancel_confirm_form_submit($form, &$form_state) {
global $user;
$account = $form_state['values']['_account'];
if (user_access('administer users') && empty($form_state['values']['user_cancel_confirm']) && $account->uid != $user->uid) {
user_cancel($form_state['values'], $account->uid, $form_state['values']['user_cancel_method']);
$form_state['redirect'] = 'admin/people';
}
else {
$edit = array(
'user_cancel_method' => $form_state['values']['user_cancel_method'],
'user_cancel_notify' => $form_state['values']['user_cancel_notify'],
);
$account = user_save($account, $edit);
_user_mail_notify('cancel_confirm', $account);
drupal_set_message(t('A confirmation request to cancel your account has been sent to your e-mail address.'));
watchdog('user', 'Sent account cancellation request to %name %email.', array(
'%name' => $account->name,
'%email' => '<' . $account->mail . '>',
), WATCHDOG_NOTICE);
$form_state['redirect'] = "user/{$account->uid}";
}
}
function user_cancel_methods() {
$methods = array(
'user_cancel_block' => array(
'title' => t('Disable the account and keep its content.'),
'description' => t('Your account will be blocked and you will no longer be able to log in. All of your content will remain attributed to your user name.'),
),
'user_cancel_block_unpublish' => array(
'title' => t('Disable the account and unpublish its content.'),
'description' => t('Your account will be blocked and you will no longer be able to log in. All of your content will be hidden from everyone but administrators.'),
),
'user_cancel_reassign' => array(
'title' => t('Delete the account and make its content belong to the %anonymous-name user.', array(
'%anonymous-name' => variable_get('anonymous', t('Anonymous')),
)),
'description' => t('Your account will be removed and all account information deleted. All of your content will be assigned to the %anonymous-name user.', array(
'%anonymous-name' => variable_get('anonymous', t('Anonymous')),
)),
),
'user_cancel_delete' => array(
'title' => t('Delete the account and its content.'),
'description' => t('Your account will be removed and all account information deleted. All of your content will also be deleted.'),
'access' => user_access('administer users'),
),
);
drupal_alter('user_cancel_methods', $methods);
$default_method = variable_get('user_cancel_method', 'user_cancel_block');
foreach ($methods as $name => $method) {
$form[$name] = array(
'#type' => 'radio',
'#title' => $method['title'],
'#description' => isset($method['description']) ? $method['description'] : NULL,
'#return_value' => $name,
'#default_value' => $default_method,
'#parents' => array(
'user_cancel_method',
),
);
}
return $form;
}
function user_cancel_confirm($account, $timestamp = 0, $hashed_pass = '') {
$timeout = 86400;
$current = REQUEST_TIME;
if (isset($account->data['user_cancel_method']) && !empty($timestamp) && !empty($hashed_pass)) {
if ($timestamp <= $current && $current - $timestamp < $timeout && $account->uid && $timestamp >= $account->login && $hashed_pass == user_pass_rehash($account->pass, $timestamp, $account->login, $account->uid)) {
$edit = array(
'user_cancel_notify' => isset($account->data['user_cancel_notify']) ? $account->data['user_cancel_notify'] : variable_get('user_mail_status_canceled_notify', FALSE),
);
user_cancel($edit, $account->uid, $account->data['user_cancel_method']);
batch_process('');
}
else {
drupal_set_message(t('You have tried to use an account cancellation link that has expired. Please request a new one using the form below.'), 'error');
drupal_goto("user/{$account->uid}/cancel");
}
}
return MENU_ACCESS_DENIED;
}
function user_page() {
global $user;
if ($user->uid) {
menu_set_active_item('user/' . $user->uid);
return menu_execute_active_handler(NULL, FALSE);
}
else {
return drupal_get_form('user_login');
}
}