<?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['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_ip = "	SELECT
			condes_macchine.id_proxy_pool AS id,
			proxy_pool.nome,
			proxy_pool.ip
		FROM 
			condes_macchine
		JOIN
			proxy_pool
			ON
				condes_macchine.id_proxy_pool = proxy_pool.id
		ORDER BY
			-proxy_pool.nome DESC,
			INET_ATON(proxy_pool.ip) ASC
";
$res_ip = mysql_query( $query_ip, $DB_ID );
	
?>

<form name="" method="post">
<?php $tabella = new html (0,"90%", array (3,15,2,10,2,20,2,10,2,10,0)); ?>
	<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 value="HOUR" <?php if ($tempo == "HOUR") {print "selected=\"selected\""; } ?>>Ultima Ora</option>
				<option value="DAY" <?php if ($tempo == "DAY") {print "selected=\"selected\""; } ?>>Ultimo Giorno</option>
				<option value="WEEK" <?php if ($tempo == "WEEK") {print "selected=\"selected\""; } ?>>Ultima Settimana</option>
				<option value="MONTH" <?php if ($tempo == "MONTH") {print "selected=\"selected\""; } ?>>Ultimo Mese</option>
				<option 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 value=0 <?php if ($idutente == 0) {print "selected=\"selected\""; } ?>>----</option>
<?php while ($dato = mysql_fetch_array ( $res_user )) { ?>
				<option 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="idip" onchange="this.form.submit()">
				<option value=0 <?php if ($idip == 0) {print "selected=\"selected\""; } ?>>----</option>
<?php while ($dato = mysql_fetch_array ( $res_ip )) { ?>
				<option value="<?php print $dato['id'] ?>" <?php if ($idip == $dato['id']) {print "selected=\"selected\""; } ?>><?php print $dato['ip']; if ($dato['nome'] != "") print " (".$dato['nome'].")" ?></option>
<?php } ?>
			</select>
		</td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
	</tr>
<?php
$tabella->riga();
$tabella->intestazione (array("", "Data", "", "Utente", "", "Macchina", "", "ACL", "", "Stato", ""));

$query = "	SELECT 
			condes_logs.id,
			condes_logs.stato,
			DATE_FORMAT(condes_logs.data, '%d.%m.%Y %H:%i:%s') AS data,
			utenti.utente,
			proxy_pool.nome AS nome_pool,
			proxy_pool.ip,
			proxy_acl_list.nome
		FROM
			condes_logs
		JOIN
			utenti
			ON
				condes_logs.id_utenti = utenti.id
				$queryutente
		JOIN
			condes_regole
			ON
				condes_logs.id_condes_regole = condes_regole.id
		JOIN
			proxy_acl_list
			ON
				condes_regole.id_proxy_acl_list = proxy_acl_list.id
		JOIN
			condes_macchine
			ON
				condes_regole.id_condes_macchine = condes_macchine.id
		JOIN
			proxy_pool
			ON
				condes_macchine.id_proxy_pool = proxy_pool.id
				$queryip
		WHERE
			condes_logs.data >= DATE_SUB(NOW(), INTERVAL 1 $tempo)
		ORDER BY 
			condes_logs.data DESC
";

$res = mysql_query( $query, $DB_ID );
while ($dato = mysql_fetch_array ( $res )) {
?>
	<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['ip']; if ($dato['nome_pool'] != "") print " (".$dato['nome_pool'].")" ?></td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="colip"><?php print $dato['nome'] ?></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>
		</td>
	</tr>
<?php
} 
$tabella->close();
?>
</form>
<?php view_footer(); ?>
