<?
//
// Reference page monitor
// Aug 3, 2001
//
// Revised:
// 2001 Aug 10: Add "remote address" to logged information
// 2002 Mar 01: Add feature to exclude one machine name from logging
//      Apr 29: Notification message includes most recent log results
//
//
// External links on reference page are monitored with this script.
// Links should be written as follows:
//
//	refpage.php3?cat=CAT&dest=URL
//
// where "CAT" is replaced by a category name, and "URL" is replaced 
// with the destination URL. The script logs the info about the user
// and where they clicked, and sends an email notification periodically,
// e.g., every 10 clicks.
//

// BEGIN USER CONFIGURATION

// Filenames for log file and counter file
$logname = "counters/refpagelog.txt";
$cntname = "counters/refpagecnt.txt";

// Increment for email notification 
$incval = 100;

// Machine name to exclude from logging
$exclude = "doering3.institute.rose-hulman.edu";
$exclude = " "; // debugging only

// END USER CONFIG

// Record the day and time
$today = date("y.m.d_G:i");

// Record the user info
$user = "$REMOTE_HOST ($REMOTE_ADDR)";

// Do logging for all users except exluded machine name
if (strcmp($exclude,$REMOTE_HOST)) {

	// Append info to a file
	$fp = fopen ($logname, "a");
	fwrite ($fp, "$today\t$user\t$cat\t$dest\n");
	fclose ($fp);

	// Update counter
	$counterval = 0; 
	$fp = fopen ($cntname, "r"); 
	$counterval = fread ($fp, filesize ($cntname));
	fclose( $fp ); 
	$counterval = (integer)$counterval + 1; 
	$fp = fopen( $cntname,"w"); 
	fwrite( $fp, $counterval); 
	fclose( $fp ); 

        // Send email note whenever counter rolls over to another increment
        if ($counterval % $incval == 0) {
                $body = "Resource Page has had another $incval clicks ($counterval to date):\n\n";
                $body = $body.`/usr/bin/tail -$incval $logname`;
                $from = "From: Resource.Page\r\n";
                $subj = "$counterval Resource Page clicks to date";
                $to = "Ed.Doering@Rose-Hulman.Edu";

                mail ($to, $subj, $body, $from);
        }

}

// Redirect the user to destination URL
header("Location: $dest");

?>