# Utils package # Loads all config files, and supplies common functions package Globals; use strict; use URI::Escape; use URI::URL; use HTML::Entities (); use CGI; use LWP::UserAgent; require LWP; use URI; use vars qw(@ISA @EXPORT @EXPORT_OK); use vars qw(@MUSIC @VIDEO @CUSTOM @SHORTCUT $CGIROOT $APPNAME $MINTRAWL %PLATTERS %ALLPLATTERS $VERSION $debug $delim $target $tracklink $audioRocketman $videoRocketman $bgcolour); require Exporter; @ISA = qw(Exporter); @EXPORT = qw(makeUnNice makeNice makeHTML getFile openTemplate getABCs makeJs getArtistString makeTrackPlaylist makeTrackURL getPlatterStats); @EXPORT_OK = qw(@MUSIC @VIDEO @CUSTOM @SHORTCUT $CGIROOT $APPNAME $MINTRAWL %PLATTERS %ALLPLATTERS $VERSION $delim $debug $target $audioRocketman $videoRocketman $bgcolour); $CGIROOT = "localhost"; $APPNAME = "distroplat"; $VERSION = "£3 6s 0d"; $MINTRAWL = 30; $debug = 0; $delim = chr(255); open(CONF, "../data/distroplat.conf") or die "No config file found"; my $list; while () { s/#.*//; chomp; my $line = $_; if (length($line) == 0) { next } if ($line =~/^\s*\[music\]/i) { $list = \@MUSIC; next } if ($line =~/^\s*\[video\]/i) { $list = \@VIDEO; next } if ($line =~/^\s*\[shortcut\]/i) { $list = \@SHORTCUT; next } if ($line =~/^\s*\[customfolders\]/i) { $list = \@CUSTOM; next } if ($line =~/^\s*cgiroot\s*=\s*(.*)/i) { $CGIROOT = $1; next } if ($line =~/^\s*appname\s*=\s*(.*)/i) { $APPNAME = $1; next } if ($line =~/^\s*colour\s*=\s*(.*)/i) { $bgcolour = $1; next } if ($line =~/^\s*mintrawl\s*=\s*(.*)/i) { $MINTRAWL = $1; next } if ($line =~/^\s*audioRocketman\s*=\s*(.*)/i) { $audioRocketman = $1; next } if ($line =~/^\s*videoRocketman\s*=\s*(.*)/i) { $videoRocketman = $1; next } if ($line =~/^\s*debug/i) { $debug = 1; next } push @$list, $line; } if ($debug) { print "HTTP/1.0 200 OK\nContent-type: text/plain\n\n"; } close CONF; # Init CGI my $query = CGI::new(); # Load server file my $exclude = $query->cookie('exclude'); open ( SERVERS, "../data/servers.txt" ) or die "No servers file found"; while ( ) { chomp; my @INLINE = split ( / /, $_ ); my $NAME = $INLINE[ 0 ]; my $URL = $INLINE[ 1 ]; if ($URL =~ /\/$/) { chop $URL; } unless ($exclude =~ /$NAME,/) { $PLATTERS{ $NAME } = $URL; } $ALLPLATTERS{ $NAME } = $URL; } close SERVERS; # target makes users with small screens open searches and artists full frame # instead of in a window $target = $query->cookie('target'); $target = 'iframe' unless $target; # abcs are loaded in a function because it's expensive and not used much sub getABCs { my %ABCS; open (ABC, "../inventory/abcs.txt"); while () { chop; my @thisabc = split($delim); $ABCS{$thisabc[0]} = $thisabc[1]; } close(ABC); return %ABCS; } # Unified MakeNice/UnNice sub makeUnNice( @_ ) { my $UNNICE = $_[ 0 ]; $UNNICE = uri_escape($UNNICE); # These things break reglier expressions $UNNICE =~ s/\(/%28/g; $UNNICE =~ s/\)/%29/g; $UNNICE =~ s/\&/%26/g; $UNNICE =~ s/\+/%2B/g; $UNNICE =~ s/%2F/\//gi; # some versions of uri_escape escape forward slashes return $UNNICE; } sub makeNice( @_ ) { my $NICE; $NICE = uri_unescape($_[0]); $NICE =~ s/([^\/]*),\sThe$/The $1/gi; $NICE =~ s/(^|\/)\d{2} - /$1/g; $NICE =~ s/\.($_)$/ (Video)/gi foreach ( @VIDEO ) ; $NICE =~ s/\.($_)$//gi foreach ( @MUSIC ) ; $NICE =~ s/\.($_)$//gi foreach ( @SHORTCUT ) ; $NICE =~ s/^Singles\//Single - /; return $NICE; } sub makeJs( @_ ) { my ($NICE) = @_; $NICE =~ s/'/\\'/g; return $NICE; } sub makeHTML( @_ ) { my ($NICE) = @_; $NICE = $NICE; $NICE = HTML::Entities::encode($NICE); $NICE =~ s/\s(\(.*\))$/ <\/a> $1<\/i>/; return $NICE; } sub getFile( @_ ) { my ($INURL) = @_; my $INFO; my $HEADER = new HTTP::Headers( 'Accept' => 'text/plain', 'User-Agent' => 'PlatterTwaartter/1.0' ); my $URL = new URI::URL( $INURL ); my $REQUEST = new HTTP::Request( 'GET', $URL, $HEADER ); my $UAGENT = new LWP::UserAgent; my $RESPONSE = $UAGENT -> request( $REQUEST ); # Did we get a 200? # if ( $RESPONSE -> is_success ) { return $RESPONSE -> content; } return "Error fetching ".$INURL; } # opens a template file and returns it as a string # sub openTemplate( @_ ) { my $TEMPLATE; open FILE, "../templates/".$_[ 0 ].".template" or die "Could not find template ".$_[0]; while( ) { $TEMPLATE .= $_; } close FILE; return $TEMPLATE; } sub getArtistString( @_ ) { my ($folder, $trackartist) = @_; my $artist; foreach (@CUSTOM) { $folder = undef if /^$folder$/ } if ($folder && $trackartist) { $artist = "$folder & $trackartist" } elsif ($folder) { $artist = $folder } elsif ($trackartist) { $artist = $trackartist } else {$artist = 'Unknown'}; return $artist; } sub makeTrackPlaylist( @_ ) { my ($folder, $trackartist, $album, $track, $platter) = @_; my $linkartist = $folder; $linkartist= $trackartist if (length $trackartist > 0); $album= 'Miscellany' if (length $album == 0); return "search.plx?artist=".makeUnNice($linkartist)."&album=".makeUnNice($album)."&title=".makeUnNice($track)."&exact=1&searchFor=track&inv=$platter&filter=music,video,shortcut"; } sub makeTrackURL( @_ ) { my ($platter, $url, $folder, $trackartist, $title) = @_; foreach (@SHORTCUT) { if ($url =~ /$_$/) { # is a shortcut my $thisartist = $trackartist; $thisartist = $folder unless (length $trackartist); return "http://$CGIROOT/$APPNAME/search.plx?artist=".makeUnNice($thisartist)."&title=".makeUnNice($title)."&exact=1&output=redir"; } } return 'http://'.$platter.$url; } sub getPlatterStats( @_ ) { my ($platter) = @_; my $artists=0; my $tracks=-1; open (INV, "../inventory/".$platter.".inv"); my $line; while ($line = ) { $tracks++ unless($line =~ /^http/); } $tracks = 0 if $tracks < 1; close INV; open (ARTIST, "../inventory/".$platter.".artist"); $artists++ while (); close ARTIST; return ($tracks, $artists); }