#!/usr/bin/perl

package easylist;

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

require Exporter;

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

$prgname = "EasyList";
$copyright = "Copyright Š2004 FocalMedia.Net - All Rights Reserved";


@ISA = qw(Exporter);

@EXPORT = qw(check_access 
				 send_mail
				 problem_popup
				 ); 

@EXPORT_OK = qw(
					);


&get_setup;

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





sub problem_popup
{

my ($problem_text) = @_;

$template = &get_file_contents("$data_dir/controlpanel.html");

$tmplc = <<END_OF_PR;

<table border="0" cellpadding="0" cellspacing="0" width="100%">
  <tr>
    <td width="6%" valign="top"><img src="$web_url/add.gif"></td>
    <td width="94%" valign="top"><font face="Arial" size="4" color="#000000"><strong>Problem</strong></font><font
    face="Verdana" size="1" color="#000000"><br>
    </font><font face="Verdana" size="1">$problem_text</font></td>
  </tr>
</table>
<p><font face="Verdana" size="1"><strong>Use the back button of your browser to go back.</strong></font></p>
<hr noshade size="1" color="#EBEBEB">

END_OF_PR

$template =~ s/!!controlpanel!!/$tmplc/g;

print "$template";

exit;
}



sub send_mail
{

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

my($sendmail, $sendername, $fromemail, $toemail, $mail_subject, $email_message, $etype) = @_;
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 ($etype ne "NORMAL")
{
$uline = "Content-Type: text/html; charset=UTF-8";
}


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

open (SENDMAIL, "| $sendmail $toemail");
print SENDMAIL <<End_of_Mail;
From: $sendername $ftext
To: $toemail
Reply-To: $fromemail
Subject: $mail_subject
Sender: $fromemail
$uline

$email_message

End_of_Mail
}
}




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 (($usercookie eq "") and ($passcookie eq "")){&login_screen;}

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

}











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

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

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

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

print "<html>
<head>
<title>HTML Rotate</title>
<meta HTTP-EQUIV=\"REFRESH\" CONTENT=\"0;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 qq[
<html>
<head>
<title></title>
</head>
<body>
<p>&nbsp;</p>
<p>&nbsp;</p>
<form method="POST" action="$script_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">__</font></td>
    </tr>
  </table>
  </center></div>
</form>
<font color="#ffffff" face="Verdana" size="1">

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



exit;

}



sub wrongps_screen
{

my ($scripts_admin);


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


print qq[
<html>
<head>
<title>Login</title>
</head>

<body>

<p>&nbsp;</p>

<p>&nbsp;</p>

<form method="POST" action="$script_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">__</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></p>
  </center></div>
</form>
<font color="#ffffff" face="Verdana" size="1">

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


exit;

}







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

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 =~ /^WEBDIR/){$webdir = &get_setup_line($line, WEBDIR);}
if ($line =~ /^WEBURL/){$weburl = &get_setup_line($line, WEBURL);}
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);}

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

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


sub get_file_contents
{

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

$filesize = (-s "$filename");
open (TFILECNTS, "$filename");
 if ($flocking ne "No"){flock (TFILECNTS,2);}
	read(TFILECNTS,$thefile,$filesize);
 if ($flocking ne "No"){flock (TFILECNTS,2);}
close (TFILECNTS);

return ($thefile);
}







sub demo
{

print qq[

<html>

<head>
<title>Editing Template</title>
<meta content="MSHTML 6.00.5730.11" name="GENERATOR">
</head>

<body>

<p align="center"><font face="Arial" size="2">&nbsp;</font></p>

<p align="center"><font face="Arial" size="2"><img src="$webdir/ellogo.gif"></font></p>

<p align="center"><font face="Arial" size="2">This functionality is not available in this
demo so that <br>
we can provide the demo to you in an orderly fashion.<br>
<strong>Use the back button of your browser to go back.</strong></font></p>
</body>
</html>


];

exit;



}


1;