#!/usr/bin/perl

########################################################################
# COPYRIGHT NOTICE:
#
# Copyright 2001 FocalMedia.Net All Rights Reserved.
#
# Selling the code for this program without prior written consent 
# from FocalMedia.Net is expressly forbidden. You may not 
# redistribute this program in any shape or form.
# 
# This program is distributed "as is" and without warranty of any
# kind, either express or implied. In no event shall the liability 
# of FocalMedia.Net for any damages, losses and/or causes of action 
# exceed the total amount paid by the user for this software.
#
########################################################################

use Fcntl qw(:DEFAULT :flock);
use CGI::Carp qw(fatalsToBrowser);
use CGI;
use tseek;
&get_setup;
&get_env;
&get_vars;
$rateitcgi = "$scripts_url/rate_it.cgi";
$review_it = "$scripts_url/review_it.cgi";

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

$exists1 = (-e "$data_dir/whatsnew.dat");

if ($exists1 < 1)
  {

print <<END_OF_NW;

<html>

<head>
<title>Problem</title>
</head>
<body>
<p><font face="Verdana" size="2"><font color="#0000FF"><strong>Problem:</strong></font> No
New Listings has been compiled yet. To compile new listings go to the admin control panel
and click on 'Build Indexes' and then click on 'Compile What's New and Top Hits Listings'.</font></p>
</body>
</html>

END_OF_NW
exit;
  
  }
 

if (-e "$data_dir/whatsnew_main.html")
   {
   $fsize1 = (-s "$data_dir/whatsnew_main.html");
   open (RVF, "$data_dir/whatsnew_main.html");
    if ($file_locking ne "No"){flock (RVF,LOCK_SH);}
	  read(RVF,$whatsnew_main,$fsize1);
	 if ($file_locking ne "No"){flock (RVF,LOCK_UN);}
   close (RVF);
   
   $whatsnew_main =~ s/!build_url!/$build_url/g;   
   $whatsnew_main =~ s/!!scripts_url!!/$scripts_url/g;
   
   }
   else
   {
   print "Error: Unable to locate whatsnew_main.html"; exit;
   }


if (-e "$data_dir/whatsnew_lists.html")
   {
   $fsize1 = (-s "$data_dir/whatsnew_lists.html");
   open (RVF, "$data_dir/whatsnew_lists.html");
    if ($file_locking ne "No"){flock (RVF,LOCK_SH);}
	  read(RVF,$whatsnew_lists,$fsize1);
	 if ($file_locking ne "No"){flock (RVF,LOCK_UN);}
   close (RVF);
   }
   else
   {
   print "Error: Unable to locate whatsnew_lists.html"; exit;
   }


### KEYS
$current_kd = tseek::get_file_contents("$data_dir/keys.dat");
@key_lines = split(/\n/, $current_kd);
########################################


open (WNL, "$data_dir/whatsnew.dat");
 if ($file_locking ne "No"){flock (WNL,LOCK_SH);}

   while (defined($line=<WNL>))
     {
     (@litems) = split(/\[::\]/,$line);
     
     $listitems = $whatsnew_lists;
     

     #### TITLE WITH LINK
     $catlink = $litems[14];
     $catlink =~ s/\n//g;
     $catlink =~ s/\//-/g;
     $id = int($litems[1]);
     if ($configitems[19] eq "Yes") { $nwind = " target=\"_blank\" ";}

     if (($litems[4] eq "") or ($litems[4] eq "http://"))
     {
     $titlewl = "$litems[2]";
     }
     else
     {
     $titlewl = "<a href=\"$scripts_url/tseek.cgi?id=$id&ct=$catlink\" $nwind>$litems[2]</a>";
     }

     $listitems =~ s/%%title_with_link%%/$titlewl/g;
     

     #### DESCRIPTION
     $listitems =~ s/%%description%%/$litems[3]/g;
     

     #### VOTES
     if ($litems[6] eq "") {$litems[6] = 0;}
     $listitems =~ s/%%votes%%/$litems[6]/g;
     

     #### HITS
     $listitems =~ s/%%hits%%/$litems[7]/g;
     

     #### RATING
     if (($litems[5] eq "") or ($litems[5] eq "Unrated") or ($litems[5] eq "0")) {$litems[5] = "$configitems[9]";}
     $listitems =~ s/%%rating%%/$litems[5]/g;
     $rimages = tseek::get_rating_images ($litems[5]);
	  $listitems =~ s/%%rating_image%%/$rimages/g;

     
     ### RATiNG LINK
     if ($configitems[12] eq "Yes")
     	{
     	$rateit = "<a href=\"$rateitcgi?id=$id&ct=$catlink\" onClick=\"popup('$rateitcgi?id=$id&ct=$catlink',300,300);return false;\">$configitems[11]</a>";
     	}
     	else
     	{
     	$rateit = "<a href=\"$rateitcgi?id=$id&ct=$catlink\">$configitems[11]</a>";
     	}
     $listitems =~ s/%%rateit%%/$rateit/g;


     ### REVIEW LISTING
     if ($configitems[13] eq "Yes")
     	{		
     	$reviews = "<a href=\"$review_it?id=$id&ct=$catlink\" onClick=\"popup('$review_it?id=$id&ct=$catlink',400,300);return false;\">$configitems[10]</a>";
     	}
       else
        {
     	$reviews = "<a href=\"$review_it?id=$id&ct=$catlink\">$configitems[10]</a>";
     	}
     $listitems =~ s/%%reviews%%/$reviews/g;


     #### CATEGORY LOCATION NAME
     
     $catname = $litems[14];
     $catname =~ s/-/\//g;
     $catname = tseek::decode_dir($catname);
     
     if ($fields{'status'} ne "built")
     	{
     	$catwl = "<a href=\"$scripts_url/tseekdir.cgi?id=$id&location=Root-$catlink\">$catname</a>";
     	}
     	else
     	{
     	$catlink =~ s/-/\//g;
     	$catwl = "<a href=\"$build_url/$catlink/\">$catname</a>";
     	}
     
     $listitems =~ s/%%category%%/$catwl/g;

     
     #### DATE
     $listitems =~ s/%%date%%/$litems[9]/g;
		
		
	  #### KEYS
	  $kcount = 15;
	  $keystowrite = "";
	  foreach $itemk(@key_lines)
	  		{
	  		($kdiz, $kname, $klen, $syn, $reqr, $mtxt) = split (/\t/,$itemk);
	  		$kname = "%%" . $kname . "%%";
	  		$listitems =~ s/$kname/$litems[$kcount]/g;
	  		$kcount++;
	  		}


			#### LINK LISTING IMAGE SUPPORT
			$crit = "<img src=\""; 													$listitems =~ s/\[img\]/$crit/gi;
			$crit = "\">"; 															$listitems =~ s/\[\/img\]/$crit/gi;
			$crit = "<a href=\"$scripts_url/tseek.cgi?id=$id&ct=$catlink\"><img src=\""; $listitems =~ s/\[imglnk\]/$crit/gi;
			$crit = "\" border=\"0\"></a>"; 										$listitems =~ s/\[\/imglnk\]/$crit/gi;		  

	  
     $total_list = $total_list . $listitems;
     }
  if ($file_locking ne "No"){flock (WNL, LOCK_UN);}
close (WNL);


### JAVA SCRIPT

$javascr = <<END_OF_JS;
<script LANGUAGE="JavaScript">
<!-- Hide
var popupwin = null;
  function popup(loc,ww,hh) 
   {
    var mywidth = (ww + 0);
    var myheight = (hh + 0);
    var myspecs = "'menubar=0,status=1,resizable=1,location=0,titlebar=1,toolbar=1,scrollbars=1,width=" + mywidth + ",height=" + myheight + "'";
      if (popupwin == null || popupwin.closed) 
			{
            popupwin = window.open (loc, 'popupwin', myspecs);
           }
		 else 
		   {
            popupwin.focus();
            popupwin.location.href = loc;
          }
    }
 // End Hide -->
</script>
</head>
END_OF_JS

$whatsnew_main =~ s/<\/head>/$javascr/gi;

$whatsnew_main =~ s/%%whats_new_listings%%/$total_list/gi;

print $whatsnew_main;

####################


sub get_env
{
if ($ENV{'QUERY_STRING'} ne "") {
								$temp = $ENV{'QUERY_STRING'};
								}
								else
								{
								read(STDIN, $temp, $ENV{'CONTENT_LENGTH'});
								}

@pairs=split(/&/,$temp);

foreach $item(@pairs) {
	($key,$content)=split (/=/,$item,2);
	$content=~tr/+/ /;
	$content=~ s/%(..)/pack("c",hex($1))/ge;
	$fields{$key}=$content;
}

$fields{'comment'}=~s/\cM//g;
$fields{'comment'}=~s/\n\n/<p>/g;
$fields{'comment'}=~s/\n/<br>/g;
}









sub get_setup
{
#### GET CONFIGURATION ########################################################

$csize = (-s "config.cgi");
open (RVF, "config.cgi");
	read(RVF,$data_dir,$csize);
close (RVF);

$data_dir =~ s/\n//g;
$crit = chr(13); $crit =~ s/$crit//g; 
$crit = chr(10); $crit =~ s/$crit//g;

$exists = (-e "$data_dir/setup.cfg");
if ($exists > 0)
	{

	open (STP, "$data_dir/setup.cfg");
		while (defined($line=<STP>))
			{
			if ($line =~ m/#/g)
				{
				$r = pos($line);
				$line = substr($line, 0, $r - 1);
				}
				
				$line =~ s/\n//g;
			
if ($line =~ /BUILD_DIR/){$line =~ s/BUILD_DIR//g; $line =~ s/ //g; $build_dir = $line;}
if ($line =~ /BUILD_URL/){$line =~ s/BUILD_URL//g; $line =~ s/ //g; $build_url = $line;}
if ($line =~ /SCRIPTS_URL/){$line =~ s/SCRIPTS_URL//g; $line =~ s/ //g; $scripts_url = $line;}
if ($line =~ /SCRIPTS_LOC/){$line =~ s/SCRIPTS_LOC//g; $line =~ s/ //g; $scripts_loc = $line;}
if ($line =~ /ADMIN_DIRECTORY/){$line =~ s/ADMIN_DIRECTORY//g; $line =~ s/ //g; $admin_directory = $line;}
if ($line =~ /SEND_MAIL/){$line =~ s/SEND_MAIL//g; $line =~ s/ //g; $sendmail_loc = $line;}
if ($line =~ /RGST/){$line =~ s/RGST//g; $line =~ s/ //g; $thspl = $line;}
if ($line =~ /USE_MYSQL/){$line =~ s/USE_MYSQL//g; $line =~ s/ //g; $use_mysql = $line;}
if ($line =~ /DB_NAME/){$line =~ s/DB_NAME//g; $line =~ s/ //g; $db_name = $line;}
if ($line =~ /DB_USERNAME/){$line =~ s/DB_USERNAME//g; $line =~ s/ //g; $db_username = $line;}
if ($line =~ /DB_PASSWORD/){$line =~ s/DB_PASSWORD//g; $line =~ s/ //g; $db_password = $line;}
if ($line =~ /MYSQL_HOSTNAME/){$line =~ s/MYSQL_HOSTNAME//g; $line =~ s/ //g; $mysql_hostname = $line;}
if ($line =~ /MYSQL_PORT/){$line =~ s/MYSQL_PORT//g; $line =~ s/ //g; $mysql_port = $line;}
if ($line =~ /FILE_LOCKING/){$line =~ s/FILE_LOCKING//g; $line =~ s/ //g; $file_locking = $line;}
if ($line =~ /CPUSERNAME/){$line =~ s/CPUSERNAME//g; $line =~ s/ //g; $c_username = $line;}
if ($line =~ /CPPASSWORD/){$line =~ s/CPPASSWORD//g; $line =~ s/ //g; $c_password = $line;}

			}
	close (STP);
	
	}

#### END CONFIGURATION ########################################################
}






sub get_vars
{

open (CIT, "$data_dir/config.cfg");
 if ($file_locking ne "No"){flock (CIT,LOCK_SH);}
	while (defined($line=<CIT>))
	{
	$line =~ s/\n//g;
	$configitems[$cntr] = $line;
	$cntr++;
	}
 if ($file_locking ne "No"){flock (CIT,LOCK_UN);}
close (CIT);

$cpu = "Co" . "py";
$vrm = "&nbsp;&nbsp;Un" . "regis" . "ter" . "ed"; 
if (($thspl ne "noiqb321kh") and ($thspl ne "uncxkqpp13d"
)and ($thspl ne "923nsdfmlkejr"
)and ($thspl ne "9234432kejr"))
{$unr = "<font face=\"Verdana\" size=\"2\"><b>$vrm $cpu</right></b></font>";}else{$unr="";}

#	DIR_EXTENSION 				#0
#	DEFAULT_INDEX				#1
#	INDEX_BUILD_SV				#2
#	
#	LISTINGS_SORTEDBY			#3
#	DIR_ITEMS_PERPAGE			#4
#	
#	SEARCH_ITEMS_PERPAGE			#5
#	
#	NEXTPAGE_TEXT				#6
#	PREVPAGE_TEXT				#7
#	PAGES_TEXT				#8
#	NOT_RATED_TEXT				#9
#	REVIEW_TEXT				#10
#	RATE_IT_TEXT				#11
#	JSCRIPT_WINDOW_RATE_IT			#12
#	JSCRIPT_WINDOW_REVIEWS			#13
#	SEPERATOR_NEXTPREV			#14
#	CAT_DISPLAY_SEPERATOR			#15
#	NOMATCH_TEXT				#16

#	SEARCH NEW LINKS			#18
#	DIRS NEW LINKS				#19

}


