#!/usr/local/bin/perl -T use strict; use warnings; use CGI::Carp qw(fatalsToBrowser); use CGI::Simple qw(-debug1); use Data::FormValidator; use Data::FormValidator::Constraints qw(:closures); use Scalar::Util qw(tainted); # This is a basic template function for building a new application in # MITOMASTER. MITOMASTER is built on Apache and mod_perl. # POST_MAX is the max amount (bytes) that should be sent during a single post $CGI::Simple::POST_MAX = 1024; # File uploading is disabled by default, set this value to zero if the web # application will involve uploading files. $CGI::Simple::DISABLE_UPLOADS = 1; # Create a basic CGI object for doing work my $q = new CGI::Simple; # Parameter validation. # List your optional parameters here. my @optional_params = qw( ); # List your required parameters here. my @required_params = qw( myInput ); # Define parameter constraints here. It's important to define a tight # constraint for each parameter to maximize security on the server. # Parameters are automatically untainted when they pass their constraint. # Built-in constraint tests include: email, state, zip, phone, ip_address. # You can also define a new type of constraint. The my_constraint below uses # the fields named The my_constraint subroutine (see template below) does the # work of returning a code reference that will actually serve as the # constraint. More information on defining constraints is available in # Data::FormValidatorConstraints. my %param_constraints = ( # myInput => email(), # myInput => my_constraint( 40, 60, # {fields => [qw/myInput2 myInput3 myInput4/]} # ), ); # Create a validator profile and validate. Probably you want to leave this alone. my $profile = { optional => \@optional_params, required => \@required_params, constraint_methods => \%param_constraints, untaint_all_constraints => 1, }; my $results = Data::FormValidator->check($q, $profile); # Missing or invalid errors will be sent back as a message in the browser. # Define your message below using the croak function. if ($results->has_invalid or $results->has_missing) { croak "You've either missed something or given and improper value. Please fix it."; } # If we make to here, then the data is good. It will be availble in the # safe_params hash reference. my $safe_params = $results->valid; #for (keys %{$safe_params}){ # print 'parameter: ', $_, ' value: ', $safe_params->{$_}, "\n"; #} # DO INCREDIBLE THINGS HERE # Return an HTML response to the browser. print $q->header('text/html'), $safe_params->{'myInput'}; sub my_constraint { # Template for defining a custom parameter constraint. This example is # used for checking a numerical parameter. It accepts minimum and maximum # values and a list of other parameters that must also have values. The # value of the parameter is returned if the constraints are satisfied, # while undef is returned if they are not. my ($min_val, $max_val, $attrs) = @_; my ($myInput2, $myInput3, $myInput4) = @{ $attrs->{fields} } if $attrs->{fields}; return sub { my $dfv = shift; # Name it to refer to in the 'msgs' system. $dfv->name_this('my_constraint'); # value of 'myInput' parameter my $val = $dfv->get_current_constraint_value(); # get other data to refer to my $data = $dfv->get_input_data(as_hashref=>1); # check that the three required parameters have a value my $has_all_three = ($data->{$myInput2} && $data->{$myInput3} && $data->{$myInput4}); # return either the value or undef if ( ($val >= $min_val) && ($val <= $max_val) && $has_all_three ) { return $val; } else { return undef; } } }