<?php
/* Author: Leon Bukhman *******************************************/
/* User Class *****************************************************/

/******************************************************************
This class encapsulates the session information of a logged in
user. It performs authentication and session managment.

Public Methods:
    login($username, $password)
        login a user with name and pw
        returns boolean

    logout()
        clear the session

    checkPerm($role)
        check if user has enough permissions to view page
        $role is the role required to view a particular page

Private Methods:
    _checkSession()
        check if the current session is valid (otherwise logout)

    _setSession(&$values)
        set the current session
        $values is a pear db row

    _updateRecord ()
        Update session data on the server
        Called when a user logs in (first page hit)

Usage:
    $user->login($name,$pw,$remember)
    $user->checkPerm(n) //for n=1,2,3

Requires:
    functions.php
    DB.php

*******************************************************************/

//session has not been established
if (!isset($_SESSION['UserID']) ) {
    
set_session_defaults();
}

//reset session values
function set_session_defaults() {
    
$_SESSION['UserID'] = '0';          //User ID
    
$_SESSION['Login'] = '';            //LoginName
    
$_SESSION['UserName'] = '';         //UserName
    
$_SESSION['Role'] = '0';            //Role

    
$_SESSION['LoggedIn'] = false;      //is user logged in
    
$_SESSION['Persistent'] = false;    //is persistent cookie set
}

class 
User {
    
/*Local Atributes**********************************************/
    
var $db null;     // pointer to PEAR::DB object
    
var $id 0;        // ID of current user

    /*Constructor**************************************************/
    
function User(&$db) {
        
$this->db = &$db;
        
        if (!
defined('NO_DB') && $_SESSION['LoggedIn']) {
            
$this->_checkSession();
        }
    }

    
/*Public Methods***********************************************/

    //Login a user with name and pw.
    //Returns Boolean
    
function login($username$password) {
        
$md5pw    md5($password);
        
$username $this->db->quote($username);
        
$password $this->db->quote($password);
        
$sql "SELECT * FROM tblUsers WHERE
                Login = $username AND
                Password = md5($password)"
;

        
$result $this->db->getRow($sql);

        
//check if pw is correct again (prevent sql injection)
        
if ($result and $result['Password'] == $md5pw) {
            
$this->_setSession($result);
            
$this->_updateRecord();     //update session info in db
            
return true;
        } else {
            
set_session_defaults();
            return 
false;
        }
    }

    
//Logout the current user (reset session)
    
function logout() {
        
$_SESSION = array();                //clear session
        
unset($_COOKIE[session_name()]);    //clear cookie
        
session_destroy();                  //kill the session
        //header('Location: '.BASE_URL);    //redirect to clear cookie
    
}

    
//check if user has enough permissions
    //$role is the minimum level required for entry
    //Returns Boolean
    
function checkPerm($role) {
        if (
$_SESSION['LoggedIn']) {
            if (
$_SESSION['Role']>=$role) {
                return 
true;
            } else {
                return 
false;
            }
        } else {
            return 
false;
        }
    }

    
/*Private Methods**********************************************/

    //check if the current session is valid (otherwise logout)
    
function _checkSession() {
        
$login   $this->db->quote($_SESSION['Login']);
        
$role    $this->db->quote($_SESSION['Role']);
        
$session $this->db->quote(session_id());
        
$ip      $this->db->quote($_SERVER['REMOTE_ADDR']);

        
$sql "SELECT * FROM tblUsers WHERE
                Login = $login AND
                Role = $role AND
                SessionID = $session AND
                SessionIP = $ip"
;

        
$result $this->db->getRow($sql);

        if (
$result) {
            
$this->_setSession($result);
        } else {
            
$this->logout();
        }
    }

    
//Set the current session
    //$values is a pear db row
    
function _setSession(&$values) {
        
$this->id $values['UserID'];
        
$_SESSION['UserID'] = $this->id;
        
$_SESSION['Login']  = htmlspecialchars($values['Login']);
        
$_SESSION['Role']   = htmlspecialchars($values['Role']);
        
$_SESSION['UserName'] = htmlspecialchars($values['FirstName'].' '.$values['LastName']);
        
$_SESSION['LoggedIn'] = true;
    }

    
//Update session data on the server
    
function _updateRecord () {
        
$session $this->db->quote(session_id());
        
$ip      $this->db->quote($_SERVER['REMOTE_ADDR']);

        
$sql "UPDATE tblUsers SET
                    LastLogon = CURRENT_DATE,
                    SessionID = $session,
                    SessionIP = $ip
                WHERE UserID = $this->id"
;
        
$this->db->query($sql);
    }

//end of class User
?>