Приглашаем посетить
Fortune Cookie
#!/usr/local/bin/perl
# Name: Selena Sol's Fortune Cookie
#
# Version: 3.0
#
# Last Modified: 08-14-96
#
# Copyright info: This application was written by Selena Sol
# (selena@eff.org, http://www.eff.org/~erict) having been inspired by
# countless other Perl authors. Feel free to copy, cite, reference,
# sample, borrow, resell or plagiarize the contents. However, if you
# don't mind, please let me know where it goes so that I can at least
# watch and take part in the development of the memes. Information wants
# to be free, support public domain freware. Donations are appreciated
# and will be spent on further upgrades and other public domain scripts.
#
# Finally, PLEASE SEND WORKING URL's to selena@eff.org. I maintain a list
# of implementations around the world.
#######################################################################
# Require and Helper Functions #
#######################################################################
# First tell Perl to bypass the buffer so that the fortunes are displayed
# directly to the browser. Then, fire up srand so that we will be able to
# generate random numbers. Then require cgi-lib.pl and flush.pl to gather
# form data and to clear the buffer. Then use the ReadParse subroutine
# in cgi-lib.pl to parse the incoming data and drop it into an
# associative array called %form_data
$| = 1;
srand (time|$$);
require "./nph-fortunes.setup";
require "./nph-fortunes.html";
require "$location_of_cgi_lib";
&ReadParse(*form_data);
###########################################################################
# Figure Out Which Data File to Use #
###########################################################################
# Now assign the incoming user defined fortune_file to the $fortune_file
# variable. Thus if the user typed ...
# http://www.foobar.com/nph-fortunes.cgi?fortune_file=foobar
# Then, $fortune_file would become foobar.dat.
# If, however, the user just called this script directly, without
# asking to see a particular data file, let's assign them a default data
# file.
if ($form_data{fortune_file} eq "")
{
$fortune_file = "$fortune_file_directory/$default_data_file.dat";
}
else
{
$fortune_file = "$fortune_file_directory/$form_data{'fortune_file'}.dat";
}
###########################################################################
# Print http Header. #
###########################################################################
# Now let's print out the HTTP header letting the browser know that we
# are going to send multiple documents such that new documents should
# replace old ones...The browser will know that we are sending a new
# document when we send it the flag --ARandomString\n.
print "$ENV{'SERVER_PROTOCOL'} 200 OK\n";
print "Server: $ENV{'SERVER_SOFTWARE'}\n";
print "Content-type: multipart/x-mixed-replace;boundary=ARandomString\n\n";
print "--ARandomString\n";
###########################################################################
# Print Out The Fortunes. #
###########################################################################
# Now, let's print out our fortunes. This for loop basically counts from
# 0 to the number that the admin has set for $number_of_fortunes_to_display
# Thus, for every number from zero to $number_of_fortunes_to_display
# we'll print the following...
for ($loop = 0; $loop <= $number_of_fortunes_to_display; $loop++)
{
print "Content-type: text/html\n\n";
&page_header;
# Grab a randomly generated fortune from the subroutine get_fortune at
# the end of this script...however, send it the name of the fortune file as
# a parameter so that the subroutine will know which data file to grab
# the fortune from.
print &get_fortune($fortune_file);
# Print out the rest of the HTML.
&page_footer;
# Now take a break while the client reads the fortune. Wait for as many
# seconds as the admin has set for $number_seconds_to_display. Then tell
# the browser that we are about to overwrite the existing information with
# a new fortune.
# Then go on to the next fortune ($loop incrementing by one)
sleep ($number_seconds_to_display);
print "\n--ARandomString\n";
}
###########################################################################
# Randomly Generate a Fortune #
###########################################################################
sub get_fortune
{
# Assign to the local variable $fortune_file the parameter sent from above.
local ($fortune_file) = @_;
# Open that file for reading
open (FORTUNE_FILE, "$fortune_file") || &CgiDie ("I am sorry, but I
was unable to open the fortune file in the Randomly Generate a Fortune
routine. The value I have is $fortune_file. PLease check the
permissions and path.");
# Read through the data file one line at a time...
while (<FORTUNE_FILE>)
{
# If the line you are reading is not %% folllowed by a new line, add the
# line to a continually growing variable $fortune (.= means to add to the
# end of the variable rather than resetting the variable) ($_ is another
# name for the current line we are reading)
if ($_ ne "%%\n")
{
$fortune .= "$_";
}
# If the line is %%newline however, let's add to the array @fortunes the
# current value of $fortune, then let's reset $fortune. What this does
# is basically create a huge array in which each element is a separate
# fortune. The %% defines the beginning and ending of fortunes. Note,
# can you see why you must have a %% as the last line of the data file?
# Otherwise we lose the last fortune, because it never gets "pushed" into
# the array.
else
{
push (@fortunes, $fortune);
$fortune = "";
}
}
# Once we go through all the lines, close up the data file.
close (FORTUNE_FILE);
# Then we'll need to return to the main routine to print a random fortune
# from the array @fortunes. First we'll figure out how many elements are
# in @fortunes. As it happens, @fortunes by itself equals the number of
# elements in @fortunes. If there are 10 fortunes, @fortunes will equal
# 10. Then we will pick a random number from 1 to 10, rand(@fortunes).
# But, we will get some funky result like 5.43245231. So we round that
# number off to an integer with the int function.. Now that we have a
# random number from 0 to the number of elements in @fortunes we will
# figure out which number in @fortunes that corresponds to and grab that
# elemnt out to print up above. splice(@fortunes,...
splice(@fortunes,int(rand(@fortunes)),1)."\n";
}