in reply to How do you search multiple flat file databases?

I'm posting the OP's question and script (copied from the above mentioned URL) here for the benefit of anyone reading this thread:


If you are reading this, Thank You for your assistance. These scripts were written for me by a friend before he left to work overseas. I have little experience with Perl. Any assistance would be a great help thanks. Please note that each search request can only search one database file at a time. I want to customize to search multiple files at the same time. I currently use the following form to search single dbname files from html template.
<a href="/cgi-bin/search.cgi?template=summary.htm&dbname=dbname&displ +aynew=yes&action=searchdbdisplay"> Display all new records</a>
After attempted customization, i want use the following form (example only) to search multiple files (of the same field definitions/format):
<a href="/cgi-bin/search.cgi?template=summary.htm&dbname=dbname1,dbnam +e2,dbname3& displaynew=yes&action=searchdbdisplay">Display all new records</a>
or
<a href="/cgi-bin/search.cgi?template=summary.htm&dbname=**dbname**& displaynew=yes&action=searchdbdisplay">Display all new records</a>
In the "Summary Report" template i want to use this link for sending the user to the detail report of the record displayed
<a href="/cgi-bin/search.cgi?template=summary.htm&dbname=**dbname**&ke +y2=**key**& action=searchdbdisplay">Detail Report</a>
Can the Perls of Wisdom, assist me in my struggle. I have been trying for 2 months to find a solution.
Thanks Again

Complete Search Script:

############# Must not change the codes after this line ########### ############# if you don't know what you are doing ########### # Lock File. Only created and deleted by this software $lock_fn = "db_lockfile"; # Time to wait for the database file to be released $wait_time = 3; $locked = 0; # Parse Form Contents &parse_form; &chk_cookie; if ($ENV{'REQUEST_METHOD'} ne 'POST') { if (!$ARGV[0] && $ENV{'QUERY_STRING'} !~ /=/) { $in{'template'} = $ENV{'QUERY_STRING'}; $in{'opendb'} = 1; } elsif ($ARGV[0] && !$in{'template'}) { $in{'template'} = $ARGV[0]; $in{'opendb'} = 1; } } # Validate & execute command according to Action Type unless ( $in{'opendb'} || ($in{'action'} eq "showblank") || ($in{'action'} eq "searchdbdisplay")) { &error_not_a_command; } if ($in{'opendb'}) {&showblank} if ($in{'action'} eq "showblank") {&showblank} if ($in{'action'} eq "searchdbdisplay") {&search} sub parse_form { if ($ENV{'REQUEST_METHOD'} eq 'GET') { @pairs = split(/&/, $ENV{'QUERY_STRING'}); } elsif ($ENV{'REQUEST_METHOD'} eq 'POST') { read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); @pairs = split(/&/, $buffer); } else { &error_form_method; } foreach $pair (@pairs){ if ($pair =~ /=/) { ($name, $value) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $value =~ s/<!--(.|\n)*-->//g; $name =~ tr/+/ /; $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $in{$name} .= "\0" if (defined($in{$name})); # \0 is multiple + separator # 290102 $in{$name} .= $value; # 290102 } } } sub search { &check_url_referer; &check_template; if ($in{'dbname'}) { &check_dbname; } else { &error_no_db_name; } if ($in{'days_for_new_entries'} && $in{'days_for_new_entries'} =~ / +^\d+$/i && ($in{'days_for_new_entries'} <=365 && $in{'days_for_new_entries +'} > 0)) { $days_for_new_entries = $in{'days_for_new_entries'}; } else { if ($in{'days_for_new_entries'}) { &invalid_days_for_new_entries; } if (!$days_for_new_entries) { $days_for_new_entries = 0; } } &calculate_new_entries_end_date; if ($in{'noofdays'} && $in{'noofdays'} =~ /^\d+$/i && ($in{'noofdays'} <=365 && $in{'noofdays'} > 0)) { $noofdays = $in{'noofdays'}; &calculate_search_begin_date; } else { if ($in{'noofdays'}) { &invalid_noofdays; } } if ($in{'maxdispscreen'} && # 050303 ($in{'maxdispscreen'} > 0 && $in{'maxdispscreen'} < 1000)) { # +050303 $maxdispscreen = $in{'maxdispscreen'}; # 050303 } # 050303 undef @multi_selections; # 290102 if ($in{'multiple_selections'}) { # 290102 @multi_selections = split (/,/,$in{'multiple_selections'}); # 29 +0102 } # 290102 if ($in{'key2'} && ($in{'key2'} ne "")) { $in{'key'} = $in{'key2'}; } $sort_idx = 0; if ($in{'sortfield'} ne "") { $sortfld_found = 0; if ($in{'sortfield'} =~ /^key$/i) { # 090203 $sortfld_found = 1; # 090203 } # 090203 else { # 090203 $ctfs=1; $fs="f1"; while ($ctfs <= $maxfields) { if ($in{'sortfield'} eq $fs) { $sortfld_found = 1; last; } $sort_idx++; $ctfs++; $fs = "f" . "$ctfs"; } } # 090203 if ($sortfld_found == 0) { &invalid_sort_field; } } if (!$in{'sorttype'} || ($in{'sorttype'} eq "")) { $in{'sorttype'} = "$sorttype_default"; } ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(t +ime); if ($mday < 10) { $mday = "0$mday"; } $month = ($mon + 1); if ($month < 10) { $month = "0$month"; } $year = 1900 + $year; $date_today = "$year/$month/$mday"; $us_date = "$month/$mday/$year"; $europe_date = "$mday/$month/$year"; &rotate_database; # open(DB,"$database_dir$dbname"); if ($in{'searchfield'}) { if ($in{'searchfield'} =~ /^all$/i) { if ($in{'searchterm'} && $in{'searchterm'} !~ /^[ ]+$/) { $in{'keywords'} = $in{'searchterm'}; if ($in{'wordmatch-searchterm'} =~ /^yes$/i) { $in{'wordmatch-keywords'} = "yes"; } } } else { if ($in{'searchfield'} =~ /^f\d+$/i) { if ($in{'searchterm'} && $in{'searchterm'} !~ /^[ ]+$/) { $in{$in{'searchfield'}} = $in{'searchterm'}; if ($in{'wordmatch-searchterm'} =~ /^yes$/i) { $wm_searchfield = "wordmatch-$in{'searchfield'}"; $in{$wm_searchfield} = "yes"; } } } } } if ($in{'keywords'}) { if (!$in{'case'} || $in{'case'} !~ /^yes$/i) { $in{'keywords'} =~ tr/a-z/A-Z/; } if ($in{'keywords'} =~ /^[ ]+$/) { $in{'keywords'} = ""; } else { if ($in{'keywords'} =~ /\&\&/i && $in{'keywords'} =~ /\+\+/i) + { # ds &error_invalid_operator_mix; # ds } # ds $keywords_operator = ""; # ds if ($in{'keywords'} =~ /\&\&/i) { # ds $in{'keywords'} =~ s/ *\&\& */&&/i; # ds @keywords = split(/\&\&/,$in{'keywords'}); # ds $keywords_operator = "and"; # ds } # ds elsif ($in{'keywords'} =~ /\+\+/i) { # ds $in{'keywords'} =~ s/ *\+\+ */++/i; # ds @keywords = split(/\+\+/,$in{'keywords'}); # ds } # ds else { # ds if ($in{'keywords_separator_is_comma'} =~ /^yes$/i) { # ds @keywords = split(/ *, */,$in{'keywords'}); # ds } # ds else { # ds @keywords = split(" +",$in{'keywords'}); # ds } # ds } # ds } } $cte = $maxfields; $mcnt = 0; # 42 $keysstr = ""; $xct=0; $ctfs=1; $fs="f1"; until ($xct==$cte) { if ($in{$fs} ne "") { $in{$fs} =~ s/\n/ -RET- /g; $in{$fs} =~ s/\r//g; $in{$fs} =~ s/^[ ]*//; # qp $in{$fs} =~ s/[ ]*$//; # qp if ($delimitor =~ /\t/i) { $in{$fs} =~ s/\t/ _TAB_ /g; # "tab" } if ($delimitor =~ /\,/i) { $in{$fs} =~ s/\,/ _COMMA_ /g; # "," } if ($delimitor =~ /_/i) { $in{$fs} =~ s/_/ _UNSC_ /g; # "_" } $exactm = "exactmatch-" . "$fs"; $wordm = "wordmatch-" . "$fs"; # Test if $in{} contains "Select" only if ($in{$fs} =~ /^select$/i) { $keysstr .= ".*$delimitor"; } elsif ($in{'exactmatch-all'}) { $keysstr .= "$in{$fs}$delimitor"; $mcnt++; # 42 } elsif ($in{$exactm}) { $keysstr .= "$in{$fs}$delimitor"; $mcnt++; # 42 } else { if ($in{$wordm} =~ /y/i) { $keysstr .= "$in{$fs}$delimitor"; # qp $mcnt++; # 42 } else { $keysstr .= ".*$in{$fs}.*$delimitor"; $mcnt++; # 42 } } } else { $keysstr .= ".*$delimitor"; } $ctfs++; $fs = "f" . "$ctfs"; $xct++; } # &set_content_type; open(DB,"$database_dir$dbname"); undef @MATCH; undef @sorted_keys; $mct = 0; $keymatched = 0; while (<DB>) { $row =$_; chop $row; if (!$row || $row =~ /^[ ]+$/) {next;} # Convert all \n in $row to -RET- $row =~ s/\n/ -RET- /g; $row =~ s/\r//g; @dbfld = split(/$delimitor/,$row); $ctss=@dbfld; if ($expiryDate_fld) { # 150803 $expired = 0; # 150803 &check_expiry_date; # 150803 if ($expired == 1) {next;} # 150803 } # 150803 # if ($ctss < $xct) { # $howmany = $xct-$ctss; # $dbfld_ptr = $ctss; # 311203 # while ($howmany > 0) { # $dbfld[$dbfld_ptr] = ""; # $row .= $delimitor; # $dbfld_ptr++; # 311203 # $howmany--; # } # } if ($uid_pwd_sep =~ /^ \| $/i) { ($key,$recpwd) = split(/ \| /,$dbfld[0]); } else { ($key,$recpwd) = split(/$uid_pwd_sep/,$dbfld[0]); } if ($in{'displaynew'} =~ /^yes$/i) { if ((!$in{'f4'}) || ($in{'f4'} eq $dbfld[3])) { if ((!$in{'f5'}) || ($in{'f5'} eq $dbfld[4])) { ($entry_year,$entry_month,$entry_day) = split(/\//,$dbf +ld[1]); $entry_date = ($entry_year*10000) + ($entry_month*100) ++ $entry_day; if ($entry_date >= $new_entries_end_date) { $other_digits = 10000 + $mct; if ($sort_idx == 0) { # 090203 $dbfldtmp = $key; # 090203 } # 090203 else { # 090203 $dbfldtmp = $dbfld[$sort_idx]; if ($delimitor =~ /\t/i) { $dbfldtmp =~ s/ _TAB_ /\t/g; # "tab" } if ($delimitor =~ /\,/i) { $dbfldtmp =~ s/ _COMMA_ /,/g; # "," } if ($delimitor =~ /_/i) { $dbfldtmp =~ s/ _UNSC_ /_/g; # "_" } } # 090203 $sort_key = "$dbfldtmp$other_digits"; $sort_key =~ tr/a-z/A-Z/; # XNEW $MATCH{$sort_key} = $row; $mct++; } } } } elsif ($in{'key'} ne "") { if ($in{'key'} eq $key) { if ((!$in{'f4'}) || ($in{'f4'} eq $dbfld[3])) { if ((!$in{'f5'}) || ($in{'f5'} eq $dbfld[4])) { $other_digits = 10000 + $mct; if ($sort_idx == 0) { # 090203 $dbfldtmp = $key; # 090203 } # 090203 else { # 090203 $dbfldtmp = $dbfld[$sort_idx]; if ($delimitor =~ /\t/i) { $dbfldtmp =~ s/ _TAB_ /\t/g; # "tab" } if ($delimitor =~ /\,/i) { $dbfldtmp =~ s/ _COMMA_ /,/g; # "," } if ($delimitor =~ /_/i) { $dbfldtmp =~ s/ _UNSC_ /_/g; # "_" } } # 090203 $sort_key = "$dbfldtmp$other_digits"; $sort_key =~ tr/a-z/A-Z/; # XNEW $MATCH{$sort_key} = $row; $mct++; $keymatched = 1; } } } } else { $continue = 1; if ($in{'noofdays'}) { ($entry_year,$entry_month,$entry_day) = split(/\//,$dbfld[ +1]); $entry_date = ($entry_year*10000) + ($entry_month*100) + $ +entry_day; if ($entry_date < $search_begin_date) { $matchflag=0; $con +tinue = 0; } } if ($continue == 1) { $matchflag=0; $joinstring = $keysstr; # print "$row<br>$joinstring<br><br>"; @fs = split(/$delimitor/,$joinstring); &convert_chars; $fsmatch = 0; $cnt2=0; foreach $flds (@dbfld) { if ($flds) { # r1 $flds = &replace_spc("$flds"); # r1 } # r1 $cnt3=$cnt2 + 1; # r1 $min_val = "min-f" . "$cnt3"; # r1 $max_val = "max-f" . "$cnt3"; # r1 if ($in{$min_val} || $in{$max_val}) { # r1 &check_range; # r1 } # r1 elsif ($fs[$cnt2] !~ /^\.\*$/) { # r1 $exactm = "exactmatch-f" . "$cnt3"; $wordm = "wordmatch-f" . "$cnt3"; $mkeysm = "multikeys-f" . "$cnt3"; $exclm = "exclude-f" . "$cnt3"; if (!$in{'case'} || $in{'case'} !~ /^yes$/i) { $flds =~ tr/a-z/A-Z/; $fs[$cnt2] =~ tr/a-z/A-Z/; } if ($in{$exactm}) { if ($in{$mkeysm} =~ /^yes$/i || $in{$exclm} =~ /^ +yes$/i) { undef @exclm_array; if ($in{'keywords_separator_is_comma'} =~ /^ye +s$/i) { @exclm_array = split (/[ ]*\,[ ]*/,$fs[$cnt +2]); } else { @exclm_array = split (/[ ]+/,$fs[$cnt2]); } $exclm_found = 0; $mkeysm_found = 0; foreach $exclmitem (@exclm_array) { if ($flds =~ /^$exclmitem$/ || $flds =~ /^$exclmitem\|\|/ || # $flds =~ /\|\|$exclmitem\|\|/ || # $flds =~ /\|\|$exclmitem$/) { # if ($in{$exclm} =~ /^yes$/i) { $exclm_found = 1; last; } else { $mkeysm_found = 1; last; } } } if ($in{$exclm} =~ /^yes$/i) { if ($exclm_found == 0) { $fsmatch++; } } else { if ($mkeysm_found == 1) { $fsmatch++; } } } else { if ($flds =~ /^$fs[$cnt2]$/ || # 001 $flds =~ /^$fs[$cnt2]\|\|/ || # 290102 $flds =~ /\|\|$fs[$cnt2]\|\|/ || # 290102 $flds =~ /\|\|$fs[$cnt2]$/) { # 290102 $fsmatch++; } } } else { if ($in{$wordm} =~ /y/i) { if ($in{$mkeysm} =~ /^yes$/i || $in{$exclm} =~ + /^yes$/i) { undef @exclm_array; if ($in{'keywords_separator_is_comma'} =~ / +^yes$/i) { @exclm_array = split (/[ ]*\,[ ]*/,$fs[$ +cnt2]); } else { @exclm_array = split (/[ ]+/,$fs[$cnt2]) +; } $exclm_found = 0; $mkeysm_found = 0; foreach $exclmitem (@exclm_array) { if (($flds =~ /^$exclmitem$/) || ($flds =~ /^$exclmitem /) || ($flds =~ / $exclmitem /) || ($flds =~ /$exclmitem\|\|/) || ($flds =~ /\|\|$exclmitem\|\|/) || ($flds =~ /\|\|$exclmitem/) || ($flds =~ / $exclmitem$/)) { if ($in{$exclm} =~ /^yes$/i) { $exclm_found = 1; last; } else { $mkeysm_found = 1; last; } } } if ($in{$exclm} =~ /^yes$/i) { if ($exclm_found == 0) { $fsmatch++; } } else { if ($mkeysm_found == 1) { $fsmatch++; } } } else { if (($flds =~ /^$fs[$cnt2]$/) || ($flds =~ /^$fs[$cnt2] /) || # qp ($flds =~ / $fs[$cnt2] /) || # qp ($flds =~ /$fs[$cnt2]\|\|/) || # 290102 ($flds =~ /\|\|$fs[$cnt2]\|\|/) || # 29 +0102 ($flds =~ /\|\|$fs[$cnt2]/) || # 290102 ($flds =~ / $fs[$cnt2]$/)) { $fsmatch++; } } } else { if ($in{$mkeysm} =~ /^yes$/i || $in{$exclm} =~ + /^yes$/i) { undef @exclm_array; if ($in{'keywords_separator_is_comma'} =~ / +^yes$/i) { @exclm_array = split (/[ ]*\,[ ]*/,$fs[$ +cnt2]); } else { @exclm_array = split (/[ ]+/,$fs[$cnt2]) +; } $exclm_found = 0; $mkeysm_found = 0; foreach $exclmitem (@exclm_array) { if ($flds =~ /$exclmitem/) { if ($in{$exclm} =~ /^yes$/i) { $exclm_found = 1; last; } else { $mkeysm_found = 1; last; } } } if ($in{$exclm} =~ /^yes$/i) { if ($exclm_found == 0) { $fsmatch++; } } else { if ($mkeysm_found == 1) { $fsmatch++; } } } else { if ($flds =~ /$fs[$cnt2]/) { $fsmatch++; } } } } } # else { # 42 # $fsmatch++; # 42 # } # 42 $cnt2++; } # if ($fsmatch==$cnt2) { # 42 if ($mcnt==$fsmatch) { # 42 &match_keywords; } } if ($matchflag == 1) { $other_digits = 10000 + $mct; if ($sort_idx == 0) { # 090203 $dbfldtmp = $key; # 090203 } # 090203 else { # 090203 $dbfldtmp = $dbfld[$sort_idx]; if ($delimitor =~ /\t/i) { $dbfldtmp =~ s/ _TAB_ /\t/g; # "tab" } if ($delimitor =~ /\,/i) { $dbfldtmp =~ s/ _COMMA_ /,/g; # "," } if ($delimitor =~ /_/i) { $dbfldtmp =~ s/ _UNSC_ /_/g; # "_" } } # 090203 $sort_key = "$dbfldtmp$other_digits"; $sort_key =~ tr/a-z/A-Z/; # XNEW $MATCH{$sort_key} = $row; $mct++; } } if (($mct >= $maxdisplay) || ($keymatched == 1)) { last; } # print "$row"; } close(DB); unless ($mct) { &nothing_found; } &set_content_type; open(FL,"$template_dir$in{'template'}"); @nfile=<FL>; close(FL); # More than one record matched the keys foreach $line (@nfile) { # For each html line $linet=$line; # Replace eman="fn" by its actual value # One $line can contain only one eman="fn" if ($linet =~ /eman=\"f.*\"/i) { $linet =~ s/.*eman=\"//i; $linet =~ s/\".*//; chop($linet); $line =~ s/eman=\"$linet\"//i; } if ($linet =~ /\*\*matchcnt\*\*/i) { $line =~ s/\*\*matchcnt\*\*/$mct/i; } if ($linet =~ /\*\*usdate\*\*/i) { $line =~ s/\*\*usdate\*\*/$us_date/i; } if ($linet =~ /\*\*europedate\*\*/i) { $line =~ s/\*\*europedate\*\*/$europe_date/i; } if ($line !~ /<!--repeat-->/ && $start ne "1") { # $line is before or after the repeat/endrepeat section print "$line"; } elsif ($line !~ /<!--repeat-->/ && $start==1) { # $line is between repeat and endrepeat (including endrepeat) $repeated .= $line; } else { # $line is repeat line $start=1; } if ($line =~ /<!--endrepeat-->/) { # $line is endrepeat, set $start ne 1 undef $start; $itemno = 0; if ($in{'nextstart'}) { $nextst = $in{'nextstart'}; $nextend = $nextst + $maxdispscreen - 1; } else { $nextst = 1; $nextend = $maxdispscreen; } if ($in{'sortfield'} eq "") { @sorted_keys = keys(%MATCH); } else { &sort_recs; } # Now sort all the matched records $oddeven = 0; foreach $sorted_key (@sorted_keys) { # For each matched recor +d @fs = split("$delimitor",$MATCH{$sorted_key}); if ($uid_pwd_sep =~ /^ \| $/i) { ($key,$pwd) = split(/ \| /,$fs[0]); } else { ($key,$pwd) = split(/$uid_pwd_sep/,$fs[0]); } $itemno++; if (($itemno >= $nextst) && ($itemno <= $nextend)) { if ($in{'url_flds'}) { # 050402 undef @urlflds; # 050402 @urlflds = split (/,/,$in{'url_flds'}); # 050402 foreach $urlfld (@urlflds) { # 050402 $urlfld =~ s/^f//i; # 050402 $urlfld = $urlfld - 1; # 050402 if ($fs[$urlfld] && $fs[$urlfld] !~ /^http:\/\//i +) { # 050402 $fs[$urlfld] = "http://" . $fs[$urlfld]; # 050 +402 } # 050402 } # 050402 } # 050402 # Assign field values in Matched Record to $TMP[f1,f2,. +.] $ctfs2=1; $fs2="f1"; foreach $val (@fs) { if ($delimitor =~ /\t/i) { $val =~ s/ _TAB_ /\t/g; # "tab" } if ($delimitor =~ /\,/i) { $val =~ s/ _COMMA_ /,/g; # "," } if ($delimitor =~ /_/i) { $val =~ s/ _UNSC_ /_/g; # "_" } $TMP{$fs2} = $val; if ($fs2 =~ /^f2$/i) { # 130303 ($x_yy,$x_mm,$x_dd) = split (/\//,$val); # 130303 $dateposted_us = "$x_mm/$x_dd/$x_yy"; # 130303 $dateposted_eu = "$x_dd/$x_mm/$x_yy"; # 130303 } # 130303 if ($fs2 =~ /^f3$/i) { # 140704 ($x_yy,$x_mm,$x_dd) = split (/\//,$val); # 140704 $dateupdated_us = "$x_mm/$x_dd/$x_yy"; # 140704 $dateupdated_eu = "$x_dd/$x_mm/$x_yy"; # 140704 } # 140704 $ctfs2++; $fs2 = "f" . "$ctfs2"; } &replace_inhtml_flds; # Extract and display matched keys $tline = $repeated; if ($tline =~ /<!--check-new-entry-->/i) { ($entry_year,$entry_month,$entry_day) = split(/\//,$ +TMP{'f2'}); $entry_date = ($entry_year*10000) + ($entry_month*10 +0) + $entry_day; if ($entry_date >= $new_entries_end_date) { $tline =~ s/<!--check-new-entry-->/<img src=\"$ne +w_image\" border=0>/ig; } } # Replace **dbname** **key** or **fn** by the actual va +lue undef @temp; (@temp) = split(/\*\*/,$tline); $repeat_string =""; foreach $item (@temp) { # This If applies to all search actions if ($item eq "key") { $item =~ s/$item/$key/; } if ($item eq "pwd") { $item =~ s/$item/$pwd/; } if ($item eq "dbname") { $item =~ s/$item/$dbname/; } if ($item eq "usdate") { $item =~ s/$item/$us_date/; } if ($item eq "europedate") { $item =~ s/$item/$europe_date/; } if ($item eq "datepostedus") { # 130303 $item =~ s/$item/$dateposted_us/; # 130303 } # 130303 if ($item eq "datepostedeu") { # 130303 $item =~ s/$item/$dateposted_eu/; # 130303 } # 130303 if ($item eq "dateupdatedus") { # 140704 $item =~ s/$item/$dateupdated_us/; # 140704 } # 140704 if ($item eq "dateupdatedeu") { # 140704 $item =~ s/$item/$dateupdated_eu/; # 140704 } # 140704 if ($item eq "bgcolor") { if ($oddeven == 0) { $item =~ s/$item/$bgcolor0/; } else { $item =~ s/$item/$bgcolor1/; } } # This If applies to search and display actions only if ($item =~ /^f\d+$/) { $inhtml_found = 0; foreach $inhtml_fld (@inhtml_array) { if ($item =~ /^$inhtml_fld$/i) { $inhtml_found = 1; last; } } $nitem = $TMP{$item}; if ($in{'multiple_selections'}) { # 290102 foreach $m_sel (@multi_selections) { # 290102 if ($item = ~ /^$m_sel$/i) { # 290102 $nitem =~ s/\|\|/; /g; # 290102 $nitem =~ s/; *$//; # 290102 last; # 290102 } # 290102 } # 290102 } # 290102 $nitem =~ s/ *-RET- */ /g; if ($inhtml_found == 0) { $nitem =~ s/\</&lt;/g; $nitem =~ s/\>/&gt;/g; $nitem =~ s/\"/&quot;/g; } $nitem =~ s/^select$//gi; $nitem =~ s/\.\*$delimitor$//g; $item =~ s/$item/$nitem/; } $repeat_string .= $item; } $ctfs2=1; $fs2="f1"; until ($ctfs2 > $maxfields) { $ctfsx = $ctfs2 - 1; $valx = $fs[$ctfsx]; if ($valx =~ /^0$/i || ($valx && $valx !~ /^select$/ +i)) { # if ($valx && $valx !~ /^select$/i) { $repeat_string =~ s/<!-- check-display-$fs2-//ig; $repeat_string =~ s/check-display-$fs2- -->//ig; } else { $repeat_string =~ s/<!-- check-display-!$fs2-//ig +; $repeat_string =~ s/check-display-!$fs2- -->//ig; } $ctfs2++; $fs2 = "f" . "$ctfs2"; } print "$repeat_string"; undef %TMP; if (($itemno >= $mct) && ($nextst >= ( $maxdispscreen + + 1 ))) { $prevst = $nextst - $maxdispscreen; print "</table><table border=0 cellspacing=0 cellpad +ding=5 width=90\%>\n<tr>\n<td width=15\%>\n"; print "<form method=\"POST\" action=\"$this_cgi_url\ +">\n"; foreach $entry (@pairs) { ($inname, $value) = split(/=/, $entry); if ($inname !~ /nextstart/i) { &prt_inname; # 141103 } } print "<input type=hidden name=\"nextstart\" value=\ +"$prevst\">\n"; print "<input type=\"submit\" value=\"Previous $maxd +ispscreen\">\n</form>\n"; print "</td><td width=85\%><b><font color=#ff0000 si +ze=2>\n"; print "Current $nextst to $itemno\n"; print "</font></b></td></tr>\n"; } } else { if ($itemno > $nextend) { print "</table><table border=0 cellspacing=0 cellpad +ding=5 width=90\%>\n<tr>\n<td width=15\%>\n"; if ($nextst >= ( $maxdispscreen + 1 )) { $prevst = $nextst - $maxdispscreen; print "<form method=\"POST\" action=\"$this_cgi_u +rl\">\n"; foreach $entry (@pairs) { ($inname, $value) = split(/=/, $entry); if ($inname !~ /nextstart/i) { &prt_inname; # 141103 } } print "<input type=hidden name=\"nextstart\" valu +e=\"$prevst\">\n"; print "<input type=\"submit\" value=\"Previous $m +axdispscreen\">\n</form>\n"; } print "</td>\n<td width=15\%>\n"; print "<form method=\"POST\" action=\"$this_cgi_url\ +">\n"; foreach $entry (@pairs) { ($inname, $value) = split(/=/, $entry); if ($inname !~ /nextstart/i) { &prt_inname; # 141103 } } $nextst2 = $nextend + 1; print "<input type=hidden name=\"nextstart\" value=\ +"$nextst2\">\n"; print "<input type=\"submit\" value=\"Next $maxdisps +creen\">\n</form>\n"; print "</td>\n<td width=70\%><b><font color=#ff0000 +size=2>\n"; print "Current $nextst to $nextend\n"; print "</font></b></td></tr>\n"; last; } } if ($oddeven == 0 ) { $oddeven = 1; } else { $oddeven = 0; } } } } } sub showblank { &check_template; open(FL,"$template_dir$in{'template'}"); @nfile=<FL>; close(FL); &set_content_type; # For each html line foreach $line (@nfile) { $linet=$line; # Replace eman="fn" by blank # One $line can contain only one eman="fn" if ($linet =~ /eman=\"f.*\"/i) { $linet =~ s/.*eman=\"//i; $linet =~ s/\".*//; chop($linet); $line =~ s/eman=\"$linet\"//i; } $line =~ s/\*\*f\d+\*\*//ig; # r10 print "$line"; } } sub return { print "Location: $ENV{'DOCUMENT_URI'}\n\n"; } sub check_url_referer { $referral_cnt = @valid; if ($referral_cnt > 0) { foreach $referer (@valid) { if ($ENV{'HTTP_REFERER'} =~ /$referer/i) { $good_ref = "yes"; last; } } if ($good_ref ne "yes") { &go_away; } } } sub error_no_db_name { &set_content_type; print "<html><body><center><font size=+1 color=\"FF0000\">ERROR: No + database filename specified.</font></center>"; print "<p>You must make sure a database filename is specified.</p>< +/body></html>\n"; exit; } sub error_no_template_name { &set_content_type; print "<html><body><center><font size=+1 color=\"FF0000\">ERROR: No + template filename specified.</font></center>"; print "<p>You must make sure a template filename is specified for y +our output.</p></body></html>\n"; exit; } sub error_not_a_command { &set_content_type; print "<html><body><center><font size=+1 color=\"FF0000\">ERROR: No +t a valid command.</font></center>"; print "<p>The \"action\" command is not valid. This might have been + caused by reloading a cgi program generated Web page.<p>Please go ba +ck to <a href=\"$return_url\">home page</a> to continue.</p></body></ +html>\n"; exit; } sub nothing_found { if ($error_page =~ /^yes$/i) { print "Location: $epage_not_found\n\n"; } else { &set_content_type; print "<html><body><center><font size=+1 color=\"FF0000\">Sorry! + Nothing found.</font></center>"; print "<p>No records match your search critera. Please press \"B +ack\" button and try again.</p></body></html>\n"; } exit; } sub go_away { if ($error_page =~ /^yes$/i) { print "Location: $epage_go_away\n\n"; } else { &set_content_type; print "<html><body><center><font size=+1 color=\"FF0000\">ERROR: + Unauthorised Access.</font></center>"; print "<p>Request denied. You are attempting to access our serve +r using an unauthorized form.</p></body></html>\n"; } exit; } sub invalid_sort_field { &set_content_type; print "<html><body><center><font size=+1 color=\"FF0000\">ERROR: In +valid field name specified for sorting.</font></center>"; print "<p>You didn't supply a correct field name for sorting. Pleas +e press \"Back\" button and enter again.</p></body></html>\n"; exit; } sub sort_recs { if ($in{'sorttype'} =~ /number-ascend/i ) { @sorted_keys = sort numeric keys(%MATCH); } elsif ($in{'sorttype'} =~ /number-decend/i ) { @sorted_keys = reverse(sort numeric keys(%MATCH)); } elsif ($in{'sorttype'} =~ /alpha-ascend/i ) { @sorted_keys = sort keys(%MATCH); } else { @sorted_keys = reverse(sort keys(%MATCH)); } } sub numeric { $a <=> $b }; sub error_form_method { &set_content_type; print "<html><body><center><font size=+1 color=\"FF0000\">Error: In +correct Form Request Method.</font></center>"; print "<p>You are not using \"method=get\" or \"method=post\" to su +bmit your Form. Please contact Webmaster.</p></body></html>\n"; exit; } sub rotate_database { if ($rotate_db_records eq "yes") { &check_db; if ($busy == 0) { open(DLR,"$last_rotated_date_file"); $last_rotated_date = <DLR>; close(DLR); if ($date_today ne $last_rotated_date) { undef @db_recs; open(DB,"$database_dir$dbname"); @db_recs = <DB>; close(DB); $rec_total = @db_recs; if ($rec_total > $rotation_count) { undef @top_recs; $offset = $rec_total - $rotation_count; @top_recs = splice(@db_recs,$offset,$rotation_count); unshift(@db_recs,@top_recs); open(DB,">$database_dir$dbname"); foreach $db_rec (@db_recs) { print DB "$db_rec"; } close(DB); open(DLR,">$last_rotated_date_file"); print DLR "$date_today"; close(DLR); } } &unlock_db; } } } sub convert_chars { $fsfs2_count = 0; foreach $fsPPP (@fs) { if ($fsPPP ne "") { $fs[$fsfs2_count] = &replace_spc("$fsPPP"); } $fsfs2_count++; } } sub replace_inhtml_flds { $inhtml_ctr = @inhtml_array; if ($inhtml_ctr > 0) { foreach $inhtml_fld (@inhtml_array) { undef @inhtml_file; if ($TMP{$inhtml_fld} =~ /^FILE\:\:/) { $t_inhtml = $TMP{$inhtml_fld}; $t_inhtml =~ s/FILE\:\://; open(INHTML,"$template_dir$t_inhtml"); @inhtml_file = <INHTML>; close(INHTML); $TMP{$inhtml_fld} = ""; foreach $inhtml_line (@inhtml_file) { $TMP{$inhtml_fld} .= $inhtml_line; } } } } } sub set_content_type { if ($charset eq "") { print "content-type: text/html\n\n"; } else { print "content-type: text/html\; charset=$charset\n\n"; } } sub check_special_chars { if ($keyword =~ /^\+*\\*\$*\%*\/*\!*\#*\@*\|*\&*\^*\~*\`*\(*\)*$/i) + { next; } else { $keyword = &replace_spc("$keyword"); } } sub replace_spc { local($word) = shift(@_); if ($match_special_chars =~ /^yes$/i) { # DO NOT CHECK ^, |, AND . THEY ARE USED FOR OTHER PURPOSE # 290 +102 $word =~ s/\+/SPC001/g; $word =~ s/\\/SPC002/g; $word =~ s/\$/SPC003/g; $word =~ s/\%/SPC004/g; $word =~ s/\//SPC005/g; $word =~ s/\#/SPC006/g; $word =~ s/\@/SPC007/g; # $word =~ s/\|/SPC008/g; # 290102 $word =~ s/\&/SPC009/g; $word =~ s/\~/SPC010/g; $word =~ s/\`/SPC011/g; $word =~ s/\(/SPC012/g; $word =~ s/\)/SPC013/g; $word =~ s/\[/SPC014/g; $word =~ s/\]/SPC015/g; $word =~ s/\{/SPC016/g; $word =~ s/\}/SPC017/g; $word =~ s/\</SPC018/g; $word =~ s/\>/SPC019/g; } else { if ($delimitor =~ /\t/i) { $word =~ s/[^a-z0-9A-Z\,\t\^\|\ ]+/.*/g; # 290102 } elsif ($delimitor =~ /\,/i) { $word =~ s/[^a-z0-9A-Z\,\^\|\ ]+/.*/g; # 290102 } else { $word =~ s/[^a-z0-9A-Z\,\^\|\ ]+/.*/g; # 290102 } } return $word; } sub check_dbname { if ($in{'dbname'} =~ /[^a-z0-9A-Z\ \_\-\.]+/) { &error_invalid_dbname; } else { $dbname = $in{'dbname'}; } } sub error_invalid_dbname { &set_content_type; print "<html><body><center><font size=+1 color=\"FF0000\">ERROR: In +valid database filename (dbname).</font></center>"; print "<p>Please use only alphanumeric characters (space, dot, hyph +en, underscore allowed) for your database filename.</p></body></html> +\n"; exit; } sub check_template { if ($in{'template'}) { if ($in{'template'} =~ /[^a-z0-9A-Z\ \_\-\.]+/) { &error_invalid_template; } } else { &error_no_template_name; } } sub error_invalid_template { &set_content_type; print "<html><body><center><font size=+1 color=\"FF0000\">ERROR: In +valid template filename.</font></center>"; print "<p>Please use only alphanumeric characters (space, dot, hyph +en, underscore allowed) for your template filename.</p></body></html> +\n"; exit; } sub check_db { $now_time = time; $busy = 1; $sec_cnt = 0; while ($sec_cnt < $wait_time) { if (-e "$database_dir$lock_fn") { open(LOCK,"$database_dir$lock_fn"); $last_time = <LOCK>; close(LOCK); $diff_time = $now_time - $last_time; if ($diff_time > 120) { # file exists longer than 120 secs &lock_db; $busy = 0; last; } sleep 1; } else { &lock_db; $busy = 0; last; } $sec_cnt++; } } sub lock_db { open(LOCK,">$database_dir$lock_fn"); print LOCK "$now_time"; close(LOCK); $locked = 1; } sub unlock_db { if (($locked == 1) && (-e "$database_dir$lock_fn")) { unlink("$database_dir$lock_fn"); } } sub match_keywords { if ($in{'keywords'}) { $row1 = &replace_spc("$row"); if (!$in{'case'} || $in{'case'} !~ /^yes$/i) { $row1 =~ tr/a-z/A-Z/; } $skip_record = 0; # ds foreach $keyword (@keywords) { &check_special_chars; if (!$in{'wordmatch-keywords'} || $in{'wordmatch-keywords'} ! +~ /y/i) { if ($row1 =~ /$keyword/) { if ($keywords_operator eq "and") { # ds next; # ds } # ds else { # ds $matchflag=1; # ds last; # ds } # ds } # ds else { # ds if ($keywords_operator eq "and") { # ds $skip_record = 1; # ds last; # ds } # ds } } else { if (($row1 =~ /$delimitor$keyword /) || ($row1 =~ / $keyword /) || ($row1 =~ / $keyword$delimitor/) || ($row1 =~ / $keyword$delimitor$/) || ($row1 =~ /^$keyword /) || ($row1 =~ /^$keyword$delimitor$/) || ($row1 =~ /^$keyword$delimitor/) || ($row1 =~ /$delimitor$keyword$delimitor/) || ($row1 =~ /$delimitor$keyword\|\|/) || # 250703 ($row1 =~ /\|\|$keyword\|\|/) || # 250703 ($row1 =~ /\|\|$keyword$delimitor/) || # 250703 ($row1 =~ /$delimitor$keyword$delimitor$/)) { if ($keywords_operator eq "and") { # ds next; # ds } # ds else { # ds $matchflag=1; # ds last; # ds } # ds } # ds else { # ds if ($keywords_operator eq "and") { # ds $skip_record = 1; # ds last; # ds } # ds } } } if ($keywords_operator eq "and" && $skip_record == 0) { # ds $matchflag=1; # ds } # ds } else { $matchflag=1; } } sub chk_cookie { if ($in{'mbr_only'} =~ /^yes$/i) { if ($ENV{HTTP_COOKIE}) { ($dummy,$key_pwd_stuff) = split ("userinfo=",$ENV{HTTP_COOKIE +}); $key_pwd_stuff =~ s/\;.*//; if ($key_pwd_stuff) { ($user_name,$user_pwd) = split (" _ ",$key_pwd_stuff); if (!$user_name || !$user_pwd) { &error_invalid_member; } } else { &error_invalid_member; } } else { &error_invalid_member; } } } sub error_invalid_member { &set_content_type; print "<html><body><center><font size=+1 color=\"FF0000\"><b>Sorry! + Your request is rejected. Please Login to access this service.</b>< +/font></center>\n"; print "<p><center><b><a href=\"$return_url\">Back Home</a></b></cen +ter></p></body></html>\n"; exit; } sub check_range { # r1 if ($in{$min_val}) { # r1 $lower = $in{$min_val}; # r1 if (($lower !~ /^-?\d+$/) && # r1 ($lower !~ /^-?\d+\.$/) && # r1 ($lower !~ /^-?\.\d+$/) && # r1 ($lower !~ /^-?\d+\.\d+$/)) { # r1 &error_range_input; # r1 } # r1 } # r1 if ($in{$max_val}) { # r1 $upper = $in{$max_val}; # r1 if (($upper !~ /^-?\d+$/) && # r1 ($upper !~ /^-?\d+\.$/) && # r1 ($upper !~ /^-?\.\d+$/) && # r1 ($upper !~ /^-?\d+\.\d+$/)) { # r1 &error_range_input; # r1 } # r1 } # r1 if ($delimitor =~ /\,/i) { # r1 $flds =~ s/ _COMMA_ //g; # "," # r1 } # r1 $flds =~ s/\,//g; # r1 if ($in{$min_val} && !$in{$max_val}) { # r1 if ($flds >= $lower) { # r1 $fsmatch++; # r1 } # r1 } # r1 elsif (!$in{$min_val} && $in{$max_val}) { # r1 if ($flds <= $upper) { # r1 $fsmatch++; # r1 } # r1 } # r1 else { # r1 if ($in{$min_val} && $in{$max_val}) { # r1 if ($flds >= $lower && $flds <= $upper) { # r1 $fsmatch++; # r1 } # r1 } # r1 } # r1 } # r1 sub error_range_input { # r1 &set_content_type; # r1 print "<html><body><center><font size=+1 color=\"FF0000\">ERROR: In +valid Input In Range Search Specifications.</font></center>"; # r1 print "<p>All range search inputs must be integers or floating poin +t numbers. Please press \"Back\" button and check your input again.</ +p></body></html>\n"; exit; # r1 } # r1 sub error_invalid_operator_mix { &set_content_type; print "<html><body><center><font size=+1 color=\"FF0000\"><b>Error: + Invalid Operator Mix</b></font></center>\n"; print "<p>You can only use either \"&&\" (the <b>AND</b> operator) +or \"++\" (the <b>OR</b> operator) in your search box. Please press t +he \"Back\" button to try again.</p>\n"; print "<p><center><b><a href=\"$return_url\">Back Home</a></b></cen +ter></p></body></html>\n"; exit; } sub calculate_new_entries_end_date { %xmondays = ('1','31','2','28','3','31','4','30','5','31','6','30', +'7','31','8','31','9','30','10','31','11','30','12','31'); ($xsec,$xmin,$xhour,$xday,$xmon,$xyr,$xwday,$xyday,$xisdst) = local +time(time); $xmonth = ($xmon + 1); $xyr = 1900 + $xyr; $xleapYr = $xyr % 4; if ($xleapYr == 0) { $xmondays{'2'} = "29"; } $days_left = $days_for_new_entries; $days_this_mon = $xday; $xdone = 0; while ($xdone==0) { if ($days_this_mon > $days_left) { $xdone = 1; last; } else { $days_left = $days_left - $days_this_mon; if ($xmonth > 1) { $xmonth--; } else { $xmonth = 12; $xyr--; } $days_this_mon = $xmondays{$xmonth}; if ($days_left <= 0) { $xdone = 1; last; } } } $end_day = $days_this_mon - $days_left; $end_month = $xmonth; # 230203 $end_year = $xyr; # 230203 $new_entries_end_date = ($end_year*10000) + ($end_month*100) + $end +_day; } sub calculate_search_begin_date { %xmondays = ('1','31','2','28','3','31','4','30','5','31','6','30', +'7','31','8','31','9','30','10','31','11','30','12','31'); ($xsec,$xmin,$xhour,$xday,$xmon,$xyr,$xwday,$xyday,$xisdst) = local +time(time); $xmonth = ($xmon + 1); $xyr = 1900 + $xyr; $xleapYr = $xyr % 4; if ($xleapYr == 0) { $xmondays{'2'} = "29"; } $days_left = $noofdays; $days_this_mon = $xday; $xdone = 0; while ($xdone==0) { if ($days_this_mon > $days_left) { $xdone = 1; last; } else { $days_left = $days_left - $days_this_mon; if ($xmonth > 1) { $xmonth--; } else { $xmonth = 12; $xyr--; } $days_this_mon = $xmondays{$xmonth}; if ($days_left <= 0) { $xdone = 1; last; } } } $end_day = $days_this_mon - $days_left; $end_month = $xmonth; # 230203 $end_year = $xyr; # 230203 $search_begin_date = ($end_year*10000) + ($end_month*100) + $end_da +y; } sub invalid_noofdays { &set_content_type; print "<html><body><center><font size=+1 color=\"FF0000\"><b>Error: + Invalid \"noofdays\" Specification</b></font></center>\n"; print "<p>Something wrong with the specification of your search for +m parameter \"noofdays\". Please check with your Webmaster.</p>\n"; print "<p><center><b><a href=\"$return_url\">Back Home</a></b></cen +ter></p></body></html>\n"; exit; } sub invalid_days_for_new_entries { &set_content_type; print "<html><body><center><font size=+1 color=\"FF0000\"><b>Error: + Invalid \"days_for_new_entries\" Specification</b></font></center>\n +"; print "<p>Something wrong with the specification of your search for +m parameter \"days_for_new_entries\". Please check with your Webmaste +r.</p>\n"; print "<p><center><b><a href=\"$return_url\">Back Home</a></b></cen +ter></p></body></html>\n"; exit; } sub check_expiry_date { # 150803 if ($in{'supervisor'} =~ /^yes$/i) { $expired = 0; } else { $x_expiryDate_fld = $expiryDate_fld; $x_expiryDate_fld =~ s/^f//ig; $x_expiryDate_fld--; @numbers = split (/\//,$dbfld[$x_expiryDate_fld]); $array_size = @numbers; if ($array_size != 3) {$expired = 1;} if ($expired == 0) { if ($numbers[0] =~ /\D+/ || $numbers[1] =~ /\D+/ || $numbers[2] =~ /\D+/) { $expired = 1; } } if ($expired == 0) { if (($numbers[0] < 1 || $numbers[0] > 31) || ($numbers[1] < 1 || $numbers[1] > 12) || ($numbers[2] < 2000 || $numbers[2] > 3000)) { $expired = 1; } } if ($expired == 0) { $xdate1 = ($year*10000) + ($month*100) + $mday; $xdate2 = ($numbers[2]*10000) + ($numbers[1]*100) + $numbers[ +0]; if ($xdate2 < $xdate1) { $expired = 1; } } } } # 150803 sub prt_inname { # 141103 $val_inname = $in{$inname}; if ($delimitor =~ /\t/i) { $val_inname =~ s/ _TAB_ /\t/g; # "tab" } if ($delimitor =~ /\,/i) { $val_inname =~ s/ _COMMA_ /\,/g; # "," } if ($delimitor =~ /_/i) { $val_inname =~ s/ _UNSC_ /_/g; # "_" } print "<input type=hidden name=\"$inname\" value=\"$val_inname\">\n +"; } # 141103

Replies are listed 'Best First'.
Re^2: How do you search multiple flat file databases?
by Anonymous Monk on Oct 29, 2004 at 09:26 UTC
    Wow, i thank you for the assistance in making my question legable for wise men. Your the best, Thanks