## ------------------  Here we keep global settings
package Utils::spd;
require Exporter;
@ISA = qw(Exporter);
@EXPORT_OK = qw(checksize checkemail start_html end_html html_header redirect_url printmsg printerr sendmail writelog);  # symbols to export on request  
use strict;

my $mail_prog = '/usr/sbin/sendmail'; #sendmail path

#-----------------------------------------------------------------------------------
sub html_header{
return "Content-Type: text/html\n\n";
}
#-----------------------------------------------------------------------------------
sub start_html{
my ($title)=@_;
my $html="<html>\n";
$html .= "<head>\n";
$html .= "<title>$title</title>\n";
$html .= "</head>\n";
$html .= "<body>\n";
returm $html;
}

#-----------------------------------------------------------------------------------
sub end_html{
return "</body></html>";
}
#-----------------------------------------------------------------------------------
sub redirect_url{   
my ($title,$url,$time)=@_;
my $html="<html>\n";
$html .= "<head>\n";
$html .= "<meta http-equiv=\"Refresh\" CONTENT=\"$time; URL=$url\">";
$html .= "<title>$title</title>\n"; 
$html .= "</head>\n";
$html .= "<body>\n";
return $html;
}
#-----------------------------------------------------------------------------------
sub printmsg{
my ($msg,$url,$time)=@_;

print html_header,
redirect_url("Message",$url,$time),
"<div align=center>",
"<br><br><b>",$msg,"</b>",
"</div>",
end_html;
}
#-----------------------------------------------------------------------------------
sub printerr{
my ($msg,$url,$time)=@_;
print html_header,
redirect_url("Eroare",$url,$time),
"<div align=center>",
"<br><br><b>Eroare</b><br>",
($msg),
"</div>",
end_html;
}
#-----------------------------------------------------------------------------------
sub sendmail{
#Trimite email folosindu-se de pogramul sendmail ($mail_prog)
my ($to,$from,$subject,$message)=@_;
open (MAIL, "|$mail_prog -t");
print MAIL "To: $to\n";
print MAIL "Reply-to: $from\n";
print MAIL "From: $from\n";
print MAIL "Subject: $subject\n" ;
print MAIL "\n\n";
print MAIL "$message\n" ;
print MAIL "\n\n";
close (MAIL);
}
#-----------------------------------------------------------------------------------
sub writelog {
my ($flog,$msg)=@_;
my $proxyaddr  = $ENV{'REMOTE_ADDR'};
my $ipaddr   = $ENV{'HTTP_X_FORWARDED_FOR'};
open (LOGFILE, ">> $flog") ;
flock(LOGFILE, 2) ;     #LOCK - comment for locking on a non-Unix server.
seek(LOGFILE, 0, 2) ;   #LOCK - comment for locking on a non-Unix server.
my $datetime = localtime ;
if ($ipaddr eq "") {print LOGFILE "$datetime\t$proxyaddr\t$msg\n";}
else {print LOGFILE "$datetime\t$ipaddr<--$proxyaddr\t$msg\n";}
flock(LOGFILE, 8) ;     #UNLOCK - comment for locking on a non-Unix server.
close (LOGFILE) ;
}
#-----------------------------------------------------------------------------------
sub checksize{
#
my ($var,$min,$max)=@_;
return ( ($max>=$var) and ($var>=$min) );
}
#-----------------------------------------------------------------------------------
sub checkemail{
my ($addr)=@_;
return ( ($addr =~ /.+\@.+\..+/ ) and !($addr =~ s/[^a-zA-Z0-9@._-]//g) )
}
#----------------------------------------------------------------------------------
sub jredirect{
#javascript redirect
my ($url)=@_;
my $html.="<!--\n";
$html.= "var URL   = \"$url\"\n";
$html.= "var speed = 7000 \n";
$html.= "function reload() {\n";
$html.= "location = URL;\n";
$html.= "target = \"_parent\";\n";
$html.= " }\n";
$html.= "setTimeout(\"reload()\", speed);\n";
$html.= "//-->\n";
return $html;
}
#-----------------------------------------------------------------------------------

1;
