#!/usr/bin/perl ########## # the moecounter_downloadversion # all code is original and copyright john byrne april 1999 all rights reserved # modified to download and count files 2001 george shaw # this script is owned by george shaw and onetendesign, only they can modify it. got it? # this is dedicated to moe, the ultimate four-footed badass ###### cgi.pm makes the download possible with a real filename use CGI qw(:standard); $query = new CGI; #### change this variable to the path for the downloaded files #$path='http://www.lordoftherings.net/media/screensavers/'; $path="http://$ENV{SERVER_NAME}/media/screensavers/"; #define variables $dl_file=$ENV{QUERY_STRING}; $dl_location="$path$dl_file"; $countfile_dir = 'countdatafiles'; #let's hope that flock works on this unholy machine $EXCLUSIVE = 2; $UNLOCK = 8; # strip the extension #this is a little sloppy, but it works $this_datafile=$dl_file; $this_datafile =~ s/\..*//; #check the query string for shell chars if ($this_datafile =~ /[^A-Za-z0-9]/io){ exit(1); } ###### download the file print $query->redirect("$dl_location"); #unless the datafile already exists (and it needs to be updated), make a new one unless (-e "$countfile_dir/$this_datafile"){newDataFile($this_datafile);} else {existingDataFile($this_datafile);} ############ subs ################## #this makes a fresh new file and writes the number 0 and a timestamp below it. sub newDataFile { my $datafile = $_[0]; open (NDF, ">$countfile_dir/$datafile") || die "can't create the new file. $!\n"; flock NDF, $EXCLUSIVE; print NDF 1, "\n"; print NDF scalar localtime; close NDF; flock NDF, $UNLOCK; exit; } #this reads an existing file, then increments and overwrites what's inside. sub existingDataFile{ my $datafile = $_[0]; #open the existing file and read it open (EDF, "$countfile_dir/$datafile") or die "can't open the file $!\n"; @lines = ; $count_value = $lines[0]; close EDF; if ($count_value eq '' || $count_value =~ /[A-Za-z]/io){print "either the count file is funky or contents aren't numeric"; exit;} #now, increment the counter value and overwrite the datafile with freshness open (EDF, ">$countfile_dir/$datafile") or die "can't overwrite the count file. $!\n"; flock EDF, $EXCLUSIVE; print EDF ++$count_value, "\n"; print EDF scalar localtime; close EDF; flock EDF, $UNLOCK; exit; } #ok, that's it