#!/usr/bin/perl

# $nocache = 1; #DEBUG
require($bin = qw( /usr/home/tukey/bin/common2.pl ));

$HOMEURL = "http://marmacs.org/2008";
$HOMEURL = "http://njacs.org/marm/2008";

# E.g. http://marmacs.org/2008/confex.pl?qs=qs&ses=http://acs.confex.com/acs/htsearch.cgi

$Q = $ENV{QUERY_STRING}; $Q = "MEETING.HTM" if($Q eq ""); $ENV{QUERY_STRING} = $Q;
$R  = $ENV{HTTP_REFERER};
$QQ = $Q;
$QQ =~ s/http:\/\/((www.)?marmacs.org(\/2008)?|(acs.)?confex.com)//gs;

######### DETECT & BLOCK ABUSE: ######### DETECT & BLOCK ABUSE: #########

if($Q =~ /^\/|[;]|\b(curl|wget|powned|matheus|bot\.pl)\b|regimesyndicate|(drexel.)?bkstore.com|default.asp|^[PS][0-9]+.HTM=/is || $QQ =~ /^[a-z0-9]+:\/\/|https?:\/\//is) {
    $remote = $ENV{REMOTE_ADDR};
    $host = `/usr/bin/host $remote 2>/dev/null || true` if($remote =~ /^\d+\./);
    chomp($host);
    LOGIT("BAD_QUERY\t$remote\t$host");
    &ContentType;

    print <<EOF;
<html>
<head>
<title>403 ERROR: Invalid query</title>
</head>
<body>
<h1>403 ERROR: Invalid query</h1>
</body>
</html>
EOF

    exit 0;
}

######### Another sanity check to detect & block abuse:

if($Q =~ m/^(https*:\/\/|confex\.pl)/is && $Q !~ m/confex\.com/is ) {
    $remote = $ENV{REMOTE_ADDR};
    $host = `/usr/bin/host $remote 2>/dev/null || true` if($remote =~ /^\d+\./);
    chomp($host);
    if(0 && $R eq "") {
	## Too strong!  Wouldn't allow folks to keep bookmarks to sessions!
	LOGIT("BAD_REFERER\t$remote\t$host");
	&ContentType;
	print ReadFile("403_forbidden.html");
	exit 0;
    }
    if($host =~ /googlebot/i) {
	## Does this prevent Google from indexing our program ???
	## No, since it's only external hrefs
	LOGIT("NO_PASS_THROUGH\t$&");
	print "Location: missing.html\n\n";  ## Which doesn't exist!
	exit 0;
#	&ContentType;
#	print ReadFile("404_page_not_found.html");
#	print "<pre>remote=($remote)\nhost = ($host)\n";
#	exit 0;
    }
    LOGIT("REDIRECT\t$Q");
    print "Location: $Q\n\n";
    exit 0;
    ## When I discovered Google trying to index the Drexel bookstore
    ## *through* this script, I realized I needed to add this bit 
    ## of code that essentially does a pass-through for any URL that
    ## isn't "ours".  Actually, this is just a fail-safe here;
    ## the core pass-through fix is below.
}

## Why can't I do this down below???




$ENV{PATH} = "$bin:$ENV{PATH}" unless($ENV{PATH} =~ m($bin));
($C) = ($0 =~ m|([^/]*)$|);
use subs ("DE"."BUG", qw(Usage Die Warn Print ERR));
ChDir("/usr/www/users/tukey/acs/marmacs.org/2008");
&RunMeAsWebOwner;

# &ContentType;
# print "<pre>\n";

$MARM      = "http://marmacs.org/2008";
$confex_pl = "$MARM/confex.pl?";
# $confex_pl = "$MARM/confex1.pl?"; #DEBUG
$BASE      = "http://acs.confex.com/acs/marm08/techprogram";
$EXTRA     = "confex-extra";

if(-f $EXTRA) {
   if(open(EXTRA,$EXTRA)) {
       while(<EXTRA>) {
	   chomp;
	   next if(/^ *#/);
	   next unless(/.\t./);
	   ($code,$url,$txt) = (split /\t/, $_);
	   $extra_url{$code} = $url;
	   $extra_txt{$code} = $txt;
       }
       close(EXTRA);
   }
}

## Uncomment next line to force use of local copies, bypassing call to confex
## (except for confex keyword searches)

$BYPASS = "/usr/www/users/tukey/acs/marmacs.org/2008/confex/techprogram"; # Also see: $CACHE

$closewin = "<input type=button value='Close Window' onclick='window.close()'></form>";
$closewin = "<table cellpadding=0 cellspacing=0 border=0 align=left><tr><td>$closewin</td></tr></table>";
$closewin = "";
$css      = qq(<link href="$MARM/confex.css" rel="stylesheet" type="text/css">);
$base     = qq(<base href="$BASE/" src="$BASE/">);
$QUERY    = $ENV{QUERY_STRING};
$ses      = $ARGV[0];
$ses      = $& if($QUERY =~ /^([PSD][0-9]+|ME[A-Z.]*|[A-Z][A-Z]*|http.*)/);
# DEBUG "1: ses=($ses)";

%FORM = &cgiVals;
# show_hash("FORM",\%FORM); #DEBUG
$force = $FORM{force};
$FORMx = $FORM{x};
&LOGIT;

$URL   = $ses;

#if($URL =~ m|^[a-z]+://|i && $URL !~ m|http://confex.com/|i) {
#    print "<xmp>Location: ($URL)</xmp>\n\n\n\n"; #DEBUG
#    exit 0;
#}

&ContentType;

$ses = $FORM{ses} if($FORM{ses});
$SES = $ses;
$SES = $1 if($ses =~ m|/techprogram/([A-Z][0-9]+)\.HTM|);
$SES =~ s/.*\///;
$SES =~ s/\.[A-Z]*$//;

# DEBUG "SES=($SES) ses=($ses) QUERY_STRING=($QUERY)";
#show_hash("FORM",\%FORM); #DEBUG
#show_hash("ENV",\%ENV); #DEBUG

if($FORM{formaction} =~ /qs=qs/) {
    $ses = $1 if ($FORM{formaction} =~ /ses=([^&]*)/);
    $FORM{formaction} = $ses;
    $URL = $ses;
    $Q = "";
    for(keys %FORM) {
	$Q .= '&' if($Q);
	$Q .= "$_=".URLencode($FORM{$_});
    }
    $URL .= "?$Q";
#    DEBUG "qs=qs: URL=($URL)<br>\nses=($ses)";
}

if($FORM{action} eq "search" && $ses !~ /^http/ && $FORM{formaction} =~ /htsearch.cgi/) {
  $URL = "http://acs.confex.com/acs/htsearch.cgi?";
} else {
  $URL  = "$BASE/$ses.HTM" unless($ses =~ /^http/);
  $URL  =~ s/(\.htm)\.htm$/$1/is;
}

#DEBUG "C: URL=($URL)";

if ($ses =~ /xxx/i) {
#    print "Content-Type: text/html\n\n"; #DEBUG

    print <<EOF;
<html>
<head>
<title>Details Not Available Yet</title>
$css
</head>
<body>
<h3>
<br>
The details for this session should be available soon
</h3>
$closewin
</body>
</html>
EOF

  exit 0;
}


if(0) {
    # Emergency bypass -- also disable &ContentType above.
    print "Location: $URL\n\n";
    exit 0; 
}

ChDir("/usr/www/users/tukey/acs/marmacs.org/2008");
# print "Content-Type: text/html\n\n";
$TMP   = "tmp";
$CACHE = "cache";	# Also see: $BYPASS
for $file (($TMP,$CACHE)) {
    if(  -f $file) { unlink $file; }
    if(! -d $file) { mkdir $file || ERR $!; }
}
# $max_mins = 60*24*365*10;  ## ten years, i.e. infinity!
$max_mins = 60; # an hour
$max_mins = 0 if($nocache);

# $max_mins = 0; ## Uncomment to force NO-CACHE nocache no-cache no cache

#DEBUG "max_mins = $max_mins";

for $file (<$CACHE/x-*>) {
    $age = mins_old($file);
    if(0 && $age > $max_mins) {   # No need to delete old material.  It's all frozen.
	unlink $file;
#	DEBUG "Removed $file which was $age minutes old";
    }
}

# $URL =~ s/acs/acx/g; #DEBUG
# $URL =~ s/htm\b/html/gi; #DEBUG

$md5 = md5($URL);
$out = "x-$md5.htm";
$cache = "$CACHE/$out";
unlink $cache if($force && -f $cache);
# DEBUG "force=$force";
if(-f $cache && !$force) {
#    $age = mins_old($cache); DEBUG "Using $cache which is $age minutes old";
    $_ = ReadFile($cache);
    print;
    if($URL =~ /MEETING/) { WriteFile($_,"MEETING.HTM"); }
#    DEBUG "ckpt 2: cache=$cache URL=$URL pwd=".`pwd`;
    exit;
}
# DEBUG "<pre>D: URL = ($URL) md5=($md5)</pre>"; # exit;

$URL =~ s/(['`;|]|%27|%7C|%3B|%60)//gs;   # FURTHER SANITIZE URL - FAILSAFE!

$cmd = "cd $TMP && wget -O $out -nv 2>&1 '$URL'";

# DEBUG "cmd=( $cmd )";

if(-d $BYPASS) {   # different from CACHE
    $file = $URL;
    $file =~ s|^.*[/?]||;
#    DEBUG "file=$file";
    if(-f "$BYPASS/$file") {
	$cmd = "cd $TMP && cp $BYPASS/$file $out";
#	DEBUG "bypassing confex";
    } else {
#	DEBUG "Not bypassing confex";
	}
}

#     DEBUG "cmd=($cmd)";

    $wgetrtn = $wgetrtn0 = `$cmd`;

    $wgetrtn =~ s/\b[^\n]* -> [^\n]*(\n|$)/$1/s;
    chomp($wgetrtn);
    $wgetrtn = Trim($wgetrtn);
#     print "<pre>DEBUG: wgetrtn=($wgetrtn)</pre>\n" if($wgetrtn);

$file0 = $URL;
$file0 =~ s/.*\///;
$file = "$TMP/$out";
# DEBUG "wget output file=($file)";
$_ = ReadFile($file);
unlink $file;

#### HACK ALERT!!!!
s/Joseph\s+D[.]?\s+Armstrong(\s+III)?/Shane Krska/gs;
s/\bGerad\b/Gerard/gs;

$zelleka = <<'EOF';
<tr[^\n]*sessionpapertime[^\n]*8:55 AM[^\n]*P42904.HTM[^\n]*437[^\n]*Zelleka[^\n]*tr>
EOF

$hunt = <<'EOF';
<tr valign="top"><td class="sessionpapertime" valign="top">8:40 AM</td><td class="sessionpaperlink" valign="top"><span a href="http://marmacs.org/2008/confex.pl?P42871.HTM">---</a></td><td valign="top"><!--InterestPaper42871--></td><td class="Title_SE" valign="top">
Special Address: Re-igniting our Commitment to Science and Technology: Education, Collaboration and Innovation!
<br><b></b><p class="author"><b>Katie Hunt, ACS President</b></p></td></tr>
EOF

s/$zelleka/$hunt\n$&/s;

s/<\/head/\n$base\n$css\n$&/is;

######### add EXTRA to abstract here

if($extra_url{$SES} || $extra_txt{$SES}) {
    $s  = " ";
    $s .= "<nobr>[ <i><b><a href=\"$extra_url{$SES}\">more...</a></b></i> ]</nobr> " if($extra_url{$SES});
    $s .= $extra_txt{$SES}." ";
    if($SES =~ /^S/) {
	s|(<!--#abstract#-->)(.*?)(<!--#/abstract#-->)|$1$2$s$3|s;
    } else {
	my($old) = $_;
        s|(<div class="techprogramabstracttext">)(.*?)(<P>)|$1$2$s$3|is;
#	DEBUG "SES=($SES).  Changed? ".( ($_ eq $old)? "NO" : "YES" );
    }
}

s|<!--#([^#/]+)#-->|<p class="$1">|gs;
s|<!--#/([^#]+)#-->|</p>|gs;
s/\n[\t ]*/\n/gs;
# s/\n<tr.*?>Presider:.*?<\/tr>/$&\n<tr><td height=12><\/td><\/tr>/s;

s/(<tr valign="top"><td class=.sessionpapertime)/<tr><td height=12><\/td><\/tr>\n$&/gs;

s/\nBack to  /<form><hr>$&/s;


#$ttl = q(
#<TABLE cellpadding=10 cellspacing=0 border=0 width="100%" bgcolor="#006699"><TR><TD>
#<TABLE cellpadding=4 cellspacing=0 border=0 width="100%" bgcolor=white background="http://marmacs.org/2008/graf/greypin-white.jpg">
#<TR><TD rowspan=2 width=50 valign=center><img src="http://marmacs.org/2008/graf/forge87x88.jpg"
#height=80 title='"Forging new connections in Chemistry"' ></TD>
#<TD width="99%" valign=top>
#<p class=marmtitle><span style="font-size:1.05em">MARM 2008</span> - May 17-21, 2008</p>
#</TD><TD rowspan=2 width=50 valign=top><img src="http://marmacs.org/2008/graf/acsdia2.gif"
#height=80 title='American Chemical Society'></TD>
#</TR><TR><TD colspan=3>
#<p class=marmnavbar>
#[ <nobr><a href="JavaScript:window.history.back()" >back</a></nobr>
#| <nobr><a href="http://marmacs.org/2008" >home</a></nobr>
#| <nobr><a href="MEETING.HTM" >program by day</a></nobr>
#| <nobr><a href="AAG.HTM" >at-a-glance</a></nobr>
#| <nobr><a href="MARM.HTM" >organizers</a></nobr>
#]
#</p>
#</TD></TR></TABLE>
#</TD></TR></TABLE>
#);

$ttl = qq(
<p class=pgtitle>Technical Program</p>
<center><img src="$HOMEURL/graf/rule03.gif" height=5 width=500></center>
<p class=marmnavbar style="margin-top:6pt;margin-bottom:0pt;" >
[ <nobr><a href="JavaScript:window.history.back()" >back</a></nobr>
| <nobr><a href="MEETING.HTM" >program by day</a></nobr>
| <nobr><a href="$HOMEURL/BYAREA.HTM" >program by area</a></nobr>
| <nobr><a href="AAG.HTM" >at-a-glance</a></nobr>
<!--| <nobr><a href="MARM.HTM" >organizers</a></nobr>-->
]
</p>
);

# q(<center><IMG SRC="http://marmacs.org/2008/graf/rule03.gif" width="100%" height=4 vspace=10></center>);
unless(s/<body.*?>/$&$ttl/s) {
    s/<\/head.*?>/$&$ttl/s;
}

s/<p class="date">/$&<b><i>Session:<\/i><\/b>\&nbsp;\&nbsp; /;
s/(<p class="abstract">)( *:)?/$1<b><i>Abstract:<\/i><\/b>\&nbsp;\&nbsp; /;
s/(Sponsor|Organizer|Presider):/<i>$&<\/i>/gs;
s/Workshop Leader:/<nobr><i>$&<\/i>\&nbsp;<\/nobr>/gs;
s/<td[^<>]*/$& valign=top/gis;

if($URL =~ /AAG/) {
#    s/Program-at-a-Glance/$& (preliminary)/gs;
    s/<table[^<>]*>/x1($&)/gse;
    sub x1 {
	local($_) = @_;
	unless(s/(border=["']?)0/${1}1/s) { s/\s/$&border=1 /s };
	my($cp) = 4;
	unless(s/(cellpadding=["']?)[0-9]*/${1}$cp/s) { s/\s/$&cellpadding=$cp /s };
	return $_;
    }
    $ntr = 0;
    s/<tr.*?<\/tr/tr1($&)/gse;
    sub tr1 {
	local($_) = @_;
	if(++$ntr <= 2) {
	    s/tr/tr bgcolor=gainsboro/s;
	} else {
	    s/<\/td>/<hr>$&/gs
	}
	return $_;
    }
    s/<h2>/<h2 style="margin-top:8pt;margin-bottom:2pt;text-align:center">/s;
    s/(<div[^<>]*><h3>)(.*?)(<\/h3>\s*<\/div>\n*)//s;
    ($hold1,$hold2,$hold3) = ($1,$2,$3);
    $hold2 =~ s/h3/$& style="text-align:left;margin-top:6pt"/s;
    s/<\/table[^<>]*>/$&<br><span class=h3>\n$hold2/s;
    s/<\/form>/<\/h3>$&/s;
    s/<table/<table bgcolor="#FFFFFF"/s;
    s/<body/$& bgcolor="whitesmoke"/s;
}
if($URL =~ /MARM.HTM/) {
    %sli = &read_SLI;
    s/(<b>Sessions:<\/b>)([^<>]*)(<)/x2($1,$2,$3)/gise;
    sub x2 {
	local($a,$b,$c) = @_;
	$b =~ s/; */;<\/p><p class=session>/gs;
	return "$a<br><p class=session>$b</p>$c";
    }
    s/(<p class=session> *)([^ ][^<>]*[^ ]) *(<\/p>)/x4($1,$2,$3)/gise;
    sub x4 {
	local($a,$b,$c) = @_;
	local($key) = lc(Trim($b));
	$key =~ s/\s*;$//;
	return "$a$b$c" unless($sli{$key});
	$b = qq(<a href="$sli{$key}" class=sessionlistitem2>$b</a>);
	return "$a$b$c";
    }
    s/<table/<table bgcolor="#FFFFFF"/s;
    s/<body/$& bgcolor="whitesmoke"/s;
    s/<a[^<>]*>\s*View Program Details and Presentation Times<\/a>/<br>/s;
    s|</p><br>&nbsp;</p></td>|</p></td>|gs;
}
if($URL =~ /MEETING.HTM|D37.HTM/) {
    s/(\n *<\/table> *\n *<\/form>)(-->)( *\n[\n ]*<p> *\n *<\/table>)/$1$3$2/si;
    s/(<span class="mainhead">)(.*?)(<\/span>)/x3($1,$2,$3)/gise;
    s/<body/$& bgcolor="whitesmoke"/s;
    sub x3 {
	local($a,$b,$c) = @_;
	$a =~ s/span/p/;
	$c =~ s/span/p/;
	return "$a$b$c";
    }
    if($URL =~ /MEETING/) {
	s/<table/<table border=1 cellspacing=0 bgcolor=white/s; #Search box
#	s/Program Listing/$& by Day (preliminary)/s;
	s/Program Listing/$& by Day/s;
    }
    if($URL =~ /D37/) {
	s/(<table.*?\/table>\n)(<p>\n)/$2$1/s;
#	s/<h3>GENERAL PROGRAM/<h3 style="margin-bottom:0pt">GENERAL PROGRAM BY DAY (preliminary)/s;
	s/<h3>GENERAL PROGRAM/<h3 style="margin-bottom:0pt">GENERAL PROGRAM BY DAY/s;
    }
}
if($URL =~ /action=search/) {
    s/<\/head/<style> li, .li { margin-top:6pt; } <\/style>\n$&/s;
}

# s/(<a href=")([A-Z][0-9]+\.HTM)(">)/${1}$confex_pl$2$3/gs;
# s/(<a[^<>]*href=["'])([^J].*?)(['"]>)/${1}$confex_pl$2$3/gis;

s/(<a[^<>]*href=["'])([^J].*?)(['"]\s*>)/fix_href($1,$2,$3)/gise;
s/(\.open\(')([^']*)()/fix_href($1,$2,$3)/gise;
sub fix_href {
    local($a,$b,$c) = @_;
    return "$a$b$c" if($b =~ /^J|\.jpg|\.gif|^(http:\/\/)?(www\.)?marmacs.org(\/2008)?/si);
    ## Next line causes a pass-through for any URL that isn't "ours":
    return "$a$b$c" if($b =~ m|^https?://| && $b !~ m|confex.com|  );
    return "$a$confex_pl$b$c";
}


s/<table.*?>/fix_table($&)/gse if($URL =~ /MARM.HTM/);
if($URL =~ /[A-Z]\d\d\d\d/) {
    s/<\/table/<tr><td width="5%" height=1><\/td><td width="95%" colspan=3><\/td><\/tr>$&/gs;
}
s/<td[^<>]*="programlistitem".*?<\/td>/fix_pli($&)/gise;
s/<td[^<>]*="sessionlistitem".*?<\/td>/fix_sli($&)/gise;
s/<span( class="normaltime">.*?<\/)span>/<p $&p>/s;
s/<font( class=.*?)<\/font/<span$1<\/span/gis;
s/<div[^\n]*The Middle Atlantic Regional Meeting[^\n]*<\/div> *\n*//gs;
# s/([>\n])Back to([^\n]*The Middle Atlantic Regional Meeting)/${1}Go to$2/gs;
s/Back to([^\n]*<a[^<>]*>The Middle Atlantic Regional Meeting).*?<\/a>//gs;
$sp = "&nbsp;"x8;
# s/(\s|<\/*(p|br|div|span|font)[^<>]*>)*<\/body>/\n$sp$closewin\n$&/is;
s/<a[^<>]*>View The Middle Atlantic Regional Meeting.*?<\/a>//gs;
s|(action=')(http://acs.confex.com/acs/htsearch.cgi')|$1${confex_pl}qs=qs\&ses=$2|gs;
s|(<form action=")(/acs/htsearch.cgi")|$1${confex_pl}http://acs.confex.com$2|gs;
s|(value=")(http://acs.confex.com/acs/htsearch.cgi")|$1${confex_pl}qs=qs\&ses=$2|gs;
s/^\s*//s;
s/([^\s])\s*(<html)/$1\n$2/s;

&doctor_remaining_URLS;

if(1) {
s/<body bgcolor="whitesmoke">/<body bgcolor="#006699">/gs;
$stuff = $_;
#$_ = ReadFile("template.html");
$_ = ReadFile("program.html");
s/xxxPgTitle/Technical Program/s;
s/<body>/<body style="background-color:#006699" bgcolor="#006699">/gs;
s/<\/*(html)[^<>]*>//gis;
s/(<!-- Main Content of Page ==* bgn { -->).*(<!-- Main Content of Page ==* end \} -->)/$1\n$stuff$2/s;
# WriteFile($_,"DEBUG");
}
if($URL =~ /MEETING/) {
#    DEBUG "ckpt 3";
    s/Program Listing by Day/<span style="color:#336699;">$&<\/span><br>/si;
}

s/(<p class="sponsor")(>)(<[bi]>)*([^<>]*)(<\[ib]>)*/$1 style="margin-top:0pt;font-size:8pt;font-weight:bold;font-style:normal;"$2$4/gs;
# DEBUG "======================= x=$FORMx";
if($FORMx eq "x") { 
#    DEBUG "======================= Saw x=x";
    s/<table[^<>]*>/add_borders($&)/gise;
    sub add_borders {
	local($_)=@_;
	s/\<border=0/border=1/is || s/>/ border=1>/s;
	return $_;
    }
}

print;
if($URL =~ /MEETING/) { WriteFile($_,"MEETING.HTM"); }
if(/[^\s]/s && $wgetout eq "" && !/<title>.*?ERROR.*?<\/title>|404 NOT FOUND/is) {
    ## Don't cache unless it looks like good output. Need more heuristics?
    $file = "$CACHE/$out";
    if(-f $file) {
	$age  = mins_old($file);
#	DEBUG "File $file exists and is $age minutes old.";
    } else {
	$extra = "<!-- URL: $URL -->\n<!-- wget: $wgetrtn0 -->\n";
	unless(s/<html/$extra$&/si) { s/^/$extra/s; }
	WriteFile($_,"$CACHE/$out");
#	DEBUG "Wrote $CACHE/$out";
	&save_SLI;

    }
}
# &save_SLI;

$br = "<br>"x10;
# print "\n<br>\n<hr>\n$br<xmp>$_</xmp>\n"; #DEBUG zzz

1;

########################### subs ###########################

sub doctor_remaining_URLS {
  return;
    ## Was interfering with <form...> for search.
    ## Better approach:  s/(^|>)([^<>]+)(<|$)/{ $1.add_href($2,$3) }/gise;
    ## But test it!
    %hrefs = ();
    $hrefs = 100;
    if(s/<a .*?<\/a[^<>]*>/hide_href($&)/gise) {
	s/\b(https?:\/\/|www\.)+([^\s<>]+)($|[^a-z0-9_+:\-\/])/add_href($1.$2,$3)/gise;
	s/`HREF([0-9]+)`/{ $hrefs{$1} }/gse;
    }
}
sub hide_href {
    local($_) = @_;
    $hrefs{++$hrefs} = $_;
    return "`HREF$hrefs`";
}
sub add_href {
    local($_,$sfx) = @_;
    return $_.$sfx unless(/\.[a-z]{2,4}\b/);
    return $_.$sfx if(/"/);
    my $show = $_;
    $show =~ s/^http:\/\///i;
    $show = "www.$show" unless($show =~ /^(http|www)\b/i);
    $_    = "http://$_" unless(/^https?:/i);
    my $target = "";
    $target = " target=_blank" unless(/\bmarmacs\.org(\/2008)?\b/);
    return "<a href=\"$_\"$target>$show </a>$sfx";
}

sub LOGIT {
    local($WHAT) = @_;
    $_now = "".localtime time;
    $_formaction = $FORM{formaction};
    $_action = $FORM{action};
    $_ses   = $FORM{ses};
    $_blank = $FORM{''};
    $_remote     = $ENV{REMOTE_ADDR};
    $_referer    = $ENV{HTTP_REFERER};
    $_uri        = $ENV{REQUEST_URI};
    $_formaction = "-" if($_formaction eq "");
    $_action     = "-" if($_action     eq "");
    $_ses        = "-" if($_ses        eq "");
    $_remote     = "-" if($_remote     eq "");
    $_referer    = "-" if($_referer    eq "");
    $_blank      = "-" if($_blank      eq "");
    $_uri        = "-" if($_uri        eq "");
    $_action = $WHAT if($WHAT);
    $_LOG = "/usr/www/users/tukey/acs/marmacs.org/2008/private/confex.log";
#    WriteFile("$_now\t$_remote\t$_referer\t$_blank\t$_ses\t$_formaction\t$_action\t$_uri\n",$_LOG,$_APPEND=1);
}

sub save_SLI {
    return unless(%SLI);
    local($s,$_) = ();
    for(sort keys %SLI) {
	$s .= "$SLI{$_}\t$_\n";
    }
    WriteFile($s,"SLI.txt");
#    DEBUG "Wrote SLI.txt";
    %SLI = ();
    1;
}
sub read_SLI {
    local($_,%sli,$val,$key) = ();
    return () unless(-s "SLI.txt");
    foreach(split /\n/, ReadFile("SLI.txt")) {
	($val,$key) = (split /\t/, $_);
	$key = lc(Trim($key));
	$sli{$key} = $val;
    }
#    DEBUG "Read SLI.txt";
    return %sli;
}

sub mins_old {
    local($file) = @_;
    return "" unless(-f $file);
    return int( (time - FileMtime($file))/60. + .5);
}

sub fix_pli {
    local($_) = @_;
    s/<br>\&nbsp;//s;
    s/<\/?b>//gs;
    s/<a/<p class=programlistitem>$&/s;
    s/<\/a.*?>/$&<\/p>/s;
    return $_;
}
sub fix_sli {
    local($_) = @_;
    s/<br>\&nbsp;//s;
    s/<\/?b>//gs;
    s/<a/<p class=sessionlistitem>$& class=sessionlistitem2/s;
    s/<\/a.*?>/$&<\/p>/s;
    s/href="([^"]*)"[^<>]*>([^<>]*)<\/a/{ $SLI{$2} = $1; $&; }/ise;
    return $_;
}
sub fix_table {
    local($_) = @_;
    s/(cellspacing|border)=["']?\d?['"]?//gis;
    s/( |>)/ border=1 cellpadding=4 cellspacing=0$&/s;
    return $_;
}
sub ERR {
    local($msg) = @_;
    print "<pre>ERROR: $msg</pre></body></html>\n";
}
1;
