#!/usr/bin/perl

use CGI;
use Mail::Sender;
use HTML::Entities;
use vars qw($trans $nazwisko $email $telefon $pytania $subm $temat $kat $smtpserver $jak $tr_nick $back_url $back $testform $exitURL @ka @em);

require("email.conf");

my $lang = 'PL';

my $q = new CGI;

my $sslOn = !$testform?$q->http('HTTPS'):1;


if(!$sslOn)
{
	if (defined($jak) && $jak eq "popup")
	{
		print $q->header(
				-type => "text/html",
				-charset => "utf-8");
		print "<html><body onLoad=\"window.close()\"></body></html>";
		exit;
	}
	else
	{
#		print $q->redirect($exitURL);
#		exit;
	}
}

my $fcr = '(\'|<|>|%|\/|\\\\|\.\.)';

for (qw/nazwisko email telefon pytania subm kat temat jak tr_nick back_url back/) {
   $$_ = $q->param($_);
#   $$_ = "" if $$_ =~/$fcr/; 
   $$_ = HTML::Entities::encode($$_,"<>&\"'%");
}

$email = "" if $email eq "1";

my $lay = $flay = '';

if (defined($jak) && $jak eq "popup")
{
	$lay = "mini_";
}
elsif (defined($jak) && ($jak eq 'lajt')) 
{
	$lay = $flay = 'lajt_';
}

my $bledy = "";
my $poszlo = 0;

open MAILTO, "UCIFORM_ADRES.html.i";
my $emailto = <MAILTO>;
close MAILTO;

open (KAT,"UCIFORM_KATEGORIE.html.i");

@ka = ();
@em = ();



while (<KAT>)
{
#	print STDERR "p=$_, ";
	chomp;
	my ($k,$e) = ($_=~/(.+)\t(.+)/);
#	print STDERR "o=$_: $k, $e\n";
	push @ka,$k;
	push @em,$e;
}
close KAT;


if ($ENV{REQUEST_METHOD}=~/^post$/i && $ENV{HTTP_REFERER}=~/email\.pl/)
{
	my $nreg = '^[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzĘÓĄŚŁŻŹĆŃęóąśłżźćń\s\-]+$';
	$bledy.="<li>Podaj swoje imię i nazwisko</li>" if !(HTML::Entities::decode($nazwisko));
	$bledy.="<li>Pole imię i nazwisko może zawierać wyłącznie litery</li>" if (!(HTML::Entities::decode($nazwisko)=~/$nreg/) && ($bledy eq ""));
	$bledy.="<li>Podaj swój adres e-mail</li>" if (!$email);
	$bledy.="<li>Podaj prawidłowy adres e-mail</li>" if ($email && HTML::Entities::decode($email)!~/^[\d\w\.\-\+_]+@([\d\w\.\-\+_]+\.)+[\d\w]{2,4}$/);
	$bledy.="<li>Wybierz kategorię</li>" if ($kat!~/^[\d]+$/ || $kat eq "0");
	$bledy.="<li>Wpisz temat</li>" if ($temat =~ /^\s*$/);
	$bledy.="<li>Wpisz treść wiadomości</li>" if ($pytania=~/^\s*$/);

	if (!$bledy)
	{
		$poszlo = 1;
		# I jeszcze wysylka e-majlem.
#		open MAILTO, "UCIFORM_ADRES.html.i";
#		my $emailto = <MAILTO>;
		my $emailto = $em[$kat-1];
#		close MAILTO;
		open TEMAT, "UCIFORM_NAGLOWEK.html.i";
		my $mailtemat = <TEMAT>;
		close TEMAT;
		open STOPKA, "UCIFORM_STOPKA.html.i";
		my $stopka = "";
		while (<STOPKA>)
		{
			$stopka.=$_;
		}
		close STOPKA;
		$mailtemat.=" ".$ka[$kat-1].": $temat";
		$Mail::Sender::NO_X_MAILER=1;
		$Mail::Sender::NO_MESSAGE_ID=1;
		my $sender = new Mail::Sender{
		smtp => $smtpserver,
		from => HTML::Entities::decode($nazwisko)." <".HTML::Entities::decode($email).">",
		to => $emailto,
		subject => HTML::Entities::decode($mailtemat),
		charset => "utf-8",
		};
		$sender->MailMsg({msg=>"".HTML::Entities::decode($pytania)."\n\n-- \n".$stopka});
#		print STDERR "mailing: $pytania 
#		ze $stopka 
#		i tematem: $mailtemat, 
#		do $emailto 
#		od $from 
#		(poprzez $smtpserver)\n";

	}
}

print $q->header(
        -type => "text/html",
        -charset => "utf-8");

cat ("email_${lay}head.html");
if (!$poszlo)
{
	if ($bledy && $back ne "1")
	{
		print qq|
				<div class="shortBox" style="padding: 10px 0px 10px 0px;">
		|;
		cat ("UCIFORM_INVALID.html.i");
		print qq|
				<ul class="circ">
				$bledy
				</ul>
				</div>
		|;
		if (defined($jak) && $jak eq "popup")
		{
		print '
			<form method="post" action="email.pl" name="contactForm">
			<input type="hidden" name="posted" value="1">
			<input type="hidden" name="tr_nick" value="'.$tr_nick.'">
			<input type="hidden" name="jak" value="'.$jak.'">
			<input type="hidden" name="nazwisko" value="'.$nazwisko.'">
			<input type="hidden" name="email" value="'.$email.'">
			<input type="hidden" name="temat" value="'.$temat.'">
			<input type="hidden" name="pytania" value="'.$pytania.'">
			<input type="hidden" name="kat" value="'.$kat.'">
			<input type="hidden" name="back_url" value="'.$back_url.'">
			<input type="hidden" name="back" value="1">
			<a href="#" onclick="document.contactForm.submit(); return false;">popraw</a><br/><br/>
			</form>';
		}
		else
		{
			do ("email_${flay}form.pl");
		}
	}
	else
	{
		do ("email_${flay}form.pl");
	}
}
else
{
		print qq|
				<div class="shortBox">
		|;
		cat ("UCIFORM_CONFIRM.html.i");
		print qq|
				</div>
		|;

}
if (defined($jak) && $jak eq "popup")
{
	print '
	<form action="'.$back_url.'" name="mczat_tr" method="post">
	<input type="hidden" name="tr_nick" value="'.$tr_nick.'">
	</form>
	<table cellspacing="0" border="0" cellpadding="0" width="100%">
	<tr>
	<td align="left"><a href="#" onclick="document.mczat_tr.submit(); return false;" class="bullet">powrót</a></td>
	<td align="right"><a href="#" onclick="window.close();" class="bullet">zamknij okno</a></td>
	</tr>
	</table>
	';
}

cat_inc ("email_${lay}foot.html");

sub catto
{
my ( $file ) = @_;
	my ($fil) = $file=~/\/?(.+)$/;
	open PLIK, $fil;
	$ret = "";
	while (<PLIK>)
	{
		$ret.=$_;
	}
	close PLIK;
	return $ret;
}

sub cat_inc
{
my ( $file ) = @_;
    open FILE,$file || return;
    while (<FILE>)
    {   
        if (/<!--#include virtual="(.+)"-->/)
        {   
			my $t =$_;
			my $swp = catto($1);
            $t=~(s/<!--#include virtual="(.+)"-->/$swp/);
            print $t;
        }
        else
        {   
            print;
        }
    }
    close (FILE);
}

sub cat 
{
	open (FILE,shift) || return;
	while (<FILE>){ print $_;}
	close (FILE);
}
