用來解析.htgroup文件的PHP類
更新時間:2012年09月05日 23:16:59 作者:
用來解析.htgroup文件的PHP類代碼,需要的朋友可以參考下
.htgroup 文件示例:
admin: user2
editor: user1 user3
writer: user3
class Htgroup {
private $file = '';
private function write($groups = array()) {
$str = '';
foreach ($groups as $group => $users) {
$users_str = '';
foreach ($users as $user) {
if (!empty($users_str)) {
$users_str .= ' ';
}
$users_str .= $user;
}
$str .= "$group: $users_str\n";
}
file_put_contents($this -> file, $str);
}
private function read() {
$groups = array();
$groups_str = file($this -> file, FILE_IGNORE_NEW_LINES);
foreach ($groups_str as $group_str) {
if (!empty($group_str)) {
$group_str_array = explode(': ', $group_str);
if (count($group_str_array) == 2) {
$users_array = explode(' ', $group_str_array[1]);
$groups[$group_str_array[0]] = $users_array;
}
}
}
return $groups;
}
public function __construct($file) {
if (file_exists($file)) {
$this -> file = $file;
} else {
die($file." doesn't exist.");
return false;
}
}
public function addUserToGroup($username = '', $group = '') {
if (!empty($username) && !empty($group)) {
$all = $this -> read();
if (isset($all[$group])) {
if (!in_array($username, $all[$group])) {
$all[$group][] = $username;
}
} else {
$all[$group][] = $username;
}
$this -> write($all);
} else {
return false;
}
}
public function deleteUserFromGroup($username = '', $group = '') {
$all = $this -> read();
if (array_key_exists($group, $all)) {
$user_index = array_search($username, $all[$group]);
if ($user_index !== false) {
unset($all[$group][$user_index]);
if (count($all[$group]) == 0) {
unset($all[$group]);
}
$this -> write($all);
}
} else {
return false;
}
}
}
$groupHandler = new Htgroup('/home/myuser/.htgroup');
// Add user 'user1' to group 'admin' in .htgroup. Group will be automatically created if it doesn't exist.
$groupHandler -> addUserToGroup('user1', 'admin');
// Delete user 'user1' from group 'admin' in .htgroup. Group will be automatically removed if it doesn't contain any users.
$groupHandler -> deleteUserFromGroup('user1', 'admin');
admin: user2
editor: user1 user3
writer: user3
復(fù)制代碼 代碼如下:
class Htgroup {
private $file = '';
private function write($groups = array()) {
$str = '';
foreach ($groups as $group => $users) {
$users_str = '';
foreach ($users as $user) {
if (!empty($users_str)) {
$users_str .= ' ';
}
$users_str .= $user;
}
$str .= "$group: $users_str\n";
}
file_put_contents($this -> file, $str);
}
private function read() {
$groups = array();
$groups_str = file($this -> file, FILE_IGNORE_NEW_LINES);
foreach ($groups_str as $group_str) {
if (!empty($group_str)) {
$group_str_array = explode(': ', $group_str);
if (count($group_str_array) == 2) {
$users_array = explode(' ', $group_str_array[1]);
$groups[$group_str_array[0]] = $users_array;
}
}
}
return $groups;
}
public function __construct($file) {
if (file_exists($file)) {
$this -> file = $file;
} else {
die($file." doesn't exist.");
return false;
}
}
public function addUserToGroup($username = '', $group = '') {
if (!empty($username) && !empty($group)) {
$all = $this -> read();
if (isset($all[$group])) {
if (!in_array($username, $all[$group])) {
$all[$group][] = $username;
}
} else {
$all[$group][] = $username;
}
$this -> write($all);
} else {
return false;
}
}
public function deleteUserFromGroup($username = '', $group = '') {
$all = $this -> read();
if (array_key_exists($group, $all)) {
$user_index = array_search($username, $all[$group]);
if ($user_index !== false) {
unset($all[$group][$user_index]);
if (count($all[$group]) == 0) {
unset($all[$group]);
}
$this -> write($all);
}
} else {
return false;
}
}
}
復(fù)制代碼 代碼如下:
$groupHandler = new Htgroup('/home/myuser/.htgroup');
// Add user 'user1' to group 'admin' in .htgroup. Group will be automatically created if it doesn't exist.
$groupHandler -> addUserToGroup('user1', 'admin');
// Delete user 'user1' from group 'admin' in .htgroup. Group will be automatically removed if it doesn't contain any users.
$groupHandler -> deleteUserFromGroup('user1', 'admin');
相關(guān)文章
全新的PDO數(shù)據(jù)庫操作類php版(僅適用Mysql)
在公司里也用了1年之久。如今公司規(guī)模變大了,產(chǎn)品也日益完善,曾經(jīng)的那個數(shù)據(jù)庫操作函數(shù)雖說使用上沒出什么大問題,但為了更顯專業(yè),花了1天時間重寫了這個,現(xiàn)在,它確實(shí)是個類了2012-07-07php提取身份證號碼中的生日日期以及驗(yàn)證是否為成年人的函數(shù)
本篇文章使用php技術(shù)提取身份證號碼中的生日日期來判斷是否是未成年人的一個函數(shù)。下面小編把代碼分享給大家,供大家參考2015-09-09php curl抓取網(wǎng)頁的介紹和推廣及使用CURL抓取淘寶頁面集成方法
抓取網(wǎng)頁內(nèi)容,分析網(wǎng)頁數(shù)據(jù)經(jīng)常使用php curl,簡潔易用,本篇文章通過代碼實(shí)例給大家講解 php curl抓取網(wǎng)頁的介紹和推廣及使用CURL抓取淘寶頁面集成方法,需要的朋友參考下2015-11-11PHP文件及文件夾操作之創(chuàng)建、刪除、移動、復(fù)制
這篇文章主要介紹了PHP文件及文件夾操作之創(chuàng)建、刪除、移動、復(fù)制的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-07-07Laravel 自動生成驗(yàn)證的實(shí)例講解:login / logout
今天小編就為大家分享一篇Laravel 自動生成驗(yàn)證的實(shí)例分析:login / logout,具有好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10PHP常用技術(shù)文之文件操作和目錄操作總結(jié)
這篇文章主要介紹了PHP常用技術(shù)文之文件操作和目錄操作總結(jié),本文講解了基本文件的操作、目錄的操作等內(nèi)容,需要的朋友可以參考下2014-09-09