<?php
ini_set('max_execution_time', 60);
/* 
 * Interface d'administration de configuration de l'agent SIGB
 */
include('adminBroker.class.php');
include 'includes/functions.php';
define('INI_SERVER', '../serveur/config/clients.ini');
define('INI_SERVER_SAVE', '../serveur/config/save/');

$adminBroker = new adminBroker();




function check_config($dir){
    $tab_config = array();
        if (file_exists($dir)) {
        $dir_handle = @opendir($dir);
        $i=0;
        while($entry = readdir($dir_handle)){
            if(is_file($dir.$entry)){
                $date_tmp = explode("clients_",$entry);
                $date_tmp = $date_tmp[1];
                $date_tmp = explode(".",$date_tmp);
                $date_tmp = $date_tmp[0];
                $tab_config[$i]=$entry;
                $i++;
            }
        }
        //krsort($tab_config);
        closedir($dir_handle);
    }
    return $tab_config;
}

function compare_config($conf1,$conf2){
    $tab_conf1 = file(INI_SERVER_SAVE.$conf1);
    $tab_conf2 = file(INI_SERVER_SAVE.$conf2);
    $count_conf1 = count($tab_conf1);
    $count_conf2 = count($tab_conf2);

    $i = 0;
    if ($count_conf1==$count_conf2){
            $del = 1;
            while ($i < $count_conf1){
                     // if ($tab_conf1[$i]!=$tab_conf2[$i]) echo $tab_conf1[$i]." | ".$tab_conf2[$i]." <b>NOK</b><br>";
                     // else echo $tab_conf1[$i]." | ".$tab_conf2[$i]." OK<br>";

                    if ($tab_conf1[$i]!=$tab_conf2[$i]){
                            $del = 0;
                            break;
                    }

                    $i++;
            }
            //echo "$conf1 $count_conf1 | $conf2 $count_conf2 => <b>$del</b><hr>";
            //echo "<br>";

            if ($del==1) return $conf2;
            else return false;
    }	
}


$tab_config = check_config(INI_SERVER_SAVE);
$nb = count($tab_config);

for ($i=0; $i<$nb;$i++){
	
	$j_tmp="";
	$j = 0;
	$x = 0;
	while ($j < $nb){
		if ($j!=$i){
			$j_tmp .= $j.",";
			if (compare_config($tab_config[$i],$tab_config[$j])){
				if (!$list_config_del[compare_config($tab_config[$i],$tab_config[$j])]) 
					$list_config_del[compare_config($tab_config[$i],$tab_config[$j])] = compare_config($tab_config[$i],$tab_config[$j]);
			}
		}
		
		$j++;
	}

	// echo "i : $i => $j_tmp<br>";
	// echo "<hr>";
}

// echo "<pre>";
// print_r($list_config_del);
// echo "</pre>";

// exit();


if ($_POST['check']==3){	
	if (file_exists(INI_SERVER)) unlink(INI_SERVER);
	@copy(INI_SERVER_SAVE.'clients_'.$_POST['id_config_client'].'.ini', INI_SERVER);
}	

$tabConfigServeur = $adminBroker->readConfig(INI_SERVER);


if ($_POST['check']==2){
	$now = time();
        if (!file_exists(INI_SERVER_SAVE)) {
            mkdir(INI_SERVER_SAVE);
        }
	if(@copy(INI_SERVER, INI_SERVER_SAVE.'clients_'.$now.'.ini')){
	
		if (file_exists(INI_SERVER)) unlink(INI_SERVER);
		
		$file_ini = fopen(INI_SERVER, 'w+');
		foreach ($tabConfigServeur as $partConfig => $configTab) {	
			
			$id_client = "[".$partConfig."]";
			$key = 'key = '.$tabConfigServeur[$partConfig]['key'];
			
			fputs($file_ini, $id_client."\n");
			fputs($file_ini, $key."\n");
			
			if ($_POST['client']==$partConfig){
				$ip_agent = 'ip_agent = '.$_POST['ip'];
			}
			else $ip_agent = 'ip_agent = '.$tabConfigServeur[$partConfig]['ip_agent'];
			
			fputs($file_ini, $ip_agent."\n");
		}
		fclose($file_ini);
		$tabConfigServeur = $adminBroker->readConfig(INI_SERVER);
	}
	else $error = "Modification échoué : impossible de créer une copie du fichier clients.ini";
}

ksort($tabConfigServeur);

echo headerPage('Configuration de l\'IP de l\'agent SIGB');
?>

<div class="main">
		<?php if ($_POST['check']==2) echo "IP Client <b>\"".$_POST['client']."\"</b> modifie !<br><br>"; ?>
		
        <form action="configAgentIp.php" method="post">
            <table style="width:50%;margin:0 auto;">
                <tr>
                    <td>
						 
						<b><u>Liste des clients</u></b><br><br>
						<?php 
							echo '<select name="id_client">'; 
								foreach ($tabConfigServeur as $partConfig => $configTab) {	
								$data = '';
								foreach ($configTab as $value) {
									$data .= '|'.$value;
								}
								$value = $partConfig.$data;
								
								if ($_POST['id_client']){
									$idcli_tmp = explode("|",$_POST['id_client']);
									$idcli_tmp = $idcli_tmp[0];
								}
								else $idcli_tmp = "";
								
								echo '<option value="'.$value.'" ';
								
								if ($idcli_tmp==$partConfig) echo "selected";
								
								echo '>'.$partConfig.'</option>';
								//echo $partConfig.$data."<br>";
								}	
							echo '</select>';
					
						?>
						
						<input type="hidden" name="check" value="1">
						<input type="submit" name="Valider" value="Valider">
					</td>
                </tr>
                
			</table>
		</form>
		
		
		<?php
			
			if ($_POST['check']==1 || $_POST['check']==2){
				$id_client_tmp = explode("|",$_POST['id_client']);
				$client= $id_client_tmp[0];
				$key= $id_client_tmp[1];
				
				if ($_POST['ip']) $ip = $_POST['ip'];
				else $ip= $id_client_tmp[2];
			?>
			<br>
			<form action="configAgentIp.php" method="post">
				<input type="hidden" name="check" value="2">
				<input type="hidden" name="id_client" value="<?=$_POST['id_client'];?>">
				<input type="hidden" name="client" value="<?=$client;?>">
				<table style="width:60%;margin:0 auto;" border=0>
				
					<tr>
						<td width=30%>Client : </td>
						<td align="left"><input type="text" name="client2" value="<?=$client;?>" disabled="disabled"></td>
					</tr>
					
					<tr>
						<td>Key : </td>
						<td align="left"><input type="text" name="key" value="<?=$key;?>" disabled="disabled"></td>
					</tr>
					
					<tr>
						<td>Ip : </td>
						<?php
						if ($_POST['check']==2){
						?>
						<td align="left"><input type="text" name="ip" value="<?=$ip;?>" size="40" disabled="disabled"></td>
						<?php
						}
						else{
						?>
						<td align="left"><input type="text" name="ip" value="<?=$ip;?>" size="40"></td>
						<?php
						}
						?>
						
					</tr>
					
					<tr>
						<?php
						if ($_POST['check']==2){
						?>
						<td colspan="2"><input type="submit" value="Modifier" name="Modifier" disabled="disabled"></td>
						<?php
						}
						else{
						?>
						<td colspan="2"><input type="submit" value="Modifier" name="Modifier"></td>
						<?php
						}
						?>
					</tr>
					
				</table>
			</form>
			<br>
			<?php
				
			}
		?>
		
</div>
<br>

<?php

echo headerPage('Restauration fichier de configuration IP');

if (file_exists(INI_SERVER_SAVE)) {
    $dir_handle = @opendir(INI_SERVER_SAVE);
    $i=0;
    while($entry = readdir($dir_handle)){
            if(is_file(INI_SERVER_SAVE.$entry)){

                    $date_tmp = explode("clients_",$entry);
                    $date_tmp = $date_tmp[1];
                    $date_tmp = explode(".",$date_tmp);
                    $date_tmp = $date_tmp[0];
                    $tab[$date_tmp]=$entry;
                    $i++;
            }
    }
    @krsort($tab);
    closedir($dir_handle);
}

?>

<div class="main">
<?php
if ($_POST['check']==3){
	$d = date("d/m/Y à H:i:s",$_POST['id_config_client']); 
	echo "$file_client Fichier de configuration du <b>\"$d\"</b> restauré !<br><br>";
}
?>
<form action="configAgentIp.php" method="post">
            <table style="width:70%;margin:0 auto;">
                <tr>
                    <td>
						<b><u>Liste des points de restauration</u></b><br><br>
						<?php 
							if ($tab){
								echo '<select name="id_config_client">'; 
									
										foreach ($tab as $key => $value) {
											$d = date("d/m/Y à H:i:s",$key);
											echo "<option value=$key>$d</option>";
											//echo "key : $value : ".date("d/m/Y à H:i:s",$key)."<br>";
										}
									
								echo '</select>';
							
					
						?>
						
						<input type="hidden" name="check" value="3">
						<input type="submit" name="Valider" value="Valider">
						<?php
						}
							else echo "Aucun point de restauration";
						?>
						</td>
                </tr>
                
			</table>
		</form>
</div>
<br>
<?php

echo headerPage('Check adresse IP');

?>

<div class="main">
<?php

if ($_POST['check_ip']) $check_ip = $_POST['check_ip'];
else $check_ip = "";

if ($_POST['port']) $port = $_POST['port'];
else $port = "80";

if ($_POST['check']==4){
	$check_ip = $_POST['check_ip'];
	$port = $_POST['port'];
	
	$fp = @fsockopen($check_ip, $port, $errno, $errstr, 1);
	if (!$fp) echo "Impossible d'accéder à l'ip <b>\"$check_ip\"</b> sur le port <b>\"$port\"</b><br><br>";
    else echo "Connexion à l'ip <b>\"$check_ip\"</b> réussi sur le port <b>\"$port\"</b><br><br>";

}
?>
<form action="configAgentIp.php" method="post">
            <table style="width:70%;margin:0 auto;">
                <tr>
                    <td>
						<b><u>Ip à vérifier</u></b><br><br>
						<?php 
							
							
					
						?>
						IP : <input type="text" name="check_ip" value="<?=$check_ip;?>" size="20">
						Port : <input type="text" name="port" value="<?=$port;?>" size="5">
						<input type="hidden" name="check" value="4">
						<input type="submit" name="Valider" value="Valider">
						
						</td>
                </tr>
                
			</table>
		</form>
</div>

<?php
echo footerPage();
?>
