<?php

	//---------------------------------------------------------------------------------------------
	function RetrieveUserIP() {
		
		if ( ! empty($_SERVER['HTTP_CLIENT_IP']) ) {
			
			if ( filter_var($_SERVER['HTTP_CLIENT_IP'], FILTER_VALIDATE_IP) ) {
				$address = $_SERVER['HTTP_CLIENT_IP'] ;
			} else {
				$address = "001.001.001.001" ;
			}
		}
		
		elseif ( ! empty($_SERVER['HTTP_X_FORWARDED_FOR']) ) {
			
			if ( filter_var($_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP) ) {
				$address = $_SERVER['HTTP_X_FORWARDED_FOR'] ;
			} else {
				$address = "002.002.002.002" ;
			}
		
		}
		
		else {
			
			if ( ! empty($_SERVER['REMOTE_ADDR']) ) {
				
				if ( filter_var($_SERVER['REMOTE_ADDR'], FILTER_VALIDATE_IP) ) {
					$address = $_SERVER['REMOTE_ADDR'] ;
				} else {
					$address = "003.003.003.003" ;
				}
		
			}
			else {
				
				$address = "000.000.000.000" ;
				
			}
		}

		return $address ;

	}

	//---------------------------------------------------------------------------------------------
	$code_erreur = 0;
	
	$ip_wan = RetrieveUserIP() ;
	
	//---------------------------------------------------------------------------------------------
	include("./_identifiants/sql_identification_main.php");
	
	//---------------------------------------------------------------------------------------------
	$mac			= $_GET["MAC"];
	
	if ( isset($_GET["IP"]) ) { $ip=$_GET["IP"] ;} else { $ip="---.---.---.---" ;} ;
	
	$sonde_nom		= $_GET["SONDE_NOM"];
	$site_id		= $_GET["SITE_ID"];
	$bat    		= $_GET["BAT"];
	$niveau			= $_GET["NIVEAU"];
	$pwd			= $_GET["PWD"];
	
	$marqueur_temps = time() ;
	date_default_timezone_set('Europe/Paris');
	$year  = date('Y', $marqueur_temps);
	$month = date('m', $marqueur_temps);
	$day   = date('d', $marqueur_temps);
	$heure = date('H:i:s', $marqueur_temps);

	//---------------------------------------------------------------------------------------------
	$link = mysqli_connect($mysql_host,$mysql_user,$mysql_password, $mysql_base);
	
	if (!$link) {
		
		echo ("KOKO1");
		$code_erreur = 1;
		
	} else {

		$query_3 = "
			INSERT INTO AQCM05_DETECTED 
				(aqcm_MAC, aqcm_IP_SONDE, temps, annee, mois, jour, heure, aqcm_NAME, aqcm_SITE_ID, aqcm_BAT, aqcm_NIVEAU, aqcm_PWD, aqcm_IP_WAN) 
			VALUES 
				('$mac','$ip','$marqueur_temps','$year','$month','$day','$heure', '$sonde_nom','$site_id', '$bat','$niveau','$pwd','$ip_wan') 
			ON DUPLICATE KEY UPDATE 
				aqcm_MAC = '$mac'  ,
				aqcm_IP_SONDE = '$ip'  ,
				temps = '$marqueur_temps'  ,
				annee = '$year'  ,
				mois = '$month'  ,
				jour = '$day'  ,
				heure = '$heure'  ,
				aqcm_NAME = '$sonde_nom'  ,
				aqcm_SITE_ID = '$site_id'  ,
				aqcm_BAT = '$bat'  ,
				aqcm_NIVEAU = '$niveau'  ,
				aqcm_PWD = '$pwd',
				aqcm_IP_WAN = '$ip_wan'
		";
		
		//echo($query_3 . "</br>");
		
		$result_3 = mysqli_query($link, $query_3);
		
		if (!$result_3) {
			
			$code_erreur = 3;
			echo ("KOKO2");
			
		} else {	
		
			$code_erreur = 0;
			echo ("OKOK");
			
		}   
		
		mysqli_close($link);
	}
	
	//echo("code erreur = " . $code_erreur . "</br>");

?>

