NAME

Htgroup - manage httpd Basic Authentication group files


SYNOPSIS

    include("class.Htgroup.php3");
    $htgroup = new Htgroup("/full/path/to/.htgroup");

    $htgroup->addGroup("admins");
    $htgroup->addUserToGroup("cdi","admins");

    if($htgroup->isUserInGroup("cdi","admins"))
    {
        echo "User cdi is a member of the admins group\n";
    }
    else
    {
        echo "User cdi is not a member of the admins group\n";
    }

    // Add multiple users to a group
    $NewUsers = array ("cdi","john","david","susan","tom");
    $htgroup->addGroup("users",$NewUsers);

    // Remove multiple users from a group
    $OldUsers = array("snoopy","daffy","bugs","tweety");
    $htgroup->deleteUserFromGroup($OldUsers,"toons");

    // Remove one user
    $htgroup->deleteUserFromGroup("dilbert","toons");

    if(!$htgroup->isUserInGroup("dilbert","toons"))
    {
        echo "Yup - removed.\n";
    }


DESCRIPTION

This class facilitates the maintenance of htgroup files, used to do Basic Authentication on a web server. This class assumes PHP v3.0 or better and Apache 1.3.3 or better. Htgroup file format should conform to the httpd spec

    groupname: user user user user
    groupname: user user user user user

A groupname, terminated by a colon, followed by one or more users seperated by spaces, and line terminated with a carriage return. Group files that do not conform to the httpd spec will cause strange things to happen.


METHODS


function Htgroup($groupFile = "")

Called from new(), the Htgroup() method requires the full path and filename of the htgroup file to work with. If no groupFile is specified when Htgroup() is called, you must call the initialize() method. See WIN32


function initialize($groupFile)

The initialize() method is only needed if you omit the path and filename from the new Htgroup() method. This method does a quick sanity check on the file to manage, then opens and reads the files contents into memory.


function do_not_blame_cdi()

Calling this method disables sanity checking, which may be required on some WIN32 and the rare *Nix servers.


function sane ($filename)

This is an internal method and should not be called directly from your programs. Performs 4 basic checks on the filename specified.

is_readable(), is_writeable(), is_directory(), is_symlink()

If the first two fail, or the last two succeed, then the file is considered to be tainted and the class will bomb out with a fatal exit. See also ERRORS.


function version()

Just returns the version number of this class.


function error ($errMsg,$die)

This is an internal method and should not be called directly from your program. Depending on the status of the DEBUG variable, it will error_log the string $errMsg, and if $die == 1, will exit the program. The contents of the last error() call can be retrieved from the ERROR variable. See also DEBUG and ERROR.


function htReadFile ()

This is an internal method and should not be called directly from your program. Reads the group file into memory, setting up two global variables. The GROUPS variable and the CONTENTS variable. See also GROUPS.


function isGroup ($GroupID)

Returns true if the group exists in the htgroup file. Returns false otherwise. Group names are case sensitive.


function getGroupNum ($GroupID)

Returns the groupnumber of the specified group, or -1 on failure. The group numbering is zero indexed, so the first group in the htgroup file will be group number zero.


function renameGroup ($GroupID,$NewID)

Renames the specified GroupID to the NewID. Returns true on success. Will return false on failure. Fails if GroupID does not exist, or if NewID already exists.


function addGroup ($GroupID, $Users = "")

addGroup() will add an empty GroupID to the group file. If the second argument Users contains data, it will create the new group and populate it with the user(s) specified. $Users can either be a single user name, or an array of users. Returns true on success, false on failure. Will fail if the GroupID already exists. NOTE: User IDs can not contain whitespace! httpd and Apache delimit users in the group file using a space character. A user of ``John Doe'' will be considered to be two users, ``John'' and ``Doe''.


function isUserInGroup ($UserID, $GroupID)

Returns true if the UserID exists within the specified GroupID. Returns false otherwise.


function getGroups ()

Returns an array listing all the group names from the htgroup file. The array construct is such that both the keys and the values of the returned array will be set to the corresponding group name. Returns null on failure.


function getUsers ()

Similar to getGroups(), getUsers() returns an array of all users found in the group file. Users that appear in more than one group will only be listed once in the returned array. The returned array is similar to getGroups() in that the keys and values of the returned array will be set to the corresponding user name. Returns null on failure.


function getGroupsForUser ($UserID)

Returns an array of group names that the specified user is a member of. Returns null on failure or if the user is not found in any group.


function getUsersForGroup($GroupID)

Returns an array consisting of all the users from a specified group. Returns null if the group has no users, or if the group does not exists.


function addUser ($UserID,$GroupID)

THIS IS AN INTERNAL METHOD - DO NOT USE! - USE addUserToGroup() !


function addUserToGroup ($UserID,$GroupID)

To add a user to a group, you must use this method. If you use the addUser() method despite the massive warning above, hell I'll shoot you myself. Adds a specified user or array of users to the specified group. If the groupID submitted does not exist, it will automatically create the new group. This is a convenience. If you don't want it to create the new group, the make sure to call isGroup() before calling this method. Returns true on success, false on failure. Users that already exist in the specified group will generate a warning (via error() ) but are not considered fatal. (They just get skipped).


function deleteUser($UserID,$GroupID)

THIS IS AN INTERNAL METHOD - DO NOT USE! - USE deleteUserFromGroup() !


function deleteUserFromGroup ($UserID,$GroupID)

This is fairly obvious I should think. UserID can be a single user, or an array of userIDs. Regardless, they all get nuked from the specified GroupID. Returns true on success, false on failure. UserIDs not found in the specified group will generate a warning, but are not considered fatal. (as in addUserToGroup(), they just get skipped)


function deleteGroup ($GroupID)

You got it. Give it a group ID and the entire group gets nuked from the group file. Returns true on success, false on failure. Only returns false if the GroupID doesn't exist.


function htWriteFile ()

THIS IS AN INTERNAL FUNCTION - YOU CALL IT YOU BOUGHT IT


VARIABLES


$WIN32 (boolean, default == false )

Set this to true if you're unlucky enough to be saddled with a NT (Nice Try) server. Prevents the class from doing a lot of cool *Nix stuff.


$FILE (string, htgroup path and filename)

Just holds the path and filename to the file specified by new() or initialize().


$ERROR (string)

String, holds the last message generated by a call to error().


$EMPTY (boolean, default == false)

This is set to true if the file pointed to by $FILE is empty.


$CONTENTS (string, possibly a really BIG string)

This variable holds the raw group file contents. Allows you to perform your own ``stuff'' on the contents. Of course, if you do, you won't be able to save your changes and besides: If you're making your own changes, why the hell are you using this class? :)


$EXISTS (boolean, default == false)

Set to true if the file pointed to by new() or initialize() actually exists.


$SANE (boolean, default == false)

This is set to false if the sane() method fails. Is set to true if sane() succeeds or the do_not_blame_cdi() method is called prior to a sane() call.


$IDIOT (boolean, default == false)

If you call do_not_blame_cdi(), then this variable is set to true. It's left as an exercise for the reader to figure out why.


$DEBUG (boolean, default == true)

If set to true, errors() will generate entries in the error_log via the error_log() function. Set this to false to prevent that. (Usefull if you don't have permission to write to the system log files)


$GROUPS (array(), default == array("") )

After a successful new() or initialize() method is called, this variable contains the raw data from the group file in the following format;

    $GROUPS[$GroupID] = array ($Users)

If you want to write your own methods to manipulate this array directly, I'll ask again, why are you using this class? :) Oh - and since I don't plan on documenting htWriteFile, guess who gets to figure out how to save the changes made to the GROUPS array? < evil grin >


$GROUPCOUNT (integer, default == -1)

The total number of groups found in the group file. If there are 3 groups, GROUPCOUNT will = 3, and the group numbers will be 0, 1 and 2.


EXAMPLES

Sorry, fresh out. See the synopsis.


ERRORS

ERRORS can be retrieved by grabbing the ERROR variable, as in;

    $htgroup->addGroup("fools");
    if(!$htgroup->isGroup("fools"))
    {
        echo "Something barfed, $htgroup->ERROR \n";
    }

The only error that causes Htgroup to actually exit are fatal file write errors. Fatal file write errors will also be echoed to STDOUT. IF DEBUG is true, errors will be logged to the system log files via error_log().


DOCUMENTATION

HAHAHAHAAHAHA!


INSTALLATION

Copy this class to the directory specified by the include_path directive in your php3.ini file. Failing that, drop this into one of your own directories and include() it using the full path.


NOTES

1. Call mom.

2. Get a DSL connection...


BUGS

I didn't look for any, so I didn't find any :)


VERSION

Version 0.4 (BETA) 1999/02/01 CDI, cdi@thewebmasters.net


AUTHOR

Copyright (c) 1999, CDI - cdi@thewebmasters.net, All Rights Reserved.


LICENSE

This program is free software; you can redistribute it and/or modify it under the GNU General Artistic License, with the following stipulations;

Changes or modifications must retain these Copyright statements. Changes or modifications must be submitted to the AUTHOR, cdi@thewebmasters.net.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Artistic License for more details. This software is distributed AS-IS.


AVAILABILITY

Round up the usual suspects...

http://www.thewebmasters.net/php/

http://php.codebase.org/


SEE ALSO

class.Htpasswd.php3 - available at finer bookstores and gift shops everywhere. (Or, check the sites mentioned above)


FILES

Nope.


HISTORY

Long and distinguished I assure you.

Actually, this is the first public release, has not been heavily tested, and should be considered BETA, since I said it was up in the Version section didn't I?