<?php
include_once ("../core/config.php");
$UTENTE = login();
$DIRITTI = diritti('Admin');
view_top();

if (isset($_POST['tempo'])) {
	$tempo = $_POST['tempo'];
} else {
	$tempo = "DAY";
}

if (isset($_POST['tipo']) && $_POST['tipo'] != '0') {
	$tipo = $_POST['tipo'];
	$querytipo1 = "AND firewall_vpn.tipo = '$tipo'";
	$querytipo2 = "AND firewall_macchine_ext.tipo = '$tipo'";
} else {
	$tipo = "";
	$querytipo1 = "";
	$querytipo2 = "";
}

if (isset($_POST['nome']) && $_POST['nome'] != '0') {
	$nome = $_POST['nome'];
	$querynome1 = "AND firewall_versointerno.nome = '$nome'";
	$querynome2 = "AND firewall_versoesterno.nome = '$nome'";
} else {
	$nome = "";
	$querynome1 = "";
	$querynome2 = "";
}

if (isset($_POST['idutente']) && $_POST['idutente'] > 0) {
	$idutente = $_POST['idutente'];
	$queryutente = "AND utenti.id = $idutente";
} else {
	$idutente = 0;
	$queryutente = "";
}

if (isset($_POST['idip']) && $_POST['idip'] > 0) {
	$idip = $_POST['idip'];
	$queryip = "AND proxy_pool.id = $idip";
} else {
	$idip = 0;
	$queryip = "";
}

$stato = array ("Disattivo", "Attivo", "Permanente");

$query_user = "	SELECT
			utenti.id,
			utenti.utente
		FROM
			utenti
		JOIN
			permessi
			ON
				permessi.id_utenti = utenti.id
		WHERE
			permessi.id_moduli = ".$MODULO['id']."
		ORDER BY
			utente
";
$res_user = mysql_query( $query_user, $DB_ID );

$query_nome = "	SELECT
			nome
		FROM 
			firewall_versoesterno
		UNION SELECT
			nome
		FROM
			firewall_versointerno
		GROUP BY
			nome
		ORDER BY
			nome
";
$res_nome = mysql_query( $query_nome, $DB_ID );

$query_tipo = "	SELECT
			tipo
		FROM
			firewall_vpn
		UNION SELECT
			tipo
		FROM
			firewall_macchine_ext
		GROUP BY
			tipo
		ORDER BY
			tipo
";
$res_tipo = mysql_query( $query_tipo, $DB_ID );
?>
<form name="" method="post">
<?php $tabella = new html (0,"90%", array (3,12,2,10,2,10,2,15,2,15,2,5,2,5,2,5,0)); 
$tabella->intestazione (array("", "Data", "", "Utente", "", "Nome Regola", "", "", "", "", "", "", "", "Tipo", "", "", "")); ?>
	<tr>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="spazioh">
			<select name="tempo" onchange="this.form.submit()">
				<option class="testo" value="HOUR" <?php if ($tempo == "HOUR") {print "selected=\"selected\""; } ?>>Ultima Ora</option>
				<option class="testo" value="DAY" <?php if ($tempo == "DAY") {print "selected=\"selected\""; } ?>>Ultimo Giorno</option>
				<option class="testo" value="WEEK" <?php if ($tempo == "WEEK") {print "selected=\"selected\""; } ?>>Ultima Settimana</option>
				<option class="testo" value="MONTH" <?php if ($tempo == "MONTH") {print "selected=\"selected\""; } ?>>Ultimo Mese</option>
				<option class="testo" value="YEAR" <?php if ($tempo == "YEAR") {print "selected=\"selected\""; } ?>>Ultimo Anno</option>
			</select>
		</td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="spazioh">
			<select name="idutente" onchange="this.form.submit()">
				<option class="testo" value=0 <?php if ($idutente == 0) {print "selected=\"selected\""; } ?>>----</option>
<?php while ($dato = mysql_fetch_array ( $res_user )) { ?>
				<option class="testo" value="<?php print $dato['id'] ?>" <?php if ($idutente == $dato['id']) {print "selected=\"selected\""; } ?>><?php print $dato['utente'] ?></option>
<?php } ?>
			</select>
		</td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="spazioh">
			<select name="nome" onchange="this.form.submit()">
				<option class="testo" value=0 <?php if ($idip == 0) {print "selected=\"selected\""; } ?>>----</option>
<?php while ($dato = mysql_fetch_array ( $res_nome )) { ?>
				<option class="testo" value="<?php print $dato['nome'] ?>" <?php if ($nome == $dato['nome']) {print "selected=\"selected\""; } ?>><?php print $dato['nome'] ?></option>
<?php } ?>
			</select>
		</td>
		<td class="spazioh"></td>
		<td class="spazioh"></td>
		<td class="spazioh"></td>
		<td class="spazioh"></td>
		<td class="spazioh"></td>
		<td class="spazioh"></td>
		<td class="spazioh"></td>
		<td class="spazioh">
			<select name="tipo" onchange="this.form.submit()">
				<option class="testo" value=0 <?php if ($idip == 0) {print "selected=\"selected\""; } ?>>----</option>
<?php while ($dato = mysql_fetch_array ( $res_tipo )) { ?>
				<option class="testo" value="<?php print $dato['tipo'] ?>" <?php if ($tipo == $dato['tipo']) {print "selected=\"selected\""; } ?>><?php print $dato['tipo']; ?></option>
<?php } ?>
			</select>
		</td>
		<td class="spazioh"></td>
		<td class="spazioh"></td>
		<td class="spazioh"></td>
	</tr>
<?php
$tabella->riga();
$tabella->intestazione (array("", "Data", "", "Utente", "", "Nome Regola", "", "Da", "", "A", "", "Proto", "", "Tipo", "", "Stato", ""));

$query = "	SELECT 
			firewall_logs.tabella,
			firewall_logs.stato,
			DATE_FORMAT(firewall_logs.data, '%d.%m.%Y %H:%i:%s') AS data,
			utenti.utente,
			firewall_versoesterno.nome AS reg_nome,
			firewall_versoesterno.int_port,
			firewall_versoesterno.ext_port,
			firewall_versoesterno.proto,
			proxy_pool.nome AS int_nome,
			proxy_pool.ip AS int_ip,
			firewall_macchine_ext.nome AS ext_nome,
			firewall_macchine_ext.ip AS ext_ip,
			firewall_macchine_ext.tipo
		FROM
			firewall_logs
		JOIN
			utenti
			ON
				firewall_logs.id_utenti = utenti.id
				$queryutente
		JOIN
			firewall_versoesterno_regole
			ON
				firewall_logs.id_regole = firewall_versoesterno_regole.id
				AND
				firewall_logs.tabella = firewall_versoesterno_regole.regola
		JOIN
			firewall_versoesterno
			ON
				firewall_versoesterno_regole.id_firewall_versoesterno = firewall_versoesterno.id
				$querynome2
		JOIN
			firewall_macchine
			ON
				firewall_versoesterno.id_firewall_macchine = firewall_macchine.id
		JOIN
			proxy_pool
			ON
				firewall_macchine.id_proxy_pool = proxy_pool.id
				$queryip
		JOIN
			firewall_macchine_ext
			ON
				firewall_versoesterno.id_firewall_macchine_ext = firewall_macchine_ext.id
				$querytipo2
		WHERE
			firewall_logs.data >= DATE_SUB(NOW(), INTERVAL 1 $tempo)

		UNION

		SELECT 
			firewall_logs.tabella,
			firewall_logs.stato,
			DATE_FORMAT(firewall_logs.data, '%d.%m.%Y %H:%i:%s') AS data,
			utenti.utente,
			firewall_versointerno.nome AS reg_nome,
			firewall_versointerno.int_port,
			firewall_versointerno.fw_port,
			firewall_versointerno.proto,
			proxy_pool.nome AS int_nome,
			proxy_pool.ip AS int_ip,
			firewall_vpn.utenza AS ext_nome,
			firewall_vpn.ip AS ext_ip,
			firewall_vpn.tipo
		FROM
			firewall_logs
		JOIN
			utenti
			ON
				firewall_logs.id_utenti = utenti.id
				$queryutente
		JOIN
			firewall_versointerno_regole
			ON
				firewall_logs.id_regole = firewall_versointerno_regole.id
				AND
				firewall_logs.tabella = firewall_versointerno_regole.regola
		JOIN
			firewall_versointerno
			ON
				firewall_versointerno_regole.id_firewall_versointerno = firewall_versointerno.id
				$querynome1
		JOIN
			firewall_macchine
			ON
				firewall_versointerno.id_firewall_macchine = firewall_macchine.id
		JOIN
			proxy_pool
			ON
				firewall_macchine.id_proxy_pool = proxy_pool.id
				$queryip
		JOIN
			firewall_vpn
			ON
				firewall_versointerno.id_firewall_vpn = firewall_vpn.id	
				$querytipo1
		WHERE
			firewall_logs.data >= DATE_SUB(NOW(), INTERVAL 1 $tempo)
		ORDER BY 
			data DESC

";

$res = mysql_query( $query, $DB_ID );
while ($dato = mysql_fetch_array ( $res )) {

	if ($dato['tabella'] == "versoesterno") {
		$da = $dato['int_ip']." (".$dato['int_nome']."):".$dato['int_port'];
		$a  = $dato['ext_ip']." (".$dato['ext_nome']."):".$dato['ext_port'];
	} else {
		$a  = $dato['int_ip']." (".$dato['int_nome']."):".$dato['int_port'];
		$da = $dato['ext_ip']." (".$dato['ext_nome']."):".$dato['ext_port'];
	}
?>
	<tr>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="colip"><?php print $dato['data'] ?></td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="colip"><?php print $dato['utente'] ?></td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="colip"><?php print $dato['reg_nome'] ?></td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="colip"><?php print $da ?></td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="colip"><?php print $a ?></td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="colip"><?php print $dato['proto'] ?></td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="colip"><?php print $dato['tipo'] ?></td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="colip"><?php print $stato[$dato['stato']] ?></td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
	</tr>
<?php
} 
$tabella->close();
?>
</form>
<?php view_footer(); ?>
