<?php
ini_set('display_errors','Off');
/**
 * SYSTEM SETTINGS
 */
//  HOW TO USE THE TODO TRACKER
//$TYPE = 'Issues'; // https://en.wikipedia.org/wiki/Issue_tracking_system
//$TYPE = 'Bugs';   // https://en.wikipedia.org/wiki/Bug_tracking_system
//$TYPE = 'MOSCOW'; // https://en.wikipedia.org/wiki/MoSCoW_method
//$TYPE = 'GTD';    // https://en.wikipedia.org/wiki/Getting_Things_Done
$TYPE = 'Documents';
ini_set('memory_limit', '1024M');
//  LOGIN DATA - Must be changed before first real use!
$ARR_USERS = array();
$uAdmin = new stdClass();
$uAdmin->username = 'tmd1';
$uAdmin->password = '740518';
$uAdmin->role = 'admin';
$ARR_USERS[] = $uAdmin;
$uUser1 = new stdClass();
$uUser1->username = 'tmd2';
$uUser1->password = 'hcy123';
$uUser1->role = 'register';
$ARR_USERS[] = $uUser1;
$arrAddressIP = array('192.168.1.155','14.161.4.149','118.69.186.184');
//my address ip : 74.208.59.55
/* $USER = 'admin';
  $PASS = 'admin'; */

//  DATE FORMAT
// DATE FORMAT FOR DATA FILE - SEE: http://php.net/manual/en/function.date.php
$DATE = 'Y.m.d'; // DEFAULT 'Y.m.d'
// DATE FORMAT FOR JS DATEPICKER - SEE: http://materializecss.com/pickers.html
$PICK = 'yyyy.mm.dd'; // DEFAULT 'yyyy.mm.dd'
//  Set Language to 'en' for english OR 'de' for german
$LANG = 'en'; // USE 'en' OR 'de'
//  Login Script Salt
$SALT = '70YD1AK1vq'; // USE first value from URL https://randomkeygen.com/

/* EOF - END OF FILE */
/* EOF - END OF FILE */
$listUser = 'data/User.json';
$ARR_USERS = json_decode(file_get_contents($listUser));


function check_current_user_login($ARR_USERS, $SALT) {
		
	$ipaddress = '';
        if (getenv('HTTP_CLIENT_IP')){
            $ipaddress = getenv('HTTP_CLIENT_IP');
        }else if(getenv('HTTP_X_FORWARDED_FOR')){
            $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
        }else if(getenv('HTTP_X_FORWARDED')){
            $ipaddress = getenv('HTTP_X_FORWARDED');
        }else if(getenv('HTTP_FORWARDED_FOR')){
            $ipaddress = getenv('HTTP_FORWARDED_FOR');
        }else if(getenv('HTTP_FORWARDED')){
           $ipaddress = getenv('HTTP_FORWARDED');
        }else if(getenv('REMOTE_ADDR')){
            $ipaddress = getenv('REMOTE_ADDR');
        }else{
            $ipaddress = 'UNKNOWN';
        }
    //echo 'ipaddress='.$ipaddress;
	$data = file_get_contents('http://ip-api.com/json/'.$ipaddress);
        $data = json_decode($data);
	
	

	$time = time();
	$timeout_duration = 600;
	if (isset($_SESSION['LAST_ACTIVITY']) &&  ($time - $_SESSION['LAST_ACTIVITY']) > $timeout_duration) {
		session_unset();
		session_destroy();
		session_start();
	}
	
    $result = array('status' => 0, 'user' => null);
    foreach ($ARR_USERS as $u) {
        $sessionname = md5($u->name . $SALT);
        if ($_SESSION[$sessionname]){
            // var_dump($sessionname);die;
			$_SESSION['LAST_ACTIVITY'] = time();			
            $result['status'] = 200;
            $result['user'] = $u;
            //var_dump($sessionname);var_dump($data);die;
            if($data->countryCode != 'VN' && $data->countryCode != '' && $u->name != 'GM' && $u->name != 'LEX'  && $u->name != 'SAM' && $u->name != 'ANA' ){	//
			header("Location: https://htlservice.net/expense/login.php");
            }
            break;
        }
    }
    return $result;
}

