#!/usr/bin/perl -w
#-------------------------------------------------------------------------------
# viewsongs.pl - this lists the songs I have on my iPod from a file
# written by Eric Pence
#-------------------------------------------------------------------------------
use strict;
use CGI qw(-oldstyle_urls :standard);
use CGI::Carp qw(fatalsToBrowser);
use Data::Dumper;
use IO::File;
use File::stat;
use Date::Format;

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);
}

$| = 1; # Disable buffering

my $count = 0;
my $songs = '<ul>';

# Get the stored song list from the file
my $file = IO::File->new("ipod_songs.txt", 'r') || die "Cannot open file: $!";
while(<$file>) {
my $songname = clean_up($_);
$songs .= "<li><span style='color:#333399' title=\"$songname\">$songname</span>";
$count++;
}
$file->close;
$songs .= '</ul>';

# Get last-modified date of file
my %months = (
Jan => 'January',
Feb => 'February',
Mar => 'March',
Apr => 'April',
May => 'May',
Jun => 'June',
Jul => 'July',
Aug => 'August',
Sep => 'September',
Oct => 'October',
Nov => 'November',
Dec => 'December',
);
my $fileDate = ctime(stat("ipod_songs.txt")->mtime);
my $colon1 = index $fileDate,':';
my $colon2 = rindex $fileDate,':';
my $len = length $fileDate;
my $mth = substr($fileDate,4,3);
my $month = $months{$mth};
my $changeDate = $month.substr($fileDate,7,$len-$colon1-9).','.substr($fileDate,$colon2+3);

print header();
print "<table>
<tr>
<td width=\"10\"></td>
<td nowrap>
These are the songs I have on playlists on my <i>smartphone</i>, sorted primarily by artist...
$songs
</td>
</tr>
<tr>
<td></td><td><p>$count songs - list updated $changeDate</td>
</tr>
</table>";

log_visit();

exit;

#-------------------------------------------------------------------------------
# Replace certain characters to make browser compliant
#-------------------------------------------------------------------------------
sub clean_up {
my ($str) = @_;
$str =~ s/\s&\s/ &amp; /g;
$str =~ s/'/\'/g;
$str =~ s/\//&frasl;/g;
$str =~ s/ _ / \/ /g;
return $str;
}

#---------------------------------------------------------------------------------
# Log visit
#---------------------------------------------------------------------------------
sub log_visit {
my $user_ip = $ENV{'REMOTE_ADDR'} || '';
# ignore visits from my PCs at work and home
  if (($user_ip =~ /^67\.189\.148\./) || # home
       ($user_ip =~ /^70\.192\..+/) || # my cell
       ($user_ip =~ /^12\.130\.32\./)) # Safety
   {
return;
}
my $browser = '';
my $agent = '';
$agent = $ENV{"HTTP_USER_AGENT"} || '';
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 = "access_log.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;
}