#!/usr/bin/perl

#<ENC>53</ENC>

package psfp;

########################################################################
# 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";

# 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 lib ".";

use CGI::Carp qw(fatalsToBrowser);
use CGI;
use Time::Local;
use DBI;
use IO::Socket;
use dmodrw;
#use strict;

require Exporter;
use vars qw/@ISA @EXPORT @EXPORT_OK $copyright $prgname/;

@ISA = qw(Exporter);

@EXPORT = qw(generate_categories); 

@EXPORT_OK = qw($disp_table);

&get_setup;

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


sub generate_categories
{
my ($conf_file) = @_;

my ($time, $date, $gsettings, $fpsettings, @fp_settings, $nowtime, $dbh,
$nlevel1, $nlevel2, $sql, $sth, $serror, $cnt, @row, $ecat, $cclv, $cat_sql, $ms, $orderby,
$level1_count, $level2_count, $c1cat, $c2cat, $newgif, $new1secs, $new1time, $lnknr,
@maincats, $crslink_sql, @maincatlinks, @subcats, $dynamic_url_m, $dynamic_url, 
@subcats_urls, $rcat, $edir, $mcnt, $replc_match, $replc_crit, $citem, $maincat_item,
$subc_count, $subcat_number, $subcat_item, @currentc, @currentc1, @currents, @currents1,
$r11, $tmp, $lnksnr, @disp_cats, $cel_width, $colcounter, $table_background, $disp_table,
$mncnt, $item, @one_block, $catcount, $subcats_string, $current_main_cat, $lnmbrs,
$ccat_diz, $header, @main_cat_diz, $serrors, $maincat_item_count, $cell_background,
$return_val, $stat_cat, $exclude_sql, @cid_items, $cs_item, $diz_bold, $diz_italic, 
$dbold1, $dbold2, $ditalic1, $ditalic2, $mbold1, $mbold2, $main_bold, $main_italic, 
$mitalic1, $mitalic2, $r, $r2, $etc_c, $etc_n, $etn_r, $min, $mday, $cct, $main_cat_style_tmp,
$subcnt, $table_html, $colcellstart, $cellstartm, $main_cat_style, $sub_cat_style,
$cat_diz, $cellend, $colcellend, $table_html_end, 
$cellstart, $dsn, $not_to_include, $rows, $clv, $cat_diz_tmp, $catdfont, $newgifdisp, $lnksnr2,
$tbgcolor, $clheight, $lnmbrs2, $maingif, $mainhfont, $maincurlh, $sub_bold, $sub_italic, $sbold1, 
$sbold2, $sitalic1, $sitalic2, $indsubdir, $newgifsub, $catdfonts, $mainhfontsize, $subcaturl,
$subcatname, $subcatnr, $subcatnr2, $sub_cat_style_tmp, $ssubcfont, $sssubfsize, $lastre_1, $simgcnt,
$catdsk
);

$simgcnt = 0;

### GET FRONT PAGE HTML TEMPLATES

	### IF WE'RE GENERATING A FRONT PAGE FROM FRONT PAGE CONFIGURATION
if (($conf_file eq "fp_jscript.cfg") or ($conf_file eq "fp_iframe.cfg") or ($conf_file eq "fp_template.cfg") or ($conf_file eq "fp_ssi.cfg"))
{
$table_html = &get_file_contents22("$data_dir/fp_tableconf.dat");
($table_html, $colcellstart, $cellstart, $main_cat_style, $sub_cat_style, $cat_diz, $cellend, $colcellend, $table_html_end) = split (/oo-_-oo/, $table_html);
}

if ($table_html eq "") { $table_html = qq[<table border="0" cellpadding="!!cellpadding!!" cellspacing="!!cellspacing!!" !!table_background_color!! width="100%">]; }

if ($colcellstart eq "") { $colcellstart = qq[<tr>];}

if ($cellstart eq "") { $cellstart = qq[<td valign="top" !!cell_background_color!! width="!!cell_width!!%" height="!!cell_height!!">\n]; }

if ($main_cat_style eq "") { $main_cat_style = qq[<font face="!!main_font!!" size="!!main_font_size!!">!!bold!! !!italic!!<a href="!!main_cat_url!!">!!main_cat_name!!</a>!!nr!! !!closeitalic!! !!closebold!!</font>!!new!!];}

if ($sub_cat_style eq "") { $sub_cat_style = qq[<font face="!!sub_cat_font!!" size="!!sub_cat_font_size!!">!!subbold!!!!subitalic!!<a href="!!sub_cat_url!!">!!sub_cat_name!!</a>!!nr!!!!new!!!!etc!!!!subcloseitalic!!!!subclosebold!!</font>];}

if ($cat_diz eq "") { $cat_diz = qq[<font face="!!diz_cat_font!!" size="!!diz_cat_font_size!!"> !!dizbold!! !!dizitalic!! !!cat_name_diz!! !!dizcloseitalic!! !!dizclosebold!! </font></a>];}

if ($cellend eq "") { $cellend = "</td>"; }

if ($colcellend eq "") { $colcellend = "</tr>";}

if ($table_html_end eq "") { $table_html_end = "</table>"; }

if ($conf_file eq "fp_iframe.cfg")
	{
	$main_cat_style =~ s/!!main_cat_url!!"/!!main_cat_url!!" target="_top"/gi;
	$sub_cat_style =~ s/!!sub_cat_url!!"/!!sub_cat_url!!" target="_top"/gi;
	}


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


$time = time();
$date = &decode_date2($time);

$gsettings = &new_gsettings;

if ($conf_file eq "")
	{
	$fpsettings = &get_file_contents22("$data_dir/fp.cfg");
	}
	else
	{
	$fpsettings = &get_file_contents22("$data_dir/$conf_file");
	}

if ($fpsettings eq "")
	{
	$fpsettings = &get_file_contents22("$data_dir/fp.cfg");
	}


(@fp_settings) = split (/\n/, $fpsettings);

$table_html =~ s/!!cellpadding!!/$fp_settings[2]/gi;
$table_html =~ s/!!cellspacing!!/$fp_settings[1]/gi;

		open (DATAC, ">> $data_dir/logcatinc.log");
			print DATAC "$data_dir/$conf_file" . "\n";
		close (DATAC);


if ($fp_settings[21] == 0) { $conf_file = ""; }


#0  cols
#1  cell_spacing
#2  table_padding
#3  table_back
#4  cell_back
#5  cat_diz
#6  sub_dirs_include
#7  subcat_number!!
#8  link_numbers
#9  frontpage_file_loc
#10 automatically_build_fp
#11 include_frontpage_template
#12 main_cat_font
#13 main_cat_font_size
#14 sub_cat_font
#15 sub_cat_font_size
#16 cell height

#20 New graphic

$nowtime = time();

### GET MAIN CATEGORIES

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 THERE ARE CATEGORIES NOT TO INCLUDE, CREATE SQL SYNTAX FOR THIS


if ($fp_settings[26] ne "")
	{
	#$exclude_sql = " AND (";
	
	@cid_items = split (/,/,$fp_settings[26]);
	
	foreach $cs_item (@cid_items)
		{
		if ($cs_item ne "")
			{
			$exclude_sql = $exclude_sql . " (cid != '$cs_item') AND";
			$not_to_include = "true";
			}
		}
	
	#$exclude_sql = substr($exclude_sql, 0, length($exclude_sql) - 3);
	#$exclude_sql = $exclude_sql . ")";
	}




### IF THERE ARE HIDDEN CATEGORIES NOT TO INCLUDE GET WHAT NEEDS TO BE EXCLUDED

$sql = "SELECT * FROM dirs WHERE (level = '1' or level = '2') ORDER BY l1, l2";
$sth = $dbh->prepare($sql); 
$sth->execute;
$serror = ""; $serror = $sth->errstr; if ($serror ne "") {die "SQL Syntax Error: $serror";}
while ( @row = $sth->fetchrow() )
	{ 
	if ($row[22] eq "H") 
		{
		$exclude_sql = $exclude_sql . " (cid != '$row[0]') AND"; 
		$not_to_include = "true";
		}
		
	}



if ($not_to_include eq "true") 
	{
	$exclude_sql = substr($exclude_sql, 0, length($exclude_sql) - 3);
	$exclude_sql = " AND (" . $exclude_sql . ")"; 
	}




#### GET CATEGORIES

if ($conf_file eq "")
{

if (length($exclude_sql) < 8) { $exclude_sql = ""; }

$sql = "SELECT * FROM dirs WHERE (level = '1' or level = '2') $exclude_sql ORDER BY l1, l2";

$nlevel1 = 1;
$nlevel2 = 2;


}
else
{

$sql = "SELECT * FROM dirs WHERE cid = '$fp_settings[21]'";

$sth = $dbh->prepare($sql); 
$sth->execute;
$serror = ""; $serror = $sth->errstr; if ($serror ne "") {die "SQL Syntax Error: $serror";}
$cnt = 0;
while ( @row = $sth->fetchrow() )
	{ 
	$ecat = "";
	$cclv = 0;
	$cat_sql = "";
	for ($ms = 2; $ms < 18; $ms++) 
		{
		if ($row[$ms] ne "")
			{
			$cclv++;
			$ecat = $ecat . &encode_dir2($row[$ms]) . "-";
			
			$row[$ms] =~ s/'/\\'/g; 
			$cat_sql = $cat_sql . " (l" . $cclv . " = '$row[$ms]') AND";
			
			$stat_cat = $stat_cat . &encode_dir2($row[$ms]) . "/";
			}
		}
	}

	$cat_sql = substr($cat_sql, 0, length($cat_sql) - 3);
	$stat_cat = substr($stat_cat, 0, length($stat_cat) - 1);
	
	
$nlevel1 = $cclv + 1;
$nlevel2 = $cclv + 2;

$orderby = "l" . $nlevel1 . ",l" . $nlevel2;



### GET CATEGORY LEVEL AND NAME

if ($cat_sql ne "")
	{
	if (length($exclude_sql) < 8) { $exclude_sql = ""; }
	
				if ($fp_settings[6] eq "Yes")
					{
					$sql = "SELECT * FROM dirs WHERE   ((level = '$nlevel1') OR  (level = '$nlevel2')) 
																	AND $cat_sql
																	$exclude_sql
																	ORDER BY $orderby";
					}
					else
					{
					$sql = "SELECT * FROM dirs WHERE   (level = '$nlevel1') 
																	AND $cat_sql
																	$exclude_sql
																	ORDER BY $orderby";
					}


	}
	else
	{
	$sql = "SELECT cid FROM dirs WHERE l1 = 'werwerwesdfdbvcbc'";
	}

}


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


$level1_count = 0; $level2_count = 0;
while ( @row = $sth->fetchrow() )
	{
	$c1cat = $row[$nlevel1+1];
	$c2cat = $row[$nlevel2+1];
	
	#print "--> $c1cat, $c2cat<br>";
	
	## $row[2] $row[$nlevel1+1]
	## $row[3] $row[$nlevel2+1]
	
	if ($row[$nlevel2+1] eq "") ### IF MAIN CAT
		{
			#### CHECK IF WE NEED TO ADD A NEW GRAPHIC
			$newgif = "";
			if ($fp_settings[20] eq "Yes")
			{

					$new1secs = $gsettings->{days1} * 86400;
					$new1time = $nowtime - $new1secs;

					if ($row[23] > $new1time)
						{
						$newgif = " <img src=\"$web_url/new1.gif\" border=\"0\">";
						}

					if ($newgif eq "")
					{
					$new1secs = $gsettings->{days2} * 86400;
					$new1time = $nowtime - $new1secs;
					if ($row[23] > $new1time)
						{
						$newgif = " <img src=\"$web_url/new2.gif\" border=\"0\">";
						}
					}

					if ($newgif eq "")
					{
					$new1secs = $gsettings->{days3} * 86400;
					$new1time = $nowtime - $new1secs;
					if ($row[23] > $new1time)
						{
						$newgif = " <img src=\"$web_url/new3.gif\" border=\"0\">";
						}
					}
			}

		#########
		
		$lnknr = "";
		if ($row[20] eq "") { $row[20] = 0; }

			if ($row[29] > 0) ### IF CROSSLINK DIR
				{
				if ($fp_settings[8] eq "Yes"){$lnknr = "SEPPPRAT (-$row[29]-LNKNUMBR)";}
				}
				else
				{
				if ($fp_settings[8] eq "Yes"){$lnknr = "SEPPPRAT ($row[20])";}
				}
		
		if ($newgif ne "") { $newgif = "NEWGIFSEP" . $newgif; }
		
		$maincats[$level1_count] = $row[$nlevel1+1] . $lnknr . $newgif;
		
		$dynamic_url = &encode_dir2($row[$nlevel1+1]);
		if ($gsettings->{dyn_stat} eq "Dynamic") ### IF DYNAMIC DIRECTORY
			{
			if ($row[29] > 0) ### IF CROSSLINK
				{
				$crslink_sql = $crslink_sql . " (cid = '$row[29]') OR";
				$maincatlinks[$level1_count] = "$script_url/dirs.cgi?-$row[29]-CRSLCDYNAM";
				}
				else
				{

				$clv = 0; $cct = "";
				for ($ms = 2; $ms < 18; $ms++)
					{
					if ($row[$ms] ne "") { $cct = $cct . &encode_dir2($row[$ms]) . "-"; $clv++;}
					}
					if ($cct ne "") { chop($cct); }
					
				$maincatlinks[$level1_count] = "$script_url/dirs.cgi?lv=$clv&ct=$cct";
				}
			}
		elsif ($gsettings->{dyn_stat} eq "Mod_Rewrite")
			{
								$catdsk = "";
								for ($ms = 2; $ms < 18; $ms++)
								{
								if ($row[$ms] ne "") { $catdsk = $catdsk . "-" . $row[$ms]; }
								}
				$catdsk = dmodrw::convert_cat($catdsk);
					
					if ($row[29] > 0)
						{
						$crslink_sql = $crslink_sql . " (cid = '$row[29]') OR";
						$maincatlinks[$level1_count] = "$web_url/-$row[29]-CRSLCDYNAM";
						}
						else
						{
						$maincatlinks[$level1_count] = "$web_url/$row[0]" . "$catdsk.html";
						}
			}
		else #### IF STATIC DIRECTORY
			{
				if ($row[29] > 0)
					{
					$crslink_sql = $crslink_sql . " (cid = '$row[29]') OR";
					$maincatlinks[$level1_count] = "$web_url/-$row[29]-CRSLCDYNAM";
					}
					else
					{
					$maincatlinks[$level1_count] = "$web_url/$stat_cat/$dynamic_url/";
					}
			}
		
		#### INSERT IMAGES FOR MAIN CATEGORIES
		if (-e ("$web_dir/imagefiles/cimage/$row[0].gif") > 0) { $ifname = "<img src=\"$web_url/imagefiles/cimage/$row[0].gif\">";  }
		elsif (-e ("$web_dir/imagefiles/cimage/$row[0].jpg") > 0) { $ifname = "<img src=\"$web_url/imagefiles/cimage/$row[0].jpg\">";  }
		else {$ifname = "";} 
		$main_images[$level1_count] = $ifname;
		
		
		### IF CATEGORY DESCRIPTIONS ARE NEEDED
		if (($fp_settings[5] eq "Yes") and ($row[18] ne ""))
			{
			($diz_bold, $diz_italic) = split (/:/, $fp_settings[25]);
			
			$dbold1 = ""; $dbold2 = "";
			if ($diz_bold eq "Y") {$dbold1 = "<b>"; $dbold2 = "</b>";}
			$ditalic1 = "";  $ditalic2 = "";
			if ($diz_italic eq "Y"){$ditalic1 = "<i>"; $ditalic2 = "</i>";}
			
			
			#<font face="!!diz_cat_font!!" size="!!diz_cat_font_size!!"> !!dizbold!! !!dizitalic!! !!cat_name_diz!! !!dizcloseitalic!! !!dizclosebold!! </font></a>
			
			$cat_diz_tmp = $cat_diz;
			$catdfont = $fp_settings[17]; $cat_diz_tmp =~ s/!!diz_cat_font!!/$catdfont/gi;
			$catdfonts = $fp_settings[18]; $cat_diz_tmp =~ s/!!diz_cat_font_size!!/$catdfonts/gi;
			
			$cat_diz_tmp =~ s/!!dizbold!!/$dbold1/gi;
			$cat_diz_tmp =~ s/!!dizitalic!!/$ditalic1/gi;
			
			$cat_diz_tmp =~ s/!!cat_name_diz!!/$row[18]/gi;
			
			$cat_diz_tmp =~ s/!!dizcloseitalic!!/$ditalic2/gi;
			$cat_diz_tmp =~ s/!!dizclosebold!!/$dbold2/gi;
			
			#$main_cat_diz[$level1_count] = "<font face=\"$fp_settings[17]\" size=\"$fp_settings[18]\">" . $dbold1 . $ditalic1 . $row[18] . $ditalic2 . $dbold2 . "</font>";
			$main_cat_diz[$level1_count] = $cat_diz_tmp;
			}
		
		$level1_count++;
		
		#print "<BR>";
		}
	elsif ($fp_settings[6] eq "Yes") ### SUB CATEGORIES ######################################################################
		{
		
		#### CHECK IF WE NEED TO ADD A NEW GRAPHIC
				$newgif = "";
				if ($fp_settings[20] eq "Yes")
				{
						$new1secs = $gsettings->{days1} * 86400;
						$new1time = $nowtime - $new1secs;
						if ($row[23] > $new1time)
							{
							$newgif = " <img src=\"$web_url/new1.gif\" border=\"0\">";
							}
				
				if ($newgif eq "")
						{
						$new1secs = $gsettings->{days2} * 86400;
						$new1time = $nowtime - $new1secs;
						if ($row[23] > $new1time)
							{
							$newgif = " <img src=\"$web_url/new2.gif\" border=\"0\">";
							}
						}
				
				if ($newgif eq "")
						{
						$new1secs = $gsettings->{days3} * 86400;
						$new1time = $nowtime - $new1secs;
						if ($row[23] > $new1time)
							{
							$newgif = " <img src=\"$web_url/new3.gif\" border=\"0\">";
							}
						}				
				}
		
		#########

		
		$lnknr = "";
		if ($row[20] eq "") { $row[20] = 0; }

		if ($row[29] > 0) ### IF CROSSLINK
			{
			if ($fp_settings[8] eq "Yes"){$lnknr = "SEPPPRAT2 (-$row[29]-LNKNUMBR)";}
			}
			else
			{
			if ($fp_settings[8] eq "Yes"){$lnknr = "SEPPPRAT2 ($row[20])";}
			}
		

		
		### GET SUB CATEGORY NAMES
		#print "===!!> $row[$nlevel2+1]<br>";
		
		$subcats[$level2_count] = $row[$nlevel1+1] . "\t" . $row[$nlevel2+1] . $lnknr . "SUBGIFSEP" . $newgif;
		
		#print $subcats[$level2_count] . "<BR>";

		#### INSERT IMAGES FOR SUB CATEGORIES
		if (-e ("$web_dir/imagefiles/cimage/$row[0].gif") > 0) { $ifname = "<img src=\"$web_url/imagefiles/cimage/$row[0].gif\">";  }
		elsif (-e ("$web_dir/imagefiles/cimage/$row[0].jpg") > 0) { $ifname = "<img src=\"$web_url/imagefiles/cimage/$row[0].jpg\">";  }
		else {$ifname = "";} 
		$sub_images[$level2_count] = $ifname;

		
		### GET SUB CATEGORY URLS
		$dynamic_url_m = &encode_dir2($row[$nlevel1+1]);
		$dynamic_url = $dynamic_url_m . "-" . &encode_dir2($row[$nlevel2+1]);
		if ($gsettings->{dyn_stat} eq "Dynamic") ### IF DYNAMIC DIR
			{
			
				if ($row[29] > 0)
					{
					$crslink_sql = $crslink_sql . " (cid = '$row[29]') OR";
					$subcats_urls[$level2_count] = "$script_url/dirs.cgi?$row[29]-CRSLSUBDYNAM";
					}
					else
					{
							$clv = 0; $cct = "";
							for ($ms = 2; $ms < 18; $ms++)
								{
								if ($row[$ms] ne "") { $cct = $cct . &encode_dir2($row[$ms]) . "-"; $clv++; }
								}
								if ($cct ne "") { chop($cct); }
									
					$subcats_urls[$level2_count] = "$script_url/dirs.cgi?lv=$clv&ct=$cct";
					}
			
			}
			elsif ($gsettings->{dyn_stat} eq "Mod_Rewrite")
			{
							$catdsk = "";
							for ($ms = 2; $ms < 18; $ms++)
								{
								if ($row[$ms] ne "") { $catdsk = $catdsk . "-" . $row[$ms]; }
								}
			
				$catdsk = dmodrw::convert_cat($catdsk);
				
					if ($row[29] > 0)
					{
					$crslink_sql = $crslink_sql . " (cid = '$row[29]') OR";
					$subcats_urls[$level2_count] = "$web_url/$row[29]-CRSLSUBDYNAM";
					}
					else
					{
					$subcats_urls[$level2_count] = "$web_url/$row[0]" . $catdsk . ".html";
					}
				
			}
			else ### IF STATIC DIR
			{
			if ($row[29] > 0)
				{
				$crslink_sql = $crslink_sql . " (cid = '$row[29]') OR";
				$subcats_urls[$level2_count] = "$web_url/$row[29]-CRSLSUBDYNAM";
				}
				else
				{
				$dynamic_url =~ s/-/\//g;
				$arts2a = "/$stat_cat/$dynamic_url/";
				$arts2a =~ s/\/\//\//g;
				$subcats_urls[$level2_count] = "$web_url" . $arts2a;
				}
			}
		
		$level2_count++;
		}
		
	#print "$row[2]/$row[3] <Br>";
	}



#### IF CROSSLINKS IS INVOLVED, RESOLVE THEM

if ($crslink_sql ne "")
	{
	$crslink_sql = "SELECT * FROM dirs WHERE $crslink_sql";
	$crslink_sql = substr($crslink_sql, 0, length($crslink_sql) - 2);

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

	while ( @row = $sth->fetchrow() )
		{
		$rcat = "";
		$ecat = "";
		$mrcat = "";
		for ($ms = 2; $ms < 18; $ms++) 
			{
			if ($row[$ms] ne "")
				{
				$edir = &encode_dir2($row[$ms]);
				$ecat = $ecat . $edir . "-"; 
				$rcat = $rcat . $edir . "/";
				$mrcat = $mrcat . "-" . $row[$ms];
				}
			}
		$ecat = substr($ecat, 0, length($ecat) - 1);

		#### MAIN CAREGORIES
		
		$mcnt = 0;
		foreach $citem (@maincatlinks)
			{
			if ($row[20] eq "") { $row[20] = 0; }
			
			$maincats[$mcnt] =~ s/-$row[0]-LNKNUMBR/$row[20]/g; ### MAINCAT NAME

				if ($gsettings->{dyn_stat} eq "Dynamic")
				{			
				$replc_match = "-$row[0]-CRSLCDYNAM";
				$replc_crit = "ct=$ecat&lv=$row[1]";
				}
				elsif ($gsettings->{dyn_stat} eq "Mod_Rewrite")
				{
				$replc_match = "-$row[0]-CRSLCDYNAM";
				$catdsk = dmodrw::convert_cat($mrcat);
				$replc_crit = "$row[0]" . $catdsk . ".html";
				}
				else ### STATIC
				{
				$replc_match = "-$row[0]-CRSLCDYNAM";
				$replc_crit = "$rcat";
				}
			
			#print "--> $replc_match ..... $replc_crit <BR>";
			
			$maincatlinks[$mcnt] =~ s/$replc_match/$replc_crit/g;
			
			$mcnt++;
			}




		######### SUB CATEGORIES
		
		$mcnt = 0;
		foreach $citem (@subcats_urls)
			{
			if ($row[20] eq "") { $row[20] = 0; }
			
		#	print "$citem<BR>";
			
			$subcats[$mcnt] =~ s/-$row[0]-LNKNUMBR/$row[20]/g;

				if ($gsettings->{dyn_stat} eq "Dynamic")
				{			
				$replc_crit = "ct=$ecat&lv=$row[1]";
				$replc_match = "$row[0]-CRSLSUBDYNAM";
				}
				elsif ($gsettings->{dyn_stat} eq "Mod_Rewrite")
				{
				$mrcat1 = dmodrw::convert_cat($mrcat);
				$replc_crit = "$row[0]" . $mrcat1 . ".html";
				$replc_match = "$row[0]-CRSLSUBDYNAM";
				}				
				else
				{
				$replc_crit = "$rcat";
				$replc_match = "$row[0]-CRSLSUBDYNAM";
				}
			
			$subcats_urls[$mcnt] =~ s/$replc_match/$replc_crit/g;
			$mcnt++;
			}

		}

	}


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


$main_nr = @maincats;
$sub_nr = @subcats;


####### SORT MAIN CATEGORIES AND SUB CATEGORIES

$level1_count = 0; $maincat_item_count = 0;
foreach $maincat_item (@maincats)
	{
	
	#### ADD SUB CATEGORIES FOR CELLS
	$subc_count = 0;
	$subcat_number = 0;
	foreach $subcat_item (@subcats)
		{
				
		@currentc = split(/\t/, $maincat_item);
		@currentc1 = split(/SEPPPRAT/, $currentc[0]);
		
		@currents = split(/SEPPPRAT2/, $subcat_item);
		@currents1 = split(/\t/, $currents[0]);
		
		#print "$subcat_item<BR>";
		#if (($subcat_item =~ /^$currentc1[0]/) and ($subcat_number < $fp_settings[7]) and ($fp_settings[6] eq "Yes"))
		
		if ($currentc1[0] =~ m/ <img/g)
			{
			$r11 = pos($currentc1[0]);
			$currentc1[0] = substr($currentc1[0], 0, $r11 - 5);
			}
		
		if ($currentc1[0] =~ /NEWGIFSEP/) { $currentc1[0] =~ s/NEWGIFSEP//g; }
		
		#print "! $currents1[0] --!> $currentc1[0]<BR>"; 
		
		if (($currents1[0] eq $currentc1[0]) and ($subcat_number < $fp_settings[7]) and ($fp_settings[6] eq "Yes"))
			{
			
			#print "! $currents1[0] --!> $currentc1[0] --> $subcat_item<BR>\n";
			
			($tmp, $subcat_item) = split (/\t/, $subcat_item);
			
			($subcat_item, $lnksnr, $newgifdisp) = split (/SEPPPRAT2/, $subcat_item);
			
			if ($subcat_item ne "")
				{
				#$subcats_urls[$subc_count] =~ s/http:\/\///g;
				#$subcats_urls[$subc_count] =~ s/\/\//\//g;
				#$subcats_urls[$subc_count] = "http://" . $subcats_urls[$subc_count];
				#$disp_cats[$level1_count] = $disp_cats[$level1_count] . "\t" . "<a href=\"$subcats_urls[$subc_count]\">" . $sbold1 . $sitalic1 . $subcat_item . $sitalic2. $sbold2 . "</a>$lnksnr";
				
				($lnksnr2, $newgifdisp) = split (/NEWGIFSEP/, $lnksnr);
				
				$disp_cats[$level1_count] = $disp_cats[$level1_count] . "\t" . $subcats_urls[$subc_count] . "o-k-o" .  $subcat_item . "o-k-o" . $lnksnr2;
				$subcat_number++;
				}
			
			#$subcats[$subcat_number] = "3910238192";
			}
		$subc_count++;  
		}

	#$maincat_item =~ s/SEPPPRAT//g;
	$disp_cats[$level1_count] = $maincat_item . "\t" . $disp_cats[$level1_count];
	$level1_count++;
	}



#### MAIN CATEGORY LINKS: @maincatlinks
#### MAIN CATEGORY NAMES: @maincats

#### SUB CATEGORY LINKS: @subcats_urls
#### SUB CATEGORY NAMES: @subcats

### @disp_cats = HOLDS MAIN CATEGORY WITH SUB CATEGORIES

### CREATE TABLE

		
		### GET WIDTH OF CELLS
		$cel_width = (100 / ($fp_settings[0] + 1));
		$cel_width = int($cel_width);
		

$colcounter = 0;

	### TABLE BACKGROUND COLOR
	#if ($fp_settings[3] ne ""){ $table_background = "bgcolor=\"$fp_settings[3]\""; }
	
	$tbgcolor = "";
	if ($fp_settings[3] ne "")
		{
		$tbgcolor = "bgcolor=\"$fp_settings[3]\"";
		$table_html =~ s/!!table_background_color!!/$tbgcolor/gi; 
		}
		else
		{
		$table_html =~ s/!!table_background_color!!/$tbgcolor/gi; 
		}
	
	### CELL COLORS
	if ($fp_settings[4] ne "") 
		{
		$cell_background = "bgcolor=\"$fp_settings[4]\""; 
		}
		else
		{
		$cell_background = "";
		}
	

#$disp_table = "<table border=\"0\" cellpadding=\"$fp_settings[2]\" cellspacing=\"$fp_settings[1]\" $table_background width=\"100%\">\n";
#$table_html = "<table border="0" cellpadding=\"$fp_settings[2]\" cellspacing=\"$fp_settings[1]\" $table_background width=\"100%\">\n";





$disp_table = $table_html;

$mncnt = 0;

foreach $item (@disp_cats) ### CATEGORIES THAT NEEDS TO BE DISPLAYED
	{
	@one_block = split (/\t/, $item);
	
	if ($colcounter == 0)
		{
		$disp_table = $disp_table . "\n" . $colcellstart . "\n"; ## <tr>
		}
	
	#$disp_table = $disp_table . "   <td valign=\"top\" $cell_background width=\"$cel_width%\" height=\"$fp_settings[16]\">\n";
	$cellstart =~ s/!!cell_background_color!!/$cell_background/gi;
	$cellstart =~ s/!!cell_width!!/$cel_width/gi;
	$clheight = $fp_settings[16]; $cellstart =~ s/!!cell_height!!/$clheight/gi;
	$disp_table = $disp_table . $cellstart;
	
	
	  $catcount = 0; $subcats_string = "";
		foreach $citem (@one_block)
			{
			if ($catcount == 0)
				{
					#$maincat_item =~ s/SEPPPRAT//g;
					if ($citem =~ /SEPPPRAT/)
						{
						($current_main_cat, $lnmbrs) = split(/SEPPPRAT/, $citem);
						}
						else
						{
						$current_main_cat = $citem;
						}
					
					### CHECK IF WE NEED TO INSERT DESCRIPTIONS
					if ($fp_settings[5] eq "Yes")
						{
						$ccat_diz = "$main_cat_diz[$mncnt]";
						}
				
				
				($main_bold, $main_italic) = split (/:/, $fp_settings[22]);
				
				$mbold1 = ""; $mbold2 = "";
				if (($main_bold eq "Y") or ($fp_settings[22] eq "")) {$mbold1 = "<b>"; $mbold2 = "</b>";}
				$mitalic1 = ""; $mitalic2 = "";
				if ($main_italic eq "Y"){$mitalic1 = "<i>"; $mitalic2 = "</i>";}
				
				
				$maincatlinks[$mncnt] =~ s/http:\/\///g;
				$maincatlinks[$mncnt] =~ s/\/\//\//g;
				$maincatlinks[$mncnt] = "http://" . $maincatlinks[$mncnt];
				
				#$header = "<font face=\"$fp_settings[12]\" size=\"$fp_settings[13]\"><a href=\"$maincatlinks[$mncnt]\">" . $mbold1 .  $mitalic1 . "$current_main_cat" . $mitalic2 . $mbold2 . "</a>$lnmbrs</font><br>$ccat_diz";
				
				$main_cat_style_tmp = $main_cat_style;
				
				($lnmbrs2, $maingif) = split (/NEWGIFSEP/, $lnmbrs);
				
				
				$mainhfont = $fp_settings[12]; 		$main_cat_style_tmp =~ s/!!main_font!!/$mainhfont/gi;
				$mainhfontsize = $fp_settings[13];	$main_cat_style_tmp =~ s/!!main_font_size!!/$mainhfontsize/gi;
																$main_cat_style_tmp =~ s/!!bold!!/$mbold1/gi;	 
																$main_cat_style_tmp =~ s/!!closebold!!/$mbold2/gi;
																$main_cat_style_tmp =~ s/!!italic!!/$mitalic1/gi;
																$main_cat_style_tmp =~ s/!!closeitalic!!/$mitalic2/gi;
																$main_cat_style_tmp =~ s/!!nr!!/$lnmbrs2/gi;
																$main_cat_style_tmp =~ s/!!main_cat_name!!/$current_main_cat/gi;
				$maincurlh = $maincatlinks[$mncnt]; $main_cat_style_tmp =~ s/!!main_cat_url!!/$maincurlh/gi;
																$main_cat_style_tmp =~ s/!!new!!/$maingif/gi;
																$main_cat_style_tmp =~ s/NEWGIFSEP//g;
																
				$maincat_image =  $main_images[$mncnt]; $main_cat_style_tmp =~ s/!!cimage!!/$maincat_image/gi;
				
																
					### IF CAT DIZ OR SUB DIRS
					#if (($fp_settings[5] eq "Yes") or ($fp_settings[6] eq "Yes")) { $main_cat_style_tmp = $main_cat_style_tmp . "<BR>"; }
					if ($fp_settings[6] eq "Yes") { $main_cat_style_tmp = $main_cat_style_tmp . "<BR>"; }
																
				$header = $main_cat_style_tmp;
				$mncnt++;
				}
				else ### IF SUB CATEGORY
				{
				#if ($fp_settings[19] eq "") { $fp_settings[19] = ", "; } ### SEPERATOR
				#if ($citem ne ""){$subcats_string = $subcats_string . $citem . $fp_settings[19] . " ";}
				$subcats_string = $subcats_string . $citem . "!-!ookloo!-!";
				}
			$catcount++;
			}
			
			### SET FONT FOR SUB CATEGORIES
			if ($subcats_string ne "") 
				{
				#$subcats_string = substr($subcats_string, 0, length($subcats_string) - length($fp_settings[19]) - 1);
				#$subcats_string = $subcats_string . "$fp_settings[23]";
				#print "--> $subcats_string<BR><BR>\n\n\n\n";
				
				my @totalsubs = split (/!-!ookloo!-!/, $subcats_string);
				
						$subcats_string = "";
						
						($sub_bold, $sub_italic) = split (/:/, $fp_settings[24]);
						$sbold1 = ""; $sbold2 = "";
						if ($sub_bold eq "Y") {$sbold1 = "<b>"; $sbold2 = "</b>";}
						$sitalic1 = ""; $sitalic2 = "";
						if ($sub_italic eq "Y"){$sitalic1 = "<i>"; $sitalic2 = "</i>";}
						
						
						$subcnt = 0; 
						foreach $indsubdir (@totalsubs)
						{
						if ($indsubdir ne "")
						{
						($subcaturl, $subcatname, $subcatnr) = split(/o-k-o/, $indsubdir);
				
						($subcatnr2, $newgifsub) = split (/SUBGIFSEP/, $subcatnr);
						
						
						#$subcats_string = "<font face=\"$fp_settings[14]\" size=\"$fp_settings[15]\">" . $subcats_string . " </font>";
				
						$sub_cat_style_tmp = $sub_cat_style;
				
						$sub_cat_style_tmp =~ s/!!sub_cat_url!!/$subcaturl/gi;
						$ssubcfont = $fp_settings[14];  $sub_cat_style_tmp =~ s/!!sub_cat_font!!/$ssubcfont/gi;
						$sssubfsize = $fp_settings[15]; $sub_cat_style_tmp =~ s/!!sub_cat_font_size!!/$sssubfsize/gi;
				
						$sub_cat_style_tmp =~ s/!!subbold!!/$sbold1/gi; 
						$sub_cat_style_tmp =~ s/!!subitalic!!/$sitalic1/gi; 
				
						$sub_cat_style_tmp =~ s/!!sub_cat_name!!/$subcatname/gi; 
						$sub_cat_style_tmp =~ s/!!nr!!/$subcatnr2/gi; 
						
						$sub_cat_style_tmp =~ s/!!new!!/$newgifsub/gi; 
				
						$sub_cat_style_tmp =~ s/!!subcloseitalic!!/$sitalic2/gi; 
						$sub_cat_style_tmp =~ s/!!subclosebold!!/$sbold2/gi;
						
						$etc_c = "!!etc!!" . $subcnt . "--";
						$sub_cat_style_tmp =~ s/!!etc!!/$etc_c/gi;

						$sub_cat_style_tmp =~ s/SUBGIFSEP//gi;
						
						
						$sub_cat_style_tmp =~ s/!!cimage!!/$sub_images[$simgcnt]/gi; $simgcnt++;
						
						#$subcats_string = "<font face=\"$fp_settings[14]\" size=\"$fp_settings[15]\">" . $subcats_string . " </font>";
						$subcats_string = $subcats_string . "\n" . $sub_cat_style_tmp . $fp_settings[19];
						$subcnt++;
						}
						}
						
						$subcats_string = substr($subcats_string, 0, length($subcats_string) - length($fp_settings[19])); # . $fp_settings[23];
						
						for ($ms = 0; $ms < ($subcnt - 1); $ms++)
							{
							$etc_n = "!!etc!!" . $ms . "--";
							$subcats_string =~ s/$etc_n//gi;
							}
							$etn_r = "!!etc!!" . ($subcnt - 1) . "--"; $lastre_1 = $fp_settings[23];
							$subcats_string =~ s/$etn_r/$lastre_1/gi;
				}
			

	
	
			
			
	###### CEL END
	if ($ccat_diz ne "") { $ccat_diz = "<BR>" . $ccat_diz; }
	
	$disp_table = $disp_table . $header . $subcats_string . $ccat_diz;
	$disp_table = $disp_table . "\n";
	$disp_table = $disp_table . "$cellend\n"; ## </tr>


	### COLLUMN END
	if ($colcounter == ($fp_settings[0]))
		{
		$disp_table = $disp_table . $colcellend; ### </td>
		}
	
	$colcounter++;
	
	if ($colcounter > $fp_settings[0])
		{
		$colcounter = 0;
		}

}### END LOOP

##### FILL TABLE WITH REMAINING CELLS WHERE REQUIRED

if ($colcounter != 0)
	{
	for ($ms = $colcounter; $ms <= $fp_settings[0]; $ms++)
		{
		#$disp_table = $disp_table . "<td $cell_background width=\"$cel_width%\">&nbsp; </td>";
		#$cellstart_tmp =~ s/!!cell_background_color!!/$cell_background/gi;
		#$cellstart_tmp =~ s/!!cell_width!!/$cel_width/gi;
		#$ecellheight = $fp_settings[16];
		#$cellstart_tmp =~ s/!!cell_height!!/$ecellheight/gi;
		$disp_table = $disp_table . $cellstart . "&nbsp;" . $cellend;
		}	
	}

$disp_table = $disp_table . "\n$colcellend\n$table_html_end\n";

#####

#print "$disp_table <br>"; exit;

return ($disp_table);

}










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_line2($line, DB_NAME);}
if ($line =~ /^DB_USERNAME/){$db_username = &get_setup_line2($line, DB_USERNAME);}
if ($line =~ /^DB_PASSWORD/){$db_password = &get_setup_line2($line, DB_PASSWORD);}
if ($line =~ /^MYSQL_HOSTNAME/){$mysql_hostname = &get_setup_line2($line, MYSQL_HOSTNAME);}
if ($line =~ /^MYSQL_PORT/){$mysql_port = &get_setup_line2($line, MYSQL_PORT);}

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

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

if ($line =~ /^MAIL_METHOD/){$mail_method = &get_setup_line2($line, MAIL_METHOD);}
if ($line =~ /^SENDMAIL/){$sendmail = &get_setup_line2($line, SENDMAIL);}
if ($line =~ /^SMTP_SERVER/){$smtp_server = &get_setup_line2($line, SMTP_SERVER);}


			}
	close (STP);
	
	}
}







sub get_setup_line2
{
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 ########################################################



sub get_file_contents22
{

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

if ((-e "$filename11") > 0) 
	{
$filesize = (-s "$filename11");
open (TFILECNTS, "$filename11") || die "$filename11";
	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, @conflines);

open (GSETT, "$data_dir/gsettings.dat");
	@conflines = <GSETT>;
close (GSETT);
$dateformat = $conflines[48];
chop($dateformat);


($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 ($hour == 0){$hour = 12;}

if ($dateformat eq "date_format=0")
	{
	$rdvalue = "$marray[$mon] $mday, $year";
	}
	elsif ($dateformat eq "date_format=1")
	{
	$rdvalue = "$mday $marray[$mon] $year";
	}
	elsif ($dateformat eq "date_format=2")
	{
	$mon++;
	if (length($mday) == 1) { $mday = "0" . $mday; }
	if (length($mon) == 1) { $mon = "0" . $mon; }
	$rdvalue = "$year-$mon-$mday";
	}
	elsif ($dateformat eq "date_format=3")
	{
	$mon++;
	if (length($mday) == 1) { $mday = "0" . $mday; }
	if (length($mon) == 1) { $mon = "0" . $mon; }
	$rdvalue = "$mon-$mday-$year";
	}
	elsif ($dateformat eq "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 new_gsettings
{
my $this = {};

my (@all_obj_items);

$scnts = &get_file_contents22("$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;
}


1;