<?php

session_start();
//session_start();
require_once "config.php";
//require_once "passwordHash.php";
//require_once("../ichat/admin/assets/includes/Library/Loader.php");
//require_once("../ichat/admin/assets/includes/Library/SessManager.php");
//require_once("../ichat/admin/assets/includes/Library/User.php");
require_once 'database.php';

$msg="";
if(isset($_GET['message']))
{
    $msg = $_GET['message'];
    $success = 1;
}

class index extends database
{
    
    
    public function login($data)
    {
		$username = $where['username'] = $data['username'];
		$password = $data['password'];
		$user = $this->fetchRow('ichat_users', $where);
		if(!$user)
		{
                    
			return 0;
			exit;
		}
                
              

		if ($user['disabled'] == "1") {
				return 0;
				exit;
		}

		
                $found_user = $this->fetchRow('ichat_users', ['username' => $data['username']]);
		if(!$found_user)
		{
                    
			return 0;
			exit;
		}
                
             


		//check if disabled ...
		if ($found_user['disabled'] == "1") {
			return 0;
			exit;
		}
                
            

			//check password ...
			 $saltedhash = $found_user['password'];
			
			if ( substr($password , 0, 1 ) == '#' ) {	//direct hash mode!
				
				$password = str_replace('#', "", $password);
                               
				if ($saltedhash == $password) {
                                    
					//$session->login($found_user);
					
					//Log::log_action($found_user->id , "Login" , "Login to system using hashed password");
					
					$_SESSION['name'] = $found_user->name;
					$_SESSION['id'] = $found_user->id;
					/*if(isset($_POST['remember-me']) && $_POST['remember-me'] == '1') {
						$params = session_get_cookie_params();
						setcookie(session_name(), $_COOKIE[session_name()], time() + 60*60*24*30, $params["path"], $params["domain"], $params["secure"], $params["httponly"]);
					}*/
					return 1;
					exit;
				} else {
					return 0;
					exit;
				}
			
			} else {
                            
                              
				//$phpass = new PasswordHash(8, true);
                                if(password_verify($password, $saltedhash))
                                {
                                    
                                    $_SESSION['name'] = $found_user['name'];
					$_SESSION['id'] = $found_user['id'];
                                    if($found_user['prvlg_group'] == 4)
					{
						return 4;
					}
					else
					{
						return 1;
					}
					exit;
                                
                                } else {
                                   
					return 0;
					exit;
				}
                                
				/*if ($this->CheckPassword($password, $saltedhash)) {
					
					//$session->login($found_user);
					//Log::log_action($found_user->id , "Login" , "Login to system");
					$_SESSION['name'] = $found_user->name;
					$_SESSION['id'] = $found_user->id;
					/*if(isset($_POST['remember-me']) && $_POST['remember-me'] == '1') {
						$params = session_get_cookie_params();
						setcookie(session_name(), $_COOKIE[session_name()], time() + 60*60*24*30, $params["path"], $params["domain"], $params["secure"], $params["httponly"]);
					}*/
					
				/*	if($user['prvlg_group'] == 4)
					{
						return 4;
					}
					else
					{
						return 1;
					}
					exit;
				} else {

					return 0;
					exit;
				} */
				
			}
    }
}


if(isset($_POST['data']))
{
$msg="";
$success = 0;
	$data=$_POST['data'];
	if(trim($data['username']) == '')
	{
		$msg.= "Username is required<br/>";
	}
	if(trim($data['password']) == '')
	{
		$msg.= "Passowrd is required<br/>";
	}
	
	if($msg=="")
	{
		$index = new index();
		$loggedInUser = $index->login($data);
	
		if($loggedInUser)
		{

                    if($loggedInUser == 1)
                    {
                       $_SESSION['name'] = 'admin';
					$_SESSION['id'] = 1;
						header('Location: dashboard.php');
						exit;
                    }
                    else if($loggedInUser == 4)
                    {
                    	header('Location: /product/list');
						exit;
                    }
                  header('Location: index.php');
                    exit;
		}
	}
	$msg = 'Login Invalid';
}


?>
<!DOCTYPE html>
<html lang="en">

<head>
    <!-- Required meta tags-->
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="au theme template">
    <meta name="author" content="Hau Nguyen">
    <meta name="keywords" content="au theme template">

    <!-- Title Page-->
    <title>Login</title>

    <!-- Fontfaces CSS-->
    <link href="css/font-face.css" rel="stylesheet" media="all">
    <link href="vendor/font-awesome-4.7/css/font-awesome.min.css" rel="stylesheet" media="all">
    <link href="vendor/font-awesome-5/css/fontawesome-all.min.css" rel="stylesheet" media="all">
    <link href="vendor/mdi-font/css/material-design-iconic-font.min.css" rel="stylesheet" media="all">

    <!-- Bootstrap CSS-->
    <link href="vendor/bootstrap-4.1/bootstrap.min.css" rel="stylesheet" media="all">

    <!-- Vendor CSS-->
   <!-- <link href="vendor/animsition/animsition.min.css" rel="stylesheet" media="all">-->
    <link href="vendor/bootstrap-progressbar/bootstrap-progressbar-3.3.4.min.css" rel="stylesheet" media="all">
    <link href="vendor/wow/animate.css" rel="stylesheet" media="all">
    <link href="vendor/css-hamburgers/hamburgers.min.css" rel="stylesheet" media="all">
    <link href="vendor/slick/slick.css" rel="stylesheet" media="all">
    <link href="vendor/select2/select2.min.css" rel="stylesheet" media="all">
    <link href="vendor/perfect-scrollbar/perfect-scrollbar.css" rel="stylesheet" media="all">

    <!-- Main CSS-->
    <link href="css/theme.css" rel="stylesheet" media="all">

</head>

<body class="animsition">
    <div class="page-wrapper">
        <div class="page-content--bge5">
            <div class="container">
                <div class="login-wrap">
                    <div class="login-content">
                        <div class="login-logo">
                            <a href="#">
                                <img src="images/logo.png" alt="EfixAdmin">
                            </a>
                        </div>
                        <div class="login-form">
                            <form action="" method="post">
                                <?php
                                
                                    if($msg != '')
                                    {
                                        ?>
                                        <div class="alert alert-danger"><?php echo  htmlspecialchars($msg) ?></div>
                                        <?php
                                    }
                                ?>
                                <div class="form-group">
                                    <label>Username</label>
                                    <input class="au-input au-input--full" name="data[username]" type="text"  placeholder="Username">
                                </div>
                                <div class="form-group">
                                    <label>Password</label>
                                    <input class="au-input au-input--full" name="data[password]" type="password" placeholder="Password">
                                </div>
                                <!--<div class="login-checkbox">
                                    <label>
                                        <input type="checkbox" name="remember">Remember Me
                                    </label>
                                    <label>
                                        <a href="#">Forgotten Password?</a>
                                    </label>
                                </div>-->
                                <button class="au-btn au-btn--block au-btn--green m-b-20" type="submit">sign in</button>
                               
                            </form>
                            <!--<div class="register-link">
                                <p>
                                    Don't you have account?
                                    <a href="#">Sign Up Here</a>
                                </p>
                            </div>-->
                        </div>
                    </div>
                </div>
            </div>
        </div>

    </div>

    <!-- Jquery JS-->
    <script src="vendor/jquery-3.2.1.min.js"></script>
    <!-- Bootstrap JS-->
    <script src="vendor/bootstrap-4.1/popper.min.js"></script>
    <script src="vendor/bootstrap-4.1/bootstrap.min.js"></script>
    <!-- Vendor JS       -->
    <script src="vendor/slick/slick.min.js">
    </script>
    <script src="vendor/wow/wow.min.js"></script>
    <script src="vendor/animsition/animsition.min.js"></script>
    <script src="vendor/bootstrap-progressbar/bootstrap-progressbar.min.js">
    </script>
    <script src="vendor/counter-up/jquery.waypoints.min.js"></script>
    <script src="vendor/counter-up/jquery.counterup.min.js">
    </script>
    <script src="vendor/circle-progress/circle-progress.min.js"></script>
    <script src="vendor/perfect-scrollbar/perfect-scrollbar.js"></script>
    <script src="vendor/chartjs/Chart.bundle.min.js"></script>
    <script src="vendor/select2/select2.min.js">
    </script>

    <!-- Main JS-->
    <script src="js/main.js"></script>

</body>

</html>
<!-- end document-->