head	1.4;
access;
symbols;
locks; strict;
comment	@# @;


1.4
date	2001.02.20.20.56.37;	author beecher;	state Exp;
branches;
next	1.3;

1.3
date	2000.01.26.01.27.52;	author beecher;	state Exp;
branches;
next	1.2;

1.2
date	2000.01.26.00.45.57;	author beecher;	state Exp;
branches;
next	1.1;

1.1
date	99.03.30.16.23.01;	author beecher;	state Exp;
branches;
next	;


desc
@get full or partial web report for personal directory
@


1.4
log
@don't call getconfig because we don't need it
@
text
@#!/usr/bin/perl
# find the Web usage information for a specific user in the weekly
# report, and display it

$ICON_REG='http://www.columbia.edu/httpd/getstats/getstats.gif';
$ICON_SEC='https://www1.columbia.edu/icons/getstats.gif';

$SRVPORT=$ENV{'SERVER_PORT'};		# get server's port number
if ($SRVPORT eq '443') {		# secure server
  $ICON_OUT=$ICON_SEC;
  $basedir='/etc/httpd/data/sec/acis/rad/web-usage/homepag';
  $baseurl='https://www1.columbia.edu/sec/acis/rad/web-usage/homepag';
} else {				# regular server
  $ICON_OUT=$ICON_REG;
  $basedir='/etc/httpd/data/httpd/homepag';
  $baseurl='http://www.columbia.edu/httpd/homepag';
}

					# set defaults
$reptype='dtr';
$server='www';				# default server
$repdate='';
$webdir='';
$detail=2;
$honly='';
$sortkey='req';				# alp or req
$debug='';

if ($ENV{'REQUEST_METHOD'} eq 'GET') {	# get the input using method=GET
  $buffer = $ENV{'QUERY_STRING'};
} else {				# get the input using method=POST
  read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
}
@@pairs = split(/&/, $buffer);		# split the name-value pairs

foreach $pair (@@pairs)			# put values into scalar variables
{
  ($name, $value) = split(/=/, $pair);

  # Un-Webify plus signs, undo the %-encoding
  $value =~ tr/+/ /;
  $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

  if ($name eq 'reptype') {		# report type: dtr, dom, red
    $reptype = $value;
  } elsif ($name eq 'server') {		# web server short name
    $server = $value;
  } elsif ($name eq 'repdate') {	# 8 digit report date
    $repdate = $value;
  } elsif ($name eq 'webdir') {		# desired directory
    $webdir = $value;
  } elsif ($name eq 'detail') {		# levels of detail
    $detail = $value;
  } elsif ($name eq 'honly') {		# count HTML files only
    $honly = $value;
  } elsif ($name eq 'sortkey') {	# alp or req
    $sortkey = $value;
  } elsif ($name eq 'debug') {
    $debug = $value;
  } else {
    &errmsg2("not a supported option: $name=$value");
  }
}

&errmsg2("repdate must be specified") unless $repdate;
&errmsg2("repdate must be numeric: $repdate")
				if $repdate =~ /\D/;
&errmsg2("repdate must be eight digits: $repdate")
				unless length($repdate)==8;

$repdir=$basedir;
$webdir =~ s|/$||;			# remove trailing slash
$webdirq='';
if ($webdir) {
  $webdir = '~' . $webdir unless $webdir =~ m|~|;  # add tilde
  $webdirq=$webdir;
  $webdir = '/' . $webdir unless $webdir =~ m|^/|; # add leading slash
  $webdir = "/Personal web pages$webdir"; # personal dirs are down one level
  @@wdirs = split('/', $webdir);		# count the slashes in webdir
  $detail = $#wdirs if $detail < $#wdirs; # increase detail if necessary
  undef(@@wdirs);
}

&errmsg2("$reptype reports not available for personal web pages")
     unless $reptype eq 'dtr';
&errmsg2("no personal web pages on server $server")
     unless $server eq 'www';

$honly='';
$sortkey='req';
$detail=2 if $detail<2;
$detail=4 if $detail>4;

unless ($webdir) {			# user wants FULL REPORT
		# URL of the form /httpd/homepag/19970816.dtrreq3.html
  print "Location: $baseurl/$repdate.$reptype$sortkey$detail.html\n\n";
  exit;
}

print "Content-type: text/html\n\n";	# user wants partial dir tree report

# construct file name of the form "19950114.dtrreq3.html"
$repfilnam = "$repdir/$repdate.$reptype$sortkey$detail.html";
if (!open(REP, "<$repfilnam")) {
  &errmsg("Can't open report file $repfilnam: $!");
}

$visible=1;
$dtr_header='';
while (<REP>) {
  # skip the summary and the top 10 list
  # save the Directory Tree Report header in memory

  # must display icon from server this CGI script is running on
  #  to prevent Netscape warnings about mixing secure and unsecure data
  s|$ICON_REG|$ICON_OUT|o;
  s|$ICON_SEC|$ICON_OUT|o;
  if (/\<!-- end summary --\>/) {
    $visible=1;
    next;
  }
  $visible='' if /\<!-- begin summary --\>/;
  s/Directory Tree Report/$& for $webdirq/;	# append dir name
  $dtr_header .= $_ if $visible;

  if ($visible && /------------------/) { # line after "Dir/file Req"
    $doffset = length($`);		# find starting column for URLs
    last;
  }
}

&errmsg("$webdir not found in $detail level report for $srv server")
		unless &extract_section($webdir, $dtr_header);

$visible='';
while (<REP>) {
    $visible=1 if /Created by /;
    print if $visible;
}
close REP;
exit;

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

sub extract_section {
    # print the relevant portion of the web usage report
    # returns 1 on success, '' otherwise

    local($webdir, $dheader) = @@_;
    local($visible, $dirlevel, $offset, $tlev, $m, $j);
    local(@@wdirs) = split('/', $webdir);

    # directory components are stored in the @@wdirs array
    # e.g. /~abc34/gifs is translated to
    #    $wdir[1] = 'Personal web pages'
    #    $wdir[2] = '~abc34/'
    #    $wdir[3] = 'gifs/'


    $dirlevel = $#wdirs;		# levels specified by the user
    foreach $wdir (@@wdirs) {
	$wdir .= '/' unless $wdir eq 'Personal web pages';
    }

repline:
    while (<REP>) {
	last repline if m|^\</pre\>|;
	chop($rline=$_);
	# set $tlev to the level of the current line (0 to 6)
	# set $dirname[$tlev] to the directory name
	for ($tlev = 0; $tlev <= 6; $tlev++) {
	    local($rlev) = $tlev;
	    local($roffset) = 0;	# level 0, indent 0
	    if ($rlev) {		# level 1, indent 1
		$roffset++;
		$rlev--;
	    }
	    $roffset += $rlev*3;	# indent 3 for each level past 1
	    $offset = $doffset+$roffset;
	    if (substr($rline, $offset, 1) ne ' ') {
		$dirname[$tlev] = substr($rline, $offset);
		last;
	    }
	}
	if ($visible) {
	    if (($tlev < $dirlevel) ||
		(($tlev == $dirlevel) && ($wdirs[$tlev] ne $dirname[$tlev]))) {
		last repline;		# we are done
	    }
	    print "$rline\n";
	} else {
	    next repline if $tlev != $dirlevel; # keep looking
	    $m = 1;
	    for ($j = 1; $j <= $dirlevel; $j++) {
		if ($wdirs[$j] ne $dirname[$j]) {
		    $m = 0;
		    last;
		}
	    }
	    if ($m == 1) {		# found beginning of section
		$visible = 1;
		print $dheader;
		print "$rline\n";
	    }
	}
    }
    return $visible;
} # extract_section



sub errmsg {				# print an error message
    local($mess) = @@_;
    print "<head><title>Showstats Error</title></head>
<body><h1>Showstats Error</h1>
$mess</body>";
    exit;
}



sub errmsg2 {				# header + error message
    local($mess) = @@_;
    print "Content-type: text/html\n
<head><title>Showstats Error</title></head>
<body><h1>Showstats Error</h1>
$mess</body>";
    exit;
}
@


1.3
log
@move reports back to regular server
@
text
@a4 4
unshift(@@INC, '/opt/ACISweb/lib');
require 'webconfig.pm';
&webconfig'getconfig('', \@@sname, \%lname, \%stype, \%droot, \%host1, \%host2);

@


1.2
log
@reports are on the secure server
@
text
@d19 2
a20 2
  $basedir='/etc/httpd/data/httpd/staff';
  $baseurl='http://www.columbia.edu/httpd/reports';
d99 1
a99 1
			# URL of the form /httpd/staff/19970816.dtrreq3.html
@


1.1
log
@Initial revision
@
text
@d1 38
a38 17
#!/usr/local/bin/perl -w
#
# generic-stats.pl
# finds the Web usage stats for a specific user in the weekly
# report, and displays it

$REPDIR='/k/cnet/httpd/reports/total/';
$WEEKLIST=$REPDIR.'weeklist';

print "Content-type: text/html\n\n" ;

# get the input using method = post
$buffer='';
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

# split the name-value pairs
@@pairs = split(/&/, $buffer);
d40 1
a40 2
# put the expected values into scalar variables
foreach $pair (@@pairs)
d42 1
a42 1
    ($name, $value) = split(/=/, $pair);
d44 66
a109 13
    # Un-Webify plus signs, undo the %-encoding
    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

    if ($name eq 'weeknum') {		# 0=last week, 1=two weeks ago
	$weeknum = $value;
    } elsif ($name eq 'detail') {	# levels of detail
	$detail = $value;
    } elsif ($name eq 'webdir') {	# directory to get stats for
	$webdir = $value;
    } else {
	print "unexpected variable: $name=$value<br>\n";
    }
d112 5
a116 22
&err_badform('detail must be at least 1')           if $detail < 1;
&err_badform('detail must not be greater than 3')   if $detail > 3;
&err_badform('weeknum must not be negative')        if $weeknum < 0;
&err_badform('weeknum must not be greater than 30') if $weeknum > 30;
&err_badform('directory must be specified')          unless $webdir;

$repdate = &get_repdate($weeknum);
&err_badform("weeknum too large") unless $repdate;

# construct file name of the form "950114.dtrreq3.html"
$repfilnam = $REPDIR . $repdate . '.dtrreq' . $detail . '.html';

if (!open(REP, "<$repfilnam")) {die "Can't open report because $!"}

print <<EOM;
<head>
<title>Web Usage Report for $webdir</title>
</head>
<body><h1><img alt=""
src="http://www.columbia.edu/httpd/getstats/getstats.gif">
Web Usage Report for $webdir</h1>
EOM
d118 16
a133 4
while (<REP>) {
    print $_ if /This report covers/;
    print $_ if /Reporting on Server/;
    last if /Running time/;
d136 4
a139 1
$summary='';
d141 2
a142 3
    # save the summary report in memory
    $summary .= $_;
    last if m|^\</pre\>|;
d144 2
d147 1
a147 6
$visible='';
$dtr_header='';
while (<REP>) {
    # skip the top 14 list
    # save the Directory Tree Report header in memory
    # stop when you get to the " -----"
d149 18
a166 2
    if (substr($_, 0, 25) eq '<h2>Directory Tree Report') {
	$visible=1;
a167 3
    $dtr_header .= $_ if $visible;
    last if ($visible && substr($_, 0, 3) eq ' --');
}
d169 40
a208 8
$visible = '';
while (<REP>) {
    if (substr($_, 25, 1) eq '/') {
	last if $visible;
	$dirname = substr($_, 26);
	if ($dirname =~ m|^\~$webdir/|) {
	    $visible = 1;
	    print $dtr_header;
d211 2
a212 29
    print $_ if $visible;
}
close REP;
if ($visible) {				# found it
    print "</pre><hr width=80%>\n$summary";

} else {				# not found
    print <<EOM;
<h2>Directory $webdir not found in stat file</h2>
Use the <a href="/dir/">online directory</a> if you're not usre of the
person's username.<p>

This search interface will only find public_html directories.
If you are looking for stats on a directory that is part of ColumbiaWeb,
check the <a href="/httpd/reports/">Web Usage Statistics</a> home page.
EOM
}

print <<EOM;
<p>This report was created by <a href="http://www.columbia.edu/httpd/getstats/">getstats 2.0</a><p>
<hr width=80%><dl><dt>Academic Information Systems
<dd>Help Line: <code>212 854.4854</code><br>
Email:
<code><a href=/acis/email-consultant.html>consultant@@columbia.edu</a></code>
</body>
EOM
exit;

########################################
d216 1
a216 1
sub err_badform {				# general error message
d218 3
a220 3
    print "<head><title>Generic Stats Error</title></head>";
    print "<body><h1>Generic Stats Error</h1>";
    print "$mess</body>";
d225 8
a232 14
# reads the weeklist file to map weeknum into repdate
sub get_repdate {
    local ($weeknumber) = @@_;
    local ($thisweek) = 0;
    if (!open(DATES, "<$WEEKLIST")) {die "Can't open weeklist because $!"}
    while (<DATES>) {
	if ($weeknumber == $thisweek) {
	    chop;
	    return $_;			# success
	}
	$thisweek++;
    }
    close DATES;
    return '';				# not found
@
