#!/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 ##############################

$starttime=gettimeofday(); $starttimestr=timestr();

$query=clean_str($input{'query'}); $query_raw=CGI::escape($query);
$fsite=clean_str($input{'fsite'}); $fsite_raw=CGI::escape($fsite);
$page=$input{'page'};
$client=ip2fqdn(client_ip());

if ( $fsite eq "" ) {
  $resultfname=$query_raw;
  $CGIF="";
  $STRF="";
} else {
  $resultfname=$query_raw."-$fsite";
  $CGIF="&fsite=$fsite_raw";	# for cgi page list
  $STRF="&nbsp;$fsite";		# for output
}

if ( $query!~m![/\w\d\.\+\-\\_~]{2}! ) {	# keyword to short
  print "Location: $SCRIPT_flsummary?form=flsearch\n\n";
  exit 0;
} elsif ( -f "$CACHEDIR/$resultfname" ) {	# cache hit
  $cachestr="cache";
} elsif ( -f "$FTPLOCATEDIR/flindex.pl.RUNNING" ) {	# service not available
  print html_header_str($STR_title);
  print qq|<br><br>
<b><font color=#cc0000>Indexing, please try later...</font></b>
<br><br><br>
<table width=89%><tr><td nowrap align=right><hr size=1>
$STR_dslab
</td></tr></table></body></html>|;
  exit 0;
} else {					# cache miss
  $cachestr="";
  do_flsearch($client, $query, $fsite, $resultfname);
}

open (M, "$CACHEDIR/$resultfname.more");
$_=<M>; chop; $setstr=$cachestr." $_";
while (<M>) { chop; $moreset{$_}=1; }
close(M);
open (L, "$CACHEDIR/$resultfname");

$i=0;
while ( <L> ){
  chop; $list[$i]=$_;
  $i++;
  if ( $i>$MAXREC ) { last; }
}
close(L);

$searchtime=int((gettimeofday()-$starttime)*100+0.5)/100;
loguser($starttimestr, $searchtime, $client, $fsite, $i, $query, $cachestr);

print html_header_str($STR_title);
print qq|
<form method=get action="$ENV{'SCRIPT_NAME'}">
<table width=89%><tr>
<td nowrap width=40%>
<input name=query size=20>
<input type=submit VALUE="Filename Search">
&nbsp;
<a href="$URL_help">$STR_help</a>
</td>
<td nowrap align=center width=50%>
|;

$n=$#list+1;
($listmin, $listmax)=init_pageline($n, 100, 10, $page, "$ENV{'SCRIPT_NAME'}?query=$query_raw$CGIF");
print_pageline();

print qq|</td>
<td align=right nowrap width=10%>
<a href="$SCRIPT_flsummary?form=dsearch">$STR_search_by_desc</a>
</td>
</form>
</tr></table>

<table width=90% cellspacing=0 cellpadding=0><tr>
<td bgcolor=#408080 nowrap>
<font color=#ffffff>$STRF&nbsp;$STR_related_file $query $STR_related_file2 $n</font>
&nbsp;
<font color=#cccccc>$STR_shorthelp</font>
</td>
<td bgcolor=#408080 nowrap align=right>
<font size=-1 color=#cccccc>$setstr&nbsp;</font>
</td>
</tr>
<tr><td colspan=2>
|;

$currenthost="";
$currentdir=""; 
dbmopen(%desc, "$DESCDIR/desc", undef);
for ($i=$listmin; $i<=$listmax; $i++) {
  $j=$i+1;
  $_=$list[$i];
  ($host,$path,$type,$size,$date)=/(.*)\/\d+:\s*(.*)!!(.)!!(\d+)!!(.*)/;

  if ( $host ne $currenthost ) {
    if ( $currenthost ne "" ) {
      print"<br><hr size=1>"; 
    }
    $currenthost=$host; 
    $currentdir=""; 
    if ( $moreset{$host}==1 ) {
      $host_raw=CGI::escape($host);
      $more=qq|
<a href="$ENV{'SCRIPT_NAME'}?query=$query_raw&fsite=$host_raw">
<i><font size=-1 color=#c00000>more!</font></i></a>|;
    }
    print qq|<img src="$HOSTICON" alt="[$j]">
<font color=#c00000>$STR_ftpsite</font> <a href="ftp://$host">$host</a>$more
<br>\n|;
  }

  $desc=descstr($path);
  if ( $desc ne "" ) { $desc="<font size=-1 color=#408080>$desc</font>"; }

  $path=~s!^\s*\.?/?!!;
   
  if ( $type eq "d" ) {
    $dname=$path;
    $fname="";
    $dirdesc=$desc; 
    $filedesc="";
  } else {			# type = l(link) or -(file)
    $dname=dirname($path);
    $fname=filename($path);
    $dirdesc="";
    $filedesc=$desc; 
  }

  $fq=filename($query); $fq=~s/[\^\$]//;

  if ( $dname ne $currentdir ) {
#    if ( $currentdir ne "" ) { print"<br>"; }
    $currentdir=$dname;

    $dnamestr=$dname; $dnamestr=~s!($fq)!<b>$1</b>!ig;
    print qq|<img src="$DIRICON" valign=abscenter alt="[$j]">
$STR_dir <a href="ftp://$host/$dname">/$dnamestr</a> $dirdesc<br>\n|;
  }

  if ( $fname ne "" ) {
    $s=sizestr($size);
    if ( $type eq "l" ) {
      $t=$STR_link;
    } else {
      $t=$STR_file;
    }
    $fnamestr=$fname; $fnamestr=~s!($fq)!<b>$1</b>!ig;
    print qq|&nbsp;&nbsp;<img src="$FILEICON" valign=abscenter alt="[$j] $size bytes">
$t <a href="ftp://$host/$dname/$fname">$fnamestr</a>
$s [$date] $filedesc<br>\n|;
  }

}
dbmclose(%desc);

print qq|</td></tr>
</table>
<hr size=1 width=90%>
<table width=89%><tr>
<td nowrap>
<font color=#999999 size=-1>
$searchtime sec
</font>
</td>
<td nowrap width=60% align=center>
|;

print_pageline();

print qq|</td>
<td nowrap align=right>
$STR_dslab
</td></tr>
</table>
</center>
</body>
</html>
|;
exit 0;
