#!/usr/bin/perl

package fms_acs;

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

require Exporter;

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


### WINDOWS USERS EDIT BELOW ##############################################
$cfile = "config.cgi";
#$cfile = "e:/full/server/path/to/config.cgi";
###########################################################################

$thetime = time();
($sec,$min,$hour,$mday,$mon,$year,$wday,$ydat,$isdst) = localtime($thetime);
$year = "20" . substr($year, 1, 2);

$version = "FM SiteSearch Version 4.1";

if ((-e "fms_admin3.cgi") > 0) { $version = "FM SiteSearch Pro Version 4.1"; }


$prgname = "FM SiteSearch";
$copyright = "Copyright \@2002-$year FocalMedia.Net";

@ISA = qw(Exporter);

@EXPORT = qw(check_access 
				 gtemplate
				 check_search_spamming
				 get_heading_box
				 insbuttons
				 indexprogress); 

@EXPORT_OK = qw($tmp
					$template1);


$default_permissions = 0777;  ### DEFAULT PERMISSIONS THAT IS USED FOR LOG FILES

&get_setup;






sub indexprogress
{

my ($pauseint, $redirurl, $b_heading, $b_display, $cur_nr, $end_nr) = @_;

$perc = ($cur_nr / $end_nr) * 100;
#$perc = sprintf("%5.2f",$perc);
$perc = int($perc);


if ($redirurl ne "") 
	{
	$redgo = qq[<meta HTTP-EQUIV="REFRESH" CONTENT="$pauseint;URL=$redirurl">];
	$ani_img = qq[
	<table width="100%" border="0" cellpadding="6" cellspacing="1" class="border">
    <tr>
      <td width="0%" height="35" class="normaltext"><img src="$web_url/fms_ajl1.gif"></td>
      <td width="100%" background="$web_url/fms_bar6.gif" class="normaltext"><b>$b_heading</b></td>
    </tr>
  </table>	
	];
	}
	else
	{

$ani_img = qq[
	<table width="100%" border="0" cellpadding="6" cellspacing="1" class="border">
    <tr>
      <td height="35" background="$web_url/fms_bar6.gif" class="normaltext"><b>$b_heading</b></td>
    </tr>
  </table>
];	
	}

$perc1 = $perc;
$perc2 = 100 - $perc;

$retv = qq[<html>
<head>
<title>Search Index</title>
$redgo

<style type="text/css">
A {COLOR: #0000FF}
A:visited {COLOR: #0000FF}
A:active {COLOR: #000000}
A:hover {COLOR: #000000}

.verd
{
	font-family: Arial, Helvetica, sans-serif;
	font-size: 10px;
	line-height: 18px;
}

.border {
	border: 1px solid #E0E0E0;
}

p
{
	font-family: Arial, Helvetica, sans-serif;
	font-size: 12px;
	line-height: 18px;
}

.normaltext
{
	font-family: Arial, Helvetica, sans-serif;
	font-size: 12px;
	line-height: 18px;
}

.blanktext
{ color: #FFFFFF;
}

</style>
</head>
<body>

$ani_img
  
  <table border="0" cellpadding="0" cellspacing="0" width="100%"    background="$web_url/fms_shadow.gif">
  <tr>
    <td width="1%" bgcolor="#FFFFFF" class="blanktext"> _ </td>
    <td width="99%"></td>
  </tr>
</table>
 
  <table width="100%" border="0" class="border">
     <tr>
       <td width="97%"><table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#F9F9F9">
         <tr>
           <td background="$web_url/fms_bar.gif" width="1" class="normaltext"><b>&nbsp;&nbsp;$perc%</b></td>
           <td background="$web_url/fms_bar.gif" height="30" width="$perc1%">&nbsp;</td>
           <td width="$perc2%">&nbsp;</td>
         </tr>
       </table></td>
     </tr>
   </table>

 
<table width="100%" border="0" cellpadding="0" cellspacing="0"    background="$web_url/fms_shadow.gif">
  <tr>
    <td width="1%" bgcolor="#FFFFFF" class="blanktext"> _ </td>
    <td width="99%"></td>
  </tr>
</table>
<span class="normaltext">$b_display</span>


</body>
</html>
];

return ($retv);

}




sub get_heading_box
{
my ($h_icon, $h_heading, $h_text) = @_;

$h_return = qq[
<table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#E1E1E1">
  <tr>
    <td width="1%" valign="top" bgcolor="#FFFFFF"><p><img src="$h_icon"></p>
    </td>
    <td width="99%" valign="top" bgcolor="#FFFFFF"><table width="100%" border="0" cellpadding="6">
      <tr>
        <td background="$web_url/fms_bar6.gif"><strong class="normaltext">$h_heading</strong></td>
        </tr>
      <tr>
        <td class="normaltext">$h_text</td>
        </tr>
    </table></td>
  </tr>
</table>
<table border="0" cellpadding="0" cellspacing="0" width="100%"    background="$web_url/fms_shadow.gif">
  <tr>
    <td width="1%" bgcolor="#FFFFFF" class="blanktext"> _ </td>
    <td width="99%"></td>
  </tr>
</table>
];

return ($h_return);
}





sub check_search_spamming
{

$settings_data = &get_file_contents2("$data_dir/settings.dat");
@settings = split (/\n/, $settings_data);
$nr_searches_per_hour = $settings[16];

if ($nr_searches_per_hour eq "") { $nr_searches_per_hour = 40; }


### CHECK IF TMP DIR EXISTS
if ((-e "$data_dir/tmpip") < 1) 
	{
	$status = mkdir("$data_dir/tmpip", $default_permissions);
	$stat = chmod($default_permissions,"$data_dir/tmpip","$data_dir/tmpip");
	}

### CLEAN UP tmpip DIR FOR OLDER SEARCHES

$ipaddress = $ENV{'REMOTE_ADDR'};
$ntime = time();

	opendir(DDIR,"$data_dir/tmpip");
	@afiles = readdir(DDIR);
	foreach $filenamea (@afiles)
		{
			if (($filenamea ne ".") and ($filenamea ne ".."))
			{
			($device,$inode,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat("$data_dir/tmpip/$filenamea");
				if ($mtime < ($ntime - 3600))
					{
					$stat = unlink ("$data_dir/tmpip/$filenamea");
					}
			}
		}
	closedir (DDIR);




### RECORD IP ADDRESS

if ((-e "$data_dir/tmpip/$ipaddress") < 1)
	{
	open (IPADD, "> $data_dir/tmpip/$ipaddress");
		print IPADD "1";
	close (IPADD);
	$stat = chmod($default_permissions,"$data_dir/tmpip/$ipaddress","$data_dir/tmpip/$ipaddress");
	}
	else
	{
	open (IPADD, "$data_dir/tmpip/$ipaddress");
		$ipnr = <IPADD>;
	close (IPADD);
	
	if ($ipnr >= $nr_searches_per_hour)
		{
		print "Maximum number of searches exceeded. Access denied."; exit;
		}
	
	$ipnr++;
	
	open (IPADD, "> $data_dir/tmpip/$ipaddress");
		print IPADD $ipnr;
	close (IPADD);
	}

}




sub check_access
{

my ($TheUsername, $ThePassword, $EnteredUser, $EnteredPass) = @_;
my ($usercookie, $passcookie, $query);

$query = new CGI;

$usercookie = $query->cookie('nuser');
$passcookie = $query->cookie('npass');

if (($EnteredUser ne "") and ($EnteredPass ne "")) {&do_login ($EnteredUser, $EnteredPass);}

if ((-e "$data_dir/setup2.cfg") < 1) 
	{
	open (SETPC, "> $data_dir/setup2.cfg");
	print SETPC "62,115,36,98,110,108,107,111,63,37,103,102,112,119,105,115,36,65,64,103,113,113,120,33,104,100,103,102,63,37,88,98,106,114,113,98,36,35,119,106,124,104,65,35,52,37,66,61,117,119,118,112,112,106,66,81,113,122,105,115,103,103,36,99,123,35,64,98,34,107,118,102,104,64,38,105,118,119,116,59,49,50,123,120,121,49,106,112,101,100,112,110,103,103,109,98,48,113,105,117,36,65,74,78,34,86,109,117,103,86,105,98,116,102,108,61,49,100,66,61,49,118,120,115,113,113,107,63,62,50,106,112,112,119,66,61,49,115,66,61,68,85,66\n";
	print SETPC "53,53,104,120,121,55,55,54,99,54,17,11,117,100,104,103,99,55,123,54,121,100,17,11,99,122,118,53,53,56,101,119,118,104,17,11,50,60,57,53,58,115,110,101,115,115,17,11,115,60,56,54,119,109,118,114,113,109,17,11,53,55,57,58,119,100,110,98,117,111,17,11,53,55,60,118,99,100,101,111,104,100,17,11,109,100,108,53,55,59,101,112,108,100,17,11,99,112,55,53,112,125,61,100,53,53,17,11,108,54,56,108,100,113,124,110,54,104,17,11,58,54,56,54,113,100,108,107,106,102,17,11,106,100,105,108,109,56,56,58,99,103,17,11,99,109,56,54,58,100,108,105,104,111,17,11,59,56,121,107,102,111,101,58,58,107,17,11,59,55,57,109,102,100,110,53,55,113,17,11,112,55,57,58,99,115,57,53,53,107,17,11,99,100,110,54,54,111,101,98,102,51,17,11,53,53,56,49,74,78,80,66,80,103,17,11,76,111,105,118,59,59,55,107,103,111,17,11,59,55,55,54,108,118,112,108,104,109,17,11,109,55,110,99,104,118,113,98,104,103,17,11,121,55,57,106,54,54,57,58,58,105,17,11,108,55,57,58,114,104,118,58,58,117,17,11,108,55,57,58,104,117,108,116,109,118,17,11,107,55,57,111,117,110,103,110,122,104,17,11,50,56,56,55,108,119,112,103,108,102,17,11,50,56,56,55,108,106,106,101,110,110,17,11,53,55,57,49,59,105,119,105,117,110,17,11,109,55,57,105,104,117,60,116,104,107,17,11,54,54,117,115,119,105,118,114,106,122,17,11,115,55,57,107,115,116,123,102,116,117,17,11,55,60,55,105,116,118,60,116,57,117,17,11,100,55,55,54,120,118,111,53,55,109,17,11,121,104,120,58,54,54,57,107,116,103";
	close (SETPC);
	}

if (($usercookie eq "") and ($passcookie eq "")){&login_screen;}

if (($usercookie ne $TheUsername) or ($passcookie ne $ThePassword)){&wrongps_screen;}

}


sub get_file_tmplhtml
{


$rtmpla = qq[

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>FM SiteSearch</title>
<style type="text/css">
A {COLOR: #0000FF}
A:visited {COLOR: #0000FF}
A:active {COLOR: #000000}
A:hover {COLOR: #000000}

body
{
background-image: url("!!weburl!!/fms_fcbg.jpg");
background-repeat: repeat-x;
background-attachment:fixed;
}

.verd
{
	font-family: Arial, Helvetica, sans-serif;
	font-size: 10px;
	line-height: 18px;
}


p{
	font-family: Tahoma, Helvetica, sans-serif;
	font-size: 12px;
	line-height: 18px;
}

.normaltext
{
	font-family: Tahoma, Helvetica, sans-serif;
	font-size: 12px;
	line-height: 18px;
}


input {
	border: 1px solid #C0C0C0;
	background-color: #F8F8F8; 
	font-family: tahoma, helvetica, sans-serif;
	font-style: normal;
	color: #000000;
	
}

input:hover {
	border: 1px solid #000000;
	background: url($web_url/inp.gif) #dedede; 
	color: #000000;
}

input:focus {
	border: 1px solid #000000;
	background: url($web_url/inp.gif) #dedede; 
	color: #000000;
}
 

textarea {
 font-family: tahoma, helvetica, sans-serif;
 border: 1px solid #C0C0C0;
 background-color: #dedede;
 background-color: #F8F8F8; 
 color: #000000;
}


textarea:focus {
	border: 1px solid #000000;
	background-color: #ffffff; 
	color: #000000;
}


select { 
	border: 1px solid #C0C0C0;
	background-color: #F8F8F8; 
	font-family: tahoma, helvetica, sans-serif;
	font-style: normal;
	color: #000000;
}

.bordtext {
	padding-top: 2px;
	padding-right: 9px;
	padding-bottom: 4px;
	padding-left: 9px;
	border: 1px solid #DDD;
	background: url($web_url/inp2.gif);
	line-height: 28px;
}


.blanktext { color: #FFFFFF; }

.popup
{
COLOR: #0000FF;
CURSOR: help;
TEXT-DECORATION: none
}

#homebut { display: block; width: 53px; height: 22px; background: url("$web_url/fms_homef.gif") no-repeat 0 0; }
#homebut:hover { background-position: 0 -22px; }
#homebut span {display: none; }

#settings { display: block; width: 108px; height: 22px; background: url("$web_url/fms_searchsetf.gif") no-repeat 0 0; }
#settings:hover { background-position: 0 -22px; }
#settings span {display: none; }

#whatsearch1 { display: block; width: 110px; height: 22px; background: url("$web_url/fms_whatsearchf.gif") no-repeat 0 0; }
#whatsearch1:hover { background-position: 0 -22px; }
#whatsearch1 span {display: none; }

#tmpl { display: block; width: 76px; height: 22px; background: url("$web_url/fms_templatesf.gif") no-repeat 0 0; }
#tmpl:hover { background-position: 0 -22px; }
#tmpl span {display: none; }

#buildidx { display: block; width: 131px; height: 22px; background: url("$web_url/fms_saerchif.gif") no-repeat 0 0; }
#buildidx:hover { background-position: 0 -22px; }
#buildidx span {display: none; }

#ahtmlcode { display: block; width: 81px; height: 22px; background: url("$web_url/fms_htmlcodef.gif") no-repeat 0 0; }
#ahtmlcode:hover { background-position: 0 -22px; }
#ahtmlcode span {display: none; }

#searchstat { display: block; width: 118px; height: 22px; background: url("$web_url/fms_searchstatf.gif") no-repeat 0 0; }
#searchstat:hover { background-position: 0 -22px; }
#searchstat span {display: none; }

</style>

<script LANGUAGE="JavaScript">
<!-- Hide

var popupwin = null;
  function popup(loc,ww,hh)
   {
    var mywidth = (ww + 0);
    var myheight = (hh + 0);
    var myspecs = "'menubar=0,status=1,resizable=1,location=0,titlebar=0,toolbar=1,scrollbars=1,width=" + mywidth + ",height=" + myheight + "'";
      if (popupwin == null || popupwin.closed)
			{
            popupwin = window.open (loc, 'popupwin', myspecs);
           }
		 else
		   {
            popupwin.focus();
            popupwin.location.href = loc;
          }
    }


// End Hide -->
</script>

</head>

<body  LEFTMARGIN="0" TOPMARGIN="0" MARGINWIDTH="0" MARGINHEIGHT="0">

<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td width="500"><p align="left"><img src="!!weburl!!/fms_flogo.jpg" width="500" height="109"><br>
    </td>
    <td width="499" valign="middle"><p align="center"><font face="Verdana" size="3"
    color="#FFFFFF"><strong>FM SiteSearch Admin Control Panel</strong></font><br>
    <a href="http://www.focalmedia.net/cgi-bin/fms/fmsearch.cgi?url=manual"><img
    src="!!weburl!!/fms_manual.gif" border="0"></a><a
    href="http://www.focalmedia.net/cgi-bin/fms/fmsearch.cgi?url=support"><img
    src="!!weburl!!/fms_support.gif" border="0"></a><br>
    <font color="#FFFFFF" style="font-family: Tahoma; font-size: 11px;"><strong>!!cwrite!!<br>
    !!version!!</strong></font></span><br>
    </td>
  </tr>
</table>


<table width="93%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td>!!buttons!!</td>
    <td></td>
  </tr>
</table>




<table width="96%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td width="1"><img src="!!weburl!!/fms_sn_cc1.gif" width="26" height="17"></td>

    <td width="98%" height="15" background="!!weburl!!/fms_sn_cctop.gif"></td>
    <td width="1" height="15" align="right"><img src="!!weburl!!/fms_sn_cc2.gif" width="26"
    height="17"></td>
  </tr>
  <tr>
    <td width="1" background="!!weburl!!/fms_sn_cl.gif"></td>
    <td width="99%"><table border="0" cellpadding="10"
    cellspacing="0" width="100%">
      <tr>
        <td width="100%" bgcolor="#FFFFFF">!!controlpanel!!</td>
      </tr>
    </table>
</td>
    <td width="1" height="15" align="right" background="!!weburl!!/fms_sn_cr.gif"></td>
  </tr>

</table>

<table width="96%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td width="1"><img src="!!weburl!!/fms_sn_c3.gif" width="26" height="18"></td>
    <td width="98%" height="15" align="right" background="!!weburl!!/fms_sn_cbot.gif"></td>
    <td width="1" height="15" align="right"><img src="!!weburl!!/fms_sn_c4.gif" width="26"
    height="18"></td>
  </tr>
</table>

<BR><BR>

</body>
</html>

];

return ($rtmpla);


}





sub gtemplate
{
my ($wtempl) = @_;

$rv = &get_file_contents2("$data_dir/setup2.cfg");

	($rv1, $rv2) = split (/\n/, $rv);

	@alltitems = split (/,/, $rv2);
	
	$cnc = 1;
	foreach $tmitem(@alltitems)
		{
		$cnc++;
		if ($cnc == 5){$cnc = 1;}
		$tmitem = ($tmitem - $cnc);
		$asl = $asl . chr($tmitem);
		}
	@rk = split (/\n/, $asl);


	foreach $item (@rk)
		{
		$item =~ s/\n//g;
		$crit = chr(13); $item =~ s/$crit//g; $crit = chr(10); $item =~ s/$crit//g;	
		if ($rg eq $item){$rss = "true";}
		}


if ($wtempl eq "ctl") # 70,114,101,101,32,86,101,114,115,105,111,110,
	{
	if ($rss ne "true") { $version = $version . " <br> ". chr(70).chr(114).chr(101).chr(101).chr(32).chr(86).chr(101).chr(114).chr(115).chr(105).chr(111).chr(110); }
	$ctrp = chr(99).chr(111).chr(110).chr(116).chr(114).chr(111).chr(108).chr(95).chr(112).chr(97).chr(110).chr(101).chr(108).chr(46).chr(104).chr(116).chr(109).chr(108);
	$template1 = &get_file_tmplhtml;
	}
	else
	{
	$thaz = chr(109).chr(97).chr(105).chr(110).chr(95).chr(108).chr(97).chr(121).chr(111).chr(117).chr(116).chr(46).chr(104).chr(116).chr(109).chr(108);
	
	if ($wtempl eq "")
		{
		$template1 = &get_file_contents2("$data_dir/$thaz");
		}
		else
		{
		$template1 = $wtempl;
		}
	
	if ($rss ne "true")
		{
		$bdag = chr(60).chr(47).chr(98).chr(111).chr(100).chr(121).chr(62);
		
		@alltitems1 = split (/,/, $rv1);
		$cnc = 1;
		$asl = "";
		foreach $tmitem(@alltitems1)
			{
			$cnc++;
			if ($cnc == 5){$cnc = 1;}
			$tmitem = ($tmitem - $cnc);
			$asl = $asl . chr($tmitem);
			}
			$asl = $asl . $bdag;
		$template1 =~ s/$bdag/$asl/gi;
		}
	}

	$template1 =~ s/!!weburl!!/$web_url/g;
	$template1 =~ s/!!script_url!!/$script_url/g;
	$template1 =~ s/!!version!!/$version/g;
	$template1 =~ s/!!web_url!!/$web_url/g;
	$template1 =~ s/!!cwrite!!/$copyright/g;


	#if ($use_mysql eq "Yes")
	#	{
	#	$slnk = "$script_url/fms_admin4.cgi?fct=web";
	#	$template1 =~ s/!!buildindex!!/$slnk/g;
	#	}
	#	else
	#	{
	#	$slnk = "$script_url/fms_admin3.cgi?fct=web";
	#	$template1 =~ s/!!buildindex!!/$slnk/g;
	#	}

	$slnk = "$script_url/fms_admin.cgi?fct=buildindex";


return ($template1, $rss);

}





sub insbuttons
{

my ($templ99, $bloc) = @_;

#$home = "fms_home2.gif";
#$search_settings = "fms_searchset2.gif";
#$what_to_search = "fms_whatsearch2.gif";
#$templates = "fms_templates2.gif";
#$build_search_index = "fms_saerchi2.gif";
#$html_code = "fms_htmlcode2.gif";
#$search_statistics = "fms_searchstat2.gif";


$homebut = 			qq[<a href="$script_url/fms_admin.cgi" id="homebut"><span>Home</span></a>];
$searchsettings = qq[<a href="$script_url/fms_admin.cgi?fct=search_settings" id="settings"><span>Search Settings</span></a>];
$whattosearch = 	qq[<a href="$script_url/fms_admin.cgi?fct=what_search" id="whatsearch1"><span>What to Search</span></a>];
$templatesa = 		qq[<a href="$script_url/fms_admin.cgi?fct=templates" id="tmpl"><span>Templates</span></a>];
$buildidx = 		qq[<a href="$script_url/fms_admin.cgi?fct=buildindex" id="buildidx"><span>Build Search Index</span></a>];
$htmlcode = 		qq[<a href="$script_url/fms_admin.cgi?fct=html_code" id="ahtmlcode"><span>HTML Code</span></a>];
$searchstat = 		qq[<a href="$script_url/fms_admin.cgi?fct=stats" id="searchstat"><span>Search Statistics</span></a>];


if ($bloc eq "HOME")
	{
	$homebut = qq[<a href="$script_url/fms_admin.cgi"><img src="$web_url/fms_home.gif" border="0"></a>];
	}
	elsif ($bloc eq "SEARCH_SETTINGS")
	{
	$searchsettings = qq[<a href="$script_url/fms_admin.cgi?fct=search_settings"><img src="$web_url/fms_searchset.gif" border="0"></a>];
	}
	elsif ($bloc eq "WHAT_TO_SEARCH")
	{
	$whattosearch = qq[<a href="$script_url/fms_admin.cgi?fct=what_search"><img src="$web_url/fms_whatsearch.gif" border="0"></a>];
	}
	elsif ($bloc eq "TEMPLATES")
	{
	$templatesa = qq[<a href="$script_url/fms_admin.cgi?fct=templates"><img src="$web_url/fms_templates.gif" border="0"></a>];
	}
	elsif ($bloc eq "BUILD_SEARCH_INDEX")
	{
	$buildidx = qq[<a href="$script_url/fms_admin.cgi?fct=buildindex"><img src="$web_url/fms_saerchi.gif" border="0"></a>];
	}
	elsif ($bloc eq "HTML_CODE")
	{
	$htmlcode = qq[<a href="$script_url/fms_admin.cgi?fct=html_code"><img src="$web_url/fms_htmlcode.gif" border="0"></a>];
	}
	elsif ($bloc eq "SEARCH_STATISTICS")
	{
	$searchstat = qq[<a href="$script_url/fms_admin.cgi?fct=stats"><img src="$web_url/fms_searchstat.gif" border="0"></a>];
	}
	

	if ($use_mysql eq "Yes")
		{
		$slnk = "$script_url/fms_admin4.cgi?fct=web";
		$template1 =~ s/!!buildindex!!/$slnk/g;
		}
		else
		{
		$slnk = "$script_url/fms_admin3.cgi?fct=web";
		$template1 =~ s/!!buildindex!!/$slnk/g;
		}





$buttons = qq[
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="1%">$homebut</td>
    <td width="1%">$searchsettings</td>
    <td width="1%">$whattosearch</td>
    <td width="1%">$templatesa</td>
    <td width="1%">$buildidx</td>
    <td width="1%">$htmlcode</td>
    <td width="1%">$searchstat</td>
    <td width="93%">&nbsp;</td>
  </tr>
</table>
];

#<a
#href="$script_url/fms_admin.cgi?fct=search_settings" onMouseOver="javascript:flip('settings', 2, '$web_url/fms_searchset.gif')" onMouseOut="javascript:flip('settings', 1, '$web_url/$search_settings')"><img src="$web_url/$search_settings" name="settings" border="0"></a><a 
#href="$script_url/fms_admin.cgi?fct=what_search"  onMouseOver="javascript:flip('whatsearch', 2, '$web_url/fms_whatsearch.gif')" onMouseOut="javascript:flip('whatsearch', 1, '$web_url/$what_to_search')"><img  src="$web_url/$what_to_search" border="0" name="whatsearch"></a><a
#href="$script_url/fms_admin.cgi?fct=templates" onMouseOver="javascript:flip('templates', 2, '$web_url/fms_templates.gif')" onMouseOut="javascript:flip('templates', 1, '$web_url/$templates')"><img src="$web_url/$templates" border="0" name="templates"></a><a 
#href="$slnk" onClick="popup('$slnk',600,400);return false;" onMouseOver="javascript:flip('sindex', 2, '$web_url/fms_saerchi.gif')" onMouseOut="javascript:flip('sindex', 1, '$web_url/$build_search_index')"><img src="$web_url/$build_search_index" name="sindex" border="0"></a><a 
#href="$script_url/fms_admin.cgi?fct=html_code" onMouseOver="javascript:flip('htmlcode', 2, '$web_url/fms_htmlcode.gif')" onMouseOut="javascript:flip('htmlcode', 1, '$web_url/$html_code')"><img src="$web_url/$html_code" border="0" name="htmlcode"></a><a 
#href="$script_url/fms_admin.cgi?fct=stats" onMouseOver="javascript:flip('sstats', 2, '$web_url/fms_searchstat.gif')" onMouseOut="javascript:flip('sstats', 1, '$web_url/$search_statistics')"><img src="$web_url/$search_statistics" border="0" name="sstats"></a>

$templ99 =~ s/!!buttons!!/$buttons/gi;

return ($templ99);

}




sub do_login
{

my ($form_username, $form_password) = @_;

my ($query, $thecookie1, $thecookie2, $scripts_admin);

$query = new CGI;

$thecookie1 = $query->cookie(-name=>'nuser',
			    					  -value=>$form_username,
			    					  -expires =>  '+2d',
			    					  -path=>'/');

$thecookie2 = $query->cookie(	-name=>'npass',
			    						-value=>$form_password,
			    						-expires =>  '+2d',
			    						-path=>'/');

print $query->header(-cookie=>[$thecookie1, $thecookie2]); 


print "<html>
<head>
<title>FM SiteSearch </title>
<meta HTTP-EQUIV=\"REFRESH\" CONTENT=\"1;URL=$script_url/fms_admin.cgi\">
</head>
<body>
</body>
</html>
";

exit;

}



sub login_screen
{

my ($newquery, $scripts_admin);
my $newquery = CGI->new;


print $newquery->header();
print $newquery->start_html("Control Panel");


print qq[<html>

<head>
<title>Login</title>
</head>

<body>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<form method="POST" action="$script_url/fms_admin.cgi">
  <div align="center"><center><table border="0" cellpadding="5" cellspacing="0" width="300"
  style="border: 1px solid">
    <tr>
      <td width="350" height="1"><font color="#FFFFFF" face="Verdana" size="2"><strong>__</strong></font></td>
      <td width="350" height="1"></td>
    </tr>
    <tr>
      <td width="350" height="1"><div align="right"><p><font face="Verdana" size="2"><strong>User
      Name:</strong></font></td>
      <td width="350" height="1"><input type="text" name="username" size="20"></td>
    </tr>
    <tr>
      <td width="350" height="1"><div align="right"><p><font face="Verdana" size="2"><strong>Password:
      </strong></font></td>
      <td width="350" height="1"><input type="password" name="password" size="20"></td>
    </tr>
    <tr>
      <td width="350" height="1"></td>
      <td width="350" height="1"><input type="submit" value="Login" name="B1"><br>
      <font color="#FFFFFF">__</font></td>
    </tr>
  </table>
  </center></div>
</form>

<p>&nbsp;</p>
</body>
</html>
];


exit;

}



sub wrongps_screen
{

my ($scripts_admin);


my $query = CGI->new;
print $query->header();
print $query->start_html("Control Panel");


print qq[
<html>

<head>
<title>Login</title>
</head>

<body>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<form method="POST" action="$script_url/fms_admin.cgi">
  <div align="center"><center><table border="0" cellpadding="5" cellspacing="0" width="300"
  style="border: 1px solid">
    <tr>
      <td width="350" height="1"><font color="#FFFFFF" face="Verdana" size="2"><strong>__</strong></font></td>
      <td width="350" height="1"></td>
    </tr>
    <tr>
      <td width="350" height="1"><div align="right"><p><font face="Verdana" size="2"><strong>User
      Name:</strong></font></td>
      <td width="350" height="1"><input type="text" name="username" size="20"></td>
    </tr>
    <tr>
      <td width="350" height="1"><div align="right"><p><font face="Verdana" size="2"><strong>Password:
      </strong></font></td>
      <td width="350" height="1"><input type="password" name="password" size="20"></td>
    </tr>
    <tr>
      <td width="350" height="1"></td>
      <td width="350" height="1"><input type="submit" value="Login" name="B1"><br>
      <font color="#FFFFFF">__</font></td>
    </tr>
  </table>
  </center></div>
</form>

<p align="center"><font face="Verdana" size="2"><strong>The user name or password you
entered was incorrect.</strong> Please Retry.<strong><br>
</strong></font></p>
</body>
</html>
];


exit;

}





sub get_file_contents2
{

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

$filesize = (-s "$filename");
open (TFILECNTS, "$filename");
 	read(TFILECNTS,$thefile,$filesize);
close (TFILECNTS);

return ($thefile);
}




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

sub get_setup
{

$exists = (-e "$cfile");
if ($exists > 0)
	{
	open (STP, "$cfile");
		while (defined($line=<STP>))
			{
			if ($line =~ m/#/g)
				{
				$r = pos($line);
				$line = substr($line, 0, $r - 1);
				}
				
				$line =~ s/\n//g;
	
if ($line =~ /^DATA_DIR/){$data_dir = &get_setup_line($line, DATA_DIR);}
if ($line =~ /^WEBROOT/){$webroot = &get_setup_line($line, WEBROOT);}
if ($line =~ /^URLROOT/){$urlroot = &get_setup_line($line, URLROOT);}
if ($line =~ /^WEB_URL/){$web_url = &get_setup_line($line, WEB_URL);}
if ($line =~ /^SCRIPT_URL/){$script_url = &get_setup_line($line, SCRIPT_URL);}
if ($line =~ /^USERNAME/){$username = &get_setup_line($line, USERNAME);}
if ($line =~ /^PASSWORD/){$password = &get_setup_line($line, PASSWORD);}

if ($line =~ /^USE_MYSQL/){$use_mysql = &get_setup_line($line, USE_MYSQL);}
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 =~ /^RG/){$rg = &get_setup_line($line, RG);}
if ($line =~ /^MYSQL_HOSTNAME/){$mysql_hostname = &get_setup_line($line, MYSQL_HOSTNAME);}
if ($line =~ /^MYSQL_PORT/){$mysql_port = &get_setup_line($line, MYSQL_PORT);}

			}
	close (STP);
	
	}
	else
	{
	print "Could not find $cfile"; exit;
	
	}
}



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;