########################################################################
#                                                                      #
# BBN.PM - a Perl module for the BBN site. Defines several paths and   #
# the following routines:  					       #
#	cgi_receive, cgi_decode, cgi_header, cgi_head, cgi_footer,	       #
#       unmangle                                                       #
#                                                                      #
########################################################################

$BASE_PATH 	= "/home/www/proof/";
$BASE_URL 	= "http://www.catosource.com/";

$SCRIPT_DIR	= "cgi-bin";
$DOC_DIR 	= "htdocs";
$IMAGE_DIR	= "pix";
$MAP_DIR	= "maps";
$HELP_DIR	= "help";
$CLIENT_DIR	= "client-html";
$SESSION_DIR	= "sessions";
$TEMPLATE_DIR	= "template";

$SCRIPT_PATH 	= $BASE_PATH . $SCRIPT_DIR . "/";
$DOC_PATH    	= $BASE_PATH . $DOC_DIR . "/";
$HELP_PATH 	= $DOC_PATH . $HELP_DIR . "/";
$CLIENT_PATH 	= $DOC_PATH . $CLIENT_DIR . "/";
$SESSION_PATH	= $DOC_PATH . $SESSION_DIR . "/";
$TEMPLATE_PATH	= $SESSION_PATH . $TEMPLATE_DIR . "/";
$IMAGE_PATH	= $DOC_PATH . $IMAGE_DIR . "/";
$CATEGORIES_IMAGE_PATH	= "/$IMAGE_DIR/$CATEGORIES_DIR/";

$KEYTABLE = $CLIENT_PATH . "keywordtable" ;
$FIRST_DOC = "overview.html";
$LOGON = "logon.html" ;
$GEN_SID = "newSession";
$MASH_SCRIPT = "mashurl" ;

$ALPHA_SEARCH_FORM = "alpha.search.html";
$GEO_SEARCH_FORM = "geo.search.html";
$POWER_SEARCH_FORM = "fullsearch.html";

# Where the mode info is stored - shared between ShowModes and alpha.search
$MODE_PAGE = "modes.html";
$MODE_MAP = "modes.map";

# The script used for building category queries from an image map
$CAT_SCRIPT = "ShowCategories.pl";
$CAT_PAGE = "categories.html";
$CAT_MAP = "categories.map";

#Replacement string used for creating files from templates
$REPLACE_STRING_SID = "\@\@\@REPLACEMESID\@\@\@";
$REPLACE_STRING_DIR = "\@\@\@REPLACEMEDIR\@\@\@";
$REPLACE_STRING_RER = "\@\@\@RER\@\@\@";
$REPLACE_STRING_OVMAP = "\@\@\@REPLACEMEOVMAP\@\@\@" ;
$REPLACE_STRING_TK = "\@\@\@REPLACEMETK\@\@\@" ;
$REPLACE_STRING_NOTAB = "\@\@\@NOTAB\@\@\@" ;

# session timing information
$MAX_AGE = 24 * 3600 ;  # 24 hours
$MAX_IDLE = 1800 ;      # 1/2 hour

# database names, etc.
$DB          = "vespa";
$DBDUMP_DIR 	= "dbdumps" ;
$DBDUMP_PATH 	= $BASE_PATH . $DBDUMP_DIR . "/" ;
$DBDUMPER = "/home/mercury/Minerva/bin/msqldump" ;
$TBL_SESSION = "session";
$TBL_REG = "registration";
$TBL_FILES = "clienthtml";
$REG_FIELDS = "(logon, pwd, lastname, firstname, company, title, 
		address, country, phone, fax, howheard, datemade, 
		ipaddress, hostname, username, status, lastvisit)";
$SESSION_FIELDS = "(session, logon, ipaddress, hostname, username, 
		    dateused, datemade)";
$FILES_FIELDS = "(filename,date,company)";


# Useful arrays that may be needed in several places
@DAYS = qw{ Sunday Monday Tuesday Wednesday Thursday Friday Saturday };
@MONTHS = qw{ Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec };


# Return 1 to indicate success
1;

######################################################################
#  SUBROUTINES

#####################################################################
# Procedure to get the information sent from a form. Works with both  
# POST and GET methods.
#####################################################################

sub cgi_receive {
    if ($ENV{'REQUEST_METHOD'} eq "POST") {
        read(STDIN, $incoming, $ENV{'CONTENT_LENGTH'});
    }
    else {
        $incoming = $ENV{'QUERY_STRING'};
    }
}


#####################################################################
# Procedure to process the information sent from a form. Takes the
# raw information and creates two arrays: @fields and %FORM. The 
# elements of @fields are the subscripts for %FORM. The elements
# of %FORM are the form values.
#####################################################################

sub cgi_decode {
    local(@pairs) = split(/&/, $incoming);

    foreach (@pairs) {
        ($name, $value) = split(/=/, $_);

        $name  =~ tr/+/ /;
        $value =~ tr/+/ /;
        $name  =~ s/%([A-F0-9][A-F0-9])/pack("C", hex($1))/gie;
        $value =~ s/%([A-F0-9][A-F0-9])/pack("C", hex($1))/gie;

        #### Strip out semicolons unless for special character
        $value =~ s/;/$$/g;
        $value =~ s/&(\S{1,6})$$/&\1;/g;
        $value =~ s/$$/ /g;

        $value =~ s/\|/ /g;
        $value =~ s/^!/ /g; ## Allow exclamation points in sentences
	$value =~ s/\n/ /g; ## Remove embedded newlines

        #### Skip blank text entry fields
        next if ($value eq "");

        #### Check for "assign-dynamic" field names
        #### Mainly for on-the-fly input names, especially checkboxes
        if ($name =~ /^assign-dynamic/) {
            $name = $value;
            $value = "on";
        }

	#### Allow for multiple values of a single name
        $FORM{$name} .= " " if ($FORM{$name});

        $FORM{$name} .= $value;
        push (@fields, $name) unless ($name eq $fields[$#fields]);
    }
}


sub unmangle {
  local ($dummy) = @_;

  $dummy =~ s/___/ \& /g;
  $dummy =~ s/__/\&/g;
  $dummy =~ s/_/ /g;

  return $dummy;
}


sub cgi_head {
  print "Content-type: text/html\n\n";
}

sub cgi_header {
  local ($title) = @_;

  print<<EndOfDoc;
Content-type: text/html

<HTML>
<HEAD>
<TITLE>$title</TITLE>
</HEAD>
<BODY
background="/$IMAGE_DIR/panel.gif"
text="#000000" link="#ff0000" vlink="#000aaf" alink="#0ff000"
>
EndOfDoc
}


sub cgi_footer {
  local ($context) = @_;
  if (!$context) { $context = "footer"; }

  print<<EndOfDoc;
<br>
<hr height=5 >

<ADDRESS>
&#169 copyright 1995 Cato Creative Systems, Inc.<br>
4364 S. Alston Ave., Durham NC 27713 USA<br>
tel: 919-361-2286 fax: 919-361-2290<br>
email: <a href="mailto:source\@mail.cato.com">source\@mail.cato.com</a>
</ADDRESS>

</BODY>
</HTML>
EndOfDoc
}



