_php instance of PHP class used to parse. htgroup files

Source: Internet
Author: User
. htgroup File Example:
Admin:user2
Editor:user1 User3
Writer:user3
Copy Code code as follows:

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;
}
}
}

Copy Code code as follows:

$groupHandler = new Htgroup ('/home/myuser/.htgroup ');
ADD user ' User1 ' to group ' admin ' in. Htgroup. Group would be automatically created if it doesn ' t exist.
$groupHandler-> addusertogroup (' user1 ', ' admin ');
Delete user ' User1 ' from group ' admin ' in. Htgroup. Group would be automatically removed if it doesn ' t contain any users.
$groupHandler-> deleteuserfromgroup (' user1 ', ' admin ');

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.