Приглашаем посетить
Арцыбашев (artsybashev.lit-info.ru)

Random Banner Generator

#!/usr/local/bin/perl

# Name: Selena Sol's Random Banner Generator
#
# Version: 4.0
#
# Last Modified: 07-31-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.


# Note: Take a look at the sample HTML file located in the sub-directory
# Html...it contains an important line <!--IMG GOES HERE--> which must
# appear in any HTML file you want to display a random banner in.
# Once you have configured the setup variables and the HTML file to the
# specifics of your own server setup, you can try out the random banner
# generator.  

#######################################################################
#                       Print http Header.                            #
#######################################################################

# First tell Perl to bypass the buffer so that information generated by
# the CGI will be sent immediately to the browser. The line $! = 1; does
# this. Then, print out the http header so that we will easily be able to
# debug and so that the browser will not time us out.

  $| = 1;
  print "Content-type: text/html\n\n";

#######################################################################
#                       Require Libraries.                            #
#######################################################################

# Further, cgi-lib.pl is loaded and the routine ReadParse is used to read
# and parse any incoming url encoded data.

  require "./random_banner.setup";
  require "$location_of_cgi_lib";
  &ReadParse(*form_data);

#######################################################################
#                       Initialize the Randomizer.                    #
#######################################################################

# Then, Perl's randomizer is accessed using the srand command.

  srand (time|$$);


#######################################################################
#                       Define Database and Html File to Use          #
#######################################################################

# The URL encoded data which may be coming in as form data will be the
# name of the image file that this script should use to find the locations
# of the images as well as the hyperlinks those images are associated
# with. If there was an image file specified in the URL, the script will
# assign that value to the variable $image_database, if there is none, the
# script will use the value of $default_image_list as was defined above.

  if ($form_data{'image_list'} ne "")
    {
    $image_database = $form_data{'image_list'};
    }
  else
    {
    $image_database = "$default_image_list";
    }

# Similarly, the script must determine which HTML file it should display
# with the randomly generated ad.  By default, it will load the HTML file
# defined in  $default_html_file.  However, if the client has specified an
# alternate file to load in the URL string, the script will load that
# instead.

  if ($form_data{'html_file'} ne "")
    {
    $html_file = "$html_directory_path/$form_data{'html_file'}";
    }
  else
    {
    $html_file = "$default_html_file";
    }

#######################################################################
#                       Get the Random Banner and Link.               #
#######################################################################

# Next, the script opens the image file, dying with CgiDie if there is a
# problem opening the image file specified in the URl or defined by
# $default_image_list.

  open (IMAGE_DATABASE, "$image_database") || 
        &CgiDie ("I am sorry, but I was not able to open the image
	databse in the Get the Random Banner and Link routine. The value I
	have  is $image_database.  Would you please check the path and
	permissions.");

# It then goes through the image list file a line at a time and, for every
# line, it gathers the image and the associated hyperlink by splitting the
# line by the pipe (|) symbol.  Further, it pushes the location of the
# image to the list array @imagelist and the associated URL to @url_list.  
# Finally, it closees the image list # file.

  while (<IMAGE_DATABASE>)
    {
    ($image, $url) = split (/\|/, $_);
    chop $url;
    push (@imagelist, $image);
    push (@url_list, $url);
    }
  close (IMAGE_DATABASE);

# Now that the script has a liust of all the images in the image file, it
# uses Perl's randomizer function to choose one of them at random.  Since
# "@imagelist" is interpreted by Perl as the number of values in the
# array, $random_number is set to be a random (rand) integer value (int)
# from zero to the number of images in the @images array.

  $random_number = int(rand(@imagelist));

# Then, the script takes the number assigned to $random_number and access
# the array element in @imagelist that is associated with the number.
# $random_image then becomes the name of one of the images in @imagelist.

  $random_image = $imagelist[$random_number];
  $random_url = $url_list[$random_number];

#######################################################################
#                      Insert Random Banner Into Page                 #
#######################################################################

# Next, the script opens the HTML file that the client has requested to
# see, using CgiDie if it cannot be opened for some reason.

  open (HTML_FILE, "$html_file") ||
        &CgiDie ("I'm sorry, but I was unable to open the requested
	HTML file in the Insert Random Banner Into Page routine.  The
	value I have is $html_file.  Would you please check the path and
	the permissions for the file.");

# The script then reads through the HTML file a line at a time.

  while (<HTML_FILE>)
    {

# If it comes upon a line which looks like the following:
# <!--IMG GOES HERE-->
# the script knows that it is supposed to replace the line with the
# randomly generated image and the associated hyperlink. So, using the
# "here" method of printing, it replaces the line with the HTML code.
# Remember that since the greater than (>) and less tha (<) symbols
# are Perl special characters and must be escaped if they are to be
# treated as patterns to match for.

    if (/\<!--IMG GOES HERE--\>/)
      {
      print qq!
      <A HREF = "$random_url">
      <IMG SRC = "$image_url/$random_image"></A>!;
      }

# If the line is not the special tag, the script simply prints out the
# line. Thus, every line in the HTML file will be displayed through the
# Web browser except for the special tag line, which will be replaced with
# the <IMG> and <A HREF> tags.

    else
      {
      print "$_";
      }
   } # End of while (<HTML_FILE>)

# Finally, the HTML file is closed and the script exits.

  close (HTML_FILE);