#!/usr/bin/perl ############################################################################### # search.cgi 7/4/2001 # (c)Pam Carey Durstock durp@one.net # searchable index of Kentucky Biographies # biographies are located at http://www.rootsweb.com/~kygenweb/kybiog/ # path to the bios: /u1/home0013/home/kygenweb/public_html/kybiog/..... ############################################################################### use DBI; use Text::Soundex; use CGI::Carp qw(fatalsToBrowser); $db_name="test_kybios"; $dbserver="resources.rootsweb.com"; $alt="Kentucky Biographies"; $searchscript="search.cgi"; $path="/u1/home0013/home/kygenweb/public_html/kybiog"; print "Content-type: text/html\n\n"; # the double '\n' thing is *crucial* &parse_form; $surname=$FORM{'surname'}; $fname=$FORM{'fname'}; $county=$FORM{'county'}; $soundex=$FORM{'soundex'}; # $read=$FORM{'read'}; $vsurname=$FORM{'vsurname'}; $vfname=$FORM{'vfname'}; $vcounty=$FORM{'vcounty'}; $vsoundex=$FORM{'vsoundex'}; $srchterm=$FORM{'srchterm'}; # $main_id=$FORM{'main_id'}; $next=$FORM{'next'}; $sort=$FORM{'sort'}; # $database=$FORM{'database'}; $return_to=$FORM{'return_to'}; # $database="$alt" unless ($database); $return_to="http://www.rootsweb.com/~kygenweb/kybiog/search.html" unless ($return_to); &illegal if ($surname=~/^\%/); &grab if ($exists{'read'}); &missing_info unless ($surname || $fname || $county || $read); $qstring2=$ENV{'QUERY_STRING'}; &getlist if (($exists{'getlist'}) || ($exists{'getmore'}) || ($surname ne '')); # comes from form #################################################################### # # SUBROUTINES # #################################################################### sub parse_form { read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); # for POST methods $qstring=$ENV{'QUERY_STRING'}; # for GET methods if ($buffer eq '') {$buffer=$qstring;} else {$buffer.="&$qstring";} # @pairs = split(/&/, $buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $value=$1 if ($value=~/^(.*)$/); # untaint for writing if ($FORM{$name}) { # if this var is already defined (multiple checkbox loop) $FORM{$name}.=", $value"; # append the new one } else { $FORM{$name}=$value; # assign it } $exists{$name}='y'; # all passed variables will set this } } #################################################################### # SEARCH (getlist) #################################################################### sub getlist { &header; $dbh = DBI->connect("DBI:mysql:$db_name:$dbserver", nobody, undef, undef) or &dberror; if ($surname ne '') { &too_short if (length($surname)<3); $sndx=soundex($surname); $surname2=$surname."%"; $quotesurname=$dbh->quote($surname2); # escapes the nasties that lusers enter, which can kill mysql } ######################################################################################### # INITIAL RESULTS SCREEN ######################################################################################### $fields2search=0; if ($surname ne '') { $fields2search++; if ($exists{'soundex'}) {$statement="SELECT surname,fname,maincounty,allplaces,filename,otherbios FROM bios,surnames WHERE sndx='$sndx'";} else {$statement="SELECT surname,fname,maincounty,allplaces,filename,otherbios FROM bios,surnames WHERE surname like $quotesurname";} } if ($fname ne '') { $fields2search++; $fname2=$fname."%"; $quotefname=$dbh->quote($fname2); if ($fields2search==1) {$statement="SELECT surname,fname,maincounty,allplaces,filename,otherbios FROM bios,surnames WHERE fname like $quotefname";} else {$statement.=" AND fname like $quotefname";} } if ($county ne '') { $fields2search++; $county4all="%$county%"; if ($fields2search==1) {$statement="SELECT surname,fname,maincounty,allplaces,filename,otherbios FROM bios,surnames WHERE kycounties like '$county4all'";} else {$statement.=" AND kycounties like '$county4all'";} } # $statement.=" AND lname_id=surnames.id"; # $sth=$dbh->prepare($statement); $sth->execute or &dberror; $findcount=0; while (@row=$sth->fetchrow_array) { # main.id,surname,fname,maincounty,kycounties,allplaces,filename,otherbios $lname=$row[0]; $fname2=$row[1]; $maincounty=$row[2]; $allplaces=$row[3]; $filename=$row[4]; $otherbios=$row[5]; $lname=~s/ \*\*//; $otherbios=~s/
/, /g; # $findcount++; push (@foundstuff, "$lname\t$fname2\t$maincounty\t$allplaces\t$filename\t$otherbios"); } $sth->finish; $dbh->disconnect; # foreach $gizmo (sort @foundstuff) {push (@foundrecords, "$gizmo");} print "
\n"; if ($findcount!=0) { print "

$findcount Matches for [ "; if ($surname ne '') {print "surname=$surname";} if ($fname ne '') {print ", first name=$fname";} if ($county ne '') {print ", county=$county";} # print " ]
If you get too many matches, return to the form and add information in other fields.
If you get too few, return to the form and remove or change some of the restrictions.

\n\n"; print "\n"; # # $next=0 if (!($next)); $next2=$next; $next3=$next + 1; if (($next + 50) < $findcount) { $last=$next + 50; } else { $last=$findcount; } print " \n"; print " \n"; if ($findcount>50) { for ($next; $next<=$next2+49; $next++) { ($lname,$fname2,$maincounty,$allplaces,$filename,$otherbios)=split(/\t/,$foundrecords[$next]); if ($lname ne '') { print " \n"; if ($fname2 ne '') { print " \n"; } else { print " \n"; } print " \n"; print " \n"; $srchterm="$surname/$fname/$county/$soundex"; if ($otherbios ne '') { print " \n"; } $fname4url=$fname2; $fname4url=~s/([^a-zA-Z0-9])/sprintf("%%%02lx",unpack("C",$1))/ge; print " \n" if ($otherbios eq ''); print " \n"; } } } else { for ($next; $next<$findcount; $next++) { ($lname,$fname2,$maincounty,$allplaces,$filename,$otherbios)=split(/\t/,$foundrecords[$next]); if ($lname ne '') { print " \n"; if ($fname2 ne '') { print " \n"; } else { print " \n"; } print " \n"; print " \n"; $srchterm="$surname/$fname/$county/$soundex"; if ($otherbios ne '') { print " \n"; } $fname4url=$fname2; $fname4url=~s/([^a-zA-Z0-9])/sprintf("%%%02lx",unpack("C",$1))/ge; print " \n" if ($otherbios eq ''); print " \n"; } } } print "
Matches $next3 - $last of $findcount
NameResidence/BirthplaceOther Locations MentionedFound in Bio for:
 \;$lname, $fname2 \;$lname \;$maincounty \;$allplaces \;"; @otherbios=split(/, /,$otherbios); $obiocount=0; foreach $obio (@otherbios) { if ($obio=~/(.*)<\/a>/) {$sread=$1; $sname=$2;} $obiocount++; print ", " if ($obiocount>1 && $obio ne ''); print "$sname"; } print " \;Self
 \;$lname, $fname2 \;$lname \;$maincounty \;$allplaces \;"; @otherbios=split(/, /,$otherbios); $obiocount=0; foreach $obio (@otherbios) { if ($obio=~/(.*)<\/a>/) {$sread=$1; $sname=$2;} $obiocount++; print ", " if ($obiocount>1 && $obio ne ''); print "$sname"; } print " \;Self
\n"; # # ************** DON'T FORGET TO CHANGE THE SCRIPTNAME HERE **************************** # if ( (($findcount > 50) && ($next<49)) || (($next>49) && ($next<$findcount)) ) { print "
\n"; print " \n"; print " \n"; print " \n"; print " \n"; print " \n"; print " \n"; print " \n"; print " \n"; print "

\n"; } # print "

\n"; } else { print "

NO RECORDS

\n\n"; print "No records were found matching [ \n"; if ($surname ne '') {print "surname=$surname";} if ($fname ne '') {print ", first name=$fname";} if ($county ne '') {print ", county=$county";} print " ].\n\n"; } print "

New Search

\n"; #print "$statement

\n"; print "

\n"; # &footer; exit; } #################################################################### # GRAB AND DISPLAY THE BIO #################################################################### sub grab { &header; ($surname,$fname,$county,$sndx)=split(/\//,$srchterm); print "KyGenWeb > Kentucky Biographies > Search Biographies > Results > $vsurname"; print ", $vfname" if ($vfname ne ''); print "

\n"; print "

\n";
   open (BIO, "$path/$read") || die "Sorry.  File Not Available.  Please try again later.

\n"; while () { $line=$1 if ($_=~/^(.*)$/); $vsurname=~s/_/ /g; if ($line!~/^http/) { $line=~s/$vsurname/$vsurname<\/b><\/font>/gi if ($vsurname ne ''); $fname2=$vfname; $fname2=~s/\./\\./g; $line=~s/$fname2/$vfname<\/b><\/font>/gi if ($vfname ne ''); $line=~s/$vcounty/$vcounty<\/b><\/font>/gi if ($vcounty ne ''); } print "$line\n"; } close (BIO); print "

\n"; print "\n"; exit; } #################################################################### # DB ERROR #################################################################### sub dberror { # This routine masks database errors. It gives the visitors a non-scary message # (i.e. a "graceful exit"), but at the same time sends the programmer an email # specifying both the specific error, the location in the script where the error # happened, and the cause (so it can be fixed). # open (MAIL, "|/usr/sbin/sendmail -oi -t") || print "Can't open sendmail
\n"; print MAIL "Precedence: bulk\n"; print MAIL "To: pam\@rootsweb.com\n"; print MAIL "From: kybios\@www.rootsweb.com\n"; print MAIL "Subject: KyBios DB Error\n\n"; print MAIL "DB = $db_name\n\n"; print MAIL "statement = $statement\n"; print MAIL "statement2 = $statement2\n"; print MAIL "statement3 = $statement3\n"; print MAIL "statement4 = $statement4\n\n"; print MAIL "error = $DBI::errstr\n\n"; close(MAIL); # # GRACEFUL EXIT FOR THE VISITOR # print "
\n"; print "

DATABASE BUSY

\n\n"; print "The database is busy at the moment. Please try back again later.

\n"; print "$statement
$DBI::errstr

\n"; print "

\n"; # &footer; $dbh->disconnect; exit; } sub missing_info { print "
\n"; print "

INPUT ERROR

\n\n"; print "You must enter something in at lest one of the searchfields.

Please return to the form and correct this.

\n"; print "Return to the Form

\n"; print "

\n"; # &footer; exit; } sub too_short { print "
\n"; print "

INPUT ERROR

\n\n"; print "If you entered a surname, you must include at least the first 3 characters.
\n"; print "Please return to the form and correct this.

\n"; print "Return to the Form

\n"; print "

\n"; # &footer; exit; } sub illegal { print "
\n"; print "

ILLEGAL SEARCH

\n\n"; print "Names are not permitted to begin with a wildcard character. Please return to the form and correct this.

\n"; print "Return to the Form

\n"; print "

\n"; # &footer; exit; } sub needsurname { print "
\n"; print "

ILLEGAL SEARCH

\n\n"; print "Soundex searches require a surname. Please return to the form and either enter a surname, or uncheck the soundex option.

\n"; print "Return to the Form (using your 'Back' button will preserve your entry)

\n"; print "

\n"; # &footer; exit; } sub header { print "\n"; print "\n"; print " $alt\n"; print "\n"; # if ($exists{'read'}) {print "\n";} if ($exists{'read'}) {print "\n";} else { print "\n"; print "
\n"; open (HEADER, "../header.pam.incl"); while (
) {print;} close (HEADER); } } sub footer { open (FOOTER, "../footer.pam.incl"); while (