<?xml version="1.0" encoding="windows-1252"?>
<node id="153774" title="Frankenpage: Build on-the-fly customizable web pages from a directory of files" created="2002-03-23 06:08:33" updated="2005-08-11 09:39:53">
<type id="1748">
sourcecode</type>
<author id="46752">
S_Shrum</author>
<data>
<field name="doctext">
&lt;code&gt;
#!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		=&gt;	"",
					debug		=&gt;	"",
);

# define script properties (don't change these)
my %script = (		name		=&gt;	"frankenpage",
					version		=&gt;	"1.02",
					release		=&gt;	"Final",
					author		=&gt;	"Sean Shrum",
					email		=&gt;	"sean\@shrum.net",
					created		=&gt;	"2001.03.22",
					modified	=&gt;	"2002.03.23",
					status		=&gt;	"open source",
					distribute	=&gt;	"http://www.shrum.net/programming",
);

# define script error messages (don't change these)
my %errors = (		header		=&gt;	"$script{'name'} v. $script{'version'} [ $script{'release'} ] by $script{'author'} ( $script{'email'} ) - $script{'status'}\n\n",
					nopath		=&gt;	"No PATH parameter was specified.\n",
					badpath		=&gt;	"Unable to open the specified PATH.\n",
					badsource	=&gt;	"Unable to open the specified FILE.\n",
);

################################################################################
#
#	WHAT DOES THIS SCRIPT DO?
#	Reads a directory of files and builds a form that allows you to specify 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 thing you
#	see.  Not with my scripts.  Why?  Well, it makes it easier to parse the
#	script file for various important items, things like the version #,
#	release, status, etc.  As a result, I moved this section below them in
#	the event that I write something like "..this version contains...", my
#	parsing scripts don't get the two confused.  This is really for my 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 centralized.
#	on my website.  Go to www.shrum.net/programming and do a search
#	on the script name for the latest source and white paper discussion
#	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 "&amp;" =&gt; @ARGV ); } else { $input = new CGI; }

# check for input; if no parameters are supplied, redirect to technical white paper
unless ( $input-&gt;param() ) { print $input-&gt;redirect( $script{'distribute'} . '/redirects/' . $script{'name'} . '.shtml' ); exit; }

# set defaults for parameters omitted by user
for ( keys %defaults ) { $input-&gt;param(-name=&gt;$_,-value=&gt;$defaults{$_}) unless $input-&gt;param($_); }

# check for required parameters
if ( ! defined $input-&gt;param('path') ) { die $errors{'header'} . $errors{'nopath'}; }

# see if this is part of a SSI call, if not print out the HTML header
print $input-&gt;header unless @ARGV;

################################################################################

# for debugging
if ( $input-&gt;param('debug') ) { print "&lt;p&gt;Retrieving file list..."; }

# open the user specified directory
opendir (DIR, $rootPath . $input-&gt;param('path')) or die $errors{'header'} . $errors{'badpath'};

# fill the array with all the filenames in the specified PATH
#@files = grep { !/^\./ &amp;&amp; -f } map {$rootPath . $input-&gt;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-&gt;param('debug') ) { print $num_files . " found."; }

# initalize $page
my $page = "";

# see if we are building the final resume
if ( $input-&gt;param('s1') ne "" ) {

	# for debugging
	if ( $input-&gt;param('debug') ) { print "&lt;hr&gt;&lt;p&gt;Building PAGE...&lt;/p&gt;"; }

	# for debugging
	if ( $input-&gt;param('debug') ) { print "&lt;hr&gt;&lt;p&gt;...adding section "; }

	# loop through each possible iteration
	foreach ( @files ) {

		# increment the count
		$count++;

		# for debugging
		if ( $input-&gt;param('debug') ) { print $count . ","; }

		# see if the user selected a entry
		$skey = "s" . $count;
		if ( $input-&gt;param($skey) ne "" ) {

			# retrieve the defined file
			$section = &amp;get_data( $rootPath . $input-&gt;param('path') . "/" . $input-&gt;param($skey) );

			# see if the user requested a HR be inserted above this section
			$ckey = "c" . $count;
			if ( $input-&gt;param($ckey) ) { $break = "&lt;hr&gt;"; } else { $break = ""; }

			# add the result to the page
			$page = $page . $break . $section;

		}

	}

} else {

	# for debugging
	if ( $input-&gt;param('debug') ) { print "&lt;hr&gt;&lt;p&gt;Building FORM...&lt;/p&gt;"; }

	# build the section_template
	$page = "&lt;H1&gt;Welcome to Frankenpage&lt;/h1&gt;&lt;p&gt;&lt;font size=2&gt;The &lt;i&gt;on-the-fly-slap-it-together&lt;/i&gt; web page construction Script.&lt;/font&gt;&lt;P&gt;Looks like you'll be building a page from the contents of " . $input-&gt;param('path') . ".";

	# make the script call itself again
	$page = $page . "&lt;p&gt;&lt;form method=\"GET\" action=\"" . $ENV{'SCRIPT_NAME'} . "\"&gt;";

	# for debugging
	if ( $input-&gt;param('debug') ) { print "&lt;hr&gt;&lt;p&gt;...adding section "; }

	# create a combobox for each file (in case the user wants everything).
	foreach ( @files ) {

		# increment the count
		$count++;

		# for debugging
		if ( $input-&gt;param('debug') ) { print $count . ","; }

		# start off the section
		$section = "&lt;P&gt;" . $count . ". &lt;select size=\"1\" name=\"s" . $count . "\"&gt;&lt;option selected&gt; &lt;/option&gt;";

		# create a entry for each file in each combobox.
		foreach ( @files ) { $section = $section . "&lt;option&gt;" . $_ . "&lt;/option&gt;"; }

		# cap off the SELECT tag and add a checkbox
		$section = $section . "&lt;/select&gt;  &lt;input type=\"checkbox\" name=\"c" . $count . "\" value=\"y\"&gt; Break before this section with hortizontal rule";

		# add the section to the page
		$page = $page . $section;

	}

	# for debugging
	if ( $input-&gt;param('debug') ) { print "&lt;/p&gt;"; }

	# add a submit button and cap off the FORM tag
	$page = $page . "&lt;p&gt;&lt;input type=\"hidden\" name=\"path\" value=\"" . $input-&gt;param('path'). "\"&gt;&lt;input type=\"Submit\" value=\"Built it!\" name=\"B1\"&gt;&lt;input type=\"reset\" value=\"Start Over\" name=\"B2\"&gt;&lt;/form&gt;";

}

# for debugging
if ( $input-&gt;param('debug') ) { print "&lt;hr&gt;&lt;p&gt;Printing...&lt;/p&gt;"; }

# 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-&gt;param('debug') ) { print "&lt;hr&gt;&lt;p&gt;...openning locally hosted file: $source&lt;P&gt;"; }

		# 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 = &lt;DATA&gt;;

		# close the file
		close (DATA);

	}

	# return the results
	return $data;

}&lt;/code&gt;</field>
<field name="codedescription">
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?  franken&lt;i&gt;page&lt;/i&gt;...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 &lt;a href="http://www.shrum.net/programming/whitepapers/frankenpage.shtml"&gt;here&lt;/a&gt;.  It could probably do more but hey, I wrote it in like 30 minutes.  &lt;i&gt;"IT'S ALIVE!"&lt;/i&gt;</field>
<field name="codecategory">
Web Stuff</field>
<field name="codeauthor">
Sean Shrum</field>
</data>
</node>
