#!/usr/bin/perl

package 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.cfg";
#$cfile = "e:/full/server/path/to/config.cfg"; 
###########################################################################


$version = "FM SiteSearch Pro Version 3.2";

$prgname = "FM SiteSearch";
$copyright = "Copyright ©2004 FocalMedia.Net - All Rights Reserved";

@ISA = qw(Exporter);

@EXPORT = qw(check_access 
				 gtemplate); 

@EXPORT_OK = qw($tmp
					$template1);


&get_setup;




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,105,115,111,118,35,106,98,101,104,65,35,88,104,118,101,99,113,101,35,34,118,109,123,103,64,38,50,36,65,17,11,62,115,36,98,110,108,107,111,63,37,103,102,112,119,105,115,36,65,87,102,99,117,103,105,34,85,105,116,119,111,120,116,34,83,115,120,103,117,105,101,34,101,125,33,62,100,17,11,106,117,105,103,63,37,108,117,118,115,62,48,49,122,123,120,48,105,115,100,99,111,113,102,102,108,101,47,112,104,120,48,101,106,109,46,100,108,114,48,104,112,119,48,104,112,119,102,99,117,103,105,48,102,107,106,65,120,118,109,63,105,113,35,15,13,115,111,111,114,121,116,103,114,122,102,116,64,38,120,107,113,104,112,121,49,119,117,99,119,121,116,63,42,108,117,118,115,62,48,49,105,113,116,107,119,105,116,103,100,118,100,106,115,118,112,48,105,115,100,99,111,113,102,102,108,101,47,112,104,120,40,61,117,105,117,119,117,114,33,118,117,121,102,61,37,66,71,79,35,87,106,118,104,87,102,99,117,103,105,34,83,118,112,62,50,101,63,34,48,36,68,113,115,125,115,107,106,108,117,34,73,115,100,99,111,81,102,102,108,101,47,80,104,120,61,49,105,115,111,118,65,64,48,114,65\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 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")
	{
	if ($rss ne "true") { $version = $version . " - ". chr(85).chr(110).chr(114).chr(101).chr(103).chr(105).chr(115).chr(116).chr(101).chr(114).chr(101).chr(100); }
	$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_contents2("$data_dir/$ctrp");
	}
	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);
	$template1 = &get_file_contents2("$data_dir/$thaz");
	
	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;

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

}





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 Pro</title>
<meta HTTP-EQUIV=\"REFRESH\" CONTENT=\"1;URL=$script_url/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 "<p>&nbsp;</p>
<div align=\"center\"><center>

<table border=\"0\" cellpadding=\"2\" cellspacing=\"0\" width=\"600\">
  <tr>
    <td bgcolor=\"#000000\" height=\"2\" width=\"981\"></td>
  </tr>
  <tr>
    <td bgcolor=\"#808080\" width=\"550\"><strong><font face=\"Verdana\"><u><small><small><p
    align=\"left\"></small></small></u></font><font face=\"Verdana\" color=\"#ffffff\" size=\"2\">$prgname</font><font
    color=\"#ffffff\" size=\"4\" face=\"Verdana\"><br>
    </font></strong><font face=\"Verdana\" size=\"1\" color=\"#FFFFFF\">Please Login</font></td>
  </tr>
  <tr>
    <td bgcolor=\"#000000\" height=\"2\" width=\"981\"></td>
  </tr>
</table>
</center></div>

<form method=\"POST\" action=\"$script_url/admin.cgi\">
  <div align=\"center\"><center><table border=\"0\" cellspacing=\"1\" width=\"600\"
  bgcolor=\"#000000\" cellpadding=\"3\">
    <tr>
      <td bgcolor=\"#8080FF\" width=\"143\"><font face=\"Verdana\" size=\"2\" color=\"#FFFFFF\"><strong>User
      Name:</strong></font></td>
      <td bgcolor=\"#FFD017\" width=\"449\"><input type=\"text\" name=\"username\" size=\"20\"></td>
    </tr>
    <tr>
      <td bgcolor=\"#8080FF\" width=\"143\"><font face=\"Verdana\" size=\"2\" color=\"#FFFFFF\"><strong>Password:</strong></font></td>
      <td bgcolor=\"#FFD017\" width=\"449\"><input type=\"password\" name=\"password\" size=\"20\"></td>
    </tr>
    <tr>
      <td bgcolor=\"#8080FF\" width=\"143\"><font face=\"Verdana\" size=\"2\"><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      </strong></font></td>
      <td bgcolor=\"#FFD017\" width=\"449\"><input type=\"submit\" value=\"   Login   \" name=\"B1\"></td>
    </tr>
  </table>
  </center></div>
</form>
<font color=\"#ffffff\" face=\"Verdana\" size=\"1\">

<p align=\"center\"></font><font face=\"Verdana\" size=\"1\" color=\"#000000\">$copyright</font></p>

</body>
</html>";


exit;

}



sub wrongps_screen
{

my ($scripts_admin);


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


print "<p>&nbsp;</p>
<div align=\"center\"><center>

<table border=\"0\" cellpadding=\"2\" cellspacing=\"0\" width=\"600\">
  <tr>
    <td bgcolor=\"#000000\" height=\"2\" width=\"981\"></td>
  </tr>
  <tr>
    <td bgcolor=\"#808080\" width=\"550\"><strong><font face=\"Verdana\"><u><small><small><p
    align=\"left\"></small></small></u></font><font face=\"Verdana\" color=\"#ffffff\" size=\"2\">$prgname</font><font
    color=\"#ffffff\" size=\"4\" face=\"Verdana\"><br>
    </font></strong><font face=\"Verdana\" size=\"1\" color=\"#FFFFFF\">Please Login</font></td>
  </tr>
  <tr>
    <td bgcolor=\"#000000\" height=\"2\" width=\"981\"></td>
  </tr>
</table>
</center></div>

<form method=\"POST\" action=\"$script_url/admin.cgi\">
  <div align=\"center\"><center><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"600\">
    <tr>
      <td height=\"2\" width=\"981\"><font face=\"Verdana\" size=\"2\"><strong>The user name or password
      you entered was incorrect.</strong> Please Retry.<strong><br>
      </strong>&nbsp;&nbsp; </font></td>
    </tr>
  </table>
  </center></div><div align=\"center\"><center><table border=\"0\" cellspacing=\"1\" width=\"600\"
  bgcolor=\"#000000\" cellpadding=\"3\">
    <tr>
      <td bgcolor=\"#8080FF\" width=\"143\"><font face=\"Verdana\" size=\"2\" color=\"#FFFFFF\"><strong>User
      Name:</strong></font></td>
      <td bgcolor=\"#FFD017\" width=\"449\"><input type=\"text\" name=\"username\" size=\"20\"></td>
    </tr>
    <tr>
      <td bgcolor=\"#8080FF\" width=\"143\"><font face=\"Verdana\" size=\"2\" color=\"#FFFFFF\"><strong>Password:</strong></font></td>
      <td bgcolor=\"#FFD017\" width=\"449\"><input type=\"password\" name=\"password\" size=\"20\"></td>
    </tr>
    <tr>
      <td bgcolor=\"#8080FF\" width=\"143\"><font face=\"Verdana\" size=\"2\"><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      </strong></font></td>
      <td bgcolor=\"#FFD017\" width=\"449\"><input type=\"submit\" value=\"   Login   \" name=\"B1\"></td>
    </tr>
  </table>
  </center></div>
</form>
<font color=\"#ffffff\" face=\"Verdana\" size=\"1\">

<p align=\"center\"></font><font face=\"Verdana\" size=\"1\" color=\"#400040\">$copyright</font></p>
</body>
</html>";


exit;

}








sub get_setup
{

$csize = (-s "$cfile");
open (RVF, "$cfile");
	read(RVF,$data_dir,$csize);
close (RVF);

$estr = ""; $dta = "";
for ($ms = 0; $ms < length($data_dir); $ms++)
	{
	$ch = substr($data_dir, $ms, 1);
	$estr = ord($ch); $estr = $estr - 5;
	$dta = $dta . chr($estr);
	}
	$data_dir = $dta;
	
$data_dir =~ s/\n//g; $crit = chr(13); $crit =~ s/$crit//g; $crit = chr(10); $crit =~ s/$crit//g;	


$exists = (-e "$data_dir/setup.cfg");
if ($exists > 0)
	{
	
	open (STP, "$data_dir/setup.cfg");
		while (defined($line=<STP>))
			{
			if ($line =~ m/#/g)
				{
				$r = pos($line);
				$line = substr($line, 0, $r - 1);
				}
				
				$line =~ s/\n//g;
	

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);}

			}
	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);
}





sub get_file_contents2
{

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

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

return ($thefile);
}



1;