<?php
 session_start();
// error_reporting(E_ALL); ini_set('display_errors', 1);
// session_unset(); 
// 10 mins in seconds
$inactive = 30; 
//$_session['start_time']='xcadsasdasdasd' ;
//$_session['timeout']=time();
 header('Content-Type: charset=utf-8');

 $PhpOS = strtolower(PHP_OS);
	if(substr_count( $PhpOS, 'win' ) == 0 ){
		$ClientOS="";}
	else
	{$ClientOS="Windows";
}
//  echo gethostbyaddr($_SERVER['REMOTE_ADDR']).'<br>';
//  echo gethostname().'<br>';
//  echo php_uname('n').'<br>';
#   echo $_SERVER['REMOTE_ADDR']  ;
#   echo "\n" ;

global $iniValues;

$file = fopen("vsApps.ini","r");
 while(! feof($file)) {
   $line=fgets($file);
   $myArray  = explode('=', $line);
   if (count($myArray)>1) {
		 $key = str_replace("'", "\'",trim($myArray[0]));	
	   $value = str_replace("'", "\'",trim($myArray[1]));  
	   $value = str_replace('"', '',trim($myArray[1]));
	   
		  if($key!='') { $iniValues[$key]= $value;}
	}    
  }
fclose($file);
 
$_SESSION['inValues']=$iniValues;
 
$iniValues['DB_USERNAME']=$iniValues['DB_PREFIX'].$iniValues['DB_USERNAME'];
$iniValues['DB_NAME']=$iniValues['DB_PREFIX'].$iniValues['DB_NAME'];

$DB_SERVER=str_replace('"', '',$iniValues['DB_SERVER']);
$DB_USERNAME=str_replace('"', '',$iniValues['DB_USERNAME']);
$DB_PASSWORD=str_replace('"', '',$iniValues['DB_PASSWORD']);
$DB_NAME=str_replace('"', '',$iniValues['DB_NAME']);
 
$_SESSION['inactive_time']=$iniValues['inactive_time'];


$txtAppName = $_GET["AppName"];
$txtEponymia = $_GET["Eponymia"];
$txtTilefono = $_GET["Tilefono"];
$txtAFM = $_GET["AFM"];
$txtHDDSerial = $_GET["HDDSerial"];
$txtLocalTimeDate = $_GET["LocalTimeDate"];
$SendUnlockKey = $_GET["SendUnlockKey"];
$TerminalUpdate= $_GET["TerminalUpdate"]; 
$isActive=0;

  $now = new DateTime();
 $year = $now->format("Y");
$year2 = $now->format("y");
$month = $now->format("m");
  $day = $now->format("d");

$HmNow=$now->format('Y-m-d');
$Hmeres =0;

//$key=($year2*365+($month-1)*30+$day)*200+$txtHDDSerial+$txtAFM;
//$UnlockKey=dechex($key);

# ===================================================
If(strlen($txtHDDSerial) > 9){ $txtHDDSerial =substr($txtHDDSerial,0,9);}
 
 $txtHDDSerial = $txtHDDSerial . str_pad($myCode, 9-strlen($txtHDDSerial), '0', STR_PAD_LEFT);

 $myCode =$txtLocalTimeDate . "";
 
 If($myCode == "") { $myCode = "0";}
  $myCode = hexdec($myCode);
 $myCode = str_pad($myCode, 8, '0', STR_PAD_LEFT);  
 $myDate = substr($myCode,4,4);
 $myTime = substr($myCode,0,4);

$UnlockKey = dechex($myTime . substr($txtHDDSerial,0, 5));
# ===================================================




$ActivateUses=0;
$FtpUserName="customers_user";
$FtpPassword="P@ssw0rdFtp!";

//echo $DB_NAME;

# $_serverHost = $_SERVER['HTTP_HOST'];
# $_phpSelf = $_SERVER['PHP_SELF'];
# $_serverDir = dirname( $_phpSelf );
# $_serverPort = $_SERVER['SERVER_PORT'];
# echo "phpversion : ";  print_r(phpversion() );
# echo "IsWindows : ". IsWindows()."<br>";
# echo function_exists('parse_ini_file');

# echo file_exists( "vsApps.ini");
 
// ================================== 
//     Connect to MySQL database    /
// ==================================
//echo $DB_SERVER. " " .$iniValues['DB_PREFIX'].$DB_USERNAME . " " . $DB_PASSWORD . " " . $iniValues['DB_PREFIX'].$DB_NAME;

//$simera=
  $conn = mysqli_connect($DB_SERVER, $DB_USERNAME, $DB_PASSWORD,  $DB_NAME);
   mysqli_set_charset($conn, "utf8");
   
if($conn === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}
 else
{
               $query = "SELECT * FROM apps WHERE Application='$txtAppName'";
	          $result = mysqli_query($conn, $query);
                  $rs = mysqli_fetch_assoc($result);
	         $Version = $rs['Version'];
	     $VersionDate = $rs['VersionDate'];
	       $FtpServer = $rs['FtpServer'];
	 $FtpServerFolder = $rs['FtpServerFolder'];
	//  $rs->close();	
	 
      $query = "SELECT * FROM symvolaia WHERE AFM='$txtAFM' and ApplicationName='$txtAppName'";
	 $result = mysqli_query($conn, $query);
         $rs = mysqli_fetch_assoc($result);
        
	    $TerminalsNow=$rs['Terminal'];
	    $TerminalFound=(strpos($rs['Terminal'], $txtHDDSerial)>0);
          if($TerminalFound) {  
 		            $isActive = $rs['isActive'];
		   $ActivateStartDate = $rs['ActivateStartDate'];
		     $ActivateEndDate = $rs['ActivateEndDate'];
			    $ActivateUses = $rs['ActivateUses'];
			       $LicenseNo = $rs['LicenseNo'];
			  	   $Terminals = $rs['Terminal'];
			          $Hmeres = date_diff(date_create($HmNow), date_create($ActivateEndDate))->format('%r%a') ;
		  // $Hmeres = date_diff(date_create($ActivateEndDate), date_create($HmNow))->format('%a');
           }
	  // =========== Update Terminals ===========
	 if ($TerminalUpdate==1 && $TerminalFound !== true){
		 	$query="update symvolaia SET Terminal='$TerminalsNow|$txtHDDSerial|' WHERE AFM='$txtAFM' and ApplicationName='$txtAppName'";
	    $result = mysqli_query($conn, $query); 
	 }
	 
	}
 $conn->close();	

// echo  $query . "\n";
	 
echo "HTTP_HOST=". $_SERVER['HTTP_HOST'] . "\n";
echo "Global_IP_Client=". $_SERVER['REMOTE_ADDR'] . "\n";
echo "SERVER_SOFTWARE=". $_SERVER['SERVER_SOFTWARE'] . "\n";
echo "ClientOS=". $ClientOS . "\n";
echo "HmLixis=" .  $ActivateEndDate . "\n"; 
echo "HmServer=" .  $HmNow . "\n"; 
echo "Version=" .  $Version . "\n"; 
echo "VersionDate=" .  $VersionDate . "\n"; 
echo "FtpServer=" .  $FtpServer . "\n"; 
echo "FtpServerFolder=" .  $FtpServerFolder . "\n"; 
echo "FtpUserName=" .  $FtpUserName . "\n"; 
echo "FtpPassword=" .  $FtpPassword . "\n"; 
echo "Hmeres= " .  $Hmeres . "\n"; 
echo "isActive=" . $isActive. "\n";
echo "ActivateUses=" . $ActivateUses. "\n";
echo "UnlockKey=" . $UnlockKey. "\n";

 if ($SendUnlockKey==1) {
    echo "key=" . $key. "\n";
//	echo "UnlockKey=" . $UnlockKey. "\n";
}

echo "LicenseNo=" . $LicenseNo. "\n";
echo "Terminals=" . $Terminals. "\n";

?>

 
