You are here

function _mass_pwreset_generate_password in Mass Password Reset 7

Same name and namespace in other branches
  1. 8 mass_pwreset.module \_mass_pwreset_generate_password()
  2. 2.x mass_pwreset.module \_mass_pwreset_generate_password()

Generate user passwords.

Modified version of Drupal's user_password() for generating passwords which will never be sent to anyone. The default length of the password has been changed to 12 characters to increse entropy. Includes two sets of password requirements (standard and admin).

Parameters

int $length: Length to generate the password

bool $admin: (optional) TRUE if password should be generated from more stringent password requirements. Defaults to FALSE. Mimimum password length shifts to 20 if TRUE, regardless of the password length passed into $length.

Return value

string Generated password

1 call to _mass_pwreset_generate_password()
mass_pwreset_execute_reset in ./mass_pwreset.batch.inc
Callback: Reset User password

File

./mass_pwreset.module, line 54
Reset user passwords and optionally notify users.

Code

function _mass_pwreset_generate_password($length = 8, $admin = FALSE) {

  // Set password requirements for standard or admin based on $admin value.
  if ($admin) {

    // Enforce minimum password length.
    if ($length < 12) {
      $length = 12;
    }

    // This veriable contains the regex to enforce the following password
    // requirements:
    // First and last characters cannot be digits (0-9).
    // Must contain two digit characters (0-9).
    // Must contain one lower case character (a-z).
    // Must contain one upper case character (A-Z).
    // Must contain three special characters
    // ( ()`~!@#$%^&*-+=|\{}[]:;"'<>,.?/ ).
    // Minimum length is 12 characters.
    // Maximum length is 128 characters.
    $pass_requirements = '_^(?=.*\\d.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[()`~!@#$%^\\&*\\-+=\\|\\{}[\\]:;"\'<>,.?/].*[()`~!@#$%^\\&*\\-+=\\|\\{}[\\]:;"\'<>,.?/].*[()`~!@#$%^\\&*\\-+=\\|\\{}[\\]:;"\'<>,.?/])[\\D]{1}[\\s0-9a-zA-Z()`~!@#$%^\\&*\\-+=\\|\\{}[\\]:;"\'<>,.?/]{10,126}[\\D]{1}$_';
  }
  else {

    // Enforce minimum password length.
    if ($length < 8) {
      $length = 8;
    }

    // This veriable contains the regex to enforce the following password
    // requirements:
    // First and last characters cannot be digits (0-9).
    // Must contain one digit character (0-9).
    // Must contain one lower case character (a-z).
    // Must contain one upper case character (A-Z).
    // Must contain one special character
    // ( ()`~!@#$%^&*-+=|\{}[]:;"'<>,.?/ ).
    // Minimum length is 8 characters.
    // Maximum length is 128 characters.
    $pass_requirements = '_^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[()`~!@#$%^\\&*\\-+=\\|\\{}[\\]:;"\'<>,.?/])[\\D]{1}[\\s0-9a-zA-Z()`~!@#$%^\\&*\\-+=\\|\\{}[\\]:;"\'<>,.?/]{6,126}[\\D]{1}$_';
  }

  // This variable contains the list of allowable characters for the password.
  $allowable_characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789()`~!@#$%^&*-+=|\\{}[]:;"\'<>,.?/';

  // Zero-based count of characters in the allowable list.
  $len = drupal_strlen($allowable_characters) - 1;

  // Declare the password as a blank string.
  $pass = '';

  // Generate passwords until password requiments are met.
  while (preg_match($pass_requirements, $pass) == 0) {

    // Declare the password as a blank string.
    $pass = '';

    // Loop the number of times specified by $length.
    for ($i = 0; $i < $length; $i++) {
      do {

        // Find a secure random number within the range needed.
        $index = ord(drupal_random_bytes(1));
      } while ($index > $len);

      // Each iteration, pick a random character from the
      // allowable string and append it to the password:
      $pass .= $allowable_characters[$index];
    }
  }
  return $pass;
}