用来解析.htpasswd文件的PHP类
程序员文章站
2022-05-25 20:57:08
.htpasswd 文件示例: user1:{sha}kgpad671vnu0ou5lqlin/h6q6ac= user2:{sha}npmqpex3kpqto+x/+zc...
.htpasswd 文件示例:
user1:{sha}kgpad671vnu0ou5lqlin/h6q6ac=
user2:{sha}npmqpex3kpqto+x/+zckhdricqi=
user3:{sha}q1fh2ltujjkncp11m0m9wuh5zrw=
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}$password\n";
}
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));
}
}
使用方法
$passwdhandler = new htpasswd('/home/myuser/.htpasswd');
// add a user with name 'user1' and password 'i prefer to use passphrase rather than password.' if it doesn't exist in .htpasswd.
$passwdhandler -> adduser('user1', 'i prefer to use passphrase rather than password.');
// 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.
}
user1:{sha}kgpad671vnu0ou5lqlin/h6q6ac=
user2:{sha}npmqpex3kpqto+x/+zckhdricqi=
user3:{sha}q1fh2ltujjkncp11m0m9wuh5zrw=
复制代码 代码如下:
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}$password\n";
}
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));
}
}
使用方法
复制代码 代码如下:
$passwdhandler = new htpasswd('/home/myuser/.htpasswd');
// add a user with name 'user1' and password 'i prefer to use passphrase rather than password.' if it doesn't exist in .htpasswd.
$passwdhandler -> adduser('user1', 'i prefer to use passphrase rather than password.');
// 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.
}
上一篇: Linux基本操作——文件相关
推荐阅读
-
PHP实现的获取文件mimes类型工具类示例
-
PHP操作文件类的函数代码(文件和文件夹创建,复制,移动和删除)
-
php 网页播放器用来播放在线视频的代码(自动判断并选择视频文件类型)
-
适用于初学者的简易PHP文件上传类
-
php.ini 配置文件的深入解析
-
php的XML文件解释类应用实例
-
PHP解析html类库simple_html_dom的转码bug
-
php遍历所有文件及文件夹的方法深入解析
-
C#_Excel数据读取与写入_自定义解析封装类_支持设置标题行位置&使用excel表达式收集数据&单元格映射&标题映射&模板文件的参数数据替换(第二版-增加深度读取和更新功能)
-
用来解析.htpasswd文件的PHP类