= 10000000000) return sprintf ("%.2f GB", $b/1000000000); if ($b >= 10000000) return sprintf ("%.2f MB", $b/1000000); if ($b >= 10000) return sprintf ("%.2f KB", $b/1000); return "$b B"; } $rdns_cache = false; function get_rdns_name ($ip=false) { global $rdns_cache; if (!is_array ($rdns_cache)) { $rdns_cache = array(); $q = mysql_query ("select rdns_ip, rdns_mono_name, rdns_ptr from rdns"); while ($q && ($row = mysql_fetch_row ($q))) { $rdns_cache[$row[0]] = isset($row[2]) ? $row[2] : $row[1]; } } if ($ip !== false && isset ($rdns_cache[$ip])) return $rdns_cache[$ip]; if ($ip == "0.0.0.0") return "WAN"; return $ip; } function print_traffic_graphs ($options) { global $adminmode; $ip = $options["ip"]; $t = time(); foreach (array (7200, 86400, 2592000, 31557600) as $span) { print "
\n"; } } function print_traffic_table ($options) { global $adminmode; $period = $options["period"]; $ip = $options["ip"]; if ($adminmode) get_rdns_name(); // populate cache $where = "where 1=1"; if (!$adminmode || !$options["top"]) { $rowlabel_type = "date"; $where .= " and a.traffic_history_ip='$ip'"; } $table = "traffic_history$testonly"; if ($period == "day") { if (isset ($_REQUEST["month"])) { $firstday = ereg_replace ("[^-/0-9]", "", $_REQUEST["month"]); } else { $row = mysql_fetch_row (mysql_query ("select now()")); $firstday = $row[0]; } $firstday = ereg_replace ("[^0-9][0-9][0-9]?( .*)?$", "-01", $firstday); $where .= " and a.traffic_history_date >= '$firstday'"; $where .= " and a.traffic_history_date <= last_day('$firstday')"; $firstday_a = " a.traffic_history_date"; $dategroup_a = " a.traffic_history_date"; $jointable = "traffic_history$testonly"; $datesort = "traffic_history_date"; } else { $where .= " and a.traffic_history_month >= extract(year_month from date_add(now(),interval -3 month))"; $firstday_a = " concat(a.traffic_history_month,'01')"; $dategroup_a = " a.traffic_history_month"; if (mysql_query ("select count(*) from traffic_history_month$testonly")) { $jointable = "traffic_history_month$testonly"; $datesort = "traffic_history_month"; } else { $jointable = "traffic_history$testonly"; $datesort = "traffic_history_date"; } } $dategroup_b = ereg_replace (" a\.", " b.", $dategroup_a); $dategroup_c = ereg_replace (" a\.", " c.", $dategroup_a); if ($adminmode) $limit = "limit 2000"; elseif ($period == "day") $limit = "limit 31"; else $limit = "limit 12"; $q = mysql_query ("select $dategroup_a dategroup, max($dategroup_b) dategroup_b, if(max(b.traffic_history_total) is null or max(b.traffic_history_total) > min(a.traffic_history_total), max(a.traffic_history_total), max(a.traffic_history_total) -max(b.traffic_history_total)) ip_month_traffic, a.traffic_history_ip ip, if(min(a.traffic_history_date)=max(a.traffic_history_date), date_format(min(a.traffic_history_date),'%M %e, %Y'), concat(date_format(min(a.traffic_history_date),'%M %e'), ' to ', date_format(max(a.traffic_history_date),'%e, %Y'))) nicedaterange, max(a.traffic_history_in) inbytes, max(a.traffic_history_out) outbytes, max(a.traffic_history_total) totalbytes, if(min(a.traffic_history_in)>=max(b.traffic_history_in), max(a.traffic_history_in)-max(b.traffic_history_in), max(a.traffic_history_in)) inbytes_thistime, if(min(a.traffic_history_out)>=max(b.traffic_history_out), max(a.traffic_history_out)-max(b.traffic_history_out), max(a.traffic_history_out)) outbytes_thistime, if(min(a.traffic_history_total)>=max(b.traffic_history_total), max(a.traffic_history_total)-max(b.traffic_history_total), max(a.traffic_history_total)) totalbytes_thistime from $table a left join $jointable b on a.traffic_history_ip=b.traffic_history_ip and date_add($firstday_a,interval -1 day)=b.traffic_history_date $where group by a.traffic_history_ip, $dategroup_a having ip_month_traffic > 0 order by a.$datesort desc, ip_month_traffic desc $limit"); echo mysql_error(); $dategroup_last = false; $dategroup_rows = 0; print "\n"; while ($row = mysql_fetch_assoc ($q)) { if ($dategroup_last != $row["dategroup"]) { if (($adminmode && $options["top"]) || $dategroup_last == false) { print ""; foreach (array ("  Download", " Upload", " Total") as $a) print ""; print "\n"; } $dategroup_last = $row["dategroup"]; $dategroup_rows = 0; } if ($options["top"] > 0 && ++$dategroup_rows > $options["top"]) continue; print ""; print ""; print ""; $star = ""; if (!isset($row["dategroup_b"])) $star = "*"; print ""; print ""; print ""; print "\n"; } print "
"; if (!$adminmode) { print "$ip"; } elseif ($options["period"] == "month" || $options["top"]) { $nicedaterange = $row["nicedaterange"]; if ($period != "day") $nicedaterange = ereg_replace ("([0-9]+ to )?[0-9]+, ", "", $nicedaterange); print ereg_replace (" ", " ", $nicedaterange); } else { } print "$a
"; if ($adminmode) { if ($options["top"]) print "".htmlspecialchars(get_rdns_name($row["ip"])).""; else print htmlspecialchars($row["nicedaterange"]); } print " "; if (!$adminmode) print ereg_replace (" ", " ", $row["nicedaterange"]); print " $star".nicesize($row["inbytes_thistime"])." $star".nicesize($row["outbytes_thistime"])." $star".nicesize($row["totalbytes_thistime"])."
\n"; } ?>