用来解析.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之第九天
Oct 09 PHP
mysql下创建字段并设置主键的php代码
May 16 PHP
php Static关键字实用方法
Jun 04 PHP
Wordpress 相册插件 NextGEN-Gallery 添加目录将中文转为拼音的解决办法
Dec 29 PHP
PHP中PDO的错误处理
Sep 04 PHP
基于PHP生成静态页的实现方法
May 10 PHP
深入解析PHP垃圾回收机制对内存泄露的处理
Jun 14 PHP
PHP使用xmllint命令处理xml与html的方法
Dec 15 PHP
微信公众平台开发实现2048游戏的方法
Apr 15 PHP
php简单实现发送带附件的邮件
Jun 10 PHP
深入理解PHP的远程多会话调试
Sep 21 PHP
php加速缓存器opcache,apc,xcache,eAccelerator原理与配置方法实例分析
Mar 02 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
优化WordPress中文章与评论的时间显示
2016/01/12 PHP
PHP请求远程地址设置超时时间的解决方法
2016/10/29 PHP
PHP获取路径和目录的方法总结【必看篇】
2017/03/04 PHP
php readfile下载大文件失败的解决方法
2017/05/22 PHP
xmlHTTP实例
2006/10/24 Javascript
利用JQuery为搜索栏增加tag提示
2009/06/22 Javascript
jquery tools之tooltip
2009/07/25 Javascript
js 优化次数过多的循环 考虑到性能问题
2011/03/05 Javascript
Javascript 面向对象(一)(共有方法,私有方法,特权方法)
2012/05/23 Javascript
JS定时器实例
2013/04/17 Javascript
基于JavaScript实现继承机制之调用call()与apply()的方法详解
2013/05/07 Javascript
在AngularJS中使用jQuery的zTree插件的方法
2016/04/21 Javascript
AngularJS动态菜单操作指令
2017/04/25 Javascript
vue+axios新手实践实现登陆的示例代码
2018/06/06 Javascript
cocos2dx+lua实现橡皮擦功能
2018/12/20 Javascript
JavaScript实现英语单词题库
2019/12/24 Javascript
原生js实现无缝轮播图
2020/01/11 Javascript
微信浏览器下拉黑边解决方案 wScroollFix
2020/01/21 Javascript
wxpython 最小化到托盘与欢迎图片的实现方法
2014/06/09 Python
编写Python脚本来实现最简单的FTP下载的教程
2015/05/04 Python
Python数据类型详解(一)字符串
2016/05/08 Python
Python实现七彩蟒蛇绘制实例代码
2018/01/16 Python
详解Django中间件执行顺序
2018/07/16 Python
详解python分布式进程
2018/10/08 Python
django 微信网页授权认证api的步骤详解
2019/07/30 Python
Python3实现mysql连接和数据框的形成(实例代码)
2020/01/17 Python
python中二分查找法的实现方法
2020/12/06 Python
阿里健康大药房:阿里自营网上药店
2017/08/01 全球购物
美国时尚在线:Showpo
2017/09/08 全球购物
Under Armour瑞典官方网站:美国高端运动科技品牌
2018/11/21 全球购物
StubHub新西兰:购买和出售你的门票
2019/04/22 全球购物
周年庆典邀请函范文
2014/01/24 职场文书
班级课外活动总结
2014/07/09 职场文书
四风剖析查摆对照检查材料思想汇报
2014/09/24 职场文书
2015年光棍节活动总结
2015/03/24 职场文书
2015小学五年级班主任工作总结
2015/05/21 职场文书