## /*   bcnu.inc,v 1.17 2000/03/16 14:38:15 jap Exp
#
# *    Copyright (c) 1998 John Phillips
# *
# *    You may distribute under the terms of the Artistic License
# *
# */
#

require "dotsh.pl";

dotsh("/usr/local/bcnu/etc/bcnuenv");

if ( $BCNUSERVER eq "linux" ) {	# linux doesn't seem to work correctly with bash
	$ENV{'SHELL'}="/bin/sh";
	dotsh("/usr/local/bcnu/etc/bcnulocal");
}


######################## Should not require to change below here #############

$BUILD='1.17';
$VERSIONDATE='2000/03/16 14:38:15';

# The following variables can be set in bcnulocal to override
# the defaults in here

$PERLINE=8 unless $PERLINE;	# hosts per line to show
$REFRESH=2 unless $REFRESH;	# browser refresh interval (minutes)
$QRYLIMIT=50 unless $QRYLIMIT;	# default number of records returned for a query
$LASTTIME=.2 unless $LASTTIME;	# About 3 hours

#

$IGNOREPAT='net-.+';	# Don't show net subagents e.g. net.ftp in main

# external web server
if ( $BCNUVERSION ) {
        $VERSION = $BCNUVERSION;
        }
else    {
	open (VERSION, "$BCNUHOME/etc/bcnu_version") || die ("version file problem");
	$VERSION=<VERSION>;
	chop $VERSION;
	close (VERSION);
	}

$WWW="$BCNUHOME/www";
$DATA="$BCNUHOME/data";
$VIEWHOME="$BCNUHOME/etc";

$BASECELL="";
$BASEROW="ALIGN=CENTER BGCOLOR=#ffff80";

$WARNCELL="BGCOLOR=#c0c0c0";
$CELL=$BASECELL;
$ECELL="BGCOLOR=#FF8080";
$BLACKCELL="BGCOLOR=#000000";
$IGNORECELL="BGCOLOR=#FF00FF";
$BGCELL="ALIGN=CENTER BGCOLOR=#8080FF";
#$DEFCELL="$BGCELL WIDTH=50";
$DEFCELL="$BGCELL";

$RED="#FF0000";
$GREEN="#008000";
$ORANGE="#FF8000";
$VIOLET="#FF00FF";

$SAD="<IMG BORDER=0 WIDTH=14 HEIGHT=14 SRC=$BCNUURL/images/bcsad.gif";
$WORRY="<IMG BORDER=0 WIDTH=14 HEIGHT=14 SRC=$BCNUURL/images/bcworry.gif";
$NORMAL="<IMG BORDER=0 WIDTH=14 HEIGHT=14 SRC=$BCNUURL/images/bcsmiley.gif";
$ATTACH="<IMG BORDER=0 WIDTH=19 HEIGHT=16 SRC=$BCNUURL/images/bcatt.gif";
$HISTRY="<IMG BORDER=0 WIDTH=17 HEIGHT=17 SRC=$BCNUURL/images/bchist.gif";
$IMGIGNORE="<IMG BORDER=0 SRC=$BCNUURL/images/bcignore.gif";

$DEFBG="#ffffff";
#$SIREN="<BGSOUND SRC=\"$BCNUURL/sounds/alert.wav\" LOOP=INFINITE>\n";
#$SIREN.="<EMBED SRC=\"$BCNUURL/sounds/alert.wav\" HIDDEN=TRUE AUTOSTART=TRUE LOOP=TRUE>\n";
#$WARNSND="<BGSOUND SRC=\"$BCNUURL/sounds/warning.wav\" LOOP=INFINITE>\n";
#$WARNSND.="<EMBED SRC=\"$BCNUURL/sounds/warning.wav\" HIDDEN=TRUE AUTOSTART=TRUE LOOP=TRUE>\n";

( $sec,$min,$hr,$mday,$mon,$yr,$rest ) = (localtime());
$mon++;

# Y2k stuff
$yy=19;
$yy=20 unless $yr > 70;
$yrs=$yr + 1900;

$now=sprintf("%2.2d/%2.2d/%4.4d %2.2d.%2.2d",$mday,$mon,$yrs,$hr,$min);

push (@INC,"$BCNUHOME/cgi-bin");

%bcnu_status = (0,"ok",1,"blackout",5,"warning",6,"alert",9,"error");
%bcnu_host_type = (0,"Unspecified",1,"Unix",2,"Netware",3,"WinNT",4,"Win95",20,"Router",30,"Printer",40,"Appliance");
$def_host_type=1;

&get_cookie("bcnulogin");
undef $cookie{"bcnulogin"} if $cookie{"bcnulogin"} =~ /deleted/;
if (defined $cookie{"bcnulogin"}) {
	$loginid=$cookie{"bcnulogin"};
	$loginmsg="Logged in";
}
else { $loginmsg="Login"; }

return 1;

#########################################################################

# Copyright (c) 1998 Peter D. Kovacs
# Unpublished work.
# Permission granted to use and modify this library so long as the
# copyright above is maintained, modifications are documented, and
# credit is given for any use of the library.

# Modified 26/1/2000 by John Phillips, to allow cookies with no domain
#
# Distributed with bcnu http://bcnu.sourceforge.net/
#


#$cookie{bcnulogin} = "loginid";
#&set_cookie(time()+28800, "/");	# cookie expires in 8 hours
#&get_cookie();

sub get_cookie {
  local($chip, $val);
  foreach (split(/; /, $ENV{'HTTP_COOKIE'})) {
    	# split cookie at each ; (cookie format is name=value; name=value; etc...)
    	# Convert plus to space (in case of encoding (not necessary, but recommended)
    s/\+/ /g;
    	# Split into key and value.  
    ($chip, $val) = split(/=/,$_,2); # splits on the first =.
    	# Convert %XX from hex numbers to alphanumeric
    $chip =~ s/%([A-Fa-f0-9]{2})/pack("c",hex($1))/ge;
    $val =~ s/%([A-Fa-f0-9]{2})/pack("c",hex($1))/ge;
    	# Associate key and value
    $cookie{$chip} .= "\1" if (defined($cookie{$chip})); # \1 is the multiple separator
    $cookie{$chip} .= $val;
  }
}
sub set_cookie {
  # $expires must be in unix time format, if defined.  If not defined it sets the expiration to December 31, 1999.
  # If you want no expiration date set, set $expires = -1 (this causes the cookie to be deleted when user closes
  # his/her browser).

  local($expires,$path,$domain,$sec) = @_;
    local(@days) = ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
    local(@months) = ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
  local($seconds,$min,$hour,$mday,$mon,$year,$wday) = gmtime($expires) if ($expires > 0); #get date info if expiration set.
  $seconds = "0" . $seconds if $seconds < 10; # formatting of date variables
  $min = "0" . $min if $min < 10; 
  $hour = "0" . $hour if $hour < 10; 
  local(@secure) = ("","secure"); # add security to the cookie if defined.  I'm not too sure how this works.
  if (! defined $expires) { $expires = " expires\=Fri, 31-Dec-1999 00:00:00 GMT;"; } # if expiration not set, expire at 12/31/1999
  elsif ($expires == -1) { $expires = "" } # if expiration set to -1, then eliminate expiration of cookie.
  else { 
    $year += 1900; 
    $expires = "expires\=$days[$wday], $mday-$months[$mon]-$year $hour:$min:$seconds GMT; "; #form expiration from value passed to function.
  }
  if (defined $domain) { $domain = "domain\=$domain"; } #set domain of cookie.  Default is current host.
  if (! defined $path) { $path = "/"; } #set default path = "/"
  if (! defined $secure) { $secure = "0"; }
  local($key);
  foreach $key (keys %cookie) {
    $cookie{$key} =~ s/ /+/g; #convert plus to space.
    print "Set-Cookie: $key\=$cookie{$key}; $expires path\=$path; $domain $secure[$sec]\n";
	#print cookie to browser,
	#this must be done *before* you print any content type headers.
  }
}

sub delete_cookie {
  # to delete a cookie, simply pass delete_cookie the name of the cookie to delete.
  # you may pass delete_cookie more than 1 name at a time.
  local($name,$path) = @_;
  if (! defined $path) { $path = "/"; } #set default path = "/"
  undef $cookie{$name}; #undefines cookie so if you call set_cookie, it doesn't reset the cookie.
  print "Set-Cookie: $name=deleted; expires=Thu, 01-Jan-1970 00:00:00 GMT; path\=$path;\n";
   			#this also must be done before you print any content type headers.
}

return 1;
