#!/local/bin/perl ################################################################## # Original: Simple Database by Brent Michalski # # Chopped, channelled, and otherwise much modified # # by Steve Sowder beginning 4/12/99 # # Andrews University, Berrien Springs, Michigan # ################################################################## # # # photos.cgi # # # ################################################################## &makebackup; # added June 4, 2004 by sowder require "cgi-lib.pl"; &ReadParse(*input); ## Change these 2 lines to make a new db! @fields = ("Number","timestamp","call#","category","name","photographer","date", "descript","condition","provenance","note1","note2", "subject1","subject2","subject3","subject4","subject5", "CD-num","imagefile","thumbnail","release"); $database = "photosdb.dat"; print &PrintHeader; ########## $ra = $ENV{'REMOTE_ADDR'}; $host=$ENV{'REMOTE_ADDR'}; if ($ENV{'HTTP_X_FORWARDED_FOR'} ne ""){ (@list)=split(/,/,$ENV{'HTTP_X_FORWARDED_FOR'}); $host = @list[$#list]; $ra=@list[0]; } $badkarma = 'bad news'; if ($ra eq '143.207.14.30') {$badkarma = 'good'; } # sowder if ($ra eq '143.207.14.213') {$badkarma = 'good'; } # jim ford if ($ra eq '143.207.14.109') {$badkarma = 'good'; } # ahc workstation if ($badkarma =~ /bad/) { &badkarma; die; } ############### $field_count = @fields; $colspan = $field_count+1; $EXCLUSIVE = 2; $UNLOCK = 8; $cr = chr(13); # this is a carriage return $search_for = $input{'search_for'}; $search_field = $input{'search_field'}; $action = $input{'action'}; @keys = $input{'key'}; $key_matches = @keys; $Keylength = 5; $search_field = "all" if($search_field eq ""); $search_for = '.' if ($search_for eq ""); if($action =~ /add record/i){ # Add the record passed from the add record page # $key = $keys[0]; &add_record; $message="Record Added"; &print_message($message); } elsif($action =~ /add/i){ # Display the add record page &print_add_screen; } elsif($action =~ /modify record/i){ # Display the results of the search &search_database($input{'key'}); $count = @results; &no_match if($count < 1); &print_modify_page; } elsif($action =~ /modify this record/i){ # Modify the record that was passed $key=$keys[0]; &add_record; $message="Record Modified"; &print_message($message); } elsif($action =~ /modify/i){ # Search and display results for modification &search_database($search_for); $count = @results; if($count < 1){ &no_match; } elsif($count == 1){ &print_modify_page; } else { $caption="Modify Which Record?"; $button_text="Modify Record"; &multiple_match("RADIO","modify"); } } elsif($action =~ /delete record/i){ # Delete the record(s) that were passed $key = $keys[0]; &delete_records; $message="Record(s) Deleted"; &print_message($message); } elsif($action =~ /delete/i){ # Search and display results for modification &search_database($search_for); $count = @results; &no_match if($count < 1); $caption="Delete Which Record(s)?"; $button_text="Delete Record(s)"; &multiple_match("CHECKBOX","delete"); } elsif($action =~ /update/i){ # update file &update_database; $message='File Updated'; &print_message($message); } elsif($action =~ /search/i){ # Search database and display the results &search_database($search_for); $count = @results; if($count > 0) { $button_text = "Back to Database"; $caption = "Search Results"; &multiple_match; } else { &no_match; } } else { &print_default; } exit; ### Subroutines go below here. ########################################### sub add_record { $key = $input{'Number'}; if (length($key) != $Keylength) { &get_highest_key; $key = $nextkey; } $record = $key; $timestamp = time(); $record .= "\^$timestamp"; $field = $input{'call#'}; $field = filter($field); $record .= "\^$field"; $field = $input{'category'}; $field = filter($field); $record .= "\^$field"; $field = $input{'name'}; $field = filter($field); $record .= "\^$field"; $field = $input{'photographer'}; $field = filter($field); $record .= "\^$field"; $field = $input{'date'}; $field = filter($field); $record .= "\^$field"; $field = $input{'descript'}; $field = filter($field); $record .= "\^$field"; $field = $input{'condition'}; $field = filter($field); $record .= "\^$field"; $field = $input{'provenance'}; $field = filter($field); $record .= "\^$field"; $field = $input{'note1'}; $field = filter($field); $record .= "\^$field"; $field = $input{'note2'}; $field = filter($field); $record .= "\^$field"; $field = $input{'subject1'}; $field = filter($field); $record .= "\^$field"; $field = $input{'subject2'}; $field = filter($field); $record .= "\^$field"; $field = $input{'subject3'}; $field = filter($field); $record .= "\^$field"; $field = $input{'subject4'}; $field = filter($field); $record .= "\^$field"; $field = $input{'subject5'}; $field = filter($field); $record .= "\^$field"; $field = $input{'CD-num'}; $field = filter($field); $record .= "\^$field"; $field = $input{'imagefile'}; $field = filter($field); $record .= "\^$field"; $field = $input{'thumbnail'}; $field = filter($field); $record .= "\^$field"; $field = $input{'release'}; $field = substr(filter($field),0,2); $record .= "\^$field"; open (DB, ">>$database") || die "Error opening database for add. $!\n"; flock DB, $EXCLUSIVE; # seek DB, 0, 2; print DB "$record\n"; flock DB, $UNLOCK; close(DB); } # End of add_record subroutine. ########################################### sub get_highest_key { open (DB, "$database") || die "Error opening database for get_key $!\n"; flock DB, $EXCLUSIVE; # seek DB, 0, 2; $nextkey = ' '; $didthis = 'yes'; while ($getkey = ) { $thiskey = substr($getkey,0,$Keylength); if ($thiskey gt $nextkey and $thiskey lt '99000') {$nextkey = $thiskey;} } flock DB, $UNLOCK; close(DB); $nextkey = $nextkey + 1; while (length($nextkey) < $Keylength) {$nextkey = '0' . $nextkey; } } # end of get_highest_key ########################################### sub print_add_screen{ print<Add a Record
Add a Record

HTML $x=0; foreach $field (@fields) { print< HTML # if ($x < 30) # { print ""; if ($x <= 1) {print ''; } else { print < HTML2 } # } # else # { # print ""; # } print ''; $x++; } # End of foreach. print<
$fs\u$field:$fc

HTML } # End of print_add_screen subroutine. ########################################### sub delete_records { # $record = $key; $record = $input{'key'}; $record = substr($record,0,$Keylength); # get only number part of key $timestamp = time(); $record .= "\^$timestamp"; $record .= "\^**KILLED**"; # in name field for ($x = 4; $x < $field_count; $x++) {$record .= "\^"; } open (DB, ">>$database") || die "Error opening database for del. $!\n"; flock DB, $EXCLUSIVE; print DB "$record\n"; flock DB, $UNLOCK; close(DB); } # End of delete subroutine. ########################################### sub print_modify_page{ (@field_vals) = split(/\^/, $results[0]); $key = $field_vals[0]; $fs=""; $fc=""; print<

Modify Record
Modify Record

HTML $x=0; foreach $field (@fields) { print< HTML $x++; } # End of foreach. print<
$fs\u$field:$fc


HTML } ########################################### sub multiple_match{ print "
\n"; print "(TITLE=>'Match Results'\n"; print "\n"; print<
$caption
There were $count matches

HTML if($_[1] =~ /(modify|delete)/){ print "\n"; } foreach $field (@fields){ print "\n"; } # End of foreach print ""; foreach $record (@results){ (@field_vals) = split(/\^/, $record); print ""; if($_[1] =~ /(modify|delete)/){ print "\n"; } # End of if. for($x=0;$x<$field_count;$x++){ $item = &check_empty($field_vals[$x]); print "\n"; } print ""; } # End of foreach loop. print<
"; print "Select"; print "\u$field
"; print ""; print "$item
HTML } # End of multiple_match subroutine. ########################################### sub no_match{ print <
No Match

There was no match for $search_for please hit back and try again.

HTML exit; } # End of no_match subroutine. ########################################### sub search_database { $search_for = $_[0]; open(DB, $database) or die "Error opening file: $!\n"; while() { if (length($search_for) == 15) # search_for has number & timestamp { $search_for = substr($_[0],0,$Keylength) . "\^" . substr($_[0],$Keylength); $search_field = 'all'; } if($search_field =~ /all/i) { if (length($search_for) != 16) {if(/$search_for/oi){push @results, $_}; } if (length($search_for) == 16) { $temp = substr($_,0,16); if ($temp eq $search_for) {push @results, $_ ;} } #open (TRACKER, ">>debug.scr") || die "can't open tracker\n"; #$x = substr($_,0,14); #print TRACKER "length=4: <$search_for> <$x>\n"; #if ($x eq $search_for) {print TRACKER "i found it\n"; } #close TRACKER; } else { (@field_vals) = split(/\^/, $_); # delimiter changed Sowder if($field_vals[$search_field] =~ /$search_for/oi){push @results, $_}; } # End of else. } # End of while. close (DB); } # End of search_database subroutine. ########################################### sub print_default { print< Photos Default Screen
The Photos Databases

To add a record, click on the Add button. To search/modify/delete records, enter the text in the box below and choose the field to search on. Then click to appropriate button.
Search For:
Search On: All Number Name HTML print<
HTML } # End of print_default subroutine. ########################################### sub filter{ $temp = $_[0]; $temp =~ s/\^//; # Remove pipe symbols in text. $temp =~ s/\r//; # Remove carriage return in text. return ($temp); } ########################################### # remove all control characters and delimiters sub filterext{ my $i = 0; $temp = $_[0]; $temp =~ s/\^//; # Remove pipe symbols in text. $temp =~ s/ / /g; #remove multiple blanks $temp =~ s/ / /g; #remove multiple blanks $temp =~ s/ / /g; #remove multiple blanks $temp =~ s/ / /g; #remove multiple blanks my $len = length($temp); for ($i =0; $i < $len; $i++) { $x = ord(substr($temp,$i,1)); if ($x < 32) { substr($temp,$i,1) = ' '; } } return ($temp); } ########################################### sub print_message{ print<
$_[0]

Back To Main Database Screen
HTML } ########################################### sub check_empty{ $r_val = $_[0]; if($r_val =~ /^\s*$/){$r_val=" "} return($r_val); } ################################################# sub update_database { ## sort the database file to update database print "UPDATING THE DATABASE...\n"; open (INFILE, 'photosdb.dat') or die "no joy in\n"; open (OUTFILE, '>sordid.scr') or die "no joy out\n"; print OUTFILE sort (); close INFILE; close OUTFILE; open (INFILE, 'sordid.scr') or die "no joy in\n"; open (OUTFILE, '>photosdb.dat') or die "no joy out\n"; $lastrecord = ''; while ($line = ) { chomp($line); if (length($lastrecord) > 0 and substr($line,0,$Keylength) ne substr($lastrecord,0,$Keylength)) { if (index($lastrecord,'**KILLED**') < 0) {print OUTFILE "$lastrecord\n"; } } $lastrecord = $line; } print OUTFILE "$line\n"; print OUTFILE "99999\^999999999\^999\^\^\^\^\^\^\n"; close (INFILE); close (OUTFILE); } # end of update_database ######################################################## # badkarma sub badkarma { print <

Sorry
According to our files
you are not allowed to run
this screen

If you believe this is in error
please contact the
system librarian

$ra KILLME } # end of badkarma (we hope!) ######################################################## sub makebackup { # make backup if necessary ($DAY, $MONTH, $YEAR) = (localtime)[3,4,5]; #print "finis local time1\n"; $M=$MONTH; #bidwell fix $MONTH++; $YEAR = $YEAR + 1900; if (length($MONTH) < 2) {$MONTH = '0' . $MONTH} if (length($DAY) < 2) {$DAY = '0' . $DAY} $backupfile = 'photosdb.dat.' . $YEAR . $MONTH . $DAY; open BACKUPFILE, "$backupfile"; $getit = ; close BACKUPFILE; chomp $getit; if ($backupfile =~ /(.+)/) {$backupfile = $1; } # to untaint file name if (length($getit) == 0) { open INFILE, 'photosdb.dat'; open BACKUP, ">$backupfile"; while ($line = ) {print BACKUP "$line"} close INFILE; close BACKUP; # system "cp photosdb.dat $backupfile"; # even untainted this doesn't work } }