#!/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;
use Geo::IP;
use FileHandle;
BEGIN {
# Set up error log file
my $log_file = "error_log.txt";
use CGI::Carp qw(carpout);
open(LOG, ">>$log_file") or die "Unable to append to error log: $!";
carpout(*LOG);
}
my $commuting_photos = '';
my $work_dir = "../pics/boat_commute";
opendir(DIR,$work_dir) || die("Cannot open directory $work_dir!\n");
my @dir_contents = sort readdir(DIR);
closedir(DIR);
# Display the images as clickable thumbnails
my $file_num = 0;
foreach my $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;line-height:1.2;font-size:12px\" align=\"center\">
<a href=\"commuter_boat_slideshow.html?pic=$file_num\"><img src=\"pics/boat_commute/$filename\"
border=\"0\" hspace=\"20\" height=\"70\" title=\"$caption\" vspace=\"5\" /></a><br />
$caption
</div>";
$file_num++;
}
}
print header();
print "<html>
<body background=\"../images/myback.gif\">
<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 home router
if ($user_ip =~ /^24\.147\.133\./) { # home
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";
}
# 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);
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 = "boatpage_visit.txt";
my $access_file = "web_access.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 :: $webpage :: $user_ip :: $city, $state, $country :: $os :: $browser :: $agent :: $previous_page\n";
close(ACCESS_FILE);
return;
}