#!/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 ''; print '
    Directions:
  1. Enter name (required)
  2. Choose number to display (required)
  3. Choose type (required)
  4. Click generate (required)
Note: Random generates random fortune,
others generate fortune based upon your name.
Adult and Random can be offensive... consider yourself warned.
', start_form(-method => 'POST', -action => './index.cgi'), table( Tr ( {align=>LEFT}, [ td ( [ "Enter your name: ", textfield (-name => 'name', -value => $name ) ] ), td ( [ "Number to display: ", popup_menu(-name=>'num2disp', -values=>[@numchoices], -default=>'Normal' ) ] ), td ( [ "Category: ", popup_menu(-name=>'choice', -values=>[@values], -default=>'Normal' ) ] ), td ( {colspan=>'2', align=>CENTER}, [ submit ( -value => 'Generate') ] ), ] ) ), end_form, '
' ; } sub callfortune { print p(h3("$name asked for it so here it is... some meaningless \$h!t. Don't read too much into it, you might hurt yourself.")); # if the category isn't random set some values and seed based on the name if ( $choice ne 'Random' ){ $file = $filehash{"$choice"}; srand(unpack("%32b*",$name)); } # open the file and retrieve a quote # grab a random number using the number of quotes in the file as the max rand print "

"; } # retrieve and print quote sub getquote { $num = shift; # loop through the file trying to find quote my $cnt = 0; QUOTE: while ( ){ if ( $cnt == $num ) { #if we have an offensive/adult file if ( $file =~ /.*-o$/ ) { my $crypto = new Crypt::Rot13; $crypto->charge($_); print CGI::escapeHTML( $crypto->rot13() ) . '
'; } else { print CGI::escapeHTML("$_") . '
'; } while ( ){ last QUOTE if /^%$/; #if we have an offensive/adult file if ( $file =~ /.*-o$/ ) { my $crypto = new Crypt::Rot13; $crypto->charge($_); print CGI::escapeHTML( $crypto->rot13() ) . '
'; } else { print CGI::escapeHTML("$_") . '
'; } } } else { $cnt++ if /^\s*%\s*$/; } } return; }