#!/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");

############################# main ##############################

$order=$input{'order'};
$client=ip2fqdn(client_ip());
@maplist=localmaplist();

foreach (@maplist) {
  ($host,$info)=split(/!!/,$_,2);
  ($lastdate{$host}, $flcgi{$host}, $totalsize{$host}, @ftplist)=split(/!!/, $info);
  $sitecount{$host}=$#ftplist+1;
  foreach (@ftplist) {
    $ftplist{$host}=$ftplist{$host}.qq|
<a href="ftp://$_" target=_blank>$_</a>|;
  }  
}

$symbo{'NA'}=$STR_ascending; $symbo{'ND'}=$STR_dscending;	#name
$symbo{'DA'}=$STR_ascending; $symbo{'DD'}=$STR_dscending;	#date
$symbo{'CA'}=$STR_ascending; $symbo{'CD'}=$STR_dscending;	#count
$symbo{'SA'}=$STR_ascending; $symbo{'SD'}=$STR_dscending;	#size

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

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

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

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

} elsif ( $order eq "NA" ) {
  @list=server_sort($client, keys %lastdate);
  $symbo{'ND'}=$STR_ascending2;

} elsif ( $order eq "ND" ) {
  @list=reverse server_sort($client, keys %lastdate);
  $symbo{'NA'}=$STR_dscending2;

} elsif ( $order eq "DA" ) {
  @list=sort {
    if ( $lastdate{$a} eq "n/a") {
      return 1;
    } elsif ( $lastdate{$b} eq "n/a") {
      return -1;
    } else {
      return($lastdate{$a} cmp $lastdate{$b});
    } } keys %lastdate;

  $symbo{'DD'}=$STR_ascending2;

} else {	# default sort by date decending
  @list=sort { 
    if ( $lastdate{$a} eq "n/a") {
      return 1;
    } elsif ( $lastdate{$b} eq "n/a") {
      return -1;
    } else {
      return($lastdate{$b} cmp $lastdate{$a});
    } } keys %lastdate;

  $symbo{'DA'}=$STR_dscending2;

}

if ( $order eq "ND" ) { $N="NA" } else { $N="ND"; }
if ( $order eq "DD" || $order eq "" ) { $D="DA" } else { $D="DD"; }
if ( $order eq "CD" ) { $C="CA" } else { $C="CD"; }
if ( $order eq "SD" ) { $S="SA" } else { $S="SD"; }

print html_header_str($STR_title);
print qq|
<table width=90%>
<tr><td colspan=3>
&nbsp;<font color=#c00000>FtpLocate $STR_maplist</font>
</td>
<td align=right colspan=3>
<a href="javascript:history.go(-1)">
$STR_back</a>
&nbsp;
<a href="$ENV{'SCRIPT_NAME'}">$STR_refresh</a>
&nbsp;
&nbsp;
</td></tr>
<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>FtpLocate $symbo{$N}</a>
</td>
<td nowrap>
<a href="$ENV{'SCRIPT_NAME'}?form=$form&order=$D" style="text-decoration:none">
<font color=#ffffff>$STR_datatime $symbo{$D}</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=$C" style="text-decoration:none">
<font color=#ffffff>$symbo{$C}</a>
</td>
<td nowrap>
<font color=#ffffff>$STR_ftpsite
</td>
</tr>
|;

$i=1;
foreach $site (@list) {
  $s=sizestr($totalsize{$site});
  print qq|
<tr valign=top>
<td align=center><font size=-1>$i</td>
<td align=center><font size=-1><a href="http://$site$flcgi{$site}/flsearch.pl">$site</a></td>
<td align=center nowrap><font size=-1>$lastdate{$site}</td>
<td align=center><font size=-1>$s</td>
<td align=center><font size=-1>$sitecount{$site}</td>
<td><font size=-1>$ftplist{$site}</td>
</tr>
|;
  $i++;
}

print qq|
</table>
<table cellpadding=0 cellspacing=0 width=90%>
<tr><td>
<hr width=100% size=1>
</td></tr>
<tr>
<td align=right>
$STR_dslab
</td></tr>
</table>
</center>
&nbsp;
&nbsp;
</body>
</html>
|;

exit(0);
