#!/usr/bin/perl
 
########################################################################
# COPYRIGHT NOTICE:
#
# Copyright 2003 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.
#
########################################################################

#### EDIT HERE -- FOR WINDOWS/IIS BASED INSTALLATIONS ONLY #######

$config_cgi = "config.cgi"; ## <-- CHANGE THIS LINE TO THE FULL SERVER PATH TO config.cgi

# THE PATH ON A WINDOWS INSTALLATION WILL LOOK SOMETHING LIKE THIS:
# $config_cgi = "c:/inetpub/webpub/cgi-bin/pseek/config.cgi";

#### DO NOT CHANGE ANYTHING BELOW THIS LINE #################


#use FindBin;
#use lib $FindBin::Bin;
use CGI;
use DBI;
use pseek;
use CGI::Carp qw(fatalsToBrowser); 
use fmspm;

&get_setup;
fmspm::check_spamb("addurl.cgi", "header", "$data_dir");
$q = CGI->new;

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

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

$gsettings = new_gsettings tseek;


if ($gsettings->{addurl_reg_system} eq "Yes"){&add_url_reg_system; exit;}

if ($q->param('fct') eq ""){&add_url_screen;}

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


sub add_url_reg_system
{
pseek::accesscheck("", "", "ADDURL_CL");
&add_url_screen;
}



sub add_url_screen
{

if ($gsettings->{add_url_form} eq "medium")
	{
	
	if ($q->param('p') == 1) ### IF PREMIUM LINK SUBMISSION
		{

		if ($gsettings->{addurl_reg_system} eq "Yes") ### IF REGISTRATION SYSTEM IN USE
			{
			$addurl_template = pseek::get_file_contents("$data_dir/rgs_add_url_medp.html");
			}
			else ### IF REGISTRATION SYSTEM IN NOT USE
			{
			$addurl_template = pseek::get_file_contents("$data_dir/add_url_medp.html");
			}
		
		}
		else ### IF NOT PREMIUM LINK SUBMISSION
		{

		if ($gsettings->{addurl_reg_system} eq "Yes") ### IF REGISTRATION SYSTEM IN USE
			{
			$addurl_template = pseek::get_file_contents("$data_dir/rgs_add_url_med.html");
			}
			else ### IF REGISTRATION SYSTEM IN NOT USE
			{
			$addurl_template = pseek::get_file_contents("$data_dir/add_url_med.html");
			}		
		}
		
	}
	else ### IF LARGE SEARCH ENGINE ADD URL FORMS ARE USED
	{

	if (int($q->param('cid')) < 1)
		{
		
		if ($gsettings->{addurl_reg_system} eq "Yes") ### IF REG SYSTEM IN USE
			{
			$addurl_template = pseek::get_file_contents("$data_dir/rgs_add_url_instructions.html");
			}
			else
			{
			$addurl_template = pseek::get_file_contents("$data_dir/add_url_instructions.html");
			}
		

		$addurl_template = pseek::ts_insert_template_includes($addurl_template);
		$addurl_template =~ s/!!addurlcgi!!/$script_url\/addurl2.cgi/g;
		$addurl_template =~ s/!!scripts_url!!/$script_url/g;
		
		if ($gsettings->{searchmeth} eq "fulltext")
			{
			$addurl_template =~ s/!!tseeksearch!!/$script_url\/search2\.cgi/g;
			}
			else
			{
			$addurl_template =~ s/!!tseeksearch!!/$script_url\/search\.cgi/g;
			}
		
		$addurl_template =~ s/!build_url!/$web_url/g;
		$addurl_template =~ s/!!build_ursl!!/$web_url/g;
		$addurl_template =~ s/!!modify_link_url!!/$script_url\/modify\.cgi/g;
		$addurl_template = pseek::fill_vars($addurl_template);
 
		print $addurl_template;
		exit;
		}

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


	if ($q->param('p') == 1) ### IF PREMIUM URL
		{
		if ($gsettings->{addurl_reg_system} eq "Yes") 
			{
			$addurl_template = pseek::get_file_contents("$data_dir/rgs_add_urlp.html");
			}
			else
			{
			$addurl_template = pseek::get_file_contents("$data_dir/add_urlp.html");
			}
		}
		else
		{
			if ($gsettings->{addurl_reg_system} eq "Yes") 
			{
			$addurl_template = pseek::get_file_contents("$data_dir/rgs_add_url.html");
			}
			else
			{
			$addurl_template = pseek::get_file_contents("$data_dir/add_url.html");
			}
		}	
		
	}



$addurl_template = pseek::ts_insert_template_includes($addurl_template);
$addurl_template =~ s/!!modify_link_url!!/$script_url\/modify\.cgi/g;


if ($gsettings->{dyn_stat} eq "Dynamic")
	{
	$addurl_template =~ s/!!whatsnew!!/$script_url\/dirs\.cgi?newpop=new/g;
	$addurl_template =~ s/!!tophits!!/$script_url\/dirs\.cgi?newpop=pop/g;
	}
	else
	{
	$addurl_template =~ s/!!whatsnew!!/$web_url\/new\//g;
	$addurl_template =~ s/!!tophits!!/$web_url\/pop\//g;
	}


$addurl_template =~ s/!!add_url_link!!/$script_url\/addurl\.cgi/g;
$addurl_template =~ s/!!add_premium_url_link!!/$script_url\/addurl\.cgi?p=1/g;

$addurl_template =~ s/!!addurlcgi!!/$script_url\/addurl2.cgi/g;
$addurl_template =~ s/!!scripts_url!!/$script_url/g;


		if ($gsettings->{searchmeth} eq "fulltext")
			{
			$addurl_template =~ s/!!tseeksearch!!/$script_url\/search2\.cgi/g;
			}
			else
			{
			$addurl_template =~ s/!!tseeksearch!!/$script_url\/search\.cgi/g;
			}


$addurl_template =~ s/!build_url!/$web_url/g;
$addurl_template =~ s/!!build_ursl!!/$web_url/g;

$texta = "<textarea rows=\"5\" name=\"description\" cols=\"33\"></textarea>";
$addurl_template =~ s/%%submissionbox%%/$texta/g;

$cid = $q->param('cid');


if ($gsettings->{add_url_form} eq "medium")
	{
	&get_dropdown;
	}
	else
	{
	&get_cat_large_engines;
	}

if ($q->param('p') == 1){$addurl_template =~ s/!!p!!/1/g;}



### IF IMAGE AUTHENTICATION IS ENABLED THEN DISPLAY LETTERS

#if ($gsettings->{img_auth} eq "Yes")
if ($gsettings->{img_auth} eq "Yes")
	{
	$imgbox = pseek::get_imgboxes;
	
	($imdisp, $imgnames) = split (/:::/, $imgbox);
	
	$imgnames =~ s/\.gif//gi;
	$addurl_template =~ s/!!ia!!/$imgnames/g;
	$addurl_template =~ s/%%imgbox%%/$imdisp/g;
	}


print $addurl_template;

}












sub get_cat_large_engines
{

### CONNECT TO DB

if ($mysql_hostname eq ""){$dsn = "DBI:mysql:$db_name";}else{$dsn = "DBI:mysql:$db_name:$mysql_hostname:$mysql_port";}
$dbh = DBI->connect($dsn, $db_username, $db_password);
if ( !defined $dbh ) {die "Cannot connect to MySQL server: $DBI::errstr\n"; }


### GET CATEGORIES

$sth = $dbh->prepare("SELECT * FROM dirs WHERE cid = '$cid'");
$sth->execute;
$serror = ""; $serror = $sth->errstr; if ($serror ne "") {die "SQL Syntax Error: $serror \n<br> From: SELECT * FROM dirs WHERE cid = '$cid'";}

while ( @row = $sth->fetchrow() )
	{
	for ($ms = 2; $ms < 18; $ms++) 
		{
		if ($row[$ms] ne "")
			{
			$dcat = $dcat . $row[$ms] . "/";
			}
		}
	$catlock = $row[22];
	}

$sth->finish;
$dbh->disconnect;


###########

if ($catlock eq "Y")
	{
	if ($gsettings->{addurl_reg_system} eq "Yes")
		{
		$addurl_template = pseek::get_file_contents("$data_dir/rgs_locked.html");
		}
		else
		{
		$addurl_template = pseek::get_file_contents("$data_dir/locked.html");
		}

	$addurl_template = pseek::ts_insert_template_includes($addurl_template);
	$addurl_template =~ s/!!addurlcgi!!/$script_url\/addurl2.cgi/g;
	$addurl_template =~ s/!!scripts_url!!/$script_url/g;
	
		if ($gsettings->{searchmeth} eq "fulltext")
			{
			$addurl_template =~ s/!!tseeksearch!!/$script_url\/search2\.cgi/g;
			}
			else
			{
			$addurl_template =~ s/!!tseeksearch!!/$script_url\/search\.cgi/g;
			}
	
	$addurl_template =~ s/!build_url!/$web_url/g;
	$addurl_template =~ s/!!build_url!!/$web_url/g;
	$addurl_template =~ s/!!modify_link_url!!/$script_url\/modify\.cgi/g;
	$addurl_template = pseek::fill_vars($addurl_template);
	print $addurl_template;
	exit;
	}

$addurl_template =~ s/%%add_category%%/$dcat/g;
$addurl_template =~ s/!!catid!!/$cid/g;

}




sub get_dropdown
{

### CONNECT TO DB

if ($mysql_hostname eq ""){$dsn = "DBI:mysql:$db_name";}else{$dsn = "DBI:mysql:$db_name:$mysql_hostname:$mysql_port";}
$dbh = DBI->connect($dsn, $db_username, $db_password);
if ( !defined $dbh ) {die "Cannot connect to MySQL server: $DBI::errstr\n"; }

### GET CATEGORIES


#$sth = $dbh->prepare("SELECT * FROM dirs WHERE ((crosslinkid IS NULL) OR (crosslinkid < 1)) AND (catlock != 'Y')");

$sth = $dbh->prepare("SELECT * FROM dirs WHERE ((crosslinkid IS NULL) OR (crosslinkid < 1)) ORDER BY level");
$sth->execute;
$serror = ""; $serror = $sth->errstr; if ($serror ne "") {die "SQL Syntax Error: $serror \n<br> From: SELECT * FROM dirs WHERE ((crosslinkid IS NULL) OR (crosslinkid < 1)) AND ((catlock IS NULL) OR (catlock = 'N'))";}

$hcount = 0;
while ( @row = $sth->fetchrow() )
	{
	
	$dirstring = "";
	if ($row[2] ne "") {$dirstring = $row[2];}
	if ($row[3] ne "") {$dirstring = $dirstring . "/$row[3]";}
	if ($row[4] ne "") {$dirstring = $dirstring . "/$row[4]";}
	if ($row[5] ne "") {$dirstring = $dirstring . "/$row[5]";}
	if ($row[6] ne "") {$dirstring = $dirstring . "/$row[6]";}
	if ($row[7] ne "") {$dirstring = $dirstring . "/$row[7]";}
	if ($row[8] ne "") {$dirstring = $dirstring . "/$row[8]";}
	if ($row[9] ne "") {$dirstring = $dirstring . "/$row[9]";}
	if ($row[10] ne "") {$dirstring = $dirstring . "/$row[10]";}
	if ($row[11] ne "") {$dirstring = $dirstring . "/$row[11]";}
	if ($row[12] ne "") {$dirstring = $dirstring . "/$row[12]";}
	if ($row[13] ne "") {$dirstring = $dirstring . "/$row[13]";}
	if ($row[14] ne "") {$dirstring = $dirstring . "/$row[14]";}
	if ($row[15] ne "") {$dirstring = $dirstring . "/$row[15]";}
	if ($row[16] ne "") {$dirstring = $dirstring . "/$row[16]";}
	if ($row[17] ne "") {$dirstring = $dirstring . "/$row[17]";}


		if (($row[22] ne "H") and ($row[22] ne "Y"))
		{

		
			$part_of_hidden = "false";
			foreach $hitem (@hcats)
				{
				$matchcrit = substr($dirstring, 0, length($hitem)) . "/";
				
					if ($matchcrit eq "$hitem/")
					{
					$part_of_hidden = "true";
					}
				
				#print "--> $hitem/ | $matchcrit = $part_of_hidden<BR>\n";
				}
			
			
			if ($part_of_hidden eq "false")
				{
				$atree[$acnt] = $dirstring . "\t" . 
									 $row[0] . "\t" .  #### CID
									 $row[1] . "\t" .  #### LEVEL
									 $row[28]; #### NAME
				$acnt++;
				}
			
		}
		elsif ($row[22] eq "H")
		{
		$hcats[$hcount] = $dirstring;
		$hcount++;
		}
	

	if ($row[1] == 1) { $total_links = $total_links + $row[20]; }
	
	}



$dropdownb = "<select name=\"category_dropdown\" size=\"1\">";

@atree = sort(@atree);

foreach $item (@atree)
	{
	($dirstr, $cid2, $level, $name) = split (/\t/, $item);

	$spacing = "";
	for ($ms = 0; $ms < ($level + 1); $ms++) 
			{
			$spacing = $spacing . "&nbsp; &nbsp; ";
			}

	if ($cid2 eq $cid)
		{
		$dropdownb = $dropdownb . "<option selected value=\"$cid2\">$dirstr</option>";
		}
		else
		{
		$dropdownb = $dropdownb . "<option value=\"$cid2\">$dirstr</option>";
		}
	
	}

$dropdownb = $dropdownb . "</select>";

########

$sth->finish;
$dbh->disconnect;

$addurl_template =~ s/%%category_dropdown%%/$dropdownb/g;

}















sub get_setup
{

$exists = (-e "$config_cgi") or die ("Unable to open $config_cgi");
if ($exists > 0)
	{
	open (STP, "$config_cgi");
		while (defined($line=<STP>))
			{
			if ($line =~ m/#/g)
				{
				$r = pos($line);
				$line = substr($line, 0, $r - 1);
				}
				
				$line =~ s/\n//g;
	
if ($line =~ /^DB_NAME/){$db_name = &get_setup_line($line, DB_NAME);}
if ($line =~ /^DB_USERNAME/){$db_username = &get_setup_line($line, DB_USERNAME);}
if ($line =~ /^DB_PASSWORD/){$db_password = &get_setup_line($line, DB_PASSWORD);}
if ($line =~ /^MYSQL_HOSTNAME/){$mysql_hostname = &get_setup_line($line, MYSQL_HOSTNAME);}
if ($line =~ /^MYSQL_PORT/){$mysql_port = &get_setup_line($line, MYSQL_PORT);}

if ($line =~ /^SCRIPT_URL/){$script_url = &get_setup_line($line, SCRIPT_URL);}
if ($line =~ /^ADMIN_URL/){$admin_url = &get_setup_line($line, ADMIN_URL);}
if ($line =~ /^WEB_URL/){$web_url = &get_setup_line($line, WEB_URL);}
if ($line =~ /^WEB_DIR/){$web_dir = &get_setup_line($line, WEB_DIR);}
if ($line =~ /^DATA_DIR/){$data_dir = &get_setup_line($line, DATA_DIR);}

if ($line =~ /^USERNAME/){$username = &get_setup_line($line, USERNAME);}
if ($line =~ /^PASSWORD/){$password = &get_setup_line($line, PASSWORD);}


			}
	close (STP);
	
	}
}




sub get_setup_line
{
my ($setup_line, $setup_var) = @_;
$crit = "\"";
$setup_line =~ m/$crit/g;
$r1 = pos($setup_line);
$setup_line =~ m/$crit/g;
$r2 = pos($setup_line);
$setup_line = substr($setup_line, $r1, ($r2 - $r1 - 1));
$return_val = $setup_line;
return ($return_val);
}

#### END CONFIGURATION ########################################################

