You are here

function logintoboggan_user_register_submit in LoginToboggan 5

Same name and namespace in other branches
  1. 6 logintoboggan.module \logintoboggan_user_register_submit()
  2. 7 logintoboggan.module \logintoboggan_user_register_submit()

Custom submit function for user registration form

File

./logintoboggan.module, line 290
Logintoboggan Module

Code

function logintoboggan_user_register_submit($form_id, $form_values) {
  global $base_url;
  $mail = $form_values['mail'];
  $name = $form_values['name'];
  $from = variable_get('site_mail', ini_get('sendmail_from'));
  $reg_pass_set = !variable_get('user_email_verification', TRUE);

  // Test here for a valid pre-auth -- if the pre-auth is set to the auth user, we
  // handle things a bit differently.
  $pre_auth = logintoboggan_validating_id() != DRUPAL_AUTHENTICATED_RID;

  // If we are allowing user selected passwords then skip the auto-generate function
  // The new user's status should default to the site settings, unless reg_passwd_set == 1
  // (immediate login, we are going to assign a pre-auth role), and we want to allow
  // admin approval accounts access to the site.
  if ($reg_pass_set) {
    $pass = $form_values['pass'];
    $status = 1;
  }
  else {
    $pass = user_password();
    $status = variable_get('user_register', 1) == 1;
  }

  // Must unset mail confirmation to prevent it from being saved in the user table's 'data' field.
  if (isset($form_values['conf_mail'])) {
    unset($form_values['conf_mail']);
  }
  if (array_intersect(array_keys($form_values), array(
    'uid',
    'roles',
    'init',
    'session',
    'status',
  ))) {
    watchdog('security', t('Detected malicious attempt to alter protected user fields.'), WATCHDOG_WARNING);
    return 'user/register';
  }

  // Set the roles for the new user -- add the pre-auth role if they can pick their own password,
  // and the pre-auth role isn't anon or auth user.
  $validating_id = logintoboggan_validating_id();
  $roles = isset($form_values['roles']) ? array_filter($form_values['roles']) : array();
  if ($reg_pass_set && $validating_id > DRUPAL_AUTHENTICATED_RID) {
    $roles[$validating_id] = 1;
  }
  $edit = array_merge($form_values, array(
    'pass' => $pass,
    'init' => $mail,
    'roles' => $roles,
    'status' => $status,
  ));
  $account = user_save('', $edit);
  watchdog('user', t('New user: %name %email.', array(
    '%name' => $name,
    '%email' => "<{$mail}>",
  )), WATCHDOG_NOTICE, l(t('edit'), 'user/' . $account->uid . '/edit'));
  $login_url = variable_get('user_register', 1) == 1 ? logintoboggan_eml_validate_url($account) : user_pass_reset_url($account);
  $variables = array(
    '!username' => $name,
    '!site' => variable_get('site_name', 'drupal'),
    '!password' => $pass,
    '!uri' => $base_url,
    '!uri_brief' => substr($base_url, strlen(_logintoboggan_protocol() . '://')),
    '!mailto' => $mail,
    '!date' => format_date(time()),
    '!login_uri' => url('user', NULL, NULL, TRUE),
    '!edit_uri' => url('user/' . $account->uid . '/edit', NULL, NULL, TRUE),
    '!login_url' => $login_url,
  );

  // Compose the appropriate user message--admin approvals don't require a validation e-mail.
  if ($reg_pass_set && variable_get('user_register', 1) == 1) {
    if ($pre_auth) {
      $message = t('A validation e-mail has been sent to your e-mail address. In order to gain full access to the site, you will need to follow the instructions in that message.');
    }
    else {
      $message = '';
    }
  }
  else {
    $message = t('Your password and further instructions have been sent to your e-mail address.');
  }
  if (variable_get('user_register', 1) == 1) {

    // Create new user account, no administrator approval required.
    $subject = _user_mail_text('welcome_subject', $variables);
    $body = _user_mail_text('welcome_body', $variables);
    $mailkey = 'user-register-welcome';
  }
  elseif (variable_get('user_register', 1) == 2) {

    // Create new user account, administrator approval required.
    $subject = _user_mail_text('approval_subject', $variables);
    $body = _user_mail_text('approval_body', $variables);
    $mailkey = 'user-register-approval-user';
    $message = t('Thank you for applying for an account. Your account is currently pending approval by the site administrator.<br />') . $message;
    $admin_variables = array(
      '!u' => $account->name,
      '!validating_url' => logintoboggan_eml_validate_url($account) . '/admin',
      '!uri' => url("user/{$account->uid}/edit", NULL, NULL, TRUE),
    );
    if ($reg_pass_set) {
      $admin_body = t("!u has applied for an account, and has automatically received the permissions of the LoginToboggan validating role.  To give the user full site permissions, click the link below:\n\n!validating_url\n\nAlternatively, you may visit their user account listed below and remove them from the validating role.\n\n!uri", $admin_variables);
    }
    else {
      $admin_body = t("!u has applied for an account.\n\n!uri", $admin_variables);
    }
    drupal_mail('user-register-approval-admin', $from, $subject, $admin_body, $from);
  }

  //mail the user.
  drupal_mail($mailkey, $mail, $subject, $body, $from);
  drupal_set_message($message);

  // where do we need to redirect after registration?
  $redirect = _logintoboggan_process_redirect(variable_get('toboggan_redirect_on_register', ''), $account);

  //log the user in if they created the account and immediate login is enabled.
  if ($reg_pass_set) {
    return logintoboggan_process_login($account, $edit, $redirect);
  }

  //redirect to the appropriate page.
  return $redirect;
}