[geeklog-cvs] Auth_Enterprise/Server AEPasswordGenerator.class.php,NONE,1.1
tony at iowaoutdoors.org
tony at iowaoutdoors.org
Wed Jun 16 19:13:12 EDT 2004
Update of /var/cvs/Auth_Enterprise/Server
In directory www:/tmp/cvs-serv21112
Added Files:
AEPasswordGenerator.class.php
Log Message:
Initial release.
--- NEW FILE: AEPasswordGenerator.class.php ---
<?php
/**
* Auth_Enterprise
*
* This source file is subject to version 2.02 of the PHP license, that is bundled with this package
* in the file LICENSE, and is available at through the world-wide-web at
* http://www.php.net/license/2_02.txt. If you did not receive a copy of the PHP license and are
* unable to obtain it through the world-wide-web, please send a note to license at php.net so we can
* mail you a copy immediately.
*
* @author Tony Bibbs <tony at geeklog.net>
* @copyright 2004
* @version $Id: AEPasswordGenerator.class.php,v 1.1 2004/06/16 23:13:08 tony Exp $
*
*/
/**
* Class that generates random passwords
*
* @author Tony Bibbs <tony at geeklog.net>
* @package net.geeklog.auth_enterprise.server
*
*/
class AEPasswordGenerator {
/**
* Generates a random password
*
* @author Tony Bibbs <tony at geeklog.net>
* @access public
* @return string Radomnly generated passsword
*
*/
public static function generatePassword()
{
global $gConf;
$randomPass = array();
// Let's set a sane password length. I realize 4 may even be small but some apps of low
// criticality may want this
if ($gConf[AE_PROVIDER_PEAR_DB]['passwordminlength'] < 4) {
$gConf[AE_PROVIDER_PEAR_DB]['passwordminlength'] = 4;
}
$numChars = $gConf[AE_PROVIDER_PEAR_DB]['passwordminlength'];
// Generate all the characters randomly.
for ($i = 0; $i < $numChars - 1; $i++) {
$tmpNum = rand(0,3);
switch ($tmpNum) {
case 0:
$randomPass[] = chr(rand(65,90));
break;
case 1:
// Generate lower case letter
$randomPass[] = chr(rand(97,122));
break;
case 2:
// Generate number
$randomPass[] = rand(0,9);
break;
case 3:
$randomPass[] = AEPasswordGenerator::generateSpecial();
}
}
// Set random Upper case character
if ($gConf['pw_require_upper']) {
$tmpIndex = rand(0, $numChars);
$randomPass[$tmpIndex] = chr(rand(65,90));;
}
// Set random Lower case character
if ($gConf['pw_require_lower']) {
$usedIndexes[] = $tmpIndex;
$tmpIndex = rand(0, $numChars);
while ($tmpIndex = in_array($usedIndexes)) {
$tmpIndex = rand(0, $numChars);
}
$randomPass[$tmpIndex] = chr(rand(97,122));
}
// Set random Numeric character
if ($gConf['pw_require_number']) {
$usedIndexes[] = $tmpIndex;
$tmpIndex = rand(0, $numChars);
while ($tmpIndex = in_array($usedIndexes)) {
$tmpIndex = rand(0, $numChars);
}
$randomPass[$tmpIndex] = rand(0,9);
}
// Set random Special character
if ($gConf['pw_require_special_char']) {
$usedIndexes[] = $tmpIndex;
$tmpIndex = rand(0, $numChars);
while ($tmpIndex = in_array($usedIndexes)) {
$tmpIndex = rand(0, $numChars);
}
$randomPass[$tmpIndex] = generateSpecial();
}
// Now return generated password
return implode('',$randomPass);
}
/**
* Generates a random special character
*
* NOTE: you can configure the special characters method uses by editing the AEServerConfig.php
* be careful of the characters you use as some can cause problems with end users, particularly when
* printed to paper (e.g. |)
*
* @author Tony Bibbs <tony at geeklog.net>
* @access private
* @return string Random number between 0 and 9
*
*/
private static function generateSpecial()
{
global $gConf;
$tmpIndex = rand(0, count($gConf['pw_special_chars']) - 1);
return $gConf['pw_special_chars'][$tmpIndex];
}
/**
* Determines if a password is valid by the configured rules
*
* You can set rules for what constitutes a good password via the
* server configuration
*
* @author Tony Bibbs <tony at geeklog.net>
* @access public
* @param string $password Password to validate
* @return boolean
*
*/
public static function isValidPassword($password)
{
global $gConf;
// First check the length
if (strlen($password) < $gConf[AE_PROVIDER_PEAR_DB]['passwordminlength']) {
return false;
}
$hasUpper = false;
$hasLower = false;
$hasNum = false;
$hasChar = false;
// First convert password to string for easy searches
for ($i = 0; $i < strlen($password); $i++) {
$pwdArray[$i] = $password[$i];
}
// Ensure we have an upper case character if one is required
if ($gConf['pw_require_upper']) {
foreach ($pwdArray as $curChar) {
if (ord($curChar) >= 65 AND ord($curChar) <= 90) {
$hasUpper = true;
print 'hasUpper';
break;
}
}
} else {
$hasUpper = true;
}
// Ensure we have a lower case character if one is required
if ($gConf['pw_require_lower']) {
foreach ($pwdArray as $curChar) {
if (ord($curChar) >= 97 AND ord($curChar) <= 122) {
$hasLower = true;
print 'hasLower';
break;
}
}
} else {
$hasLower = true;
}
// Ensure we have a number if one is required
if ($gConf['pw_require_number']) {
foreach ($pwdArray as $curChar) {
print $curNum;
if (is_numeric($curChar)) {
$hasNum = true;
print 'hasNumber';
break;
}
}
} else {
$hasNum = true;
}
// Ensure we have a special character if one is required
if ($gConf['pw_require_special_char']) {
foreach ($gConf['pw_special_chars'] as $curChar) {
if (strstr($password,$curChar)) {
$hasChar = true;
print 'hasChar';
break;
}
}
} else {
$hasChar = true;
}
// Now see if we got a valid password
if ($hasUpper AND $hasLower AND $hasNum AND $hasChar) {
return true;
}
return false;
}
}
?>
More information about the geeklog-cvs
mailing list