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