#!/usr/bin/perl

package htbuild;

########################################################################
# COPYRIGHT NOTICE:
#
# Copyright 2008 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 Time::Local;
use CGI::Carp qw(fatalsToBrowser); 
#use strict;

require Exporter;

use vars qw/@ISA @EXPORT @EXPORT_OK $copyright $prgname $q $gsettings $pages_prev $pages_next $data_dir
			  $script_url $dsn $db_name $dbh $dsn $db_username $db_password $sth $web_url $scnts
			  $default_permissions $mysql_hostname $mysql_port $line $web_dir $username $password 
			  $crit $r1 $r2 $return_val $tmp $admin_url $cofile $config_cgi $exists $r 
			  /;

$default_permissions = 0777; ### PERMISSIONS THAT IS USED FOR TEMPORARY DATA FILES

@ISA = qw(Exporter);

@EXPORT = qw(get_cat); 

@EXPORT_OK = qw($defdir);


&get_setup;
$q = CGI->new;



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

sub get_cat
{
my ($current_cat, $cur_lv, $static, $build_st, $build_nd, $newpop, $popnewcat, $morename) = @_;

my ($encoded_ct, $defdir, $tmplsearch, $subdirs, $directories1, $location_seperator,
    @ditems, $nri, $cnt, $litem, $origloc, $last_dir, $forigloc, $location,
    $this_level, $sql, $sql_links, $current_cat_level, $rows, $orows, $modp,
    $nrsub, $links_per_col, $new_lv, $lncnt, $cid, $level, $description, $keywords,
    $linknrs, $related1, $related2, $related3, $related4, $related5, $related6, 
    $template, $shortname, $crosslinkid, $dir_style, $enc_shortname, $rpld, $subcts,
    $a1, $subcts2, $subcts3, $subdata, $icnt, $nr_searchres, $pages, $st, $nd,
    $main_nd, $main_st, $ippc, $current_tmplsearch, $title_with_link, $description,
    $itemurl, $date, $votes, $rating, $hits, $url, $rateit, $comments, $unewin, $pg,
    $ms, $cnposition, $hiddenstr, $pgstring, $prev_ppos, $serror, @row, $a2, $rating_image,
    $final_tmplsearch, $currentpage, $pvst, $pvnd, $next_ppos, $spls, $st1, $nd1, $nextt,
    $prev, $spcer, $prevnext, $add_url_link, $cust_sql, $current_linkinf, $dizbox, 
    $all_reviews, $dbh2, $review_sql, $sth2, @row2, $tmp_review, $review_style, 
    $review_date, $tmp_adhtml, $bid, $adslot2, $related_dirs, $rel_tmp,
    $reldir_crit, $dirname, $edir, $rcat, $ecat, $dirname, $wsql, $ritem, @relids,
    $filename1, $filename2, $tmpl_in_use, $related_links, $crosslinked_sql,
    $replc_crit, $replc_match, $adhtml, $adslot5, $adslot4, $adslot3, $adslot2, $adslot1, 
    @prevnextp3, @alld, $lsurl, $nr, $prevnextp2, $pfn, $real_dir, $add_url_premium,
    $page_cnt, $uplug, $tmitem, $cnc, @nxpvitem1, $nxpvitem, $page_nr, $staticpos, 
    $nxpu, $page_nr, $writefile, $imdisp, $imgnames, $imgbox, $critpop, $critnew, 
    $current_static_catlink, $noreviewtext, $listing_keywords, $rowcntr, $keyname,
    $na, $imgcrit, $imgk, $k_item, $description1, $searched, $size, $visible, @akeys,
    $time1, $time2, $time3, $today_time, $nrc, $mfile, $dynstat, $cats, $dircrit,
    $ecats_stat, $ecats, $clv, $knr, $dirms, $the_link_image, $imgh, $imgw, 
    $the_image_url, $img_fname, $image_height, $image_width, $rptdl, $modifyl, $linkinfo,
    $linkinfop, $tmplsearch_prem, $pop_tmplsearch_prem, $nowtime, $keydata, $inumber_pi,
    $orderbyhcrit, $sql_str, $popinclname, $pop_tmplsearch_prem, $popinclname, $iname_pi,
    $cid_pi, $poplv, $poplvname, $popcat_sql, $itype_pi, $isort_pi, $orderby, $randomnumber, 
    $pop1, $thiscategory, $updated, $custom_pagetitlen, $sscn, $newgif, $new1time, 
    $new1secs, $newgif, $dlink_numbers, $auth_images, $filename3, $filename4, 
    $allrows, @all_dir_rows, $rowitme, $mathf, $realdir, $colccount, $numbercols,
    $srcolname, $msc, $colrepname, $msc_ad, $searchresultsname, %scol, $location_plain
    );

$scol{'col0'} = ""; $scol{'col1'} = "";
$scol{'col2'} = ""; $scol{'col3'} = "";
$scol{'col4'} = ""; $scol{'col5'} = "";

#$static = "STATIC";

#### GET SETTINGS

$gsettings = &new_gsettings;
$encoded_ct = $current_cat;

$pages_prev = "3";
$pages_next = "3";

$topvar = &get_file_contents2("$data_dir/frontlink.dat");


### FOR IMAGE AUTHENTICATION
@alpharray = ("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z");
$auth_images = &get_file_contents2("$data_dir/idirs.dat");
@auth_img = split(/\n/,$auth_images);


#### GET TEMPLATES
$defdir = &get_file_contents2("$data_dir/defdir.html");
$tmplsearch = &get_file_contents2("$data_dir/tmplsearch.html");
$review_style = &get_file_contents2("$data_dir/reviewlinks.html");

$tmplsearch_prem = &get_file_contents2("$data_dir/tmplsearch_prem.html");
if ($tmplsearch_prem eq "") { $tmplsearch_prem = $tmplsearch; }

$subdirs = &get_file_contents2("$data_dir/subdirs.html");
		if ($subdirs =~ /%%subdirs1%%/i){$nrsub = 1;}
		if ($subdirs =~ /%%subdirs2%%/i){$nrsub = 2;}
		if ($subdirs =~ /%%subdirs3%%/i){$nrsub = 3;}


$directories1 = &get_file_contents2("$data_dir/directories1.html");
$tmpl_in_use = "def";
$linkinfo = &get_file_contents2("$data_dir/linkinfo.html");
$linkinfop = &get_file_contents2("$data_dir/linkinfop.html");
if ($linkinfop eq "") { $linkinfop = $linkinfo; } ### IF THE PREMIUM LINK TEMPLATE CONTAINS NOTHING THEN USE THE NORMAL LINK INFO TEMPLATE


if ($defdir =~ /!!rss!!/)
	{
	$rssmain = &get_file_contents2("$data_dir/rss.conf");	
	@rssitems = split (/o--:--:--o/, $rssdata);
	$rss_img = $rssitems[0];
	if ($rss_img eq "") { $rss_img = "$web_url/rss.gif"; }
	$mainrss = $rssitems[1];
	$rssitems = $rssitems[2];	
	}
	

$nowtime = time();
($gsec,$gmin,$ghour,$gmday,$gmon,$gyear,$gwday,$gydat,$gisdst) = localtime($nowtime);
$gyear = $gyear + 1900;



#### CONNECT TO DATABASE
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"; }



###########################################################################
if ($newpop eq "")
{

	#### LOCATION STRING + QUERY STRING

	$location_seperator = $gsettings->{location_seperator};

	@ditems = split (/-/, $current_cat);
	$nri = @ditems;
	$cnt = 0;

	
	### GET MORE PAGES NAME
	if ($gsettings->{category_page_names} eq "categorynames")
		{
			$morename = "";
			
			$ddcodedit = &decode_dir2($ditems[$nri-1]);
			$tmp_name1 = lc($ddcodedit);
			$tmp_name1 =~ s/ /_/gi;
			for ($ms = 0; $ms < length($tmp_name1); $ms++) 
			{
			$oneletter1 = substr($tmp_name1, $ms, 1);
			if (($oneletter1 =~ /[0-9a-zA-Z]/) or ($oneletter1 eq "_"))
				{
				$morename = $morename . $oneletter1;
				}
			}
		}
		else
		{
		$morename = "more";
		}




	foreach $litem (@ditems)
		{
		$cnt++;
		
		$origloc = $origloc . $litem . "-";
		
		$litem = &decode_dir2($litem);
		$last_dir = $litem; ### LAST DIRECTORY
		
		if ($cnt != $nri)
			{
			$forigloc = substr($origloc, 0, length($origloc) - 1);
			
			if ($static eq "STATIC")
				{
				$real_dir = $forigloc;
				$real_dir =~ s/-/\//g;
				$real_dir = $real_dir . "/";
				$location = $location . " <a href=\"$web_url/$real_dir\">$litem</a> " . $location_seperator;
				$location_plain = $location_plain . $litem . "+";
				}
				else
				{
				$location = $location . " <a href=\"$script_url/dirs.cgi?lv=$cnt&ct=$forigloc\">$litem</a> " . $location_seperator;
				$location_plain = $location_plain . $litem . "+";
				}
			}
			else
			{
			$category_full = $location . " FULLCTF";
			$location = $location . " $litem";
			$location_plain = $location_plain . $litem;
			$current_cat = $litem;
			}
	
		$this_level = "l" . $cnt;
		$litem =~ s/'/\\'/g;

		$sql = $sql . " ($this_level = '$litem') and";
		$cust_sql = $cust_sql . " ($this_level = '$litem') and";
		$sql_links = $sql_links . " (A.$this_level = '$litem') and";

		}
	
	$cnt++;

	
	$current_cat_level = $cnt  -1;
	$sql = $sql . " ((level = '$cnt') or (level = '$current_cat_level'))";
	#$sql = $sql . " (level = '$cnt') or (level = '$current_cat_level')";
	$sql_links = $sql_links . " (A.level = '$current_cat_level')";


	#### CHECK IF WE ARE USING A CUSTOM TEMPLATE
	
	$cust_sql = substr($cust_sql, 0, length($cust_sql) - 3);
	$cust_sql = "SELECT template FROM dirs WHERE (level = '$cur_lv') AND ($cust_sql)";

	$sth = $dbh->prepare($cust_sql);
	if ( !defined $dbh ) {die "Cannot connect to mSQL server: $DBI::errstr\n"; }
	$sth->execute;
	$serror = ""; $serror = $sth->errstr; if ($serror ne "") {die "SQL Syntax Error: $serror";}

	while ( @row = $sth->fetchrow() )
		{
		if ($row[0] ne "")
			{
			$filename1 = "$data_dir/$row[0]-1ctst.html"; 
			if ((-e "$filename1") > 0)
					{
					$filename3 = "$data_dir/$row[0]-3ctst.html";
					$subdirs = &get_file_contents2($filename3);
					if ($subdirs =~ /%%subdirs1%%/i){$nrsub = 1;}
					if ($subdirs =~ /%%subdirs2%%/i){$nrsub = 2;}
					if ($subdirs =~ /%%subdirs3%%/i){$nrsub = 3;}
					
					$filename4 = "$data_dir/$row[0]-4ctst.html";
					$directories1 = &get_file_contents2($filename4);
					}
			}
		
		}	
	
	#print "==> $subdirs \n\n\n\n\n\n";
	#print "==> $nrsub <Br> $cust_sql";
	
	##### GET SUB DIRECTORIES 

	$sql = "SELECT * FROM dirs WHERE $sql ORDER BY shortname";
#	$sql = "SELECT * FROM dirs WHERE (catlock != 'H') AND  $sql  ORDER BY shortname";


	$sth = $dbh->prepare($sql);
	if ( !defined $dbh ) {die "Cannot connect to mSQL server: $DBI::errstr\n"; }
	$sth->execute;
	$serror = ""; $serror = $sth->errstr; if ($serror ne "") {die "SQL Syntax Error: $serror";}

	$rows = 0;
	while ( @row = $sth->fetchrow() )
		{
		
		
		if ($row[22] ne "H")
			{
			$allrows = "";
			for ($ms = 0; $ms < 35; $ms++)
				{
				$allrows = $allrows . $row[$ms] . "FDL-MITR";
				}
			$all_dir_rows[$rows] = $allrows;
			$rows++;
			}
		
		
		}
	
	#### PREPARE TO BUILD CATEGORY TABLE

	$rows = $rows - 1; #$rows = $sth->rows(); $rows = $rows - 1; 
	
	$orows = $rows;
	$modp = ($rows % $nrsub);
	$rows = $rows - $modp;
	$links_per_col = ($rows / $nrsub);
	
	$new_lv = $cur_lv + 1;
	$lncnt = 1;

	


	#### BUILD CATEGORY TABLE
	
	#while ( @row = $sth->fetchrow() )
	foreach $rowitme (@all_dir_rows)
		{
		my @row = split (/FDL-MITR/, $rowitme);
		
		
		if (($row[28] eq $last_dir) and ($cur_lv eq $row[1])) ### IF CURRENT DIR = ONE OF THE RETURNED DIRS
			{
			
			$mathf = "true";
			
			if ($row[27] ne "") ### IF CATEGORY USES CUSTOM TEMPLATE
				{
				$tmpl_in_use = $row[27];
				
				$filename1 = "$data_dir/$row[27]-1ctst.html"; 
					if ((-e "$filename1") > 0)
					{
					$filename2 = "$data_dir/$row[27]-2ctst.html";
					#$filename3 = "$data_dir/$row[27]-3ctst.html";
					#$filename4 = "$data_dir/$row[27]-4ctst.html";
					$filename7 = "$data_dir/$row[27]-7ctst.html";
					$filename8 = "$data_dir/$row[27]-8ctst.html";
					$filename9 = "$data_dir/$row[27]-9ctst.html";
					
					$defdir = &get_file_contents2($filename1);

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

					
					$defdir = &insert_template_includes($defdir);
					
					$tmplsearch = &get_file_contents2($filename2);
					
					$tmplsearch_prem = &get_file_contents2($filename8);
					if ($tmplsearch_prem eq "") { $tmplsearch_prem = $tmplsearch; }
					
					#$subdirs = &get_file_contents2($filename3);
					#$directories1 = &get_file_contents2($filename4);
					$linkinfo = &get_file_contents2($filename7);
					$linkinfop = &get_file_contents2($filename9);
					}
					else
					{
					if ($gsettings->{searchmeth} eq "fulltext"){$defdir =~ s/!!tseeksearch!!/$script_url\/search2\.cgi/gi;}
					else {$defdir =~ s/!!tseeksearch!!/$script_url\/search\.cgi/gi;}
					$defdir = &insert_template_includes($defdir);
					}
					
				}
				else
				{
				### INSERT INCLUDES
				if ($gsettings->{searchmeth} eq "fulltext"){$defdir =~ s/!!tseeksearch!!/$script_url\/search2\.cgi/gi;}
				else {$defdir =~ s/!!tseeksearch!!/$script_url\/search\.cgi/gi;}
				$defdir = &insert_template_includes($defdir);
				}
	
			$cid = $row[0];
			$level = $row[1];
			$custom_pagetitlen = $row[25];
			$description = $row[18];
			$keywords = $row[19];
			$linknrs = $row[20];
			$related1 = $row[21];
			$related2 = $row[22];
			$related3 = $row[23];
			$related4 = $row[24];
			$related5 = $row[25];
			$related6 = $row[26];
			$template = $row[27];
			$shortname = $row[28];
			$crosslinkid = $row[29];
			$related_links = $row[21];
	
			### BANNER SLOTS
			$adslot1 = $row[30];
			$adslot2 = $row[31];
			$adslot3 = $row[32];
			$adslot4 = $row[33];
			$adslot5 = $row[34];
			
			$defdir =~ s/%%location%%/$location/g;
			$defdir =~ s/%%current_cat%%/$current_cat/g;
			$location_plain =~ s/ /+/g;
			$defdir =~ s/%%location_plain%%/$location_plain/g;
			

			
			}
			else
			{
			$dir_style = $directories1;
			$dir_style =~ s/%%sub_description%%/$row[18]/gi;
			
		
			if ($row[20] eq "") {$row[20] = 0;}
			$enc_shortname = &encode_dir2($row[28]);
	
			
			if ($static eq "STATIC")
				{
				if ($row[29] > 0)
					{
					$crosslinked_sql = $crosslinked_sql . " (cid = '$row[29]') OR";
					if ($gsettings->{nr_links_dirs} ne "No") { $dlink_numbers = "(-$row[29]-LNKNMBRS)"; } else { $dlink_numbers = ""; } 
					$rpld = "<a href=\"$web_url/-$row[29]-SYMLNKDIR\">$row[28]</a> \@$dlink_numbers";
					}
					else
					{
					$realdir = "$encoded_ct-$enc_shortname";
					$realdir =~ s/-/\//g; $realdir  = $realdir  . "/";
					if ($gsettings->{nr_links_dirs} ne "No") { $dlink_numbers = "($row[20])"; } else { $dlink_numbers = ""; } 
					$rpld = "<a href=\"$web_url/$realdir\">$row[28]</a> $dlink_numbers";
					}
				}
				else
				{
				
				
				if ($row[29] > 0)
					{
					$crosslinked_sql = $crosslinked_sql . " (cid = '$row[29]') OR";
					if ($gsettings->{nr_links_dirs} ne "No") { $dlink_numbers = "(-$row[29]-LNKNMBRS) "; } else { $dlink_numbers = ""; } 
					$rpld = "<a href=\"$script_url/dirs.cgi?-$row[29]-DRLNKKK\">$row[28]</a> \@$dlink_numbers";
					}
					else
					{
					if ($gsettings->{nr_links_dirs} ne "No") { $dlink_numbers = "($row[20]) "; } else { $dlink_numbers = ""; } 
					$rpld = "<a href=\"$script_url/dirs.cgi?lv=$new_lv&ct=$encoded_ct-$enc_shortname\">$row[28]</a> $dlink_numbers";
					}
				}
			
			### SEE IF THIS IS A NEW DIRECTORY
			$newgif = "";
			if ($gsettings->{newlinks} ne "No")
				{
				$new1secs = $gsettings->{days1} * 86400;
				$new1time = $nowtime - $new1secs;
				if ($row[23] > $new1time)
					{
					$newgif = "<img src=\"$web_url/new1.gif\">";
					}
				
				####
				if ($newgif eq "")
				{
				$new1secs = $gsettings->{days2} * 86400;
				$new1time = $nowtime - $new1secs;
				if ($row[23] > $new1time)
					{
					$newgif = "<img src=\"$web_url/new2.gif\">";
					}
				}
				
				####
				if ($newgif eq "")
				{
				$new1secs = $gsettings->{days3} * 86400;
				$new1time = $nowtime - $new1secs;
				if ($row[23] > $new1time)
					{
					$newgif = "<img src=\"$web_url/new3.gif\">";
					}
				}				
				
				}
			

			$dir_style =~ s/%%substyle%%/$rpld/gi;
			$dir_style =~ s/!!new!!/$newgif/gi;

		
			if ($nrsub == 1) #########
				{
				$subcts = $subcts . $dir_style . "<br>";
				}
				elsif ($nrsub == 2) #########
				{
			
					if ($modp == 1) { $a1 = 1; }
					if ($lncnt <= $links_per_col + $a1)
						{
						$subcts = $subcts . $dir_style . "<br>";
						}
						else
						{
						$subcts2 = $subcts2 . $dir_style . "<br>";
						}
			
				}
				elsif ($nrsub == 3) #########
				{
		   
				if ($modp == 1) { $a1 = 1; }
				if ($modp == 2) { $a2 = 1; $a1 = 1;}
			
				if ($lncnt <= ($links_per_col + $a1))
					{
					$subcts = $subcts . $dir_style. "<br>";
					}
				elsif ($lncnt <= (($links_per_col * 2) + $a2 + $a1))
					{
					$subcts2 = $subcts2 . $dir_style . "<br>";
					}
					else
					{
					$subcts3 = $subcts3 . $dir_style . "<br>";
					}
				}
			$lncnt++; $sscn++;
		  }
	
	}


	#########

	$subdata = $subdirs;
	
	$subdata =~ s/%%subdirs1%%/$subcts/gi;
	if ($nrsub == 2) {$subdata =~ s/%%subdirs2%%/$subcts2/gi;}
	if ($nrsub == 3) {$subdata =~ s/%%subdirs2%%/$subcts2/gi; $subdata =~ s/%%subdirs3%%/$subcts3/gi;}
	
	if ($sscn > 0)
		{
		$defdir =~ s/%%subdirectories%%/$subdata/g;
		}
		else
		{
		$defdir =~ s/%%subdirectories%%//g;
		}

$defdir =~ s/!!cid!!/$cid/g;

	##### FILL TEMPLATE WITH DATA
	
	if ($custom_pagetitlen eq "") { $custom_pagetitlen = $gsettings->{custom_pagetitle}; }
	$defdir =~ s/%%custom_pagetitle%%/$custom_pagetitlen/gi;

	$defdir =~ s/<!-- Hide %%metadescription%% \/\/ End Hide -->/<meta name="Description" content="$description">/gi;
	$defdir =~ s/<!-- Hide %%metakeywords%% \/\/ End Hide -->/<meta name="Keywords" content="$keywords">/gi;
	
	$defdir =~ s/%%metakeywords%%/$keywords/gi;
	
	$defdir =~ s/%%pagedescription%%/$description/gi;
	$defdir =~ s/%%pagetitle%%/$shortname/gi;

	if ($keywords eq "") { $keywords = "$location_plain"; }
	
	$defdir =~ s/!!keywords!!/$keywords/gi;
	$defdir =~ s/%%keywords%%/$keywords/gi;
	
	$updated = time();
	$updated = &decode_date2($updated);
	$defdir =~ s/%%updated%%/$updated/gi;

	$thiscategory = "CST_" . $encoded_ct;
	$defdir =~ s/!!thiscategory!!/$thiscategory/gi;
	
	
### PREPARE BANNER HTML CODE

if (($adslot1 ne "") or ($adslot2 ne "") or ($adslot3 ne "") or ($adslot4 ne "") or ($adslot5 ne ""))
	{
	$randomnumber = int(rand(999999));

$adhtml = <<END_OF_ADHTML;
<!-- Begin Banner Code --> 
<SCRIPT LANGUAGE="JavaScript">
<!-- 
random = parseInt(Math.random()*10000000)
banner = '<a href="$script_url/click2.cgi?grp=%%group%%&'
banner += 'pg=' + random + '" TARGET="_blank">'
banner += '<img src="$script_url/click.cgi?grp=%%group%%&'
banner += 'pg=' + random + '" alt="Click Here!" BORDER=0></a>'
document.write(banner)
// -->
</script>
<!-- End Banner Code -->
END_OF_ADHTML
	}


###### EXPIRE PREMIUM LINKS THAT NEEDS TO EXPIRE

if ($cid ne "")
{
$premsql = "UPDATE links SET premium = '0' 
					WHERE (cid = $cid) AND
					(premium > 0) AND (premium IS NOT NULL)
					AND (subs_expire < '$nowtime') AND 
					(subs_expire IS NOT NULL) AND (subs_expire != '0')";

$sth = $dbh->prepare($premsql);
$sth->execute;
$serror = ""; $serror = $sth->errstr; if ($serror ne "") {die "SQL Syntax Error: $serror - From: $sql";} 
}

	

###### GET LINKS

	if ($gsettings->{dir_sort_links} eq "Popularity")
		{
		#$orderby = "ORDER BY ((B.votes * B.rating) + (B.premium * 1000000)) DESC";
	   #$pop1 = ", (B.rating + B.votes + B.hits) \"popul\" ";
	   $pop1 = ", (IFNULL(B.rating,0) + IFNULL(B.votes,0) + IFNULL(B.hits,0)) AS popul ";
	   $orderby = "popul DESC,  title ASC";
		}
		elsif ($gsettings->{dir_sort_links} eq "Alphabetical")
		{
		$orderby = "B.title ASC";
		}
		elsif ($gsettings->{dir_sort_links} eq "Hits")
		{
		$orderby = "hitsort DESC, B.title ASC";
		}
		elsif ($gsettings->{dir_sort_links} eq "Votes")
		{
		$orderby = "votesort DESC, B.title ASC";
		}
		elsif ($gsettings->{dir_sort_links} eq "Ratings")
		{
		$orderby = "ratingsort DESC, B.title ASC";
		}
		elsif ($gsettings->{dir_sort_links} eq "Newest")
		{
		$orderby = "B.urldate DESC, B.title ASC";
		}
		else
		{
		$orderby = "B.title ASC";
		}


	$sql_links = "SELECT B.*,
				IFNULL(B.premium, 0) AS premsort,
				IFNULL(B.rating, 0) AS ratingsort,
				IFNULL(B.votes, 0) AS votesort,
				IFNULL(B.hits, 0) AS hitsort	
				$pop1 
			FROM dirs AS A, links AS B
			WHERE 
			(A.cid = B.cid)
			AND
			($sql_links)
			AND
			((B.newsubm = '') OR (B.newsubm IS NULL))
			ORDER BY premsort DESC, $orderby
			";

#print $sql_links . "<BR>";

} ### END OF CHECK TO SEE IF THIS IS NOT WHAT'S NEW or POPULAR
elsif ($newpop eq "new") ### IF LISTING NEW LISTINGS
{
	
	$defdir = &get_file_contents2("$data_dir/whatsnew_main.html");
	$tmplsearch = &get_file_contents2("$data_dir/whatsnew_lists.html");
	$pop_tmplsearch_prem = &get_file_contents2("$data_dir/whatsnew_lists_prem.html");
	
	$defdir =~ s/%%whats_new_listings%%/%%searchresults%%/gi;
	
	$sql_links = "SELECT A.*, B.l1, B.l2, B.l3, B.l4, B.l5, B.l6, B.l7, B.l8, B.l9, B.l10, B.l11, B.l12, B.l13, B.l14, B.l15, B.l16, B.level 
					  FROM links AS A, dirs AS B
					  WHERE 
					  (A.cid = B.cid) AND 
					  ((B.catlock != 'H') OR (B.catlock IS NULL)) AND 
					  ((newsubm = '') OR (newsubm IS NULL))
					  ORDER BY urldate DESC LIMIT $gsettings->{whatsnew_link_no}";
	
	
	if ($gsettings->{searchmeth} eq "fulltext"){$defdir =~ s/!!tseeksearch!!/$script_url\/search2\.cgi/gi;}
	else {$defdir =~ s/!!tseeksearch!!/$script_url\/search\.cgi/gi;}
	$defdir = &insert_template_includes($defdir);

}
elsif ($newpop eq "pop")
{

	$defdir = &get_file_contents2("$data_dir/tophits_main.html");
	$tmplsearch = &get_file_contents2("$data_dir/tophits_listings.html");
	$pop_tmplsearch_prem = &get_file_contents2("$data_dir/tophits_listings_prem.html");
	
	$defdir =~ s/%%tophits_listings%%/%%searchresults%%/gi;
	
	$sql_links = "SELECT A.*, B.l1, B.l2, B.l3, B.l4, B.l5, B.l6, B.l7, B.l8, B.l9, B.l10, B.l11, B.l12, B.l13, B.l14, B.l15, B.l16, B.level
						FROM links AS A, dirs AS B
						WHERE 
						(A.cid = B.cid) AND 
						((B.catlock != 'H') OR (B.catlock IS NULL)) AND 
					  ((newsubm = '') OR (newsubm IS NULL)) 
					  ORDER BY votes DESC,hits DESC LIMIT $gsettings->{tophits_link_no}";

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

}
elsif ($newpop eq "include") ### GENERATE POP & NEW INCLUDES
{

	($iname_pi, $cid_pi, $isort_pi, $inumber_pi, $itype_pi, $itype_prem) = split (/:/, $popnewcat);

	####
	$nr_searchres = $inumber_pi;
	
	if ($isort_pi eq "vote") { $orderbyhcrit = "votes"; } else { $orderbyhcrit = "hits"; } 
	
	if ($itype_pi eq "New") { $orderbyhcrit = "urldate";}
	
	#### GET CATEGORIES WHERE POPULAR LINKS ARE TO BE OBTAINED
	
	$popcat_sql = "SELECT * FROM dirs WHERE cid = '$cid_pi'";
	$sth = $dbh->prepare($popcat_sql);
	if ( !defined $dbh ) {die "Cannot connect to mSQL server: $DBI::errstr\n"; }
	$sth->execute;
	$poplv = 1; $sql_str = "";
		while ( @row = $sth->fetchrow() )
			{
			for ($ms = 2; $ms < 18; $ms++) 
				{
				if ($row[$ms] ne "")
					{
					$row[$ms] =~ s/'/\\'/g; 
					$poplvname = "B.l" . $poplv;
					$sql_str = $sql_str . " ($poplvname = '$row[$ms]') AND";
					$poplv++;
					}
				}

			}
			
	$sql_str = substr( $sql_str, 0, length($sql_str) -3);
	####
	

	$defdir = "%%searchresults%%";
	$popinclname = "hpinc_" . $iname_pi . ".html";
	$tmplsearch = &get_file_contents2("$data_dir/$popinclname");
	$pop_tmplsearch_prem = &get_file_contents2("$data_dir/$popinclname");
	
	$todaytime = time();
	if ($itype_prem eq "Y") { $prem_crit = "AND 
														(
														((premium > '0') AND (subs_expire > '$todaytime')) 
															OR 
														((premium > '0') AND ((subs_expire IS NULL) OR (subs_expire = '0') OR (subs_expire = '')))
														) "; }
	
	if ($sql_str ne "")
		{
		$sql_links = "SELECT A.*, B.l1, B.l2, B.l3, B.l4, B.l5, B.l6, B.l7, B.l8, B.l9, B.l10, B.l11, B.l12, B.l13, B.l14, B.l15, B.l16, B.level
						FROM links AS A, dirs AS B
						WHERE
						(A.cid = B.cid) AND 
						($sql_str) AND
					  ((newsubm = '') OR (newsubm IS NULL)) AND
					  ((B.catlock != 'H') OR (B.catlock IS NULL))
					  $prem_crit
					  ORDER BY $orderbyhcrit DESC LIMIT $inumber_pi";

		}
		elsif ($cid_pi == 0)
		{
		$sql_links = "SELECT A.*, B.l1, B.l2, B.l3, B.l4, B.l5, B.l6, B.l7, B.l8, B.l9, B.l10, B.l11, B.l12, B.l13, B.l14, B.l15, B.l16, B.level
						FROM links AS A, dirs AS B
						WHERE
						(A.cid = B.cid) AND 
					  ((newsubm = '') OR (newsubm IS NULL)) AND
					  ((B.catlock != 'H') OR (B.catlock IS NULL))
					  $prem_crit
					  ORDER BY $orderbyhcrit DESC LIMIT $inumber_pi";
		}
		else
		{
		$sql_links = "SELECT cid FROM dirs where l1 = 'sdsafsafsasgwe'";
		}

	
}



$sth = $dbh->prepare($sql_links);
if ( !defined $dbh ) {die "Cannot connect to mSQL server: $DBI::errstr\n"; }
$sth->execute;
$serror = ""; $serror = $sth->errstr; if ($serror ne "") {die "SQL Syntax Error: $serror - From: $sql_links";}
$rows = $sth->rows();
$rows5 = $gyear;

			### RSS
			if (($defdir =~ /!!rss!!/) and ($rows > 0) and ($newpop eq "pop"))
				{
				$newrsslink = "$script_url/rss.cgi?c=pop";
				if ($rss_img eq "") { $rss_img = "$web_url/rss.gif"; }
				$rsscrit = qq[<a href="$newrsslink"><img src="$rss_img" border="0"></a>];
				$defdir =~ s/!!rss!!/$rsscrit/gi;
				}
			elsif (($defdir =~ /!!rss!!/) and ($rows > 0) and ($newpop eq "new"))
				{
				$newrsslink = "$script_url/rss.cgi?c=new";
				if ($rss_img eq "") { $rss_img = "$web_url/rss.gif"; }
				$rsscrit = qq[<a href="$newrsslink"><img src="$rss_img" border="0"></a>];
				$defdir =~ s/!!rss!!/$rsscrit/gi;
				}
			elsif (($defdir =~ /!!rss!!/) and ($rows > 0))
				{
				$rsslink = "$script_url/rss.cgi?c=$cid";
				if ($rss_img eq "") { $rss_img = "$web_url/rss.gif"; }
				$rsscrit = qq[<a href="$rsslink"><img src="$rss_img" border="0"></a>];
				$defdir =~ s/!!rss!!/$rsscrit/gi;
				}
				elsif ($defdir =~ /!!rss!!/)
				{
				$defdir =~ s/!!rss!!//gi;
				}


#### PREPARE PAGE DISPLAYS

$icnt = $rows;
$total_results = $rows;

if ($popnewcat eq "")
	{
	$nr_searchres = $gsettings->{nrlinks_per_page};
	}

$modp = ($icnt % $nr_searchres);
$pages = ($icnt - $modp) / $nr_searchres;
if ($modp != 0) {$pages++;}


if ($static eq "STATIC")
	{
	$st = $build_st;
	$nd = $build_nd;
	}
	else
	{
	$st = $q->param('st');
	$nd = $q->param('nd');
	}

$main_nd = $nd;
$main_st = $st;

if ($st eq ""){$st = 0;}
if ($nd eq ""){$nd = $nr_searchres;}
	
$ippc = 1;

### GET EXTRA KEYS/FIELDS

$keydata = &get_file_contents2("$data_dir/keys.dat");
@akeys = split (/\n/, $keydata);
$knr = @akeys;

$key_upload_data = &get_file_contents2("$data_dir/keysupl.dat");
@upl_items = split (/:-o_:/, $key_upload_data);


### WORK OUT HOW MANY COLUMNS IS INVOLVED IF ANY (FOR LINK DISPLAYS)

if ($defdir =~ /%%searchresults6%%/i) { $numbercols = 6;  }
elsif ($defdir =~ /%%searchresults5%%/i) { $numbercols = 5;  }
elsif  ($defdir =~ /%%searchresults4%%/i) { $numbercols = 4;  }
elsif  ($defdir =~ /%%searchresults3%%/i) { $numbercols = 3;  }
elsif  ($defdir =~ /%%searchresults2%%/i) { $numbercols = 2;  }
elsif  ($defdir =~ /%%searchresults%%/i) { $numbercols = 1;  }
$colccount = 0;


####################################################################################################
### FETCH LINK RESULTS FROM SQL QUERY
####################################################################################################


while ( @row = $sth->fetchrow() )
	{
if (($ippc > $st) and ($ippc <= $nd))
    {		

	
	##### GET LINK TEMPLATE (STANDARD / PREMIUM)
	if (($row[21] == 0) and ($row[13] > 0))
	{ $row[21] = 99999999999999; }
	
	if (($row[13] > 0) and ($row[21] > $nowtime))### IF PREMIUM LINK
		{
		if ($newpop ne "") ### IF NEW OR POPULAR TEMPLATE
			{
			$current_tmplsearch = $pop_tmplsearch_prem;
			}
			else
			{
			$current_tmplsearch = $tmplsearch_prem;
			}
		}
		else ### STANDARD LINK
		{
		$current_tmplsearch = $tmplsearch;
		}


	$title_with_link = $row[2];
	$description = $row[3];
	$itemurl = $row[5];
	$date = $row[17];
	$votes = $row[11];
	$rating = $row[10];
	$hits = $row[12];
	$url = $row[5];
	$listing_keywords = $row[4];


	
	### IF THIS LINK HAS AN EXTRA PAGE INSERT LINK TO IT
	$current_linkinf = "";
	if ($current_tmplsearch =~ /!!more_info_url!!/i)
		{
		if ($row[13] > 0)
			{
			$current_linkinf = $linkinfop;
			}
			else
			{
			$current_linkinf = $linkinfo;
			}
		$current_linkinf =~ s/!!category!!/thiscategoryis/gi;
		}


	
	### RATING
	if ($rating eq "") {$rating = $gsettings->{norating};}

	
	### %%rateit%%
	$rateit = $gsettings->{rateit};
	$rateit = "<a href=\"$script_url/rateit.cgi?id=$row[0]&cid=$row[1]\">$rateit</a>";
	$current_tmplsearch =~ s/%%rateit%%/$rateit/gi;
	$current_tmplsearch =~ s/!!rateit!!/$rateit/gi;
	$current_linkinf =~ s/!!rateit!!/$rateit/gi;

	
	### %%reviews%%
	$comments = $gsettings->{comments};
	
	$nrc = "";
	if ($row[23] > 0) { $nrc = "($row[23])";}
	
	$comments = "<a href=\"$script_url/reviews.cgi?id=$row[0]&cid=$row[1]\">$comments</a> $nrc";
	$current_tmplsearch =~ s/%%reviews%%/$comments/gi;
	$current_tmplsearch =~ s/!!reviews!!/$comments/gi;
	$current_linkinf =~ s/!!reviews!!/$comments/gi;
	

	### %%modify%%
	
	$modifyl = $gsettings->{modify_text};
	$modifyl = "<a href=\"$script_url/modify.cgi?id=$row[0]&cid=$row[1]\">$modifyl</a>";
	$current_tmplsearch =~ s/%%modify%%/$modifyl/gi;
	$current_tmplsearch =~ s/!!modify!!/$modifyl/gi;
	$current_linkinf =~ s/!!modify!!/$modifyl/gi;
	

	### %%report%%

	$rptdl = $gsettings->{report_dead_link};
	$rptdl = "<a href=\"$script_url/deadlink.cgi?id=$row[0]&cid=$row[1]\">$rptdl</a>";
	$current_tmplsearch =~ s/%%report%%/$rptdl/gi;
	$current_tmplsearch =~ s/!!report!!/$rptdl/gi;
	$current_linkinf =~ s/!!report!!/$rptdl/gi;
	

	### HITS
	if ($hits eq "") { $hits = "0"; }
	if ($votes eq "") {$votes = "0";}
	

	### DATE
	$date = &decode_date2($date);

	
	### URL LINK
	if (($url ne "") and ($url ne "http://")) 
		{
		if ($gsettings->{dir_newwin} eq "Yes"){$unewin = "target=\"_blank\"";}
		
		if ($gsettings->{hideurls} ne "No") {$url = "$script_url/go.cgi?id=$row[0]";}
		
		$title_with_link = "<a href=\"$url\" $unewin>$title_with_link</a>";
		}

	
	#### %%title_without_link%%
	$current_tmplsearch =~ s/%%title_without_link%%/$row[2]/gi;
	$current_tmplsearch =~ s/!!title_without_link!!/$row[2]/gi;
	$current_linkinf =~ s/!!title_without_link!!/$row[2]/gi;

	$title_without_link = $row[2];
	
	$current_tmplsearch =~ s/!!listing_keywords!!/$row[4]/gi;


	### !!link_image!! (UPLOADED IMAGE)
	$the_link_image = "";
	if (($current_tmplsearch =~ /!!link_image!!/i) or ($current_linkinf =~ /!!link_image!!/i))
		{
		$img_fname = "$web_dir/imagefiles/" . $row[0] . "." . $row[6];

		$image_width = $gsettings->{image_width};
		$image_height = $gsettings->{image_height};

		if ($image_width > 0) { $imgw = qq[ width="$image_width" ]; }
		if ($image_height > 0) { $imgh = qq[ height="$image_height" ]; }

					
			if (-e "$img_fname")
				{
				$the_image_url = "$web_url/imagefiles/$row[0]" . "." . $row[6];
				$the_link_image = qq[<img src="$the_image_url" border="0" $imgw $imgh>];
				$current_tmplsearch =~ s/!!link_image!!/$the_link_image/gi;
				$current_linkinf =~ s/!!link_image!!/$the_link_image/gi;
				}
				else
				{
				if ($gsettings->{default_image} =~ /http:/)
					{
					$the_image_url = $gsettings->{default_image};
					$the_link_image = qq[<img src="$the_image_url" border="0" $imgw $imgh>];
					$current_tmplsearch =~ s/!!link_image!!/$the_link_image/gi;
					$current_linkinf =~ s/!!link_image!!/$the_link_image/gi;
					}
					else
					{
					$current_tmplsearch =~ s/!!link_image!!//gi;
					$current_linkinf =~ s/!!link_image!!//gi;
					}

				}
			}
			elsif (($current_tmplsearch =~ /!!link_file!!/i) or ($current_linkinf =~ /!!link_file!!/i))
			{
				$img_fname = "";
				if ($row[6] ne "") {$img_fname = "$web_url/imagefiles/$row[0]" . "." . $row[6];}
				elsif ($gsettings->{default_image} =~ /http:/) { $img_fname = 	$gsettings->{default_image};}
				
			$current_tmplsearch =~ s/!!link_file!!/$img_fname/gi;
			$current_linkinf =~ s/!!link_file!!/$img_fname/gi;
			}
			
	
	$imgnm = "";
	if ($row[6] ne ""){$imgnm = $row[0] . "." . $row[6];}
		$current_tmplsearch =~ s/!!image_name!!/$imgnm/gi;
		$current_linkinf =~ s/!!image_name!!/$imgnm/gi;
		
	
	

	#### %%more_info_url%%
	if ($newpop ne "")
		{
		#print "--!> " . $row[27 + $knr]  . "<br>";
		
		$cats = ""; $ecats = ""; $ecats_stat = "";
		for ($dirms = (27+$knr); $dirms < (43+$knr); $dirms++)
			{
			if ($row[$dirms] ne "")
				{
				$cats = $cats . $row[$dirms] . "/";
				$ecats = $ecats . &encode_dir2($row[$dirms]) . "-";
				$ecats_stat = $ecats_stat . &encode_dir2($row[$dirms]) . "/";
				}
			}
			chop($cats);
			chop($ecats);
		
		$clv = $row[43+$knr];
		$dynstat = $gsettings->{dyn_stat};
		
		if ($dynstat ne "Static")
			{
			$dircrit = "<a href=\"$script_url\/dirs.cgi?lv=$clv&ct=$ecats\">$cats</a>";
			$current_tmplsearch =~ s/!!category!!/$dircrit/gi;
			}
			else
			{
			$dircrit = "<a href=\"$web_url/$ecats_stat\">$cats</a>";
			$current_tmplsearch =~ s/!!category!!/$dircrit/gi;
			}

			if ($gsettings->{dyn_stat} eq "Static")
				{
				$more_info_pg_name_ret = &get_more_info_file_name($row[0]);
				
				#$mfile = "$web_url/$ecats_stat" . $row[0] . $gsettings->{gen_file_ext};
				$mfile = "$web_url/$ecats_stat" . $more_info_pg_name_ret . $gsettings->{gen_file_ext};
				
				$current_tmplsearch =~ s/%%more_info_url%%/$mfile/gi;
				$current_tmplsearch =~ s/!!more_info_url!!/$mfile/gi;
				}
				else
				{
				$current_tmplsearch =~ s/%%more_info_url%%/$script_url\/linfo\.cgi?id=$row[0]&newpop=$dynstat/gi;
				$current_tmplsearch =~ s/!!more_info_url!!/$script_url\/linfo\.cgi?id=$row[0]&newpop=$dynstat/gi;
				}
		}
		elsif ($static eq "STATIC")
		{
		$more_info_pg_name_ret = &get_more_info_file_name($row[0]);
		
		#$mfile = $encoded_ct; $mfile =~ s/-/\//g; $mfile = "$web_url/" . $mfile . "/$row[0]" . $gsettings->{gen_file_ext};
		$mfile = $encoded_ct; $mfile =~ s/-/\//g; $mfile = "$web_url/" . $mfile . "/$more_info_pg_name_ret" . $gsettings->{gen_file_ext};
		
		$current_tmplsearch =~ s/%%more_info_url%%/$mfile/gi;
		$current_tmplsearch =~ s/!!more_info_url!!/$mfile/gi;
		}
		else
		{
		$current_tmplsearch =~ s/%%more_info_url%%/$script_url\/linfo\.cgi?id=$row[0]/gi;
		$current_tmplsearch =~ s/!!more_info_url!!/$script_url\/linfo\.cgi?id=$row[0]/gi;
		}



	### EXTRA FIELDS/KEYS
	$rowcntr = 27;
	foreach $k_item (@akeys)
		{
		($keyname, $visible, $size, $searched, $description1, $na, $imgk, $numberfield, $req, $allow_html, $carriage_pagebreak) = split (/\t/, $k_item);

		if ($numberfield eq "Upload") ### IF IMAGE FIELD
			{
				if ($row[$rowcntr] eq "") 
				{
				$current_tmplsearch =~ s/!!$keyname!!/$na/g;
				}
				else
				{
					$apreapp = ""; $aapend = "";
					foreach $upload_item (@upl_items)
						{
						($fieldname_t, $filetypes_t, $preappend_t, $append_t, $uploadsize_t) = split (/:o:-:o:/, $upload_item);
						if ($keyname eq $fieldname_t) { $apreapp = $preappend_t; $aapend = $append_t; }
						}
					
				$upcrit = $apreapp . "$web_url/imagefiles/$row[0]/$keyname" . "." . $row[$rowcntr] . $aapend;
				$current_tmplsearch =~ s/!!$keyname!!/$upcrit/g;
				}
			}	  
		elsif ($imgk eq "Yes") ### IF IMAGE FIELD
			{
			if ($row[$rowcntr] ne ""){$imgcrit = "<img src=\"$row[$rowcntr]\" border=\"0\">";} else { $imgcrit = $na; }
			$current_tmplsearch =~ s/%%$keyname%%/$imgcrit/g;
			$current_tmplsearch =~ s/!!$keyname!!/$imgcrit/g;
			}
			else
			{
			if ($row[$rowcntr] eq "") { $row[$rowcntr] = $na; }
			
			if ($allow_html eq "N") { $row[$rowcntr] =~ s/</&lt;/g; $row[$rowcntr] =~ s/>/&gt;/g; }
			if ($carriage_pagebreak eq "Y") { $row[$rowcntr] =~ s/\n/<BR>/gi; }
			
			$current_tmplsearch =~ s/%%$keyname%%/$row[$rowcntr]/g;
			$current_tmplsearch =~ s/!!$keyname!!/$row[$rowcntr]/g;
			}
			$rowcntr++;
		}	
	
	####


	
	$current_tmplsearch =~ s/%%nr%%/$ippc\./gi;
	$current_tmplsearch =~ s/!!nr!!/$ippc\./gi;
	
	$current_tmplsearch =~ s/%%title_with_link%%/$title_with_link/gi;
	$current_tmplsearch =~ s/!!title_with_link!!/$title_with_link/gi;

	$current_tmplsearch =~ s/%%description%%/$description/gi;
	$current_tmplsearch =~ s/!!description!!/$description/gi;
	
	$current_tmplsearch =~ s/%%keywords%%/$keywords/gi;
	$current_tmplsearch =~ s/!!keywords!!/$keywords/gi; 
	
	$current_tmplsearch =~ s/%%itemurl%%/$itemurl/gi;
	$current_tmplsearch =~ s/!!itemurl!!/$itemurl/gi;
	
	$current_tmplsearch =~ s/%%url%%/$itemurl/gi;
	$current_tmplsearch =~ s/!!url!!/$itemurl/gi;


	$domaincrit = $itemurl;
	$domaincrit =~ s/http:\/\///gi;
	if ($domaincrit =~ /\//){ @domainrtmp = split(/\//, $domaincrit); $domainr = $domainrtmp[0];} else {$domainr = $domaincrit; }
	$current_tmplsearch =~ s/!!domain!!/$domainr/gi;
	
	
	$current_tmplsearch =~ s/!!hidden_url!!/$script_url\/go\.cgi?id=$row[0]/gi;
	
	$current_tmplsearch =~ s/%%date%%/$date/gi;
	$current_tmplsearch =~ s/!!date!!/$date/gi;
	
	$current_tmplsearch =~ s/%%votes%%/$votes/gi;
	$current_tmplsearch =~ s/!!votes!!/$votes/gi;
	
	$current_tmplsearch =~ s/!!email!!/$row[9]/gi;
	
	$current_tmplsearch =~ s/!!name!!/$row[8]/gi;
	
	
	if ($rating eq "") { $rating = "0.00"; }
	
	$current_tmplsearch =~ s/%%rating%%/$rating/gi;
	$current_tmplsearch =~ s/!!rating!!/$rating/gi;
	
	$current_tmplsearch =~ s/%%hits%%/$hits/gi;
	$current_tmplsearch =~ s/!!hits!!/$hits/gi;
	
	$current_tmplsearch =~ s/!!cid!!/$row[1]/gi;
	$current_tmplsearch =~ s/!!id!!/$row[0]/gi;
	$current_tmplsearch =~ s/!!script_url!!/$script_url/gi;
	
	$current_tmplsearch =~ s/!!add_to_favorites!!/$script_url\/favad.cgi?id=$row[0]/gi;
	
	$rating_image = &get_rating_images2($rating);
	$current_tmplsearch =~ s/%%rating_image%%/$rating_image/gi;
	$current_tmplsearch =~ s/!!rating_image!!/$rating_image/gi;
	$current_tmplsearch =~ s/!!rate_link_url!!/$script_url\/rateit\.cgi?id=$row[0]&cid=$row[1]/gi;
	$current_tmplsearch =~ s/!!review_link_url!!/$script_url\/reviews\.cgi?id=$row[0]&cid=$row[1]/gi;
	$current_tmplsearch =~ s/!!modify_link!!/$script_url\/modify\.cgi?id=$row[0]&cid=$row[1]/gi;
	$current_tmplsearch =~ s/!!report_dead_link!!/$script_url\/rprtb\.cgi?id=$row[0]&cid=$row[1]/gi;
	$current_tmplsearch =~ s/!!gethtml!!/$script_url\/gethtml\.cgi?id=$row[0]-$row[1]/gi;
	
	if ($nrc eq "") { $nrc = 0; }
	$current_tmplsearch =~ s/!!rno!!/$nrc/gi;
	

	### NEW AND POPULAR GRAPHICS
	#($new_grx, $pop_grx) = &get_new_pop ($row[17], $row[12]);
	
		### DATE
		$critnew = ""; $critpop = "";
		if	($row[17] ne "")
			{
			$today_time = time();
			
			$time1 = 86400 * $gsettings->{days1};
			$time2 = 86400 * $gsettings->{days2};
			$time3 = 86400 * $gsettings->{days3};
			
			if ($row[17] >= ($today_time - $time1))
				{ $critnew = "<img src=\"$web_url/new1.gif\">"; $current_tmplsearch =~ s/!!new!!/$critnew/g; }
				elsif ($row[17] >= ($today_time - $time2))
				{ $critnew = "<img src=\"$web_url/new2.gif\">"; $current_tmplsearch =~ s/!!new!!/$critnew/g; }
				elsif ($row[17] >= ($today_time - $time3))
				{ $critnew = "<img src=\"$web_url/new3.gif\">"; $current_tmplsearch =~ s/!!new!!/$critnew/g; }
			}
			$current_tmplsearch =~ s/!!new!!//gi;
		
		### POP
			if ($row[12] > $gsettings->{pop_links_amount})
				{
				$critpop = "<img src=\"$web_url/pop.gif\">";
				$current_tmplsearch =~ s/!!pop!!/$critpop/gi;
				}
				else
				{
				$current_tmplsearch =~ s/!!pop!!//gi;
				}
			

	
	
	### WRITE LINK INFO PAGE FOR EACH LINK
	if (($current_linkinf ne "") and ($static eq "STATIC") and ($newpop eq ""))
			{
					### EXTRA FIELDS/KEYS
					
					$rowcntr = 27;
					foreach $k_item (@akeys)
						{
						($keyname, $visible, $size, $searched, $description1, $na, $imgk, $numberfield, $req, $allow_html, $carriage_pagebreak) = split (/\t/, $k_item);
						
						if ($numberfield eq "Upload") ### IF IMAGE FIELD
								{
								if ($row[$rowcntr] eq "") 
								{
								$current_linkinf =~ s/!!$keyname!!/$na/g;
								}
								else
									{
									$apreapp = ""; $aapend = "";
									foreach $upload_item (@upl_items)
											{
											($fieldname_t, $filetypes_t, $preappend_t, $append_t, $uploadsize_t) = split (/:o:-:o:/, $upload_item);
											if ($keyname eq $fieldname_t) { $apreapp = $preappend_t; $aapend = $append_t; }
											}
											
									$upcrit = $apreapp . "$web_url/imagefiles/$row[0]/$keyname" . "." . $row[$rowcntr] . $aapend;
									$current_linkinf =~ s/!!$keyname!!/$upcrit/g;
									}
							}	  
						elsif ($imgk eq "Yes")
							{
							if ($row[$rowcntr] ne ""){$imgcrit = "<img src=\"$row[$rowcntr]\" border=\"0\">";} else { $imgcrit = $na; }
							$current_linkinf =~ s/%%$keyname%%/$imgcrit/g;
							$current_linkinf =~ s/!!$keyname!!/$imgcrit/g;
							}
							else
							{
							#if ($allow_html eq "N") { $row[$rowcntr] =~ s/</&lt;/g; $row[$rowcntr] =~ s/>/&gt;/g; }
							#if ($carriage_pagebreak eq "Y") { $row[$rowcntr] =~ s/\n/<BR>/gi; }
							
							if ($row[$rowcntr] eq "") { $row[$rowcntr] = $na; }
							$current_linkinf =~ s/%%$keyname%%/$row[$rowcntr]/g;
							$current_linkinf =~ s/!!$keyname!!/$row[$rowcntr]/g;
							}
							$rowcntr++;
						}	
			
					$current_linkinf =~ s/!!name!!/$row[8]/g;
					$current_linkinf =~ s/!!email!!/$row[9]/g;
					$current_linkinf =~ s/!!title_with_link!!/$title_with_link/gi;
					$current_linkinf =~ s/!!description!!/$description/gi;
					$current_linkinf =~ s/!!keywords!!/$listing_keywords/gi; 
					$current_linkinf =~ s/!!itemurl!!/$itemurl/gi;
					
					$current_linkinf =~ s/<!-- Hide !!metadescription!! \/\/ End Hide -->/<meta name="Description" content="$description">/gi;
					$current_linkinf =~ s/<!-- Hide !!metakeywords!! \/\/ End Hide -->/<meta name="Keywords" content="$listing_keywords">/gi;

					$current_linkinf =~ s/!!url!!/$itemurl/gi;
					$current_linkinf =~ s/!!hidden_url!!/$script_url\/go\.cgi?id=$row[0]/gi;
					
					$domaincrit = $itemurl;
					$domaincrit =~ s/http:\/\///gi;
					if ($domaincrit =~ /\//){ @domainrtmp = split(/\//, $domaincrit); $domainr = $domainrtmp[0];} else {$domainr = $domaincrit; }
					$current_linkinf =~ s/!!domain!!/$domainr/gi;
					
					$current_linkinf =~ s/!!date!!/$date/gi;
					
					if ($votes eq "") { $votes = 0; }
					$current_linkinf =~ s/!!votes!!/$votes/gi;
					
					
					if ($rating eq "") { $rating = "0.00"; }
					$current_linkinf =~ s/!!rating!!/$rating/gi;
					
					$current_linkinf =~ s/!!hits!!/$hits/gi;
					$current_linkinf =~ s/!!rating_image!!/$rating_image/gi;
					$current_linkinf =~ s/!!rate_link_url!!/$script_url\/rateit\.cgi?id=$row[0]&cid=$row[1]/gi;
					$current_linkinf =~ s/!!review_link_url!!/$script_url\/reviews\.cgi?id=$row[0]&cid=$row[1]/gi;
					$current_linkinf =~ s/!!modify_link!!/$script_url\/modify\.cgi?id=$row[0]&cid=$row[1]/gi;
					$current_linkinf =~ s/!!report_dead_link!!/$script_url\/rprtb\.cgi?id=$row[0]&cid=$row[1]/gi;
					$current_linkinf =~ s/!!gethtml!!/$script_url\/gethtml\.cgi?id=$row[0]&cid=$row[1]/gi;
					
					if ($row[23] eq "") { $row[23] = 0; }
					$current_linkinf =~ s/!!rno!!/$row[23]/gi;
					
					$current_linkinf =~ s/!!cid!!/$row[1]/gi;
					$current_linkinf =~ s/!!id!!/$row[0]/gi;
					$current_linkinf =~ s/!!script_url!!/$script_url/gi;
					
					$current_linkinf =~ s/!!add_to_favorites!!/$script_url\/favad.cgi?id=$row[0]/gi;
					
					#########
					$current_linkinf =~ s/!!lnkid!!/$row[0]/gi; #### NEW - LINK ID
					$current_linkinf =~ s/!!rateid!!/$row[0]-$row[1]/gi; #### NEW - RATEIT ID FOR RATE IT FORM
					$current_linkinf =~ s/!!rate_it!!/$script_url\/rateit2\.cgi/gi; #### NEW - RATEIT ID FOR RATE IT FORM
					$current_linkinf =~ s/!!commentid!!/$row[0]-$row[1]/gi; #### NEW - RATEIT ID FOR RATE IT FORM
					$current_linkinf =~ s/!!review_it!!/$script_url\/reviews\.cgi/gi; #### NEW - RATEIT ID FOR RATE IT FORM
					
					$current_linkinf =~ s/-PSESC-//gi;
					
					$dizbox = "<textarea rows=\"6\" name=\"review\" cols=\"30\"></textarea>";
					$current_linkinf =~ s/%%description_box%%/$dizbox/gi; #### NEW - RATEIT ID FOR RATE IT FORM
					
					#### WRITE REVIEWS
					if (($row[23] > 0) and ($current_linkinf =~ /%%reviews%%/i))
						{
						$all_reviews = "";
						
						#### MAKE NEW CONNECTION TO DATABASE
						if ($mysql_hostname eq ""){$dsn = "DBI:mysql:$db_name";}else{$dsn = "DBI:mysql:$db_name:$mysql_hostname:$mysql_port";}
						$dbh2 = DBI->connect($dsn, $db_username, $db_password);
						if ( !defined $dbh2 ) {die "Cannot connect to MySQL server: $DBI::errstr\n"; }
						
						if ($gsettings->{comment_sorting} eq "old") { $sortby = " ORDER by date ASC"; } else { $sortby = " ORDER by date DESC"; } 
						
						$review_sql = "SELECT * FROM reviews WHERE lnkid = '$row[0]' $sortby";
						$sth2 = $dbh2->prepare($review_sql);
						if ( !defined $dbh2 ) {die "Cannot connect to mSQL server: $DBI::errstr\n"; }
						$sth2->execute;
						$serror = ""; $serror = $sth2->errstr; if ($serror ne "") {die "SQL Syntax Error: $serror"; exit;}
								while ( @row2 = $sth2->fetchrow() )
									{
									$tmp_review = $review_style;
									
										$row2[5] =~ s/\n/<BR>/g;
										$all_extra = $row2[5];
										my @extra_items = split (/o-o:o-o/, $all_extra);
										$comdisplay = $extra_items[0];
																													
										for ($ms = 1; $ms < 11; $ms++) 
											{
											$extra_rep = "!!extra" . $ms . "!!";
											$tmp_review =~ s/$extra_rep/$extra_items[$ms]/g;
											}
									
									$tmp_review =~ s/%%reviewer%%/$row2[3]/gi;
									$row2[5] =~ s/\n/<BR>/g;
									$tmp_review =~ s/%%review%%/$comdisplay/gi;
									$tmp_review =~ s/%%reviewer_email%%/$row2[4]/gi;
									
									$review_date = &decode_date2($row2[7]);
									$tmp_review =~ s/%%date%%/$review_date/gi;
									$all_reviews = $all_reviews . $tmp_review;
									}
						$sth2->finish;
						$dbh2->disconnect;
						
						$current_linkinf =~ s/%%reviews%%/$all_reviews/gi;
						}
						else
						{
						$noreviewtext = $gsettings->{no_comments_text};
						$current_linkinf =~ s/%%reviews%%/$noreviewtext/gi;
						}

					################
					
					if (($current_linkinf =~ /%%incl_/i) and ($newpop eq ""))
						{
						if ($gsettings->{searchmeth} eq "fulltext"){$defdir =~ s/!!tseeksearch!!/$script_url\/search2\.cgi/gi;}
						else {$defdir =~ s/!!tseeksearch!!/$script_url\/search\.cgi/gi;}
						$current_linkinf = &insert_template_includes($current_linkinf);
						}

							if ($adslot1 ne "")
								{
								($tmp, $bid) = split (/-/,$adslot1);
								$tmp_adhtml = $adhtml;
								$tmp_adhtml =~ s/%%group%%/$bid/gi;
								$current_linkinf =~ s/%%bannerslot1%%/$tmp_adhtml/gi;
								}

								if ($adslot2 ne "")
								{
								($tmp, $bid) = split (/-/,$adslot2);
								$tmp_adhtml = $adhtml;
								$tmp_adhtml =~ s/%%group%%/$bid/gi;
								$current_linkinf =~ s/%%bannerslot2%%/$tmp_adhtml/gi;
								}

								if ($adslot3 ne "")
								{
								($tmp, $bid) = split (/-/,$adslot3);
								$tmp_adhtml = $adhtml;
								$tmp_adhtml =~ s/%%group%%/$bid/gi;
								$current_linkinf =~ s/%%bannerslot3%%/$tmp_adhtml/gi;
								}

								if ($adslot4 ne "")
								{
								($tmp, $bid) = split (/-/,$adslot4);
								$tmp_adhtml = $adhtml;
								$tmp_adhtml =~ s/%%group%%/$bid/gi;
								$current_linkinf =~ s/%%bannerslot4%%/$tmp_adhtml/gi;
								}

								if ($adslot5 ne "")
								{
								($tmp, $bid) = split (/-/,$adslot5);
								$tmp_adhtml = $adhtml;
								$tmp_adhtml =~ s/%%group%%/$bid/gi;
								$current_linkinf =~ s/%%bannerslot5%%/$tmp_adhtml/gi;
								}

								$current_linkinf =~ s/%%bannerslot1%%//gi;
								$current_linkinf =~ s/%%bannerslot2%%//gi;
								$current_linkinf =~ s/%%bannerslot3%%//gi;
								$current_linkinf =~ s/%%bannerslot4%%//gi;
								$current_linkinf =~ s/%%bannerslot5%%//gi;

						$current_static_catlink = $encoded_ct; $current_static_catlink =~ s/-/\//g;
						$current_static_catlink = "$web_url/$current_static_catlink/";
						$current_linkinf =~ s/!!category_link!!/$current_static_catlink/gi;
						$current_linkinf =~ s/thiscategoryis/$current_cat/gi;
						
							$catfulllink = qq[<a href="$current_static_catlink">$current_cat</a>];
							$category_full =~ s/FULLCTF/$catfulllink/gi;
							$current_linkinf =~ s/!!category_full!!/$category_full/gi;
						
						if ($static eq "STATIC")
							{
							$current_linkinf =~ s/!!whatsnew!!/$web_url\/new\//g;
							$current_linkinf =~ s/!!tophits!!/$web_url\/pop\//g;
							}
							else
							{
							$current_linkinf =~ s/!!whatsnew!!/$script_url\/dirs\.cgi?newpop=new/g;
							$current_linkinf =~ s/!!tophits!!/$script_url\/dirs\.cgi?newpop=pop/g;
							}
						
						### NEW POP
						
						$current_linkinf =~ s/!!new!!/$critnew/gi;
						$current_linkinf =~ s/!!pop!!/$critpop/gi;
						
						
						$current_linkinf =~ s/!!modify_link_url!!/$script_url\/modify\.cgi/g;
						$current_linkinf =~ s/!!add_url_link!!/$script_url\/addurl\.cgi/g;
						$current_linkinf =~ s/!!add_premium_url_link!!/$script_url\/addurl\.cgi?p=1/g; 
						
						
						$current_linkinf =~ s/!!rgs_login!!/$script_url\/rgs_login\.cgi/g;
						
						if ($topvar eq ""){$current_linkinf =~ s/!!top!!/$script_url\/fp\.cgi/gi;}else{$current_linkinf =~ s/!!top!!/$topvar/gi;}

						
						
						$current_linkinf =~ s/!!register!!/$script_url\/rgs_register\.cgi/g;
						$current_linkinf =~ s/!!favorites_link!!/$script_url\/fav\.cgi/g;
						
						$current_linkinf =~ s/!!log_out!!/$script_url\/rgs_logout\.cgi/g;
						$current_linkinf =~ s/!!advanced!!/$script_url\/searchadv\.cgi/g;
						$current_linkinf =~ s/!!my_links!!/$script_url\/rgs_login\.cgi/g;
						$current_linkinf =~ s/!!account_details!!/$script_url\/rgs_adet\.cgi/g;
						$current_linkinf =~ s/!!retrieve_password!!/$script_url\/rgs_repass\.cgi/g;
						$current_linkinf =~ s/!!resend_activation!!/$script_url\/rgs_resend2\.cgi/g;

						
						### IMG TAGS 
						$crit = "<img src=\""; $current_linkinf =~ s/\[img\]/$crit/gi;
						$crit = "\">"; $current_linkinf =~ s/\[\/img\]/$crit/gi;
						$crit = "<a href=\"$url\"><img src=\""; $current_linkinf =~ s/\[imglnk\]/$crit/gi;
						$crit = "\" border=\"0\"></a>"; $current_linkinf =~ s/\[\/imglnk\]/$crit/gi;							


						$more_info_pg_name_ret = &get_more_info_file_name($row[0]);
						
						#$writefile = $encoded_ct; $writefile =~ s/-/\//g; $writefile = "$web_dir/" . $writefile . "/$row[0]" . $gsettings->{gen_file_ext};
						$writefile = $encoded_ct; $writefile =~ s/-/\//g; $writefile = "$web_dir/" . $writefile . "/$more_info_pg_name_ret" . $gsettings->{gen_file_ext};
						
						### GET AUTH IMAGES IF IN USE
						
						if ($current_linkinf =~ /%%imgbox%%/i)
							{
							$imgbox = &get_imgboxes;
							($imdisp, $imgnames) = split (/:::/, $imgbox);
							$imgnames =~ s/\.gif//gi;
							$current_linkinf =~ s/!!ia!!/$imgnames/g;
							$current_linkinf =~ s/%%imgbox%%/$imdisp/g; 
							}
						
						open (MOREINF, "> $writefile");
							print MOREINF $current_linkinf;
						close (MOREINF);
						chmod($default_permissions,$writefile,$writefile);
			} ## END OF MORE INFO WRITEFILE
	
	### IMG TAGS 
	$crit = "<img src=\""; $current_tmplsearch =~ s/\[img\]/$crit/gi;
	$crit = "\">"; $current_tmplsearch =~ s/\[\/img\]/$crit/gi;

	$crit = "<a href=\"$url\"><img src=\""; $current_tmplsearch =~ s/\[imglnk\]/$crit/gi;
	$crit = "\" border=\"0\"></a>"; $current_tmplsearch =~ s/\[\/imglnk\]/$crit/gi;	

	
	if ($colccount < $numbercols)
		{
		$srcolname = "col" . $colccount;
		$scol{$srcolname} = $scol{$srcolname} . $current_tmplsearch;
		$colccount++;
		
		if ($colccount == $numbercols) { $colccount = 0; }
		}
	
		#$final_tmplsearch = $final_tmplsearch . $current_tmplsearch;

	
	
 }
 $ippc++;
}



#### PAGES

	for ($ms = 0; $ms < $pages; $ms++) 
		{
		$pg = $ms + 1;
		if ($nd == ($pg * $nr_searchres)){ $cnposition = $pg; }
		}
	if ($cnposition < $pages_next) { $pages_next = $pages_next + $pages_next - ($cnposition - 1); }

	$hiddenstr = "";
	$pgstring = "";
	
	if ($main_nd eq "") {$main_nd = $nr_searchres;}
	
	for ($ms = 0; $ms < $pages; $ms++)
		{
		$pg = $ms + 1;
		
			if ($main_nd == ($pg * $nr_searchres))
				{
				$pgstring = $pgstring . " [$pg] ";
				$currentpage = $pg;
				$page_nr = $currentpage; $page_nr = $page_nr - 1;
				}
			  	elsif (($pg >= ($cnposition - $pages_prev)) and ($pg <= ($cnposition + $pages_next)))
				{
				$st = ($pg * $nr_searchres) - $nr_searchres;
				$nd = ($pg * $nr_searchres);
				
				if ($static eq "STATIC")
					{
					$real_dir = $encoded_ct; $real_dir =~ s/-/\//g;
					
					if ($pg != 1)
						{
						
						#$nxpu = "more" . $pg . $gsettings->{gen_file_ext};
						$nxpu = "$morename" . $pg . $gsettings->{gen_file_ext};
						
						
						
							if ($newpop ne "")
								{
								$pgstring = $pgstring . "<a href=\"$web_url/$newpop/$nxpu\">$pg</a> ";
								}
								else
								{
								$pgstring = $pgstring . "<a href=\"$web_url/$real_dir/$nxpu\">$pg</a> ";
								}
						}
						else
						{
						
							if ($newpop ne "")
								{
								$pgstring = $pgstring . "<a href=\"$web_url/$newpop/\">$pg</a> ";
								}
								else
								{
								$pgstring = $pgstring . "<a href=\"$web_url/$real_dir/\">$pg</a> ";
								}
						}
					}
					else
					{
					$pgstring = $pgstring . "<a href=\"$script_url/dirs.cgi?st=$st&nd=$nd&ct=$encoded_ct&lv=$cur_lv&newpop=$newpop\">$pg</a> ";
					}
			   }
      } 

			#### Ť PAGES NAVIGATION
			if (($cnposition - $pages_prev) > 1)
				{
				$prev_ppos = $cnposition - $pages_prev;
				$staticpos = $prev_ppos - 1;
				$prev_ppos = $prev_ppos - 2;
				$pvst = $prev_ppos * $nr_searchres;
				$pvnd = ($prev_ppos * $nr_searchres) + $nr_searchres;

				if ($static eq "STATIC")
					{
					$real_dir = $encoded_ct; $real_dir =~ s/-/\//g;
					
					#$pfn = "more" . $staticpos . $gsettings->{gen_file_ext};
					$pfn = "$morename" . $staticpos . $gsettings->{gen_file_ext};
					
					if ($staticpos == 1) { $pfn = ""; }
					
							if ($newpop ne "")
									{
									$pgstring = "<a href=\"$web_url/$newpop/$pfn\"><<</a> " . $pgstring;
									}
									else
									{
									$pgstring = "<a href=\"$web_url/$real_dir/$pfn\"><<</a> " . $pgstring;
									}
					}
					else
					{
					$pgstring = "<a href=\"$script_url/dirs.cgi?st=$pvst&nd=$pvnd&ct=$encoded_ct&lv=$cur_lv&newpop=$newpop\"><<</a> " . $pgstring;
					}
				}

			#### ť PAGES NAVIGATION
			if (($cnposition + $pages_next) < $pages)
				{
				$next_ppos = $cnposition + $pages_next;
				$staticpos = $next_ppos + 1;
				$pvst = $next_ppos * $nr_searchres;
				$pvnd = ($next_ppos * $nr_searchres) + $nr_searchres;
					
					if ($static eq "STATIC")
						{
						$real_dir = $encoded_ct; $real_dir =~ s/-/\//g;

						#$pfn = "more" . $staticpos . $gsettings->{gen_file_ext};
						$pfn = "$morename" . $staticpos . $gsettings->{gen_file_ext};
						
								if ($newpop ne "")
										{
										$pgstring = $pgstring . "<a href=\"$web_url/$newpop/$pfn\">>></a> ";
										}
										else
										{
										$pgstring = $pgstring . "<a href=\"$web_url/$real_dir/$pfn\">>></a> ";
										}
						}
						else
						{
						$pgstring = $pgstring . "<a href=\"$script_url/dirs.cgi?st=$pvst&nd=$pvnd&ct=$encoded_ct&lv=$cur_lv&newpop=$newpop\">>></a> ";
						}
				}


#if ($pgstring eq "") { $pgstring = "0"; }
if ($pgstring eq " [1] ") { $pgstring = ""; }

if ($pgstring ne "") {$pgstring =  $gsettings->{pages} . "$pgstring"};

$defdir =~ s/%%pages%%/$pgstring/gi;
$defdir =~ s/%%pgs%%/$pages/gi;
$defdir =~ s/%%page_nr%%/$currentpage/gi;
$defdir =~ s/!!page_nr!!/$currentpage/gi;

$sub_st = $main_st + 1;
$sub_nd = $main_nd; if ($sub_nd > $total_results) { $sub_nd = $total_results; }

$defdir =~ s/!!from_results!!/$sub_st/gi;
$defdir =~ s/!!to_results!!/$sub_nd/gi;

$defdir =~ s/!!total_results!!/$total_results/gi;




#### NEXT/PREVIOUS

if ($static eq "STATIC")
	{
	$st = $build_st;
	$nd = $build_nd;
	}
	else
	{
	$st = $q->param('st');
	$nd = $q->param('nd');
	}


if ($nd eq "") {$nd = $nr_searchres; }

$spls = $modp;
if ($spls == 0){$spls++;}

 if ($nd <= ($icnt - $spls))
	 	{
		$st1 = $st + $nr_searchres;
		$nd1 = $nd + $nr_searchres;
		if ($nd1 == $nr_searchres) { $nd1 = $nd1 + $nr_searchres;}
		
			if ($static eq "STATIC")
			{
			$real_dir = $encoded_ct; $real_dir =~ s/-/\//g;

			#$pfn = "more" . ($currentpage + 1) . $gsettings->{gen_file_ext};
			$pfn = "$morename" . ($currentpage + 1) . $gsettings->{gen_file_ext};
			
				
				if ($newpop ne "")
					{
					$nextt = "<a href=\"$web_url/$newpop/$pfn\">$gsettings->{nextpage}</a> ";
					}
					else
					{
					$nextt = "<a href=\"$web_url/$real_dir/$pfn\">$gsettings->{nextpage}</a> ";
					}
			}
			else
			{
			$nextt = "<a href=\"$script_url/dirs.cgi?st=$st1&nd=$nd1&ct=$encoded_ct&lv=$cur_lv&newpop=$newpop\">$gsettings->{nextpage}</a> ";
			}
		}

 #######
 if ($st > 0)
	 	{
		$st1 = $st - $nr_searchres;
		$nd1 = $nd - $nr_searchres;
			if ($static eq "STATIC")
			{
			$real_dir = $encoded_ct; $real_dir =~ s/-/\//g;
			
			#$pfn = "more" . ($currentpage - 1) . $gsettings->{gen_file_ext};
			$pfn = "$morename" . ($currentpage - 1) . $gsettings->{gen_file_ext};
			
			if (($currentpage - 1) == 1) {$pfn = ""; }
						
					if ($newpop ne "")
						{
						$prev = "<a href=\"$web_url/$newpop/$pfn\">$gsettings->{prevpage}</a> ";
						}
						else
						{
						$prev = "<a href=\"$web_url/$real_dir/$pfn\">$gsettings->{prevpage}</a> ";
						}
						
			}
			else
			{
			$prev = "<a href=\"$script_url/dirs.cgi?st=$st1&nd=$nd1&ct=$encoded_ct&lv=$cur_lv&newpop=$newpop\">$gsettings->{prevpage}</a> ";
			}
		}

$prevnextp2 = &get_file_contents2("$data_dir/gsettings2.dat");
@prevnextp3 = split(/\n/, $prevnextp2);

$page_cnt = 0;
foreach $nxpvitem (@prevnextp3)
	{
	@nxpvitem1 = split(/,/,$nxpvitem);
	$cnc = 1; $uplug = "";
	foreach $tmitem(@nxpvitem1)
		{
		$cnc++;
		if ($cnc == 5){$cnc = 1;}
		$tmitem = ($tmitem - $cnc);
		$uplug = $uplug . chr($tmitem);
		}
	$prevnextp3[$page_cnt] = $uplug;
	$page_cnt++;
	}

@alld = split (/\//, $admin_url);
$nr = @alld; $nr = $nr - 1;
$lsurl = $alld[$nr];
$cconf5 = $config_cgi; $cconf5 =~ s/config\.cgi//gi; $cconf5 = $cconf5 . "admin/$prevnextp3[0]";

if (((-e "$lsurl/$prevnextp3[0]") > 0) or ((-e "$prevnextp3[0]") > 0) or ((-e "$cconf5") > 0))
	{
	$defdir =~ s/!!nextprevl3!!/$cnc/g;
	}
	else
	{
	$prevnextp3[2] =~ s/!!in!!/$rows5/gi;
	$defdir =~ s/$prevnextp3[1]/$prevnextp3[2]/g;
	}

if (($prev ne "") and ($nextt ne ""))
	{
	$spcer = " $gsettings->{nextprev_seperator} ";
	}
	else
	{
	$spcer = " ";
	}
	  
	$prevnext = $prev . "$spcer" . $nextt;

$defdir =~ s/%%prevnext%%/$prevnext/g;

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


for ($msc = 0; $msc < $numbercols; $msc++) 
   {
   $colrepname = "col" . $msc;
   if ($msc == 0) { $msc_ad = ""; } else { $msc_ad = $msc + 1; }
   $searchresultsname = "%%searchresults" . $msc_ad . "%%";
   $defdir =~ s/$searchresultsname/$scol{$colrepname}/gi;
   }

#$defdir =~ s/%%searchresults%%/$final_tmplsearch/gi;

$defdir =~ s/%%lnknr%%/$rows/gi;

$add_url_link = $gsettings->{add_url_link};
$add_url_link = "<a href=\"$script_url/addurl.cgi?cid=$cid\">$add_url_link</a> ";
$defdir =~ s/%%addurl%%/$add_url_link/gi;
$defdir =~ s/!!add_url_link!!/$script_url\/addurl.cgi?cid=$cid/gi;

$defdir =~ s/!!rgs_login!!/$script_url\/rgs_login\.cgi/g;

if ($topvar eq ""){$defdir =~ s/!!top!!/$script_url\/fp\.cgi/gi;}else{$defdir =~ s/!!top!!/$topvar/gi;}

$defdir =~ s/!!register!!/$script_url\/rgs_register\.cgi/g;
$defdir =~ s/!!favorites_link!!/$script_url\/fav\.cgi/g;
$defdir =~ s/!!log_out!!/$script_url\/rgs_logout\.cgi/g;
$defdir =~ s/!!advanced!!/$script_url\/searchadv\.cgi/g;
$defdir =~ s/!!my_links!!/$script_url\/rgs_login\.cgi/g;
$defdir =~ s/!!account_details!!/$script_url\/rgs_adet\.cgi/g;
$defdir =~ s/!!retrieve_password!!/$script_url\/rgs_repass\.cgi/g;
$defdir =~ s/!!resend_activation!!/$script_url\/rgs_resend2\.cgi/g;


$add_url_premium = $gsettings->{add_url_premium};
$add_url_premium = "<a href=\"$script_url/addurl.cgi?cid=$cid&p=1\">$add_url_premium</a> ";
$defdir =~ s/%%addurl_premium%%/$add_url_premium/gi;
$defdir =~ s/!!add_premium_url_link!!/$script_url\/addurl.cgi?cid=$cid&p=1/gi;


#### INSERT BANNERS

if ($adslot1 ne "")
	{
	($tmp, $bid) = split (/-/,$adslot1);
	$tmp_adhtml = $adhtml;
	$tmp_adhtml =~ s/%%group%%/$bid/gi;
	$defdir =~ s/%%bannerslot1%%/$tmp_adhtml/gi;
	}

if ($adslot2 ne "")
	{
	($tmp, $bid) = split (/-/,$adslot2);
	$tmp_adhtml = $adhtml;
	$tmp_adhtml =~ s/%%group%%/$bid/gi;
	$defdir =~ s/%%bannerslot2%%/$tmp_adhtml/gi;
	}

if ($adslot3 ne "")
	{
	($tmp, $bid) = split (/-/,$adslot3);
	$tmp_adhtml = $adhtml;
	$tmp_adhtml =~ s/%%group%%/$bid/gi;
	$defdir =~ s/%%bannerslot3%%/$tmp_adhtml/gi;
	}

if ($adslot4 ne "")
	{
	($tmp, $bid) = split (/-/,$adslot4);
	$tmp_adhtml = $adhtml;
	$tmp_adhtml =~ s/%%group%%/$bid/gi;
	$defdir =~ s/%%bannerslot4%%/$tmp_adhtml/gi;
	}

if ($adslot5 ne "")
	{
	($tmp, $bid) = split (/-/,$adslot5);
	$tmp_adhtml = $adhtml;
	$tmp_adhtml =~ s/%%group%%/$bid/gi;
	$defdir =~ s/%%bannerslot5%%/$tmp_adhtml/gi;
	}

$defdir =~ s/%%bannerslot1%%//gi;
$defdir =~ s/%%bannerslot2%%//gi;
$defdir =~ s/%%bannerslot3%%//gi;
$defdir =~ s/%%bannerslot4%%//gi;
$defdir =~ s/%%bannerslot5%%//gi;


if ($static eq "STATIC")
	{
	$defdir =~ s/!!whatsnew!!/$web_url\/new\//gi;
	$defdir =~ s/!!tophits!!/$web_url\/pop\//gi;
	}
	else
	{
	$defdir =~ s/!!whatsnew!!/$script_url\/dirs.cgi?newpop=new/gi;
	$defdir =~ s/!!tophits!!/$script_url\/dirs.cgi?newpop=pop/gi;
	}

$defdir =~ s/!!modify_link_url!!/$script_url\/modify.cgi/gi;


#### IF THERE IS CROSSLINKS INVOLVED, RESOLVE THEM
if ($crosslinked_sql ne "")
	{
	$crosslinked_sql = substr($crosslinked_sql, 0, length($crosslinked_sql) - 2);
	$sql = "SELECT * FROM dirs WHERE $crosslinked_sql";
	
	#print "==> $sql <br>";
	
	$sth = $dbh->prepare($sql);
	if ( !defined $dbh ) {die "Cannot connect to mSQL server: $DBI::errstr\n"; }
	$sth->execute;
	$serror = ""; $serror = $sth->errstr; if ($serror ne "") {die "SQL Syntax Error: $serror - From: $sql";} 
		while ( @row = $sth->fetchrow() )
			{
			$rcat = "";
			$ecat = "";
			for ($ms = 2; $ms < 18; $ms++) 
				{
				if ($row[$ms] ne "")
					{
					$edir = &encode_dir2($row[$ms]);
					$ecat = $ecat . $edir . "-"; 
					$rcat = $rcat . $edir . "/";
					}
				}
			$ecat = substr($ecat, 0, length($ecat) - 1);

				if ($static eq "STATIC")
				{
				$replc_match = "-$row[0]-SYMLNKDIR";
				$replc_crit = "$rcat";
				$defdir =~ s/$replc_match/$replc_crit/g;
				}
				else
				{
				$replc_match = "-$row[0]-DRLNKKK";
				$replc_crit = "lv=$row[1]&ct=$ecat";
				$defdir =~ s/$replc_match/$replc_crit/g;
				}
			
			if ($row[20] eq "") { $row[20] = 0;}
			$replc_match = "-$row[0]-LNKNMBRS";
			$replc_crit = "$row[20]";
			$defdir =~ s/$replc_match/$replc_crit/g;

			}
	}
	$crosslinked_sql = "";


### IF THERE IS RELATED CATEGORIES INVOLVED, RESOLVE THEM


if ($related_links ne "")
	{
	### GET TEMPLATES
	$related_dirs = "";
	
	if ($tmpl_in_use ne "def")
		{
		$filename1 = "$tmpl_in_use-5ctst.html"; 
		$filename2 = "$tmpl_in_use-6ctst.html"; 
		$related1 = &get_file_contents2("$data_dir/$filename1");
		$related2 = &get_file_contents2("$data_dir/$filename2");
		}
		else
		{
		$related1 = &get_file_contents2("$data_dir/related1.html");
		$related2 = &get_file_contents2("$data_dir/related2.html");
		}
	
	@relids = split (/,/, $related_links);
	
	$wsql = "";
	foreach $ritem (@relids)
		{
		$wsql = $wsql . " (cid = '$ritem') OR";
		}
	$wsql = substr($wsql, 0, length($wsql) -2);
	
	$sql = "SELECT * FROM dirs WHERE $wsql";
	
	$sth = $dbh->prepare($sql);
	if ( !defined $dbh ) {die "Cannot connect to mSQL server: $DBI::errstr\n"; }
	$sth->execute;
	$serror = ""; $serror = $sth->errstr; if ($serror ne "") {die "SQL Syntax Error: $serror - From: $sql";} 
		while ( @row = $sth->fetchrow() )
			{
				$rcat = "";
				$ecat = "";
				for ($ms = 2; $ms < 18; $ms++) 
					{
					if ($row[$ms] ne "")
						{
						$dirname = $row[$ms];
						$edir = &encode_dir2($row[$ms]);
						$ecat = $ecat . $edir . "-"; 
						$rcat = $rcat . $edir . "/";
						}
					}
			$ecat = substr($ecat, 0, length($ecat) - 1);
			
			## $ecat = dynamic
			## $rcat = static
			## $catname
			
			if ($static eq "STATIC")
				{
				$rel_tmp = $related2;
				$reldir_crit = "<a href=\"$web_url/$rcat\">$dirname</a>";
				$rel_tmp =~ s/%%related_categories%%/$reldir_crit/g;
				$related_dirs = $related_dirs . $rel_tmp;
				}
				else
				{
				$rel_tmp = $related2;
				$reldir_crit = "<a href=\"$script_url/dirs.cgi?lv=$row[1]&ct=$ecat\">$dirname</a>";
				$rel_tmp =~ s/%%related_categories%%/$reldir_crit/g;
				$related_dirs = $related_dirs . $rel_tmp;
				}
			}
		
		chop($related_dirs);chop($related_dirs);
		#$related2 =~ s/%%related_categories%%/$related_dirs/gi;
		$related1 =~ s/%%related_categories%%/$related_dirs/gi;
		$defdir =~ s/%%related%%/$related1/gi;
		
	}
	else
	{
	$defdir =~ s/%%related%%//g;
	}
#################

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

$defdir =~ s/!!scripts_url!!/$script_url/gi;


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

$defdir =~ s/!build_url!/$web_url/gi;


if ($defdir =~ /%%location%%/)
	{
	$defdir =~ s/%%location%%/This category has been set to be hidden/gi;
	}


return ($defdir, $pages, $currentpage, $rows);

}






sub get_rating_images2
{
my ($rating) = @_;
my ($image_rate, $decm, $images);

	if ($rating > 0)
			{
			($image_rate, $decm) = split (/\./, $rating);
		
			if (length($decm) == 1)
				{
				if ($decm > 4) {$image_rate++;}
				}
			if (length($decm) == 2)
				{
				if ($decm > 49) {$image_rate++;}
				}
		
			$image_rate = "r" . $image_rate . ".gif";
			$image_rate =~ s/ //g;
			$images = "<img src=\"$web_url/$image_rate\">";
			}

return ($images); 
}




sub insert_template_includes
{

my ($tmpl_page) = @_;

my (@files, $ifilen, $incl, $theincl);

opendir(DIR,"$data_dir");
@files = readdir(DIR);
closedir(DIR);

if ($tmpl_page =~ /%%incl_/)
	{
	foreach $ifilen (@files)
		{
		if (substr($ifilen, 0, 5) eq "incl_")
			{
			$incl = $ifilen;
			$incl =~ s/\.html//g;
			$incl = "%%" . $incl . "%%";
				
				if ($tmpl_page =~ /$incl/)
					{
					$theincl = &get_file_contents2 ("$data_dir/$ifilen");
					$tmpl_page =~ s/$incl/$theincl/g;
					}
				
			}
		}
	}


$tmpl_page =~ s/!build_url!/$web_url/g;
$tmpl_page =~ s/!!scripts_url!!/$script_url/g;
$tmpl_page =~ s/!!tseeksearch!!/$script_url\/search\.cgi/g;


return ($tmpl_page);

}
















sub get_file_contents2
{

my ($filename) = @_;
my ($filesize, $thefile);

if ((-e "$filename") > 0) 
	{
$filesize = (-s "$filename");
open (TFILECNTS, "$filename") || die "$filename";
	read(TFILECNTS,$thefile,$filesize);
close (TFILECNTS);
	}

return ($thefile);

}




sub decode_date2
{

my ($tvalue) = @_;
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$ydat,$isdst,@marray, $rdvalue, $ampm);

($sec,$min,$hour,$mday,$mon,$year,$wday,$ydat,$isdst) = localtime($tvalue);
$year = "20" . substr($year, 1, 2);
@marray = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");

if (length($min) == 1) {$min = "0" . $min;}
if (length($min) == 1) {$min = "0" . $min;}

if ($gsettings->{date_format} == 0)
	{
	$rdvalue = "$marray[$mon] $mday, $year";
	}
	elsif ($gsettings->{date_format} == 1)
	{
	$rdvalue = "$mday $marray[$mon] $year";
	}
	elsif ($gsettings->{date_format} == 2)
	{
	$mon++;
	if (length($mday) == 1) { $mday = "0" . $mday; }
	if (length($mon) == 1) { $mon = "0" . $mon; }
	$rdvalue = "$year-$mon-$mday";
	}
	elsif ($gsettings->{date_format} == 3)
	{
	$mon++;
	if (length($mday) == 1) { $mday = "0" . $mday; }
	if (length($mon) == 1) { $mon = "0" . $mon; }
	$rdvalue = "$mon-$mday-$year";
	}
	elsif ($gsettings->{date_format} == 4)
	{
	$mon++;
	if (length($mday) == 1) { $mday = "0" . $mday; }
	if (length($mon) == 1) { $mon = "0" . $mon; }
	$rdvalue = "$mday-$mon-$year";
	}

#print "==>$dateformat $rdvalue <BR>";

return ($rdvalue);

}






sub encode_dir2
{

my ($fstring) = @_;
my ($ms, $enc_string, $oneletter, $bstr);

$fstring =~ s/ /_/g;

 for ($ms = 0; $ms < length($fstring); $ms++) 
	{
	
	$oneletter = substr($fstring, $ms, 1);
	
	if (($oneletter !~ /[0-9a-zA-Z]/) and ($oneletter ne "_"))
		{
			$bstr = ord($oneletter);
			if (length($bstr) == 1) {$bstr = "00" . $bstr;}
			if (length($bstr) == 2) {$bstr = "0" . $bstr;}
			$bstr = "," . $bstr;
			$enc_string = $enc_string . $bstr;
		}
		else
		{
			$enc_string = $enc_string . $oneletter;
		}
	}

return ($enc_string);

}






sub decode_dir2
{

my ($coded_string) = @_;
my ($ms, $oneletter, $decoded_string, $plusval, $dechar, $dechar, $fdchar);

$coded_string =~ s/_/ /g;

$plusval = 0;

 for ($ms = 0; $ms < length($coded_string); $ms++) 
	{
	$oneletter = substr($coded_string, $ms, 1);

	if ($plusval != 0)
		{
		$plusval++;
		if ($plusval == 4) {$plusval = 0;}
		}
	elsif ($oneletter eq ",")
		{
		$dechar = substr($coded_string, ($ms+1), 3);
		$dechar = int($dechar);
		$dechar = chr($dechar);
		
		$dechar =~ s/<//g;
		
		$decoded_string = $decoded_string . $dechar;
		$plusval++;
		}
	else 
		{
		$decoded_string = $decoded_string . $oneletter;
		}

	}

return($decoded_string);
}






sub new_gsettings
{
my $this = {};

my ($obj_prp, $scnts, @all_obj_items, $obji, $obj_prp, $obj_val);

$scnts = &get_file_contents2("$data_dir/gsettings.dat");
@all_obj_items = split (/\n/, $scnts);

foreach $obji (@all_obj_items)
	{
	($obj_prp, $obj_val) = split (/=/, $obji);
	$this->{$obj_prp} = $obj_val;
	}

bless $this;
return $this;
}






sub get_imgboxes ### IMAGE AUTHENTICATION
{

my ($ls1, $ls2, $ls3, $ls4, $alpha, $alpha_number1, $alpha_number2, $alpha_number3, $alpha_number4, $alpha1,
    $alpha2, $alpha3, $alpha4, $filename, $img1, $iname1, $img2, $iname2, $img3, $iname3, $img4, $iname4,
    $un_crypted_str, $crypted_str);


$ls1 = int(rand(4));
$ls1++;
if ($ls1 < 1) { $ls1 = 1; }
if ($ls1 == 1){ $ls1 = 49; }if ($ls1 == 2){ $ls1 = 50; }if ($ls1 == 3){ $ls1 = 51; }if ($ls1 == 4){ $ls1 = 52; }

$ls2 = int(rand(4));
$ls2++;
if ($ls2 < 1) { $ls2 = 1; }
if ($ls2 == 1){ $ls2 = 49; }if ($ls2 == 2){ $ls2 = 50; }if ($ls2 == 3){ $ls2 = 51; }if ($ls2 == 4){ $ls2 = 52; }

$ls3 = int(rand(4));
$ls3++;
if ($ls3 < 1) { $ls3 = 1; }
if ($ls3 == 1){ $ls3 = 49; }if ($ls3 == 2){ $ls3 = 50; }if ($ls3 == 3){ $ls3 = 51; }if ($ls3 == 4){ $ls3 = 52; }

$ls4 = int(rand(4));
$ls4++;
if ($ls4 < 1) { $ls4 = 1; }
if ($ls4 == 1){ $ls4 = 49; }if ($ls4 == 2){ $ls4 = 50; }if ($ls4 == 3){ $ls4 = 51; }if ($ls4 == 4){ $ls4 = 52; }

$alpha1 = rand(26);
$alpha_number1 = ord($alpharray[$alpha1]);

$alpha2 = rand(26);
$alpha_number2 = ord($alpharray[$alpha2]);

$alpha3 = rand(26);
$alpha_number3 = ord($alpharray[$alpha3]);

$alpha4 = rand(26);
$alpha_number4 = ord($alpharray[$alpha4]);



foreach $filename (@auth_img)
	{
			if ((substr($filename, 5, 2) ==  $ls1) and (substr($filename, 9, length($filename)- 9) ==  $alpha_number1))
					{
					$img1 = "<img src=\"" . "$web_url/$filename\">";
					$iname1 = $filename;
					#print "-!-> $filename <BR>$img1";
					}

				if ((substr($filename, 5, 2) ==  $ls2) and (substr($filename, 9, length($filename)- 9) ==  $alpha_number2))
					{
					$img2 = "<img src=\"" . "$web_url/$filename\">";
					$iname2 = $filename;
					#print "-!-> $filename <BR>$img2";
					}					

				if ((substr($filename, 5, 2) ==  $ls3) and (substr($filename, 9, length($filename)- 9) ==  $alpha_number3))
					{
					$img3 = "<img src=\"" . "$web_url/$filename\">";
					$iname3 = $filename;
					#print "-!-> $filename <BR>$img3";
					}					

				if ((substr($filename, 5, 2) ==  $ls4) and (substr($filename, 9, length($filename)- 9) ==  $alpha_number4))
					{
					$img4 = "<img src=\"" . "$web_url/$filename\">";
					$iname4 = $filename;
					#print "-!-> $filename <BR>$img4";
					}					
	}

$un_crypted_str = $iname1 ."O". $iname2 . "O" . $iname3 . "O" . $iname4;
$crypted_str = &encode_ps($un_crypted_str);
$imgb = "$img1 $img2 $img3 $img4 :::" . $crypted_str;
return ($imgb);

}









sub encode_ps
{
   my($cc) = @_;
   
   my($rcc, $ccount, $rndchar, $onechar, $lcnt, $ordchar, $retrcc);
   
   $retrcc = "";
   
   $rcc = "";
   $ccount = 0;
   while ($ccount < length($cc))
     {
       $ccount++;
       $rndchar = int(rand(24)) + 97;
       $onechar = ord(substr($cc, length($cc) - $ccount, 1));
       $onechar = $onechar + 1;
       $rcc = $rcc . chr($onechar) . chr($rndchar);
    }

$lcnt = 0;
while ($lcnt < length($rcc))
	{
	$onechar = substr($rcc,$lcnt,1);
	$ordchar = ord($onechar);
	
	if (length($ordchar) == 1) { $ordchar = "00" . $ordchar; }
	if (length($ordchar) == 2) { $ordchar = "0" . $ordchar; }
	
	$retrcc = $retrcc  . $ordchar;
	
	$lcnt++;
	}
	

return ($retrcc);
}



sub get_more_info_file_name
{

my ($rlnkid) = @_;

	if ($gsettings->{more_info_naming} eq "linktitles")
	{
		$title_without_link_tmp = $title_without_link;
		
			$more_info_pg_name = ""; $title_without_link_tmp =~ s/ /_/gi;
			for ($ms = 0; $ms < length($title_without_link_tmp); $ms++) 
				{
				$oneletter1 = substr($title_without_link_tmp, $ms, 1);
					if (($oneletter1 =~ /[0-9a-zA-Z]/) or ($oneletter1 eq "_"))
					{
					$more_info_pg_name = $more_info_pg_name . $oneletter1;
					}
				}
			$more_info_pg_name = lc($more_info_pg_name);
			if (length($more_info_pg_name) > 80) { $more_info_pg_name = substr($more_info_pg_name, 0, 80); }
			$more_info_pg_name = $more_info_pg_name . "_" . $rlnkid;
	
	}
	else
	{
	$more_info_pg_name = $rlnkid;
	}

return ($more_info_pg_name);

}






#### GET CONFIGURATION ########################################################

sub get_setup
{

if ((-e "config.cgi") and (length($config_cgi) < 15))
	{
	$cofile = "config.cgi"; $exists = 1;
	}
	elsif (-e "$config_cgi") 
	{
	$cofile = "$config_cgi"; $exists = 1;
	}
	else
	{
	print "Content-type: text/html\n\n"; 
	print "Could not find config.cgi"; exit;
	}

#$exists = (-e "config.cgi");

if ($exists > 0)
	{
	open (STP, $cofile);
		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 ########################################################



1;