#!/usr/local/bin/perl
#################################################################
#
# + FtpLocate - FTP search engine
#
# by Distributed System Lab E.E. NCKU Taiwan 2000
# http://ftp.ee.ncku.edu.tw/ftplocate/
#
#################################################################

use CGI; CGI::ReadParse(*input);
use Socket;
unshift(@INC, substr($0, 0, rindex($0,'/')) ); # add prog path to INC
require ("config");

############################# subs ##############################

sub form_str {
  my $type=$_[0];
  my ($label, $otherlink);

  if ( $type eq "dsearch" ) {
    $label="Description Search";
    $url=$SCRIPT_dsearch;
    $otherlink=qq|<a href="$SCRIPT_flsummary?form=flsearch">$STR_search_by_file</a>|;
  } else {
    $label="Filename Search";
    $url=$SCRIPT_flsearch;
    $otherlink=qq|<a href="$SCRIPT_flsummary?form=dsearch">$STR_search_by_desc</a>|;
  }
  return qq|
<form method=get action="$url">
<table width=89%><tr>
<td nowrap width=30%>
<input name=query size=20>
<input type=submit VALUE="$label">
&nbsp;
<a href="$URL_help">$STR_help</a>
</td>
<td nowrap align=right width=50%>
<a href="$SCRIPT_flmap">$STR_maplist</a>
<a href="$SCRIPT_fltop">$STR_hotlist</a>
<a href="$SCRIPT_flhistory">$STR_history</a>
&nbsp;
$otherlink
</td>
</form>
</table>
|;
}

############################# main ##############################
$form=$input{'form'};
$order=$input{'order'};
$client=ip2fqdn(client_ip());

@summary=summarylist($client);
$i=0;
foreach (@summary) {
  ($site[$i],$info)=split(/!!/,$_,2);
  ($dcount{$site[$i]},$count{$site[$i]},$size{$site[$i]},$rate{$site[$i]},$date{$site[$i]})=split(/!!/,$info);
  $date{$site[$i]}=~s!\d\d\d\d/(\d\d/\d\d \d\d:\d\d):\d\d!$1!;

  if ( $count{$site[$i]} > 0 ) {
    $avg{$site[$i]}=$size{$site[$i]}/$count{$site[$i]};
  } else {
    $avg{$site[$i]}=0;
  }
  $totaldcount+=$dcount{$site[$i]};
  $totalcount+=$count{$site[$i]};
  $totalsize+=$size{$site[$i]};
  $i++;
}

$symbo{'NA'}=$STR_ascending; $symbo{'ND'}=$STR_dscending;
$symbo{'DA'}=$STR_ascending; $symbo{'DD'}=$STR_dscending;
$symbo{'CA'}=$STR_ascending; $symbo{'CD'}=$STR_dscending;
$symbo{'SA'}=$STR_ascending; $symbo{'SD'}=$STR_dscending;
$symbo{'VA'}=$STR_ascending; $symbo{'VD'}=$STR_dscending;
$symbo{'RA'}=$STR_ascending; $symbo{'RD'}=$STR_dscending;

if ( $order eq "DA" ) {
  @list=sort { $dcount{$a}<=>$dcount{$b} } keys %dcount;
  $symbo{'DD'}=$STR_ascending2;

} elsif ( $order eq "DD" ) {
  @list=sort { $dcount{$b}<=>$dcount{$a} } keys %dcount;
  $symbo{'DA'}=$STR_dscending2;

} elsif ( $order eq "CA" ) {
  @list=sort { $count{$a}<=>$count{$b} } keys %count;
  $symbo{'CD'}=$STR_ascending2;

} elsif ( $order eq "CD" ) {
  @list=sort { $count{$b}<=>$count{$a} } keys %count;
  $symbo{'CA'}=$STR_dscending2;

} elsif ( $order eq "SA" ) {
  @list=sort { $size{$a}<=>$size{$b} } keys %size;
  $symbo{'SD'}=$STR_ascending2;

} elsif ( $order eq "SD" ) {
  @list=sort { $size{$b}<=>$size{$a} } keys %size;
  $symbo{'SA'}=$STR_dscending2;

} elsif ( $order eq "VA" ) {
  @list=sort { $avg{$a}<=>$avg{$b} } keys %avg;
  $symbo{'VD'}=$STR_ascending2;

} elsif ( $order eq "VD" ) {
  @list=sort { $avg{$b}<=>$avg{$a} } keys %avg;
  $symbo{'VA'}=$STR_dscending2;

} elsif ( $order eq "RA" ) {
  @list=sort { $rate{$a}<=>$rate{$b} } keys %rate;
  $symbo{'RD'}=$STR_ascending2;

} elsif ( $order eq "RD" ) {
  @list=sort { $rate{$b}<=>$rate{$a} } keys %rate;
  $symbo{'RA'}=$STR_dscending2;

} elsif ( $order eq "ND" ) {
  @list=reverse @site;
  $symbo{'NA'}=$STR_dscending2;

} else { # default order by name acending
  @list=@site;
  $symbo{'ND'}=$STR_ascending2;
}

if ( $order eq "ND" ) { $N="NA" } else { $N="ND"; }
if ( $order eq "DD" ) { $D="DA" } else { $D="DD"; }
if ( $order eq "CD" ) { $C="CA" } else { $C="CD"; }
if ( $order eq "SD" ) { $S="SA" } else { $S="SD"; }
if ( $order eq "VD" ) { $V="VA" } else { $V="VD"; }
if ( $order eq "RD" ) { $R="RA" } else { $R="RD"; }

print html_header_str($STR_title);
print form_str($form);
print qq|
<table width=90%>
<tr bgcolor=#408080 align=center>
<td>
<font color=#ffffff>#
</td>
<td nowrap>
<a href="$ENV{'SCRIPT_NAME'}?form=$form&order=$N" style="text-decoration:none">
<font color=#ffffff>$STR_ftpsite $symbo{$N}</a>
</td>
<td nowrap>
<a href="$ENV{'SCRIPT_NAME'}?form=$form&order=$D" style="text-decoration:none">
<font color=#ffffff>$STR_dircount $symbo{$D}</a>
</td>
<td nowrap>
<a href="$ENV{'SCRIPT_NAME'}?form=$form&order=$C" style="text-decoration:none">
<font color=#ffffff>$STR_filecount $symbo{$C}</a>
</td>
<td nowrap>
<a href="$ENV{'SCRIPT_NAME'}?form=$form&order=$S" style="text-decoration:none">
<font color=#ffffff>$STR_totalsize $symbo{$S}</a>
</td>
<td nowrap>
<a href="$ENV{'SCRIPT_NAME'}?form=$form&order=$V" style="text-decoration:none">
<font color=#ffffff>$STR_averagesize $symbo{$V}</a>
</td>
<td nowrap>
<a href="$ENV{'SCRIPT_NAME'}?form=$form&order=$R" style="text-decoration:none">
<font color=#ffffff>$STR_xferrate $symbo{$R}</a>
</td>
<td nowrap>
<font color=#ffffff>$STR_datatime
</td>
</tr>
|;

$i=1;
foreach $site (@list) {
  $s=sizestr($size{$site});
  $v=sizestr($avg{$site});
  $r=sizestr($rate{$site});
  print qq|
<tr align=center>
<td><font size=-1>$i</td>
<td><font size=-1><a href="ftp://$site/">$site</a></td>
<td><font size=-1>$dcount{$site}</td>
<td><font size=-1>$count{$site}</td>
<td><font size=-1>$s</td>
<td><font size=-1>$v</td>
<td><font size=-1>$r</td>
<td><font size=-1>$date{$site}</td>
</tr>
|;
  $i++;
}

$s=sizestr($totalsize);
$v=sizestr($totalsize/$totalcount);
$count=$#site+1;
print qq|<tr bgcolor=#408080 align=center>
<td>&nbsp;</td>
<td nowrap><font color=#ffffff>$count $STR_ftpsite</td>
<td nowrap><font color=#ffffff>$totaldcount $STR_dir</td>
<td nowrap><font color=#ffffff>$totalcount $STR_file</td>
<td nowrap><font color=#ffffff>$s</td>
<td nowrap><font color=#ffffff>$v</td>
<td><font color=#ffffff>&nbsp;</td>
<td><font color=#ffffff>&nbsp;</td>
</tr>
</table>

<table width=89%>
<tr>
<td nowrap>
<font color=#c00000>
$STR_thanks
</font>
</td>
<td nowrap align=right>
$STR_dslab
</td></tr>
</table>
<br>
</CENTER></BODY></HTML>
|;
