Location: PHPKode > projects > Club video list website > animelists/userclass.php
<?php
/*
Animelists website
Copyright (C) 2002 Jeff Wartes

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

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
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

-------------------------------------------------

This file is not a stand-alone page, it provides no HTML whatsoever.

Defines the data associated with a user who has regestered on the site,
and how to handle that data.
This is done with two classes:
   User: A single user
   UserList: The entire list of regestered site users

Additionally, this file defines functions that can be used to determine
if one user is allowed delete another user, or increase or decrease their
access level.

$Id: userclass.php,v 1.3 2002/10/20 19:20:59 pyreforge Exp $
*/


if (!isset($COMMONINCLUDED)){
    include("common.php");
}
$USERINCLUDED = true;

class User {
    //File Format: <username>|<password>|<email>|<rank>

    //userINDEX variables:
    //NOTE: The numbering order here MUST match the order in the User->outputDataLine() function!
    var $NAMEINDEX = 0;
    var $PASSINDEX = 1;
    var $EMAILINDEX = 2;
    var $RANKINDEX = 3;


    var $name = "";
    var $pass = "";
    var $email = "";
    var $rank = DEFAULTRANK;

    var $delete = false;  //prevents output functions from returning anything if set to true

    function User($line="", $sep=SEP){
        $line = trim($line);  //remove leading and trailing whitespace
        $fileData = explode($sep, $line);
        //echo "User Constructor called with:" . HTMLSpecialChars($line) . "<br>";  //debug statement

        //used in the "if" tests below to prevent array subscript errors due to
        //improper $line parameter construction
        $fileDataSize = count($fileData);

        if ($line){
            if ($this->NAMEINDEX < $fileDataSize) $this->name = $fileData[$this->NAMEINDEX];
            if ($this->PASSINDEX < $fileDataSize) $this->pass = $fileData[$this->PASSINDEX];
            if ($this->EMAILINDEX < $fileDataSize) $this->email = $fileData[$this->EMAILINDEX];
            if ($this->RANKINDEX < $fileDataSize) $this->rank = $fileData[$this->RANKINDEX];
            if (!$this->rank) $this->rank = DEFAULTRANK;
        }
        //else creates a User with empty data
    }

    function outputDataLine($lineend=NEWLINE) {
        //NOTE: This order MUST match the numbering order of the userINDEX variables!
        if (!$this->delete){
            return $this->name . SEP . $this->pass . SEP . $this->email . SEP . $this->rank . $lineend;
        }
        return "";
    }

    //return true if the parameter username (case insensitive) and password (case sensitive) match
    function validateUser($username, $password){
        if ($this->compareName($username) && $this->comparePass($password)){
            return true;
        }
        return false;
    }
    function compareName($cmpAgainst){
        return (strcasecmp($this->name, $cmpAgainst) == 0);
    }
    function comparePass($cmpAgainst){
        return $this->pass == $cmpAgainst;
    }

    //requires all parameters, using this function means that changes will cause errors in the right places
    //if the number of bits of data in a User change here but not other places.
    function setupUser($name, $pass, $email, $rank=DEFAULTRANK){
        $this->name = $name;
        $this->pass = $pass;
        $this->email = $email;
    }
}

function compareUsers($first, $second){
    return strcasecmp($first->name, $second->name);
}

//rules on whether a promotion/demotion/delete is allowed are encapsulated here.
//relies heavily on constants defined in common.php
function allowPromotion($promotor, $promotee){
    if ( (($promotee->rank < $promotor->rank && !RESTRICTPROMOTION)
           || $promotor->rank >= ADMIN )    //promotor outranks and is allowed to promote
          && $promotee->rank < ADMIN){      //can't promote past ADMIN
        return true;
    }
    return false;
}
function allowDemotion($demotor, $demotee){
    if ($demotor->rank >= $demotee->rank  //demotor is of at least equivilent rank
            && !$demotee->compareName(PROTECTEDUSER)  //nobody can demote the protecteduser
            && !$demotee->compareName($demotor->name) //can't demote yourself
            && $demotee->rank > USER){                //can't demote below USER
        return true;
    }
    return false;
}
function allowDelete($deletor, $deletee){
    if ( ($deletor->rank >= ADMIN     //only admins can delete users
              || $deletor->compareName($deletee->name))  //except that users can delete themselves
          && !$deletee->compareName(PROTECTEDUSER)){     //nobody can delete the protecteduser
        return true;
    }
    return false;
}


class UserList {

    var $users = array();

    function UserList($filename){

        $fileLines = @file($filename); //TODO, insert error checking code here
        if ($fileLines){
            //for each array index (a line in the file), explode the contents using the $sep
            foreach ($fileLines as $line){
                $line = trim($line);
                //remove comment lines and blank lines
                if ($line && !ereg("^;", $line)){
                    $this->users[] = new User($line);
                }
            }
        }
    }
    function addUser($newUser){
        $this->users[] = $newUser;
    }


    //overwrites any old contents in the $filename.
    //NOTE: Pushing  a datafile thru this class deletes any comments and blank lines
    function outputListToFile($filename){
        $fp = fopen($filename, "w");
        if (!$fp) return false;
        fwrite($fp, $this->outputList());
        fclose($fp);
        return true;
    }

    function outputList($type=NEWLINE){
        $output = "";
        usort($this->users, compareUsers);   //TEMPORARY FIX: causes the list to be semi-sorted whenever the list is changed
        foreach($this->users as $aUser){
            $output .= $aUser->outputDataLine();
            if ($type != NEWLINE){
                $output .= $type;
            }
        }
        return $output;
    }

    //search through all the users in $users for a matching user/pass, return true if match was found
    function validateUser($username, $password){
        foreach($this->users as $aUser){
            if ($aUser->validateUser($username, $password)){
                return true;
            }
        }
        return false;

    }

    //replaces the User object in the $this->users array with the matching name field with the parameter User object
    function replaceByName($obj){
        for ($i = 0; $i < count($this->users); $i++){
            if ($this->users[$i]->compareName($obj->name)){
                $this->users[$i] = $obj;
                return true;
            }
        }
        return false;
    }

    function deleteByName($name){
        for ($i = 0; $i < count($this->users); $i++){
            if ($this->users[$i]->compareName($name)){
                $this->users[$i]->delete = true;
                return true;
            }
        }
        return false;
    }

    //returns a copy of the user object that has the matching name field
    function getByName($name){
        foreach($this->users as $aUser){
            if ($aUser->compareName($name)){
                return $aUser;
            }
        }
        return false;
    }
}

?>
Return current item: Club video list website