#!/usr/bin/perl # uncomment flock # chmod files # delete error output # tritz.cgi # SOME OPTIONS # 'no' or incorrect password prevents removal of word/meaning entries $subtract = 'yes'; $passwd = 'fireicebolt'; # database.txt will be a plain text file containing words and their meanings stored thus: # "word\n # definition\n\n" # The following are the expected form inputs: # # $FORM{'do'} # tells script what action to perform # # will redirect to tritz.cgi if omitted # eq 'search' # $FORM{'pattern'} # this is the pattern to look for # $FORM{'match_what'} # eq 'words' # look in words # eq 'meanings' # look in meanings # eq 'quotes' # look in quotes # eq 'alphabet' # $FORM{'letter'} # look for all words beginning with this letter # or all non-letters if !~ /[a-zA-Z]/ # eq 'add' # $FORM{'word'} # add this word to database # $FORM{'meaning'} # add this meaning # $FORM{'quote'} # add this quote # eq 'subtract' # $FORM{'word'} # delete this word from the database # $FORM{'meaning'} # optional meaning to specify which word # $FORM{'password'} # password for deleting a word from the database # eq 'last10' require "read_in.cgi"; %FORM = &read_input; unless (defined($FORM{'do'})) { print "URI: tritz.html\n"; print "Location: tritz.html\n\n"; exit; } $do = $FORM{'do'}; $pattern = $FORM{'pattern'}; $match_what = $FORM{'match_what'}; $letter = $FORM{'letter'}; $word = $FORM{'word'}; $meaning = $FORM{'meaning'}; $quote = $FORM{'quote'}; $password = $FORM{'password'}; print "Content-type: text/html\n\n"; if (defined($FORM{'out'})) { open(OUTFILE, ">$FORM{'out'}") || die "Couldn't open file $FORM{'out'}: $!"; select(OUTFILE); } # What should the script do? # Add, subtract, search? if ($do eq 'search') { &search; } elsif ($do eq 'alphabet') { &alphabet; } elsif ($do eq 'last10') { &last10; } elsif ($do eq 'add') { &add; } elsif ($do eq 'subtract') { &subtract; } else { &write_error("Undefined action do=$do"); } sub search { $pattern =~ s/([\^\(\)\{\}\*\+\?\.\[\]\|\\\$])/\\$1/g; $pattern =~ s/; close(DATABASE); if ($match_what =~ /words/) { push(@data, grep(/$pattern.*\n.+\n.+/i, @database)); push(@match, 'words'); } if ($match_what =~ /meanings/) { push(@data, grep(/.+\n.*$pattern.*\n.+/i, @database)); push(@match, 'meanings'); } if ($match_what =~ /quotes/) { push(@data, grep(/.+\n.+\n.*$pattern/i, @database)); push(@match, 'quotes'); } if ($match_what !~ /words|meanings|quotes/) { @data = grep(/$pattern/i, @database); push(@match, 'words, meanings, quotes'); } @data = grep(($Last eq $_ ? 0 : ($Last = $_, 1)),sort @data); $match = join(', ', @match); &write_search; } sub alphabet { open(DATABASE, '; close(DATABASE); if ($letter =~ /^[a-zA-Z]$/) { @data = grep(/^$letter/i, @database); } else { @data = grep(/^[^a-zA-Z]/, @database); } @data = grep(($Last eq $_ ? 0 : ($Last = $_, 1)),sort @data); &write_alphabet; } sub last10 { open(DATABASE, '; close(DATABASE); @database = reverse (@database); @data = @database[0..9]; &write_last10; } sub add { &write_error('Please make sure both a word and its meaning were included in the form.') if ($word eq '' || $meaning eq ''); # replaces such things as carriage returns, etc... with a single space $word =~ s/\s+/ /g; $meaning =~ s/\s+/ /g; $quote =~ s/\s+/ /g; # strips beginning and trailing white space $word =~ s/^\s+//; $word =~ s/\s+$//; $meaning =~ s/^\s+//; $meaning =~ s/\s+$//; if ($quote eq '') { $quote = ' '; } else { $quote =~ s/^\s+//; $quote =~ s/\s+$//; } $quote =~ s/>database.txt') || die "Couldn't open database.txt for appending: $!"; # flock(DATABASE, 2); print DATABASE "$word\n$meaning\n$quote\n\n"; # flock(DATABASE, 8); close(DATABASE); &write_add; } sub subtract { &write_error('Incorrect password') unless ($password eq $passwd); &write_error('Cannot delete entry. Option not available.') unless ($subtract eq 'yes'); &write_error('Please make sure a word was included for deletion -- an accompanying definition is not required.') if ($word eq ''); open(OLD_DATA, 'databack.txt') || die "Couldn't open databack.txt for writing: $!"; open(TEMP_DATA, '>datatemp.txt') || die "Couldn't open datatemp.txt for writing: $!"; $/ = "\n\n"; while ($line = ) { print BACK_DATA $line; if ($line =~ /^$word\n$meaning/o) { push(@data,$line); next; } print TEMP_DATA $line; } close(TEMP_DATA); close(BACK_DATA); close(OLD_DATA); open(TEMP_DATA, 'database.txt') || die "Couldn't open database.txt for writing: $!"; flock(NEW_DATA, 2); undef $/; $line = ; print NEW_DATA $line; flock(NEW_DATA, 8); close(NEW_DATA); close(TEMP_DATA); @data = grep(($Last eq $_ ? 0 : ($Last = $_, 1)),sort @data); &write_subtract; } sub write_search { print < Search results Searched for $match
which matched the following pattern: $FORM{'pattern'} END_SEARCH &write_list; print "

Back to the MainPage\n\n"; print "\n"; } sub write_alphabet { print < Tritz was brought to you by the letter $letter Searched for words beginning with the letter $letter END_ALPHABET &write_list; print "

Back to the MainPage\n\n"; print "\n"; } sub write_last10 { print < Last 10 entries These are the last 10 entries: END_LAST10 &write_list; print "

Back to the MainPage\n\n"; print "\n"; } sub write_add { print < Add results The following entry has been successfully added:

$word
$meaning
$quote

Back to the MainPage END_ADD } sub write_subtract { print < Subtract results This word was searched for:

$word
$meaning

The following words have been deleted from the database: END_SUBTRACT &write_list; print "

Back to the MainPage\n\n"; print "\n"; } sub write_list { foreach $dat (@data) { ($word,$meaning,$quote) = split(/\n/,$dat,3); print "

$word\n"; print "
$meaning\n"; print "
$quote\n\n"; } } sub write_error { $message = shift; print < Error!

An error has occurred.

$message

Back to the MainPage END_ERROR exit; } select(STDOUT); close(OUTFILE); exit;