#!/usr/bin/perl -w -CSDA use v5.14; use strict; use warnings; use utf8; use CGI::Fast qw/header param/; use Email::Simple; use Email::Sender::Simple qw/sendmail/; use File::Slurp qw/append_file/; use JSON qw/encode_json/; use YAML::Any qw/Dump LoadFile/; use Fcntl qw/LOCK_EX LOCK_UN/; use List::Util qw/sum/; ################################################## # Inceput setari my %events = ( bucuresti => { date => '35 jebruarie 2059', locul => 'Strada speranţei, nr 1', sala => 'sala IDT de la etajul 10', locuri => 10, link => 'http://ieval.ro/', image => 'http://0.tqn.com/d/animatedtv/1/0/M/H/1/annoying_orange.jpg.jpg', }, balti => { date => '35 jebruarie 2059', locul => 'Strada speranţei, nr 1', sala => 'sala IDT de la etajul 10', locuri => 10, link => 'http://ieval.ro/', image => 'http://tasty-dishes.com/data_images/encyclopedia/orange/orange-03.jpg', }, cluj => { date => '35 jebruarie 2059', locul => 'Strada speranţei, nr 1', sala => 'sala IDT de la etajul 10', locuri => 10, link => 'http://ieval.ro/', image => 'http://blog.clove.co.uk/wp-content/uploads/2013/07/Samsung-Logo.jpg', }, chisinau => { date => '35 jebruarie 2059', locul => 'Strada speranţei, nr 1', sala => 'sala IDT de la etajul 10', locuri => 10, link => 'http://ieval.ro/', image => 'http://images.moneysavingexpert.com/images/OrangeLogo.jpg', }, constanta => { date => '35 jebruarie 2059', locul => 'Strada speranţei, nr 1', sala => 'sala IDT de la etajul 10', locuri => 10, link => 'http://ieval.ro/', image => 'https://lh3.ggpht.com/-Dsy_8YO5Ais/UCxPnnfMOgI/AAAAAAAAAJ4/1iRXOKWACo8/s200/fresh_orange_slice.jpg', }, ); use constant EMAIL_FROM => 'Robotul Verde '; use constant ADMIN_EMAIL => 'Marius Gavrilescu '; use constant DATAFILE => '/var/www/dir/data.yml'; # Sfarsit setari ################################################## open LOCK, '<', DATAFILE; sub nr_participanti { my $event = shift; sum 0, map { $_->{numar} } grep { $_->{event} eq $event } @_ } sub append{ flock LOCK, LOCK_EX; eval { my $prenume = param('prenume') or die 'Nu ati completat campul "Prenume"'; utf8::decode($prenume); my $nume = param('nume') // ''; utf8::decode($nume); my $email = param('email') or die 'Nu ati completat campul "Email"'; utf8::decode($email); my $event = param('oras') or die 'Nu ati ales orasul evenimentului'; die 'Ziua Libertatii Programelor nu se tine in orasul ales' unless exists $events{$event}; my $numar = int param('numar') or die 'Nu ati ales nuamrul de participanti'; die 'Numarul de participanti trebuie sa fie intre 1 si 5' unless $numar >= 1 && $numar <= 5; my $captcha = param('captcha') or die 'Nu ati completat anul de lansare al proiectului GNU'; die 'Ati completat gresit anul de lansare al proiectului GNU' unless $captcha == 83; my $spam = param('spam') or 0; my @db = grep { $_->{event} eq $event } LoadFile DATAFILE; die 'Aceasta adresa de poşta electronica este deja folosita' if grep { $_->{email} eq $email } @db; my $participanti = nr_participanti $event, @db; die 'Nu sunt suficiente locuri libere' if $events{$event}{locuri} < $participanti + $numar; my %entry = ( prenume => $prenume, nume => $nume, email => $email, event => $event, numar => $numar, spam => defined($spam) && $spam ? 1 : 0, ); my $success_email = Email::Simple->create( header => [ To => "$nume <$email>", Subject => 'Inscriere la Ziua Libertatii Programelor', From => EMAIL_FROM, ], body => "Aceasta este o confirmare de inscriere la Ziua Libertatii Programelor\n\n" . Dump \%entry, ); sendmail $success_email, { to => [$email, ADMIN_EMAIL]}; append_file DATAFILE, Dump \%entry; }; flock LOCK, LOCK_UN; if ($@) { my $eroare = $@ =~ s/ at .*//r; my $error_email = Email::Simple->create( header => [ To => "Administrator <" . ADMIN_EMAIL . ">", Subject => 'Eroare in inscrierea la Ziua Libertatii Programelor', From => EMAIL_FROM, ], body => "Eroare: $eroare", ); sendmail $error_email; print header('text/html; charset=utf-8', '500 Internal Server Error'); print $eroare; } else { print header('text/html; charset=utf-8'); print 'Aţi fost inregistrat cu succes'; } } sub info{ my $event = param('event'); eval { die 'Eveniment inexistent' unless defined $event && exists $events{$event}; my %out = %{$events{$event}}; my $participanti = nr_participanti $event, LoadFile DATAFILE; $out{locuri} = $out{locuri} - $participanti; print header('application/json; charset=utf-8'); print encode_json \%out; }; if ($@) { $@ =~ s/ at .*//; print header('text/html; charset=utf-8', '500 Internal Server Error'); print $@; } } sub view{ my $event = param('event'); unless (exists $events{$event}) { print header('text/html; charset=utf-8', '500 Internal Server Error'); print 'Acest eveniment nu exista'; return; } my @db = grep { $_->{event} eq $event } LoadFile DATAFILE; my $participanti = nr_participanti $event, @db; print header('text/html; charset=utf-8'); print "Sunt $participanti participanti inscrisi

"; for my $p(@db) { print "Nume: $p->{nume}
Prenume: $p->{prenume}
Email: $p->{email}
Event: $p->{event}
Numar: $p->{numar}
Spam: $p->{spam}

"; } } while (CGI::Fast->new) { my $op = param 'op' // ''; append if $op eq 'append'; info if $op eq 'info'; view if $op eq 'view'; } 1; __END__ =head1 NAME zlp - Inscrieri Ziua Libertatii Programelor =head1 AUTHOR Marius Gavrilescu Emarius@ieval.roE =head1 COPYRIGHT AND LICENSE Copyright (C) 2013 by Fundatia Ceata This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . =cut