<?php
#############################################################
# Project:      bCounter                                    #
# File:         class.user.php                              #
# Decription:   user class
#                                                           #
# Copyright 2009 Alexander Bondar (http://bondar.de/)       #
#############################################################

define( "BCOUNTER_USER_UPDATE_STATUS_FROM_DB", 2 );
define( "BCOUNTER_USER_UPDATE_STATUS_LOGIN", 1 );
define( "BCOUNTER_USER_UPDATE_STATUS_LOGOUT", 0 );

class bCounterUser {
    function bCounterUser ( $DB )
    {
        global $bCounter_SID;
        global $bCounter_dbPrefix;
        $this->id        = 0;
#        $this->email     = "";
#        $this->pw_hash   = "";
        $this->role      = ""; # admin
        $this->signed_in = false;

        $this->db        = $DB;
        $this->db_prefix = $bCounter_dbPrefix;
        $this->SID       = $bCounter_SID;

        $this->update_login_status();
    }
/*
    function set_data( $id, $email, $pw_hash, $role ) {
        $this->id        = $id;
        $this->email     = $email;
        $this->pw_hash   = $pw_hash;
        $this->role      = $role;
    }
#*/

    function set_signed_in( $bStatus ) {
        $this->signed_in = $bStatus;
    }

    function is_signed_in() {
        return $this->signed_in;
    }

    function signin ( $username, $pw ) {
        global $SID;
        global $bCounter_users;
        $pw_hash = md5($pw);

#        $bSuccess  = false;
#        $strStatus = "no data";
#        $arrReturn = array();
#        $username = $this->text2db($username);
#        $pw_hash = $this->encrypt($pw);

#        $strSQLstring = "SELECT id FROM ".$this->strDBprefix."users WHERE email = '$username' AND pw_hash='$pw_hash' AND active='1'";
#        $SQLresult = $this->DB->query($strSQLstring);
/*
        if ( mysql_num_rows($SQLresult) === 1 ) {
        # richtige Daten eingegeben
            $UID = mysql_result( $SQLresult, 0, "id" );
            # Login-Log aktualisieren
            #$strSQLstring = "UPDATE ".$this->strDBprefix."login_log SET SID='' WHERE sID='$SID'"; #nötig?
            $strSQLstring = "INSERT INTO ".$this->strDBprefix."login_log (uID, sID, timestamp)
VALUES('$UID', '$SID', '".time()."')";
            if ( $this->DB->query($strSQLstring) ) {
                $arrReturn[0] = "ok";
            }
        }

        if ( count($arrReturn) == 0 ) {
            $arrReturn[0] = "error";
            $arrReturn[1] = $strStatus;
        }
        return $arrReturn;
*/
        foreach ( $bCounter_users as $user ) {
            if ( $user["username"] == $username && $user["pw_hash"] == $pw_hash ) {
                #$this->set_signed_in( true );
                $this->update_login_status( BCOUNTER_USER_UPDATE_STATUS_LOGIN, $user["id"] );
                return true;
            }
        }
        return false;
    }

    function update_login_status ( $iAction = BCOUNTER_USER_UPDATE_STATUS_FROM_DB, $UID = 0 ) { #, $bLogegedIn = false
#        echo "<br>update_login_status<br>";
        switch ( $iAction ) {
            case BCOUNTER_USER_UPDATE_STATUS_FROM_DB:
                # get data from DB
                $strSQLstring = "SELECT id, SID, UID FROM ".$this->db_prefix."login_log WHERE sID='".$this->SID."' AND timestamp > DATE_SUB( NOW(), INTERVAL 2 DAY);";
                $SQLresult = $this->db->query($strSQLstring);
                if ( @mysql_num_rows($SQLresult) === 1 ) {
                    $this->id        = (int) mysql_result( $SQLresult, 0, "uID" );
                    $this->set_signed_in( true );

                    # update the timestamp
                    $strSQLstring = "UPDATE ".$this->db_prefix."login_log SET timestamp=NOW() WHERE id='".mysql_result( $SQLresult, 0, "id" )."';";
                    $this->db->query($strSQLstring);
                }
                break;
            case BCOUNTER_USER_UPDATE_STATUS_LOGIN:
                # log in user with id UID:
                # clear all data sets with current SID and UID
                $strSQLstring = "DELETE FROM ".$this->db_prefix."login_log WHERE sID='".$this->SID."' OR uID='$UID';";
                $this->db->query($strSQLstring);

                # insert current user data set
                $strSQLstring = "INSERT INTO ".$this->db_prefix."login_log (sID, uID) VALUES('".$this->SID."', '$UID');";
                if ( $this->db->query($strSQLstring) ) {
                    $this->set_signed_in( true );
                }
                break;
            case BCOUNTER_USER_UPDATE_STATUS_LOGOUT:
                # log out user with id UID
                # clear all data sets with current SID
                $strSQLstring = "DELETE FROM ".$this->db_prefix."login_log WHERE sID='".$this->SID."';";
                if ( $this->db->query($strSQLstring) ) {
                    $this->set_signed_in( false );
                }
                break;
        }
/*
        global $SID;
        $timestamp = time()-1*60*60; # 1h ago
        
        $strSQLstring = "SELECT id, uID FROM ".$this->strDBprefix."login_log WHERE sID='$SID' AND timestamp>$timestamp ORDER BY id DESC LIMIT 1";
        $SQLresult = $this->DB->query($strSQLstring);
        if ( mysql_num_rows( $SQLresult ) === 1 ) {
            $iID = mysql_result( $SQLresult, 0, "id" );
            $UID = mysql_result( $SQLresult, 0, "uid" );
            $strSQLstring = "UPDATE ".$this->strDBprefix."login_log SET timestamp='".time()."' WHERE sid='$SID' LIMIT 1";
            if ( $this->DB->query($strSQLstring) ) {
                return $UID;
            }
        }
        return 0;
*/
    }

    function signout () {
        $this->update_login_status(BCOUNTER_USER_UPDATE_STATUS_LOGOUT);
    }
}
?>