Zend - The PHP Company




Security

Add Code


.htpasswd file manipulation class  

Type: class
Added by: DeckZ
Entered: 19/09/2000
Last modified: 01/12/2000
Rating: **** (5 votes)
Views: 17568
The class can be used for manipulating unix password files, created with htpasswd. It is possible to add, remove and update users.


<?

// pwfile.req


class PwFile {

    var 
$fp// filepointer
    
var $filename;
    

    
    function 
PwFile($filename// constructor
    
{
        
$this->fp fopen ($filename,"r+");
        
$this->filename $filename;
    }
       
    function 
existsUser ($usr_ID)
    {
    
rewind($this->fp); //rewind filepointer
    
while (feof($this->fp) == 0
    {
        
$line chop(fgets($this->fp,1000));  //read line from file
              
$arr split(":"$line);  //slit at ':'
              
if ($arr[0] == $usr_ID
              {
              return 
1// username found
              
}
              else 
              {
              return 
0;  // username not found
              
}
          }
    }
    
    function 
getFileSize() 
    {
        return (
filesize($this->filename));
    }

    function 
write_temp ($contents)
    {
        echo 
"<pre>$contents</pre>";
      
$temp_filename tempnam ("/tmp""php_");
      echo 
"<br>$temp_filename<br>";
         
$temp_fp fopen($temp_filenamew);
         
fwrite($temp_fp$contents);
         
fclose($temp_fp);
         
fclose($this->fp);
         
copy ($temp_filename$this->filename);
         
$this->fp fopen($this->filename"r+");
         
//unlink($temp_filename);
    
}
     
    function 
addUser ($usr_ID$usr_passwd)
    {
    
rewind($this->fp);
    
$contents fread($this->fp$this->getFileSize());
         echo 
"<pre>bevor $contents</pre>";
         
$contents .= "$usr_ID:".crypt($usr_passwd)."n";
         echo 
"<pre>danach $contents</pre>";
         
$this->write_temp($contents);
    }
    
    function 
deleteUser ($usr_ID
    {
    
$contents "";
    
rewind($this->fp);
    while (
feof($this->fp) == 0
    {
        
$line "";
        
$line chop(fgets($this->fp,1000)); 
              
$arr split(":"$line); 
              if (
$arr[0] != $usr_ID)
              {
                  echo 
$arr[0] ." ist nicht gleich "$usr_ID;
              
$contents .= $line "n";
              }
          }
          echo 
"<hr>$contents<hr>";
          
$contents chop ($contents); 
          
$contents .= "n"// kind a tricky
          
$this->write_temp($contents);
          
    }
    
    function 
updateUser ($usr_ID$usr_passwd
    {
        
$this->deleteUser($usr_ID);
        
$this->addUser($usr_ID,$usr_passwd);
    }
}
?>


Usage Example


<?
    
echo "Testfile f�r passwd-file Manipulation";
    require(
"pwfile.req");
    
$pw = new PwFile("/var/cgi-data/passwd");    
    
$usr_ID "martind";
    
$usr_passwd "sdfasdf";    
    
//$pw->addUser($usr_ID, $usr_passwd);
    //$pw->deleteUser($usr_ID);
    
$pw->updateUser($usr_ID$usr_passwd);
?>


Rate This Script





Search



This Category All Categories