#!/usr/bin/perl
#######################################################
#		Home Free v3.20
#     
#    	Created by Solution Scripts
# 		Email: solutions@solutionscripts.com
#		Web: http://solutionscripts.com
#
#######################################################
#
#
# COPYRIGHT NOTICE:
#
# Copyright 1999 Solution Scripts  All Rights Reserved.
#
# Selling or redistributing the code for this program 
# without prior written consent is expressly forbidden. 
# In all cases copyright and header must remain intact.
#
#######################################################

$|=1;

($0 =~ m,(.*)/[^/]+,)   && unshift (@INC, "$1");
($0 =~ m,(.*)\\[^\\]+,) && unshift (@INC, "$1");

require "variables.pl";
require "dblib.pl";
require "acc_auth.pl";

print "Content-type: text/html\n\n ";

&Account::initialize;

if ($INPUT{'search'} || $INPUT{'cata'} || $INPUT{'letter'}) { &search; }
else { &main; }
exit;

########## MAIN DISPLAY SCREEN ##########
sub main {

&SHeader;

%cata_list = &Account::get_cata_hash;

if ($query eq "advanced") { $advanced = 1; }

if ($advanced) {
print <<EOF;
<table cellspacing=0 cellpadding=5 border=0 width=90%>
<TR><TD align=left colspan=2>
<form action="$files::search" method="GET">
<font face=$font_face size=$font_size>
Search For:&nbsp;&nbsp;<input type=text name="search" size=20>&nbsp;&nbsp;<input type=submit value="$lang::se1">
EOF

	if ($category) {

print <<EOF;
<font face=$font_face size=$font_size>&nbsp;&nbsp;&nbsp;In:</FONT>
<SELECT name=cata>
<OPTION value="_all_" Selected>$lang::se2 $cata_name
EOF
		if ($cata_base) {
print <<EOF;
<OPTION value="accounts">$lang::se3
EOF
		}

		while ( ($key, $value) = each(%cata_list) ) {
			@catt = split(/\|/,$value);
			print "<OPTION VALUE=\"$key\">$catt[1]\n";
		}
print <<EOF;
</SELECT>
EOF
	}
print <<EOF;
</TD></TR>
<TR><TD align=left colspan=2><font face=$font_face size=$font_size>
$lang::se4:&nbsp;&nbsp;&nbsp;&nbsp;<input type=radio name="type" value="keywords" checked> $lang::se5
&nbsp;&nbsp;&nbsp;&nbsp;<input type=radio name="type" value="phrase"> $lang::se6
</TD></TR>
<TR><TD align=left colspan=2><font face=$font_face size=$font_size>
$lang::se7:&nbsp;&nbsp;&nbsp;&nbsp;<input type=radio name="boolean" value="and" checked> $lang::se8
&nbsp;&nbsp;&nbsp;&nbsp;<input type=radio name="boolean" value="or"> $lang::se9
</TD></TR>
<TR><TD align=left colspan=2><font face=$font_face size=$font_size>
$lang::se10:&nbsp;&nbsp;&nbsp;&nbsp;<select name="num_show">
<option value="25">25
   <option value="50">50
   <option value="75">75
   <option value="100">100
   <option value="100000">Show All
</select>
</form>
<HR NOSHADE SIZE=1>
</TD></TR><TR>
EOF

}
else {
print <<EOF;
<table cellspacing=0 cellpadding=5 border=0 width=90%>
<TR><TD align=left colspan=2>
<form action="$files::search" method="GET">
<input type="Text" name="search" size="20">&nbsp;&nbsp;<input type="Submit" value=" $lang::se1 ">
EOF

	if ($category) {

print <<EOF;
<font face=$font_face size=$font_size>&nbsp;&nbsp;&nbsp;In:</FONT>
<SELECT name=cata>
<OPTION value="_all_" Selected>$lang::se2 $cata_name
EOF
		if ($cata_base) {
print <<EOF;
<OPTION value="accounts">$lang::se3
EOF
		}

		while ( ($key, $value) = each(%cata_list) ) {
			@catt = split(/\|/,$value);
			print "<OPTION VALUE=\"$key\">$catt[1]\n";
		}
print <<EOF;
</SELECT>
<BR>
EOF
	}
print <<EOF;
<font face=$font_face size=$font_size>
<A HREF="$files::search?advanced">$lang::se11</a>
</form>
<HR NOSHADE SIZE=1>
</TD></TR><TR>
EOF
}
$a=0;
## IF category MAKE USER SELECT category ##
if ($category) {
	$nfont_size = $font_size - 1;
	$select_list = '';
	if ($cata_base) {
print <<EOF;
<TD>
<font face=$font_face size=$font_size>
<A HREF="$files::search?cata=accounts"> $cata_name</A></FONT><BR>
<font face=$font_face size=$nfont_size>
$lang::se13 $cata_name
<BR><BR>
</TD>
EOF
$a++;
	}

	while ( ($key, $value) = each(%cata_list) ) {
		@catt = split(/\|/,$value);
		if ($a == 2) { print "</TR><TR>"; $a=1; }
		else { $a++; }
print <<EOF;
<TD>
<font face=$font_face size=$font_size>
<A HREF="$files::search?cata=$key">$catt[1]</A></FONT><BR>
<font face=$font_face size=$nfont_size>
$catt[2]
<BR><BR>
</TD>
EOF

	}
print <<EOF;
</TABLE>
EOF
&SFooter;
exit;
}
## NO category STRAIT TO JOIN SCREEN ## 
else {
print <<EOF;
<TD colspan=2>
<font face=$font_face size=$font_size>
$lang::se14.<BR><BR>
<A HREF="$files::search?letter=a">A</A>&nbsp;&nbsp;
<A HREF="$files::search?letter=b">B</A>&nbsp;&nbsp;
<A HREF="$files::search?letter=c">C</A>&nbsp;&nbsp;
<A HREF="$files::search?letter=d">D</A>&nbsp;&nbsp;
<A HREF="$files::search?letter=e">E</A>&nbsp;&nbsp;
<A HREF="$files::search?letter=f">F</A>&nbsp;&nbsp;
<A HREF="$files::search?letter=g">G</A>&nbsp;&nbsp;
<A HREF="$files::search?letter=h">H</A>&nbsp;&nbsp;
<A HREF="$files::search?letter=i">I</A>&nbsp;&nbsp;
<A HREF="$files::search?letter=j">J</A>&nbsp;&nbsp;
<A HREF="$files::search?letter=k">K</A>&nbsp;&nbsp;
<A HREF="$files::search?letter=l">L</A>&nbsp;&nbsp;
<A HREF="$files::search?letter=m">M</A>&nbsp;&nbsp;
<A HREF="$files::search?letter=n">N</A>&nbsp;&nbsp;
<A HREF="$files::search?letter=o">O</A>&nbsp;&nbsp;
<A HREF="$files::search?letter=p">P</A>&nbsp;&nbsp;
<A HREF="$files::search?letter=q">Q</A>&nbsp;&nbsp;
<A HREF="$files::search?letter=r">R</A>&nbsp;&nbsp;
<A HREF="$files::search?letter=s">S</A>&nbsp;&nbsp;
<A HREF="$files::search?letter=t">T</A>&nbsp;&nbsp;
<A HREF="$files::search?letter=u">U</A>&nbsp;&nbsp;
<A HREF="$files::search?letter=v">V</A>&nbsp;&nbsp;
<A HREF="$files::search?letter=w">W</A>&nbsp;&nbsp;
<A HREF="$files::search?letter=x">X</A>&nbsp;&nbsp;
<A HREF="$files::search?letter=y">Y</A>&nbsp;&nbsp;
<A HREF="$files::search?letter=z">Z</A>&nbsp;&nbsp;<BR><BR>
</TD></TR>
</TABLE>
EOF
&SFooter;
exit;
}
}







########## SEARCH ##########
sub search {

&SHeader;

$colspan=2;

if ($search_icons) {
	$colspan=3;
	$icon_head = "<TD><font face=$font_face size=$font_size color=$text_table_head>$lang::se15</FONT></TD>";
}

print <<EOF;
<TABLE cellpadding=4 cellspacing=0 border=0>
<TR BGCOLOR=$table_head_bg>
$icon_head
<TD><font face=$font_face size=$font_size color=$text_table_head>
$lang::se16</FONT></TD>
<TD><font face=$font_face size=$font_size color=$text_table_head>
$lang::se17</FONT></TD></TR>
EOF

unless ($INPUT{'boolean'}) { $INPUT{'boolean'} = "and"; }
unless ($INPUT{'type'}) { $INPUT{'type'} = "keywords"; }
unless ($INPUT{'num_start'}) { $INPUT{'num_start'} = 1; }
unless ($INPUT{'num_show'}) { $INPUT{'num_show'} = 25; }
unless ($INPUT{'letters'}) { $INPUT{'letters'} = "ALL"; }
unless ($INPUT{'cata'}) { $INPUT{'cata'} = "accounts"; }

$num_total = 0;
$shown=0;
$shown=$shown + $INPUT{'num_start'};
$num_show = $INPUT{'num_show'} + $INPUT{'num_start'};

%cata_list = &Account::get_cata_hash;
$cata_list{'accounts'} = "|$lang::se12 $cata_name";

@selcata = split(/\,/,$INPUT{'cata'});

foreach $ssel(@selcata) {
	$print_cata{$ssel} = 1;
}


unless ($INPUT{'sort'}) {
	$INPUT{'sort'} = "name";
}

while ( ($key, $value) = each(%cata_list) ) {
	@catt = split(/\|/,$value);

	## CATA SLECT ##
	unless ($print_cata{'_all_'}) {
		unless ($print_cata{$key}) { 
			next;
		}
	}

	$cata_show_html = 0;
		
	$cata_html = qq!
		<TR bgcolor=$colors[1]>
		<TD colspan=$colspan align=center><font face=$font_face size=$font_size>$cata_name: <Font color=blue>$catt[1]</FONT></TD>
		</TR>
	!;


	undef %acc_list;
	undef @html;
	undef %sort;
	undef %content;
		
	%acc_list = &Account::get_acc_hash($key);
	
	$a=0;
	while ( ($akey, $avalue) = each(%acc_list) ) {
		@acco = split(/\|/,$avalue);
		
		my @accarray = split(//,$akey);
		## LETTER SELECT ##
		unless ($INPUT{'letters'} =~ /ALL/) {
			unless ($INPUT{'letters'} =~ /$accarray[0]/) { 
				next;
			}
		}
		
		## ONHOLD ##
		if ($acco[18]) { next; }
	
		if ($search_show) {
			next unless ($acco[8]);
			next unless ($acco[9]);
		}
		
		if ($catt[0]) {	$surl = "$url/$catt[0]/$akey"; }
		else { $surl = "$url/$akey"; }

		if ($catt[0]) {	$spath = "$free_path/$catt[0]/$akey"; }
		else { $spath = "$free_path/$akey"; }

		######### SEARCH WORDS ##########
		if ($INPUT{'search'}) {
			$found=0;
			undef $/;
			open (HEAD, "$spath/index.html");
			$index = <HEAD>;
			close (HEAD);
			$/ ="\n";

			if ($INPUT{'type'} eq "phrase") {
				if ($index =~ /$INPUT{'search'}/i) { $found=1; }
				if ($acco[8] =~ /$INPUT{'search'}/i) { $found=1; }
				if ($acco[9] =~ /$INPUT{'search'}/i) { $found=1; }
			}
			else { # KEYWORDS #
				@words = split(/ /,$INPUT{'search'});
				$lword = @words;
				$ff=0;
				foreach $line(@words) {
					if ($index =~ /$line/i) { $ff++; next; }
					if ($acco[8] =~ /$line/i) { $ff++;  next; }
					if ($acco[9] =~ /$line/i) { $ff++;  next; }				
				}
				if ($INPUT{'boolean'} eq "and") {
					if ($ff eq $lword) { $found=1; }
				}
				else {
					if ($ff) { $found=1; }
				}	
			}
			unless ($found) { next; }
		}
		
		
		$html = "<TR>\n";

		if ($search_icons) {
			$ctime = ($time - $acco[4]) / 86400;	
			$ltime = ($time - $acco[5]);

			unless ($acco[5]) {
				$sicon = "<IMG SRC=\"$url_to_icons/search_never.gif\" border=0>";
			}
			elsif ($ctime < $search_new) {
				$sicon = "<IMG SRC=\"$url_to_icons/search_new.gif\" border=0>";
			}
			elsif ($ltime < 86400) {
				$sicon = "<IMG SRC=\"$url_to_icons/search_today.gif\" border=0>";
			}
			else {
				$sicon = "<IMG SRC=\"$url_to_icons/search_plain.gif\" border=0>";
			}
			if ($acco[35]) {
				unless ($acco[35] =~ /http/) {
					$acco[35] = "$url_to_icons/$acco[35]";
				}
				$sicon = "<IMG SRC=\"$acco[35]\" border=0>";
			}	
			$sicon = "<TD><A HREF=\"$surl/\">$sicon</A></TD>";
		}


		unless ($acco[8]) { $acco[8]=$lang::se18; }
		unless ($acco[9]) { $acco[9]=$lang::se19; }

		$html .= "$sicon";
		$html .= "<TD><font face=$font_face size=$font_size><A HREF=\"$surl/\">$acco[8]</A></FONT></TD>\n";
		$html .= "<TD><font face=$font_face size=$font_size>&nbsp;&nbsp;&nbsp;$acco[9]</FONT></TD></TR>\n";

		if ($INPUT{'sort'} eq "name") { 
			$sort{$akey} = $akey;
			$content{$akey} = $html;
		}
		elsif ($INPUT{'sort'} eq "size") {
			$sort{$akey} = "$acco[3]";
			$content{$akey} = $html;
		}
		elsif ($INPUT{'sort'} eq "files") {
			$sort{$akey} = $acco[40];
			$content{$akey} = $html;
		}
		elsif ($INPUT{'sort'} eq "joined") {
			$sort{$akey} = "$acco[4]";
			$content{$akey} = $html;
		}
		elsif ($INPUT{'sort'} eq "last") {
			$sort{$akey} = "$acco[5]";
			$content{$akey} = $html;
		}
		
		
		$a++;	
#		print "$akey -- $avalue<BR>";
	}

	$atleastone = 0;
	if (($INPUT{'sort'} eq "name")) {
		foreach $pkey (sort_hasp(\%sort)) {
			if ($col) { $col=0; }
			else { $col++; }
			$num_total++;
			$content{$pkey} =~ s/BGGCOLOR/$colors[$col]/gi;
			$content{$pkey} =~ s/TNUMBER/$num_total/gi;
			$atleastone = 1;

			if (($num_total >= $INPUT{'num_start'}) && ($shown < $num_show)) {
				unless ($cata_show_html) { 
					$cata_show_html = 1;
					print $cata_html; 
				}
				print $content{$pkey};
				$shown++;
			}

		}
	}
	elsif (($INPUT{'sort'} eq "joined") || ($INPUT{'sort'} eq "last") || ($INPUT{'sort'} eq "size") || ($INPUT{'sort'} eq "files")) {
		foreach $pkey (sort_hashs(\%sort)) {
			if ($col) { $col=0; }
			else { $col++; }
			$num_total++;
			$content{$pkey} =~ s/BGGCOLOR/$colors[$col]/gi;
			$content{$pkey} =~ s/TNUMBER/$num_total/gi;
			$atleastone = 1;

			if (($num_total >= $INPUT{'num_start'}) && ($shown < $num_show)) {
				unless ($cata_show_html) { 
					$cata_show_html = 1;
					print $cata_html; 
				}
				print $content{$pkey};
				$shown++;
			}
		}	
	}

	unless($atleastone) {
		if (($num_total >= $INPUT{'num_start'}) && ($shown < $num_show)) {
			unless ($cata_show_html) { print $cata_html; }
print <<EOF;
<TR bgcolor=$colors[0]>
<TD colspan=$colspan align=center><font face=$font_face size=$font_size>$lang::se20</FONT></TD>
</TR>
EOF
		}

	}

}

$ptotal = $num_total / $INPUT{'num_show'};


print <<EOF;
<TR>
<TD colspan=$colspan><font face=$font_face size=$font_size>
$lang::se21 $num_total<BR>
$lang::se22 
EOF

while ( ($name, $value) = each(%INPUT) ) {
	unless (($name eq "num_start") || ($name eq "browse_final") || ($name eq "action")) {
		$string .= "\&$name\=$value";
	}
}

$b=1;
while ($b <= $ptotal) {
	$max = $b * $INPUT{'num_show'};
	$min = $max - $INPUT{'num_show'};
	
	if (($INPUT{'num_start'} < $max) && ($INPUT{'num_start'} >= $min)) {
		print "<font color=red>$b</font> - ";
	}
	else {
		$min++;
print <<EOF;
<A HREF="$files::search?action=search$string&num_start=$min">$b</A> - 
EOF

	}

$b++;
}

if ($num_total > $max) {
	$min = $min + 1 + $INPUT{'num_show'};
print <<EOF;
<A HREF="$files::search?action=search$string&num_start=$min">$b</A> - 
EOF
}	


print <<EOF;
<BR><A HREF="$files::search">$lang::se23</A>
</TD></TR></TABLE>
EOF
&SFooter;
exit;
}

sub sort_hasp {
	my $x = shift;
	my %array = %$x;
	if ($INPUT{'asc_dec'} eq "asc") {
		sort { $array{$a} cmp $array{$b}; } keys %array;
	}
	else {
		sort { $array{$b} cmp $array{$a}; } keys %array;
	}

}

sub sort_hashs {
	my $x = shift;
	my %array = %$x;
	if ($INPUT{'asc_dec'} eq "asc") {
		sort { $array{$a} <=> $array{$b}; } keys %array;
	}
	else {
		sort { $array{$b} <=> $array{$a}; } keys %array;	
	}

}

########## CGI HEADER ##########
sub SHeader {

print "<HTML><HEAD><TITLE>$free_name</TITLE></HEAD>\n";
print "<body bgcolor=$over_bg text=$text_color link=$link_color alink=$link_color vlink=$link_color>\n";

if ("$search_header") {
	open (HEAD, "$path/$search_header");
	@head = <HEAD>;
	close (HEAD);
	$head[0]='';
	foreach $line (@head) {
			print "$line";
	}
}
	print "<br><center>";
}

########## CGI FOOTER ##########
sub SFooter {

print "</center>";
if ("$search_footer") {
	open (HEAD, "$path/$search_footer");
	@foot = <HEAD>;
	close (HEAD);
	$foot[0]='';
	foreach $line (@foot) {
		print "$line";
	}
}
if ($credit) {
	print "<center><font size=$font_size><hr width=525 noshade size=1><a href=\"http://solutionscripts.com\">Home Free</a><br>Created by <a href=\"http://solutionscripts.com\">Solution Scripts</a><br><br>";
}
print "</BODY></HTML>\n";
}
1;
