apps / action / action /
admin cloud-section (root) fix: mauvais contenu de variable
9b45a42 7 years ago
1 contributor
188 lines | 7.812kb
#!/usr/bin/perl -w
use strict;
use warnings;
use DBI;
use utf8;
use POSIX qw(strftime);
#use Data::Dumper;
use Email::Send::SMTP::Gmail;

my $scriptconf = $ENV{PWD} . "/$0";
$scriptconf =~ s/\.pl$//;
$scriptconf =~ s/$/.conf/;
if (-r $scriptconf) {
    package cfg;
    unless (my $return = do $scriptconf) {
        warn "couldn't parse $scriptconf: $@" if $@;
        warn "couldn't do $scriptconf: $!"    unless defined $return;
        warn "couldn't run $scriptconf"       unless $return;
    }
}
else {
    print "pas de config\n";
    exit;
}

if (-r $cfg::config{notification}->{flag}) {
    open FLAG, $cfg::config{notification}->{flag};
    while (<FLAG>) {
        if (/^to:(.+)$/) {
            $cfg::config{test}->{address} = $1;
        }
        elsif (/^intro:([[:alnum:]]+)/) {
            $cfg::config{test}->{intro} = $1;
        }
    }
    close FLAG;
    if (exists $cfg::config{test}->{address} and exists $cfg::config{test}->{intro}) {
        delete $cfg::config{notification}->{to};
        $cfg::config{notification}->{to}->{test}->{address} = $cfg::config{test}->{address};
        $cfg::config{notification}->{to}->{test}->{intro} = '@' . $cfg::config{test}->{intro};
        $cfg::config{notification}->{to}->{test}->{outro} = "pour test";
        $cfg::config{notification}->{to}->{test}->{subject} = "test de l'envoi des actions de la section";
    }
}

my $dbh = DBI->connect($cfg::config{db}->{driver}, $cfg::config{db}->{user}, $cfg::config{db}->{password}, {'RaiseError' => 1, 'AutoCommit' => 1});
$dbh->{'mysql_enable_utf8'} = 1;
$dbh->do(qq{SET NAMES "utf8"});

my $request;
my $sth;
my $polls;
my $slots;
my $votes;
my %tables = ();
$tables{polls} = $cfg::config{prefix} . "poll";
$tables{votes} = $cfg::config{prefix} . "vote";
$tables{slots} = $cfg::config{prefix} . "slot";

$request = "select id as poll_id, admin_name as category, admin_id, title, recurrent, restricted, description from $tables{polls}";
$sth = $dbh->prepare($request);
$polls = $dbh->selectall_hashref($sth, [ qw(category poll_id) ]);

# admin_name = catégorie
my $dt = strftime "%s", localtime;
foreach my $category (keys(%{$polls})) {
    foreach my $poll_id (keys(%{$polls->{$category}})) {
        $request = "select id as slot_id, title, poll_id from $tables{slots} where poll_id = '$poll_id'";
        $sth = $dbh->prepare($request);
        $slots =  $dbh->selectall_hashref($sth, [ qw(poll_id slot_id) ]);

        $request = "select id as vote_id, uniqId, poll_id, choices from $tables{votes} where poll_id = '$poll_id'";
        $sth = $dbh->prepare($request);
        $votes =  $dbh->selectall_hashref($sth, [ qw(poll_id uniqId) ]);

        my $last_date = 0;
        my @deleted = ();
        foreach my $slot_id (keys(%{$slots->{$poll_id}})) {
            if ($slots->{$poll_id}->{$slot_id}->{title} < $dt) {
                $slots->{$poll_id}->{$slot_id}->{to_delete} = 1;
            }
            my $title = $slots->{$poll_id}->{$slot_id}->{title};
            $last_date = strftime "%s", localtime($title) if ($title > $last_date);
        }

# PURGE DES ACTIONS DÉJÀ PASSÉES ET NON RÉCURRENTES
        if ($last_date < $dt and ( ! defined $polls->{$category}->{$poll_id}->{recurrent} or $polls->{$category}->{$poll_id}->{recurrent} < 1)) {
            print "purge de $category/$polls->{$category}->{$poll_id}->{title}\n";
            $request = "delete from $tables{polls} where id = '$poll_id'";
            delete $polls->{$category}->{$poll_id};
            $dbh->do($request);
#            print $request . "\n";
            $request = "delete from $tables{slots} where poll_id = '$poll_id'";
            delete $slots->{$poll_id};
            $dbh->do($request);
#            print $request . "\n";
            $request = "delete from $tables{votes} where poll_id = '$poll_id'";
            delete $votes->{$poll_id};
            $dbh->do($request);
#            print $request . "\n";
            next;
        }

# SUPPRESSION DES VOTES PASSÉS ET AJOUT DES VOTES SUIVANTS
        foreach my $slot_id (keys(%{$slots->{$poll_id}})) {
            next if (! exists $slots->{$poll_id}->{$slot_id}->{to_delete});
            $request = "delete from $tables{slots} where poll_id = '$poll_id' and title = '$slots->{$poll_id}->{$slot_id}->{title}'"; 
            $dbh->do($request);
#            print $request . "\n";            
            if ($polls->{$category}->{$poll_id}->{recurrent} > 0) {
                $last_date += $polls->{$category}->{$poll_id}->{recurrent} * 86400;
                my $new_date = strftime "%s", localtime($last_date);
                $request = "insert into $tables{slots} (poll_id, title, moments) values('$slots->{$poll_id}->{$slot_id}->{poll_id}', '$new_date', NULL)";
                $dbh->do($request);
#                print $request . "\n";
                foreach my $uniqId (keys(%{$votes->{$poll_id}})) {
                    my $vote = substr $votes->{$poll_id}->{$uniqId}->{choices}, 1;
                    if ($vote eq "") {
                        $request = "delete from $tables{votes} where uniqId = '$uniqId'";
                        delete $votes->{$poll_id}->{$uniqId};
                        $dbh->do($request);
#                        print $request . "\n";
                    }
                    else {
                        $request = "update $tables{votes} set choices='$vote' where uniqId = '$uniqId'";
                        $dbh->do($request);
#                        print $request . "\n";
                    }
                }
            }
        }
    }
}
# commit ineffective with AutoCommit enabled
# $dbh->commit();
$dbh->disconnect();

my ($mail,$error)=Email::Send::SMTP::Gmail->new(%{$cfg::config{notification}{from}});
die "session error: $error" unless ($mail!=-1);

foreach my $dest (keys(%{$cfg::config{notification}->{to}})) {

    my $data = '';

    if (substr($cfg::config{notification}->{to}->{$dest}->{intro}, 0, 1) eq "@"
    and -r $cfg::config{notification}->{intro_default_path} . substr($cfg::config{notification}->{to}->{$dest}->{intro},1) . $cfg::config{notification}->{intro_suffix}) {
        open INTRO, $cfg::config{notification}->{intro_default_path} . substr($cfg::config{notification}->{to}->{$dest}->{intro},1) . $cfg::config{notification}->{intro_suffix};
        while (<INTRO>) {
            if (/^<body /../^<\/body>/) {
                $data .= $_ unless /^<body / or /^<\/body>/;
            }
        }
        close INTRO;
    }
    else {
        $data .= $cfg::config{notification}->{to}->{$dest}->{intro};
    }

    foreach my $category (keys(%{$polls})) {
        next unless (keys%{$polls->{$category}});
        $data .= "<h1>$category</h1>";
        foreach my $poll_id (keys(%{$polls->{$category}})) {
            my $title    = $polls->{$category}->{$poll_id}->{title};
            if (exists $cfg::config{notification}->{to}->{$dest}->{admin}) {
                my $admin_id = $polls->{$category}->{$poll_id}->{admin_id};
                $data .= "<a href=$cfg::config{url}/adminstuds.php?poll=$admin_id>$title</a><br>";
            }
            else {
                $data .= "<a href=$cfg::config{url}/studs.php?poll=$poll_id>$title</a><br>";
                $data .= $polls->{$category}->{$poll_id}->{description} . "<br><br>";
            }
        }
    }

    $data .= $cfg::config{notification}->{to}->{$dest}->{outro};

    utf8::decode($data);

    $mail->send(
            -from=>$cfg::config{notification}{from_infos}{from},
            -replyto=>$cfg::config{notification}{from_infos}{replyto},
            -to=>$cfg::config{notification}->{to}->{$dest}->{address},
            -subject=>$cfg::config{notification}->{to}->{$dest}->{subject},
            -contenttype=>'text/html',
            -body=>$data
        );
}
$mail->bye();