#!/usr/bin/perl
# ================================================================================
# $Rev: 162 $ $URL: svn://localhost/PlayChess/trunk/cgi/invite.cgi $
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Author: $Author: Thomas $
# Modified: $Date: 2005-10-03 12:42:52 +0200 (Mo, 03 Okt 2005) $
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Copyright at playchess.de - all rights reserved
# ================================================================================
use lib "../cgi-bin";
use CGI qw( :standard );
use CGI::Carp qw(fatalsToBrowser);
use PCSession;
use Template;
use Util;
use MyDbi;
local $cgi = CGI->new();
local $sobj = PCSession->new( $cgi );
local $sname = $sobj->getValue( 'name' );
local $spid = $sobj->getValue( 'pid' );
local $from = $sobj->getValue( 'email' );
local $topic = $cgi->param( 'topic' );
local $config = getConfig();
local $INVITE_24 = 10;
# CGI parameters
# ~~~~~~~~~~~~~~
# local @envParms = qw( action email name first last gender cflag elo );
local @envParms = qw( action email subject text send preview );
local $pctext;
foreach $e (@envParms)
{
eval { $$e = $cgi->param($e); } # print "$e='$$e'
\n"; }
}
# local $action = $cgi->param( 'action' );
# local $name = $cgi->param( 'sort' );
# print __LINE__, "
\n";
$sobj->assertLogged();
if( $preview )
{
showPage( 'noemail' ) unless( $email );
showPage( 'nosubject' ) unless( $subject );
showPage( 'notext' ) unless( $text );
showPage( '', 'preview' );
}
elsif( $send )
{
showPage( 'noemail' ) unless( $email );
showPage( 'nosubject' ) unless( $subject );
showPage( 'notext' ) unless( $text );
# Connect to database
# ~~~~~~~~~~~~~~~~~~~
$dbh = dbiConnect() or exit(1);
my( $stmt, $invitations );
# check for email abuse
# ~~~~~~~~~~~~~~~~~~~~~
$stmt = "select count(*) from tbl_invite
where pid=$spid
and unix_timestamp(now)-unix_timestamp(ts)<86400
";
$invitations = MyDbi::getValue($stmt);
showPage( 'invite24' ) if( $invitations > $INVITE_24);
# Is already a player
# ~~~~~~~~~~~~~~~~~~~
$stmt = "select count(*) from tbl_player
where email=?
";
$players = MyDbi::getValue($stmt, $email);
showPage( 'isplayer' ) if( $players > 0 );
# Store invitation
# ~~~~~~~~~~~~~~~~
$stmt = "insert into tbl_invite (pid,email)
values(?,?)
";
$rc = MyDbi::doStmt( $stmt, $spid, $email );
showPage( 'double' ) if( $rc != 1 );
# All okay => send invitation
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~
my $pctext = getMailtext();
my $sendtext = $text . "\n\n" . $pctext;
Util::sendMail($email, $from, $subject, $sendtext);
showPage('sent');
}
elsif( $topic eq 'status' )
{
# Connect to database
# ~~~~~~~~~~~~~~~~~~~
$dbh = dbiConnect() or exit(1);
my( $stmt, $rows, $r );
$stmt = "
select i.email, unix_timestamp(i.ts), p.name, p.since, m.payment_gross
from tbl_invite i
left outer join tbl_player p on p.email=i.email and unix_timestamp(i.ts) < p.since
left outer join tbl_payment m on m.pid=p.pid
where 1
and i.pid=$spid
order by i.ts desc
";
$rows = MyDbi::getRows( $stmt );
for $r ( 0 .. $#{$rows} )
{
my( $email, $isince, $name, $psince, $money ) = @{ $rows->[$r] };
my( $user, $server ) = split( /@/, $email, 2 );
push @result, join( '|', $r, $user, $server, $isince, $name, $psince, $money );
$tickets += $money
}
$tickets = int($tickets+0.5);
my $tplfile = "invite-status.tpl";
my $tpl = Template->new( $tplfile );
$tpl->addMakro( 'TICKETS', $tickets );
$tpl->addMakro( 'NRESULTS', scalar @result );
$tpl->addArray( 'RESULTS', \@result );
print $tpl->Expand( $sobj );
exit(0);
}
else
{
showPage();
}
exit(0);
# --------------------------
sub showPage
# --------------------------
{
my( $err, $topic ) = @_;
my $tplfile = ($topic) ? "invite-$topic.tpl" : "invite.tpl";
my $tpl = Template->new( $tplfile );
my $pctext = getMailtext();
my $sendtext = $text . "\n\n" . $pctext;
# my $ttext = $text;
$sendtext =~ s/\n/
/g;
# $ttext =~ s/\n/
/g;
foreach $e (@envParms)
{
$tpl->addMakro( uc $e, $cgi->param($e) );
# print "param(", uc $e, ") = ", $cgi->param($e), "
\n";
}
$tpl->addMakro( 'SENDTEXT', $sendtext );
$tpl->addMakro( 'TEXT', $text );
$tpl->addMakro( 'PCTEXT', $pctext );
$tpl->addMakro( 'ERR', $err );
$tpl->addMakro( 'FROM', $from );
$tpl->addMakro( 'INVITE_24', $INVITE_24 );
print $tpl->Expand( $sobj );
exit(0);
}
# --------------------------
sub getMailtext
# --------------------------
{
my( $err ) = @_;
# Create email text from PlayChess
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
my $mailtpl = Template->new( 'mail-friend.tpl' );
foreach $e (@envParms)
{
$mailtpl->addMakro( uc $e, $cgi->param($e) );
# print "mailparam(", uc $e, ") = ", $cgi->param($e), "
\n";
}
my $pctext = $mailtpl->Expand($sobj);
return $pctext;
# return $text . "\n\n" . $pctext;
}