#!/usr/bin/perl -w
#---------------------------------------------------------------------------------------------------------
# boat_commute.pl - this lists the contents of a folder of photos as clickable thumbnail images
# - written by Eric Pence
# This script runs in the cgi-bin directory, so paths to images on the webpage must be relative to that location.
# The output is displayed in the root directory via Ajax (view source of webpage in browser to see this method),
# so created links are relative to that location.
# The files are named in the format 'a-some text.jpg', where 'a' is a letter of the alphabet or group of letters
# followed by a dash. This allows me to insert new files or change the order of the display by renaming files.
# The caption under the displayed thumbnail is parsed from the file name (between '-' and '.jpg').
#---------------------------------------------------------------------------------------------------------
use strict;
use CGI qw(-oldstyle_urls :standard);
use CGI::Carp qw(fatalsToBrowser);
use IO::File;

# Define global variables
use vars qw($curr_dir $log_file);

#---------------------------------------------------------------------------------------------------------
# Grab the current directory from $0. The following regular expression
# looks for 'CURR_DIR\xxxxx.xxx' or 'CURR_DIR/xxxxx.xxx'. Place it in
# the BEGIN block so we can use it to include modules
#---------------------------------------------------------------------------------------------------------
BEGIN {
$0 =~ '(.*[\\\/])\w+\.\w+$';
$curr_dir = $1 || "./";

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

use lib ( $curr_dir );
use FileHandle;

my $filename = '';
my $commuting_photos = '';
my @dir_contents = '';
my $work_dir = $curr_dir."../pics/boat_commute";

opendir(DIR,$work_dir) || die("Cannot open directory $work_dir!\n");
@dir_contents = sort readdir(DIR);
closedir(DIR);

# Display the images as clickable thumbnails
foreach $filename (@dir_contents) {
if (lc($filename) =~ /\.jpg/) {
# Extract caption from filename
  $filename =~ /^(.+-)(.+)(\.jpg)/;
  my $caption = $2;
$commuting_photos = "$commuting_photos
<div style=\"float:left;width:140px;height:125px\" align=\"center\">
<a href=\"pics/boat_commute/$filename\"><img src=\"pics/boat_commute/$filename\"
border=\"0\" hspace=\"20\" height=\"70\" title=\"$caption\" /></a><br />
$caption
</div>";
}
}

print header();
print "<html>
<body background=\"../images/myback.gif\">
<img src=\"images/clearspacer.gif\" width=\"15\" /><b>Commuting by water from Hingham to Boston...</b> (click to enlarge)
<table cellpadding=\"15\">
<tr>
<td width=\"5\"></td>
<td valign=\"top\">
$commuting_photos
</td>
<td width=\"5\"></td>
</tr>
</table>
</body>
</html>";

log_visit();

exit;

#---------------------------------------------------------------------------------
# Log visit
#---------------------------------------------------------------------------------
sub log_visit {
my $user_ip = $ENV{'REMOTE_ADDR'} || '';
# ignore visits from my PCs at work and home
if (($user_ip =~ /^38\.97\.67\./) || ($user_ip =~ /^24\.218\.31\./)) {
return;
}
my $agent = '';
$agent = $ENV{"HTTP_USER_AGENT"} || '';
my $browser = '';
if ($agent =~ /Firefox/) {
my $loc = index($agent, 'Firefox');
$browser = substr($agent,$loc,11);
substr($browser,7,1) = ' ';
}
elsif ($agent =~ /MSIE/) {
my $loc = index($agent, 'MSIE');
$browser = substr($agent,$loc+2,6);
}
elsif ($agent =~ /Opera/) {
my $loc = index($agent, 'Opera');
$browser = substr($agent,$loc,10);
substr($browser,5,1) = ' ';
}
elsif ($agent =~ /Chrome/) {
my $loc = index($agent, 'Chrome');
$browser = substr($agent,$loc,17);
substr($browser,6,1) = ' ';
}
else {
$browser = "Other";
}
my $os = '';
if ($agent =~ /Windows NT 5.1/) {
$os = "Windows XP";
}
elsif ($agent =~ /Windows NT 6.0/) {
$os = "Windows Vista";
}
elsif ($agent =~ /Windows NT 5.0/) {
$os = "Windows 2000";
}
elsif ($agent =~ /Windows NT 4.0/) {
$os = "Windows NT.";
}
elsif ($agent =~ /Windows NT 5.2/) {
$os = "Windows Server 2003";
}
elsif ($agent =~ /Win 9x 4.90/) {
$os = "Windows ME";
}
elsif (($agent =~ /Windows 98/) || ($agent =~ /Win98/)) {
$os = "Windows 98.";
}
elsif ($agent =~ /Linux/) {
$os = "Linux";
}
elsif ($agent =~ /Mac/i) {
$os = "MacOS";
}
else {
$os = "$agent";
}
my $access_file = "boatpage_visit.txt";
open(ACCESS_FILE, ">>$access_file") || die "Can't open '$access_file'";
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = $year + 1900;
if ($min < 10) {$min = "0".$min;}
$mon++;
my $timeData = "$mon/$mday/$year - $hour:$min";
print ACCESS_FILE "$timeData :: $user_ip :: $browser :: $os\n";
close(ACCESS_FILE);
return;
}