#!/usr/bin/perl

package acs;

########################################################################
# COPYRIGHT NOTICE:
#
# Copyright 2008 FocalMedia.Net All Rights Reserved.
#
# Selling the code for this program without prior written consent 
# from FocalMedia.Net is expressly forbidden. You may not 
# redistribute this program in any shape or form.
# 
# This program is distributed "as is" and without warranty of any
# kind, either express or implied. In no event shall the liability 
# of FocalMedia.Net for any damages, losses and/or causes of action 
# exceed the total amount paid by the user for this software.
#
########################################################################

#### EDIT HERE -- FOR WINDOWS/IIS BASED INSTALLATIONS ONLY #######

$config_cgi = "../config.cgi"; ## <-- CHANGE THIS LINE TO THE FULL SERVER PATH TO config.cgi

# THE PATH ON A WINDOWS INSTALLATION WILL LOOK SOMETHING LIKE THIS:
# $config_cgi = "c:/inetpub/webpub/cgi-bin/pseek/config.cgi";

#### DO NOT CHANGE ANYTHING BELOW THIS LINE #################

#use FindBin;
#use lib $FindBin::Bin;
use CGI::Carp qw(fatalsToBrowser);
use CGI;
use Time::Local;
use DBI;
use IO::Socket;
use MIME::Base64 qw(decode_base64 encode_base64);
use obbuild;

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

@ISA = qw(Exporter);

@EXPORT = qw(check_access
	    		get_template
	    		encode_dir
	    		decode_dir
	    		problem_popup
	    		get_chopped_path
	    		remove_leading_spacing
	    		update_numbers
	    		get_location_string
	    		get_file_contents
	    		get_last_dirname
	    		get_rid
	    		decode_complete_dir
	    		get_extra_vars
	    		decode_date
	    		extend_login
	    		update_setting
	    		htmlencode
	    		get_heading_box
	    		write_config
	    		psmakedir
	    		);

@EXPORT_OK = qw($template1
					 $enc_string
					 $decoded_string
					 $chopped_path
					 $chopped_string
					 $locstr_ret
					 $thefile
					 $drval
					 $mcid_str
					 $decstr
					 $extrakfd
					 $rdvalue
					 $str2
					 $h_return
					 $dir_to_create);

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

$version = "PowerSeek SQL Version 4.0.2 Beta";
$prgname = "PowerSeek Version 4.0.2 Beta";
$copyright = "Copyright 2003-$year FocalMedia.Net";

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


&get_setup;

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



sub write_config
{
my ($conf_item, $conf_value) = @_;

open (GSETT, "$data_dir/gsettings.dat");
	@conflines = <GSETT>;
close (GSETT);


foreach $ccitem (@conflines)
	{
	($obj, $ident) = split (/=/, $ccitem);
	
	if ($obj eq $conf_item)
		{
		$newfile = $newfile . $conf_item . "=" . $conf_value . "\n";
		}
		else
		{
		$newfile = $newfile . $ccitem;
		}
	}


open (GSETT, "> $data_dir/gsettings.dat");
	print GSETT $newfile;
close (GSETT);

}




sub update_cat_numbers
{

my ($cid_id, $link_number_counter) = @_;

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

$sql = "SELECT cid, l1, l2, l3, l4, l5, l6, l7, l8, l9, l10, l11, l12, l13, l14, l15, l16, shortname
					FROM dirs WHERE cid = '$cid_id'";
$sth = $dbh->prepare($sql);
$sth->execute;
$serror = ""; $serror = $sth->errstr; if ($serror ne "") {die "SQL Syntax Error: $serror";}

$nrcounter = 0;
while ( @row = $sth->fetchrow() )
	{
		$cats = "";
		for ($ms = 1; $ms < 17; $ms++) 
				{
				if ($row[$ms] ne "")
					{
					$cats = $cats . $row[$ms] . "::-::";
					}
				} 
		$cats = substr($cats, 0, length($cats) - 5);
	} 

@to_update_cat = split (/::-::/, $cats);

$lvc = 1;
foreach $catlevel (@to_update_cat)
	{
	$catlevel = $dbh->quote($catlevel);
	
	$levelsql = $levelsql . " (l" . $lvc . " = $catlevel) AND";
	
	$use_levelsql = $levelsql . " (l" . ($lvc + 1) . " = '')";
	
	$sql = "UPDATE dirs SET linknrs = linknrs + $link_number_counter WHERE $use_levelsql";
	
	$sth = $dbh->prepare($sql);
	$sth->execute;
	$serror = ""; $serror = $sth->errstr; if ($serror ne "") {die "SQL Syntax Error: $serror";} 
	
	$lvc++;
	}

if (defined($sth)){$sth->finish; } 
$dbh->disconnect;

}


sub quick_help
{
my ($help_heading, $help_text) = @_;

$helpcrit = qq[<table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#E1E1E1">
  <tr>
    <td width="1%" valign="top" bgcolor="#FFFFFF"><p><img src="$web_url/ic_kb.gif" width="55"/></p>    </td>
    <td width="99%" valign="top" bgcolor="#FFFFFF"><table width="100%" border="0" cellpadding="6">
      <tr>
        <td background="$web_url/bar6.gif"><strong class="normaltext">$help_heading</strong></td>
        </tr>
      <tr>
        <td class="normaltext">$help_text</td>
        </tr>
    </table></td>
  </tr>
</table>
<table border="0" cellpadding="0" cellspacing="0" width="100%" background="$web_url/shadow.gif">
  <tr><td width="1%" bgcolor="#FFFFFF"><font color="#FFFFFF">_</font></td><td width="99%"></td></tr>
</table>

];

return ($helpcrit);

}




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/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/shadow.gif">
  <tr><td width="1%" bgcolor="#FFFFFF"><font color="#FFFFFF">_</font></td><td width="99%"></td></tr>
</table>


];

return ($h_return);


}



sub fill_vars
{

my ($filledvar) = @_;

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

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


####
if ($statsetting =~ /dyn_stat=Dynamic/)
	{
	$filledvar =~ s/!!whatsnew!!/$script_url\/dirs\.cgi?newpop=new/g;
	$filledvar =~ s/!!tophits!!/$script_url\/dirs\.cgi?newpop=pop/g;
	}
	else
	{
	$filledvar =~ s/!!whatsnew!!/$web_url\/new\//g;
	$filledvar =~ s/!!tophits!!/$web_url\/pop\//g;
	}

$filledvar =~ s/!!modify_link_url!!/$script_url\/modify\.cgi/g;
$filledvar =~ s/!!add_url_link!!/$script_url\/addurl\.cgi/g;
$filledvar =~ s/!!add_premium_url_link!!/$script_url\/addurl\.cgi?p=1/g;
$filledvar =~ s/!!rgs_login!!/$script_url\/rgs_login\.cgi?p=1/g;


$topvar = &get_file_contents("$data_dir/frontlink.dat");
if ($topvar eq ""){$filledvar =~ s/!!top!!/$script_url\/fp\.cgi/gi;}else{$filledvar =~ s/!!top!!/$topvar/gi;}


$filledvar =~ s/!!favorites_link!!/$script_url\/fav\.cgi/g;

$filledvar =~ s/!!register!!/$script_url\/rgs_register\.cgi?p=1/g;
$filledvar =~ s/!!log_out!!/$script_url\/rgs_logout\.cgi/g;
$filledvar =~ s/!!advanced!!/$script_url\/searchadv\.cgi/g;
$filledvar =~ s/!!my_links!!/$script_url\/rgs_login\.cgi/g;
$filledvar =~ s/!!account_details!!/$script_url\/rgs_adet\.cgi/g;

$filledvar =~ s/!!retrieve_password!!/$script_url\/rgs_repass\.cgi/g;
$filledvar =~ s/!!resend_activation!!/$script_url\/rgs_resend2\.cgi/g;


$query = new CGI;
$searched_keywords = $query->param('keywords');
$filledvar =~ s/!!keywords!!/$searched_keywords/g;


return ($filledvar);
} 




sub update_setting
{
my ($keyname, $keyval) = @_;

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

foreach $item (@allitems)
	{
	($sobj, $sval) = split (/=/, $item);
	
	if ($sobj eq $keyname)
		{
		$the_setting = $the_setting . "$sobj=$keyval\n";
		}
		else
		{
		$the_setting = $the_setting . $item . "\n";
		}
	
	}

open (PSETTINGS, "> $data_dir/gsettings.dat");
print PSETTINGS $the_setting;
close (PSETTINGS);

}



sub get_extra_vars
{

my ($keydata1, @akeys1, $knr1, $kcount1, $k_item1, 
    $k_keyname1, $k_visible1, $k_size1, $k_searched1, $k_description1, $k_image1, $extrakfd);

$keydata1 = &get_file_contents("$data_dir/keys.dat");
@akeys1 = split (/\n/, $keydata1);
$knr1 = @akeys1;

if ($knr1 > 0)
	{
	$kcount1 = 0;
	foreach $k_item1 (@akeys1)
		{
		($k_keyname1, $k_visible1, $k_size1, $k_searched1, $k_description1, $k_image1, $tmp, $nmbup) = split (/\t/, $k_item1);
		
		if ($nmbup ne "Upload")
			{
			$extrakfd = $extrakfd . "%%$k_keyname1%%<br><br>";
			$kcount1++;
			}
		}
	} 



return ($extrakfd);
}




sub get_cid_cat
{

my ($dstring) = @_;

my (@alldirs, $lvcount, $ccats, $item44, $readycat, $sqlstr, $sql, $cidret);

@alldirs = split (/\//, $dstring);

$lvcount = 1;
$ccats = "";
foreach $item44 (@alldirs)
	{
	$item44 =~ s/'/\\'/g;
	
	$ccats = $ccats . "(l" . $lvcount . " = '$item44') AND ";
	
	$readycat = substr($ccats, 0, length($ccats) - 4);
	
	$sqlstr = $sqlstr . "($readycat AND (level = '$lvcount')) OR";
	
	$lvcount++;
	}

$sqlstr = substr($sqlstr, 0, length($sqlstr) - 2);

$sql = "SELECT cid FROM dirs WHERE $sqlstr ORDER BY level";

#print "$sql \n\n";

### CONNECT TO DB
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"; }
 
$sth = $dbh->prepare($sql);
$sth->execute;
$serror = ""; $serror = $sth->errstr; if ($serror ne "") {die "SQL Syntax Error: $serror\n From: $sql";}

$cidret = "";
while ( @row = $sth->fetchrow() )
	{
	$cidret = $cidret . $row[0] . "-";
	}

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

$cidret = substr($cidret, 0, length($cidret) - 1);

return ($cidret);

}


sub decode_complete_dir
{

my ($complete_dir) = @_;
my (@dits, $dritem, $decstr);

@dits = split (/-/, $complete_dir);
	
$decstr = "Root ";
	
	foreach $dritem (@dits)
		{
		$decstr = $decstr . " > " . &decode_dir($dritem);
		}

return ($decstr);
}


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

sub get_rid
{
my ($ecat) = @_;

@dcats = split (/-/, $ecat);

$gcntr = 0;
$lvcnt = 1;
foreach $item (@dcats)
	{
	$item = &decode_dir($item);
	$item =~ s/'/\\'/g;
	
	$fieldnames[$gcntr] = "l" . $lvcnt;
	$fieldvalues[$gcntr] = $item;
		
		$fncount = 0;
		$fieldsel = "";
		foreach $field_name_item (@fieldnames)
			{
			$fieldsel = $fieldsel . " ($field_name_item = '$fieldvalues[$fncount]') and";
			$fncount++;
			}
		$fieldsel = substr($fieldsel, 0, length($fieldsel) -3);
	
	$qstring = $qstring . " ((level = '$lvcnt') and $fieldsel) or";
	
	$lvcnt++;
	$gcntr++;
	}
	$qstring = substr($qstring, 0, length($qstring) - 2);

$qstring = "SELECT cid,level FROM dirs WHERE $qstring ORDER BY level ASC";

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

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

$serror = ""; $serror = $sth->errstr; if ($serror ne "") {die "SQL Syntax Error: $serror";}

$mcid_str = "";
$updc = 0;
while ( @row = $sth->fetchrow() )
	{
	$mcid_str = $mcid_str . "$row[0]" . "-";
	}

$mcid_str = substr($mcid_str, 0, length($mcid_str) -1);


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


return ($mcid_str);

}





sub get_last_dirname
{

my ($complete_dir) = @_;

@allds = split (/-/, $complete_dir);
$dnr = @allds; $dnr = $dnr - 1;
$drval = $allds[$dnr];
$drval = &decode_dir($drval);

return ($drval);
}




sub new_gsettings
{
my $this = {};

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

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

bless $this;
return $this;
}



sub get_file_contents
{

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

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

return ($thefile);

}




sub update_numbers
{

my ($plus_minus, $nr, $ctg) = @_;

my (@allds, $item, $qsel);

$nowtime = time();

@allds = split (/-/, $ctg);

$qc = 0;
$lvl = 1;

$qstring = "SELECT cid, level, linknrs FROM dirs WHERE ";

foreach $item (@allds)
	{
	$qstring = $qstring . "(cid = '$item') or ";
	}

$qstring = substr ($qstring, 0, length($qstring) - 3);
$qstring = $qstring . " ORDER BY level";

#print "$qstring <br>";

########

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



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

$updc = 0;
while ( @row = $sth->fetchrow() )
	{
	
	if ($plus_minus eq "+")
		{
		$updatetime = ",newindate = '$nowtime'";
		$newc = $row[2] + $nr;
		}
		else
		{
		$newc = $row[2] - $nr;
		}

	$update_arr[$updc] = "$row[0]-$row[1]-$newc";
	$updc++;
	
	#print "=> $row[0]-$row[1]-$newc <br>";
	}

foreach $item (@update_arr)
	{
	($icid, $ilv, $link_count) = split (/-/, $item);

$sth = $dbh->prepare("UPDATE dirs SET 	linknrs = '$link_count' 
													$updatetime
													WHERE cid = '$icid'");
if ( !defined $dbh ) {die "Cannot connect to mSQL server: $DBI::errstr\n"; }
$sth->execute;
	}
	
$sth->finish;
$dbh->disconnect;

}






sub get_location_string
{

my ($lcstring) = @_;
my (@als, $item);

@als = split (/-/, $lcstring);

$locstr_ret = "Root";
foreach $item (@als)
	{
	$locstr_ret = $locstr_ret . " > " . &decode_dir($item);
	}

return ($locstr_ret);
}





sub remove_leading_spacing
{
my ($ostring) = @_;
my ($ds, $wfound);

for ($ds = 0; $ds < length($ostring); $ds++) 
	{
	if ((substr($ostring, length($ostring) - 1, 1) eq " ") and ($wfound ne "true"))
		{
		chop($ostring);
		}
		else
		{
		$wfound = "true";
		}
	
	}

$chopped_string = $ostring;

return ($chopped_string);
}



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

sub get_chopped_path
{

my ($fpath) = @_;

$chopped_path = "";

@alldi = split (/\//, $fpath);
$inr = @alldi;
$inr = $inr -1;

for ($ds = 0; $ds < $inr; $ds++) 
	{
	if ($ds != 0)
		{
		$chopped_path = $chopped_path . "/$alldi[$ds]";
		}
		else
		{
		$chopped_path = $chopped_path . "$alldi[$ds]";
		}
	
	}

return ($chopped_path);
}

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



sub check_access
{

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

if ($TheUsername =~ /'/) { &wrongps_screen; } if ($TheUsername =~ /\\/) { &wrongps_screen; }
if ($ThePassword =~ /'/) { &wrongps_screen; } if ($ThePassword =~ /\\/) { &wrongps_screen; }
if ($EnteredUser =~ /'/) { &wrongps_screen; } if ($EnteredUser =~ /\\/) { &wrongps_screen; }
if ($EnteredPass =~ /'/) { &wrongps_screen; } if ($EnteredPass =~ /\\/) { &wrongps_screen; }

if (-e "$web_dir/$db_name.sql")
	{
	$tmp = unlink("$web_dir/$db_name.sql");
	}


if (($username ne "") and ($password ne "")) ### IF CONTROL PANEL USER/PASS ENABLED
{

$query = new CGI;

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

#print "Content-type: text/html\n\n";
#print "==> $usercookie $passcookie";

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

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

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


}

}




sub extend_login
{

$query = new CGI;

$thecookie1 = $query->cookie(-name=>'nuser',
			    					  -value=>$username,
			    					  -expires=>'+24h',
			    					  -path=>'/');

$thecookie2 = $query->cookie(	-name=>'npass',
			    						-value=>$password,
			    						-expires=>'+24h',
			    						-path=>'/');

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

}




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=>'+24h',
			    					  -path=>'/');

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

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

#print "Content-type: text/html\n\n";

print "<html>
<head>
<title>Powerseek</title>
<meta HTTP-EQUIV=\"REFRESH\" CONTENT=\"0;URL=$admin_url/admin.cgi\">
</head>
<body>
</body>
</html>
";

exit;

}




sub login_screen
{

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


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


print <<END_OF_LOGIN;

<html>

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

<body>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<form method="POST" action="$admin_url/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 face="Verdana" size="2"><strong><font color="#FFFFFF">__</font></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>


END_OF_LOGIN

exit;

}





sub wrongps_screen
{

my ($scripts_admin);


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


print <<END_OF_WRONGPS;

<html>

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

<body>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<form method="POST" action="$admin_url/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" face="Verdana" size="2"><strong>__</strong></font></td>
    </tr>
  </table>
  </center></div><div align="center"><center><p><strong><font face="Verdana" size="2">The
  user name or password you entered was incorrect.</font></strong></p>
  </center></div>
</form>
</body>
</html>

END_OF_WRONGPS


exit;

}





sub get_template
{

my ($menu) = @_;

$navb = "navb.gif";
$b_home = "b_home.gif";
$b_dman = "b_dman.gif";
$b_userm = "b_userm.gif";
$b_settings = "b_settings.gif";
$b_tmplm = "b_tmplm.gif";
$b_plugin = "plug.gif";
$b_todo = "todoy.gif";
$b_userm = "userman1.gif";

$search_boxes = qq[

<form method="POST" action="$admin_url/admin11.cgi" name="searchform" target="_blank">
      <strong class="verd">Search Links:</strong><br>
      <input type="text" name="keywords" size="15" style="font-family: MS Sans Serif; font-size: 8pt"> 
      <input type="submit" value="Go" name="B1" style="font-family: MS Sans Serif; font-size: 8" onClick="submitsearch(document.searchform); return false;">
</form>

    <form method="POST" action="$admin_url/admin69.cgi" name="searchform1" target="_blank">
      <strong class="verd">Search Categories:</strong><br>
      <input type="text" name="keywords" size="15" style="font-family: MS Sans Serif; font-size: 8pt"> 
      <input type="submit" value="Go" name="B1" style="font-family: MS Sans Serif; font-size: 8" onClick="submitsearch(document.searchform1); return false;">
    </form>    
    
    <form method="POST" action="$script_url/!!!search!!!" target="_blank">
      <strong class="verd">End User Search:</strong><br>
      <input type="text" name="keywords" size="15" style="font-family: MS Sans Serif; font-size: 8pt"> 
      <input type="submit" value="Go" name="B1" style="font-family: MS Sans Serif; font-size: 8">
    </form>
<p><span class="verd"><strong>Admin Functions</strong><br>
    <strong>
    <img src="$web_url/lary.gif" width="6" height="10"><a href="$admin_url/admin11.cgi?fct=advanced" onClick="popup('$admin_url/admin11.cgi?fct=advanced',screen.width-200,screen.height-200);return false;">Advanced Search</a><br>
    <img src="$web_url/lary.gif" width="6" height="10"><a href="$admin_url/admin3.cgi" onClick="popup('$admin_url/admin3.cgi',screen.width-200,screen.height-200);return false;">Stats + Tree</a><br>
    <img src="$web_url/lary.gif" width="6" height="10"><a href="$admin_url/admin8.cgi" onClick="popup('$admin_url/admin8.cgi',screen.width-200,screen.height-200);return false;">Generate HTML Files</a> <br>
    <img src="$web_url/lary.gif" width="6" height="10"><a href="$admin_url/admin26.cgi" onClick="popup('$admin_url/admin26.cgi',screen.width-200,screen.height-200);return false;">Generate Front Page</a> <br>
    <img src="$web_url/lary.gif" width="6" height="10"><a href="$admin_url/admin20.cgi" onClick="popup('$admin_url/admin20.cgi',screen.width-200,screen.height-200);return false;">New Submissions</a> <br>
    <img src="$web_url/lary.gif" width="6" height="10"><a href="$admin_url/admin14.cgi" onClick="popup('$admin_url/admin14.cgi',screen.width-200,screen.height-200);return false;">New Reviews</a><br>
    <img src="$web_url/lary.gif" width="6" height="10"><a href="$admin_url/admin18.cgi" onClick="popup('$admin_url/admin18.cgi',screen.width-200,screen.height-200);return false;">Modified Links</a><br>
    <img src="$web_url/lary.gif" width="6" height="10"><a href="$admin_url/admin44.cgi" onClick="popup('$admin_url/admin44.cgi',screen.width-200,screen.height-200);return false;">Broken Links</a><br>
    </strong>
];


$navbar_links = <<END_OF_LL;



  
  &nbsp;&nbsp;&nbsp; <br>
  <strong>Quick Links</strong><br>
    <img src="$web_url/lary.gif" width="6" height="10"><a href="$admin_url/admin29.cgi" onClick="popup('$admin_url/admin29.cgi',screen.width-200,screen.height-200);return false;">Banner Manager</a><br>
    <img src="$web_url/lary.gif" width="6" height="10"><a href="$admin_url/admin32.cgi" onClick="popup('$admin_url/admin32.cgi',screen.width-200,screen.height-200);return false;">Search Stats</a><br>
    <img src="$web_url/lary.gif" width="6" height="10"><a href="$admin_url/admin23.cgi" onClick="popup('$admin_url/admin23.cgi',screen.width-200,screen.height-200);return false;">Premium Links Mngr</a><br>
    <img src="$web_url/lary.gif" width="6" height="10"><a href="$admin_url/admin17.cgi" onClick="popup('$admin_url/admin17.cgi',screen.width-200,screen.height-200);return false;">Template Includes</a><br>
    <img src="$web_url/lary.gif" width="6" height="10"><a href="$admin_url/admin27.cgi" onClick="popup('$admin_url/admin27.cgi',screen.width-200,screen.height-200);return false;">Custom Templates</a></span><span class="verd"><br>
    <img src="$web_url/lary.gif" width="6" height="10"><a href="$admin_url/admin33.cgi" onClick="popup('$admin_url/admin33.cgi',screen.width-200,screen.height-200);return false;">Mailing Options</a></span><br>
    <span class="verd"><img src="$web_url/lary.gif" width="6" height="10"><a href="$admin_url/admin43.cgi" onClick="popup('$admin_url/admin43.cgi',800,600);return false;">Database Backup</a></span><br>
  <br>
  
END_OF_LL



    


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



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

if ($menu eq "HOME")
	{



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


	$sth = $dbh->prepare("SELECT COUNT(*) FROM links");
	$sth->execute;
	$serror = ""; $serror = $sth->errstr; if ($serror ne "") {die "SQL Syntax Error: $serror";}
	while ( @row = $sth->fetchrow() ){$links = $row[0];}

	$sth = $dbh->prepare("SELECT COUNT(*) FROM dirs");
	$sth->execute;
	$serror = ""; $serror = $sth->errstr; if ($serror ne "") {die "SQL Syntax Error: $serror";}
	while ( @row = $sth->fetchrow() ){$dirs = $row[0];}
	
	$sth->finish;
	$dbh->disconnect;

	$b_home = "b_home2.gif";


### CHECK IF WE HAVE THE PPC PLUGIN


$ppc_plug = $config_cgi;
$ppc_plug =~ s/config\.cgi//gi;
$ppc_plug = $ppc_plug . "ppc_internal.pm"; 

if ((-e "$ppc_plug") > 0) 
	{
	$ppc_link = qq[<table border="0" cellpadding="4" cellspacing="0" width="100%" style="border: 1px solid rgb(0,0,0)">
  <tr>
    <td bgcolor="#FFFFFF"><p align="center"><a href="$admin_url/p_internal_ppc.cgi"><strong><font
    face="Verdana" size="1">Switch To<br>
    PowerSeek PPC <br>
    Admin Panel</font></strong></a></td>
  </tr>
</table><BR>];
	}

$subt = <<END_OF_H;

<table border="0" width="100%" cellpadding="0" cellspacing="0">
  <tr>
    <td bgcolor="#FFE135" valign="top" width="1%" background="$web_url/stripl.gif"><table width="100%" border="0" cellpadding="6">
      <tr>
        <td><p>$ppc_link<span class="verd"><strong>Number of Dirs:<br>
          </strong>$dirs<br>
          <strong>&nbsp; <br>
            </strong></font><strong>Number of Links:<br>
              </strong>$links</span></p>
          <p><img src="$web_url/lary.gif" width="6" height="10"> <span class="verd"><strong><a href="$admin_url/admin.cgi?fct=logoff">Logout</a><br>
            
            <img src="$web_url/lary.gif" width="6" height="10"> <a href="$admin_url/admin3.cgi"
            onClick="popup('$admin_url/admin3.cgi',800,600);return false;">Statistics</a><br>
              </strong></span></p>
          <p>%%inst%%</p>
          <p><br>
          </p></td>
      </tr>
    </table>
      <p><img src="$web_url/sep1556.gif" alt=""></p>
    </td>
    <td valign="top" width="99%"><table width="100%" border="0" cellpadding="6">
      <tr>
        <td>!!data!!</td>
      </tr>
    </table></td>
  </tr>
</table>





END_OF_H
	
	}
elsif ($menu eq "DIR_MANAGER")
	{

$subt = qq[
<table border="0" width="100%" cellpadding="0" cellspacing="0">
  <tr>
    <td width="1%" valign="top" background="$web_url/stripl.gif"><table border="0"
    cellpadding="6" cellspacing="0" width="100%">
      <tr>
        <td width="100%">
        $search_boxes
        $navbar_links</td>
      </tr>
    </table>
    <p><img src="$web_url/sep1556.gif"></td>
    <td width="99%" valign="top"><table border="0" cellpadding="6" cellspacing="0"
    width="100%">
      <tr>
        <td width="100%">!!data!!</td>
      </tr>
    </table>
    </td>
  </tr>
</table>];


#$subt = <<END_OF_SB;
#<table border="0" width="100%" cellpadding="6" cellspacing="0">
#  <tr>
#    <td width="975" bgcolor="#FFFFFF" valign="top">!!data!!</td>
#  </tr>
#</table>
#END_OF_SB


	$b_dman = "b_dman2.gif";
	}
elsif ($menu eq "SETTINGS")
	{

$subt = <<END_OF_SB;

<table border="0" width="100%" cellpadding="0" cellspacing="0">
  <tr>
    <td width="1%" valign="top" background="$web_url/stripl.gif"><table border="0"
    cellpadding="6" cellspacing="0" width="100%">
      <tr>
        <td width="100%">
        $search_boxes
        $navbar_links</td>
      </tr>
    </table>
    <p><img src="$web_url/sep1556.gif"></td>
    <td width="99%" valign="top"><table border="0" cellpadding="6" cellspacing="0"
    width="100%">
      <tr>
        <td width="100%">!!data!!</td>
      </tr>
    </table>
    </td>
  </tr>
</table>

END_OF_SB

	$b_settings = "b_settings2.gif";
	}
elsif ($menu eq "TEMPLATES")
	{

$subt = <<END_OF_SB;

<table border="0" width="100%" cellpadding="0" cellspacing="0">
  <tr>
    <td width="1%" valign="top" background="$web_url/stripl.gif"><table border="0"
    cellpadding="6" cellspacing="0" width="100%">
      <tr>
        <td width="100%">
        $search_boxes
        
        <p><span class="verd"><b>Quick Links:</b><BR><img src="$web_url/lary.gif" width="6" height="10"><a
		  href="$admin_url/admin26.cgi"
		  onClick="popup('$admin_url/admin26.cgi',screen.width-200,screen.height-200);return false;">Configure
		  Front Page</a><br>
		  <img src="$web_url/lary.gif" width="6" height="10"><a href="$admin_url/admin17.cgi"
		  onClick="popup('$admin_url/admin17.cgi',screen.width-200,screen.height-200);return false;">Template
		  Includes</a><br>
		  <img src="$web_url/lary.gif" width="6" height="10"><a href="$admin_url/admin27.cgi"
		  onClick="popup('$admin_url/admin27.cgi',screen.width-200,screen.height-200);return false;">Custom
		  Templates</a><br>
		  <img src="$web_url/lary.gif" width="6" height="10"><a
		  href="$admin_url/admin78.cgi?fct=global_vars"
		  onClick="popup('$admin_url/admin78.cgi?fct=global_vars',screen.width-200,screen.height-200);return false;">Global
		  Variables</a><br>
		  <img src="$web_url/lary.gif" width="6" height="10"><a
		  href="$admin_url/admin78.cgi?fct=how_to_edit2"
		  onClick="popup('$admin_url/admin78.cgi?fct=how_to_edit2',screen.width-200,screen.height-200);return false;">How
		  to Edit Templates</a><br>
		  <span class="verd"><img src="$web_url/lary.gif" width="6" height="10"></span><a
		  href="$admin_url/admin48.cgi"
		  onClick="popup('$admin_url/admin48.cgi',screen.width-200,screen.height-200);return false;">Category
		  Includes</a><br>
		  <span class="verd"><img src="$web_url/lary.gif" width="6" height="10"></span><a
		  href="$admin_url/p_tmptool.cgi"
		  onClick="popup('$admin_url/p_tmptool.cgi',screen.width-200,screen.height-200);return false;">Template
		  Switch Tool</a><br>
		  <span class="verd"><img src="$web_url/lary.gif" width="6" height="10"></span><a
		  href="$admin_url/p_tmpglobal.cgi"
		  onClick="popup('$admin_url/p_tmpglobal.cgi',screen.width-200,screen.height-200);return false;">Import
		  Template</a>
</span></p>
        
        </td>
      </tr>
    </table>
    <p><img src="$web_url/sep1556.gif"></td>
    <td width="99%" valign="top"><table border="0" cellpadding="6" cellspacing="0"
    width="100%">
      <tr>
        <td width="100%">!!data!!</td>
      </tr>
    </table>
    </td>
  </tr>
</table>

END_OF_SB
	
	$b_tmplm = "b_tmplm2.gif";
	}
elsif ($menu eq "PLUGINS")
	{
	
$subt= <<END_OF_PL;

<table border="0" width="100%" cellpadding="0" cellspacing="0">
  <tr>
    <td width="1%" valign="top" background="$web_url/stripl.gif"><table border="0"
    cellpadding="6" cellspacing="0" width="100%">
      <tr>
        <td width="100%">
        $search_boxes
        $navbar_links</td>
      </tr>
    </table>
    <p><img src="$web_url/sep1556.gif"></td>
    <td width="99%" valign="top"><table border="0" cellpadding="6" cellspacing="0"
    width="100%">
      <tr>
        <td width="100%">!!data!!</td>
      </tr>
    </table>
    </td>
  </tr>
</table>

END_OF_PL
	$b_plugin = "plug2.gif";
	}
elsif ($menu eq "TODO")
	{
$subt= <<TODO;

<table border="0" width="100%" cellpadding="0" cellspacing="0">
  <tr>
    <td width="1%" valign="top" background="$web_url/stripl.gif"><table border="0"
    cellpadding="6" cellspacing="0" width="100%">
      <tr>
        <td width="100%">
        $search_boxes
        $navbar_links</td>
      </tr>
    </table>
    <p><img src="$web_url/sep1556.gif"></td>
    <td width="99%" valign="top"><table border="0" cellpadding="6" cellspacing="0"
    width="100%">
      <tr>
        <td width="100%">!!data!!</td>
      </tr>
    </table>
    </td>
  </tr>
</table>

TODO
$b_todo = "todob.gif";
	}
elsif ($menu eq "USERMAN")
	{

$subt= qq[

<table border="0" width="100%" cellpadding="0" cellspacing="0">
  <tr>
    <td width="1%" valign="top" background="$web_url/stripl.gif"><table border="0"
    cellpadding="6" cellspacing="0" width="100%">
      <tr>
        <td width="100%">
        $search_boxes
        $navbar_links</td>
      </tr>
    </table>
    <p><img src="$web_url/sep1556.gif"></td>
    <td width="99%" valign="top"><table border="0" cellpadding="6" cellspacing="0"
    width="100%">
      <tr>
        <td width="100%">!!data!!</td>
      </tr>
    </table>
    </td>
  </tr>
</table>

];

$b_userm = "userman2.gif";
	
	}	

@plugins = (72,114,103,98,110,80,105,101,107,100,50,79,103,119,36,46,34,63,102,63,87,113,118,102,105,108,119,117,103,117,105,101,62,50,102,63);
@plugins2 = (119,113,112,112,101,110,50,100,105,108);
@plugins3 = (34,73,115,100,99,111,81,102,102,108,101,47,80,104,120);
obbuild::get_setupa_line($config_cgi);

$cnc = 1;
foreach $tmitem(@plugins)
	{
	$cnc++;
	if ($cnc == 5){$cnc = 1;}
	$tmitem = ($tmitem - $cnc);
	$uplug = $uplug . chr($tmitem);
	}
$cnc = 1;
foreach $tmitem(@plugins3)
	{
	$cnc++;
	if ($cnc == 5){$cnc = 1;}
	$tmitem = ($tmitem - $cnc);
	$uplug2 = $uplug2 . chr($tmitem);
	}
$cnc = 1;
foreach $tmitem(@plugins2)
	{
	$cnc++;
	if ($cnc == 5){$cnc = 1;}
	$tmitem = ($tmitem - $cnc);
	$fplug = $fplug . chr($tmitem);
	}


#if ((-e "$fplug") < 1) {$cpanel =~ s/$uplug2/$uplug/i;}

#$cpanel = &get_file_contents("$data_dir/control_panel2.html");

$cpanel = &get_cpanel;



$cpanel =~ s/!!web_url!!/$web_url/gi;
$cpanel =~ s/!!admin_url!!/$admin_url/gi;



if ($config_cgi ne "../config.cgi")
{
$nlck = $config_cgi;
$nlck =~ s/config\.cgi/admin\/unlock\.cgi/gi;
}
else
{
$nlck = "unlock.cgi";
}


if ((-e "$nlck") < 1){$version = $version . "<BR>Unlicensed Trial Version";}
$cpanel =~ s/!!version!!/$version/gi;


$cpanel =~ s/!!copyright!!/$copyright/gi;
$cpanel =~ s/!!subt!!/$subt/gi;

$cpanel =~ s/!navb!/$navb/g;
$cpanel =~ s/!b_home!/$b_home/g;
$cpanel =~ s/!b_dman!/$b_dman/g;
$cpanel =~ s/!b_plugin!/$b_plugin/g;
$cpanel =~ s/!b_settings!/$b_settings/g;
$cpanel =~ s/!b_tmplm!/$b_tmplm/g;
$cpanel =~ s/!b_todo!/$b_todo/g;
$cpanel =~ s/!b_userm!/$b_userm/g;

return ($cpanel);

}






sub encode_dir
{

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

$fstring =~ s/ /_/g;

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

return ($enc_string);

}






sub decode_dir
{

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

$coded_string =~ s/_/ /g;

$plusval = 0;

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

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

	}

return($decoded_string);
}




sub problem_popup
{

my ($problem_text) = @_;

print <<END_OF_PR;

<html>
<head>
<title>Problem</title>
<link href="$web_url/ps.css" rel="stylesheet" type="text/css" /> 
</head>
<body>
<table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#E1E1E1">
  <tr>
    <td width="1%" valign="top" bgcolor="#FFFFFF"><p><img src="$web_url/ic_alert.gif"></p>
    </td>
    <td width="99%" valign="top" bgcolor="#FFFFFF"><table width="100%" border="0" cellpadding="6">
      <tr>
        <td background="$web_url/bar6.gif"><strong class="normaltext">Problem</strong></td>
        </tr>
      <tr>
        <td class="normaltext">$problem_text</td>
        </tr>
      <tr>
        <td class="normaltext"><strong>Use the back button of your browser to go back.</strong></td>
      </tr>
    </table></td>
  </tr>
</table>



</body>
</html>

END_OF_PR

exit;
}





sub decode_date
{

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

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 decode_date_numbers
{

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

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

$rdvalue = "$year/$mon/$mday";

return ($rdvalue);

}







sub send_email
{
my($sendername1, $fromemail1, $toemail1, $mail_subject1, $email_message1) = @_;


if ($mail_method eq "smtp_pm")
	{
	&send_mail_smtp_pm($toemail1, $fromemail1, $sendername1, $mail_subject1, $email_message1, $smtp_server);
	}
elsif ($mail_method eq "smtp")
	{
	&send_mail_smtp($toemail1, $fromemail1, $sendername1, $mail_subject1, $email_message1, $smtp_server);
	}
elsif ($mail_method eq "sendmail")
	{
	&send_mail_sendmail ($sendername1, $fromemail1, $toemail1, $mail_subject1, $email_message1);
	}
elsif ($mail_method eq "smtp_auth")
	{
	&send_mail_smtp_auth($toemail1, $fromemail1, $sendername1, $mail_subject1, $email_message1, $smtp_server, $smtp_user, $smtp_pass);	
	}
	
}





sub send_mail_smtp_auth
{

my ($to_email, $from_email, $from_name, $email_subject, $email_msg, $smtp_server_ss, $e_user, $e_pass) = @_;

$e_user = encode_base64($e_user);
$e_pass = encode_base64($e_pass);

chomp($e_user);
chomp($e_pass);

$remote = IO::Socket::INET->new(
 										Proto => "tcp",
                              PeerAddr  => $smtp_server_ss,
                              PeerPort  => 25
                                 );

        unless ($remote)
        		{
        		print "Connection failed when trying to connect to $smtp_server_ss";
        		$TCPconnect = "False";
        		exit;
        		}

open (MAITH, "> $data_dir/mailauth.log");

if ($TCPconnect ne "False")
	{
	$remote->autoflush(1);
	
	send($remote, "EHLO localhost\r\n", 0);
	recv($remote, $buffer, 200, 0);
	print MAITH "EHLO localhost -> $buffer\n";

	### AUTHENTICATE #####################################################
	
	send($remote, "AUTH LOGIN\r\n", 0);
	recv($remote, $buffer, 200, 0);
	print MAITH "AUTH LOGIN SENT -> $buffer\n";

	send($remote, "$e_user\r\n", 0);
	recv($remote, $buffer, 200, 0);
	print MAITH "$e_user - USER NAME SENT -> $buffer\n";

	send($remote, "$e_pass\r\n", 0);
	recv($remote, $buffer, 200, 0);
	print MAITH "$e_pass - PASS SENT -> $buffer\n";

	### SEND MAIL ########################################################
	
	send($remote, "MAIL From:<$from_email>\r\n", 0);
	recv($remote, $buffer, 200, 0);
	print MAITH "MAIL From [$from_email] -> $buffer\n";

	send($remote, "RCPT To:<$to_email>\r\n", 0);
	recv($remote, $buffer, 200, 0);
	print MAITH "RCPT To: [$to_email] -> $buffer\n";


	send($remote, "DATA\r\n", 0);
	recv($remote, $buffer, 200, 0);
	print MAITH "DATA -> $buffer\n";

	send($remote, "From: $from_name <$from_email>\r\n", 0);
	send($remote, "Subject: $email_subject\r\n", 0);
	
	send($remote, $email_msg, 0); ### SEND MESSAGE

	send($remote, "\r\n.\r\n", 0);
	send($remote, "QUIT\n", 0);
	recv($remote, $buffer, 200, 0);
	print MAITH "QUIT -> $buffer\n\n\n\n\n";
	
close $remote;

	}

close (MAITH);

}





sub send_mail_smtp_pm
{

my ($to_email, $from_email, $from_name, $email_subject, $email_msg, $smtp_server_ss) = @_;

#print "Content-type: text/html\n\n";
#print "--> $to_email <BR>"; exit;

eval('use Net::SMTP; 1;') or acs::problem_popup("The Perl module called Net::SMTP is not installed. Please install it. Perl version $]");

   $smtp = Net::SMTP->new($smtp_server_ss,
			  Hello => $smtp_server_ss,
			  Timeout => 30,
			  Debug   => 0
			  ) or &problem_popup("Could not connect to: $smtp_server_ss - $@");


    $smtp->mail($to_email);
    $smtp->to($to_email);
    
    $smtp->data();
    $smtp->datasend("To: $to_email\n");
    $smtp->datasend("From: $from_name <$from_email>\n");
    $smtp->datasend("Subject: $email_subject\n");
    $smtp->datasend("\n");
    $smtp->datasend("$email_msg\n");
    $smtp->dataend();

    $smtp->quit;    


}





sub send_mail_sendmail
{

# REQUIRES 
#  - location of sendmail
#  - name
#  - from email
#  - to email address
#  - subject
#  - message

my($sendername, $fromemail, $toemail, $mail_subject, $email_message) = @_;
my ($ftext);

$sendername =~ s/\n//g;
$fromemail =~ s/\n//g;
$toemail =~ s/\n//g;
$mail_subject =~ s/\n//g;

### check for attacks

$toemail =~ s/\;//g;
$toemail =~ s/^\s+//g;
$toemail =~ s/\s+$//g;
if ($toemail =~ /^\S+\@\S+$/)
{
$ftext = "<" . "$fromemail" . ">";

open (SENDMAIL, "| $sendmail $toemail");# or &problem_popup("Could not execute $sendmail - $@");
print SENDMAIL <<End_of_Mail;
From: $sendername $ftext
To: $toemail
Reply-To: $fromemail
Subject: $mail_subject
Sender: $fromemail

$email_message

End_of_Mail
}
}






sub send_mail_smtp
{
my ($to_email, $from_email, $from_name, $email_subject, $email_msg, $smtp_server_ss) = @_;
use IO::Socket;

$remote = IO::Socket::INET->new(
 										Proto => "tcp",
                              PeerAddr  => $smtp_server_ss,
                              PeerPort  => 25
                                 );

        unless ($remote)
        		{
        		print "Connection failed when trying to connect to $smtp_server_ss";
        		$TCPconnect = "False";
        		exit;
        		}


if ($TCPconnect ne "False")
	{
	sleep(1);
	$remote->autoflush(1);
	#send($remote, "HELO localhost \n", 0);
	send($remote, "HELO $smtp_server \n", 0);
	recv($remote, $buffer, 200, 0);
	if ($buffer !~ /220/) { $remote_error = $buffer; }
	#print "HELO localhost -> $buffer <Br><Br>";
	
	if ($remote_error eq "")
	{
	send($remote, "MAIL From: <$from_email>\n", 0);
	recv($remote, $buffer, 200, 0);
	if ($buffer !~ /250/) { $remote_error = $buffer; }
	#print "MAIL From [$from_email] -> $buffer <Br><Br>";
	}

	if ($remote_error eq "")
	{
	send($remote, "RCPT To: <$to_email>\n", 0);
	recv($remote, $buffer, 200, 0);
	if ($buffer !~ /250/) { $remote_error = $buffer; }
	#print "RCPT To: [$to_email] -> $buffer <Br><Br>";
	}

	if ($remote_error eq "")
	{
	send($remote, "DATA\n", 0);
	recv($remote, $buffer, 200, 0);
	if ($buffer !~ /250/) { $remote_error = $buffer; }
	#print "DATA -> $buffer <Br><Br>";
	}

	send($remote, "From: $from_name\n", 0);
	send($remote, "Subject: $email_subject\n", 0);

	send($remote, $email_msg, 0);

	send($remote, "\r\n.\r\n", 0);
	send($remote, "QUIT\n", 0);

close $remote;

	}

}




sub send_mail_smtp2
{
my ($to_email, $from_email, $from_name, $email_subject, $email_msg, $smtp_server_ss) = @_;
use IO::Socket;

$remote = IO::Socket::INET->new(
 										Proto => "tcp",
                              PeerAddr  => $smtp_server_ss,
                              PeerPort  => 25
                                 );

        unless ($remote)
        		{
        		print "Connection failed when trying to connect to $smtp_server_ss";
        		$TCPconnect = "False";
        		exit;
        		}

if ($TCPconnect ne "False")
	{
   
   $remote->autoflush(1);

	send($remote, "HELO localhost\n", 0);
	recv($remote, $buffer, 200, 0);
	if ($buffer !~ /220/) { $remote_error = $buffer; }
	#print "HELO localhost -> $buffer <Br><Br>";

	if ($remote_error eq "")
	{
	send($remote, "MAIL From: <$from_email>\n", 0);
	recv($remote, $buffer, 200, 0);
	if ($buffer !~ /250/) { $remote_error = $buffer; }
	#print "MAIL From [$from_email] -> $buffer <Br><Br>";
	}

	if ($remote_error eq "")
	{
	send($remote, "RCPT To: <$to_email>\n", 0);
	recv($remote, $buffer, 200, 0);
	if ($buffer !~ /250/) { $remote_error = $buffer; }
	#print "RCPT To: [$to_email] -> $buffer <Br><Br>";
	}

	if ($remote_error eq "")
	{
	send($remote, "DATA\n", 0);
	recv($remote, $buffer, 200, 0);
	if ($buffer !~ /250/) { $remote_error = $buffer; }
	#print "DATA -> $buffer <Br><Br>";
	}

	send($remote, "From: $from_name\n", 0);
	send($remote, "Subject: $email_subject\n", 0);

	send($remote, $email_msg, 0);

	send($remote, "\r\n.\r\n", 0);
	send($remote, "QUIT\n", 0);

close $remote;

	}

}





sub ts_insert_template_includes
{

my ($tmpl_page) = @_;

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

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


########
$tmpl_settings = &get_file_contents ("$data_dir/gsettings.dat");
@tmpl_sett = split (/\n/, $tmpl_settings);

foreach $titem (@tmpl_sett)
	{
	if ($titem =~ /searchmeth/)
		{
		if ($titem =~ /fulltext/i)
			{
			$search_rpl = "$script_url/search2.cgi";
			}
		}
	
	}

if ($search_rpl eq "") 
	{ 
	$tmpl_page =~ s/!!tseeksearch!!/$script_url\/search\.cgi/g;
	}
	else
	{
	$tmpl_page =~ s/!!tseeksearch!!/$search_rpl/g;
	}
#########





$tmpl_page =~ s/!build_url!/$web_url/g;
$tmpl_page =~ s/!!scripts_url!!/$script_url/g;


#$tmpl_page =~ s/!!tseeksearch!!/$script_url\/search\.cgi/g;

return ($tmpl_page);

}


sub get_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);}
if ($line =~ /^SMTP_USER/){$smtp_user = &get_setup_line2($line, SMTP_USER);}
if ($line =~ /^SMTP_PASS/){$smtp_pass = &get_setup_line2($line, SMTP_PASS);}

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

sub ppc_settings
{
my $this = {};

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

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

bless $this;
return $this;
}



sub get_cpanel
{

$ccptemplate = qq[<html>

<head>
<title>Powerseek</title>
<link href="!!web_url!!/ps.css" rel="stylesheet" type="text/css" />
<!-- Hide !!JSCRIPT!! // End Hide -->
<script LANGUAGE="JavaScript">
<!-- Hide

  function popup(loc,ww,hh)
   {
    var mywidth = (ww + 0);
    var myheight = (hh + 0);

    var winl = (screen.width-ww)/2;
	 var wint = (screen.height-hh)/2;
    
    var myspecs = 'menubar=0,status=1,resizable=1,location=0,titlebar=1,toolbar=1,scrollbars=1,width=' + mywidth + ',height=' + myheight + ',top=' + wint + ',left=' + winl;
	var random = parseInt(Math.random()*10000)

	  var popupwin = null;
      myWindow = open (loc, popupwin + random, myspecs);
      myWindow.moveTo(100,40);
      myWindow.focus();

    }

 function submitsearch(theForm)
   {
     var windowName = "Saerch";
     windowURL = theForm.action + '?';
     windowURL = windowURL + "keywords=" + theForm.keywords.value;
     
	  var wval = screen.width-200;
	  var hval = screen.height-200;

     var winl = (screen.width-wval)/2;
	  var wint = (screen.height-hval)/2;
     
     var myspecs = 'menubar=0,status=1,resizable=1,location=0,titlebar=1,toolbar=1,scrollbars=1,width=' + wval + ',height=' + hval + ',top=' + wint + ',left=' + winl;

     var popupwin = null;
     myWindow = open(windowURL, popupwin, myspecs);
     myWindow.focus();
   }

 // End Hide -->
 </script>
</head>

<body topmargin="0" leftmargin="0" background="$web_url/cpback.gif">


<table border="0" cellpadding="0" cellspacing="0" width="100%">
  <tr>
    <td width="1%" bgcolor="#0000A0"><img src="!!web_url!!/pscp.jpg"></td>
    <td width="99%" bgcolor="#0000A0" valign="top" background="$web_url/psbck.jpg"><p
    align="center"><br>
    <a href="http://www.focalmedia.net/pseekmanual/"><img src="!!web_url!!/manual.gif"
    border="0"></a> <a href="http://www.focalmedia.net/cgi-bin/psmembers/members.cgi"><img
    src="!!web_url!!/support2.gif" border="0"></a><br>
    <font color="#FFFFFF" class="verd">!!copyright!!<strong><br>
    !!version!!</strong></font></td>
  </tr>
</table>

<table border="0" cellpadding="0" cellspacing="0" width="100%">
  <tr>
    <td width="100%" valign="top" bgcolor="#8080C0"><p align="left"><img
    src="!!web_url!!/navb.gif"><a href="!!admin_url!!/admin.cgi"><img
    src="!!web_url!!/!b_home!" border="0"></a><a href="!!admin_url!!/admin2.cgi"><img
    src="!!web_url!!/!b_dman!" border="0"></a><a href="!!admin_url!!/admin10.cgi"><img
    src="!!web_url!!/!b_settings!" border="0"></a><a href="!!admin_url!!/admin55.cgi"><img
    src="!!web_url!!/!b_userm!" border="0"></a><a href="!!admin_url!!/admin6.cgi"><img
    src="!!web_url!!/!b_tmplm!" border="0"></a><a href="!!admin_url!!/plugins.cgi"><img
    src="!!web_url!!/!b_plugin!" border="0"></a><a href="!!admin_url!!/todo.cgi"><img
    src="!!web_url!!/!b_todo!" border="0"></a></td>
  </tr>
</table>

!!subt!!
</body>
</html>

];

return ($ccptemplate);

}


sub psmakedir
{
my ($dir_to_create) = @_;

if ((-e $dir_to_create) < 1) 
	{
	mkdir ($dir_to_create, $default_permissions);
	chmod($default_permissions,$dir_to_create); 
	}

}  



sub htmlencode
{

my ($str1) = @_;

    $str1 =~ s/&/&amp;/g;
    $str1 =~ s/</&lt;/g;
    $str1 =~ s/>/&gt;/g;
    $str1 =~ s/"/&quot;/g;
    #$str1 =~ s/\n/\<BR\>/g;
    
    $str2 = $str1;

return ($str2);
}






#### END CONFIGURATION ########################################################



########################################################
#
# check_access($TheUsername, $ThePassword, $EnteredUser, $EnteredPass);
#		Check if user has access to the admin script	
#			$TheUsername = control panel user name
#			$ThePassword = control panel password
#
# encode_dir($dirname);
#		Encode a directory name/path so that it is URL/language friendly
#
# decode_dir($dirname);
#		Decode an encoded a directory name/path
#
# get_template
# 		Get the control panel template for with the specific area/location
#		of where the user finds himself
#
# problem_popup($problem_text);
#		Display an error message and exit all scripts
#
# get_chopped_path($decoded_path);
#		The first directory name is chopped from the path - The path 
#		has to be in decoded form. Example /Arts/Animation/Gifs
#		results in /Arts/Animation
#
# remove_leading_spacing($directory_name)
#		Removes leading spaces at the end of a string - Usefull
#		when users supply directory names and accidently adds
#		spaces to the end
#
# update_numbers (+/-, $number, $cat_id seperated by -)
#		Updates number of links in directories - Needs to be out of the 
#		scope of a MySQL connection
#
# Root >> ars >> animiation = get_location_string(encoded_string);
#
# get_cid_cat ("/arts/animation/etc");
#		Returns cat ids
#		example: 10/30/4213
#
#
# decode_date($time_value_numerical)
#		Decodes seconds
#
# get_last_dirname($encoded_dir)
#		Pass on /Arts/Animation/Gifs and receive
#		Gifs back
#
# get_rid($encoded_category, $connection_status)
#		Converts category names to id's - Returns a - delimeted list.
#		IN: Arts-Animiated_Gifs,124-Something
#		OUT: 10-31-130
#		
#		if ($connection_status = 'true') then do not make a database
#		connection because one has already been established
#
#  decode_complete_dir (Encoded_dir);
#		
#	send_email ($sendername1, $fromemail1, $toemail1, $mail_subject1, $email_message1);
#
#  (YYYY/MM/DD) = decode_date_numbers(seconds);
#
#  get_extra_vars; Returns an array of extra variables to use.
#
########################################################


1;