<?php
include_once ("core/config.php");
$UTENTE = login();
//isadmin();

include_once ("top_mini.php");

$id = $_POST['iddefrule'];
$quanti = $_POST['quanti'];

//print_r ($_POST);

for ($i=0,$errori=0,$insert=0,$update=0,$delete=0,$regole=0;$i<$quanti;$i++) {
	$idrule = "idrule$i";
	$fromip = "fromip$i";
	$fromport = "fromport$i";
	$toip = "toip$i";
	$toport = "toport$i";
	$check = "check$i";

	if (isset ($_POST[$check]) && ($_POST[$fromip] == '') && ($_POST[$fromport] == 0) && ($_POST[$toip] == '') && ($_POST[$toport] == 0) ) {
//		print "--- $i - ".$_POST[$check]." - ".$_POST[$fromip]." - ".$_POST[$fromport]." - ".$_POST[$toip]." - ".$_POST[$toport]. " ---<br>";
		$errore = 1;
		$i = $quanti;
	}

	if (isset ($_POST[$idrule])) {
		if (isset ($_POST[$check])) {
			$query[$regole++] = "UPDATE rule SET ipin='".$_POST[$fromip]."', portin=".$_POST[$fromport].", ipout='".$_POST[$toip]."', portout=".$_POST[$toport]." WHERE id =".$_POST[$idrule];
			$update++;
		} else {
			$query[$regole++] = "DELETE FROM rule WHERE id = ".$_POST[$idrule];
			$delete++;
		}
	} else {
		if (isset ($_POST[$check])) {
			$query[$regole++] = "INSERT INTO rule SET iddefrule=$id, ipin='".$_POST[$fromip]."', portin=".$_POST[$fromport].", ipout='".$_POST[$toip]."', portout=".$_POST[$toport];
			$insert++;
		} 
	}
}
?>

<table cellpadding="0" cellspacing="0" border="0" id="pulsanti">
<tbody>
	<tr><td colspan="2"><img src="img/spazio.gif" width="1" height="7" alt=""></td></tr>
<?php	if (!$errore) {
		for ($i=0;$i<$regole;$i++) {
			mysql_query( $query[$i], $DB_ID );
		}
		if ($regole > 0) {
			$querydefrule = "SELECT attivo FROM defrule WHERE id = $id";
			$res_defrule = mysql_query( $querydefrule, $DB_ID );
			$dato_defrule = mysql_fetch_array ( $res_defrule );
			if ($dato_defrule['attivo']) {
				azioni ("defrule", $id, 2);
			}
		}
?>
	<tr><td colspan="2" align="center">Effettuati <?php print $insert ?> inserimenti</td></tr>
	<tr><td colspan="2" align="center">Effettuate <?php print $delete ?> cancellazioni</td></tr>
	<tr><td colspan="2" align="center">Effettuati <?php print $update ?> aggiornamenti</td></tr>
	<tr><td colspan="2"><img src="img/spazio.gif" width="1" height="7" alt=""></td></tr>
	<tr>
		<td colspan="2" align="center"><input type="button" value="Ok" onclick="window.opener.location.reload(); window.close(); return false;""></button></td>
	</tr>

<?php	} else {
?>
	<tr><td colspan="2" align="center">L'operazione non puo' essere portatata a termine</td></tr>
	<tr><td colspan="2" align="center">Alcune regole non sono valide</td></tr>
	<tr><td colspan="2"><img src="img/spazio.gif" width="1" height="7" alt=""></td></tr>
	<tr>
		<td width="50%" align="center"><input type="button" value="Ripeti" onclick="location.href = 'rule.php?id=<?php print $id ?>';"></button></td>
		<td width="50%" align="center"><input type="button" value="Annulla" onclick="window.opener.location.reload(); window.close(); return false;""></button></td>
	</tr>

<?php	} ?>
	<tr><td colspan="2"><img src="img/spazio.gif" width="1" height="7" alt=""></td></tr>
</tbody>
</table>
<?php include_once ("footer_mini.php"); ?>
