Adresa zlp@ceata.org pentru notificări.
[zlp.git] / zlp
CommitLineData
fb4277ce
MG
1#!/usr/bin/perl -w -CSDA
2use v5.14;
3use strict;
4use warnings;
5use utf8;
6
7use CGI::Fast qw/header param/;
8use Email::Simple;
9use Email::Sender::Simple qw/sendmail/;
10use File::Slurp qw/append_file/;
11use JSON qw/encode_json/;
12use YAML::Any qw/Dump LoadFile/;
13
14use Fcntl qw/LOCK_EX LOCK_UN/;
15use List::Util qw/sum/;
16
17##################################################
18
19# Inceput setari
20my %events = (
fb4277ce 21 balti => {
313b0389
TT
22 date => 'duminică, 29 septembrie 2013',
23 locul => '',
24 sala => '',
25 locuri => 60,
26 link => '',
27 image => '',
fb4277ce
MG
28 },
29
313b0389
TT
30 bucuresti => {
31 date => 'sâmbătă, 21 septembrie 2013',
32 locul => '',
33 sala => '',
34 locuri => 60,
35 link => '',
36 image => '',
fb4277ce
MG
37 },
38
39 chisinau => {
313b0389
TT
40 date => 'sâmbătă, 28 septembrie 2013',
41 locul => '',
42 sala => '',
43 locuri => 60,
44 link => '',
45 image => '',
46 },
47
48 cluj => {
49 date => 'sâmbătă, 28 septembrie 2013',
50 locul => '',
51 sala => '',
52 locuri => 60,
53 link => '',
54 image => '',
fb4277ce
MG
55 },
56
57 constanta => {
313b0389
TT
58 date => 'sâmbătă, 21 septembrie 2013',
59 locul => '',
60 sala => '',
61 locuri => 60,
62 link => '',
63 image => '',
fb4277ce 64 },
313b0389
TT
65
66 valcea => {
67 date => 'vineri, 27 septembrie 2013',
68 locul => '',
69 sala => '',
70 locuri => 60,
71 link => '',
72 image => '',
73 }
fb4277ce
MG
74);
75
165d37a0
TT
76use constant EMAIL_FROM => 'Ziua Libertății Programelor — Fundația Ceata <zlp@ceata.org>';
77use constant ADMIN_EMAIL => 'Ziua Libertății Programelor — Fundația Ceata <zlp@ceata.org>';
78use constant DATAFILE => 'date.yml';
fb4277ce
MG
79# Sfarsit setari
80
81##################################################
82
83open LOCK, '<', DATAFILE;
84
85sub nr_participanti { my $event = shift; sum 0, map { $_->{numar} } grep { $_->{event} eq $event } @_ }
86
87sub append{
88 flock LOCK, LOCK_EX;
89
90 eval {
91 my $prenume = param('prenume') or die 'Nu ati completat campul "Prenume"';
92 utf8::decode($prenume);
93 my $nume = param('nume') // '';
94 utf8::decode($nume);
95 my $email = param('email') or die 'Nu ati completat campul "Email"';
96 utf8::decode($email);
97 my $event = param('oras') or die 'Nu ati ales orasul evenimentului';
98 die 'Ziua Libertatii Programelor nu se tine in orasul ales' unless exists $events{$event};
99 my $numar = int param('numar') or die 'Nu ati ales nuamrul de participanti';
100 die 'Numarul de participanti trebuie sa fie intre 1 si 5' unless $numar >= 1 && $numar <= 5;
101 my $captcha = param('captcha') or die 'Nu ati completat anul de lansare al proiectului GNU';
102 die 'Ati completat gresit anul de lansare al proiectului GNU' unless $captcha == 83;
103 my $spam = param('spam') or 0;
104 my @db = grep { $_->{event} eq $event } LoadFile DATAFILE;
105 die 'Aceasta adresa de poşta electronica este deja folosita' if grep { $_->{email} eq $email } @db;
106 my $participanti = nr_participanti $event, @db;
107 die 'Nu sunt suficiente locuri libere' if $events{$event}{locuri} < $participanti + $numar;
108
109 my %entry = (
110 prenume => $prenume,
111 nume => $nume,
112 email => $email,
113 event => $event,
114 numar => $numar,
115 spam => defined($spam) && $spam ? 1 : 0,
116 );
117 my $success_email = Email::Simple->create(
118 header => [
119 To => "$nume <$email>",
120 Subject => 'Inscriere la Ziua Libertatii Programelor',
121 From => EMAIL_FROM,
122 ],
123 body => "Aceasta este o confirmare de inscriere la Ziua Libertatii Programelor\n\n" . Dump \%entry,
124 );
125 sendmail $success_email, { to => [$email, ADMIN_EMAIL]};
126 append_file DATAFILE, Dump \%entry;
127 };
128
129 flock LOCK, LOCK_UN;
130 if ($@) {
131 my $eroare = $@ =~ s/ at .*//r;
132 my $error_email = Email::Simple->create(
133 header => [
134 To => "Administrator <" . ADMIN_EMAIL . ">",
135 Subject => 'Eroare in inscrierea la Ziua Libertatii Programelor',
136 From => EMAIL_FROM,
137 ],
138 body => "Eroare: $eroare",
139 );
140 sendmail $error_email;
141 print header('text/html; charset=utf-8', '500 Internal Server Error');
142 print $eroare;
143 } else {
144 print header('text/html; charset=utf-8');
145 print 'Aţi fost inregistrat cu succes';
146 }
147}
148
149sub info{
150 my $event = param('event');
151
152 eval {
153 die 'Eveniment inexistent' unless defined $event && exists $events{$event};
154 my %out = %{$events{$event}};
155 my $participanti = nr_participanti $event, LoadFile DATAFILE;
156 $out{locuri} = $out{locuri} - $participanti;
157 print header('application/json; charset=utf-8');
158 print encode_json \%out;
159 };
160
161 if ($@) {
162 $@ =~ s/ at .*//;
163 print header('text/html; charset=utf-8', '500 Internal Server Error');
164 print $@;
165 }
166}
167
168sub view{
169 my $event = param('event');
170
171 unless (exists $events{$event}) {
172 print header('text/html; charset=utf-8', '500 Internal Server Error');
173 print 'Acest eveniment nu exista';
174 return;
175 }
176
177 my @db = grep { $_->{event} eq $event } LoadFile DATAFILE;
178 my $participanti = nr_participanti $event, @db;
179 print header('text/html; charset=utf-8');
180 print "Sunt $participanti participanti inscrisi<p>";
181 for my $p(@db) {
182 print "Nume: $p->{nume}<br>Prenume: $p->{prenume}<br>Email: $p->{email}<br>Event: $p->{event}<br>Numar: $p->{numar}<br>Spam: $p->{spam}<p>";
183 }
184}
185
186while (CGI::Fast->new) {
187 my $op = param 'op' // '';
188 append if $op eq 'append';
189 info if $op eq 'info';
190 view if $op eq 'view';
191}
192
1931;
194__END__
195
196=head1 NAME
197
198zlp - Inscrieri Ziua Libertatii Programelor
199
200=head1 AUTHOR
201
202Marius Gavrilescu E<lt>marius@ieval.roE<gt>
203
204=head1 COPYRIGHT AND LICENSE
205
206Copyright (C) 2013 by Fundatia Ceata
207
208This program is free software: you can redistribute it and/or modify
209it under the terms of the GNU Affero General Public License as published by
210the Free Software Foundation, either version 3 of the License, or
211(at your option) any later version.
212
213This program is distributed in the hope that it will be useful,
214but WITHOUT ANY WARRANTY; without even the implied warranty of
215MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
216GNU Affero General Public License for more details.
217
218You should have received a copy of the GNU Affero General Public License
219along with this program. If not, see <http://www.gnu.org/licenses/>.
220
221
222=cut
223
This page took 0.023885 seconds and 4 git commands to generate.