<?php include('../../config.php');
session_start();
if (!isset($_SESSION['USER']) || empty($_SESSION['USER']) || mysqli_fetch_assoc(mysqli_query($conn,"SELECT us_sid FROM user WHERE uid=".$_SESSION['USER']['uid']." LIMIT 1"))['us_sid'] != session_id()) {

    header('Location:../../logout.php');
              
} else {
$userid  = $_SESSION['USER']['uid'];
$coid    = $_SESSION['USER']['us_coid'];
}

$company_query= mysqli_query($conn,"SELECT * FROM company WHERE coid='$coid' LIMIT 1 ") or die (mysql_error());
         $rowcom= mysqli_fetch_assoc($company_query);

$company_name=$rowcom['co_name'];

date_default_timezone_set('Asia/Dhaka');
$dateqe = new DateTime("now", new DateTimeZone('Asia/Dhaka')); 
$date = date("Y-m-d", $dateqe->format( 'U')); 
$intime= date("H:i:sa");


// time funtion end 
$host=$_SERVER['HTTP_HOST'];
$link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 
                "https" : "http") . "://" . $_SERVER['HTTP_HOST'] .  
                $_SERVER['REQUEST_URI']; 

$ip_address=$_SERVER['REMOTE_ADDR'];
   $pcname= gethostbyaddr($_SERVER['REMOTE_ADDR']);

$user_agent = $_SERVER['HTTP_USER_AGENT'];
   function getOS() {
       global $user_agent;
              $os_platform  = "Unknown OS Platform";
              $os_array     = array(
                          '/windows nt 10/i'      =>  'Windows 10',
                          '/windows nt 6.3/i'     =>  'Windows 8.1',
                          '/windows nt 6.2/i'     =>  'Windows 8',
                          '/windows nt 6.1/i'     =>  'Windows 7',
                          '/windows nt 6.0/i'     =>  'Windows Vista',
                          '/windows nt 5.2/i'     =>  'Windows Server 2003/XP x64',
                          '/windows nt 5.1/i'     =>  'Windows XP',
                          '/windows xp/i'         =>  'Windows XP',
                          '/windows nt 5.0/i'     =>  'Windows 2000',
                          '/windows me/i'         =>  'Windows ME',
                          '/win98/i'              =>  'Windows 98',
                          '/win95/i'              =>  'Windows 95',
                          '/win16/i'              =>  'Windows 3.11',
                          '/macintosh|mac os x/i' =>  'Mac OS X',
                          '/mac_powerpc/i'        =>  'Mac OS 9',
                          '/linux/i'              =>  'Linux',
                          '/ubuntu/i'             =>  'Ubuntu',
                          '/iphone/i'             =>  'iPhone',
                          '/ipod/i'               =>  'iPod',
                          '/ipad/i'               =>  'iPad',
                          '/android/i'            =>  'Android',
                          '/blackberry/i'         =>  'BlackBerry',
                          '/webos/i'              =>  'Mobile'
                    );

    foreach ($os_array as $regex => $value)
        if (preg_match($regex, $user_agent))
            $os_platform = $value;

    return $os_platform;
}

function getBrowser() {

    global $user_agent;

    $browser        = "Unknown Browser";

    $browser_array = array(
                            '/msie/i'      => 'Internet Explorer',
                            '/firefox/i'   => 'Firefox',
                            '/safari/i'    => 'Safari',
                            '/chrome/i'    => 'Chrome',
                            '/edge/i'      => 'Edge',
                            '/opera/i'     => 'Opera',
                            '/netscape/i'  => 'Netscape',
                            '/maxthon/i'   => 'Maxthon',
                            '/konqueror/i' => 'Konqueror',
                            '/mobile/i'    => 'Handheld Browser'
                     );

    foreach ($browser_array as $regex => $value)
        if (preg_match($regex, $user_agent))
            $browser = $value;

    return $browser;
}


$user_os        = getOS();
$user_browser   = getBrowser();

$device_details = "Browser: ".$user_browser."Operating System: ".$user_os."";

($device_details);

("<br /><br /><br />".$_SERVER['HTTP_USER_AGENT']."");


$ip=$_SERVER['REMOTE_ADDR'];

$ipdat = @json_decode(file_get_contents( "http://www.geoplugin.net/json.gp?ip=". $ip)); 
   
 $country= $ipdat->geoplugin_countryName ; 
 $city= $ipdat->geoplugin_city ; 
 $earth= $ipdat->geoplugin_continentName ; 
 $lat= $ipdat->geoplugin_latitude ; 
 $longss= $ipdat->geoplugin_longitude ; 
 $symble= $ipdat->geoplugin_currencySymbol ; 
 $currency= $ipdat->geoplugin_currencyCode ; 
 
 function isMobileDevice() { 
    return preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo 
|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i" 
, $_SERVER["HTTP_USER_AGENT"]); 
} 
if(isMobileDevice()){ 
      
    
 $user_mode="Android Mode";
} else { 
  $user_mode="Destop Mode";
} 
 $rand = strtoupper(substr(uniqid(sha1(time())),0,8));
 $urltoken= $ip . $rand;


if ($urltoken==$urltoken) {
 mysqli_query($conn,"INSERT INTO log_data(`coid`,`userid`, `username`, `intime`, `ipaddress`, `browser`, `opareting`, `country`, `city`, `earth`, `lat`, `longat`, `symble`, `currency`, `urltoken`, `date`) 
     VALUES ('$coid','$userid','$company_name','$intime','$ip_address','$user_browser','$user_os','$country','$city','$earth','$lat','$longss','$symble','$currency','$urltoken','$date') ");
} else {
  mysqli_query($conn,"INSERT INTO log_data(`coid`, `userid`, `username`, `intime`, `ipaddress`, `browser`, `opareting`, `country`, `city`, `earth`, `lat`, `longat`, `symble`, `currency`, `urltoken`, `date`) 
     VALUES ('$coid','$userid','$company_name','$intime','$ip_address','$user_browser','$user_os','$country','$city','$earth','$lat','$longss','$symble','$currency','$urltoken','$date') ") or die (mysql_error());
}

header("Location:../auth/");
?>
