#! /usr/local/bin/perl use CGI qw(:standard); ############################################### # # File: ProcessInfo.cgi # # Receives data from the file GetInfo.cgi # Modifies the contents of various files and # sends HTML results back to user. # # # # Attention: SizeConstraint must be zero based # ############################################### ############################################### # # Get the parameters from GetInfo.cgi # $StudentName = param("name"); $Database = param("database"); $OK_CHARS = '-a-zA-Z0-9_.@ '; $StudentName =~ s/[^$OK_CHARS]//mg; if ($StudentName eq "Your Name Here") {ForgotName()}; if ($Database eq "") {ForgotDatabase()}; ############################################## # # Open the indicated file # unless(open(DBNames,$Database)) {error_message($Database)}; ############################################## # # Open associated number file # $DatabaseNum = $Database . "Num"; unless(open(DBNum,$DatabaseNum)) {error_message($DatabaseNum)}; ############################################## # # Read DBNames file contents # @OtherNames = (); # Holding array $FoundMatch = "no"; # Flag $Counter = 0; while (<DBNames>) { chomp; if ($_ =~ /\b$StudentName\b/i) { $FoundMatch = "yes"; ###################### # Name will be removed ###################### } else { $OtherNames[$Counter] = $_; $Counter++; } }; # while $Counter--; close(DBNames); ############################################### # # Read file size constraint # while (<DBNum>) { $SizeConstraint = $_; chomp; } ################################################## # # Test state of FoundMatch and the SizeConstraint # to either add name or drop name. Test size of # group before adding name. if ($FoundMatch eq "no") { if ($SizeConstraint > $Counter) ################### # Room for one more ################### { open(DBNames2, ">>$Database"); # Append flock DBNames2, $LOCK_EX; print DBNames2 ($StudentName."\n"); flock DBNames2, $LOCK_UN; close(DBNames2); UpDateLastUse(); AdditionReport(); } if ($SizeConstraint <= $Counter) { GroupFullReport(); } } else #FoundMatch is yes { open(DBNames2, ">$Database"); # Write over file for ($i=0; $i <= $Counter; $i++) { flock DBNames2, $LOCK_EX; print DBNames2 ($OtherNames[$i]."\n"); flock DBNames2, $LOCK_UN; } UpDateLastUse(); NameRemovedReport(); } ####################################################### # # Addition report subroutine # sub AdditionReport { print header(); start_html("Addition Report"); print hr(); print "Your name has been added to the group."; print br(); print a({href=>'ShowPage.cgi'},'Show revised list'); print end_html(); exit(); } ######################################################## # # Subroutine GroupFullReport # sub GroupFullReport { print header(); start_html("Group Full Report"); print hr(); print "Sorry! That group is full. "; print "Go back and choose another group."; print hr(); print end_html(); exit(); } ########################################################## # # Subroutine NameRemoved Report # sub NameRemovedReport { print header(); start_html("Name Removed Report"); print hr(); print "Your name has been removed."; print hr(); print end_html(); exit(); } ######################################################## # # Subroutine to report error on file opening # sub error_message { print header(); start_html("Error Report"); print "\nError in opening file: $_[0]\n"; print end_html(); exit(); } ###################################################### # # Forgot to input name # sub ForgotName { print header(); start_html("Forgot Name"); print hr(); print "Remember to enter your name!"; print br(); print "Go back and enter your name."; print hr(); print end_html(); exit(); } ####################################################### # # Forgot to declare a database # sub ForgotDatabase { print header(); start_html("Forgot Database"); print hr(); print "Remember to choose a database group!"; print br(); print "Go back and select a database group."; print hr(); print end_html(); exit(); } ####################################################### # # Update last contents change # sub UpDateLastUse { $NewUse = `/bin/date`; $UpDateFile = "LastUse"; unless(open(UPD,">$UpDateFile")) {error_message("LastUse")}; flock UPD, $LOCK_EX; print UPD $NewUse; flock UPD, $LOCK_UN; close(UPD); } ############### eof ################################