<?php

	// http://aqcm05230.ddns.net/authorize/get_fw_update_ff.php?SITE_ID=05230_002&MAC=b8_27_eb_11_84_de&SERVER_UPDATES_MDP=4PwvwnI5F2LI

	$site_id			= $_GET["SITE_ID"];
	$mac				= $_GET["MAC"];
	$server_updates_mdp	= $_GET["SERVER_UPDATES_MDP"];
	
	include ("../_identifiants/sql_identification_sans_base.php");
	$mysql_base ='AQCM05_SITE_' . $site_id;
	
	date_default_timezone_set('Europe/Paris');
	$marqueur_temps_maintenant = time();
	$marqueur_temps_maintenant = mktime(0, 0, 0, date("m", $marqueur_temps_maintenant)  , date("d", $marqueur_temps_maintenant) + 1, date("Y", $marqueur_temps_maintenant) );

	$link = mysqli_connect($mysql_host,$mysql_user,$mysql_password, $mysql_base);
	
	if (!$link) {
		$code_erreur = 1;
		echo("NOK;MySQL(connexion);failed_1");
		exit($code_erreur);
	}
		
	$query_1 = "SELECT aqcm_FIRMWARE_PWD,aqcm_FW_FILENAME,aqcm_FW_UPDATE_FF,aqcm_T_MAX FROM AQCM05_SONDES_FIRMWARE WHERE aqcm_MAC='" . $mac . "'";

	$result = mysqli_query($link, $query_1); 
	if (!$result) {	
		echo("NOK;MySQL(requête);failed_2");
		$code_erreur = 2;
		mysqli_close($link);
		exit($code_erreur);
	}

	if (mysqli_num_rows($result) != 1) {	
		echo("NOK;MySQL(lignes);failed_3");
		$code_erreur = 3;
		mysqli_close($link);
		exit($code_erreur);
	}

	if ($row = mysqli_fetch_array($result, MYSQLI_BOTH)) { 
		extract($row);
	}
	else {
		echo("NOK;MySQL(get_row);failed_4");
		$code_erreur = 4;
		mysqli_close($link);
		exit($code_erreur);
	}
	
	if ($aqcm_T_MAX <= $marqueur_temps_maintenant) {
		echo("NOK;MySQL(check_validity);failed_5");
		$code_erreur = 5;
		mysqli_close($link);
		exit($code_erreur);
	}

	if ( password_verify($server_updates_mdp, $aqcm_FIRMWARE_PWD ) ) {
		echo "OK;" . "$aqcm_FW_FILENAME" . ";" . "$aqcm_FW_UPDATE_FF";
		$code_erreur = 0;
		mysqli_close($link);
		exit($code_erreur);

	} else {
		echo("NOK;MySQL(check_password);failed_6");
		$code_erreur = 6;
		mysqli_close($link);
		exit($code_erreur);
	}
	

?>

