Contents
HOME
ABOUT
CONTACT

Most Stuff:
HUMOR
QUOTATIONS
OQO
PICTUREBOOK
MY LINKS
PROPAGANDA
E COMMERCE
WEB/CGI
RESUME
GAMING
MUSIC

Not Much:
JAVA
MIATA
WEIRD

Web/CGI/Perl/JavaScript

I wrote a couple Perl scripts to somewhat automate my website. I'm making them available, subject to the conditions documented on this page.

The generate.pl Perl script builds a page from an HTML template and other files based on embedded tags in the template and a configuration file. The gotopage.pl Perl script serves up a page based on the same configuration files that the generate.pl uses, and can log basic visitor information to a file for traffic analysis.

CGI Security

You need to understand Perl programming and CGI programming and CGI security issues before using these files and programs.

The World Wide Web Security FAQ

Books

Perl Cookbook

Learning Perl

Programming Perl

Mastering Regular Expressions

CGIProgramming on the World Wide Web

These can be found on the O'Reilly website.

Vital Read Carefully

These files and programs are provided "as is" without warranty of any kind, either express or implied. The author assumes no responsibility for errors or omissions, or for damages resulting from the use or performance of these files and programs. You assume the entire risk arising out of use or performance of these files and programs.

Instructions

You will need to edit the variables in each script to configure them to work on your site.

Here is an example. Fred hosts his web page on a Unix box. His account resides in /usr/users/fred. He has web pages which end in .html and .htm. His web page is http://www.---.com. On his system, nslookup is /bin/nslookup. He decides to use the defaults in the rest of the variable settings. See the actual scripts for the Variable comments. His variable settings look like this:

generate.pl

## Variables
$htmlext1 = "html";        # See the actual script for the Variable comments.
$htmlext2 = "htm";         # 
$dirseparator = "/";       # 
$alphaorder = "a";         # 
$dateorder = "d";          # 
$reversealpha = "ra";      # 
$reversedate = "rd";       # 
$sortdirkey = "SORT";      # 
$outputkey = "OUTPUT";     # 
$templatekey = "TEMPLATE"; # 
$all = "all";              # 
$dirname = "ERROR";        # 
$pagevisited = "ERROR";    # 
$config = "ERROR";         # 
$websitedirectory = "/usr/users/fred/";          # 
$configdirectory = $websitedirectory."configs/"; # 
$siteconfig = $configdirectory."config.cfg";     # 
%siteconfigfiles;                                # 
%config_prefs;                                   # 
$debug = 0;                                      # 
$debugfile = $websitedirectory."debug.log";      # 
my $lengthlimit = 20;                            # 

gotopage.pl

$pagevisited = "ERROR";                           # See the actual script for 
$webdirectory = "http://www.---.com/";            # the Variable comments.
$websitedirectory = "/usr/users/fred/";           # 
$logfile = $websitedirectory."logs/logfile.txt";  # 
$configdirectory = $websitedirectory."configs/";  # 
$siteconfig = $configdirectory."configs.cfg";     # 
$config = "ERROR";                                # 
%config_prefs;                                    # 
$debug = 0;                                       # 
$debugfile = $websitedirectory."logs/debug2.log"; # 
my $lengthlimit = 20;                             # 
$nslookupprog = "bin/nslookup";                   # 
$outputkey = "OUTPUT";                            # 
$logvisit = 1;                                    # 

Fred creates a configs directory to store his configuration files. He creates a configs.cfg site configuration file in that directory.

configs.cfg

# site configuration file
homepage=/usr/users/fred/configs/homepage.cfg
powertools=/usr/users/fred/configs/powertools.cfg
search=/usr/users/fred/configs/search.cfg
about=/usr/users/fred/configs/about.cfg
contact=/usr/users/fred/configs/contact.cfg

Fred then creates the homepage.cfg and powertools.cfg configuration files.

homepage.cfg

# homepage configuration file
TITLE = Welcome to ---
PAGETITLE = Howdy Y'all
TEMPLATE = /usr/users/fred/tpl/pagetemplate.html
OUTPUT = /usr/users/fred/www/home.html
CONTENT = /usr/users/fred/misc/homecontent.html
LINKS = /usr/users/fred/base/links.html

powertools.cfg

# powertools configuration file
TITLE = Power Tools on the Web
PAGETITLE = Git Yer Power Tools Here!
TEMPLATE = /usr/users/fred/tpl/pagetemplate.html
OUTPUT = /usr/users/fred/www/powertools.html
LINKS = /usr/users/fred/base/links.html
CONTENT = /usr/users/fred/powertools
CONTENTSORT = a

Fred needs to make his pagetemplate.html to hold all this stuff, so he does.

pagetemplate.html

<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
   <meta name="GENERATOR" content="Mozilla/4.6 [en] (Win98; U) [Netscape]">
   <title>--- Yer Source Fer Power Tools! - <!-- INSERT TITLE --></title>
</head>
<body>
<table BORDER=0 CELLSPACING=3 CELLPADDING=2 HEIGHT="100%" >
<tr>
<td ALIGN=LEFT VALIGN=TOP BGCOLOR="#4169E1"><!-- INSERT LINKS --></td>
<td ALIGN=LEFT VALIGN=TOP><h1><!-- INSERT PAGETITLE --></h1>
<br><!-- INSERT CONTENT --></td>
<td ALIGN=LEFT VALIGN=TOP><!-- INSERT THIRDCOLUMN --></td>
</tr>
</table>
</body>
</html>

Note that pagetemplate has comments in the form of <!-- INSERT ... -->, where ... is an entry in the configuration file. Pagetemplate has an item, THIRDCOLUMN, which is not in the configuration files. It is ignored and will not be included in the generated page.

Fred then makes his homecontent.html file. The line(s) between the BODY tags will be loaded in place of the <!-- INSERT CONTENT --> item in pagetemplate.

NOTE: The BODY tags must be on separate lines from the content. Each <!-- INSERT ... --> link must be entirely on one line in a template file or it will not work.

homecontent.html

<html><head></head>
<body>
<p>Fred's <b>BIG</b> shop of <blink>POWER</blink> Tools</p>
</body>
</html>

Below is Fred's links.html file. He uses the gotopage.pl script to track visitors on his site and show the pages.

links.html

<html><head></head>
<body>
<h1>Links</h1>
<a href="http://www.---.com">Fred's</a>
<br><a href="http://www.---.com/cgi/gotopage.pl?search">Search Me</a>
<br><a href="http://www.---.com/cgi/gotopage.pl?about">About Fred</a>
<br><a href="http://www.---.com/cgi/gotopage.pl?contact">Contact Fred</a>
<br><a href="http://www.---.com/cgi/gotopage.pl?powertools">Power Tools</a>
</body>
</html>

When Fred generates the home.html file (the OUTPUT of the home.cfg file) he gets the following:

home.html

<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
   <meta name="GENERATOR" content="Mozilla/4.6 [en] (Win98; U) [Netscape]">
   <title>--- Yer Source Fer Power Tools! - Welcome to ---</title>
</head>
<body>
<table BORDER=0 CELLSPACING=3 CELLPADDING=2 HEIGHT="100%" >
<tr>
<td ALIGN=LEFT VALIGN=TOP BGCOLOR="#4169E1"><h1>Links</h1>
<a href="http://www.---.com">Fred's</a>
<br><a href="http://www.---.com/cgi/gotopage.pl?search">Search Me</a>
<br><a href="http://www.---.com/cgi/gotopage.pl?about">About Fred</a>
<br><a href="http://www.---.com/cgi/gotopage.pl?contact">Contact Fred</a>
<br><a href="http://www.---.com/cgi/gotopage.pl?powertools">Power Tools</a>
</td>
<td ALIGN=LEFT VALIGN=TOP><h1>Howdy Y'all</h1>
<br><p>Fred's <b>BIG</b> shop of <blink>POWER</blink> Tools</p></td>
<td ALIGN=LEFT VALIGN=TOP></td>
</tr>
</table>
</body>
</html>

The next thing on Fred's agenda is making the Power Tools page. He prepares the /usr/users/fred/powertools directory with files he wants included into the generated powertools.html page. As he specified alphabetical sorting (CONTENTSORT = a) he can make a set of pages a.html, b.html, etc. that contain the section headings (A, B, etc.) and intrapage navigation (a Return to top link, for example). He can then make pages for each of his products and name them the same as his products. When the powertools.html page is generated, it will put the content from a.html, then any files for products that start with "a" after it, then b.html, then any files for products that start with "b" after it, etc. Rather than making another contrived example, check out my films page to see what the final generated page might look like. I just add reviews in files like mummy.html and when I regenerate the page they are automatically included in the right place.

Fred is using the logging function in gotopage.pl. This is an example of what he might see in his log. The log is formatted in comma-delimited format so you can pull it into a spreadsheet and analyze it. The log entries shown have been artificially line wrapped to fit the page formatting.

logfile.txt

"1999-06-19 07:11:31","/usr/users/fred/www/home.html","184.222.22.222",
"example.com","Mozilla/4.0 (compatible; MSIE 4.01; Windows 95)" 
"1999-06-19 07:18:41","/usr/users/fred/www/powertools.html","184.222.22.222",
"example.com","Mozilla/4.0 (compatible; MSIE 4.01; Windows 95)"
"1999-06-20 17:21:50","/usr/users/fred/www/home.html","69.000.00.000",
"Unknown","Mozilla/4.6 [en] (Win98; I)" 

Programs

generate.pl

gotopage.pl



Copyright