<?php

class login
{
    //FUNCTION to login user

    function is_login($user_name, $pwd)
    {

        global $db, $db1, $db2;
        $username = $user_name;
        $password = $pwd;
        $ip = $_SERVER['REMOTE_ADDR'];
        $XIP = @getenv("HTTP_X_FORWARDED_FOR");
        $query = "SELECT * FROM " . TABLEUSETUP . " WHERE (BINARY username='$username' or BINARY email='$username')
				 AND BINARY password='$password' and status='active' and (type IN ('administrator', 'profile'))";
        //print($query); exit;
        $record = $db->query_first($query);
        $recorddb = $db1->query_first($query);
        $recorddbtx = $db2->query_first($query);

        if ($record->status == "active") {
            if ($db->affected_rows > 0) {


                $_SESSION['sess_userid'] = $record->us_id;
                //$_SESSION['sess_name'] = $record->fullname;
                $_SESSION['sess_username'] = $record->username;
                $_SESSION['sess_email'] = $record->email;
                $_SESSION['sess_type'] = $record->type;
                $_SESSION['sess_role'] = $record->role;
                //$_SESSION['sess_associatedid'] = $record->associatedid;

                $data['userid'] = $record->us_id;
                $data['email'] = $username;
                $data['type'] = $record->type;
                $data['ipaddress'] = $ip;
                $data['lastupdated'] = "NOW()";
                $data['status'] = "LOGIN SUCCESS";
                $db->query_insert(TABLE_LOGINHISTORY, $data);

                $data2['userid'] = $_SESSION['sess_userid'];
                $data2['action'] = 'LOGIN SUCCESS';
                $data2['type'] = $_SESSION['sess_type'];
                $data2['description'] = $_SESSION['sess_username'] . " has successfully login ";
                $data2['ip'] = $_SERVER['REMOTE_ADDR'];
                $data2['xip'] = @getenv("HTTP_X_FORWARDED_FOR");
                $data2['lastupdated'] = 'now()';
                //print_r($data2);exit;
                //$db->query_insert("vrs_logs", $data2);

                return true;

            } else {

                $data['userid'] = 0;
                $data['email'] = $username;
                $data['type'] = 0;
                $data['ipaddress'] = $ip;
                $data['lastupdated'] = "NOW()";
                $data['status'] = "LOGIN FAILED";
                $db->query_insert(TABLE_LOGINHISTORY, $data);

                $data2['userid'] = 0;
                $data2['action'] = 'LOGIN FAILED';
                $data2['type'] = 0;
                $data2['description'] = $username . " try to login but failed. ";
                $data2['ip'] = $_SERVER['REMOTE_ADDR'];
                $data2['xip'] = @getenv("HTTP_X_FORWARDED_FOR");
                $data2['lastupdated'] = 'now()';

                //$db->query_insert("vrs_logs", $data2);


                return false;

            }

        }
        elseif ($recorddb->status == "active") {
            $db = $db1;
            global $db;
            if ($db1->affected_rows > 0) {
                $_SESSION['nextclass'] = 1;
                $_SESSION['sess_userid'] = $recorddb->us_id;
                //$_SESSION['sess_name'] = $record->fullname;
                $_SESSION['sess_username'] = $recorddb->username;
                $_SESSION['sess_email'] = $recorddb->email;
                $_SESSION['sess_type'] = $recorddb->type;
                $_SESSION['sess_role'] = $recorddb->role;
                $_SESSION['clone'] = 1;
                //$_SESSION['sess_associatedid'] = $record->associatedid;

                $data['userid'] = $recorddb->us_id;
                $data['email'] = $username;
                $data['type'] = $recorddb->type;
                $data['ipaddress'] = $ip;
                $data['lastupdated'] = "NOW()";
                $data['status'] = "LOGIN SUCCESS";
                $db->query_insert(TABLE_LOGINHISTORY, $data);

                $data2['userid'] = $_SESSION['sess_userid'];
                $data2['action'] = 'LOGIN SUCCESS';
                $data2['type'] = $_SESSION['sess_type'];
                $data2['description'] = $_SESSION['sess_username'] . " has successfully login ";
                $data2['ip'] = $_SERVER['REMOTE_ADDR'];
                $data2['xip'] = @getenv("HTTP_X_FORWARDED_FOR");
                $data2['lastupdated'] = 'now()';
                //print_r($data2);exit;
                //$db->query_insert("vrs_logs", $data2);
                return true;
            } else {
                $data['userid'] = 0;
                $data['email'] = $username;
                $data['type'] = 0;
                $data['ipaddress'] = $ip;
                $data['lastupdated'] = "NOW()";
                $data['status'] = "LOGIN FAILED";
                $db->query_insert(TABLE_LOGINHISTORY, $data);

                $data2['userid'] = 0;
                $data2['action'] = 'LOGIN FAILED';
                $data2['type'] = 0;
                $data2['description'] = $username . " try to login but failed. ";
                $data2['ip'] = $_SERVER['REMOTE_ADDR'];
                $data2['xip'] = @getenv("HTTP_X_FORWARDED_FOR");
                $data2['lastupdated'] = 'now()';
                //$db->query_insert("vrs_logs", $data2);
                return false;
            }

        }
        elseif ($recorddbtx->status == "active") {
            $db = $db2;
            global $db;
            if ($db->affected_rows > 0) {
                $_SESSION['nextclass'] = 2;
                $_SESSION['sess_userid'] = $recorddbtx->us_id;
                //$_SESSION['sess_name'] = $record->fullname;
                $_SESSION['sess_username'] = $recorddbtx->username;
                $_SESSION['sess_email'] = $recorddbtx->email;
                $_SESSION['sess_type'] = $recorddbtx->type;
                $_SESSION['sess_role'] = $recorddbtx->role;
                $_SESSION['clone'] = 1;
                //$_SESSION['sess_associatedid'] = $record->associatedid;

                $data['userid'] = $recorddbtx->us_id;
                $data['email'] = $username;
                $data['type'] = $recorddbtx->type;
                $data['ipaddress'] = $ip;
                $data['lastupdated'] = "NOW()";
                $data['status'] = "LOGIN SUCCESS";
                $db->query_insert(TABLE_LOGINHISTORY, $data);

                $data2['userid'] = $_SESSION['sess_userid'];
                $data2['action'] = 'LOGIN SUCCESS';
                $data2['type'] = $_SESSION['sess_type'];
                $data2['description'] = $_SESSION['sess_username'] . " has successfully login ";
                $data2['ip'] = $_SERVER['REMOTE_ADDR'];
                $data2['xip'] = @getenv("HTTP_X_FORWARDED_FOR");
                $data2['lastupdated'] = 'now()';
                //print_r($data2);exit;
                //$db->query_insert("vrs_logs", $data2);
                return true;
            } else {
                $data['userid'] = 0;
                $data['email'] = $username;
                $data['type'] = 0;
                $data['ipaddress'] = $ip;
                $data['lastupdated'] = "NOW()";
                $data['status'] = "LOGIN FAILED";
                $db->query_insert(TABLE_LOGINHISTORY, $data);

                $data2['userid'] = 0;
                $data2['action'] = 'LOGIN FAILED';
                $data2['type'] = 0;
                $data2['description'] = $username . " try to login but failed. ";
                $data2['ip'] = $_SERVER['REMOTE_ADDR'];
                $data2['xip'] = @getenv("HTTP_X_FORWARDED_FOR");
                $data2['lastupdated'] = 'now()';
                //$db->query_insert("vrs_logs", $data2);
                return false;
            }

        }
        else {
            session_destroy();
            return false;
        }
    }


    function is_login_profile($user_name, $pwd)
    {

        global $db, $db1, $db2;
        $username = $user_name;
        $password = $pwd;
        $ip = $_SERVER['REMOTE_ADDR'];
        $XIP = @getenv("HTTP_X_FORWARDED_FOR");
        $query = "SELECT * FROM " . TABLEUSETUP . " WHERE (BINARY username='$username' or BINARY email='$username')
				 AND BINARY password='$password' and status='active' and (type IN ('administrator', 'profile'))";
        //print($query); exit;
        $record = $db->query_first($query);
        $recorddb = $db1->query_first($query);
        $recorddbtx = $db2->query_first($query);

        if ($record->status == "active") {
            if ($db->affected_rows > 0) {
                $_SESSION['sess_userid'] = $record->us_id;
                $_SESSION['sess_username'] = $record->username;
                $_SESSION['sess_email'] = $record->email;
                $_SESSION['sess_type'] = $record->type;
                $_SESSION['sess_role'] = $record->role;
                //$_SESSION['sess_associatedid'] = $record->associatedid;

                $data['userid'] = $record->us_id;
                $data['email'] = $username;
                $data['type'] = $record->type;
                $data['ipaddress'] = $ip;
                $data['lastupdated'] = "NOW()";
                $data['status'] = "LOGIN SUCCESS";
                $db->query_insert(TABLE_LOGINHISTORY, $data);

                $data2['userid'] = $_SESSION['sess_userid'];
                $data2['action'] = 'LOGIN SUCCESS';
                $data2['type'] = $_SESSION['sess_type'];
                $data2['description'] = $_SESSION['sess_username'] . " has successfully login ";
                $data2['ip'] = $_SERVER['REMOTE_ADDR'];
                $data2['xip'] = @getenv("HTTP_X_FORWARDED_FOR");
                $data2['lastupdated'] = 'now()';
                //print_r($data2);exit;
                //$db->query_insert("vrs_logs", $data2);
                return true;
            } else {
                $data['userid'] = 0;
                $data['email'] = $username;
                $data['type'] = 0;
                $data['ipaddress'] = $ip;
                $data['lastupdated'] = "NOW()";
                $data['status'] = "LOGIN FAILED";
                $db->query_insert(TABLE_LOGINHISTORY, $data);

                $data2['userid'] = 0;
                $data2['action'] = 'LOGIN FAILED';
                $data2['type'] = 0;
                $data2['description'] = $username . " try to login but failed. ";
                $data2['ip'] = $_SERVER['REMOTE_ADDR'];
                $data2['xip'] = @getenv("HTTP_X_FORWARDED_FOR");
                $data2['lastupdated'] = 'now()';
                //$db->query_insert("vrs_logs", $data2);
                return false;
            }
        }
        elseif ($recorddb->status == "active") {
            $db = $db1;
            if ($db->affected_rows > 0) {
                $_SESSION['nextclass'] = 1;
                $_SESSION['sess_userid'] = $recorddb->us_id;
                $_SESSION['sess_username'] = $recorddb->username;
                $_SESSION['sess_email'] = $recorddb->email;
                $_SESSION['sess_type'] = $recorddb->type;
                $_SESSION['sess_role'] = $recorddb->role;
                //$_SESSION['sess_associatedid'] = $record->associatedid;
                $_SESSION['clone'] = 1;

                $data['userid'] = $recorddb->us_id;
                $data['email'] = $username;
                $data['type'] = $recorddb->type;
                $data['ipaddress'] = $ip;
                $data['lastupdated'] = "NOW()";
                $data['status'] = "LOGIN SUCCESS";
                $db->query_insert(TABLE_LOGINHISTORY, $data);

                $data2['userid'] = $_SESSION['sess_userid'];
                $data2['action'] = 'LOGIN SUCCESS';
                $data2['type'] = $_SESSION['sess_type'];
                $data2['description'] = $_SESSION['sess_username'] . " has successfully login ";
                $data2['ip'] = $_SERVER['REMOTE_ADDR'];
                $data2['xip'] = @getenv("HTTP_X_FORWARDED_FOR");
                $data2['lastupdated'] = 'now()';
                return true;

            } else {
                $data['userid'] = 0;
                $data['email'] = $username;
                $data['type'] = 0;
                $data['ipaddress'] = $ip;
                $data['lastupdated'] = "NOW()";
                $data['status'] = "LOGIN FAILED";
                $db->query_insert(TABLE_LOGINHISTORY, $data);

                $data2['userid'] = 0;
                $data2['action'] = 'LOGIN FAILED';
                $data2['type'] = 0;
                $data2['description'] = $username . " try to login but failed. ";
                $data2['ip'] = $_SERVER['REMOTE_ADDR'];
                $data2['xip'] = @getenv("HTTP_X_FORWARDED_FOR");
                $data2['lastupdated'] = 'now()';
                return false;
            }
        }
        elseif ($recorddbtx->status == "active") {
            $db = $db2;
            if ($db->affected_rows > 0) {
                $_SESSION['nextclass'] = 2;
                $_SESSION['sess_userid'] = $recorddbtx->us_id;
                $_SESSION['sess_username'] = $recorddbtx->username;
                $_SESSION['sess_email'] = $recorddbtx->email;
                $_SESSION['sess_type'] = $recorddbtx->type;
                $_SESSION['sess_role'] = $recorddbtx->role;
                //$_SESSION['sess_associatedid'] = $record->associatedid;
                $_SESSION['clone'] = 1;

                $data['userid'] = $recorddbtx->us_id;
                $data['email'] = $username;
                $data['type'] = $recorddbtx->type;
                $data['ipaddress'] = $ip;
                $data['lastupdated'] = "NOW()";
                $data['status'] = "LOGIN SUCCESS";
                $db->query_insert(TABLE_LOGINHISTORY, $data);

                $data2['userid'] = $_SESSION['sess_userid'];
                $data2['action'] = 'LOGIN SUCCESS';
                $data2['type'] = $_SESSION['sess_type'];
                $data2['description'] = $_SESSION['sess_username'] . " has successfully login ";
                $data2['ip'] = $_SERVER['REMOTE_ADDR'];
                $data2['xip'] = @getenv("HTTP_X_FORWARDED_FOR");
                $data2['lastupdated'] = 'now()';
                return true;

            } else {
                $data['userid'] = 0;
                $data['email'] = $username;
                $data['type'] = 0;
                $data['ipaddress'] = $ip;
                $data['lastupdated'] = "NOW()";
                $data['status'] = "LOGIN FAILED";
                $db->query_insert(TABLE_LOGINHISTORY, $data);

                $data2['userid'] = 0;
                $data2['action'] = 'LOGIN FAILED';
                $data2['type'] = 0;
                $data2['description'] = $username . " try to login but failed. ";
                $data2['ip'] = $_SERVER['REMOTE_ADDR'];
                $data2['xip'] = @getenv("HTTP_X_FORWARDED_FOR");
                $data2['lastupdated'] = 'now()';
                return false;
            }
        }
        else {
            return false;
        }
    }

    //FUNCTION to logout user

    function Logout($indexpagepath)
    {

        session_destroy();
        header($indexpagepath);
        exit;

    }

    //FUCNTION to change user password

    function ChangePassword($old_pwd, $new_pwd)
    {
        global $db;
        $old_pwd = $old_pwd;
        $query = "SELECT * FROM " . TABLEUSETUP . " WHERE BINARY password='$old_pwd'
				  and BINARY username='" . $_SESSION['sess_username'] . "'";
        //print($query); exit;
        $record = $db->query_first($query);

        if ($db->affected_rows > 0) {
            $new_pwd = $new_pwd;
            $upt_query = "UPDATE " . TABLEUSETUP . " SET password = '$new_pwd'
						  where username='" . $_SESSION['sess_username'] . "'";
            //print($upt_query); exit;
            $db->query_first($upt_query);

            $data2['userid'] = $_SESSION['sess_userid'];
            $data2['action'] = 'UPDATE';
            $data2['type'] = $_SESSION['sess_type'];
            $data2['description'] = $_SESSION['sess_username'] . " has changed his/her password. ";
            $data2['ip'] = $_SERVER['REMOTE_ADDR'];
            $data2['xip'] = @getenv("HTTP_X_FORWARDED_FOR");
            $data2['lastupdated'] = 'now()';

            //$db->query_insert("vrs_logs", $data2);

            return true;

        } else {

            return false;
        }

    }


    /**********************************************************
     * / This function will return   Resource User list
     **********************************************************/

    public static function GetUserList($filter = "", $pageno = "null", $pagesize = "null")
    {

        global $db;
        $query = "SELECT * from " . TABLEUSETUP . "  " . $filter;
        //print($query);exit;
        $rows = $db->fetch_all_array($query, $pageno, $pagesize);

        if ($db->affected_rows > 0) {

            return $rows;
        } else {

            return false;
        }
    }//GetResourceList

    public static function GetNumRows()
    {
        global $db;
        return $db->GetNumRows();
    }

    public static function GetNumberOfPages()
    {
        global $db;
        return $db->GetNumberOfPages();
    }


}

?>