<?php
	
	//-----------------------------------------------------------------------------------------------------------------
	// requête pour les paramètres côté "serveur"
	//-----------------------------------------------------------------------------------------------------------------
	$query_get_parameters = "SELECT aqcm_WG_Server_Address,aqcm_WG_Server_PrivateKey,aqcm_WG_Server_PublicKey,aqcm_WG_server_AllowedIPs,aqcm_WG_URL,aqcm_WG_ListenPort,aqcm_WG_Keepalive FROM AQCM05_main.AQCM05_WG_SERVER WHERE (idx=1)";

	$result = mysqli_query($link, $query_get_parameters); 
	
	if ( !$result ) {
		
		$code_erreur = 11;
		echo "MySQL = " . $code_erreur . "<br>"; flush() ;
		echo "</br>";  flush() ;

	}
	else {
		
		if (mysqli_num_rows($result) != 1) {
			
			$code_erreur = 12;
			echo "MySQL = " . $code_erreur . "<br>"; flush() ;
			echo "</br>";  flush() ;

		}
		else {
			
			if ($row = mysqli_fetch_array($result, MYSQLI_BOTH)) { 
				extract($row);
				
			}
			else {
				
				$code_erreur = 13;
				echo "MySQL = " . $code_erreur . "<br>"; flush() ;
				echo "</br>";  flush() ;

			}
		}
	}

	//-------------------------------------------------------------------------
	if ( $Flag_debug ) {
		echo( "<br>");
		echo( "serveur" . "<br>");
		echo( $aqcm_WG_Server_Address . "<br>");
		echo( $aqcm_WG_Server_PrivateKey . "<br>");
		echo( $aqcm_WG_Server_PublicKey . "<br>");
		echo( $aqcm_WG_server_AllowedIPs . "<br>");
		echo( $aqcm_WG_URL . "<br>");
		echo( $aqcm_WG_ListenPort . "<br>");
		echo( $aqcm_WG_Keepalive . "<br>");
	}

	//-----------------------------------------------------------------------------------------------------------------
	// requêtes pour les paramètres côtés "clients"
	//-----------------------------------------------------------------------------------------------------------------
	if ( $code_erreur == 0 ) {
		include ("../M0/get_sites.php");
		if ( $NB_sites <= 0 ) {
			$code_erreur = 14 ;
		}
	}
	
	if ( $code_erreur == 0 ) {

		$Nb_peers = 0 ;

		for( $i=0; $i<$NB_sites; $i++ ) {
		
			if ( ( $aqcm_SITES_STATUS[$i] == "ON" ) && ( intval($aqcm_SITES_T_MAX[$i]) > $marqueur_temps_maintenant ) ) {

				$query_get_parameters = "
					SELECT aqcm_WG_Peer_PublicKey,aqcm_WG_Peer_AllowedIPs,aqcm_WG_T_MAX 
					FROM AQCM05_SITE_" . $aqcm_SITES_ID[$i] . " . AQCM05_SONDES_WG 
					WHERE ( ( aqcm_WG_STATUS = 'ON' ) AND ( aqcm_WG_T_MAX > " . $marqueur_temps_maintenant . " ) )
				";

				if ( $Flag_debug ) {
					echo( "---- DEBUG----<br>");
					echo( $query_get_parameters . "<br>");
					echo( "---- DEBUG----<br>");
				}

				$result = mysqli_query($link, $query_get_parameters); 
				
				if (!$result) {
					
					if ( $Flag_debug ) {
						echo "MySQL -> erreur lors de la requête !<br>"; flush() ;
						echo "</br>";  flush() ;
					}
					
				}
				else {
					
					if (mysqli_num_rows($result) == 0) {
						
						if ( $Flag_debug ) {
							echo "MySQL -> résultat de la requête vide !<br>"; flush() ;
							echo "</br>";  flush() ;
						}
						
					}
					else {

						while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)) {
							$aqcm_WG_Peer_PublicKey[$Nb_peers]  = $row["aqcm_WG_Peer_PublicKey"] ;
							$aqcm_WG_Peer_AllowedIPs[$Nb_peers] = $row["aqcm_WG_Peer_AllowedIPs"] ;
							if ( $Flag_debug ) {
								echo("$aqcm_WG_Peer_PublicKey[$Nb_peers] ; $aqcm_WG_Peer_AllowedIPs[$Nb_peers]<br>") ;
							}
							$Nb_peers++ ;
							
						}
					}
				}

			}
			else {
				
				if ( $Flag_debug ) {
					echo( "---- DEBUG----<br>");
					echo( "site : " . $aqcm_SITES_ID[$i] . " ; STATUS = " . $aqcm_SITES_STATUS[$i] . " ; T_max = " . $aqcm_SITES_T_MAX[$i] . "<br>");
					echo( "---- DEBUG----<br>");
				}

			}
		
		}

	}
	
	if ( $Nb_peers == 0 ) {
		$code_erreur = 15 ;
	}



	//-----------------------------------------------------------------------------------------------------------------
	
?>
