forked from frezik/bodgery_signin_forms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.pl
312 lines (260 loc) · 9.49 KB
/
app.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#!perl
# Copyright (c) 2016 Timm Murray
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
use v5.14;
use warnings;
use Mojolicious::Lite;
use DBI;
use DateTime;
use File::Slurp 'read_file';
use YAML 'Load';
use constant CONFIG_FILE => 'config.yml';
my $CONFIG = read_config( CONFIG_FILE );
use constant LIABILITY_INSERT => q{INSERT INTO liability_waivers}
. q{ (full_name, check1, check2, check3, check4, zip, phone, email}
. q{, emergency_contact_name, emergency_contact_phone, heard_from, signature)}
. q{ VALUES (?, true, true, true, true, ?, ?, ?, ?, ?, ?, ?)};
use constant GUEST_INSERT => q{INSERT INTO guest_signin}
. q{ (full_name, member_hosting, email, zip, join_mailing_list)}
. q{ VALUES (?, ?, ?, ?, ?)};
get '/' => sub {
my ($c) = @_;
$c->reply->static( 'index.html' );
};
get '/guest-signin' => sub {
my ($c) = @_;
$c->render( template => 'guest_signin' );
};
post '/guest-signin' => sub {
my ($c) = @_;
my $name = $c->param( 'full_name' );
my $email = $c->param( 'email' );
my $hosting = $c->param( 'member_hosting' );
my $heard_about_from = $c->param( 'heard_from' );
my $do_join_mailing_list = $c->param( 'join_mailing_list' );
my $zip = $c->param( 'zip' );
# Force to boolean
$do_join_mailing_list = !! $do_join_mailing_list;
my $args = trim_args({
name => $name,
email => $email,
hosting => $hosting,
do_join_mailing_list => $do_join_mailing_list,
zip => $zip,
});
my @errors = check_guest_params( $args );
if( @errors ) {
$c->render(
template => 'guest_signin',
errors => \@errors,
args => $args,
);
}
else {
save_guest_data( $args );
$c->render( template => 'guest_signin_submit' );
}
};
get '/liability' => sub {
my $c = shift;
$c->render( template => 'liability' );
};
post '/liability' => sub {
my $c = shift;
my $name = $c->param( 'name' );
my $check1 = $c->param( 'check1' );
my $check2 = $c->param( 'check2' );
my $check3 = $c->param( 'check3' );
my $check4 = $c->param( 'check4' );
my $zip = $c->param( 'zip' );
my $phone = $c->param( 'phone' );
my $email = $c->param( 'email' );
my $emerg_name = $c->param( 'emergency_contact_name' );
my $emerg_phone = $c->param( 'emergency_contact_phone' );
my $hosting = $c->param( 'member_hosting' );
my $heard_about_from = $c->param( 'heard_from' );
my $do_join_mailing_list = $c->param( 'join_mailing_list' );
my $signature = $c->param( 'output' );
# Force to boolean
$do_join_mailing_list = !! $do_join_mailing_list;
my $args = trim_args({
name => $name,
check1 => $check1,
check2 => $check2,
check3 => $check3,
check4 => $check4,
zip => $zip,
phone => $phone,
email => $email,
emerg_name => $emerg_name,
emerg_phone => $emerg_phone,
hosting => $hosting,
heard_about_from => $heard_about_from,
do_join_mailing_list => $do_join_mailing_list,
signature => $signature,
});
my @errors = check_liability_params( $args );
if( @errors ) {
$c->render(
template => 'liability',
errors => \@errors,
args => $args,
);
}
else {
save_liability_data( $args );
save_guest_data( $args );
$c->render( template => 'liability_submit' );
}
};
sub check_liability_params
{
my ($args) = @_;
my @errors;
push @errors => 'Name not filled in' unless $args->{name};
push @errors => 'Name should be only letters and spaces'
# Using \w technically allows numbers, but that's OK
unless $args->{name} =~ /\A (?:[\w\s\.\-]*) \z/x;
push @errors => 'First checkbox is required' unless $args->{check1} == 1;
push @errors => 'Second checkbox is required' unless $args->{check2} == 1;
push @errors => 'Third checkbox is required' unless $args->{check3} == 1;
push @errors => 'Fourth checkbox is required' unless $args->{check4} == 1;
push @errors => 'Zip not filled in' unless $args->{zip};
push @errors => 'Zip should only be numbers and dashes'
unless $args->{zip} =~ /\A (?:[\d\-]*) \z/x;
push @errors => 'Phone not filled in' unless $args->{phone};
push @errors => 'Phone should only be numbers, spaces, dashes, and parens'
unless $args->{phone} =~ /\A (?:[\d\s\-\(\)]*) \z/x;
if( $args->{email} ) {
push @errors => 'Email should have an "@" symbol'
unless $args->{email} =~ /@/x;
push @errors => 'Email should not have a "<" symbol'
# Just for XSS protection
if $args->{email} =~ /</x;
}
push @errors => 'Emergency Contact Name not filled in' unless $args->{emerg_name};
push @errors => 'Emergency Contact Name should only be letters and spaces'
unless $args->{emerg_name} =~ /\A (?:[\w\s\.\-]*) \z/x;
push @errors => 'Emergency Contact Phone not filled in' unless $args->{emerg_phone};
push @errors => 'Emergency Contact Phone should only be numbers, spaces, dashes, and parens'
unless $args->{emerg_phone} =~ /\A (?:[\d\s\-\(\)]*) \z/x;
push @errors => 'Hosting Member should only be letters and spaces'
unless $args->{hosting} =~ /\A (?:[\w\s\.\-]*) \z/x;
push @errors => 'Heard About From should only be letters and spaces'
unless $args->{heard_about_from} =~ /\A (?:[\w\s\.\-]*) \z/x;
# TODO signature
#push @errors => 'Signature should be filled in' unless $args->{signature};
return @errors;
}
sub check_guest_params
{
my ($args) = @_;
my @errors;
push @errors => 'Name not filled in' unless $args->{name};
push @errors => 'Name should be only letters and spaces'
# Using \w technically allows numbers, but that's OK
unless $args->{name} =~ /\A (?:[\w\s\.\-]*) \z/x;
push @errors => 'Zip not filled in' unless $args->{zip};
push @errors => 'Zip should only be numbers and dashes'
unless $args->{zip} =~ /\A (?:[\d\-]*) \z/x;
if( $args->{email} ) {
push @errors => 'Email should have an "@" symbol'
unless $args->{email} =~ /@/x;
push @errors => 'Email should not have a "<" symbol'
# Just for XSS protection
if $args->{email} =~ /</x;
}
push @errors => 'Hosting Member should only be letters and spaces'
unless $args->{hosting} =~ /\A (?:[\w\s\.\-]*) \z/x;
return @errors;
}
sub save_liability_data
{
my ($args) = @_;
my %args = %$args;
my $dbh = get_dbh();
my $sth = $dbh->prepare_cached( LIABILITY_INSERT )
or die "Can't prepare statement: " . $dbh->errstr;
$sth->execute( @args{qw{ name zip phone email emerg_name emerg_phone
heard_about_from }}, '' )
or die "Can't execute statement: " . $sth->errstr;
$sth->finish;
return;
}
sub save_guest_data
{
my ($args) = @_;
my %args = %$args;
my $do_join = $args->{do_join_mailing_list} ? 1 : 0;
my $dbh = get_dbh();
my $sth = $dbh->prepare_cached( GUEST_INSERT )
or die "Can't prepare statement: " . $dbh->errstr;
$sth->execute( @args{qw{ name hosting email zip }}, $do_join )
or die "Can't execute statement: " . $sth->errstr;
$sth->finish;
return;
}
sub trim_args
{
my ($args) = @_;
my %trimmed_args = map {
my $value = $args->{$_};
$value =~ s/\A\s*//;
$value =~ s/\s*\z//;
($_ => $value);
} keys %$args;
return \%trimmed_args;
}
{
my $dbh;
sub get_dbh
{
return $dbh if defined $dbh;
$dbh = DBI->connect(
'dbi:Pg:dbname=' . $CONFIG->{postgresql}{database},
$CONFIG->{postgresql}{username},
$CONFIG->{postgresql}{passwd},
{
AutoCommit => 1,
RaiseError => 0,
},
) or die "Could not connect to database: " . DBI->errstr;
return $dbh;
}
sub My::Test::set_dbh
{
my ($in_dbh) = @_;
$dbh = $in_dbh;
return 1;
}
}
sub read_config
{
my ($file) = @_;
my $contents = read_file( $file );
my $config = Load( $contents );
return $config;
}
app->secrets([ 'placeholder_passphrase_to_make_log_shutup' ]);
app->start;