#!/usr/local/bin/php -q
<?

if(ini_get("max_execution_time")!=0) die("Go get PHP 4.3.x, please.");

include("../config.inc.php");
include("../../../includes/geoip.inc");
include("../../../includes/functions.php");
include("../../../includes/worldinfos.php");

$global=0;

$net=cidr2long_net($my_net);

$gi=geoip_open("../../../includes/GeoIP.dat",GEOIP_STANDARD);
$fp=fopen($log_fullpath,"r");

while ($line=fgets($fp,255)) {

  $words=explode(" ",$line);

	if($words[1]!="honeyd" && !in_array($words[4],$net) && substr($words[1],0,3)=="tcp") {

		$ip=$words[3];
		$port=trim(strtok($words[6],":"));
		if($port) {
			$ports[$port]++;
			if($cache[$ip]=="") {
				$cache[$ip][0]=geoip_country_code_by_addr($gi,$ip);
				while(list($cont_n,$val)=each($cont)) if(strstr($val,$cache[$ip][0])!="") $cache[$ip][1]=$cont_n;
			}

			if($cache[$ip][0]) {
				$table_regions["ALL"][$port]++;
				$table_regions[$cache[$ip][1]][$port]++;
				$table_countries[$cache[$ip][0]][$port]++;
				$global++;
			}
		}
	}
}

fclose($fp);
geoip_close($gi);

## color palette
arsort($ports,SORT_NUMERIC);
$i=0;
while(list($port)=each($ports)) {
if($i==10) break;
if($i==0) { $red=204; $green=0; $blue=0; }
if($i==1) { $red=255; $green=204; $blue=0; }
if($i==2) { $red=0; $green=128; $blue=0; }
if($i==3) { $red=0; $green=64; $blue=128; }
if($i==4) { $red=165; $green=220; $blue=179; }
if($i==5) { $red=128; $green=0; $blue=128; }
if($i==6) { $red=166; $green=224; $blue=31; }
if($i==7) { $red=255; $green=255; $blue=0; }
if($i==8) { $red=255; $green=128; $blue=0; }
if($i==9) { $red=128; $green=0; $blue=0; }
$color[$port]="$red,$green,$blue";
$i++;
}

## generate piecharts...

### world map (main)

# generate continent pies
for($i=0;$i<count($c_list);$i++) {
$data="";
$legend="";
$total_c=0;

@reset($table_regions[$c_list[$i]]);
while(list($port,$nmb)=@each($table_regions[$c_list[$i]])) $total_c+=$nmb;

@reset($table_regions[$c_list[$i]]);
while(list($port,$nmb)=@each($table_regions[$c_list[$i]])) {
	if(floor(($nmb/$total_c)*360)) {
		$legend.="$port:".$color[$port].";";
		$data.="$nmb:".$color[$port].";";
	}
}
$imgsize=round(($total_c/$global)*50);
if($c_list[$i]=="ALL") $imgsize=100;
if($data=="" || $imgsize<16) genpie($data,15,"../../../images/$c_list[$i].png");
else genpie($data,$imgsize,"../../../images/$c_list[$i].png");
}

# generate world map legend
genlegend($legend,"../../../images/world_legend.png");

### continent maps

# generate country pies
while(list($c_name)=each($country)) {
$data="";
$total_c=0;
while(list($port,$nmb)=@each($table_countries[$c_name])) $total_c+=$nmb;
@reset($table_countries[$c_name]);
while(list($port,$nmb)=@each($table_countries[$c_name])) {
	if(floor(($nmb/$total_c)*360)) {  
		$c_legend[$c_name].="$port;";
		$data.="$nmb:".$color[$port].";";
	}
}
$imgsize=round(($total_c/$global)*50);
if($data=="" || $imgsize<16) genpie($data,15,"../../../images/$c_name.png");
else genpie($data,$imgsize,"../../../images/$c_name.png");
}

# generate continent maps legends
while(list($continent,$countries)=each($cont)) {

$legend="";
empty($cont_ports);

$cont_c_list=explode(";",$countries);

for($i=0;$i<count($cont_c_list);$i++) {
$c_port=explode(";",$c_legend[$cont_c_list[$i]]);
for($e=0;$e<count($c_port)-1;$e++) if($c_port[$e]!="") $cont_ports[$c_port[$e]]=1;
}

while(list($port)=@each($cont_ports)) $legend.="$port:".$color[$port].";";

genlegend($legend,"../../../images/$continent_legend.png");

}

?>
