(back to the source index) midday

commented source





#!/usr/bin/perl # This script is part of the NoonQuilt project: # # <insert URL> # # © Ali Graham, 1998 <mailto:agraham@hal9000.net.au> # # (See makeQuilt.pl for the version history.) # # Start of patchForm.pl (process the form data) # ################################################################ # make sure this is syntactically clear... use strict; use English; # ...and running under Perl5... require 5.001; # uses a module available from # CPAN ( http://www.perl.com/CPAN/ ), # CGI_Lite # set the include path to recognise it use lib 'perllib'; # include the external functions my $path = ""; if ( $OSNAME =~ /^mswin/i ) { $path = "d:\\Inetpub\\wwwroot\\quilt\\scripts\\"; chdir($path); }

(I needed to add the above lines to the start of every script, otherwise the Perl interpreter on Windows NT would not change to the directory the scripts were running in -- this is inconsistent with the behaviour on Unix, Amiga & Macintosh Perl, which did.)



(require $path."nq_config.pl") or (die "Can't open \"nq_config.pl\": $!\n"); (require $path."nq_mkpatch.pl") or (die "Can't open \"nq_mkpatch.pl\": $!\n"); (require $path."nq_dirstuff.pl") or (die "Can't open \"nq_dirstuff.pl\": $!\n"); # ################################################################ # Start of main() use CGI_Lite; my $cgi = new CGI_Lite (); my %cgi_data = $cgi->parse_form_data(); my $defaults = &nqco_getScriptDefaults; my $message; my $error; my $params = &nqco_getParameters; # check for fields that are not included in nq_config.pl foreach my $cgi_key (keys %cgi_data) { if ( ($cgi_key ne "subm_type") && ($cgi_key ne "subm_file") && ($cgi_key ne "subm_action") && not( defined($params->{$cgi_key}) ) ) { $error .= "Form has an invalid field ($cgi_key)\n\n"; } }

The code just above checks that each element of CGI data is either an internal message (i.e. one of the first three) or a component of the submission form (as defined in submit.htm and as generated in editPatch.pl). If not, an error message will be printed. Handy when building other pages/scripts which use this one, as problems will be caught in testing rather than slipping through....



# copy values from CGI data to parameters, raising # an error if necessary fields are not filled in foreach my $key (keys %{$params}) { my $data_def = defined($cgi_data{$key}); if ( $params->{$key}{'Mode'} eq "boolean" ) { $params->{$key}{Value} = $data_def?"on":"off"; } elsif ( $data_def && (length($cgi_data{$key})>0) ) { $params->{$key}{Value} = $cgi_data{$key}; } elsif ( defined($params->{$key}{'Compulsory'}) ) { $error .= "$params->{$key}{'CompulsErr'}\n\n"; } }

The code above stores all of the CGI data in a $params hash reference (see nq_config.pl) and raises an error if one of the compulsory fields has not been filled in.



if ( defined($error) && (length($error)>0) ) { &form_Incomplete($error); } elsif ($cgi_data{'subm_action'} eq "Preview") {

editPatch.pl has requested a preview of a patch, with the data that is on that page -- provide a patch, generated by nq_mkpatch.pl, to STDOUT (i.e. returned by the web server).



# decide on a random small_image file to be used... my $quilt_defs = &nqco_getQuiltDefaults(1); my $html_defs = &nqco_getHTMLDefaults; my @small_images = sort( &nqds_getDirList($quilt_defs->{'S_SmallImgDir'}) ); $params->{'P_SmallImage'}{'Value'} = $quilt_defs->{'V_SmallImgDir'}."/"; $params->{'P_SmallImage'}{'Value'} .= $small_images[rand($#small_images+1)]; print STDOUT "Content-type: text/html\n\n"; &nqmp_makePatch($params, \*STDOUT); } else { my $file_name; my $source_dir; my $dest_dir; if ( ($cgi_data{'subm_type'} eq "pending") || ($cgi_data{'subm_type'} eq "accepted") || ($cgi_data{'subm_type'} eq "rejected") ) { $params->{'I_FileName'}{'Value'} = $cgi_data{'subm_file'};

If it is a pre-existing data file, it has either been edited, needs to be moved, or both - so oblige. (Again, this will only arise from editPatch.pl.)



if ( $cgi_data{'subm_type'} eq "accepted" ) { $source_dir = $defaults->{'S_PatchDir'}; } elsif ( $cgi_data{'subm_type'} eq "rejected" ) { $source_dir = $defaults->{'S_PatchRejectDir'}; } else { $source_dir = $defaults->{'S_PatchInDir'}; } if ( $cgi_data{'subm_action'} eq "Accept") { $dest_dir = $defaults->{'S_PatchDir'}; } elsif ( $cgi_data{'subm_action'} eq "Hold") { $dest_dir = $defaults->{'S_PatchInDir'}; } else { $dest_dir = $defaults->{'S_PatchRejectDir'}; } } else {

Otherwise, it's a new file, and it needs to have its name set correctly and the counter needs to be incremented.



# a newly submitted patch $source_dir = ""; $dest_dir = $defaults->{'S_PatchInDir'}; # increment the counter open(COUNTER_HANDLE, $defaults->{'S_PatchCounter'}); my $count = <COUNTER_HANDLE>; close(COUNTER_HANDLE); open(COUNTER_HANDLE, ">$defaults->{'S_PatchCounter'}"); print COUNTER_HANDLE ++$count; close(COUNTER_HANDLE); $params->{'I_FileName'}{'Value'} = sprintf("patch_%05Vu", $count); } $file_name = $params->{'I_FileName'}{'Value'}; if ( &nqco_writeParameters($params, $dest_dir) ) { if ( ($cgi_data{'subm_type'} eq "pending") || ($cgi_data{'subm_type'} eq "accepted") || ($cgi_data{'subm_type'} eq "rejected") ) { # delete the old one, if this is moving directories... if ($source_dir ne $dest_dir) { unlink ("$source_dir$file_name"); } $message .= "Deleted ( $file_name ) from ( $source_dir )\n\n"; $message .= "Saved ( $file_name ) to ( $dest_dir )\n\n"; } else { &form_Complete; } } else { # couldn't write file $message .= "Could not save ( $file_name ) to "; $message .= "( $dest_dir ) :(\n\n"; } if ( $message ) { &form_HTMLMessage($message); } } # End of main()

The following two subroutines are the 'incomplete' and 'complete' pages for submit.htm; the last subroutine is just the HTML shell for the preview patch above.



# Start of form_Incomplete(error) sub form_Incomplete { # add line breaks so it looks better in HTML $ARG[0] =~ s/\n/<BR>\n/g; print STDOUT <<End_FormIncomplete; Content-type: text/html <!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"> <HTML> <HEAD> <TITLE>does not compute</TITLE> </HEAD> <BODY BGCOLOR="#FFFFCE" TEXT="#313131"> <BR> <BR> <BR> <BR> <BR> <DIV ALIGN=CENTER> <TABLE WIDTH="80%" BORDER=0 CELLSPACING=2 CELLPADDING=10> <TR> <TD BGCOLOR=WHITE> <FONT FACE="helvetica,arial,verdana,times" COLOR="#313131" SIZE="+1"> Sorry, the form can't be processed until you have filled in the following fields. <BR>Please go back and recheck your entries.<BR> </FONT> </TD> <TD ALIGN=CENTER> <STRONG> <FONT FACE="helvetica,arial,verdana,times" COLOR="#313131" SIZE="+6">&gt;&gt;&gt;&gt;</FONT> </STRONG> </TD> <TD ALIGN=CENTER> <STRONG> <FONT FACE="helvetica,arial,verdana,times" COLOR="#313131" SIZE="+3"> $ARG[0] </FONT> </STRONG> </TD> </TR> </TABLE> </DIV> </BODY> </HTML> End_FormIncomplete } # End of form_Incomplete() # Start of form_Complete() sub form_Complete { print STDOUT <<End_FormComplete; Content-type: text/html <!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"> <HTML> <HEAD> <TITLE>patched!</TITLE> </HEAD> <BODY BGCOLOR="#FFFFCE" TEXT="#313131"> <BR> <BR> <DIV ALIGN=CENTER> <TABLE WIDTH="50%" BORDER=0 CELLSPACING=2 CELLPADDING=5> <TR COLSPAN=2> <TD BGCOLOR=WHITE> <FONT FACE="helvetica,arial,verdana,times" COLOR="#313131" SIZE="+1"> Thank you for your submission. Your writing will be patched within the next twelve hours. </TD> </TR> <TR> <TD>&nbsp;</TD> </TR> <TR> <TD ALIGN=RIGHT> <IMG SRC="../grf/Qhome.gif" WIDTH=218 HEIGHT=82 BORDER=0 ALT="noon quilt"> </TD> <TD ALIGN=LEFT> <A HREF="../quilt_2.htm"> <IMG SRC="../grf/gonoon.gif" WIDTH=75 HEIGHT=40 BORDER=0 ALT="go quilt"> </A> </TD> </TR> </TABLE> </DIV> <BR> <BR> <BR> <BR> <DIV ALIGN=CENTER> <TABLE WIDTH=105 BORDER=0> <TR> <TD ALIGN=CENTER> <A HREF="http://trace.ntu.ac.uk/"> <IMG SRC="../grf/trace.gif" ALT="trace" WIDTH=105 HEIGHT=42 BORDER=0> </A> </TD> </TR> </TABLE> </DIV> </BODY> </HTML> End_FormComplete } # # Start of form_HTMLMessage(message) sub form_HTMLMessage { # add line breaks so it looks better in HTML $ARG[0] =~ s/\n/<BR>\n/g; print STDOUT <<End_HTMLMessage; Content-type: text/html <!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"> <HTML> <HEAD> <TITLE>patch processing</TITLE> </HEAD> <BODY BGCOLOR="#FFFFCE" TEXT="#313131"> <BR> <BR> $ARG[0] </BODY> </HTML> End_HTMLMessage } # End of form_HTMLMessage() # End of patchForm.pl