#!/usr/bin/perl # -*- perl -*- # # htdbenter.cgi - enter a new item a database directory # RMM, 15 Mar 97 # # This CGI script allows you to enter a new file into a database directory. # The format of the entry is controlled by a template located in the # database directory. Input to this script comes from a HTML form. # # This script should be called as # # http://machine/path/htdbenter.cgi? # or # http://machine/path/htdbenter.cgi?db=&args # # where is the name of the configuration file that defines # all of the information needed to generate the listing. The optional # arguments are: # # file=name initilize template using values from file # # Include the necessary libraries require "cgi-lib.pl"; require "htdblib.pl"; require "ctime.pl"; if (&htdbinit) { # See if we were called from entry form or not if ($in{'_dbconfig'}) { # Called from entry form => save or preview data if ($in{'save'}) { # If a filename has been given and it is unique, save to file chdir "$DBPath" || &CgiError("HTDB/Enter: can't find database directory $DBPath"); # If tag is present, generate a default file name from the data if ($in{'tag'}) { $fname = $in{'tag'} . ".html"; } # Check for user function to generate file name if (defined &GenerateFname) { $fname = &GenerateFname($in); } # Make sure a filename exists before doing anything if ($fname) { # Make sure the file doesn't exist, unless we are editing if (-e $fname) { $backup = $fname . "-old"; rename($fname, $backup); } # Everything looks set; open the file, print, and finish open(OFILE, ">$fname") || &CgiError("couldn't create $fname") || die; &PrintTemplate(*OFILE); close(OFILE); chmod 0666, $fname; # Let the user know what we saved print &PrintHeader; $url = $DBURL . "/" . $fname; print "
$fname\n";
		print "
Previous version saved as $backup\n" if $backup; print "
\n"; &PrintTemplate(*STDOUT); } } else { # Preview template file with the data from the form print &PrintHeader; &PrintTemplate(*STDOUT); } } else { # Compute the tag to use in the entry form $in{tag} = ($in{nexttag} = &NextTag); # Set today's date in case someone wants it $in{today} = localtime(); # See if there is a file that we should use for initialization if ($in{file}) { # Collect the names of the tags used in the form (htdb{*}) @taglist = &ScanForm; # Open the file and read the contents $contents = &htdb::readfile($in{file}); # Parse all of the tags that we can identify $in{tag} = $in{file}; $in{tag} =~ s/(.*).html/$1/; for (@taglist) { if ($val = &htdb::findtag($_, $contents)) { $in{$_} = $val; } } } # Now print out the form &PrintForm; } } else { &CgiError("HTDB error: missing argument"); } # # ScanForm - subroutine to scan a form for database tags # # Can through a form and find all references to $htdb{tag}. Returns a # list of all tags found in the file. # # The name of the file containing the form is specified in the dbconfig # file and defaults to "entryform.html" in the database directory. # sub ScanForm { # Go to the database directory and look for the entry form chdir "$DBPath" || return undef; open (F, "entryform.html") || return undef; while () { if (//i) { # Skip perl commands to avoid confusion while () { last if ($_ =~ m++i); } } else { if (/\$htdb{(\w*)}/) { push(@list, $1); } } } close (F); return @list; } # # PrintForm - subroutine to print a form for entering the data # # The name of the file containing the form is specified in the dbconfig # file and defaults to "entryform.html" in the database directory. # sub PrintForm { # Go to the database directory and look for the entry form chdir "$DBPath" || &CgiError("HTDB/PrintForm: can't find database directory $DBPath"); open (entryF, "entryform.html") || &CgiError("HTDB/PrintForm: can't open entryform.html"); # Print out the entry form print &PrintHeader; while () { if (//) { # Grab perl commands until we hit the end of the tag $command = ""; while () { last if ($_ =~ m++i); $command .= $_; } # Execute the commands and print any error messages eval $command; # Debugging line: print out perl script in html file if needed if ($in{'debug'}) { print "\n" if $command; } } else { s/\$htdb{(\w*)}/$in{$1}/g; print; } } close(entryF) } # # PrintTemplate - Use a template to generate a database file # # The name of the file containing the template is specified in the dbconfig # file and defaults to "template.html" in the database directory. # sub PrintTemplate { my $HTfile = shift; # Go to the database directory and look for the entry form chdir "$DBPath" || &CgiError("HTDB/PrintTemplate: can't find database directory $DBPath"); open (F, "template.html") || &CgiError("HTDB/PrintTemplate: can't open template.html"); # Print out the entry form while () { if (//i) { # Grab perl commands until we hit the end of the tag $command = ""; while () { last if ($_ =~ m++i); $command .= $_; } # Execute the commands and print any error messages if ($in{'debug'}) { print $HTfile "\n"; } eval $command; print $HTfile "\n" if $@; } else { s/\$htdb{(\w*)}/$in{$1}/g; print $HTfile $_; } } } # # NextTag - generate the next tag in the sequence # sub NextTag { # Check to see if there is a user function for parsing filenames $parsefname = defined &ParseFname ? \&ParseFname : htdb::ParseFname; # Get the names of the files in the database directory $getfnames = defined &GetFnames ? \&GetFnames : htdb::GetFnames; if (@filenames = &{$getfnames}($DBPath)) { for (@filenames) { # Parse the filname and extract the ID and tag information ($id, $tag) = &{$parsefname}($filename = $_); last if $id; } # Now increment the document number (allow multiple types) if ($id =~ /(\d*)([a-z])/) { $year = $1; $letter = $2; } if ($id =~ /(\d*-)(.*)/) { $year = $1; $letter = $2; } return $year . ++$letter; } }