You are here

function regcode_generate in Registration codes 8

Same name and namespace in other branches
  1. 6.2 regcode.module \regcode_generate()
  2. 7.2 regcode.module \regcode_generate()
  3. 7 regcode.module \regcode_generate()

Generates a code.

1 call to regcode_generate()
RegcodeAdminCreateForm::submitForm in src/Form/RegcodeAdminCreateForm.php
Form submission handler.

File

./regcode.module, line 373
Main functionality and hooks of regcode module.

Code

function regcode_generate($length, $output, $case) {
  static $seeded = FALSE;

  // Possible seeds.
  $outputs['alpha'] = 'abcdefghijklmnopqrstuvwqyz';
  $outputs['numeric'] = '0123456789';
  $outputs['alphanum'] = 'abcdefghijklmnopqrstuvwqyz0123456789';
  $outputs['hexadec'] = '0123456789abcdef';

  // Choose seed.
  if (isset($outputs[$output])) {
    $output = $outputs[$output];
  }

  // Seed generator (only do this once per invocation).
  if (!$seeded) {
    list($usec, $sec) = explode(' ', microtime());
    $seed = (double) $sec + (double) $usec * 100000;
    mt_srand($seed);
    $seeded = TRUE;
  }

  // Generate.
  $str = '';
  $output_count = strlen($output);
  for ($i = 0; $length > $i; $i++) {
    $str .= $output[mt_rand(0, $output_count - 1)];
  }
  if ($case) {
    $str = strtoupper($str);
  }
  return $str;
}