#!/usr/bin/perl

########################################################################
# 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.
#
########################################################################

package sn_netbadmin;

use CGI;
#use strict;
use CGI::Carp qw(fatalsToBrowser);
require Exporter;


### WINDOWS USERS EDIT BELOW ##############################################
$config_cgi = "../config.cgi"; ## <-- CHANGE THIS LINE TO THE FULL SERVER PATH TO 
#$config_cgi = "e:/full/server/path/to/config.cgi";
###########################################################################


$version = "SiteNet BBS Ver 3.0";

use vars qw/$exists $data_dir $web_url $scripts_url $scripts_admin $r $line $q $rsts
				@ISA @EXPORT @EXPORT_OK $csize $web_path $sendmail_loc/;


### FILE CONTENTS = get_file_contents (FILENAME);
### DATE = decode_date (SECONDS);
### ($totaltopics, $totalreplies, $totalviews) get_stats (cid, fid, datadir location);
### &errorbox ($the_error_html)
### CATEGORY NAME = get_cat_name(CATEGORY_ID);
### FORUM NAME = get_forum_name(CATEGORY_ID, FORUM_ID);
### Y / N = mod_messages($cid, $fid); $mod_msgs
### main_settings_saved
### $errorhtml = errorbox_main("error");
### SAVEDHTML = settings_saved();

@ISA = qw(Exporter);

@EXPORT = qw(	get_file_contents
					decode_date
					get_stats
					getcc
					check_access
					get_cat_name
					get_forum_name
					mod_messages
					get_cpanel_template
					main_settings_saved
					errorbox
					errorbox_main
					settings_saved
					write_stats
					get_setup_line
					get_newmsg_box); ### FUNCTiONS

@EXPORT_OK = qw(	$thefile
						$rdvalue
						$totaltopics
						$treplies
						$totalviews
						$rrv
						$rcat
						$forumname
						$mod_msgs
						$controlpanel_template
						$return_html
						$errorhtml
						$return_html_saved
						$return_val); ### RETURN VALUES
						
						
$thetime = time();
($sec9,$min9,$hour9,$mday9,$mon9,$year9,$wday9,$ydat9,$isdst9) = localtime($thetime);
$year9 = "20" . substr($year9, 1, 2);



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


$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 =~ /WEB_URL/){$web_url = &get_setup_line($line, WEB_URL);}
		if ($line =~ /SCRIPT_URL/){$scripts_url = &get_setup_line($line, SCRIPT_URL);}
		if ($line =~ /ADMIN_URL/){$scripts_admin = &get_setup_line($line, ADMIN_URL);}
		
		if ($line =~ /FILE_LOCKING/){$flocking = &get_setup_line($line, FILE_LOCKING);}
		if ($line =~ /RSTS/){$rsts = &get_setup_line($line, RSTS);}
		
		if ($line =~ /WEB_DIR/){$web_path = &get_setup_line($line, WEB_DIR);}

		if ($line =~ /SENDMAIL/){$sendmail_loc = &get_setup_line($line, SENDMAIL);}
		if ($line =~ /MAIL_METHOD/){$mail_method = &get_setup_line($line, MAIL_METHOD);}
		if ($line =~ /SMTP_SERVER/){$smtp_server = &get_setup_line($line, SMTP_SERVER);}
	
		if ($line =~ /USERNAME/){$c_username = &get_setup_line($line, USERNAME);}
		if ($line =~ /PASSWORD/){$c_password = &get_setup_line($line, PASSWORD);}
		
		if ($line =~ /DATA_DIR/){$data_dir = &get_setup_line($line, DATA_DIR);}

			}
	close (STP);

	}
	else
	{
	print "Content-type: text/html\n\n";
	print "Could not find config.cgi";
	exit;
	}




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


sub display_errors
{
($display_template, $error_name, $display_error) = @_;


if ($display_template =~ m/\[$error_name\]/g) 
	{
	$startpos = pos($display_template);	
	}
else 
	{
	print "Could not find <b>[$error_name]</b> inside template";
	exit;
	}

##########


if ($display_template =~ m/\[\/$error_name]/g) 
	{
	$endpos = pos($display_template); 
	}
else 
	{
	print "Could not find <b>[$error_name/]</b> inside template";
	exit;
	}

#########

	if ($display_error eq "NODISPLAY") ### DON'T DISPLAY ERROR - JUST REMOVE TAG
	{

		$return_template1 = substr($display_template, 0, $startpos - length($error_name) - 2);
		$return_template2 = substr($display_template, $endpos, length($display_template) - length($endpos));
	
		$tcomplete = $return_template1 . $return_template2;
	}
	else
	{

		$error_msg = substr($display_template, $startpos, $endpos - $startpos - length($error_name)-3 );
		$return_template1 = substr($display_template, 0, $startpos - length($error_name) - 2);
		$return_template2 = substr($display_template, $endpos, length($display_template) - length($endpos));
		
		$tcomplete = $return_template1 . $error_msg . $return_template2;
		
	}


return ($tcomplete);

}




sub click_continue
{

my ($bfct) = @_;
my ($link);

if ($bfct ne "")
	{
	$link = "$bfct";
	}
	else
	{
	$link = "$scripts_admin/sn_admin.cgi";
	}

$hbox = get_heading_box("$web_url/sn_foruman2.gif", "Your Changes Has Been Saved", qq[
<form method="POST" action>
  <p><input type="submit" value="Close This Window" name="B1"
  onClick="self.close();return false" style="font-family: MS Sans Serif; font-size: 8pt"></p>
</form>
]);



print <<END_OF_CC;
<html>
<head>
<title>Saved</title>
<link href="$web_url/sn.css" rel="stylesheet" type="text/css">
</head>
<body>
$hbox
</html>
END_OF_CC

}





sub close_window_continue
{

my ($clsw) = @_;

if ($clsw eq "NO_CLOSE")
	{
	$clsw = "<body>";
	}
	else
	{
	$clsw = "<body onUnload=\"opener.location.reload(true);\">";
	}


$hbox = get_heading_box("$web_url/sn_foruman2.gif", "Settings Saved", qq[
<form method="POST" action>
  <p><input type="submit" value="Close This Window" name="B1"
  onClick="self.close();return false" style="font-family: MS Sans Serif; font-size: 8pt"></p>
</form>

]);


print <<END_OF_CLOSECONT;

<html>
<head>
<title>SiteNet BBS</title>
<link href="$web_url/sn.css" rel="stylesheet" type="text/css">
</head>
$clsw

$hbox

</body>
</html>

END_OF_CLOSECONT

}



sub get_flip_button
{
my ($url, $but1, $but2, $popwidth, $popheight) = @_;
my ($jscr, $blankc);

my @vitems = split (/\./, $but1);

$randnr = rand(1000);

$button_name = $button_name . $vitems[0] . $randnr;


if (($popwidth ne "") and ($popwidth ne "blank"))
	{
	$jscr = qq[onClick="popup('$url',$popwidth,$popheight);return false;"];
	}
	elsif ($popwidth eq "blank")
	{
	$blankc = qq[target="_blank"];
	}

$retval = qq[<a href="$url" 
onMouseOver="javascript:flip('$button_name', 2, '$web_url/$but2')" 
onMouseOut="javascript:flip('$button_name', 1, '$web_url/$but1')"
$jscr $blankc><img src="$web_url/$but1" border="0" name="$button_name"></a>];

return ($retval);

}


sub get_heading_box2
{

my ($h_heading, $h_text) = @_;

$h_return = qq[
<table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#E1E1E1">
  <tr>
    <td width="100%" valign="top" bgcolor="#FFFFFF"><table width="100%" border="0"
    cellpadding="6">
      <tr>
        <td background="$web_url/sn_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%">
  <tr><td width="1%" bgcolor="#FFFFFF"><font color="#FFFFFF" height="20">_</font></td><td width="99%"></td></tr>
</table>
];

return ($h_return);


}



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/sn_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%">
  <tr><td width="1%" bgcolor="#FFFFFF"><font color="#FFFFFF" height="20">_</font></td><td width="99%"></td></tr>
</table>
];

return ($h_return);


}





sub settings_saved
{

my ($return_html_saved);

$return_html_saved = <<END_OF_CC;

<html>
<head>
<title>Settings Saved</title>
</head>
<body>
<table border="0" width="100%" cellspacing="0" cellpadding="3">
  <tr>
    <td width="4%" valign="top"><img src="$web_url/sn_saved.gif"></td>
    <td width="96%" valign="middle"><form method="POST" action>
      <p><strong><font face="Verdana" size="1">Your Changes Has Been Saved</font></strong><br>
      &nbsp; <br>
      <input type="submit" value="    Close Window    " name="B1"
      onClick="self.close();return false" style="font-family: MS Sans Serif; font-size: 8"></p>
    </form>
    </td>
  </tr>
  <tr>
    <td width="100%" valign="top" colspan="2"><hr noshade size="1" color="#EAEAEA">
    </td>
  </tr>
  <tr>
    <td width="100%" valign="top" colspan="2">&nbsp; </td>
  </tr>
</table>
</body>
</html>

END_OF_CC


}



sub get_cpanel_template
{

@templid = (70,114,101,101,32,86,101,114,115,105,111,110);

$vr = "";
foreach $tmid (@templid){$vr = $vr . chr($tmid);}

($idtmpl) = &get_idcount($data_dir);

$exists = (-e "$config_cgi");
if ((-e "plugins.cgi") > 0)
	{
	$psplugin = qq[<a href="$scripts_admin/admin.cgi"><img src="$web_url/sw_ps.gif" border="0"></a>];
	$idtmpl = "";
	}

if ($idtmpl ne ""){$version = $version . " | $vr";;}


$controlpanel_template = qq[

<html>

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

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


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


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

}

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


body
{
background-image:
url("!web_url!/sn_fcbg.jpg");
background-repeat: repeat-x;
background-attachment:
fixed
}
</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=1,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;
	          }
	    }

pic = new Image();
pic2 = new Image();
 function flip(pic, r, tsource)
  {
    if(r == 2) { document[pic].src = tsource; }
    if(r == 1) { document[pic].src = tsource; }
  }


var dropdownstat = ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'];


function xmlhttpPost(strURL, display_area, ddown)
	{
    var xmlHttpReq = false;
    var self = this;

  	 if (dropdownstat[ddown] == 1)
  	 	{
  	 	strURL = '!scripts_admin!/sn_admin8.cgi?fct=blank'; dropdownstat[ddown] = 0;
  	 	}
  	 else if  (dropdownstat[ddown] == 0)
  	 	{
  	 	dropdownstat[ddown] = 1;
  	 	}

    // Mozilla/Safari
    if (window.XMLHttpRequest) { self.xmlHttpReq = new XMLHttpRequest();  }

    // IE
    else if (window.ActiveXObject) {self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");}

    self.xmlHttpReq.open('GET', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	self.xmlHttpReq.onreadystatechange = function()
    	   {
         if (self.xmlHttpReq.readyState == 4)
         		{
         		document.getElementById(display_area).innerHTML  = self.xmlHttpReq.responseText;
         		}
    		}

    // Mozilla/Safari
    if (window.XMLHttpRequest) {self.xmlHttpReq.send(strURL);}

    // IE
    else if (window.ActiveXObject) {self.xmlHttpReq.send();}
	}


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

<body background="!web_url!/sn_fcbg.jpg" topmargin="0" leftmargin="0">
<div align="center"><center>

<table border="0" cellpadding="0" cellspacing="0" width="100%">
  <tr>
    <td width="41%" valign="top"><img src="!web_url!/sn_clogo.jpg"></td>
    <td width="9%" align="center">$psplugin</td>
    <td width="50%"><strong><p align="center"><font color="#FFFFFF"><font size="3"><font
    face="Verdana">SiteNet BBS</font></font><font face="Verdana" size="3"> Admin Control Panel</font></font><font
    color="#000000" face="Verdana" size="3"><br>
    <a href="http://www.focalmedia.net/cgi-bin/snbbs/snbbs.cgi?url=manual"><img
    src="!web_url!/sn_manual.gif" border="0"></a><a
    href="http://www.focalmedia.net/cgi-bin/snbbs/snbbs.cgi?url=support"><img
    src="!web_url!/sn_support.gif" border="0"></a></font><font face="Verdana" size="1" color="#FFFFFF"><BR>Copyright 2002-2008
    FocalMedia.Net<br>
    <strong>!!version!!</strong></font></td>
  </tr>
</table>
</center></div><div align="center"><center>

<table border="0" cellpadding="0" cellspacing="0" width="92%">
  <tr>
    <td width="100%"><a href="!scripts_admin!/sn_admin.cgi"
    onMouseOver="javascript:flip('home', 2, '!web_url!/sn_home2.gif')"
    onMouseOut="javascript:flip('home', 1, '!web_url!/sn_home.gif')"><img
    src="!web_url!/sn_home.gif" border="0" alt="Control Panel Front Door" Name="home"></a><a
    href="!scripts_admin!/sn_admin.cgi?fct=boardmanager"
    onMouseOver="javascript:flip('fman', 2, '!web_url!/sn_foruman_2.gif')"
    onMouseOut="javascript:flip('fman', 1, '!web_url!/sn_foruman.gif')"><img
    src="!web_url!/sn_foruman.gif" name="fman"
    alt="Create, Add and Edit Forums and Categories" border="0"></a><a
    href="!scripts_admin!/sn_admin.cgi?fct=usermanager"
    onMouseOver="javascript:flip('userm', 2, '!web_url!/sn_userman_2.gif')"
    onMouseOut="javascript:flip('userm', 1, '!web_url!/sn_userman.gif')"><img
    src="!web_url!/sn_userman.gif" name="userm" alt="Manage Registered User Accounts"
    border="0"></a><a href="!scripts_admin!/sn_admin4.cgi"
    onMouseOver="javascript:flip('settings', 2, '!web_url!/sn_settings_2.gif')"
    onMouseOut="javascript:flip('settings', 1, '!web_url!/sn_settings.gif')"><img
    src="!web_url!/sn_settings.gif" name="settings" border="0" alt="Global Settings"></a><a
    href="!scripts_admin!/sn_admin5.cgi?fct=templates"
    onMouseOver="javascript:flip('tmpl', 2, '!web_url!/sn_templates_2.gif')"
    onMouseOut="javascript:flip('tmpl', 1, '!web_url!/sn_templates.gif')"><img name="tmpl"
    src="!web_url!/sn_templates.gif" alt="Manage Forum Templates" border="0"></a><a
    href="!scripts_url!/sn_forum.cgi" target="_blank"
    onMouseOver="javascript:flip('gfor', 2, '!web_url!/sn_gotoforums_2.gif')"
    onMouseOut="javascript:flip('gfor', 1, '!web_url!/sn_gotoforums.gif')"><img
    src="!web_url!/sn_gotoforums.gif" name="gfor" alt="Goto Forums" border="0"></a></td>
  </tr>
</table>
</center></div><div align="center"><center>

<table border="0" cellpadding="0" cellspacing="0" width="96%">
  <tr>
    <td width="1"><img src="!web_url!/sn_cc1.gif" width="26" height="17"></td>
    <td width="98%" height="15" background="!web_url!/sn_cctop.gif"></td>
    <td width="1" height="15" align="right"><img src="!web_url!/sn_cc2.gif" width="26"
    height="17"></td>
  </tr>
  <tr>
    <td width="1" background="!web_url!/sn_cl.gif"></td>
    <td width="99%"><div align="center"><center><table border="0" cellpadding="10"
    cellspacing="0" width="100%">
      <tr>
        <td width="100%" bgcolor="#FFFFFF">!!criteria!!</td>
      </tr>
    </table>
    </center></div></td>
    <td width="1" height="15" align="right" background="!web_url!/sn_cr.gif"></td>
  </tr>
</table>
</center></div><div align="center"><center>

<table border="0" cellpadding="0" cellspacing="0" width="96%">
  <tr>
    <td width="1"><img src="!web_url!/sn_c3.gif" width="26" height="18"></td>
    <td width="98%" height="15" align="right" background="!web_url!/sn_cbot.gif"></td>
    <td width="1" height="15" align="right"><img src="!web_url!/sn_c4.gif" width="26"
    height="18"></td>
  </tr>
</table>
</center></div>
</body>
</html>

];




$controlpanel_template =~ s/!web_url!/$web_url/g;
$controlpanel_template =~ s/!!year!!/$year9/g;



$controlpanel_template =~ s/!scripts_admin!/$scripts_admin/g;
$controlpanel_template =~ s/!scripts_url!/$scripts_url/g;

$controlpanel_template =~ s/!!version!!/$version/g;

return ($controlpanel_template);

}




sub main_settings_saved
{

my ($return_lnk_name, $return_lnk) = @_;

my ($return_html,$return_lnkdetails);




if ($return_lnk_name ne "")
	{
	$return_lnkdetails = "| <a href=\"$return_lnk\">$return_lnk_name</a>";
	}


$return_html = get_heading_box("$web_url/sn_foruman2.gif", "Your Changes Has Been Applied", qq[
<a href="$scripts_admin/sn_admin.cgi">Home</a> $return_lnkdetails
]);

return ($return_html);

}



sub mod_messages
{
my ($ccid, $ffid) = @_;
my ($mod_msgs, $mdata);

$mdata = &get_file_contents("$data_dir/$ccid/$ffid/moderate.dat");
if (length($mdata) > 1)
	{
	$mod_msgs = "Y"; 
	}
	else
	{
	$mod_msgs = "N";
	}

return ($mod_msgs);
}




sub get_forum_name
{

### GET FORUM NAME VIA ID FROM CATEGORY & FORUM

my ($ccid, $ffid) = @_;
my ($fline, $lfid, $rfname, $forumname);

open (DCT, "$data_dir/$ccid/forumtable.dat");
	while (defined($fline=<DCT>))
		{
		($lfid, $rfname) = split(/\t/,$fline,2);
		if ($lfid == $ffid){$forumname = $rfname;}
		}
close (DCT);

return ($forumname);

}




sub get_cat_name
{

### GET CATEGORY NAME VIA ID FROM CATEGORY TABLE

my ($ccid) = @_;
my ($cid, $ccatname, $cline, $rcat);

open (DCT, "$data_dir/sn_boardtable.dat");
	while (defined($cline=<DCT>))
		{
		($cid, $ccatname) = split(/\t/,$cline,2);
		if ($cid == $ccid){$rcat = $ccatname;}
		}
close (DCT);

return ($rcat);

}




sub get_file_contents
{

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

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

return ($thefile);
}



sub decode_date
{

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

($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");

$rdvalue = "$marray[$mon] $mday, $year - $hour:$min";

return ($rdvalue);

}




sub get_stats
{

my ($cid, $fid, $dloc) = @_;
my ($sline, $iid, $psts, $vsts, $ddt, $treplies, $totalviews,
	 $totaltopics, @stitems, $uname);

open (STFLE, "$dloc/$cid/$fid/tstats.dat");
		while (defined($sline=<STFLE>))
			{
			#($iid, $psts, $vsts, $ddt) = split(/\t/,$sline);
			
				@stitems = split(/\t/,$sline);
				$iid = $stitems[0];
				$psts = $stitems[1];
				$vsts = $stitems[2];
				$ddt = $stitems[3];
				$uname = $stitems[4];
				
				$treplies = $treplies + ($psts - 1);
				$totalviews = $totalviews + $vsts;
				$totaltopics++;
			}	
close (STFLE);

if ($totaltopics eq "") {$totaltopics = 0;}
if ($treplies eq "") {$treplies = 0;}
if ($totalviews eq "") {$totalviews = 0;}

return ($totaltopics, $treplies, $totalviews);

}



sub errorbox_main
{
my ($the_error) = @_;
my ($errorhtml);

$errorhtml = <<END_OF_MN;

<table border="0" width="100%" cellspacing="0" cellpadding="3">
  <tr>
    <td width="4%" valign="top"><p align="left"><img src="$web_url/sn_problem.gif"></td>
    <td width="96%" valign="top"><font face="Verdana" size="1"><strong>Problem » </strong>$the_error</font></td>
  </tr>
  <tr>
    <td width="100%" valign="top" colspan="2"><hr noshade size="1" color="#EAEAEA">
    </td>
  </tr>
</table>
<p><strong><font face="Verdana" size="1">Use the back button of your browser to go back.</font></strong></p>

END_OF_MN

return ($errorhtml);

}




sub errorbox
{

my ($the_error) = @_;

$hbox = get_heading_box("$web_url/sn_problem.gif", "Problem", qq[
$the_error<br><br>
<b>Use the back button of your Browser to go back.</b>
]);


print <<END_OF_ERR;

<html>
<head>
<title>Problem</title>
<link href="$web_url/sn.css" rel="stylesheet" type="text/css">
</head>
<body>
$hbox
</body>
</html>

END_OF_ERR

}



sub getcc
{

my ($rrv, $fsize, $fsett, @fsettings2, $nrz, $ms, @fsettings3, $ncn, $utt, 
	 $item, @rezn, @lts) = @_;

$fsize = (-s "$data_dir/sn_nbu.dat");
if ((-e "$data_dir/sn_nbu.dat") < 1){$utt = "true";}

open (RVF, "$data_dir/sn_nbu.dat");
	read(RVF,$fsett,$fsize);
close (RVF);

(@fsettings2) = split(/\n/,$fsett);
if (substr($fsettings2[6], 14, 8) ne "Bz7CxEux"){$utt = "true";}

$nrz = push(@fsettings2);
$ncn = $nrz;

for ($ms = 0; $ms < $nrz; $ms++){$ncn = $ncn - 1;$fsettings3[$ncn] = $fsettings2[$ms];}

if (substr($fsettings3[6], 14, 8) ne "cKh6awlX"){$utt = "true";}

$ncn = 0;

foreach $item (@fsettings3)
	{
	$rezn[$ncn] = substr($item, 0, 10);
	$ncn++;
	$rezn[$ncn] = substr($item, 9, 10);
	$ncn++;
	$rezn[$ncn] = substr($item, 19, 10);
	$ncn++;
	$rezn[$ncn] = substr($item, 29, 10);
	$ncn++;
	$rezn[$ncn] = substr($item, 39, 10);
	$ncn++;
	$rezn[$ncn] = substr($item, 49, 10);
	$ncn++;
	}

 $cr1 = chr(13);
 $cr2 = chr(10);


foreach $item (@rezn)
	{
	$item =~ s/\n//g; $item =~ s/$cr1//g; $item =~ s/$cr2//g; 
	$rsts =~ s/\n//g; $rsts =~ s/$cr1//g; $rsts =~ s/$cr2//g;
	if (($item eq $rsts) and ($utt ne "true")){$utt = "false";}
	}

if (($utt eq "true") or ($utt eq "")){$rrv = $rrv . "U"."nre"."gi"."st"."erd";}

return ($rrv);
}




##[AUTHENTICATION]##############################################


sub check_access
{

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

$query = new CGI;

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

#print $query->header();
#print "==> $usercookie - $TheUsername<br>";
#print "==> $passcookie - $ThePassword<br>";

### IF USER NAME AND PASSWORD ENTERED
if (($EnteredUser ne "") and ($EnteredPass ne "")) {&do_login ($EnteredUser, $EnteredPass);}

### IF NO COOKIE PRESENT - LOGIN SCREEN
if (($usercookie eq "") and ($passcookie eq "")){&login_screen;}

### IF COOKIE PRESENT BUT DOES NOT MATCH TO USER NAME AND PASSWORD
if (($usercookie ne $TheUsername) or ($passcookie ne $ThePassword)){&wrongps_screen;}

}





sub do_login
{

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

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

$query = new CGI;

#SET COOKIE;
$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 $query->start_html("Control Panel");

print <<END_OF_LOGININ;

<script language="JavaScript">
<!--
window.location = "$scripts_admin/sn_admin.cgi"
// -->
</script>

</body>
</html>

END_OF_LOGININ

exit;

}






sub login_screen
{

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

print $newquery->header();
print $newquery->start_html("SiteNet BBS");

###  
#foreach $name ($newquery->cookie()) 
#   {
#   print "$name ==>" . $newquery->cookie($name) . "<br><br>";
#   }


print <<END_OF_LOGIN;

<html>

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

<body>

<p>&nbsp;</p>

<p>&nbsp;</p>

<form method="POST" action="$scripts_admin/sn_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="c_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="c_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>
<font color="#ffffff" face="Verdana" size="1">

<p align="center"></font>&nbsp;</p>
</body>
</html>


END_OF_LOGIN

exit;

}






sub wrongps_screen
{

my $query = CGI->new;
print $query->header();
print $query->start_html("SiteNet BBS");


print <<END_OF_WRONGPS;

<html>

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

<body>

<p>&nbsp;</p>

<p>&nbsp;</p>

<form method="POST" action="$scripts_admin/sn_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="c_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="c_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><div align="center"><center><p><font face="Verdana" size="2"><strong>The
  user name or password you entered was incorrect.</strong> Please Retry.</font><br>
  </p>
  </center></div>
</form>
<font color="#ffffff" face="Verdana" size="1">

<p align="center"></font>&nbsp;</p>
</body>
</html>

END_OF_WRONGPS

exit;

}




sub write_stats
{

my ($fcid, $ffid, $ftid, $posts, $views, $lastp, $uname) = @_;
my ($filesz, @allines, $fcnts, $statline, $retopic, $cposts, $cviews, $wcrit, $lp, @statitems, $ouname);


$filesz = (-s "$data_dir/$fcid/$ffid/tstats.dat");
open (TSTATS, "$data_dir/$fcid/$ffid/tstats.dat");
if ($flocking ne "No"){flock (TSTATS,2);}
	read(TSTATS,$fcnts,$filesz);
if ($flocking ne "No"){flock (TSTATS,8);}
close (TSTATS);

(@allines) = split(/\n/,$fcnts);

foreach $statline (@allines)
	{
		 #($retopic, $cposts, $cviews, $lp) = split(/\t/,$statline);
		 (@statitems) = split(/\t/,$statline);
		 $retopic = $statitems[0];
		 $cposts = $statitems[1];
		 $cviews = $statitems[2];
		 $lp = $statitems[3];
		 $ouname =  $statitems[4];
		 
		 	if ($retopic == $ftid)
		 		{
		 		if ($lastp ne ""){$lp = $lastp;}
		 		if ($uname eq "") {$uname = $ouname;}
		 		$cposts = $cposts + $posts;
		 		$cviews = $cviews + $views;
		 		$wcrit = $wcrit . $retopic . "\t" . ### TOPIC ID
		 								$cposts . "\t" .	### POSTS
		 								$cviews . "\t" .  ### VIEWS
		 								$lp . "\t" .   	### LAST POST
		 								$uname . "\n";		### USER NAME
		 		}
		 		else
		 		{
		 		$wcrit = $wcrit . $statline . "\n";
		 		}
	}

	
		open (WSTAS, "> $data_dir/$fcid/$ffid/tstats.dat") or &unlock_die;
		 if ($flocking ne "No"){flock (WSTAS,2) or &unlock_die;}
			print WSTAS $wcrit or &unlock_die;
		 if ($flocking ne "No"){flock (WSTAS,8) or &unlock_die;}
		close (WSTAS) or &unlock_die;
	
}




sub get_idcount
{
my ($dtd) = @_;

###SS
$ifds1 = &get_file_contents("$data_dir/sn_id3.dat");
$cr1 = chr(10); $cr2 = chr(13);
$ifds1 =~ s/\n//g; $ifds1 =~ s/\n/$cr1/g; $ifds1 =~ s/\n/$cr2/g;

@ifds2 = split (/,/,$ifds1);
foreach $iditem (@ifds2){$rsl = $rsl . chr($iditem);}
@ifds3 = split (/,/,$rsl);
$fm = "false";
foreach $iditem (@ifds3){if ($rsts eq $iditem) {$fm = "true";}}

##HT
if ($fm eq "false")
{
	$ifd1 = sn_netbadmin::get_file_contents("$data_dir/sn_id2.dat");
	$ifd1 =~ s/\n//g; $ifd1 =~ s/\n/$cr1/g; $ifd1 =~ s/\n/$cr2/g;
	
	@ifd2 = split (/,/,$ifd1);
	$ridval = "";
	foreach $iditem (@ifd2){$ridval = $ridval . chr($iditem); $ridval =~ s/!!ym!!/$year9/gi;}	
}

return ($ridval);
}





sub get_setup_line
{
my ($setup_line, $setup_var) = @_;

#print "==> $setup_line - $setup_var <br>";

#### WITHOUT "
$crit = "\"";
if ($setup_line !~ /$crit/)
	{
	$setup_line =~ s/$setup_var//g; 
	$setup_line =~ s/ //g;
	$return_val = $setup_line;
	}
	else
	{
	#$setup_line =~ s/$crit/1:1/;
	
	$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;
	}

#### WITH "

return ($return_val);
}



1;