<?php
	
	//--------------------------------------------------------------------------------------------------------
	// initialisation des variables
	//--------------------------------------------------------------------------------------------------------
	$Flag_debug_1 = 1;
	$Flag_debug_1 = 0;

	$code_erreur = 0;
	$NB_sondes   = 0 ;
	
	//--------------------------------------------------------------------------------------------------------
	// connexion à la base de données
	//--------------------------------------------------------------------------------------------------------
	$mysql_base ='AQCM05_SITE_' . $_SESSION['SITE_ID'] ;
	$link = mysqli_connect($mysql_host,$mysql_user,$mysql_password, $mysql_base);
	
	if ( ! $link) {
		
		$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['aqcm_BAT'] == 'B99' ) {

		if ( $_SESSION['aqcm_NIVEAU'] == 'N99' ) {
			$query = "
				SELECT aqcm_WG_NAME,aqcm_WG_BAT,aqcm_WG_NIVEAU,aqcm_WG_Peer_AllowedIPs,aqcm_WG_MAC,aqcm_WG_IP_LAN,aqcm_WG_STATUS 
				FROM AQCM05_SONDES_WG 
				ORDER BY  aqcm_WG_Peer_AllowedIPs" ;
		} else {
			$query = "
				SELECT aqcm_WG_NAME,aqcm_WG_BAT,aqcm_WG_NIVEAU,aqcm_WG_Peer_AllowedIPs,aqcm_WG_MAC,aqcm_WG_IP_LAN,aqcm_WG_STATUS 
				FROM AQCM05_SONDES_WG 
				WHERE (aqcm_WG_NIVEAU='" . $_SESSION['aqcm_NIVEAU'] . "') ORDER BY  aqcm_WG_Peer_AllowedIPs" ;
		}

	} else {
		if ( $_SESSION['aqcm_NIVEAU'] == 'N99' ) {
			$query = "
				SELECT aqcm_WG_NAME,aqcm_WG_BAT,aqcm_WG_NIVEAU,aqcm_WG_Peer_AllowedIPs,aqcm_WG_MAC,aqcm_WG_IP_LAN,aqcm_WG_STATUS 
				FROM AQCM05_SONDES_WG 
				WHERE (aqcm_WG_BAT='" . $_SESSION['aqcm_BAT'] . "') ORDER BY  aqcm_WG_Peer_AllowedIPs";
		} else {
			$query = "
				SELECT aqcm_WG_NAME,aqcm_WG_BAT,aqcm_WG_NIVEAU,aqcm_WG_Peer_AllowedIPs,aqcm_WG_MAC,aqcm_WG_IP_LAN,aqcm_WG_STATUS 
				FROM AQCM05_SONDES_WG 
				WHERE (aqcm_WG_BAT='" . $_SESSION['aqcm_BAT'] . "' AND aqcm_WG_NIVEAU='" . $_SESSION['aqcm_NIVEAU'] . "') ORDER BY  aqcm_WG_Peer_AllowedIPs";
		}
	}		

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

	$code_erreur = 0;

	$result_configuration = mysqli_query($link, $query); 
	
	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)) {

				$koko	= $row["aqcm_WG_Peer_AllowedIPs"];
				$koko	= explode("/", $koko) ;
				
				
				$aqcm_WG_NAME[$i]				= $row["aqcm_WG_NAME"] ;
				$aqcm_WG_BAT[$i]					= $row["aqcm_WG_BAT"] ;
				$aqcm_WG_NIVEAU[$i]				= $row["aqcm_WG_NIVEAU"] ;
				$aqcm_WG_Peer_AllowedIPs[$i]	= $koko[0] ;
				$aqcm_WG_MAC[$i]					= $row["aqcm_WG_MAC"] ;
				$aqcm_WG_IP_LAN[$i]				= $row["aqcm_WG_IP_LAN"] ;
				$aqcm_WG_STATUS[$i]				= $row["aqcm_WG_STATUS"] ;
				
				if ($Flag_debug_1 == 1) {
					echo "i=" . $i . " ; " . $aqcm_WG_NAME[$i] . " ; " . $aqcm_WG_Peer_AllowedIPs[$i] . " ; " . $aqcm_WG_MAC[$i] . " ; " . $aqcm_WG_IP_LAN[$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);

?>
