Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Frankenpage: Build on-the-fly customizable web pages from a directory of files

by S_Shrum (Pilgrim)
on Mar 23, 2002 at 11:08 UTC ( #153774=sourcecode: print w/ replies, xml ) Need Help??

Category: Web Stuff
Author/Contact Info Sean Shrum
Description: I made this script to deal with the hassle of having to maintain 4 different resumes for job recruiters. Split up a page into individual files and place them into a folder. Point the script at the folder (via the PATH parameter in the script call or by defining it as a default) and it will allow you to define a page with the files. Upon submitting the form, the script gets called again but this time will create the document from the parts you requested. The resulting URL can then be used as a link in your other web pages to display the frankenstein'ed page later (Get it? frankenpage...never mind). Acts as a pseudo SSI engine (I guess you could call it that). For more information, latest version, etc., you can view the script white paper here. It could probably do more but hey, I wrote it in like 30 minutes. "IT'S ALIVE!"
#!c:/Perl/bin/perl
    ## HOST: intranet

#!/usr/bin/perl
    ## HOST: 1dollarhosting

#!/usr/local/bin/perl
    ## HOST: verio

# define script defaults (change these to suit your needs)
my %defaults = (    path        =>    "",
                    debug        =>    "",
);

# define script properties (don't change these)
my %script = (        name        =>    "frankenpage",
                    version        =>    "1.02",
                    release        =>    "Final",
                    author        =>    "Sean Shrum",
                    email        =>    "sean\@shrum.net",
                    created        =>    "2001.03.22",
                    modified    =>    "2002.03.23",
                    status        =>    "open source",
                    distribute    =>    "http://www.shrum.net/programm
+ing",
);

# define script error messages (don't change these)
my %errors = (        header        =>    "$script{'name'} v. $script{
+'version'} [ $script{'release'} ] by $script{'author'} ( $script{'ema
+il'} ) - $script{'status'}\n\n",
                    nopath        =>    "No PATH parameter was specifi
+ed.\n",
                    badpath        =>    "Unable to open the specified
+ PATH.\n",
                    badsource    =>    "Unable to open the specified F
+ILE.\n",
);

######################################################################
+##########
#
#    WHAT DOES THIS SCRIPT DO?
#    Reads a directory of files and builds a form that allows you to s
+pecify what
#    to include/omit in the final result.  Upon submitting, the script
+ pulls the
#    files you selected in the order your chose and builds the page.
#
#    WHY ARE THE NOTES DOWN HERE?
#    Typically, you open up a script and this section is the first thi
+ng you
#    see.  Not with my scripts.  Why?  Well, it makes it easier to par
+se the
#    script file for various important items, things like the version 
+#,
#    release, status, etc.  As a result, I moved this section below th
+em in
#    the event that I write something like "..this version contains...
+", my
#    parsing scripts don't get the two confused.  This is really for m
+y own
#    reasons.  Once you have d/l'ed the script, feel free to move this
+ section
#    where ever you like.
#
#    WHERE ARE THE SCRIPT REQUIREMENTS, INPUT, OUTPUT, FAQs, ETC.?
#    To reduce the amount of redundant write up that I have to do,
#    all info pertaining to the use of this script has been centralize
+d.
#    on my website.  Go to www.shrum.net/programming and do a search
#    on the script name for the latest source and white paper discussi
+on
#    Use these resources first if you need any information.
#
#    SUPPORT AND LEGAL ISSUES:
#    See http://www.shrum.net/programming/legal for information.
#
######################################################################
+##########

# to access REMOTE files
use LWP::Simple;

# to handle parameter passing
use CGI;

# to display more meaningful error messages to the browser
use CGI::Carp qw(fatalsToBrowser);

# check if the DOCUMENT_ROOT environment variable is defined
my $rootPath = "$ENV{'DOCUMENT_ROOT'}/" || "";

# get the parameters; 1st from the ARGV array or the QUERY_STRING
if (@ARGV) { $input = new CGI ( join "&" => @ARGV ); } else { $input =
+ new CGI; }

# check for input; if no parameters are supplied, redirect to technica
+l white paper
unless ( $input->param() ) { print $input->redirect( $script{'distribu
+te'} . '/redirects/' . $script{'name'} . '.shtml' ); exit; }

# set defaults for parameters omitted by user
for ( keys %defaults ) { $input->param(-name=>$_,-value=>$defaults{$_}
+) unless $input->param($_); }

# check for required parameters
if ( ! defined $input->param('path') ) { die $errors{'header'} . $erro
+rs{'nopath'}; }

# see if this is part of a SSI call, if not print out the HTML header
print $input->header unless @ARGV;

######################################################################
+##########

# for debugging
if ( $input->param('debug') ) { print "<p>Retrieving file list..."; }

# open the user specified directory
opendir (DIR, $rootPath . $input->param('path')) or die $errors{'heade
+r'} . $errors{'badpath'};

# fill the array with all the filenames in the specified PATH
#@files = grep { !/^\./ && -f } map {$rootPath . $input->param('path')
+ . "/" . $_ } readdir DIR;
@files = readdir DIR;

# close the directory
closedir(DIR);

# sort the array
@files = sort @files;

# get the number of files
$num_files = $#files + 1;

# for debugging
if ( $input->param('debug') ) { print $num_files . " found."; }

# initalize $page
my $page = "";

# see if we are building the final resume
if ( $input->param('s1') ne "" ) {

    # for debugging
    if ( $input->param('debug') ) { print "<hr><p>Building PAGE...</p>
+"; }

    # for debugging
    if ( $input->param('debug') ) { print "<hr><p>...adding section ";
+ }

    # loop through each possible iteration
    foreach ( @files ) {

        # increment the count
        $count++;

        # for debugging
        if ( $input->param('debug') ) { print $count . ","; }

        # see if the user selected a entry
        $skey = "s" . $count;
        if ( $input->param($skey) ne "" ) {

            # retrieve the defined file
            $section = &get_data( $rootPath . $input->param('path') . 
+"/" . $input->param($skey) );

            # see if the user requested a HR be inserted above this se
+ction
            $ckey = "c" . $count;
            if ( $input->param($ckey) ) { $break = "<hr>"; } else { $b
+reak = ""; }

            # add the result to the page
            $page = $page . $break . $section;

        }

    }

} else {

    # for debugging
    if ( $input->param('debug') ) { print "<hr><p>Building FORM...</p>
+"; }

    # build the section_template
    $page = "<H1>Welcome to Frankenpage</h1><p><font size=2>The <i>on-
+the-fly-slap-it-together</i> web page construction Script.</font><P>L
+ooks like you'll be building a page from the contents of " . $input->
+param('path') . ".";

    # make the script call itself again
    $page = $page . "<p><form method=\"GET\" action=\"" . $ENV{'SCRIPT
+_NAME'} . "\">";

    # for debugging
    if ( $input->param('debug') ) { print "<hr><p>...adding section ";
+ }

    # create a combobox for each file (in case the user wants everythi
+ng).
    foreach ( @files ) {

        # increment the count
        $count++;

        # for debugging
        if ( $input->param('debug') ) { print $count . ","; }

        # start off the section
        $section = "<P>" . $count . ". <select size=\"1\" name=\"s" . 
+$count . "\"><option selected> </option>";

        # create a entry for each file in each combobox.
        foreach ( @files ) { $section = $section . "<option>" . $_ . "
+</option>"; }

        # cap off the SELECT tag and add a checkbox
        $section = $section . "</select>  <input type=\"checkbox\" nam
+e=\"c" . $count . "\" value=\"y\"> Break before this section with hor
+tizontal rule";

        # add the section to the page
        $page = $page . $section;

    }

    # for debugging
    if ( $input->param('debug') ) { print "</p>"; }

    # add a submit button and cap off the FORM tag
    $page = $page . "<p><input type=\"hidden\" name=\"path\" value=\""
+ . $input->param('path'). "\"><input type=\"Submit\" value=\"Built it
+!\" name=\"B1\"><input type=\"reset\" value=\"Start Over\" name=\"B2\
+"></form>";

}

# for debugging
if ( $input->param('debug') ) { print "<hr><p>Printing...</p>"; }

# display page
print $page;

# quit
exit;

########################################

sub get_data ( $source ) {

    # pass the arguments to localized variables
    my $source = $_[0];

    # make sure that the source is defined
    if ( $source ) {

        # for debugging
        if ( $input->param('debug') ) { print "<hr><p>...openning loca
+lly hosted file: $source<P>"; }

        # open the local file
           open ( DATA, $source ) or die $errors{'header'} . $errors{'
+badsource'} . "\n\nSource: $source";

        local($/) = undef;

        # place the raw contents of the file into a variable
        $data = <DATA>;

        # close the file
        close (DATA);

    }

    # return the results
    return $data;

}

Comment on Frankenpage: Build on-the-fly customizable web pages from a directory of files
Download Code

Back to Code Catacombs

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://153774]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (11)
As of 2014-09-16 14:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (26 votes), past polls