#!/usr/local/bin/perl
require "/home/todd/toddweb.env";
require "$PAGEHOMEDIR/weblib.lib";
#use DB_File;
&defuser($PAGEADMIN,@PAGEADMINADDR);
($pageadmin,$domain)=split(/@/,$PAGEADMIN);
if($ENV{PATH_INFO}){
	@_=split(/\//,$ENV{PATH_INFO});
	foreach $var (@_){
		($name,$value)=split(/=/,$var);
		$value="" if !defined($value);
		$PASS{$name}=$value;
	}
}
if(defined($PASS{'long'})){undef $PASS{'articles'}}
if($PASS{'articles'} eq "checked"){
	$checked="checked";
	$PASS{'articles'}="";
}
else {
	$checked="";
}
if(($PASS=join(":",$PASS{'ia'},$PASS{'ta'},$PASS{'tv'},$PASS{'v1'}))
		=~ /^\d+:\d+:\d+:\d+$/){
	($accessnum,$totalaccess,$totalvisitors,$v1)=split(/:/,$PASS);
}
else{
	($access,$accessnum,$totalvisitors,$totalaccess)=&trpagestats("$PAGEHOMEDIR/db/trconn");
	foreach $vote (keys %VOTE){
		$v{$vote}=1 if $access =~ /:vote$vote:|:vote$vote$/;
	}
}
$orig=1;
do "$PAGEHOMEDIR/local.inc";
&htmlopen("$PAGENAME|");
$typepub="section";
if(!defined($PASS{"articles"})){
$typepub="issue";
&webmail($PAGEADMIN,$PAGENAME,$access,$totalvisitors,$totalaccess)
	if(!$HTTPUSER{$pageadmin});
print <<EOF;
$localinc
<p>

Technical information about this page may be found 
<a href=/$HTMLPATH/techinfo.html>here</a>.
Other sites gracious enough to point to this page may be found
<a href=/$CGIPATH/known>here</a>.<br>
So far, $totalvisitors different visitors from a total of $totalaccess visits
have been here.  This is visit #$accessnum from your host.
EOF
$needvote="";
foreach $vote (sort keys %VOTE){
	($votename,$votetitle)=split(/:/,$VOTE{$vote},2);
	if($v{$vote} != 1){
		$needvote.="<li><A href=#$votename.art>$votetitle</a>\n";
	}
}
print <<EOF if $needvote;
You haven't voted yet in these ongoing polls:
<ul>
$needvote
</ul>
Don't forget!
EOF
}
$artpath="$PAGEHOMEDIR/articles";
open(INDEX,"$artpath/index");
print <<EOF;
<hr><a name=index href=#><h2>Index to this $typepub</h2></a>
EOF
if($ENV{CONTENT_LENGTH} != 0){
	&ReadParse(*in);
	$PASS{'articles'}=join(',',keys(%in));
}
print "<img src=\"/$HTMLPATH/pictures/nwo-finger.gif\"><br>\n";
print "<ul>\n";
$formlist=(defined $PASS{'articles'} &&
		($PASS{'articles'} eq ""||$PASS{'articles'} eq "checked"));
($sec,$min,$hour,$day,$month,$year)=localtime(time);
$checkexpire=sprintf("%4d%02d%02d%02d%02d%02d",$year+1900,$month+1,$day,$hour,$min,$sec);
($sec,$min,$hour,$day,$month,$year)=localtime(time-86400*14);
$checknew=sprintf("%4d%02d%02d%02d%02d%02d",$year+1900,$month+1,$day,$hour,$min,$sec);
($sec,$min,$hour,$day,$month,$year)=localtime(time-86400);
$checkhot=sprintf("%4d%02d%02d%02d%02d%02d",$year+1900,$month+1,$day,$hour,$min,$sec);
while(<INDEX>){
	chop;
	if(tr/:/:/ >= 3){
		($artname,$interest,$expire,$header)=split(/:/,$_,4);
		$expire="99999999999999" if $expire eq "";
	}
	else{
		$expire="99999999999999";
		($artname,$header)=split(/:/,$_,2);
	}
	next if $expire<$checkexpire;
	next if $PASS{'articles'} && ",$PASS{'articles'}," !~ /,$artname,/;
	@stat=stat("$artpath/$artname");
	($sec,$min,$hour,$day,$mon,$year)=localtime($stat[9]);
	$artdate=sprintf("%4d%02d%02d%02d%02d%02d",$year+1900,$mon+1,$day,$hour,$min,$sec);
	$new="";
	$new="<img src=/$HTMLPATH/pictures/new.gif alt=\"New! \">" if $artdate>$checknew;
	$new="<img src=/$HTMLPATH/pictures/hot.gif alt=\"Hot! \">" if $artdate>$checkhot;
	$lm=sprintf("%s %d, %s",(Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec)[$mon],$day,$year+1900);
	$URL=$PAGEURL2;
	$URL.="/articles=$PASS{'articles'}" if $PASS{'articles'} ne "";
	$URL.="#$artname";
	print "<li>$new<a href=#$artname>$header</a>  ($lm)\n";
	push(@articles,$artname);
}
print "</ul>\n";
print "<hr>\n";
foreach $article (@articles){
	&getart4("$artpath/$article","index");
	print "<hr>\n";
}
print <<EOF;
<a href=$PAGEURL>$PAGENAME</a> is maintained by
<a href=$PAGEADMINHOMEPAGE>$PAGEADMINNAME</a>
<br>
<address><a href=mailto:$PAGEADMIN>$PAGEADMIN</a></address>
<br>
EOF
undef @_;
&htmlclose;
sub trpagestats{
	local($dbfile)=@_;
	local(@time,$today);
	dbmopen(%OLDPAGESTATS,"$PAGEHOMEDIR/db/trdata.db",0644);
	dbmopen(%PAGESTATS,$dbfile.".db",0644);
	$key="$ENV{REMOTE_ADDR}:$ENV{REMOTE_HOST}";
	$access=$PAGESTATS{$key};
	if($access){
		@accessnum=split(/:/,$access);
	}
	else{
		$access=$OLDPAGESTATS{$ENV{REMOTE_ADDR}};
		@accessnum=split(/:/,$access);
		$accessnum[0]=0 unless $access;
		splice(@accessnum,1,0,0); # insert placeholder for time field
	}
	$accessnum[0]++ unless $ENV{PATH_INFO} eq "/nocount";
	$accessnum[1]=time;
	$accessnum=$accessnum[0];
	$access=$PAGESTATS{$key}=join(":",@accessnum);
	@time=localtime($accessnum[1]);
	$today=sprintf("DATECOUNT%%%%%4d%02d%2d",$time[5]+1900,$time[4]+1,$time[3]);
	$PAGESTATS{$today}++;
	$PAGESTATS{TOTALACCESS}+=1 unless $ENV{PATH_INFO} eq "/nocount";
	if($accessnum[0]==1){$PAGESTATS{TOTALVISITORS}+=1};
	$totalvisitors=$PAGESTATS{TOTALVISITORS};
	$totalaccess=$PAGESTATS{TOTALACCESS};
	dbmclose(%PAGESTATS);
	dbmclose(%OLDPAGESTATS);
	return($access,$accessnum,$totalvisitors,$totalaccess);
}
sub getart4{
	local($artname,$indexname)=@_;
	local(@art)=split(/\//,$artname);
	local($artdate,$new);
	$indexname=" href=\"#$indexname\"" if $indexname;
	open(ART,$_[0]);
	chop($_=<ART>);
	s/<\/?h3>//gi;
	print "<h3><a$indexname name=\"$art[$#art]\">$_</a></h3>\n";
	while(<ART>){
		if(/<new (\d+)>/){
			$artdate=$1;
			$artdate.="000000";
			$artdate=substr($artdate,0,14);
			$new="";
			$new="<img src=/$HTMLPATH/pictures/new.gif alt=\"New! \">"
				if $artdate>$checknew;
			$new="<img src=/$HTMLPATH/pictures/hot.gif alt=\"Hot! \">"
				if $artdate>$checkhot;
			s/<new \d+>/$new/;
		}
		s/<\/new>//;
		s/\$WWWHOST/$WWWHOST/g;
		s/\$CGIPATH/$CGIPATH/g;
		s/\$HTMLPATH/$HTMLPATH/g;
		s/\$PAGEHOMEDIR/$PAGEHOMEDIR/g;
		print;
	}
	close(ART);
}
