<?php

/**
 * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @package fusionLib
 * @copyright Copyright (c) 2007 - 2013 fusionLib. All rights reserved.
 * @link http://fusionlib.com
 */

/**
 * flUserPwdGen Class
 *
 * Class to generate a random password.
 *
 * @package fusionLib
 */
class flUserPwdGen {

	/**
	 * Generate a password.
	 *
	 * @param int $length The minimum required length of the password.
	 * @return string The password string
	 */
	static function generate($length = 8) {

		// Initialize valid char lists
		$valid_consonant = 'bcdfghjkmnprstv';
		$valid_vowel = 'aeiouy';
		$valid_numbers = '0123456789';

		// Find the charset length
		$consonant_length = strlen($valid_consonant) - 1;
		$vowel_length = strlen($valid_vowel) - 1;
		$numbers_length = strlen($valid_numbers) - 1;

		// Initialize the password and loop until we have enough chars
		$password = '';
		while(strlen($password) < $length) {
			// Select letters
			if(mt_rand(0, 2) != 1) {
				$passwordT = $valid_consonant[mt_rand(0, $consonant_length)] . $valid_vowel[mt_rand(0, $vowel_length)] . $valid_consonant[mt_rand(0, $consonant_length)];
				$password .= mt_rand(0, 3) == 1 ? strtoupper($passwordT) : $passwordT;
			}
			// Select numbers
			else
				$password .= $valid_numbers[mt_rand(0, $numbers_length)];
		}

		return $password;
	}
}
