reenadak icon

class for .htaccess user management

reenadak | PRO | 09/25/17 05:57:54 PM UTC | 0 ⭐ | 307 👁️ | Never ⏰ | []
PHP |

2.02 KB

|

None

|

0 👍

/

0 👎

<?php
 
// Usage
// $htpasswd = new htpasswd('.htpasswd'); 
// path to your .htpasswd file
 
// Usage Example
  //  if($htpasswd->user_update($user,$password))
 //   {
  //    echo "Password updated successfully for '".$user."'\n";
  //  } 
 //   else
 //   {
 //     echo "Password updated failed for '".$user."'\n";
 //   }
 
// Save as .htpasswd.php
class htpasswd {
    var $fp;
    var $filename;
 
    function htpasswd($filename) {
        @$this->fp = fopen($filename,'r+') or die('Invalid file name');
        $this->filename = $filename;
    }
 
    function user_exists($username) {
        rewind($this->fp);
            while(!feof($this->fp) && trim($lusername = array_shift(explode(":",$line = rtrim(fgets($this->fp)))))) {
                if($lusername == $username)
                    return 1;
            }
        return 0;
    }
 
    function user_add($username,$password) {
        if($this->user_exists($username))
            return false;
        fseek($this->fp,0,SEEK_END);
        fwrite($this->fp,$username.':'.crypt($password,substr(str_replace('+','.',base64_encode(pack('N4', mt_rand(),mt_rand(),mt_rand(),mt_rand()))),0,22))."\n");
        return true;
    }
 
    function user_delete($username) {
        $data = '';
        rewind($this->fp);
        while(!feof($this->fp) && trim($lusername = array_shift(explode(":",$line = rtrim(fgets($this->fp)))))) {
            if(!trim($line))
                break;
            if($lusername != $username)
            $data .= $line."\n";
        }
        $this->fp = fopen($this->filename,'w');
        fwrite($this->fp,rtrim($data).(trim($data) ? "\n" : ''));
        fclose($this->fp);
        $this->fp = fopen($this->filename,'r+');
        return true;
    }
 
    function user_update($username,$password) {
        rewind($this->fp);
            while(!feof($this->fp) && trim($lusername = array_shift(explode(":",$line = rtrim(fgets($this->fp)))))) {
                if($lusername == $username) {
                    fseek($this->fp,(-15 - strlen($username)),SEEK_CUR);
                    fwrite($this->fp,$username.':'.crypt($password,substr(str_replace('+','.',base64_encode(pack('N4', mt_rand(),mt_rand(),mt_rand(),mt_rand()))),0,22))."\n");
                    return true;
                }
            }
        return false;
    }
}
 
?>

Comments