用来解析.htgroup文件的PHP类


Posted in PHP onSeptember 05, 2012

.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');
PHP 相关文章推荐
php中判断数组是一维,二维,还是多维的解决方法
May 04 PHP
使用PHP编写的SVN类
Jul 18 PHP
php获取文件大小的方法
Feb 26 PHP
PHP实现获取中英文首字母
Jun 19 PHP
PHP中使用GD库绘制折线图 折线统计图的绘制方法
Nov 09 PHP
php使用glob函数遍历文件和目录详解
Sep 23 PHP
PHP用mysql_insert_id()函数获得刚插入数据或当前发布文章的ID
Nov 25 PHP
php结合redis高并发下发帖、发微博的实现方法
Dec 15 PHP
PHP用PDO如何封装简单易用的DB类详解
Jul 30 PHP
php处理抢购类功能的高并发请求
Feb 08 PHP
PHP ADODB生成HTML表格函数rs2html功能【附错误处理函数用法】
May 29 PHP
解决laravel 表单提交-POST 异常的问题
Oct 15 PHP
PHP curl 并发最佳实践代码分享
Sep 05 #PHP
PHP输出数组中重名的元素的几种处理方法
Sep 05 #PHP
PHP中使用crypt()实现用户身份验证的代码
Sep 05 #PHP
通过缓存数据库结果提高PHP性能的原理介绍
Sep 05 #PHP
PHP中使用foreach和引用导致程序BUG的问题介绍
Sep 05 #PHP
php循环语句 for()与foreach()用法区别介绍
Sep 05 #PHP
PHP手机号码归属地查询代码(API接口/mysql)
Sep 04 #PHP
You might like
php字符编码转换之gb2312转为utf8
2013/10/28 PHP
PHP图像处理之imagecreate、imagedestroy函数介绍
2014/11/19 PHP
PHP简单实现文本计数器的方法
2016/04/28 PHP
js中将多个语句写成一个语句的两种方法小结
2007/12/08 Javascript
基于jquery.Jcrop的头像编辑器
2010/03/01 Javascript
JQuery中serialize()用法实例分析
2015/02/06 Javascript
javascript中scrollTop详解
2015/04/13 Javascript
JS中的Replace方法使用经验分享
2015/05/20 Javascript
jQuery Form 表单提交插件之formSerialize,fieldSerialize,fieldValue,resetForm,clearForm,clearFields的应用
2016/01/23 Javascript
JS获取一个未知DIV高度的方法
2016/08/09 Javascript
Bootstrap基本插件学习笔记之标签切换(17)
2016/12/08 Javascript
Jquery Easyui进度条组件Progress使用详解(8)
2020/03/26 Javascript
angular+bootstrap的双向数据绑定实例
2017/03/03 Javascript
vue 解决循环引用组件报错的问题
2018/09/06 Javascript
vue elementUI 表单校验的实现代码(多层嵌套)
2019/11/06 Javascript
Vue通过for循环随机生成不同的颜色或随机数的实例
2019/11/09 Javascript
小程序自定义圆形进度条
2020/11/17 Javascript
[56:38]DOTA2-DPC中国联赛正赛Aster vs Magma BO3 第一场 3月5日
2021/03/11 DOTA
Python模拟登录12306的方法
2014/12/30 Python
Python常用内置模块之xml模块(详解)
2017/05/23 Python
python数据抓取分析的示例代码(python + mongodb)
2017/12/25 Python
python3下实现搜狗AI API的代码示例
2018/04/10 Python
谈谈Python中的while循环语句
2019/03/10 Python
Python3 chardet模块查看编码格式的例子
2019/08/14 Python
Django错误:TypeError at / 'bool' object is not callable解决
2019/08/16 Python
解决python3 requests headers参数不能有中文的问题
2019/08/21 Python
python 微信好友特征数据分析及可视化
2020/01/07 Python
基于Python第三方插件实现西游记章节标注汉语拼音的方法
2020/05/22 Python
python利用os模块编写文件复制功能——copy()函数用法
2020/07/13 Python
一文解决django 2.2与mysql兼容性问题
2020/07/15 Python
万户网络JAVA程序员岗位招聘笔试试卷
2013/01/08 面试题
咖啡蛋糕店创业计划书
2014/01/28 职场文书
承诺书模板
2014/08/30 职场文书
2014大学生中国梦主题教育学习思想汇报
2014/09/10 职场文书
学校政风行风评议工作总结
2014/10/21 职场文书
医生见习报告范文
2014/11/03 职场文书