<?php
require_once("database.php");
include("form.php");
class Session
{
var $username;     //Username given on sign-up
var $userlevel;    //The level to which the user pertains
var $time;         //Time user was last active (page loaded)
var $logged_in;    //True if user is logged in, false otherwise
var $userinfo = array();  //The array holding all user info

var $url;          //The page url current being viewed
var $referrer;     //Last recorded site page viewed
 
/**
* Note: referrer should really only be considered the actual
* page referrer in process.php, any other time it may be
* inaccurate.
*/
/* Class constructor */
function Session()
{
    $this->time = time();
    $this->startSession();
}
/**
* startSession - Performs all the actions necessary to 
* initialize this session object. Tries to determine if the
* the user has logged in already, and sets the variables 
* accordingly. Also takes advantage of this page load to
* update the active visitors tables.
*/
function startSession()
{
    global $database;  //The database connection
    session_start();   //Tell PHP to start the session
    /* Determine if user is logged in */
    $this->logged_in = $this->checkLogin();
  
    /* Set referrer page */
    if(isset($_SESSION['url'])){
    $this->referrer = $_SESSION['url'];
    }else{
    $this->referrer = "/";
    }
    /* Set current url */
    $this->url = $_SESSION['url'] = $_SERVER['PHP_SELF'];
}
/**
* checkLogin - Checks if the user has already previously
* logged in, and a session with the user has already been
* established. Also checks to see if user has been remembered.
* If so, the database is queried to make sure of the user's 
* authenticity. Returns true if the user has logged in.
*/
function checkLogin()
{
    global $database;  //The database connection
    /* Check if user has been remembered */
    if(isset($_COOKIE['cookname']) && isset($_COOKIE['cookid'])){
    $this->username = $_SESSION['username'] = $_COOKIE['cookname'];
    $this->userid   = $_SESSION['userid']   = $_COOKIE['cookid'];
    } 
    /* Username has been set*/
    if(isset($_SESSION['username'])){
        
    /* User is logged in, set class variables */
    $this->userinfo  = $database->getUserInfo($_SESSION['username']);
    $this->username  = $this->userinfo['username'];
    $this->userlevel = $this->userinfo['userLevel'];
    return true;
    }
    /* User not logged in */
    else{
    return false;
    }
}
/**
* login - The user has submitted his username and password
* through the login form, this function checks the authenticity
* of that information in the database and creates the session.
* Effectively logging in the user if all goes well.
*/
function login($subuser, $subpass)
{
    global $database, $form;  //The database and form object
    /* Username error checking */
    $field = "user";  //Use field name for username
    if(!$subuser || strlen($subuser = trim($subuser)) == 0){
    $form->setError($field, "Invalid Input");
    }
    /* Password error checking */
    $field = "pass";  //Use field name for password
    if(!$subpass){
    $form->setError($field, "Password is missing");
    }
    /* Return if form errors exist */
    if($form->num_errors > 0){
    return false;
    }
    /* Checks that username is in database and password is correct */
    $subuser = stripslashes($subuser);
    $result = $database->confirmUserPass($subuser, md5($subpass));
    /* Check error codes */
    if($result == 1){
    $field = "user";
    $form->setError($field, "Username is not correct. Please try again.");
    }
    else if($result == 2){
    $field = "pass";
    $form->setError($field, "Password is not correct. Please try again.");
    }
    /* Return if form errors exist */
    if($form->num_errors > 0){
    return false;
    }
    /* Username and password correct, register session variables */
    $this->userinfo  = $database->getUserInfo($subuser);
   
    $this->username  = $_SESSION['username'] = $this->userinfo['username'];
    $this->userlevel = $this->userinfo['userlevel'];
   
    /* Login completed successfully */
    return true;
}
//logout - Gets called when the user wants to be logged
function logout()
{
    global $database;  //The database connection
    /* Unset PHP session variables */
    unset($_SESSION['username']);
    /* Reflect fact that user has logged out */
    $this->logged_in = false;

    session_destroy();
}
 

};
/**
* Initialize session object - This must be initialized before
* the form object because the form uses session variables,
* which cannot be accessed unless the session has started.
*/
$session = new Session;
/* Initialize form object */
$form = new Form;
?>
