You are here

function regcode_generate in Registration codes 7

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

Generate a code.

1 call to regcode_generate()
regcode_admin_create_submit in ./regcode.admin.php
Process creation form.

File

./regcode.module, line 624
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;
}