Location: PHPKode > scripts > PassGen > passGen.class.php
<?
/*----------------------------------------------------------------------
	PassGen v.1.0 - 04/06/2010
	(c) Ricardo Daniel Burone (hide@address.com)
	Licensed under GNU GPL (www.gnu.org)
------------------------------------------------------------------------
	This program is free software; you can redistribute it and/or modify it
	under the terms of the GNU General Public License as published by the
	Free Software Foundation; either version 2 of the License, or (at your
	option) any later version.

	This program is distributed in the hope that it will be useful, but
	WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
	General Public License for more details.

	You should have received a copy of the GNU General Public License along
	with this program; if not, write to the Free Software Foundation, Inc.,
	59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
------------------------------------------------------------------------
	Generates pronunciable passwords (10 chars max).
----[ USAGE ]-----------------------------------------------------------
	$objPass = new PassGen(lenght password, if use upper and lower letters, if use numbers, if repeat letters);
	$pass = $objPass->mkPassword()
----------------------------------------------------------------------*/

class PassGen {
	public function PassGen($size = 8, $difcaps = true, $numbers = false, $repitLetters = true) {
		$this->size = $size;
		$this->difcaps = $difcaps;
		$this->numbers = $numbers;
		$this->repitLetters = $repit;
	}
		
	public function mkPassword() {
		$consts='bcdgklmnprstvxyz';
		$vowels='aeiou';
		$pass = $const = $vow = "";

		for ($x=0; $x < 5; $x++) {
			mt_srand ((double) microtime() * 1000000);
			$posC = mt_rand(0,strlen($consts)-1);
			$posV = mt_rand(0,strlen($vowels)-1);
			if ($this->difcaps){
				$const .= (mt_rand(0,1)>0)?strtoupper($consts[$posC]):$consts[$posC];
				$vow .= (mt_rand(0,1)>0)?strtoupper($vowels[$posV]):$vowels[$posV];
			} else {
				$const .= $consts[$posC];
				$vow .= $vowels[$posV];
			}
			if (!$this->repitLetters){
				$consts = str_replace($consts[$posC], "", $consts);
				$vowels = str_replace($vowels[$posV], "", $vowels);
			}
		}
		
		mt_srand ((double) microtime() * 1000000);
		$forma = mt_rand(0,2);
		switch ($forma) {
			case 0:
				$pass = $const[0] . $vow[0] .$const[2] . $const[1] . $vow[1] . $const[3] . $vow[3] . $const[4] . $vow[4] . $const[5];
				break;
			case 1:
				$pass = $vow[0] . $const[0] .$vow[1] . $vow[2] . $const[1] . $vow[3] . $const[2] . $vow[5] . $vow[4] . $const[4];
				break;
			case 2:
				$pass = $const[0] . $vow[0] . $const[1] . $vow[1] . $const[2] . $vow[2] . $vow[3] . $vow[4] . $const[3] . $vow[5];
				break;
			default:
				break;
		}
		
		if ($this->numbers) {
			$cantNum = round ($this->size / 3);
			for ($x=0; $x <= $cantNum; $x++){
				mt_srand ((double) microtime() * 1000000);
				$num = mt_rand(0,9);
				$pos = mt_rand(0,($size - $cantNum) - 1);
				$pass[$pos] = $num;
			}
		}
		
		$pass = substr($pass, 0, $this->size);
		
		return $pass;
	}
}
?>
Return current item: PassGen