#!/usr/bin/perl

package psfp;

########################################################################
# COPYRIGHT NOTICE:
#
# Copyright 2006 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 CGI::Carp qw(fatalsToBrowser);
use CGI;
use Time::Local;
use DBI;
use IO::Socket;
use pseek;
#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 ($preview, $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);

#$conf_file = "";




$preview = "true";

$time = time();
$date = pseek::decode_date($time);

$gsettings = new_gsettings;


if ($conf_file eq "")
{
$fpsettings = pseek::get_file_contents("$data_dir/fp.cfg");
}
else
{
$fpsettings = pseek::get_file_contents("$data_dir/$conf_file");
}


(@fp_settings) = split (/\n/, $fpsettings);

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)
		{
		$exclude_sql = $exclude_sql . " (cid != '$cs_item') AND";
		}
	
	#$exclude_sql = substr($exclude_sql, 0, length($exclude_sql) - 3);
	#$exclude_sql = $exclude_sql . ")";
	
	$not_to_include = "true";
	}



### 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 ($exclude_sql eq " AND ()") { $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 . pseek::encode_dir($row[$ms]) . "-";
			
			$row[$ms] =~ s/'/\\'/g; 
			$cat_sql = $cat_sql . " (l" . $cclv . " = '$row[$ms]') AND";
			
			$stat_cat = $stat_cat . pseek::encode_dir($row[$ms]) . "/";
			}
		}
	}

	$cat_sql = substr($cat_sql, 0, length($cat_sql) - 3);
	$stat_cat = substr($stat_cat, 0, length($stat_cat) - 1);
	
#print "LV=$cclv ==> $cat_sql <BR>";

$nlevel1 = $cclv + 1;
$nlevel2 = $cclv + 2;

$orderby = "l" . $nlevel1 . ",l" . $nlevel2;

### GET CATEGORY LEVEL AND NAME

if ($cat_sql ne "")
	{
	$sql = "SELECT * FROM dirs WHERE 	((level = '$nlevel1') OR  (level = '$nlevel2')) 
													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])";}
				}
		
		$maincats[$level1_count] = $row[$nlevel1+1] . $lnknr . $newgif;
		
		$dynamic_url = pseek::encode_dir($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_dir($row[$ms]) . "-"; $clv++;}
					}
					if ($cct ne "") { chop($cct); }
					
				
				#$maincatlinks[$level1_count] = "$script_url/dirs2.cgi?cid=$row[0]";
				
				$maincatlinks[$level1_count] = "$script_url/dirs.cgi?lv=$clv&ct=$cct";
				
				}
			}
			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/$dynamic_url/";
				$maincatlinks[$level1_count] = "$web_url/$stat_cat/$dynamic_url/";
				}
			}
		
		### 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>";}
			$main_cat_diz[$level1_count] = "<font face=\"$fp_settings[17]\" size=\"$fp_settings[18]\">" . $dbold1 . $ditalic1 . $row[18] . $ditalic2 . $dbold2 . "</font>";
			}
		
		$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 . $newgif;
		
		#print $subcats[$level2_count] . "<BR>";
		
		
		### GET SUB CATEGORY URLS
		$dynamic_url_m = pseek::encode_dir($row[$nlevel1+1]);
		$dynamic_url = $dynamic_url_m . "-" . pseek::encode_dir($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_dir($row[$ms]) . "-"; $clv++; }
									}
									if ($cct ne "") { chop($cct); }
									
				
				$subcats_urls[$level2_count] = "$script_url/dirs.cgi?lv=$clv&ct=$cct";
				#$subcats_urls[$level2_count] = "$script_url/dirs2.cgi?cid=$row[0]";
				}
			
			}
			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;
				$subcats_urls[$level2_count] = "$web_url/$stat_cat/$dynamic_url/";
				}
			}
		
		$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 = "";
		for ($ms = 2; $ms < 18; $ms++) 
			{
			if ($row[$ms] ne "")
				{
				$edir = &encode_dir($row[$ms]);
				$ecat = $ecat . $edir . "-"; 
				$rcat = $rcat . $edir . "/";
				}
			}
		$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]";
				}
				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; }
			
			$subcats[$mcnt] =~ s/-$row[0]-LNKNUMBR/$row[20]/g;

				$replc_match = "$row[0]-CRSLSUBDYNAM";
				if ($gsettings->{dyn_stat} eq "Dynamic")
				{			
				$replc_crit = "ct=$ecat&lv=$row[1]";
				}
				else
				{
				$replc_crit = "$rcat";
				}
			
			$subcats_urls[$mcnt] =~ s/$replc_match/$replc_crit/g;
			$mcnt++;
			}

		}

	}


$sth->finish;
$dbh->disconnect;






## 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);
			}
		
		#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) = split (/SEPPPRAT2/, $subcat_item);
			
			if ($subcat_item ne "")
				{
				($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>";}
				
				
				$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";
				$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++;
	}


#### 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]\"";}
	
	### CELL COLORS
	if ($fp_settings[4] ne ""){$cell_background = "bgcolor=\"$fp_settings[4]\"";}

$disp_table = "<table border=\"0\" cellpadding=\"$fp_settings[2]\" cellspacing=\"$fp_settings[1]\" $table_background width=\"100%\">\n";

$mncnt = 0;

foreach $item (@disp_cats)
	{
	@one_block = split (/\t/, $item);
	
	if ($colcounter == 0)
		{
		$disp_table = $disp_table . "<tr>\n";
		}
	
	$disp_table = $disp_table . "   <td valign=\"top\" $cell_background width=\"$cel_width%\" height=\"$fp_settings[16]\">\n";
	  $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]<br>";
						}
				
				
				($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";
				$mncnt++;
				}
				else
				{
				if ($fp_settings[19] eq "") { $fp_settings[19] = ", "; }
				if ($citem ne ""){$subcats_string = $subcats_string . "$citem" . $fp_settings[19] . " ";}
				}
			$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]";
				
				$subcats_string = "<font face=\"$fp_settings[14]\" size=\"$fp_settings[15]\">" . $subcats_string . " </font>";
				}
			
			$disp_table = $disp_table . $header . $subcats_string;
			$disp_table = $disp_table . "\n";
	$disp_table = $disp_table . "  </td>\n";
	
	if ($colcounter == ($fp_settings[0]))
		{
		$disp_table = $disp_table . "</tr>\n";
		}
	
	$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>";
		}	
	}

$disp_table = $disp_table . "</tr>\n</table>\n";

#####

#print "$disp_table <br>"; exit;

return ($disp_table);

}







sub get_setup
{

$exists = (-e "$config_cgi");
if ($exists > 0)
	{
	open (STP, "$config_cgi");
		while (defined($line=<STP>))
			{
			if ($line =~ m/#/g)
				{
				$r = pos($line);
				$line = substr($line, 0, $r - 1);
				}
				
				$line =~ s/\n//g;
	
if ($line =~ /^DB_NAME/){$db_name = &get_setup_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 ########################################################

1;
