user_register

Versions
4.6
user_register($edit = array())
4.7 – 6
user_register()

Code

modules/user.module, line 975

<?php
function user_register($edit = array()) {
  global $user, $base_url;

  // If we are already logged on, go to the user page instead.
  if ($user->uid) {
    drupal_goto('user/'. $user->uid);
  }

  if ($edit) {
    user_module_invoke('validate', $edit, $edit, 'account');

    if (!form_get_errors()) {
      $from = variable_get('site_mail', ini_get('sendmail_from'));
      $pass = user_password();

      // TODO: Is this necessary? Won't session_write() replicate this?
      unset($edit['session']);
      if (array_intersect(array_keys($edit), array('uid', 'roles', 'init', 'session', 'status'))) {
        watchdog('security', t('Detected malicious attempt to alter protected user fields.'), WATCHDOG_WARNING);
        drupal_goto('user/register');
      }
      $account = user_save('', array_merge($edit, array('pass' => $pass, 'init' => $edit['mail'], 'roles' => array(_user_authenticated_id()), 'status' => (variable_get('user_register', 1) == 1 ? 1 : 0))));
      watchdog('user', t('New user: %name %email.', array('%name' => theme('placeholder', $edit['name']), '%email' => theme('placeholder', '<'. $edit['mail'] .'>'))), WATCHDOG_NOTICE, l(t('edit'), 'user/'. $account->uid .'/edit'));

      $variables = array('%username' => $edit['name'], '%site' => variable_get('site_name', 'drupal'), '%password' => $pass, '%uri' => $base_url, '%uri_brief' => substr($base_url, strlen('http://')), '%mailto' => $edit['mail'], '%date' => format_date(time()), '%login_uri' => url('user', NULL, NULL, TRUE), '%edit_uri' => url('user/'. $account->uid .'/edit', NULL, NULL, TRUE));

      // The first user may login immediately, and receives a customized welcome e-mail.
      if ($account->uid == 1) {
        user_mail($edit['mail'], t('drupal user account details for %s', array('%s' => $edit['name'])), strtr(t("%username,\n\nYou may now login to %uri using the following username and password:\n\n  username: %username\n  password: %password\n\n%edit_uri\n\n--drupal"), $variables), "From: $from\nReply-to: $from\nX-Mailer: Drupal\nReturn-path: $from\nErrors-to: $from");
        // This should not be t()'ed. No point as its only shown once in the sites lifetime, and it would be bad to store the password.
        $output .= "<p>Welcome to Drupal. You are user #1, which gives you full and immediate access.  All future registrants will receive their passwords via e-mail, so please configure your e-mail settings using the Administration pages.</p><p> Your password is <strong>$pass</strong>. You may change your password on the next page.</p><p>Please login below.</p>";
        $output .= form_hidden('destination', 'user/'. $account->uid .'/edit');
        $output .= form_hidden('name', $account->name);
        $output .= form_hidden('pass', $pass);
        $output .= form_submit(t('Log in'));
        return form($output);
      }
      else {
        if ($account->status) {
          // Create new user account, no administrator approval required.
          $subject = _user_mail_text('welcome_subject', $variables);
          $body = _user_mail_text('welcome_body', $variables);
          user_mail($edit['mail'], $subject, $body, "From: $from\nReply-to: $from\nX-Mailer: Drupal\nReturn-path: $from\nErrors-to: $from");
          return t('Your password and further instructions have been sent to your e-mail address.');
        }
        else {
          // Create new user account, administrator approval required.
          $subject = _user_mail_text('approval_subject', $variables);
          $body = _user_mail_text('approval_body', $variables);

          user_mail($edit['mail'], $subject, $body, "From: $from\nReply-to: $from\nX-Mailer: Drupal\nReturn-path: $from\nErrors-to: $from");
          user_mail(variable_get('site_mail', ini_get('sendmail_from')), $subject, t("%u has applied for an account.\n\n%uri", array('%u' => $account->name, '%uri' => url("user/$account->uid/edit", NULL, NULL, TRUE))), "From: $from\nReply-to: $from\nX-Mailer: Drupal\nReturn-path: $from\nErrors-to: $from");
          return t('Thank you for applying for an account. Your account is currently pending approval by the site administrator.<br />In the meantime, your password and further instructions have been sent to your e-mail address.');
        }
      }
    }
  }

  // Display the registration form.
  $output .= variable_get('user_registration_help', '');
  $affiliates = user_auth_help_links();
  if (count($affiliates) > 0) {
    $affiliates = implode(', ', $affiliates);
    $output .= '<p>'. t('Note: if you have an account with one of our affiliates (%s), you may <a href="%login_uri">login now</a> instead of registering.', array('%s' => $affiliates, '%login_uri' => url('user'))) .'</p>';
  }
  $default = form_textfield(t('Username'), 'name', $edit['name'], 30, 64, t('Your full name or your preferred username; only letters, numbers and spaces are allowed.'), NULL, TRUE);
  $default .= form_textfield(t('E-mail address'), 'mail', $edit['mail'], 30, 64, t('A password and instructions will be sent to this e-mail address, so make sure it is accurate.'), NULL, TRUE);
  $extra = _user_forms($edit, $account, $category, 'register');
  // Only display form_group around default fields if there are other groups.
  if ($extra) {
    $output .= form_group(t('Account information'), $default);
    $output .= $extra;
  }
  else {
    $output .= $default;
  }

  $output .= form_submit(t('Create new account'));

  return form($output);
}
?>
Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.