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

$default_permissions = 0777;  ### DEFAULT PERMISSIONS THAT IS USED FOR DATA FILES IN DATA DIRECTORY

&get_setup;
&get_env;
&get_vars;

$tseekdir = "$scripts_url/tseekdir.cgi";
$tseek = "$scripts_url/tseek.cgi";
$tseekrate = "$scripts_url/tseekrate.cgi";
$rate_it_cgi = "$scripts_url/rate_it.cgi";
$review_it = "$scripts_url/review_it.cgi";


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

if ($fields{'fct'} eq "") {&review_it_screen;}
if ($fields{'fct'} eq "save_review") {&save_review;}

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


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

#### TEXT
$eftext = tseek::get_file_contents("$data_dir/texts.dat");
@text_items = split (/\n/, $eftext);


if ($fields{'name'} eq "")
	{
	## 15
	#$error_text = "You have to supply us with your name.";
	$error_text = $text_items[15];
	}

if ($fields{'description'} eq "")
	{
	## 16
	#$error_text = "You have to supply us with text in the review box.";
	$error_text = $text_items[16];
	}

	if ($error_text ne "")
		{
		$prlm = &get_template("reviewproblem.html");
		$prlm =~ s/%%problem%%/$error_text/g;
		$prlm =~ s/!build_url!/$build_url/g;
		$prlm =~ s/!!scripts_url!!/$scripts_url/g;
		$prlm = tseek::insert_tmpl_includes($prlm);   
		print $prlm;
		}
		else
		{
		&save_review2;
		}

}




sub save_review2
{

if ((-e "$item/contents.txt") < 1)
	{
	mkdir("$data_dir/tbsdata/reviews",$default_permissions);
 	chmod($default_permissions,"$data_dir/tbsdata/reviews","$data_dir/tbsdata/reviews");
	}

($sec,$min,$hour,$mday,$mon,$year,$wday,$ydat,$isdst) = localtime();
$mon++;
$year = "20" . substr($year, 1, 2);
$today = "$year-$mon-$mday";
$fields{'description'} =~ s/\n/<br>/g;

########

$drl = $fields{'ct'};
$drl =~ s/&/L2k/g; $drl =~ s/-/\//g;
$drwrite = $drl;
$drl = "$data_dir/$drl";

$current_kd = tseek::get_file_contents("$drl/contents.txt");
@dir_items = split (/\n/, $current_kd);

foreach $item (@dir_items)
	{
	@litems = split (/\[::\]/, $item);
	if (int($litems[0]) eq $fields{'id'})
		{
		$linktitle = $litems[1];
		$linkurl = $litems[3];
		}
	}

########

$cval = tseek::get_file_contents("$data_dir/dtable.ldx");
$cval++;
open (DTBLX, "> $data_dir/dtable.ldx");
 if ($file_locking ne "No"){flock (DTBLX, LOCK_EX) or die "Can't set lock for file: $!\n";}
	print DTBLX $cval;
 #if ($file_locking ne "No"){flock (DTBLX, LOCK_UN);}
close (DTBLX);


open (RVWITEMS, ">> $data_dir/tbsdata/reviews/contents.txt");
 if ($file_locking ne "No"){flock (RVWITEMS, LOCK_EX) or die "Can't set lock for file: $!\n";}

	print RVWITEMS $fields{'name'} . "\t" .				### 0 NAME
						$fields{'email'} . "\t" .     		### 1 EMAIL
						$fields{'description'} . "\t" . 		### 2 DESCRIPTION
						$today . "\t" .							### 3 TODAY
						$drwrite . "\t" . 						### 4 LOCATION
						$fields{'id'} . "\t" .					### 5 LINK ID
						$linktitle . "\t" . 						### 6 LINK TITLE
						$linkurl . "\t" . 						### 7 LINK NAME
						$cval . "\n"; 								### 8 REVIEW ID

 #if ($file_locking ne "No"){flock (RVWITEMS, LOCK_UN);}
close (RVWITEMS);

########

$adsuccs = &get_template("reviewadded.html");
$adsuccs =~ s/!build_url!/$build_url/g;
$adsuccs =~ s/!!scripts_url!!/$scripts_url/g;
$adsuccs = tseek::insert_tmpl_includes($adsuccs);   

print "$adsuccs";

}




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

$reviews = &get_template("review_it.html");

$location =  $fields{'ct'};
$location =~ s/-/\//g;
$blt_loc = $location;
$text_location = tseek::decode_dir($location);
$location = "$data_dir/$location/contents.txt";
(@itemline) = &get_item_line ($fields{'id'}, $location);

$title_with_link = "<a href=\"$tseek?id=$fields{'id'}&ct=$fields{'ct'}\" target=\"_blank\">$itemline[1]</a>";
$description = $itemline[2];
$itemurl = $itemline[3];
$rating = $itemline[4];
$votes = $itemline[5];
$reviewfile = $itemline[7];

$title = $itemline[1]; 
$reviews =~ s/%%title%%/$title/g;
$reviews =~ s/%%title_with_link%%/$title_with_link/g;
$reviews =~ s/%%description%%/$description/g;
$reviews =~ s/%%itemurl%%/$itemurl/g;
$reviews =~ s/%%votes%%/$votes/g;
if ($rating eq "") {$rating = "-";}
$reviews =~ s/%%rating%%/$rating/g;
$reviews =~ s/!!rate_it!!/$rate_it_cgi/g;
$reviews =~ s/!!id!!/$fields{'id'}/g;
$reviews =~ s/!!ct!!/$fields{'ct'}/g;
$textbox = "<textarea rows=\"5\" name=\"description\" cols=\"25\"></textarea>";
$reviews =~ s/%%description_box%%/$textbox/g;

$reviews =~ s/!!review_it!!/$review_it/g;
$reviews =~ s/!!ct!!/$fields{'ct'}/g;
$reviews =~ s/!!id!!/$fields{'id'}/g;
$reviews =~ s/!build_url!/$build_url/g;
$reviews =~ s/!!scripts_url!!/$scripts_url/g;



if ($fields{'blt'} eq "")
	{
	$text_location = "<a href=\"$scripts_url/tseekdir.cgi?location=Root-$fields{'ct'}\">$text_location</a>";
	}
	else
	{
	$text_location = "<a href=\"$build_url/$blt_loc\/\">$text_location</a>";
	}
$reviews =~ s/%%returnlink%%/$text_location/g;


$reviewfile = $location;
$reviewfile =~ s/contents\.txt/$fields{'id'}\.rvw/g;
$exists1 = (-e $reviewfile);

if ($exists1 < 1)
	{
	$review_links = "$configitems[17]";
	$reviews =~ s/%%review_listings%%/$review_links/g;
	}
	else
	{
	$fsize1 = (-s "$reviewfile");
	open (DRST, "$reviewfile");
	 if ($file_locking ne "No"){flock (DRST, LOCK_SH) or die "Can't set lock for file: $!\n";}
		read(DRST,$rdata,$fsize1);
	 #if ($file_locking ne "No"){flock (DRST, LOCK_UN);}
	close (DRST);
	
	$review_links = &get_template("reviewlinks.html");
	@ritems = split(/--=:_:=--/,$rdata);
	
	foreach $rchuck (@ritems)
		{
		if (length($rchuck) > 4)
			{
			$listitem = $review_links;

			@ireitem = split(/:-_-:/,$rchuck);
			
			
			if (($ireitem[4] == 1) or ($ireitem[4] eq ""))
			   {
			   
			   $review_disp = "true";
			
				if ($ireitem[1] ne "")
					{
					$listitem =~ s/%%reviewer%%/<a href="mailto:$ireitem[1]">$ireitem[0]<\/a>/g;
					}
					else
					{
					$listitem =~ s/%%reviewer%%/$ireitem[0]/g;
					}
			
				$listitem =~ s/%%review%%/$ireitem[2]/g;
				$listitem =~ s/%%date%%/$ireitem[3]/g;
				$allreviews = $allreviews . $listitem;
				
			   }
			
			}
		}

	}


if ($review_disp ne "true")
	{
	$review_links = "$configitems[17]";
	$reviews =~ s/%%review_listings%%/$review_links/g;	
	}

$reviews =~ s/%%review_listings%%/$allreviews/g;

$reviews = tseek::insert_tmpl_includes($reviews);   
print "$reviews";

}




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

sub get_item_line
{

my ($theid, $thelocation) = @_;

$icounter = $theid;

if (length($icounter) == 1) {$icounter = "000000000000" . $icounter;}
if (length($icounter) == 2) {$icounter = "00000000000" . $icounter;}
if (length($icounter) == 3) {$icounter = "0000000000" . $icounter;}
if (length($icounter) == 4) {$icounter = "000000000" . $icounter;}
if (length($icounter) == 5) {$icounter = "00000000" . $icounter;}
if (length($icounter) == 6) {$icounter = "0000000" . $icounter;}
if (length($icounter) == 7) {$icounter = "000000" . $icounter;}
if (length($icounter) == 8) {$icounter = "00000" . $icounter;}
if (length($icounter) == 9) {$icounter = "0000" . $icounter;}
if (length($icounter) == 10){$icounter = "000" . $icounter;}
if (length($icounter) == 11){$icounter = "00" . $icounter;}
if (length($icounter) == 12){$icounter = "0" . $icounter;}

$cnc = 0;

open (ITEMS, "$thelocation");
if ($file_locking ne "No"){flock (ITEMS, LOCK_SH) or die "Can't set lock for file: $!\n";}
	while (defined($line=<ITEMS>))
		{
		@litems = split(/\[::\]/,$line);
			if ($icounter eq $litems[0])
				{
				$ritems = $line;
			  	}
		}
#if ($file_locking ne "No"){flock (ITEMS, LOCK_UN);}
close (ITEMS);


@litems2 = split(/\[::\]/,$ritems);

return (@litems2);

}





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

if ($temp =~ /\|/) { exit; }

@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;


$fields{'location'} =~ s/\///g; $fields{'location'} =~ s/%//g;
$fields{'ct'} =~ s/\///g; $fields{'ct'} =~ s/%//g;


}



sub get_template
{
my ($tfile) = @_;

$fsize1 = (-s "$data_dir/$tfile");
if ($fsize1 > 0)
	{
	open (DRST, "$data_dir/$tfile");
	 if ($file_locking ne "No"){flock (DRST, LOCK_SH) or die "Can't set lock for file: $!\n";}
		read(DRST,$rval,$fsize1);
	 #if ($file_locking ne "No"){flock (DRST, LOCK_UN);}
	close (DRST);
	}
	else
	{
	print "Could not find <b>$data_dir/$tfile</b>";
	exit;
	}

return ($rval);
}





sub get_vars
{

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


#	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
#   NO REVIEW TEXT			    #17

}







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 ########################################################
}