#!/usr/bin/perl -w
#-------------------------------------------------------------------------------
# log_visits.pl - updates a file whenever someone visits a page that calls this
# written by Eric Pence
#-------------------------------------------------------------------------------
use strict;
use CGI qw(-oldstyle_urls :standard);
use CGI::Carp qw(fatalsToBrowser);
use IO::File;
use Geo::IP;

my $access_file = "web_access.txt";
open(ACCESS_FILE, "$access_file") || die "Can't open $access_file";
print ACCESS_FILE "Starting log_visits.pl\n";
close(ACCESS_FILE);

BEGIN {
# Define global variables
my $curr_dir = '';

$0 =~ '(.*[\\\/])\w+\.\w+$';
$curr_dir = $1 || "./";

# Set up error log file
use CGI::Carp qw(carpout);
my $error_log = "error_log.txt";
open(LOG, ">>$error_log") or die "Unable to append to error log: $!";
carpout(*LOG);
}

# Get webpage
my $q = new CGI;
my $webpage = $q->param('webpage') || '';

# Get IP-address
my $user_ip = $ENV{'REMOTE_ADDR'} || '';

# Don't log home router
if ($user_ip =~ /^24\.147\.133\./) {

# Prompt here if this is home
#window.alert("Running log_visits.pl!");
print "Running log_visits.pl!";
<STDIN>;

exit;
}

# Get user-agent string
my $agent = $ENV{"HTTP_USER_AGENT"} || '';

# Don't log bots
if (($agent =~ /bot/i) || ($agent =~ /spider/) || ($agent =~ /BingPreview/)) {
exit;
}

my $timeData = "";

# Get operating system
my $os = '';
if ($agent =~ /Windows NT 10/i) {
$os = "Windows 10";
}
elsif ($agent =~ /Windows NT 6.1/i) {
$os = "Windows 7";
}
elsif ($agent =~ /Windows NT 6.2/i) {
$os = "Windows 8";
}
elsif ($agent =~ /Windows NT 6.3/i) {
$os = "Windows 8.1";
}
elsif ($agent =~ /Windows NT 5.1/i) {
$os = "Windows XP";
}
elsif ($agent =~ /Windows NT 6.0/i) {
$os = "Windows Vista";
}
elsif ($agent =~ /Windows NT 5.0/i) {
$os = "Windows 2000";
}
elsif ($agent =~ /Windows NT 4.0/i) {
$os = "Windows NT";
}
elsif ($agent =~ /Windows NT 5.2/i) {
$os = "Windows Server 2003";
}
elsif ($agent =~ /Win 9x 4.90/i) {
$os = "Windows ME";
}
elsif (($agent =~ /Windows 98/i) || ($agent =~ /Win98/i)) {
$os = "Windows 98";
}
elsif ($agent =~ /iPhone/i) {
$os = "iPhone";
}
elsif ($agent =~ /iPad/i) {
$os = "iPad";
}
elsif ($agent =~ /Mac/i) {
$os = "MacOS";
}
elsif ($agent =~ /Android/i) {
my $loc = index($agent, 'Android');
$os = substr($agent,$loc,13);
substr($os,7,1) = ' ';
}
elsif ($agent =~ /Linux/i) {
$os = "Linux";
}
elsif ($agent =~ /X11\; CrOS/) {
$os = "Chrome OS";
}
elsif ($agent =~ /^Mozilla\/5\.0/) {
$os = "Mozilla/5.0";
}

# Get browser
my $browser = '';
if ($agent =~ /Firefox/i) {
my $loc = index($agent, 'Firefox');
$browser = substr($agent,$loc,10);
substr($browser,7,1) = ' ';
}
elsif ($agent =~ /MSIE/i) {
my $loc = index($agent, 'MSIE');
$browser = substr($agent,$loc+2,6);
}
elsif ($agent =~ /Edge/i) {
my $loc = index($agent, 'Edge');
$browser = substr($agent,$loc,7);
substr($browser,4,1) = ' ';
}
elsif ($agent =~ /Opera/i) {
my $loc = index($agent, 'Opera');
$browser = substr($agent,$loc,10);
substr($browser,5,1) = ' ';
}
elsif ($agent =~ /Chrome/i) {
my $loc = index($agent, 'Chrome');
$browser = substr($agent,$loc,9);
substr($browser,6,1) = ' ';
}
elsif ($agent =~ /Safari/i) {
my $loc = index($agent, 'Safari');
$browser = substr($agent,$loc,9);
substr($browser,6,1) = ' ';
}
elsif (($agent =~ /Trident\/7.0/i) && ($os =~ /Windows/)) {
$browser = 'IE 11.0';
}
elsif ($agent =~ /KHTML/) {
$browser = 'Konqueror';
}
elsif ($agent =~ /Wappalyzer/) {
$browser .= ' + Wappalyzer';
}
else {
$browser = "Other browser";
}

# Set name of agent if long to "Name..."
if (length($agent) > 11) {
my $length_of_word = index ($agent, ' ') - 1;
my $shortened_agent = substr( $agent, 0, $length_of_word )...';
$agent = $shortened_agent;
}

# Get previous page
my $previous_page = $ENV{'HTTP_REFERER'} || '';
if ($previous_page =~ /penceland/) { $previous_page = ''; }
#If previous page is current page set to blank
if ($previous_page =~ /$webpage/) { $previous_page = ''; }

# Get location by IP-address
#my $gi = Geo::IP->open("/usr/local/share/GeoIP/GeoIPCity.dat", GEOIP_STANDARD); # this gets a "strict subs" error
#my $rec_by_addr = $gi->record_by_addr($user_ip);
#my $city = $rec_by_addr->city;
#my $state = $rec_by_addr->region_name;
#my $country = $rec_by_addr->country_name;

my $access_file = "web_access.txt";
open(ACCESS_FILE, ">>$access_file") || die "Can't open '$access_file'";
get_timedata();
print ACCESS_FILE "$timeData :: $webpage :: $user_ip :: $os :: $browser :: $agent :: $previous_page\n";
#print ACCESS_FILE "$timeData :: $webpage :: $user_ip :: $city, $state, $country :: $os :: $browser :: $agent :: $previous_page\n";
close(ACCESS_FILE);

exit;

#---------------------------------------------------------------------------------
# Format date and time
#---------------------------------------------------------------------------------
get_timedata {
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = $year + 1900;
if ($min < 10) {$min = "0".$min;}
$mon++;
$timeData = "$mon/$mday/$year - $hour:$min";
return;
}