#!/usr/bin/perl
# This script is used to generate the page that the user has requested.
# The following things are of interest:
# area = the name of the 'directory' e.g. panda or gpg or gcc
# page = the name of the page e.g. index.htm or timetrials
# The data is currently stored in the following hashing structure
# /common = shared page elements e.g. the table layout for news blocks
# / = data elements for a given area
# // = the layout description for that page
# Please don't laugh at my perl, this was all developed as a perl learning
# exercise.
use strict;
use CGI;
# Setup the result of the CGI
my($result, $area, $page, $path, $titleElement, $CONFIG, $dateString, $NEWS, $NEWSITEM,
$newsroot, $newsitemsmax, $numnewsitems, $newspath, $archive, $elapsedtime, $days);
$result = new CGI();
# The path is where to find the information to build the pages
#$path = "/var/www/html/stillhq/";
$path = "/home/httpd/html/";
$area = $result->param('area');
$page = $result->param('page');
$archive = $result->param('archive');
# Output the standard HTML prelude, which includes stuff like the page title
print $result->header;
# If there are no arguements, then output an error message
if(($area eq "") || ($page eq "")){
error("$path", "The address that was passed as input was malformed");
exit;
}
# What if there was no archive arguement?
if($archive eq ""){
$archive=0;
}
# Get the title element
$titleElement=`cat $path/$area/$page.conf | grep TITLE | sed 's/TITLE:[ \t]*//'`;
# We must have a title to be willing to display the page
# If there are no arguements, then output an error message
if($titleElement eq ""){
error("$path", "Either the page you requested was misconfigured, or does not exist");
exit;
}
print $result->start_html(
-title=>"$titleElement",
-bgcolor=>"FFFFFF");
# Output the content of the page -- this is based on the lines in the conf file
# (where we got the title from before)
open CONFIG, "grep -v TITLE $path/$area/$page.conf |" or die;
while (){
if(/^#.*/){
# This is a comment, so we ignore it
}
elsif(/^FILE:[ \t]*(.*)/){
# If the line starts with FILE: then it is a request to include the content of that file
# $1 is the filename that we matched, output it
chomp $1;
dumpfile("$path/$1");
}
elsif(/^GETPAGE.*/){
# This prints out a message about who generated the page and when (useful for debugging)
$dateString=`date`;
print "
Generated by getpage on $dateString. ";
# We also put in the date the config file was last changed so that we can debug versions
$days=int -M CONFIG;
# if($days < 1){
# print "This page was changed today ";
# }
# elsif($days < 2){
# print "This page was changed yesterday ";
# }
# else{
# print "The content of this page was last changed $days days ago ";
# }
# $days=int -A CONFIG;
# if($days < 1){
# print "and has had other vistors today. ";
# }
# elsif($days < 2){
# print "and was last visited yesterday. ";
# }
# else{
# print "and was last visited $days days ago. ";
# }
# And we comment on how long we spent executing
$elapsedtime=time - $^T;
print "Generation of this page took $elapsedtime second(s).";
}
elsif(/^LOCATION.*/){
# This is used to build the site navigation option
print " : $area : $titleElement ";
}
elsif(/^CONTENT:[ \t]*(.*)[ \t]+(.*)/){
# $1 is the name of the content file, and $2 is the name of the leader for look and feel elements
chomp $2;
newsitemdisplay($path, "$path/$area/$1", $2, "DODATE");
}
elsif(/^CONTENTX:[ \t]*(.*)[ \t]+(.*)[ \t]+(.*)/){
# $1 is the name of the content file, and $2 is the name of the leader for look and feel elements
# $3 is whether we should show the date on the headline (NODATE means NO)
chomp $2;
chomp $3;
newsitemdisplay($path, "$path/$area/$1", $2, $3);
}
elsif(/^NEWS:[ \t]*(.*)[ \t]+(.*)[ \t]+(.*)/){
# $1 is the name of the news type, $2 is the number of news items is display, and $3
# is the leader for the look and feel elements
if($archive == 0){
open NEWS, "ls $path/news/$1 | sort -r | head -$2 |";
}
else{
open NEWS, "ls $path/news/$1 | sort -r |";
}
$newsroot=$3;
$newsitemsmax=$2;
$newspath=$1;
# For each news item, output it
while(){
chomp $_;
newsitemdisplay($path, "$path/news/$1/$_", $newsroot);
}
close(NEWS);
# We check to see if there were any news items that were not displayed, because
# we make an archive available
$numnewsitems=`ls $path/news/$newspath | wc -l | tr -d " "`;
if(($numnewsitems > $newsitemsmax) && ($archive == 0)){
dumpfile("$path/$newsroot-start");
print "More News...\n";
dumpfile("$path/$newsroot-divider");
print "There are more items than were displayed. Would you like to read more?";
dumpfile("$path/$newsroot-end");
}
}
else{
# This is a line of content
print $_;
}
}
# Close the config file
close(CONFIG);
# Finish off the page
print $result->end_html;
exit;
# Dump the named file to stdout
sub dumpfile{
my($file) = @_;
my($FILEBIT);
print "\n\n";
open FILEBIT, $file;
while(){
print "$_";
}
print "\n";
close(FILEBIT);
}
# Display an error message to the user
sub error{
my($path, $message) = @_;
dumpfile("$path/common/stdnews-start");
print "Error!";
dumpfile("$path/common/stdnews-divider");
print "$message\n";
print "
\n";
print "Please contact mikal\@stillhq.com and let him know the URL you were trying to access (normally at the top of the page), and what link you clicked on to get here (if there was one).\n";
dumpfile("$path/common/stdnews-end");
print $result->end_html;
}
# Display a news item to the user
sub newsitemdisplay{
my($path, $filename, $newsroot, $dateit) = @_;
my($NEWSITEM);
open NEWSITEM, $filename;
# Output the news starter
dumpfile("$path/$newsroot-start");
# Output the given news item
while(){
if(/^TITLE:[ \t]*(.*)/){
print "$1\n";
dumpfile("$path/$newsroot-divider");
}
elsif(/POSTDATE:[ \t]*(.*)/){
if($dateit ne "NODATE"){
print "$1 - ";
}
}
elsif(/POSTER:[ \t]*(.*)/){
if($dateit ne "NODATE"){
print "";
}
}
elsif(/POSTERNAME:[ \t]*(.*)/){
if($dateit ne "NODATE"){
print "$1
\n";
}
else{
print "\n";
}
}
elsif(/BEGIN CONTENT/){
while(){
print "$_";
}
dumpfile("$path/$newsroot-end");
}
}
close(NEWSITEM);
}