File Directory Php

password files suitable for use as Apache authentication password files.
Function AccountCryptPassword($password,$salt)
{
if($salt== "")
{
srand(time());
$random=rand();
$itoa64= "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
$salt=substr($itoa64,$random % 64,1).substr($itoa64,($random/64) % 64,1);
}
return(crypt($password,$salt));
}
class access_accounts
{
var $password_file= "";
var $accounts=array();
var $convert_to_lower_case=0;
Function ReadAccounts($password_file)
{
global $php_errormsg;
$passwd=@File($password_file);
if(GetType($passwd)!= "array")
return( "can not read password file \"$password_file\" ($php_errormsg)");
for($account=0,$accounts=array();$account {
if(($line=strtok($passwd[$account], "\n"))== "")
$line=$passwd[$account];
if(($user=strtok($line, ":"))== "")
return( "invalid user in line $account of password file \"$password_file\"");
if($this->convert_to_lower_case)
$user=strtolower($user);
$accounts[$account][ "user"]= "$user";
$accounts[$account][ "password"]=strtok( ":");
}
$this->accounts=$accounts;
$this->password_file=$password_file;
return( "");
}
Function LookupAccount($user)
{
if($this->convert_to_lower_case)
$user=strtolower($user);
for($account=0;$accountaccounts);$account++)
{
if($this->accounts[$account][ "user"]==$user)
break;
}
return($account);
}
Function CheckPassword($user,$password)
{
if($user== "")
return( "empty user");
if($password== "")
return( "empty password");
if($this->convert_to_lower_case)
{
$user=strtolower($user);
$password=strtolower($password);
}
if(($account=$this->LookupAccount($user))accounts))
{
if($this->accounts[$account][ "password"]== "*")
return( "account was deleted");
if($this->accounts[$account][ "password"]==AccountCryptPassword($password,substr($this->accounts[$account]
[ "password"],0,2)))
return( "");
else
return( "password does not match");
}
return( "unknown user");
}
Function AddAccessAccount($user,$password)
{
if($this->convert_to_lower_case)
{
$user=strtolower($user);
$password=strtolower($password);
}
$account=count($this->accounts);
$this->accounts[$account][ "user"]=$user;
$this->accounts[$account][ "password"]=AccountCryptPassword($password, "");
return($account);
}
Function SaveAccessAccounts()
{
global $php_errormsg;
if($this->password_file== "")
return( "it was not defined the password file");
if(($file=@fopen($this->password_file, "w"))==0)
return( "can not open password file \"$password_file\" ($php_errormsg)");
for($account=0;$accountaccounts);$account++)
{
$user=$this->accounts[$account][ "user"];
if($this->convert_to_lower_case)
$user=strtolower($user);
if(fputs($file,sprintf( "%s:%s\n",$user,$this->accounts[$account][ "password"]))==0)
return( "can not write to password file \"$password_file\" ($php_errormsg)");
}
fclose($file);
return( "");
}
Function DeleteAccount($user)
{
if($this->convert_to_lower_case)
$user=strtolower($user);
if(($account=$this->LookupAccount($user))accounts))
{
if($this->accounts[$account][ "password"]!= "*")
{
$this->accounts[$account][ "password"]= "*";
return( "");
}
else
return( "account was already deleted");
}
return( "unknown user");
}
Function ModifyAccessAccount($user,$password)
{
if($this->convert_to_lower_case)
{
$user=strtolower($user);
$password=strtolower($password);
}
if(($account=$this->LookupAccount($user))>=count($this->accounts))
return( "unknown user");
$this->accounts[$account][ "password"]=AccountCryptPassword($password, "");
return( "");
}
};
?>