<?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['ordina'])) {
	$ordina = $_POST['ordina'];
} else {
	$ordina = "INET_ATON(ip)";
}
?>

<form name="" method="post">
<?php $tabella = new html (0,"90%", array (3,3,2,3,2,10,2,15,2,10,2,10,2,0)); ?>
	<tr>
		<td rowspan=2 class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td rowspan=2 class="spazioh"><a href="pool_ip.php"><img src="<?php print $CONF['base_url'] ?>/img/addresource.png"></a></td>
		<td rowspan=2 colspan=4 class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td colspan=2 class="spazioh">Periodo:&nbsp;</td>
		<td colspan=4 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 rowspan=2 class="spaziov" colspan=4><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
	</tr>
	<tr>
		<td colspan=2 class="spazioh">Ordinamento:&nbsp;</td>
		<td colspan=4 class="spazioh">
			<select name="ordina" onchange="this.form.submit()">
				<option value="INET_ATON(ip)" <?php if ($ordina == "ip") {print "selected=\"selected\""; } ?>>IP</option>
				<option value="inviati DESC" <?php if ($ordina == "inviati DESC") {print "selected=\"selected\""; } ?>>Dati Inviati</option>
				<option value="ricevuti DESC" <?php if ($ordina == "ricevuti DESC") {print "selected=\"selected\""; } ?>>Dati Ricevuti</option>
			</select>
		</td>
	</tr>
<?php
$tabella->riga();
$query = "	SELECT 
			GREATEST (sum(inviati), sum(ricevuti)) AS max 
		FROM 
			proxy_dati 
		WHERE 
			data_destroy >= DATE_SUB(NOW(), INTERVAL 1 $tempo) 
		GROUP BY 
			ip
		UNION
			SELECT
				GREATEST (sum(inviati), sum(ricevuti)) AS max 
			FROM 
				proxy_dati_storico
			WHERE 
				data_destroy >= DATE_SUB(NOW(), INTERVAL 1 $tempo) 
			GROUP BY 
				ip
		";

$res = mysql_query( $query, $DB_ID );
$max = 0;
while ($valori = mysql_fetch_array ( $res )) {
	if ($valori['max'] > $max) {
		$max = $valori['max'];
	}
}

$query = "	SELECT 
			proxy_pool.id,
			proxy_pool.nome,
			proxy_pool.attivo,
			querytot.ip, 
			MIN(querytot.minimo) AS first, 
			MAX(querytot.massimo) AS last, 
			SUM(querytot.recv) AS inviati,
			SUM(querytot.send) AS ricevuti
		FROM (
			SELECT
				ip,
				data_destroy,
				MIN(data_destroy) AS minimo,
				MAX(data_destroy) AS massimo,
				SUM(inviati) AS recv,
				SUM(ricevuti) AS send
			FROM
				proxy_dati
			WHERE
				data_destroy >= DATE_SUB(NOW(), INTERVAL 1 $tempo)
			GROUP BY
				ip
			UNION SELECT
				ip,
				data_destroy,
				MIN(data_destroy) AS minimo,
				MAX(data_destroy) AS massimo,
				SUM(inviati) AS recv,
				SUM(ricevuti) AS send
			FROM
				proxy_dati_storico
			WHERE
				data_destroy >= DATE_SUB(NOW(), INTERVAL 1 $tempo)
			GROUP BY
				ip
		) querytot
		JOIN
			proxy_pool
		ON
			querytot.ip = proxy_pool.ip
		WHERE
			data_destroy >= DATE_SUB(NOW(), INTERVAL 1 $tempo)	
		GROUP BY
			ip
		UNION DISTINCT SELECT
			proxy_pool.id,
			proxy_pool.nome,
			proxy_pool.attivo,
			proxy_pool.ip,
			DATE_SUB(NOW(), INTERVAL 1 $tempo) AS first,
			NOW() AS last,
			0,
			0
		FROM
			proxy_pool
		WHERE
			proxy_pool.ip NOT IN (	SELECT 
							ip 
						FROM 
							proxy_dati 
						WHERE 
							data_destroy >= DATE_SUB(NOW(), INTERVAL 1 $tempo)
						UNION SELECT
							ip 
						FROM 
							proxy_dati_storico
						WHERE 
							data_destroy >= DATE_SUB(NOW(), INTERVAL 1 $tempo)
						)
		ORDER BY
			$ordina
	";

$res = mysql_query( $query, $DB_ID );
while ($dato = mysql_fetch_array ( $res )) {
?>
	<tr>
<?php
	if ($dato['attivo']) {
?>
		<td rowspan=2 class="col3btn"><a href="proxy_gest.php?azione=pool_rem&id=<?php print $dato['ip']?>"><img src="<?php print $CONF['base_url'] ?>/img/non_attivo.gif" ALT="Utente limitato" TITLE="Utente limitato"></a></td>
<?php	} else { ?>
		<td rowspan=2 class="col3btn"><a href="proxy_gest.php?azione=pool_ins&id=<?php print $dato['ip']?>"><img src="<?php print $CONF['base_url'] ?>/img/attivo.gif" ALT="Utente non limitato" TITLE="Utente non limitato"></a></td>
<?php	} ?>
		<td rowspan=2 class="col3btn"><a href="pool_ip.php?id=<?php print $dato['id'] ?>"><img src="<?php print $CONF['base_url'] ?>/img/modify.gif" ALT="Dettaglio" TITLE="Dettaglio"></a></td>
		<td rowspan=2 class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td rowspan=2 class="spazioh"><a href="pool_acl.php?id=<?php print $dato['id'] ?>"><img src="<?php print $CONF['base_url'] ?>/img/edit-regole.gif" ALT="ACL" TITLE="ACL"></a></td>
		<td rowspan=2 class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="colip"><?php print $dato['ip'] ?></td>
		<td rowspan=2 class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="colip"><?php print $dato['first'] ?></td>
		<td rowspan=2 class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="descrizione">Inviati</td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="colspeed"><?php print byteConvert($dato['inviati']) ?></td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="colimg"><img border="0" src="barra.php?max=<?php print $max ?>&val=<?php print $dato['inviati'] ?>&col=red"></td>
		</td>
	</tr>
	<tr>
		<td class="colip"><?php if ($dato['nome'] != "") { print $dato['nome']; } else { $host = gethostbyaddr ($dato['ip']); print substr($host, 0, strpos($host, ".")); } ?></td>
		<td class="colip"><?php print $dato['last'] ?></td>
		<td class="descrizione">Ricevuti</td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="colspeed"><?php print byteConvert($dato['ricevuti']) ?></td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="colimg"><img border="0" src="barra.php?max=<?php print $max ?>&val=<?php print $dato['ricevuti'] ?>&col=blue"></td>
	</tr>
<?php
$tabella->riga();
} 
$tabella->close();
?>
</form>
<?php view_footer(); ?>
