#!/usr/local/bin/perl -w # Author: Spencer Shimko # Project: Web1 -> index.cgi # Purpose: Perl/CGI script that will generate some interesting content with a # random component in response to user input. # recommend using this Carp stuff to debug # but remove it before submit'ing! #BEGIN { # use CGI::Carp("carpout", "fatalsToBrowser"); # open(LOG, ">>../../../cgi_data/mylog") or die "cannot log, $!"; # carpout(LOG); #} # #END { # close(LOG); #} # rot13 is needed for the offensive material found in the "Adult" category use Rot13; use CGI (":standard"); use CGI (":pretty"); ########################################### # configuration information ########################################### # the Values are the available fortune categories... the filehash maps these categories to filenames @values = ('Normal','Linux','Adult','Shorts','IRC','Random'); %filehash = ( 'Normal'=>'fortunes', 'Linux'=>'linuxcookie', 'Adult'=>'fortunes-o', 'Shorts'=>'zippy', 'IRC'=>'irc'); # possible numbers of fortunes to display @numchoices = ( '1', '5', '10' ); ############################################# # begin "main" ############################################# # print header print header, start_html(-title => "Random \$h!t Generator", -author=>"sound-mind_AT_go-linux.org", -base=>"true", -BGCOLOR=>"330066", -TEXT=>"white", -meta=>{'keywords'=>'random $h!t fortune', 'copyright'=>'Spencer Shimko 2004'} ), h1({align=>'center'}, 'Meaningless random $h!t generator'), h2({align=>'center'}, '(AKA fortunes)'); print p("
\n");
print p({align=>'center'}, strong("Hey it ain't much to look at... but at least we're wasting GL resources!"));
print "
\n";
# parse and test parameters for validity
if (param()) {
$name = param('name');
$num2disp = param('num2disp');
$choice = param('choice');
# make sure name is valid
if ( $name !~ /^[A-Za-z]+$/ ) {
if ( $name =~ /^\s*$/ ){
print p("Empty name... please try again.");
} else {
print p("\"$name\" doesn't look like a proper name. Please enter a proper name.");
}
$badfortune = 1;
}
# make sure the number selection is valid... someone could be malicious
foreach ( @numchoices ){
if ( $_ eq $num2disp ){
$validvalue = 1;
last;
}
}
if ( ! defined $validvalue ){
$badfortune = 1;
print p("Invalid number to display \"$num2disp\"... please choose a number from the provided list.");
}
undef $validvalue;
# make sure the category selection is valid... someone could be malicious
foreach ( @values ){
if ( $_ eq $choice ){
$validvalue = 1;
last;
}
}
if ( ! defined $validvalue ){
$badfortune = 1;
print p("Invalid category selection \"$choice\"... please choose a category from the provided list.");
}
# print the form
print hr();
print_form();
print hr();
# if we haven't had an error (bad fortune) get our users a fortune cookie
if ( ! defined $badfortune ){
# get a few fortunes for our user
callfortune ( );
}
} else { # had to have 2 print_form calls to force errors to appear at above form
# print the form
print hr();
print_form();
print hr();
}
print end_html;
#############################################
# end main
#############################################
##############################################
# begin sub routines
##############################################
sub print_form {
# tried embedding 2 tables using CGI:: but turned out to be a pain... reverted to coding outer table by hand
print '
others generate fortune based upon your name. Adult and Random can be offensive... consider yourself warned. | ';
print '