#!/usr/bin/perl
#
# Name : Dict
# Purpose: perl script cgi program to submit dict queries.
# Author : Doug L. Hoffman (hoffman@shopthenet.net)
# Created: Thu Aug 14 09:51:28 1997 by hoffman
# Revised: Mon Sep 3 22:44:52 2001 by bam@miranda.org
#
# This perl script both generates the www-browser form and accepts the results
# of submitting the form. The search is transmitted to a central machine
# and the results are interpreted and reposted for the user.
#
# $Log: Dict,v $
# Revision 1.11 1998/03/30 17:33:26 hoffman
# added text of query to the web page title line.
#
# Revision 1.10 1998/02/23 17:46:04 hoffman
# Fixed problem with word list anchors caused by changes to the "exact"
# resopnse.
#
# Revision 1.9 1998/02/23 16:41:30 hoffman
# Made "exact" return list of words, not definitions.
#
# Revision 1.8 1998/02/20 16:00:01 hoffman
# gave the generated page(s) a general facelift, changed internal processing
# to use the short database name, not the description, and then spent time
# fixing various bugs that the name change caused. The server calls to fetch
# the search and database options have been combined into one.
#
# Revision 1.7 1998/02/19 15:10:26 hoffman
# Rik's updated version.
#
# Revision 1.6 1997/11/12 16:07:50 hoffman
# Added link to copyright info.
#
# Revision 1.5 1997/10/01 17:50:58 hoffman
# Fixed some of the field edits for Rik.
#
# Revision 1.4 1997/10/01 13:56:52 hoffman
# fixed problem with ()'s
#
# Revision 1.3 1997/08/17 20:48:04 hoffman
# added link to dict.org home page
#
# Revision 1.2 1997/08/17 20:07:37 hoffman
# Fixed imbedded blank sequence query scanning.
#
#
# Setup is minimal.
#
# You have to redefine at most the first few lines below
#
# $ReturnUrl is the url of this file. It should be changed to reflect
# the new location.
# ---------- Configuration variables
$Debug = 0;
$Pgm = "Dict";
#$hostUrl = "http://www.dict.org";
$hostUrl = "";
$cgiPath = "$hostUrl/bin";
$ReturnUrl = "$cgiPath/$Pgm";
$bin = "/data/httpd/html/bin";
$CRInfo = "$ReturnUrl?Form=$Pgm".
"1&Query=00-database-info&Strategy=*&Database=*";
$SInfo = "$ReturnUrl?Form=$Pgm". "4";
$Dict = "/usr/local/bin/dict -h localhost";
$DictAlt = "/usr/local/bin/dict -h dega.cs.unc.edu";
$Counter = "/usr/local/etc/Counter/data/$Pgm.dat";
$Count = "$cgiPath/Count.cgi";
$Background = "/gifs/grayback.jpg";
$Heading1= "The DICT Development Group: Online Dictionary Query";
$Heading2= "The DICT Development Group: Online Dictionary Query";
$Counter1= "";
$Counter2= "";
# $Counter1= "";
# $Counter2= "";
$WebMaster="webmaster\@dict.org";
# --- display stuff
##########################################################################
#
# Driving Program
#
#########################################################################
&init; # init globals
&ReadParse; # read stdin
#
#
# If there is no standard input, this the the users first request to see
# the page. Return a decent looking page. Otherwise, you have work to do.
#
if ($in{"Form"} eq "") {
$in{"Database"} = "*";
$in{"Strategy"} = "*";
print &PrintHeader();
&SendBeginning;
&SendForm1;
&SendEnding;
}
elsif ($in{"Form"} eq ($Pgm . '1')) {
print &PrintHeader();
&SendBeginning;
&StripFields; # clean up user entered data.
&CheckFields; # Make sure all required data are there.
&SendForm1;
if ($Error eq "") {
&SendListing;
}
&SendEnding;
}
elsif ($in{"Form"} eq ($Pgm . '2')) {
$in{"Strategy"} = "*";
print &PrintHeader();
&SendBeginning;
&StripFields; # clean up user entered data.
&CheckFields; # Make sure all required data are there.
&SendForm1;
if ($Error eq "") {
&SendListing;
}
&SendEnding;
}
elsif ($in{"Form"} eq ($Pgm . '3')) {
$in{"Strategy"} = "";
$in{"Query"} = "";
print &PrintHeader();
&SendBeginning;
&StripFields; # clean up user entered data.
# &CheckFields; # Make sure all required data are there.
&SendForm1;
if ($Error eq "") {
&SendListing;
}
&SendEnding;
}
elsif ($in{"Form"} eq ($Pgm . '4')) {
$in{"Strategy"} = "";
$in{"Query"} = "";
print &PrintHeader();
&SendBeginning;
&StripFields; # clean up user entered data.
# &CheckFields; # Make sure all required data are there.
&SendForm1;
if ($Error eq "") {
$in{"Query"} = "Server";
&SendListing;
}
&SendEnding;
}
else {
print &PrintHeader();
&SendBeginning;
print "
\n";
return;
}
$count = \n";
return;
}
print CT $count;
close CT;
}
}
# ---------- Strip fields
#
# change tabs and stuff to blanks, strip any leading/trailing blanks.
#
sub StripFields {
foreach $x (@Fields) {
$in{$x} =~ y/{};/() /; # ensure no {, },",", or ";".
$in{$x} =~ y/\n\r\f\t\e/ /s; # ensure newlines or cr's.
$in{$x} =~ s/\'/\'\'/g;
$in{$x} =~ s/^\s*//;
$in{$x} =~ s/\s*$//;
$in{$x} =~ s/\s+/ /g;
}
}
#
# ---------- Check that the required fields are all present.
#
sub CheckFields {
$Error = "";
foreach $x (@ReqFields) {
if ($in{$x} eq "") {
$Error = $x;
return;
}
}
}
#############################################################################
#
# ---------- Send the html form for the editing of a record
#
sub SendForm1 {
# ----- send the header
#
local($q) = $in{"Query"};
#
#$Counter1
#
#$Counter2
print <
Definition not available or out of date?
Contribute to FILE.
\n" if ($Debug);
if (!open(IN,"$Dict $command |")) {
print " \n";
print "Backend database engine temporarily unavailable:\n";
print " please try again later\n";
print " \n" if ($Debug);
if (open(IN,"$DictAlt $command |")) {
print "Using backup server...\n";
print " ";
}
print "
Query string:
Search type:
Database:
Database copyright information
Server information
EOF3
}
#############################################################################
#
# ---------- Send the html form for the search listing results
#
sub SendListing {
local( $command, $d, $s, $q);
local( $i, $x );
local( $flag=0 );
local( $triedbackup = 0 );
# ----- add the hidden counter.
# print "\n\n";
# print "\n";
# &UpdateCounter;
# ---------- report
$d = $in{"Database"};
$d = $in{"Database"} if ($d eq "");
$s = $in{"Strategy"};
$q = $in{"Query"};
$command = "--client \"$ENV{'REMOTE_HOST'} $ENV{'HTTP_USER_AGENT'}\" ";
if ($s eq "" && $q eq "") {
$command .= "-i '$d'";
} elsif ($s eq "" && $q eq "Server") {
$command .= "-I";
} else {
$command .= "-d '$d'";
$wordlist = 0;
if ($s eq '*') {
$command .= " \'". $q . "\'";
}
# elsif ($s eq 'exact') {
# $command .= " -s exact \'". $q ."\'";
# }
else {
$command .= " -s $s -m \'". $q ."\'";
}
}
print "$Dict $command
\n";
return;
}
if ($s eq "" && $q eq "") {
local($tmp) = &lx($Dbr{$d});
print "From $tmp<\/a>:\n";
}
print "";
restart:
while(
$_\n";
}
elsif (/^\d+ /) {
print "
$_";
}
elsif (/^No definitions/) {
print "
$_\n";
}
elsif (/^No matches/) {
print "
$_\n";
}
elsif (/^(\S+) /) {
$x = $1;
($x, $line) = split(/:/, $_, 2);
$line = &anchor( $x, $line);
print "$x:$line";
$wordlist = 1;
}
elsif ($wordlist && (/^ (\S+) /)) {
$line = &anchor( $x, $_);
print $line;
}
else {
if (/(ftp|http):\/\/[^\s\)\}]*\}/) {
s,((ftp|http)://[^\s\)\}]*)\},}$1,g;
} else {
s,((ftp|http)://[^\s\)\}]*),$1,g;
}
s,(\s){([^}\s][^}]*)},$1.''.$2.'',ge;
if (/(\s){([^}\s][^}]*)(\n)$/) {
$savefirst = $2;
}
s,(\s){([^}\s][^}]*)(\n)$,$1.''.$2.$3.'',se;
s,^(\s*)([^}]*)},$1.''.$2.'',e;
print;
}
}
print "
\n";
close( IN );
if (!$flag) {
if (!$triedbackup) {
++$triedbackup;
print "$DictAlt $command \n";
goto restart;
}
}
print "\n";
print "Backend database engine error: please try again later\n";
print "
\n";
}
sub xl { local($tmp) = $_[0]; $tmp =~ tr/ /+/; $tmp; }
sub lx { local($tmp) = $_[0]; $tmp =~ tr/+/ /; $tmp; }
sub anchor {
local( $dbname, $line) = @_;
local( $x, $y, $db, $new_line);
$odd = 1;
$db = $Dbr{$dbname};
$db = $dbname;
foreach $x (split("\"", $line)) {
if ($odd) {
$x =~ s/ (\S+)/ $1<\/a>/g;
$new_line .= $x;
$odd = 0;
}
else {
($y = $x) =~ tr/ /+/;
$new_line .= "\"$x\"<\/a>";
$odd = 1;
}
}
return $new_line;
}
#
# ----- Common beginning.
#
sub SendBeginning {
local ($title);
$title = $Heading1;
if ($in{'Query'}) {
$title .= "- $in{'Query'}";
}
print <
$Heading2
EOF
}
#
# ----- Common ending.
#
sub SendEnding {
print <