PHP class for parsing. htpasswd files in Linux

Source: Internet
Author: User

Introduce a
How to use

$

The code is as follows Copy Code
Passwdhandler = new htpasswd ('/home/myuser/.htpasswd ');
Add a user with name ' user1 ' and password ' I prefer to-use passphrase rather than. ' If it password ' t doesn in. ht passwd.
$passwdHandler-> addUser (' user1 ', ' I prefer to use passphrase rather ' than; ');
Delete the user ' User1 ' if it exists in. htpasswd.
$passwdHandler-> deleteuser (' user1 ');
Check if user ' User1 ' exists in. htpasswd.
if ($passwdHandler-> doesuserexist (' user1 ')) {
User ' user1 ' exists.
}

HTPASSWD class

The code is as follows Copy Code

Class HTPASSWD {
Private $file = ';
Private $salt = ' aynlj2h.74vefi^bzelc-vb6g0eze9a55-wj ';
Private Function Write ($pairs = Array ()) {
$str = ';
foreach ($pairs as $username => $password) {
$str. = "$username: {SHA} $passwordn";
}
File_put_contents ($this-> file, $str);
}
Private Function Read () {
$pairs = Array ();
$fh = fopen ($this-> file, ' R ');
while (!feof ($FH)) {
$pair _str = Str_replace ("n", "", Fgets ($FH));
$pair _array = Explode (': {SHA} ', $pair _str);
if (count ($pair _array) = = 2) {
$pairs [$pair _array[0]] = $pair _array[1];
}
}
return $pairs;
}
Private Function Gethash ($clear _password = ') {
if (!empty ($clear _password)) {
Return Base64_encode (SHA1 ($clear _password, true));
} else {
return false;
}
}
Public function __construct ($file) {
if (file_exists ($file)) {
$this-> file = $file;
} else {
Die ($file. "Doesn ' t exist.");
return false;
}
}
Public Function addUser ($username = ', $clear _password = ') {
if (!empty ($username) &&!empty ($clear _password)) {
$all = $this-> read ();
if (!array_key_exists ($username, $all)) {
$all [$username] = $this-> gethash ($clear _password);
$this-> Write ($all);
}
} else {
return false;
}
}
Public Function deleteuser ($username = ' ") {
$all = $this-> read ();
if (Array_key_exists ($username, $all)) {
Unset ($all [$username]);
$this-> Write ($all);
} else {
return false;
}
}
Public Function doesuserexist ($username = ' ") {
$all = $this-> read ();
if (Array_key_exists ($username, $all)) {
return true;
} else {
return false;
}
}
Public Function Getclearpassword ($username) {
Return Strtolower (substr SHA1 ($username. $this-> Salt), 4, 12));
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.