<?php
	
	$Flag_debug_1 = 1;
	$Flag_debug_1 = 0;

	$code_erreur = 0;
	$NB_sondes   = 0 ;
	
	//--------------------------------------------------------------------------------------------------------
	// connexion à la base de données
	//--------------------------------------------------------------------------------------------------------
	include ("../_identifiants/sql_identification_sans_base.php");
	
	$mysql_base = 'AQCM05_SITE_' . $_SESSION['SITE_ID'] ;
	
	$link_configuration = mysqli_connect($mysql_host,$mysql_user,$mysql_password, $mysql_base);
	
	if (!$link_configuration) {
		$code_erreur = 100;
		
		if ($Flag_debug_1 == 1) {
			echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL; echo '</br>';
			echo "Debugging error: " . mysqli_connect_error() . PHP_EOL; echo '</br>';
		}

		echo "Connexion impossible à la base de donnée MySQL !" . PHP_EOL; echo '</br>';
		exit($code_erreur);
	}

	//--------------------------------------------------------------------------------------------------------
	// recherche des sondes du site demandé
	//--------------------------------------------------------------------------------------------------------
	if ( $_SESSION['level'] == 3 ) {
		
		$filtre_additionnel = " AND aqcm_MAIL='" . $_SESSION['login'] . "'";

	}
	else {
		$filtre_additionnel = "";
	}

	//--------------------------------------------------------------------------------
	if ( $_SESSION['aqcm_BAT'] == 'B99' ) {

		if ( $_SESSION['aqcm_NIVEAU'] == 'N99' ) {
			$query_configuration = "
				SELECT aqcm_MAC,aqcm_NAME,aqcm_SERVER_PWD,aqcm_SITE_ID,aqcm_MESURES 
				FROM AQCM05_SONDES_MESURES 
				WHERE (aqcm_SITE_ID='" . $_SESSION['SITE_ID'] . "'" . $filtre_additionnel . ") ORDER BY  aqcm_NAME";
		} else {
			$query_configuration = "
				SELECT aqcm_MAC,aqcm_NAME,aqcm_SERVER_PWD,aqcm_SITE_ID,aqcm_MESURES 
				FROM AQCM05_SONDES_MESURES 
				WHERE (aqcm_SITE_ID='" . $_SESSION['SITE_ID'] . "' AND aqcm_NIVEAU='" . $_SESSION['aqcm_NIVEAU'] . "'" . $filtre_additionnel . ") ORDER BY  aqcm_NAME";
		}

	} else {
		if ( $_SESSION['aqcm_NIVEAU'] == 'N99' ) {
			$query_configuration = "
				SELECT aqcm_MAC,aqcm_NAME,aqcm_SERVER_PWD,aqcm_SITE_ID,aqcm_MESURES 
				FROM AQCM05_SONDES_MESURES 
				WHERE (aqcm_SITE_ID='" . $_SESSION['SITE_ID'] . "' AND aqcm_BAT='" . $_SESSION['aqcm_BAT'] . "'" . $filtre_additionnel . ") ORDER BY  aqcm_NAME";
		} else {
			$query_configuration = "
				SELECT aqcm_MAC,aqcm_NAME,aqcm_SERVER_PWD,aqcm_SITE_ID,aqcm_MESURES 
				FROM AQCM05_SONDES_MESURES 
				WHERE (aqcm_SITE_ID='" . $_SESSION['SITE_ID'] . "' AND aqcm_BAT='" . $_SESSION['aqcm_BAT'] . "' AND aqcm_NIVEAU='" . $_SESSION['aqcm_NIVEAU'] . "'" . $filtre_additionnel . ") ORDER BY  aqcm_NAME";
		}
	}		

	if ($Flag_debug_1 == 1) {
		echo $mysql_base . "</br>" ;
		echo $query_configuration . "</br>" ;
	}

	$code_erreur = 0;

	$result_configuration = mysqli_query($link_configuration, $query_configuration); 
	
	if (!$result_configuration) {
		
		$code_erreur = 11;
		
	} else {
		
		if (mysqli_num_rows($result_configuration) == 0) {	
			$code_erreur = 12;
			
		} else {
			
			$i=0;
			while ($row = mysqli_fetch_array($result_configuration, MYSQLI_BOTH)) {

				$aqcm_MAC[$i]				= $row["aqcm_MAC"];
				$aqcm_NAME[$i]		 	= $row["aqcm_NAME"];
				$aqcm_SERVER_PWD[$i] 	= $row["aqcm_SERVER_PWD"];
				$aqcm_SITE_ID[$i]	 	= $row["aqcm_SITE_ID"];
				$aqcm_MESURES[$i]    	= $row["aqcm_MESURES"];
				
				if ($Flag_debug_1 == 1) {
					echo "i=" . $i . " ; " . $aqcm_MAC[$i] . " ; " . $aqcm_NAME[$i] . " ; " . $aqcm_SERVER_PWD[$i] . " ; " . $aqcm_SITE_ID[$i] . " ; " . $aqcm_MESURES[$i] . "</br>";
				}
				
				$i++ ;
			}
			
			$NB_sondes = $i;
			
			if ($Flag_debug_1 == 1) {
				echo "NB_sondes = " . $NB_sondes . "</br>";
				
			}
		}
	}

	if ($Flag_debug_1 == 1) {
		if ( $code_erreur != 0 ) {
			echo "code erreur = " . strval($code_erreur) . "</br>";
		}
	}

	mysqli_close($link_configuration);

?>
